From 99286d0ded32ab835698e8606cd2bbe80c69a639 Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sat, 15 Feb 2025 14:04:59 +0100 Subject: [PATCH 1/3] remove IS_PYTHON2 from tests too --- lupa/tests/test.py | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/lupa/tests/test.py b/lupa/tests/test.py index dab1a36c..fe4368ce 100644 --- a/lupa/tests/test.py +++ b/lupa/tests/test.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function - import gc import operator import os.path @@ -20,7 +18,6 @@ except (ImportError, AttributeError): IS_PYPY = False -IS_PYTHON2 = sys.version_info[0] < 3 not_in_pypy = unittest.skipIf(IS_PYPY, "test not run in PyPy") try: @@ -29,11 +26,6 @@ def _next(o): return o.next() -unicode_type = type(b'abc'.decode('ASCII') if IS_PYTHON2 else 'abc') - -if IS_PYTHON2: - unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp - class SetupLuaRuntimeMixin(object): lua_runtime_kwargs = {} @@ -170,12 +162,10 @@ def test_eval_error_message_decoding(self): try: self.lua.eval('require "UNKNOWNöMODULEäNAME"') except self.lupa.LuaError: - error = ('%s'.decode('ASCII') if IS_PYTHON2 else '%s') % sys.exc_info()[1] + error = '%s'.decode('ASCII') % sys.exc_info()[1] else: self.fail('expected error not raised') expected_message = 'module \'UNKNOWNöMODULEäNAME\' not found' - if IS_PYTHON2: - expected_message = expected_message.decode('UTF-8') self.assertTrue(expected_message in error, '"%s" not found in "%s"' % (expected_message, error)) @@ -947,7 +937,7 @@ def test_lua_error_after_intercepted_python_exception(self): def test_attribute_filter(self): def attr_filter(obj, name, setting): - if isinstance(name, unicode_type): + if isinstance(name, str): if not name.startswith('_'): return name + '1' raise AttributeError('denied') @@ -1124,7 +1114,7 @@ class Y(object): __a = 3 def attr_getter(self, obj, name): - if not isinstance(name, unicode_type): + if not isinstance(name, str): raise AttributeError('bad type for attr_name') if isinstance(obj, self.X): if not name.startswith('_'): @@ -1830,13 +1820,11 @@ def tearDown(self): gc.collect() test_string = '"abcüöä"' - if IS_PYTHON2: - test_string = test_string.decode('UTF-8') def _encoding_test(self, encoding, expected_length): lua = self.lupa.LuaRuntime(encoding) - self.assertEqual(unicode_type, + self.assertEqual(str, type(lua.eval(self.test_string))) self.assertEqual(self.test_string[1:-1], @@ -2091,10 +2079,7 @@ def mandelbrot(i, lua_func): # plausability checks - make sure it's not all white or all black self.assertEqual('\0'.encode('ASCII')*(image_size//8//2), result_bytes[:image_size//8//2]) - if IS_PYTHON2: - self.assertTrue('\xFF' in result_bytes) - else: - self.assertTrue('\xFF'.encode('ISO-8859-1') in result_bytes) + self.assertTrue('\xFF'.encode('ISO-8859-1') in result_bytes) # if we have PIL, check that it can read the image ## try: From e46495dc8c568d27212f6245feca8d900e03f59a Mon Sep 17 00:00:00 2001 From: scoder Date: Sat, 15 Feb 2025 17:37:01 +0100 Subject: [PATCH 2/3] Simplify code --- lupa/tests/test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lupa/tests/test.py b/lupa/tests/test.py index fe4368ce..a4271c8c 100644 --- a/lupa/tests/test.py +++ b/lupa/tests/test.py @@ -162,7 +162,7 @@ def test_eval_error_message_decoding(self): try: self.lua.eval('require "UNKNOWNöMODULEäNAME"') except self.lupa.LuaError: - error = '%s'.decode('ASCII') % sys.exc_info()[1] + error = str(sys.exc_info()[1]) else: self.fail('expected error not raised') expected_message = 'module \'UNKNOWNöMODULEäNAME\' not found' @@ -2079,7 +2079,7 @@ def mandelbrot(i, lua_func): # plausability checks - make sure it's not all white or all black self.assertEqual('\0'.encode('ASCII')*(image_size//8//2), result_bytes[:image_size//8//2]) - self.assertTrue('\xFF'.encode('ISO-8859-1') in result_bytes) + self.assertTrue(b'\xFF' in result_bytes) # if we have PIL, check that it can read the image ## try: From 9744829f7f89f297fd90a3319e29395b193e2784 Mon Sep 17 00:00:00 2001 From: scoder Date: Sat, 15 Feb 2025 17:46:13 +0100 Subject: [PATCH 3/3] More code cleanup --- lupa/tests/test.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lupa/tests/test.py b/lupa/tests/test.py index a4271c8c..674229b7 100644 --- a/lupa/tests/test.py +++ b/lupa/tests/test.py @@ -161,13 +161,12 @@ def test_eval_error_cleanup(self): def test_eval_error_message_decoding(self): try: self.lua.eval('require "UNKNOWNöMODULEäNAME"') - except self.lupa.LuaError: - error = str(sys.exc_info()[1]) + except self.lupa.LuaError as exc: + error = str(exc) else: self.fail('expected error not raised') expected_message = 'module \'UNKNOWNöMODULEäNAME\' not found' - self.assertTrue(expected_message in error, - '"%s" not found in "%s"' % (expected_message, error)) + self.assertIn(expected_message, error) def test_execute(self): self.assertEqual(2, self.lua.execute('return 1+1'))