Replace Python numerical kernels with native C execution

This commit is contained in:
ljz committed 2026-09-10 01:12:18 +08:00
1 parent 48da6be21c
commit 3b38f73fe0
227 files changed
+16801 -75499

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
"""The retired numerical backend must not become a hidden runtime dependency."""
import os
from pathlib import Path
import subprocess
import sys
import unittest
from unittest.mock import patch
from app.simulation.backends import numeric_engine_name
class NativeOnlyBackendTests(unittest.TestCase):
def test_python_backend_is_rejected_explicitly(self):
with patch.dict(os.environ, {'SIMULATION_NUMERIC_ENGINE': 'python'}):
with self.assertRaisesRegex(ValueError, 'removed'):
numeric_engine_name()
self.assertEqual(numeric_engine_name('native-c'), 'native')
def test_api_and_compiler_import_without_numpy_or_scipy(self):
code = '''
import importlib.abc
import sys
from pathlib import Path
class NoNumericalPython(importlib.abc.MetaPathFinder):
def find_spec(self, fullname, path=None, target=None):
if fullname.split('.')[0] in ('numpy', 'scipy'):
raise AssertionError('Retired numerical dependency imported: '+fullname)
sys.meta_path.insert(0, NoNumericalPython())
from app.main import compile_system_xml_network
from app.simulation.native_codegen.compiler import compile_native_program
from app.system_xml import validate_system_xml_document
doc = validate_system_xml_document(Path('tests/data/native-skill-test.xml').read_bytes()).document
program = compile_native_program(compile_system_xml_network(doc))
assert len(program.state_keys) == 12
assert len(program.variables) == 175
assert not any(name.startswith('app.simulation.solvers.') for name in sys.modules)
'''
subprocess.run([sys.executable, '-c', code], cwd=Path(__file__).resolve().parents[1],
capture_output=True, text=True, check=True, timeout=30)