Skip to content

Commit

Permalink
Avoid conflict between int-to-py and anonymous-enum-to-py (#5632)
Browse files Browse the repository at this point in the history
* Avoid conflict between int-to-py and anonymous-enum-to-py

Introduced in dec61cd

Fixes #5623

* Typos
  • Loading branch information
da-woods committed Aug 20, 2023
1 parent f76a841 commit 8541f4e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
13 changes: 12 additions & 1 deletion Cython/Compiler/PyrexTypes.py
Expand Up @@ -59,7 +59,8 @@ def specialization_name(self):
if self._specialization_name is None:
# This is not entirely robust.
common_subs = (self.empty_declaration_code()
.replace("unsigned ", "unsigned_")
# covers both "unsigned " and "signed "
.replace("signed ", "signed_")
.replace("long long", "long_long")
.replace(" ", "__"))
self._specialization_name = re.sub(
Expand Down Expand Up @@ -2194,6 +2195,11 @@ class CAnonEnumType(CIntType):
def sign_and_name(self):
return 'int'

def specialization_name(self):
# ensure that the to/from Python functions don't conflict with
# "int"
return '__pyx_anon_enum'


class CReturnCodeType(CIntType):

Expand All @@ -2203,6 +2209,11 @@ class CReturnCodeType(CIntType):
exception_check = False
default_format_spec = ''

def specialization_name(self):
# I don't think we should end up creating PyInt_As_int/PyInt_From_int functions
# for this type, but it's better they're distinct in case it happens.
return super(CReturnCodeType, self).specialization_name() + "return_code"

def can_coerce_to_pystring(self, env, format_spec=None):
return not format_spec

Expand Down
16 changes: 15 additions & 1 deletion tests/run/c_int_types_T255.pyx
Expand Up @@ -187,7 +187,7 @@ def test_add_short(x, y):
SSHORT_MAX = <signed short>((<unsigned short>-1)>>1)
SSHORT_MIN = (-SSHORT_MAX-1)

def test_sshort(short x):
def test_sshort(signed short x):
u"""
>>> test_sshort(SSHORT_MIN-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
Expand Down Expand Up @@ -765,6 +765,20 @@ def test_convert_pylong(x):
cdef long long r = x
return r


cdef enum:
ANONYMOUS_ENUM_MEMBER = 1

def test_anonymous_enum():
"""
The main point here is that the from-py conversion function shouldn't
conflict with the function for int
>>> test_anonymous_enum()
1
"""
return ANONYMOUS_ENUM_MEMBER


# -------------------------------------------------------------------

__doc__ = u"".join([
Expand Down

0 comments on commit 8541f4e

Please sign in to comment.