From b39ab1da180535d43a8699087ec482525a12a79d Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Tue, 25 Jul 2023 14:51:42 +0000 Subject: [PATCH] Declare PY_SSIZE_T* as const Follow the lead of `Cython/Includes/cpython/pyport.pxd` to declare `PY_SSIZE_T_MIN` and `PY_SSIZE_T_MAX` as `const`. This prevents Cython from trying to override or assign to this "variable" when doing wildcard imports. Add a test that shows this behavior in one example. Fixes #5562 --- Cython/Utility/CppConvert.pyx | 2 +- tests/compile/vector_include.h | 8 ++++++++ tests/compile/vector_include.pyx | 17 +++++++++++++++++ tests/run/bytesmethods.pyx | 4 ++-- tests/run/charptr_decode.pyx | 4 ++-- tests/run/unicode_slicing.pyx | 4 ++-- 6 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 tests/compile/vector_include.h create mode 100644 tests/compile/vector_include.pyx diff --git a/Cython/Utility/CppConvert.pyx b/Cython/Utility/CppConvert.pyx index 1c6239b2b58..0e7cf4e2dc4 100644 --- a/Cython/Utility/CppConvert.pyx +++ b/Cython/Utility/CppConvert.pyx @@ -60,7 +60,7 @@ cdef extern from "Python.h": void Py_INCREF(object) list PyList_New(Py_ssize_t size) void PyList_SET_ITEM(object list, Py_ssize_t i, object o) - cdef Py_ssize_t PY_SSIZE_T_MAX + const Py_ssize_t PY_SSIZE_T_MAX @cname("{{cname}}") cdef object {{cname}}(const vector[X]& v): diff --git a/tests/compile/vector_include.h b/tests/compile/vector_include.h new file mode 100644 index 00000000000..395bfb3b423 --- /dev/null +++ b/tests/compile/vector_include.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +std::vector get_vector() +{ + return std::vector(17); +} diff --git a/tests/compile/vector_include.pyx b/tests/compile/vector_include.pyx new file mode 100644 index 00000000000..ad9d3f2a1c0 --- /dev/null +++ b/tests/compile/vector_include.pyx @@ -0,0 +1,17 @@ +# mode: compile +# tag: cpp + +# Test that using functionality from libcpp.vector does not lead to compile +# errors when wildcard imports are used as well. + +# Import libcpp.vector, which declares PY_SSIZE_T_MAX. +from libcpp.vector cimport vector + +# Import any other module using a wildcard import. +from spam import * + +# Use the imports (details don't matter). +cdef extern from "vector_include.h": + vector[int] get_vector() + +my_vector = get_vector() diff --git a/tests/run/bytesmethods.pyx b/tests/run/bytesmethods.pyx index fecdcc66824..8714958f776 100644 --- a/tests/run/bytesmethods.pyx +++ b/tests/run/bytesmethods.pyx @@ -1,8 +1,8 @@ cimport cython cdef extern from *: - cdef Py_ssize_t PY_SSIZE_T_MIN - cdef Py_ssize_t PY_SSIZE_T_MAX + const Py_ssize_t PY_SSIZE_T_MIN + const Py_ssize_t PY_SSIZE_T_MAX SSIZE_T_MAX = PY_SSIZE_T_MAX SSIZE_T_MIN = PY_SSIZE_T_MIN diff --git a/tests/run/charptr_decode.pyx b/tests/run/charptr_decode.pyx index e7c26db1b7a..503f6bbb962 100644 --- a/tests/run/charptr_decode.pyx +++ b/tests/run/charptr_decode.pyx @@ -2,8 +2,8 @@ cimport cython cdef extern from *: - cdef Py_ssize_t PY_SSIZE_T_MIN - cdef Py_ssize_t PY_SSIZE_T_MAX + const Py_ssize_t PY_SSIZE_T_MIN + const Py_ssize_t PY_SSIZE_T_MAX ############################################################ diff --git a/tests/run/unicode_slicing.pyx b/tests/run/unicode_slicing.pyx index c84d7052e5e..645c9eb0988 100644 --- a/tests/run/unicode_slicing.pyx +++ b/tests/run/unicode_slicing.pyx @@ -169,8 +169,8 @@ __doc__ = u""" """ cdef extern from *: - cdef Py_ssize_t PY_SSIZE_T_MIN - cdef Py_ssize_t PY_SSIZE_T_MAX + const Py_ssize_t PY_SSIZE_T_MIN + const Py_ssize_t PY_SSIZE_T_MAX SSIZE_T_MAX = PY_SSIZE_T_MAX SSIZE_T_MIN = PY_SSIZE_T_MIN