Problems with subclassing enum34

2013-06-28 Thread 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:


 class MyEnum(ctypes.c_int, enum.Enum):
...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:

 class MyEnum_meta(type(ctypes.c_int), type(enum.Enum)):
... pass
...
 class MyEnum(ctypes.c_int, enum.Enum):
... FOOBAR = 42
... __metaclass__ = MyEnum_meta
...
 MyEnum.FOOBAR
42


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

Any ideas?

Thanks,
Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with subclassing enum34

2013-06-28 Thread Robert Kern

On 2013-06-28 11:48, Thomas Heller wrote:

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:


  class MyEnum(ctypes.c_int, enum.Enum):
...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:

  class MyEnum_meta(type(ctypes.c_int), type(enum.Enum)):
... 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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with subclassing enum34

2013-06-28 Thread 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:
 
 
 
 
 
   class MyEnum(ctypes.c_int, enum.Enum):
 
 ...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:
 
 
 
   class MyEnum_meta(type(ctypes.c_int), type(enum.Enum)):
 
 ... pass
 
 ...
 
   class MyEnum(ctypes.c_int, enum.Enum):
 
 ... FOOBAR = 42
 
 ... __metaclass__ = MyEnum_meta
 
 ...
 
   MyEnum.FOOBAR
 
 42
 
  
 
 
 
 It should have printed 'MyEnum.FOOBAR: 42'.
 
 
 
 Any ideas?
 
 
 
 Thanks,
 
 Thomas

Just use a dictionary for the job. Python i not c/c++.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with subclassing enum34

2013-06-28 Thread Ethan Furman

On 06/28/2013 03:48 AM, Thomas Heller wrote:

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.

--
~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with subclassing enum34

2013-06-28 Thread Thomas Heller

Am 28.06.2013 17:16, schrieb Ethan Furman:

On 06/28/2013 03:48 AM, Thomas Heller wrote:

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.


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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with subclassing enum34

2013-06-28 Thread Thomas Heller

Am 28.06.2013 17:25, schrieb Thomas Heller:

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.


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

Thomas

--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with subclassing enum34

2013-06-28 Thread Robert Kern

On 2013-06-28 16:32, Thomas Heller wrote:

Am 28.06.2013 17:25, schrieb Thomas Heller:

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.


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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with subclassing enum34

2013-06-28 Thread Ethan Furman

On 06/28/2013 08:32 AM, Thomas Heller wrote:

Am 28.06.2013 17:25, schrieb Thomas Heller:

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.


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.

--
~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list