cpdef enum with member "None"

31 views
Skip to first unread message

Paul Harwood

unread,
Jun 22, 2021, 1:27:16 AM6/22/21
to cython-users
I have a problem with wrapping an enum - I wonder if anyone knows what to do.

In the C API I of the package being wrapped - there is this enum :

```
enum MDAL_Status
{
  None,
  // Errors
  Err_NotEnoughMemory,
  Err_FileNotFound,
  Err_UnknownFormat,
  Err_IncompatibleMesh,
  Err_InvalidData,
  Err_IncompatibleDataset,
  Err_IncompatibleDatasetGroup,
  Err_MissingDriver,
  Err_MissingDriverCapability,
  Err_FailToWriteToDisk,
  Err_UnsupportedElement,

  // Warnings
  Warn_InvalidElements,
  Warn_ElementWithInvalidNode,
  Warn_ElementNotUnique,
  Warn_NodeNotUnique,
  Warn_MultipleMeshesInFile
};
'''
if I try wrapping as follows - I get errors runtime errors because None is a reserved word  (TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'):

```
cdef extern from "mdal.h":
    cpdef enum MDAL_Status:
        None = 0,
        Err_NotEnoughMemory = 1,
        Err_FileNotFound = 2,
        Err_UnknownFormat = 3,
        Err_IncompatibleMesh = 4,
        Err_InvalidData = 5,
        Err_IncompatibleDataset = 6,
        Err_IncompatibleDatasetGroup = 7,
        Err_MissingDriver = 8,
        Err_MissingDriverCapability = 9,
        Err_FailToWriteToDisk = 10,
        Err_UnsupportedElement = 11,
        Warn_InvalidElements = 12,
        Warn_ElementWithInvalidNode = 13,
        Warn_ElementNotUnique = 14,
        Warn_NodeNotUnique = 15,
        Warn_MultipleMeshesInFile = 16
```
It is currently working with the "None = 0" line removed but that, of course, means that I have a return value that is not handled.

Apart from changing the API for the upstream library (not really an option) - does anyone have a fix for this?

Thanks

P

Jonathan Kliem

unread,
Jun 22, 2021, 6:40:42 AM6/22/21
to cython-users
Hi,

I think you can just do

```
cdef extern from "mdal.h":
    cpdef enum MDAL_Status:
        MDAL_None "None" = 0
        Err_NotEnoughMemory = 1
```

This seems to do the trick. Because in cython/python the word None is reserved, you need to come up with an alternative name for it. The part it quotes tells cython the original name that it will look for in "mdal.h". That is a bit like `from ... import ... as ...`.

Btw, you do not need the comma at the end of lines in your cython file for the definition of the enumeration.

I hope that helps,

Jonathan

D Woods

unread,
Jun 22, 2021, 6:40:52 AM6/22/21
to cython-users
For most Cython features you can set a "cname" in a text-string (to match the API) and use a different name in Python. This is designed to help you solve issues like this. I haven't specifically tested it with enums but I imagine something like:

cdef extern from "mdal.h":
    cpdef enum MDAL_Status:
        SomeNameThatIsntNone "None" = 0,
        ...

Paul Harwood

unread,
Jun 22, 2021, 6:41:00 AM6/22/21
to cython...@googlegroups.com
The answer (thanks to Marcel) was documented in https://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#resolving-naming-conflicts-c-name-specifications>

That is, you can choose a different Python name for the C "None":


cdef extern from "mdal.h":
      cpdef enum MDAL_Status:
          NoError "None" = 0,
          Err_NotEnoughMemory = 1,

Then that what gets exposed on the Python side is "NoError", but it’ll
refer to "None" on the C side.

This worked very nicely - thanks

P

--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/EWWL0f5LIZ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/bcbe0d6e-f94e-465d-bfda-481a6b6a1671n%40googlegroups.com.

Paul Harwood

unread,
Jun 22, 2021, 9:35:27 AM6/22/21
to cython...@googlegroups.com
Thanks to all for the replies - sorry that our messages crossed because of moderation.

--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/EWWL0f5LIZ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages