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 3 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
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
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
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$"):
…e='rb' closefd=True>

where ... = src/fixed_struct.bin
          = src/switch_opcodes.bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants