Skip to content

Commit d98a2e2

Browse files
committed
fix: resolve module name mismatch causing Integration Test failures on Windows
**Critical Module Name Fixes:** - Fix Rust PyO3 module name: change #[pymodule] fn demopy_gb_jj to #[pymodule] fn _rust - Update Python module import: change from demopy_gb_jj._rust to demopy._rust - Align module structure with maturin configuration: module-name = 'demopy._rust' - Resolve PyInit symbol mismatch that prevented module import on Windows **Package Structure Alignment:** - Package name: demopy_gb_jj (in pyproject.toml) - Python module path: python/demopy/ - Rust module name: _rust (matches module-name config) - Import statement: import demopy (works correctly) - Rust extension import: demopy._rust (internal, automatic) **Comprehensive Integration Testing:** - Create scripts/test_integration.py for complete package validation - Test package import, functionality, wheel installation, and fallback behavior - Verify both Rust extension and Python fallback implementations work correctly - Add wheel installation testing in clean virtual environment **CI Workflow Improvements:** - Update Integration Test workflow to properly test module import after build - Fix fallback testing to use correct module name (demopy._rust instead of demopy_gb_jj._rust) - Add comprehensive verification steps after maturin develop - Include integration test execution in CI pipeline **Expected Resolution:** - Integration Test workflow should complete successfully on all platforms (Ubuntu, Windows, macOS) - maturin build should create wheels with correct module structure - import demopy should work without ModuleNotFoundError - Both Rust extension and Python fallback should function correctly - All Python versions (3.8-3.13) should be supported **Root Cause:** Module naming inconsistency between package name (demopy_gb_jj), Rust module name (demopy_gb_jj), expected module structure (demopy._rust), and actual import path (demopy). The PyO3 module name must match the module-name configuration in pyproject.toml. **Solution:** Align all module names: Rust module _rust, Python import demopy._rust, maturin config demopy._rust, maintaining backward compatibility with import demopy.
1 parent 618d85b commit d98a2e2

File tree

5 files changed

+372
-9
lines changed

5 files changed

+372
-9
lines changed

.github/workflows/ci.yml

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,28 @@ jobs:
173173
shell: bash
174174
run: |
175175
source venv/bin/activate || source venv/Scripts/activate
176+
echo "Building Rust extension with maturin..."
176177
maturin develop
177178
179+
# Verify the extension was built correctly
180+
python -c "
181+
try:
182+
import demopy
183+
print('✅ Successfully imported demopy')
184+
print(f'Version: {demopy.__version__}')
185+
hello_result = demopy.hello()
186+
print(f'Hello: {hello_result}')
187+
if 'Rust edition' in hello_result:
188+
print('✅ Using Rust extension')
189+
elif 'Python fallback' in hello_result:
190+
print('✅ Using Python fallback')
191+
else:
192+
print(f'⚠️ Unknown implementation: {hello_result}')
193+
except Exception as e:
194+
print(f'❌ Import failed: {e}')
195+
raise
196+
"
197+
178198
- name: Run Python tests
179199
shell: bash
180200
run: |
@@ -193,13 +213,25 @@ jobs:
193213
import builtins
194214
original_import = builtins.__import__
195215
def mock_import(name, *args, **kwargs):
196-
if 'demopy_gb_jj._rust' in name:
197-
raise ImportError('Mocked import error')
216+
if 'demopy._rust' in name or '_rust' in name:
217+
raise ImportError('Mocked import error for testing fallback')
198218
return original_import(name, *args, **kwargs)
199219
builtins.__import__ = mock_import
200220
201221
import demopy
202-
assert 'Python fallback' in demopy.hello()
222+
hello_result = demopy.hello()
223+
print(f'Fallback hello result: {hello_result}')
224+
assert 'Python fallback' in hello_result, f'Expected Python fallback, got: {hello_result}'
203225
assert demopy.add(2, 3) == 5
204-
print('Pure Python fallback test passed!')
226+
assert demopy.multiply(2.5, 4.0) == 10.0
227+
assert demopy.sum_list([1, 2, 3]) == 6
228+
assert demopy.reverse_string('hello') == 'olleh'
229+
assert demopy.power(2, 3) == 8
230+
print('✅ Pure Python fallback test passed!')
205231
"
232+
233+
- name: Run integration tests
234+
shell: bash
235+
run: |
236+
source venv/bin/activate || source venv/Scripts/activate
237+
python scripts/test_integration.py

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ Issues = "https://github.com/jj-devhub/demopy/issues"
3333
[tool.maturin]
3434
features = ["pyo3/extension-module"]
3535
python-source = "python"
36-
module-name = "demopy_gb_jj._rust"
36+
# Map the Rust module to the correct Python module name
37+
module-name = "demopy._rust"

python/demopy/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
try:
1010
# Import the Rust extension
11-
from demopy_gb_jj._rust import add
12-
from demopy_gb_jj._rust import hello as _rust_hello
13-
from demopy_gb_jj._rust import multiply, power, reverse_string, sum_list
11+
# When installed via maturin, the Rust module will be available as demopy._rust
12+
from demopy._rust import add
13+
from demopy._rust import hello as _rust_hello
14+
from demopy._rust import multiply, power, reverse_string, sum_list
1415

1516
# Use the Rust implementation for hello
1617
def hello():

0 commit comments

Comments
 (0)