From 1675b98e74e6af0bd5edaf236a275dcb7735069e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Nov 2019 16:18:25 +1100 Subject: [PATCH] tests/stress: Add test for maximum length limit of qstrs. --- tests/stress/qstr_limit.py | 85 ++++++++++++++++++++++++++++++++++ tests/stress/qstr_limit.py.exp | 43 +++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 tests/stress/qstr_limit.py create mode 100644 tests/stress/qstr_limit.py.exp diff --git a/tests/stress/qstr_limit.py b/tests/stress/qstr_limit.py new file mode 100644 index 0000000000..90c85dae96 --- /dev/null +++ b/tests/stress/qstr_limit.py @@ -0,0 +1,85 @@ +# Test interning qstrs that go over the limit of the maximum qstr length +# (which is 255 bytes for the default configuration) + +def make_id(n, base='a'): + return ''.join(chr(ord(base) + i % 26) for i in range(n)) + +# identifiers in parser +for l in range(254, 259): + g = {} + var = make_id(l) + try: + exec(var + '=1', g) + except RuntimeError: + print('RuntimeError', l) + continue + print(var in g) + +# calling a function with kwarg +def f(**k): + print(k) +for l in range(254, 259): + try: + exec('f({}=1)'.format(make_id(l))) + except RuntimeError: + print('RuntimeError', l) + +# type construction +for l in range(254, 259): + id = make_id(l) + try: + print(type(id, (), {}).__name__) + except RuntimeError: + print('RuntimeError', l) + +# hasattr, setattr, getattr +class A: + pass +for l in range(254, 259): + id = make_id(l) + a = A() + try: + setattr(a, id, 123) + except RuntimeError: + print('RuntimeError', l) + try: + print(hasattr(a, id), getattr(a, id)) + except RuntimeError: + print('RuntimeError', l) + +# format with keys +for l in range(254, 259): + id = make_id(l) + try: + print(('{' + id + '}').format(**{id: l})) + except RuntimeError: + print('RuntimeError', l) + +# modulo format with keys +for l in range(254, 259): + id = make_id(l) + try: + print(('%(' + id + ')d') % {id: l}) + except RuntimeError: + print('RuntimeError', l) + +# import module +# (different OS's have different results so only print those that are consistent) +for l in range(150, 259): + try: + __import__(make_id(l)) + except ImportError: + if l < 152: + print('ok', l) + except RuntimeError: + if l > 255: + print('RuntimeError', l) + +# import package +for l in range(125, 130): + try: + exec('import ' + make_id(l) + '.' + make_id(l, 'A')) + except ImportError: + print('ok', l) + except RuntimeError: + print('RuntimeError', l) diff --git a/tests/stress/qstr_limit.py.exp b/tests/stress/qstr_limit.py.exp new file mode 100644 index 0000000000..516c9d7f6c --- /dev/null +++ b/tests/stress/qstr_limit.py.exp @@ -0,0 +1,43 @@ +True +True +RuntimeError 256 +RuntimeError 257 +RuntimeError 258 +{'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst': 1} +{'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu': 1} +RuntimeError 256 +RuntimeError 257 +RuntimeError 258 +abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst +abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu +RuntimeError 256 +RuntimeError 257 +RuntimeError 258 +True 123 +True 123 +RuntimeError 256 +RuntimeError 256 +RuntimeError 257 +RuntimeError 257 +RuntimeError 258 +RuntimeError 258 +254 +255 +RuntimeError 256 +RuntimeError 257 +RuntimeError 258 +254 +255 +RuntimeError 256 +RuntimeError 257 +RuntimeError 258 +ok 150 +ok 151 +RuntimeError 256 +RuntimeError 257 +RuntimeError 258 +ok 125 +ok 126 +ok 127 +RuntimeError 128 +RuntimeError 129