Problems with subclassing enum34

T

Thomas Heller

trying out the enum34 module.

What I want to create is a subclass of enum.Enum that is also
based on ctypes.c_int so that I can better use enum instances
in ctypes api calls.

When I do this, I get a metaclass conflict:

.... FOOBAR = 0
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases


When I do this, it does not work either:
.... pass
........ FOOBAR = 42
.... __metaclass__ = MyEnum_meta
....
It should have printed '<MyEnum.FOOBAR: 42>'.

Any ideas?

Thanks,
Thomas
 
R

Robert Kern

trying out the enum34 module.

What I want to create is a subclass of enum.Enum that is also
based on ctypes.c_int so that I can better use enum instances
in ctypes api calls.

When I do this, I get a metaclass conflict:


... FOOBAR = 0
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict)
subclass of the metaclasses of all its bases



When I do this, it does not work either:

... pass

enum.EnumMeta uses super() in its __new__() implementation but
_ctypes.PyCSimpleType doesn't. Thus, only _ctypes.PyCSimpleType.__new__() gets a
chance to run. Switching the order of the two might work.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
8

88888 Dihedral

Thomas Helleræ–¼ 2013å¹´6月28日星期五UTC+8下åˆ6時48分38秒寫é“:
trying out the enum34 module.



What I want to create is a subclass of enum.Enum that is also

based on ctypes.c_int so that I can better use enum instances

in ctypes api calls.



When I do this, I get a metaclass conflict:






... FOOBAR = 0

...

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: Error when calling the metaclass bases

metaclass conflict: the metaclass of a derived class must be a

(non-strict) subclass of the metaclasses of all its bases








When I do this, it does not work either:




... pass

...


... FOOBAR = 42

... __metaclass__ = MyEnum_meta

...




It should have printed '<MyEnum.FOOBAR: 42>'.



Any ideas?



Thanks,

Thomas

Just use a dictionary for the job. Python i not c/c++.
 
E

Ethan Furman

trying out the enum34 module.

What I want to create is a subclass of enum.Enum that is also
based on ctypes.c_int so that I can better use enum instances
in ctypes api calls.

Have you tried using enum.IntEnum? If you were able to pass ints in before, IntEnum should work.
 
T

Thomas Heller

Am 28.06.2013 17:16, schrieb Ethan Furman:
Have you tried using enum.IntEnum? If you were able to pass ints in
before, IntEnum should work.

I'm sure that IntEnum works as expected, but I need enums that are
subclasses of ctypes.c_int (so that argument type checking and return
value conversions in ctypes api calls work).

Robert Kern:
enum.EnumMeta uses super() in its __new__() implementation but
_ctypes.PyCSimpleType doesn't. Thus, only
_ctypes.PyCSimpleType.__new__() gets a chance to run. Switching the
order of the two might work.

Robert found the problem but I'm unsure if there is a solution.
Also I'm unsure whether this is a bug in ctypes or in enum or if
they are simply incompatible.

Thomas
 
T

Thomas Heller

Am 28.06.2013 17:25, schrieb Thomas Heller:
Robert Kern:


Robert found the problem but I'm unsure if there is a solution.
Also I'm unsure whether this is a bug in ctypes or in enum or if
they are simply incompatible.

I forgot to mention that switching the order of metaclasses didn't work.

Thomas
 
R

Robert Kern

Am 28.06.2013 17:25, schrieb Thomas Heller:

I forgot to mention that switching the order of metaclasses didn't work.

You may also need to manually deal with the conflict between Enum.__new__() and
c_int.__new__().

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
E

Ethan Furman

Am 28.06.2013 17:25, schrieb Thomas Heller:

I forgot to mention that switching the order of metaclasses didn't work.

Here's the traceback:

Traceback (most recent call last):
File "ct.py", line 7, in <module>
class MyEnum(ctypes.c_int, Enum):
File "/home/ethan/source/enum/enum/py2_enum.py", line 149, in __new__
enum_class = super(EnumMeta, metacls).__new__(metacls, cls, bases, classdict)
TypeError: Error when calling the metaclass bases
_ctypes.PyCSimpleType.__new__(MyEnum_meta) is not safe, use type.__new__()

Not sure how to fix that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top