Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Mingun
Copy link
Contributor

@Mingun Mingun commented Apr 8, 2024

Use the non-deprecated method in Python 3 and fix tests in Python >=3.12:

  • Python 2 has only assertRaisesRegexp
  • Python <=3.11 have both assertRaisesRegex and (deprecated) assertRaisesRegexp
  • Python >=3.12 has only assertRaisesRegex

Should fix the following tests on https://ci.kaitai.io/

  • EofExceptionU4
  • EosExceptionBytes
  • EosExceptionU4
  • ExprBytesNonLiteral

Closes kaitai-io/kaitai_struct#1104

@Mingun Mingun changed the title Python: and compatibility layer for Python 2 & <=3.11 Python: add compatibility layer for Python 2 & <=3.11 Apr 11, 2024
@Mingun Mingun force-pushed the fix-python-3.12-tests branch from 4875b4c to 2e117fa Compare April 14, 2024 03:44
Comment on lines 6 to 11
# 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)
Copy link
Member

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?

Copy link
Contributor Author

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)

@Mingun Mingun force-pushed the fix-python-3.12-tests branch from 2e117fa to 47fe558 Compare April 15, 2024 16:33
@Mingun
Copy link
Contributor Author

Mingun commented Apr 15, 2024

Ok, I found a way to run python tests on Windows (7):

  • Python 3.10 was already installed
  • run pip install xmlrunner (because it was missed)
  • change path delimiter from : to ; in ci-python
  • run ./ci-python in Git Bash

I implemented the check in a common file which was placed to the spec/python/extra folder where other such files are located (should we rename spec/python/extra to helpers/python?).

Running tests reports two classes of warnings, I've fix them too.

@Mingun Mingun requested a review from GreyCat April 15, 2024 16:39
@Mingun Mingun force-pushed the fix-python-3.12-tests branch from 47fe558 to 85d54b2 Compare April 20, 2024 18:50
@Mingun Mingun force-pushed the fix-python-3.12-tests branch 2 times, most recently from 86079c1 to a8cdbcf Compare July 16, 2024 13:37
@Mingun Mingun force-pushed the fix-python-3.12-tests branch from a8cdbcf to eb16811 Compare September 13, 2024 19:29
Mingun added 2 commits October 5, 2024 21:13
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$"):
@Mingun Mingun force-pushed the fix-python-3.12-tests branch from eb16811 to c99c848 Compare October 5, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python: Fix missing assertRaisesRegexp method in Python 3.12
2 participants