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

Upgrade to NumPy 2.0 #319

Merged
merged 11 commits into from
Jun 18, 2024
2 changes: 1 addition & 1 deletion docs/algorithms/curve-curve-intersection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Curve-Curve Intersection
return -np.inf
_, result = np.frexp(value)
# Shift [1/2, 1) --> [1, 2) borrows one from exponent
return result - 1
return int(result - 1)

The problem of intersecting two curves is a difficult one
in computational geometry. The :meth:`.Curve.intersect` method (when using
Expand Down
6 changes: 3 additions & 3 deletions docs/python/binary-extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ The ``bezier._speedup`` module depends on this local copy of ``libbezier``:

$ readelf -d _speedup.cpython-311-x86_64-linux-gnu.so

Dynamic section at offset 0x49c000 contains 27 entries:
Dynamic section at offset 0x49e000 contains 27 entries:
Tag Type Name/Value
0x000000000000000f (RPATH) Library rpath: [$ORIGIN/../bezier.libs]
0x0000000000000001 (NEEDED) Shared library: [libbezier-631d8eda.so.2023.7.28]
0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000c (INIT) 0x6000
0x000000000000000d (FINI) 0x7f630
0x000000000000000c (INIT) 0x7000
0x000000000000000d (FINI) 0x809f0
...

and the local copy of ``libbezier`` depends on the other dependencies in
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# NumPy is required for building `bezier` from source.
numpy >= 1.26.4, < 2
numpy >= 2.0.0
# Sphinx and related are required for building documentation.
Sphinx >= 7.3.7
# See: https://github.com/readthedocs/sphinx_rtd_theme/issues/1463
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"jsonschema": "jsonschema >= 4.22.0",
"lcov-cobertura": "lcov-cobertura >= 2.0.2",
"matplotlib": "matplotlib >= 3.9.0",
"numpy": "numpy >= 1.26.4, < 2",
"numpy": "numpy >= 2.0.0",
"pycobertura": "pycobertura >= 3.3.2",
"Pygments": "Pygments",
"pylint": "pylint >= 3.2.3",
Expand Down
95 changes: 91 additions & 4 deletions scripts/clean_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,84 @@
import sys


NUMPY_2_INCOMPATIBLE = """\
/* "Cython/Includes/numpy/__init__.pxd":794
* return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
*
* cdef inline tuple PyDataType_SHAPE(dtype d): # <<<<<<<<<<<<<<
* if PyDataType_HASSUBARRAY(d):
* return <tuple>d.subarray.shape
*/

static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyDataType_SHAPE(PyArray_Descr *__pyx_v_d) {
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
int __pyx_t_1;
__Pyx_RefNannySetupContext("PyDataType_SHAPE", 1);

/* "Cython/Includes/numpy/__init__.pxd":795
*
* cdef inline tuple PyDataType_SHAPE(dtype d):
* if PyDataType_HASSUBARRAY(d): # <<<<<<<<<<<<<<
* return <tuple>d.subarray.shape
* else:
*/
__pyx_t_1 = PyDataType_HASSUBARRAY(__pyx_v_d);
if (__pyx_t_1) {

/* "Cython/Includes/numpy/__init__.pxd":796
* cdef inline tuple PyDataType_SHAPE(dtype d):
* if PyDataType_HASSUBARRAY(d):
* return <tuple>d.subarray.shape # <<<<<<<<<<<<<<
* else:
* return ()
*/
__Pyx_XDECREF(__pyx_r);
__Pyx_INCREF(((PyObject*)__pyx_v_d->subarray->shape));
__pyx_r = ((PyObject*)__pyx_v_d->subarray->shape);
Comment on lines +51 to +52
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the problematic block; ->subarray is being invoked on a PyArray_Descr

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #320 + cython/cython#6249 (comment), the resolution was straightforward!

goto __pyx_L0;

/* "Cython/Includes/numpy/__init__.pxd":795
*
* cdef inline tuple PyDataType_SHAPE(dtype d):
* if PyDataType_HASSUBARRAY(d): # <<<<<<<<<<<<<<
* return <tuple>d.subarray.shape
* else:
*/
}

/* "Cython/Includes/numpy/__init__.pxd":798
* return <tuple>d.subarray.shape
* else:
* return () # <<<<<<<<<<<<<<
*
*
*/
/*else*/ {
__Pyx_XDECREF(__pyx_r);
__Pyx_INCREF(__pyx_empty_tuple);
__pyx_r = __pyx_empty_tuple;
goto __pyx_L0;
}

/* "Cython/Includes/numpy/__init__.pxd":794
* return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
*
* cdef inline tuple PyDataType_SHAPE(dtype d): # <<<<<<<<<<<<<<
* if PyDataType_HASSUBARRAY(d):
* return <tuple>d.subarray.shape
*/

/* function exit code */
__pyx_L0:;
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

"""


def clean_file(c_source, virtualenv_dirname):
"""Strip trailing whitespace and clean up "local" names in C source.

Expand All @@ -34,11 +112,20 @@ def clean_file(c_source, virtualenv_dirname):
".nox", virtualenv_dirname, "lib", py_version, "site-packages", ""
)
contents = contents.replace(lib_path, "")
# Write the files back, but strip all trailing whitespace.
lines = contents.split("\n")

# Strip all trailing whitespace.
lines = [line.rstrip() for line in contents.split("\n")]
if lines[-1] != "":
lines.append("")
contents = "\n".join(lines)

# Remove the `PyDataType_SHAPE` block (incompatible with NumPy 2.0 and not
# used in the codebase).
contents = contents.replace(NUMPY_2_INCOMPATIBLE, "")

# Write the file back
with open(c_source, "w") as file_obj:
for line in lines:
file_obj.write(line.rstrip() + "\n")
file_obj.write(contents)


def main():
Expand Down
2 changes: 1 addition & 1 deletion scripts/manylinux/build-wheel-for-doctest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fi

# 0. Install the Python dependencies
"${PY_ROOT}/bin/python" -m pip install --upgrade pip
"${PY_ROOT}/bin/python" -m pip install --upgrade auditwheel cmake nox 'numpy >= 1.26.4, < 2'
"${PY_ROOT}/bin/python" -m pip install --upgrade auditwheel cmake nox numpy

# 1. Make sure no previous build artifacts are still around
cd "${BEZIER_ROOT}"
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@
""".format(
install_prefix=INSTALL_PREFIX_ENV, no_extension=NO_EXTENSION_ENV
)
REQUIREMENTS = ("numpy >= 1.26.4, < 2",)
REQUIREMENTS = ("numpy >= 2.0.0",)
# See: https://www.python.org/dev/peps/pep-0508/
# Dependency specification for Python Software Packages
EXTRAS_REQUIRE = {
"full": ["matplotlib >= 3.7.2", "scipy >= 1.11.1", "sympy >= 1.12"],
"full": ["matplotlib >= 3.9.0", "scipy >= 1.13.1", "sympy >= 1.12.1"],
}
DESCRIPTION = (
"Helper for B\u00e9zier Curves, Triangles, and Higher Order Objects"
Expand Down
Loading
Loading