From e99c747e6be8d33c08a83407fbca7a490b146a4a Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 8 Apr 2024 20:38:19 +0500 Subject: [PATCH 1/2] Python: and compatibility layer for Python 2 & <=3.11 Python 2 has only assertRaisesRegexp Python <=3.11 have both assertRaisesRegex and (deprecated) assertRaisesRegexp Python >=3.12 has only assertRaisesRegex --- spec/python/extra/base_test.py | 17 +++++++++++++++++ spec/python/test_eof_exception_bytes.py | 7 +++---- spec/python/test_eof_exception_u4.py | 7 +++---- spec/python/test_eos_exception_bytes.py | 7 +++---- spec/python/test_eos_exception_u4.py | 7 +++---- 5 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 spec/python/extra/base_test.py diff --git a/spec/python/extra/base_test.py b/spec/python/extra/base_test.py new file mode 100644 index 000000000..5742315c5 --- /dev/null +++ b/spec/python/extra/base_test.py @@ -0,0 +1,17 @@ +import unittest + +class BaseTest(unittest.TestCase): + """ + Base class for some KaitaiStruct tests that uses RegEx assertions. + + - Python 2 has only `assertRaisesRegexp` method + - Python 3.11 and below have both `assertRaisesRegex` and `assertRaisesRegexp` + - Python 2.12 and above has only `assertRaisesRegex` + + The tests run on both Python 2 & 3, so this base class adds missing method for + compatibility, therefore tests written for latest Python will work on older versions. + """ + def __init__(self, *args, **kwargs): + super(BaseTest, self).__init__(*args, **kwargs) + if not hasattr(unittest.TestCase, 'assertRaisesRegex'): + setattr(unittest.TestCase, 'assertRaisesRegex', unittest.TestCase.assertRaisesRegexp) \ No newline at end of file diff --git a/spec/python/test_eof_exception_bytes.py b/spec/python/test_eof_exception_bytes.py index 1ca959e3b..31a42d681 100644 --- a/spec/python/test_eof_exception_bytes.py +++ b/spec/python/test_eof_exception_bytes.py @@ -1,9 +1,8 @@ -import unittest +from base_test import BaseTest from eof_exception_bytes import EofExceptionBytes -import kaitaistruct -class TestEofExceptionBytes(unittest.TestCase): +class TestEofExceptionBytes(BaseTest): def test_eof_exception_bytes(self): - with self.assertRaisesRegexp(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): + with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): with EofExceptionBytes.from_file('src/term_strz.bin') as r: pass diff --git a/spec/python/test_eof_exception_u4.py b/spec/python/test_eof_exception_u4.py index 43824143a..88838d085 100644 --- a/spec/python/test_eof_exception_u4.py +++ b/spec/python/test_eof_exception_u4.py @@ -1,9 +1,8 @@ -import unittest +from base_test import BaseTest from eof_exception_u4 import EofExceptionU4 -import kaitaistruct -class TestEofExceptionU4(unittest.TestCase): +class TestEofExceptionU4(BaseTest): def test_eof_exception_u4(self): - with self.assertRaisesRegexp(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): + with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): with EofExceptionU4.from_file('src/term_strz.bin') as r: pass diff --git a/spec/python/test_eos_exception_bytes.py b/spec/python/test_eos_exception_bytes.py index bb1e14a03..899eeb841 100644 --- a/spec/python/test_eos_exception_bytes.py +++ b/spec/python/test_eos_exception_bytes.py @@ -1,9 +1,8 @@ -import unittest +from base_test import BaseTest from eos_exception_bytes import EosExceptionBytes -import kaitaistruct -class TestEosExceptionBytes(unittest.TestCase): +class TestEosExceptionBytes(BaseTest): def test_eos_exception_bytes(self): - with self.assertRaisesRegexp(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): + with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): with EosExceptionBytes.from_file('src/term_strz.bin') as r: pass diff --git a/spec/python/test_eos_exception_u4.py b/spec/python/test_eos_exception_u4.py index 7430f65ef..edf5bfb79 100644 --- a/spec/python/test_eos_exception_u4.py +++ b/spec/python/test_eos_exception_u4.py @@ -1,9 +1,8 @@ -import unittest +from base_test import BaseTest from eos_exception_u4 import EosExceptionU4 -import kaitaistruct -class TestEosExceptionU4(unittest.TestCase): +class TestEosExceptionU4(BaseTest): def test_eos_exception_u4(self): - with self.assertRaisesRegexp(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): + with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): with EosExceptionU4.from_file('src/term_strz.bin') as r: pass From c99c848df843799717ea7bb6ff0a55ea427abf26 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 15 Apr 2024 21:29:48 +0500 Subject: [PATCH 2/2] Python: fix DeprecationWarning: invalid escape sequence '\d' with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): --- spec/python/test_eof_exception_bytes.py | 2 +- spec/python/test_eof_exception_u4.py | 2 +- spec/python/test_eos_exception_bytes.py | 2 +- spec/python/test_eos_exception_u4.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/python/test_eof_exception_bytes.py b/spec/python/test_eof_exception_bytes.py index 31a42d681..8ae4ec81b 100644 --- a/spec/python/test_eof_exception_bytes.py +++ b/spec/python/test_eof_exception_bytes.py @@ -3,6 +3,6 @@ class TestEofExceptionBytes(BaseTest): def test_eof_exception_bytes(self): - with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): + with self.assertRaisesRegex(EOFError, r"^requested \d+ bytes, but only \d+ bytes available$"): with EofExceptionBytes.from_file('src/term_strz.bin') as r: pass diff --git a/spec/python/test_eof_exception_u4.py b/spec/python/test_eof_exception_u4.py index 88838d085..9d6447b7a 100644 --- a/spec/python/test_eof_exception_u4.py +++ b/spec/python/test_eof_exception_u4.py @@ -3,6 +3,6 @@ class TestEofExceptionU4(BaseTest): def test_eof_exception_u4(self): - with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): + with self.assertRaisesRegex(EOFError, r"^requested \d+ bytes, but only \d+ bytes available$"): with EofExceptionU4.from_file('src/term_strz.bin') as r: pass diff --git a/spec/python/test_eos_exception_bytes.py b/spec/python/test_eos_exception_bytes.py index 899eeb841..7ab40463b 100644 --- a/spec/python/test_eos_exception_bytes.py +++ b/spec/python/test_eos_exception_bytes.py @@ -3,6 +3,6 @@ class TestEosExceptionBytes(BaseTest): def test_eos_exception_bytes(self): - with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): + with self.assertRaisesRegex(EOFError, r"^requested \d+ bytes, but only \d+ bytes available$"): with EosExceptionBytes.from_file('src/term_strz.bin') as r: pass diff --git a/spec/python/test_eos_exception_u4.py b/spec/python/test_eos_exception_u4.py index edf5bfb79..83207aa6e 100644 --- a/spec/python/test_eos_exception_u4.py +++ b/spec/python/test_eos_exception_u4.py @@ -3,6 +3,6 @@ class TestEosExceptionU4(BaseTest): def test_eos_exception_u4(self): - with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"): + with self.assertRaisesRegex(EOFError, r"^requested \d+ bytes, but only \d+ bytes available$"): with EosExceptionU4.from_file('src/term_strz.bin') as r: pass