STINNER Victor <[email protected]> added the comment:
CreateSwappedType() is an helper function used by the _ctypes.PyCSimpleType
type. Python script reproducing this ctypes bug:
---
class PyCSimpleType(type):
def __new__(cls, name, bases, dct):
print(f"PyCSimpleType: create {name} class")
cell = dct.get('__classcell__', None)
# type.__new__() sets __classcell__
x = super().__new__(cls, name, bases, dct)
if cell is not None:
print(f"PyCSimpleType: after first type.__new__() call:
__classcell__={cell.cell_contents}")
name2 = name + "_Swapped"
dct2 = dict(dct, __qualname__=name2)
# Calling type.__new__() again with the same cell object overrides
# __classcell__
x.__ctype_be__ = super().__new__(cls, name2, bases, dct2)
if cell is not None:
print(f"PyCSimpleType: after second type.__new__() call:
__classcell__={cell.cell_contents}")
return x
class BaseItem:
pass
class Item(BaseItem, metaclass=PyCSimpleType):
def get_class(self):
# get '__class__' to create a closure
return __class__
# Alternative to create a closure:
#def __repr__(self):
# return super().__repr__()
---
Output:
---
PyCSimpleType: create Item class
PyCSimpleType: after first type.__new__() call: __classcell__=<class
'__main__.Item'>
PyCSimpleType: after second type.__new__() call: __classcell__=<class
'__main__.Item_Swapped'>
Traceback (most recent call last):
File "meta.py", line 23, in <module>
class Item(BaseItem, metaclass=PyCSimpleType):
TypeError: __class__ set to <class '__main__.Item_Swapped'> defining 'Item' as
<class '__main__.Item'>
---
It's not a bug in Python types, but a bug specific to the _ctypes.PyCSimpleType
type which prevents creating subclasses which use closures ("__class__",
"super()", etc.).
----------
nosy: +vstinner
title: super call in ctypes subclass fails -> ctypes: fail to create a
_ctypes._SimpleCData subclass using a closure like calling super() without
arguments
versions: +Python 3.11 -Python 3.8
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue29270>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com