PY-66315 PYDEVD_USE_CYTHON=YES causes ImportError with Python <=3.11

+ Tests
+ Simplified logic for custom `pytest` markers


(cherry picked from commit 45d18ed5c5fa985d15d1d4854b8822d2812dbe85)

IJ-MR-149139

GitOrigin-RevId: 107e9b3524621757937a72ee2ed186c5e31ac05d
This commit is contained in:
Pavel Karateev
2024-11-12 13:31:23 +01:00
committed by intellij-monorepo-bot
parent 5a6148be25
commit 2625838bc7
3 changed files with 154 additions and 18 deletions

View File

@@ -1,11 +1,13 @@
import os
import sys
from _pydevd_bundle.pydevd_constants import IS_PY312_OR_GREATER
use_cython = os.getenv('PYDEVD_USE_CYTHON', None)
if use_cython == 'NO':
from _pydevd_bundle import pydevd_pep_669_tracing as mod
elif use_cython == 'YES':
elif use_cython == 'YES' and IS_PY312_OR_GREATER:
from _pydevd_bundle import pydevd_pep_669_tracing_cython as mod
else:
try:
@@ -19,10 +21,10 @@ enable_pep669_monitoring = mod.enable_pep669_monitoring
global_cache_skips = mod.global_cache_skips
global_cache_frame_skips = mod.global_cache_frame_skips
def _dummy_restart_events():
pass
try:
restart_events = sys.monitoring.restart_events
except AttributeError:
def _dummy_restart_events():
pass
restart_events = _dummy_restart_events
restart_events = _dummy_restart_events

View File

@@ -1,4 +1,5 @@
import sys
from collections import namedtuple
import pytest
@@ -6,22 +7,39 @@ from pydev_tests_python.regression_check import data_regression
from pydev_tests_python.regression_check import datadir
from pydev_tests_python.regression_check import original_datadir
Marker = namedtuple("marker", "skip_condition, default_reason")
MARKERS = {
"python2": Marker(
not sys.version_info[0] == 2,
"test is only applicable for Python 2",
),
"python3": Marker(
not sys.version_info[0] == 3,
"test is only applicable for Python 3",
),
"le_python311": Marker(
not sys.version_info[:2] <= (3, 11),
"test is only applicable for Python <=3.11",
),
"ge_python312": Marker(
not sys.version_info >= (3, 12),
"test is only applicable for Python >=3.12",
),
}
def pytest_configure(config):
config.addinivalue_line("markers", "python2(reason): skip if not Python 2")
config.addinivalue_line("markers", "python3(reason): skip if not Python 3")
for marker_name, (_, default_reason) in MARKERS.items():
doc_line = "%s(reason): %s" % (marker_name, default_reason)
config.addinivalue_line("markers", doc_line)
def pytest_collection_modifyitems(config, items):
for item in items:
if 'python2' in item.keywords and sys.version_info[0] > 2:
default_reason = 'test is only applicable for Python 2'
marker = item.get_closest_marker('python2')
reason = marker.kwargs.get('reason', default_reason)
item.add_marker(pytest.mark.skip(reason=reason))
if 'python3' in item.keywords and sys.version_info[0] < 3:
default_reason = 'test is only applicable for Python 3'
marker = item.get_closest_marker('python3')
reason = marker.kwargs.get('reason', default_reason)
item.add_marker(pytest.mark.skip(reason=reason))
for marker_name, (skip_condition, default_reason) in MARKERS.items():
if marker_name in item.keywords and skip_condition:
marker = item.get_closest_marker(marker_name)
reason = marker.kwargs.get('reason', default_reason)
item.add_marker(pytest.mark.skip(reason=reason))

View File

@@ -0,0 +1,116 @@
import sys
from importlib import reload
import pytest
@pytest.fixture
def mock_env_use_cython_yes(monkeypatch):
monkeypatch.setenv("PYDEVD_USE_CYTHON", "YES")
@pytest.fixture
def mock_env_use_cython_no(monkeypatch):
monkeypatch.setenv("PYDEVD_USE_CYTHON", "NO")
@pytest.fixture
def mock_env_use_cython_none(monkeypatch):
monkeypatch.delenv("PYDEVD_USE_CYTHON", raising=False)
@pytest.fixture
def mock_env_use_cython_unexpected(monkeypatch):
monkeypatch.setenv("PYDEVD_USE_CYTHON", "HELLO")
def load_wrapper():
from _pydevd_bundle import pydevd_pep_669_tracing_wrapper as wrapper
reload(wrapper)
return wrapper
def assert_wrapper_mod(mod):
wrapper = load_wrapper()
assert wrapper.enable_pep669_monitoring is mod.enable_pep669_monitoring
assert wrapper.global_cache_skips is mod.global_cache_skips
assert wrapper.global_cache_frame_skips is mod.global_cache_frame_skips
def assert_dummy_restart_events():
wrapper = load_wrapper()
assert wrapper.restart_events is wrapper._dummy_restart_events
def assert_monitoring_restart_events():
wrapper = load_wrapper()
assert sys.version_info >= (3, 12) # only available for Python >=3.12
assert wrapper.restart_events is sys.monitoring.restart_events
@pytest.mark.ge_python312
def test_ge_python312_cython_is_explicitly_disabled(mock_env_use_cython_no):
from _pydevd_bundle import pydevd_pep_669_tracing as expected_mod
assert_wrapper_mod(expected_mod)
assert_monitoring_restart_events()
@pytest.mark.ge_python312
def test_ge_python312_cython_is_explicitly_enabled(mock_env_use_cython_yes):
from _pydevd_bundle import pydevd_pep_669_tracing_cython as expected_mod
assert_wrapper_mod(expected_mod)
assert_monitoring_restart_events()
@pytest.mark.ge_python312
def test_ge_python312_cython_env_is_none(mock_env_use_cython_none):
from _pydevd_bundle import pydevd_pep_669_tracing_cython as expected_mod
assert_wrapper_mod(expected_mod)
assert_monitoring_restart_events()
@pytest.mark.ge_python312
def test_ge_python312_cython_env_is_unexpected(mock_env_use_cython_unexpected):
from _pydevd_bundle import pydevd_pep_669_tracing_cython as expected_mod
assert_wrapper_mod(expected_mod)
assert_monitoring_restart_events()
@pytest.mark.le_python311
def test_le_python311_cython_is_explicitly_disabled(mock_env_use_cython_no):
from _pydevd_bundle import pydevd_pep_669_tracing as expected_mod
assert_wrapper_mod(expected_mod)
assert_dummy_restart_events()
@pytest.mark.le_python311
def test_le_python311_cython_is_explicitly_enabled(mock_env_use_cython_yes):
from _pydevd_bundle import pydevd_pep_669_tracing as expected_mod
assert_wrapper_mod(expected_mod)
assert_dummy_restart_events()
@pytest.mark.le_python311
def test_le_python311_cython_env_is_none(mock_env_use_cython_none):
from _pydevd_bundle import pydevd_pep_669_tracing as expected_mod
assert_wrapper_mod(expected_mod)
assert_dummy_restart_events()
@pytest.mark.le_python311
def test_le_python311_cython_env_is_unexpected(mock_env_use_cython_unexpected):
from _pydevd_bundle import pydevd_pep_669_tracing as expected_mod
assert_wrapper_mod(expected_mod)
assert_dummy_restart_events()