optapy icon indicating copy to clipboard operation
optapy copied to clipboard

Access violation when running with pytest

Open djromberg opened this issue 2 years ago • 2 comments

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.

djromberg avatar Jun 13 '22 14:06 djromberg

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

PyTest is used for OptaPy tests: https://github.com/optapy/optapy/blob/5418b0226413d7baec60339bdf67de7bf8df8a4d/optapy-core/tox.ini#L14 ; 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.

Christopher-Chianelli avatar Jun 13 '22 14:06 Christopher-Chianelli

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.

djromberg avatar Jun 13 '22 15:06 djromberg