-1

I'm chasing my tail with what I suspect is a simple problem, but I can't seem to find any explanation for the observed behavior. Assume I have a constant in a C header file defined by:

#define FOOBAR 128
typedef uint32_t mytype_t;

I convert this in Cython by putting the following in the .pxd file:

cdef int _FOOBAR   "FOOBAR"
ctypedef uint32_t mytype_t

In my .pyx file, I have a declaration:

FOOBAR = _FOOBAR

followed later in a class definition:

cdef class MyClass:
    cdef mytype_t myvar
    def __init__(self):
        try:
            self.myvar = FOOBAR
            print("GOOD")
        except:
            print("BAD")

I then try to execute this with a simple program:

try:
    foo = MyClass()
except:
    print("FAILED TO CREATE CLASS")

Sadly, this errors out, but I don't get an error message - I just get the exception print output:

BAD

Any suggestions on root cause would be greatly appreciated.

ralph
  • 141
  • 7
  • My apologies - in my desire to simplify the problem I apparently left out some critical info. The problem occurs when the variable isn't an integer but instead a defined fixed type. I have updated the question. – ralph Jul 20 '18 at 12:17
  • 1
    Please provide [mcve]. You can use Cython's verbatim code (http://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#including-verbatim-c-code) to make it stand alone. – ead Jul 20 '18 at 13:07

1 Answers1

0

I believe I have finally tracked it down. The root cause issue is that FOOBAR in my code was actually set to UINT32MAX. Apparently, Cython/Python interprets that as a -1 and Python then rejects setting a uint32_t variable equal to it. The solution is to define FOOBAR to be 0xffffffff - apparently Python thinks that is a non-negative value and accepts it.

ralph
  • 141
  • 7