-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: setup cython example #5
Draft
aryarm
wants to merge
18
commits into
main
Choose a base branch
from
cython
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e6e4d31
copy over files from https://github.com/BrianPugh/python-template
aryarm 5d1410a
copy over gitignore entires from python-template as well
aryarm 84e2602
enable cython in pyproject and add notes
aryarm 9064fab
oops add newline to end of build.py
aryarm ce75a44
refmt for black
aryarm 55c1df7
also ignore c++ files
aryarm 523a4a2
try to fix docs issue in noxfile
aryarm 0cb401f
Merge branch 'main' into cython
aryarm a30ede5
try resolve issue with python 3.10
aryarm 5fe1fb4
Merge branch 'main' into cython
aryarm 13dd85f
ensure proper py version is used when running poetry build
aryarm 6ea54f1
make shell command multiline
aryarm 6565261
try perl instead of sed to deal with differences between sed on macos…
aryarm d08d259
avoid "-i" bc of different sed on macos vs linux
aryarm 25f1777
cache dev env separately for each py version
aryarm 37b3860
match any python version and not just 3.9
aryarm 956a5c7
silence warnings about defaults channel
aryarm 7079517
add cibuildwheel to release workflow
aryarm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,51 +17,91 @@ jobs: | |
with: | ||
release-type: python | ||
|
||
- uses: actions/checkout@v3 | ||
if: ${{ steps.release.outputs.release_created }} | ||
build_wheels: | ||
name: Build wheels for ${{ matrix.arch }} ${{ matrix.os }} py3.${{ matrix.python }} | ||
needs: release | ||
runs-on: ${{ matrix.os }}-latest | ||
if: ${{ needs.release.outputs.release_created }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python: [9, 10, 11, 12, 13] | ||
os: [ubuntu, macos] | ||
arch: [x86_64, aarch64, universal2] | ||
exclude: | ||
- os: macos | ||
arch: universal2 | ||
python: 7 | ||
- os: ubuntu | ||
arch: universal2 | ||
- os: macos | ||
arch: x86_64 | ||
- os: macos | ||
arch: aarch64 | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up QEMU for aarch64 emulation | ||
if: runner.os == 'Linux' && matrix.arch == 'aarch64' | ||
uses: docker/setup-qemu-action@v3 | ||
with: | ||
fetch-depth: 2 | ||
platforms: all | ||
|
||
- name: Build wheels | ||
uses: pypa/[email protected] | ||
env: | ||
CIBW_SKIP: "*-musllinux_* pp3*-manylinux_aarch64" | ||
CIBW_BUILD: ${{ format('*p3{0}-*', matrix.python) }} | ||
CIBW_ARCHS: ${{ matrix.arch }} | ||
CIBW_TEST_REQUIRES: pytest | ||
CIBW_TEST_COMMAND: "pytest {package}/tests" | ||
|
||
- name: Set up Python | ||
if: ${{ steps.release.outputs.release_created }} | ||
uses: actions/setup-python@v4 | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
python-version: '3.9' # keep synced with dev-env.yml | ||
name: wheels-${{ matrix.os }}-${{ matrix.arch }}-3${{ matrix.python }} | ||
path: ./wheelhouse/*.whl | ||
|
||
- name: Upgrade pip | ||
if: ${{ steps.release.outputs.release_created }} | ||
run: | | ||
pip install --upgrade pip | ||
pip --version | ||
merge_wheels: | ||
runs-on: ubuntu-latest | ||
needs: build_wheels | ||
steps: | ||
- name: Merge wheel artifacts into a single artifact | ||
uses: actions/upload-artifact/merge@v4 | ||
with: | ||
name: wheels | ||
pattern: wheels-* | ||
delete-merged: true | ||
|
||
- name: Install Poetry | ||
if: ${{ steps.release.outputs.release_created }} | ||
run: | | ||
pip install 'poetry==1.8.3' # keep version synced with dev-env.yml | ||
poetry --version | ||
build_sdist: | ||
needs: release | ||
runs-on: ubuntu-latest | ||
if: ${{ needs.release.outputs.release_created }} | ||
env: | ||
job_python_version: "3.9" # keep synced with dev-env.yml | ||
|
||
- name: Bump version for developmental release | ||
if: ${{ steps.release.outputs.release_created }} | ||
env: | ||
version: ${{ steps.release.outputs.tag_name }} | ||
run: | | ||
poetry version $version | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ env.job_python_version }} | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ env.job_python_version }} | ||
|
||
- name: Build package | ||
if: ${{ steps.release.outputs.release_created }} | ||
- name: Create source distribution | ||
run: | | ||
poetry build --ansi | ||
pip install build | ||
python -m build --sdist . | ||
|
||
- uses: actions/upload-artifact@v4 | ||
if: ${{ steps.release.outputs.release_created }} | ||
with: | ||
name: dist | ||
path: dist/ | ||
name: sdist | ||
path: dist/panct-*.tar.gz | ||
|
||
upload_pypi: | ||
needs: release | ||
needs: [build_wheels, build_sdist] | ||
runs-on: ubuntu-latest | ||
if: ${{ needs.release.outputs.release_created }} | ||
environment: release | ||
permissions: | ||
# IMPORTANT: this permission is mandatory for trusted publishing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1. How do we want to include the gbz-base source code in the repo? Options: git submodule or install as setup step | ||
2. Do we want to automatically skip compilation of the cython module if it doesn't work? Or should we just fail/error explicitly. The former could be helpful if we want to implement some kind of slower alternative |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# type: ignore | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
# This file was adapted from https://github.com/BrianPugh/python-template/blob/main/build.py | ||
|
||
# Uncomment if library can still function if extensions fail to compile (e.g. slower, python fallback). | ||
# Don't allow failure if cibuildwheel is running. | ||
# allowed_to_fail = os.environ.get("CIBUILDWHEEL", "0") != "1" | ||
allowed_to_fail = False | ||
|
||
|
||
def build_cython_extensions(): | ||
# when using setuptools, you should import setuptools before Cython, | ||
# otherwise, both might disagree about the class to use. | ||
from setuptools import Extension # noqa: I001 | ||
from setuptools.dist import Distribution # noqa: I001 | ||
import Cython.Compiler.Options # pyright: ignore [reportMissingImports] | ||
from Cython.Build import ( | ||
build_ext, | ||
cythonize, | ||
) # pyright: ignore [reportMissingImports] | ||
|
||
Cython.Compiler.Options.annotate = True | ||
|
||
if os.name == "nt": # Windows | ||
extra_compile_args = [ | ||
"/O2", | ||
] | ||
else: # UNIX-based systems | ||
extra_compile_args = [ | ||
"-O3", | ||
"-Werror", | ||
"-Wno-unreachable-code-fallthrough", | ||
"-Wno-deprecated-declarations", | ||
"-Wno-parentheses-equality", | ||
] | ||
extra_compile_args.append("-UNDEBUG") # Cython disables asserts by default. | ||
# Relative to project root director | ||
include_dirs = [ | ||
"panct/", | ||
"panct/_c_src", | ||
] | ||
|
||
c_files = [str(x) for x in Path("panct/_c_src").rglob("*.c")] | ||
extensions = [ | ||
Extension( | ||
# Your .pyx file will be available to cpython at this location. | ||
"panct._c_extension", | ||
[ | ||
# ".c" and ".pyx" source file paths | ||
"panct/_c_extension.pyx", | ||
*c_files, | ||
], | ||
include_dirs=include_dirs, | ||
extra_compile_args=extra_compile_args, | ||
language="c", | ||
), | ||
] | ||
|
||
include_dirs = set() | ||
for extension in extensions: | ||
include_dirs.update(extension.include_dirs) | ||
include_dirs = list(include_dirs) | ||
|
||
ext_modules = cythonize( | ||
extensions, include_path=include_dirs, language_level=3, annotate=True | ||
) | ||
dist = Distribution({"ext_modules": ext_modules}) | ||
cmd = build_ext(dist) | ||
cmd.ensure_finalized() | ||
cmd.run() | ||
|
||
for output in cmd.get_outputs(): | ||
output = Path(output) | ||
relative_extension = output.relative_to(cmd.build_lib) | ||
shutil.copyfile(output, relative_extension) | ||
|
||
|
||
try: | ||
build_cython_extensions() | ||
except Exception: | ||
if not allowed_to_fail: | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Foo: | ||
def __init__(self): ... | ||
def __call__(self): ... | ||
|
||
def divide(x: float, y: float) -> float: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
cimport cpythontemplate # See cpythontemplate.pxd | ||
# Invoke PyErr_CheckSignals() occasionally if your C code runs long. | ||
# This allows your code to be interrupted via ctrl+c. | ||
from cpython.exc cimport PyErr_CheckSignals | ||
from cpython.mem cimport PyMem_Malloc, PyMem_Free | ||
from libc.stddef cimport size_t | ||
|
||
|
||
cdef class Foo: | ||
"""Pythonic interface to the C "foo" struct.""" | ||
cdef cpythontemplate.foo_t * _object | ||
|
||
def __cinit__(self): | ||
# Automatically called before __init__. | ||
# All arguments passed to __init__ are also passed to __cinit__. | ||
# * As a convenience, if __cinit__() takes no arguments (other than self), it will | ||
# ignore arguments passed to the constructor without complaining about signature mismatch. | ||
# Allocate memory for C objects here. | ||
self._object = <cpythontemplate.foo_t *>PyMem_Malloc(sizeof(cpythontemplate.foo_t)) | ||
if self._object is NULL: | ||
raise MemoryError | ||
|
||
def __dealloc__(self): | ||
# Should "undo" __cinit__ | ||
PyMem_Free(self._object) | ||
|
||
def __init__(self): | ||
cpythontemplate.foo_init(self._object) | ||
|
||
def __call__(self): | ||
# invoke increment | ||
cpythontemplate.foo_increment(self._object) | ||
|
||
# Functions declared with cpdef are visible to both cython and python. | ||
# https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#python-functions-vs-c-functions | ||
# https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#error-return-values | ||
cpdef float divide(float x, float y) except? 1.23: | ||
if y == 0.0: | ||
raise ZeroDivisionError | ||
return x / y |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: delete this file before merging