Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Python limited API #1997

Closed
wants to merge 13 commits into from
Closed
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
47 changes: 1 addition & 46 deletions .github/workflows/wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,17 @@ jobs:
matrix:
include:
# linux-64
- os: ubuntu-latest
python: 37
platform_id: manylinux_x86_64
- os: ubuntu-latest
python: 38
platform_id: manylinux_x86_64
- os: ubuntu-latest
python: 39
platform_id: manylinux_x86_64
- os: ubuntu-latest
python: 310
platform_id: manylinux_x86_64
- os: ubuntu-latest
python: 311
platform_id: manylinux_x86_64
- os: ubuntu-latest
python: 312
platform_id: manylinux_x86_64
# macos-x86-64
- os: macos-latest
python: 37
platform_id: macosx_x86_64
- os: macos-latest
python: 38
platform_id: macosx_universal2
- os: macos-latest
python: 39
platform_id: macosx_universal2
- os: macos-latest
python: 310
platform_id: macosx_universal2
- os: macos-latest
python: 311
platform_id: macosx_universal2
- os: macos-latest
python: 312
platform_id: macosx_universal2
# win-64
- os: windows-2019
python: 37
platform_id: win_amd64
- os: windows-2019
python: 38
platform_id: win_amd64
- os: windows-2019
python: 39
platform_id: win_amd64
- os: windows-2019
python: 310
platform_id: win_amd64
- os: windows-2019
python: 311
platform_id: win_amd64
- os: windows-2019
python: 312
platform_id: win_amd64
steps:
- uses: actions/checkout@v4
- name: Build wheels
Expand All @@ -99,7 +54,7 @@ jobs:
- uses: actions/setup-python@v5
name: Install Python
with:
python-version: '3.10'
python-version: '3.11'
- run: python -m pip install build
- name: Build sdist
run: python -m build --sdist
Expand Down
13 changes: 12 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ requires = [
"numpy>=2.0.0rc0; python_version >= '3.9'",
"nodejs-wheel~=20.9",
"pyyaml",
"cython>=0.16",
"cython>=3.0.1",
]
build-backend = "setuptools.build_meta"

Expand Down Expand Up @@ -120,6 +120,17 @@ test-skip = "cp37-*"

[tool.cibuildwheel.linux]
environment-pass = ["CIBW_BUILD"]
# Use abi3audit to catch issues with Limited API wheels
repair-wheel-command = [
"auditwheel repair -w {dest_dir} {wheel}",
"pipx run abi3audit --strict --report {wheel}",
]

[tool.cibuildwheel.macos]
repair-wheel-command = [
"delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v {wheel}",
"pipx run abi3audit --strict --report {wheel}",
]

[tool.pyright]
include = [
Expand Down
20 changes: 16 additions & 4 deletions reacnetgenerator/dps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""Connect molecule with Depth-First Search."""
from libc.stdlib cimport free, malloc

import cython


cdef extern from "c_stack.h":
# This function is copied from https://zhuanlan.zhihu.com/p/38212302
Expand All @@ -14,6 +16,7 @@ cdef extern from "c_stack.h":
int pop()


@cython.binding(False)
def dps(bonds, levels):
"""Connect molecule with Depth-First Search.

Expand All @@ -35,7 +38,7 @@ def dps(bonds, levels):
bondlist = []
cdef int _N = len(bonds)
cdef int *visited = <int *> malloc(_N * sizeof(int))
cdef int i, s, b_c, l
cdef int i, s, b_c, l, ib, nb
cdef C_Stack st
for i in range(_N):
visited[i]=0
Expand All @@ -52,7 +55,10 @@ def dps(bonds, levels):
elif visited[s]==1:
continue
mol.append(s)
for b, l in zip(bonds[s], levels[s]):
nb = len(bonds[s])
for ib in range(nb):
b = bonds[s][nb]
l = levels[s][nb]
b_c = b
if visited[b_c]==0:
# bond.append((s, b, l) if i < b else (b, s, l))
Expand All @@ -67,6 +73,7 @@ def dps(bonds, levels):
return molecule, bondlist


@cython.binding(False)
def dps_reaction(reactdict):
"""Find A+B->C+D reactions.

Expand All @@ -87,8 +94,11 @@ def dps_reaction(reactdict):
cdef set visited_right = set()
visited = [visited_left, visited_right]
cdef C_Stack st
cdef int nm, im, nr, ir

for init_mol in reactdict[0]:
nm = len(reactdict[0])
for im in range(nm):
init_mol = reactdict[0][im]
if init_mol not in visited[0]:
reaction = [[], []]
st.push(init_mol)
Expand All @@ -100,7 +110,9 @@ def dps_reaction(reactdict):
elif mol in visited[side]:
continue
reaction[side].append(mol)
for r in reactdict[side][mol]:
nr = len(reactdict[side][mol])
for ir in range(nr):
r = reactdict[side][mol][ir]
if r < 0:
if r not in reaction[1-side]:
reaction[1-side].append(r)
Expand Down
7 changes: 3 additions & 4 deletions reacnetgenerator/utils_np.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# cython: linetrace=True
# cython: infer_types=True

import cython
import numpy as np

cimport cython
Expand All @@ -15,8 +16,7 @@ DTYPE8 = np.int8
ctypedef np.int8_t DTYPE8_t


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.binding(False)
cpdef idx_to_signal(DTYPE_t[:] idx, int step):
"""Converts an index array to a signal array.

Expand Down Expand Up @@ -51,8 +51,7 @@ cpdef idx_to_signal(DTYPE_t[:] idx, int step):
return signal


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.binding(False)
cpdef check_zero_signal(DTYPE8_t[:] signal):
"""Check if the given signal contains only zeros.

Expand Down
18 changes: 14 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import setuptools.command.build_ext
import yaml
from setuptools import Extension, setup
from wheel.bdist_wheel import bdist_wheel

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -64,8 +65,19 @@ def run(self):
super().run()


class bdist_wheel_abi3(bdist_wheel):
def get_tag(self):
python, abi, plat = super().get_tag()

if python.startswith("cp"):
# on CPython, our wheels are abi3 and compatible back to 3.7
return "cp37", "abi3", plat

return python, abi, plat


if __name__ == "__main__":
define_macros = []
define_macros = [("CYTHON_LIMITED_API", "1"), ("Py_LIMITED_API", "0x030b0000")]
if os.environ.get("DEBUG", 0):
define_macros.extend((("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")))

Expand All @@ -86,7 +98,5 @@ def run(self):

setup(
ext_modules=ext_modules,
cmdclass={
"build_ext": BuildExtCommand,
},
cmdclass={"build_ext": BuildExtCommand, "bdist_wheel": bdist_wheel_abi3},
)
Loading