1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Pytest configuration for Python bindings tests
This file contains fixtures and configuration to work around PyO3 issues
with pytest, particularly the GIL (Global Interpreter Lock) and Python
initialization problems that can cause pytest to hang or fail after tests.
The main issue is that PyO3's auto-initialize feature can cause Python
to be initialized multiple times, leading to GIL deadlocks and pytest
hanging after tests complete.
Workarounds implemented:
1. Set PYO3_NO_PYTHON_VERSION_CHECK to skip version checks
2. Use session-level fixture to initialize Python once
3. Force garbage collection after each test
4. Use pytest-forked for process isolation (configured in pytest.ini)
"""
# Workaround for PyO3 pytest issues:
# Set environment variable to prevent PyO3 from doing version checks
# This can help avoid initialization issues
# Try to import the module early to catch import errors
# Note: We don't actually use the import here, but importing it early
# helps catch import errors before tests run
# noqa: F401
# If module is not available, skip all tests
=
"""Session-level fixture to set up Python environment for PyO3.
This fixture runs once per test session and ensures Python is properly
initialized before any tests run. It also cleans up after all tests.
This helps prevent the PyO3 pytest hang issue by ensuring Python
is only initialized once per test session.
"""
# Pre-initialize Python to avoid multiple initializations
# This helps prevent the PyO3 pytest hang issue
# noqa: F401
# Force module import to initialize Python bindings early
# This ensures Python is initialized before tests run
= # noqa: F401
# Module not available or doesn't have expected attributes
# This is expected in some test environments, so we silently continue
pass
yield
# Cleanup after all tests
# Force garbage collection to release any Python objects
# This helps prevent memory leaks and GIL issues
"""Fixture to isolate each test and prevent shared state issues.
This runs before and after each test to ensure proper cleanup.
The cleanup is critical for PyO3 to prevent GIL deadlocks.
"""
# Setup: ensure clean state
yield
# Teardown: force garbage collection after each test
# This releases Python objects and prevents GIL issues
# Run multiple times to ensure all cyclic references are cleared
# Configure pytest to use forked/isolated test execution if available
# This prevents GIL and initialization issues
"""Configure pytest with PyO3-specific settings."""
# Add custom markers
# If pytest-xdist is available, configure it for better isolation
# Use process-based isolation for PyO3 tests
# Default to 1 process to avoid GIL issues
= 1