Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ build/
.DS_Store
/site-packages/
/tmp

*.cpython*
omas_cython.c
omas/data-dictionary
omas/Saxon*
omas/imas_structures/*/*.xml
Expand Down
19 changes: 6 additions & 13 deletions omas/omas_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,21 +868,14 @@ def omas_global_quantities(imas_version=omas_rcparams['default_imas_version']):
return _global_quantities[imas_version]


# only attempt cython if effective user owns this copy of omas
# disabled for Windows: need to add check for file ownership under Windows
if os.name == 'nt' or os.geteuid() != os.stat(__file__).st_uid:
# Import compiled Cython extension
# Fall back to pure Python implementation if the compiled extension is not available
try:
from .omas_cython import *
except ImportError as _excp:
warnings.warn('Compiled Cython extension not available, falling back to pure Python: ' + str(_excp))
with open(os.path.split(__file__)[0] + os.sep + 'omas_cython.pyx', 'r') as f:
exec(f.read(), globals())
else:
try:
import pyximport

pyximport.install(language_level=3)
from .omas_cython import *
except Exception as _excp:
warnings.warn('omas cython failed: ' + str(_excp))
with open(os.path.split(__file__)[0] + os.sep + 'omas_cython.pyx', 'r') as f:
exec(f.read(), globals())


def l2ut(path):
Expand Down
11 changes: 11 additions & 0 deletions omas/tests/test_omas_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ def test_omas_info(self):
ods_info_list = omas_info(get_list)
assert all(item in ods_info_list for item in get_list)

def test_cython_extension_compiled(self):
"""Test that the Cython extension was properly compiled and is being used"""
import omas.omas_cython as cython_module
# Check that we're using the compiled extension (.so file) not the .pyx source
assert hasattr(cython_module, '__file__'), "Cython module should have a __file__ attribute"
module_file = cython_module.__file__
# Compiled extension should end with .so (Linux/Mac) or .pyd (Windows)
assert module_file.endswith('.so') or module_file.endswith('.pyd'), \
f"Expected compiled extension (.so/.pyd), but got: {module_file}"
print(f"✓ Using compiled Cython extension: {module_file}")

def test_p2l(self):
assert p2l('0') == [0]
assert p2l('equilibrium') == ['equilibrium']
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools>=41.2", "Cython>=0.29", "numpy>=1.16.1"]
build-backend = "setuptools.build_meta"
13 changes: 12 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import glob
import subprocess
from setuptools import Extension
from Cython.Build import cythonize

install_requires = [
'numpy>=1.16.1',
Expand Down Expand Up @@ -51,7 +53,7 @@

packages = ['omas', 'omas.examples', 'omas.samples', 'omas.tests', 'omas.utilities']
package_data = {
'omas': ['*.py', '*.pyx', 'version'],
'omas': ['*.py', 'version'],
'omas.examples': ['*.py'],
'omas.samples': ['*'],
'omas.tests': ['*.py'],
Expand Down Expand Up @@ -109,6 +111,13 @@

from setuptools import setup

# Build Cython extension
ext_modules = cythonize(
[Extension("omas.omas_cython", ["omas/omas_cython.pyx"])],
language_level=3,
compiler_directives={'embedsignature': True}
)

setup(
name='omas',
version=open(here + 'omas/version', 'r').read().strip(),
Expand All @@ -124,4 +133,6 @@
package_data=package_data,
install_requires=install_requires,
extras_require=extras_require,
ext_modules=ext_modules,
setup_requires=['Cython>=0.29', 'numpy>=1.16.1'],
)