-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python: add compatibility layer for Python 2 & <=3.11 #115
base: master
Are you sure you want to change the base?
Conversation
4875b4c
to
2e117fa
Compare
spec/python/test_eof_exception_u4.py
Outdated
# 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 we add missing method for compatibility | ||
if not hasattr(unittest.TestCase, 'assertRaisesRegex'): | ||
setattr(unittest.TestCase, 'assertRaisesRegex', unittest.TestCase.assertRaisesRegexp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, appreciate your fix here!
Do you think we can actually extract this into a separate file not to repeat these lines everywhere?
Something akin to helpers/javascript?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about building a base class for tests, but since I don't test it (I'm too lazy to set up all the language environments that will only be needed for one thing), I was not sure that this will work and I made the simplest solution.
Something like this, but I'm not sure if constructor should have some arguments:
class BaseTest(unittest.TestCase):
def __init__():
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
setattr(unittest.TestCase, 'assertRaisesRegex', unittest.TestCase.assertRaisesRegexp)
2e117fa
to
47fe558
Compare
Ok, I found a way to run python tests on Windows (7):
I implemented the check in a common file which was placed to the Running tests reports two classes of warnings, I've fix them too. |
47fe558
to
85d54b2
Compare
86079c1
to
a8cdbcf
Compare
a8cdbcf
to
eb16811
Compare
Python 2 has only assertRaisesRegexp Python <=3.11 have both assertRaisesRegex and (deprecated) assertRaisesRegexp Python >=3.12 has only assertRaisesRegex
with self.assertRaisesRegex(EOFError, "^requested \d+ bytes, but only \d+ bytes available$"):
eb16811
to
c99c848
Compare
Use the non-deprecated method in Python 3 and fix tests in Python >=3.12:
assertRaisesRegexp
assertRaisesRegex
and (deprecated)assertRaisesRegexp
assertRaisesRegex
Should fix the following tests on https://ci.kaitai.io/
Closes kaitai-io/kaitai_struct#1104