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

Access violation when running with pytest #87

Open
djromberg opened this issue Jun 13, 2022 · 2 comments
Open

Access violation when running with pytest #87

djromberg opened this issue Jun 13, 2022 · 2 comments

Comments

@djromberg
Copy link

Without pytest, everything works like a charm. However, when running some tests with pytest, I get the following error:

========================================================= test session starts ==========================================================
platform win32 -- Python 3.9.12, pytest-7.1.2, pluggy-1.0.0
rootdir: C:\Users\dromberg\Projects\Schedool\schedool-service
plugins: anyio-3.5.0
collecting ... Windows fatal exception: access violation

Current thread 0x00001634 (most recent call first):
  File "C:\Users\dromberg\AppData\Local\pypoetry\Cache\virtualenvs\schedool-service-gPSd8MaG-py3.9\lib\site-packages\jpype\_core.py", line 218 in startJVM
  File "C:\Users\dromberg\AppData\Local\pypoetry\Cache\virtualenvs\schedool-service-gPSd8MaG-py3.9\lib\site-packages\optapy\optaplanner_java_interop.py", line 334 in init
  File "C:\Users\dromberg\AppData\Local\pypoetry\Cache\virtualenvs\schedool-service-gPSd8MaG-py3.9\lib\site-packages\optapy\optaplanner_java_interop.py", line 395 in ensure_init
  File "C:\Users\dromberg\AppData\Local\pypoetry\Cache\virtualenvs\schedool-service-gPSd8MaG-py3.9\lib\site-packages\optapy\types\types.py", line 4 in <module>
...

The stack trace shown here is shortened for readability. I was able to reproduce the issue with the following code and command:

Python

# file tests/test_code.py
from optapy.types import HardSoftScore

Command

pytest .\tests\test_code.py

It seems that starting the JVM fails somehow. Let me know if I can add anything more to help, I am no Java expert though.

@Christopher-Chianelli
Copy link
Contributor

What happens when you invoke pytest as pytest --import-mode=importlib .\tests\test_code.py?

PyTest is used for OptaPy tests:

pytest --import-mode=importlib
; Without changing import-mode, I get a ClassNotFoundException. Changing import-mode to importlib, all tests pass successfully and no error is logged.
JPype has a JImport module that adds an additional module loader for java packages and classes: https://jpype.readthedocs.io/en/latest/imports.html#jimport . This module is used with OptaPy, so there is a chance the standard import-mode of Pytest does not like it.

@djromberg
Copy link
Author

Thanks for the hint, but this option does not change anything. However, while reading through the JPype documentation, I found the section Errors reported by Python fault handler. It suggests to deactivate the Python fault handler when working with the JVM.

When I add the suggested fixture, my test runs without any error. Maybe OptaPy also makes use of this or something similar? Feel free to close this issue as it works for me now.

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

No branches or pull requests

2 participants