From 8541f4e7b3ea919fa4c50f5efa0a6220c2503034 Mon Sep 17 00:00:00 2001 From: da-woods Date: Sun, 20 Aug 2023 15:08:44 +0100 Subject: [PATCH] Avoid conflict between int-to-py and anonymous-enum-to-py (#5632) * Avoid conflict between int-to-py and anonymous-enum-to-py Introduced in dec61cdd222ded6d5f96a635d9c4b0dddcbc4e78 Fixes #5623 * Typos --- Cython/Compiler/PyrexTypes.py | 13 ++++++++++++- tests/run/c_int_types_T255.pyx | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 39771d57837..5075d14bd57 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -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( @@ -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): @@ -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 diff --git a/tests/run/c_int_types_T255.pyx b/tests/run/c_int_types_T255.pyx index 58b7839d9d6..9be5739c084 100644 --- a/tests/run/c_int_types_T255.pyx +++ b/tests/run/c_int_types_T255.pyx @@ -187,7 +187,7 @@ def test_add_short(x, y): SSHORT_MAX = ((-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): @@ -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([