Skip to content

Commit

Permalink
Declare PY_SSIZE_T* as const
Browse files Browse the repository at this point in the history
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 cython#5562
  • Loading branch information
imphil committed Jul 26, 2023
1 parent 9c32681 commit b39ab1d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cython/Utility/CppConvert.pyx
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions tests/compile/vector_include.h
@@ -0,0 +1,8 @@
#pragma once

#include <vector>

std::vector<int> get_vector()
{
return std::vector<int>(17);
}
17 changes: 17 additions & 0 deletions 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()
4 changes: 2 additions & 2 deletions 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
Expand Down
4 changes: 2 additions & 2 deletions tests/run/charptr_decode.pyx
Expand Up @@ -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


############################################################
Expand Down
4 changes: 2 additions & 2 deletions tests/run/unicode_slicing.pyx
Expand Up @@ -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
Expand Down

0 comments on commit b39ab1d

Please sign in to comment.