Slot Wrapper Python
Whenever PythonQt encounters a CPP pointer (e.g. On a slot or signal) and it does not known it as a QObject derived class, it will create a generic CPP wrapper. So even unknown C objects can be passed through Python. If the wrapper factory supports the CPP class, a QObject wrapper will be created for each instance that enters Python. This difference is an implementation detail that shows up because of internal C-level slots that PyPy does not have. On CPython,.add is a method-wrapper, and list.add is a slot wrapper. On PyPy these are normal bound or unbound method objects. This can occasionally confuse some tools that inspect built-in types. So if there is a built-in base class that is not object it fails, even if it's still wrapping the standard implementation of that slot. I would consider this a bug in Python-it shouldn't be doing identity comparison on the slot wrapper object itself. Slot wrappers¶ A slot wrapper is installed in the dict of an extension type to access a special method implemented in C. For example, object.init or Integer.lt. Note that slot wrappers are always unbound (there is a bound variant called method-wrapper). Hi Raymond, The signature matters because the current code in updateoneslot forgets to set the usegeneric flag when slots have different wrappers. This causes that the slot from the base class is left in the new type. Slots have different wrappers when their signature differs.
There are a large number of structures which are used in the definition ofobject types for Python. This section describes these structures and how theyare used.
All Python objects ultimately share a small number of fields at the beginningof the object’s representation in memory. These are represented by thePyObject
and PyVarObject
types, which are defined, in turn,by the expansions of some macros also used, whether directly or indirectly, inthe definition of all other Python objects.
PyObject
¶All object types are extensions of this type. This is a type whichcontains the information Python needs to treat a pointer to an object as anobject. In a normal “release” build, it contains only the object’sreference count and a pointer to the corresponding type object.Nothing is actually declared to be a PyObject
, but every pointerto a Python object can be cast to a PyObject*
. Access to themembers must be done by using the macros Py_REFCNT
andPy_TYPE
.
PyVarObject
¶This is an extension of PyObject
that adds the ob_size
field. This is only used for objects that have some notion of length.This type does not often appear in the Python/C API.Access to the members must be done by using the macrosPy_REFCNT
, Py_TYPE
, and Py_SIZE
.
PyObject_HEAD
¶This is a macro used when declaring new types which represent objectswithout a varying length. The PyObject_HEAD macro expands to:
See documentation of PyObject
above.
PyObject_VAR_HEAD
¶This is a macro used when declaring new types which represent objectswith a length that varies from instance to instance.The PyObject_VAR_HEAD macro expands to:
See documentation of PyVarObject
above.
Py_TYPE
(o)¶This macro is used to access the ob_type
member of a Python object.It expands to:
Py_REFCNT
(o)¶This macro is used to access the ob_refcnt
member of a Pythonobject.It expands to:
Py_SIZE
(o)¶This macro is used to access the ob_size
member of a Python object.It expands to:
PyObject_HEAD_INIT
(type)¶This is a macro which expands to initialization values for a newPyObject
type. This macro expands to:
PyVarObject_HEAD_INIT
(type, size)¶This is a macro which expands to initialization values for a newPyVarObject
type, including the ob_size
field.This macro expands to:
PyCFunction
¶Type of the functions used to implement most Python callables in C.Functions of this type take two PyObject*
parameters and returnone such value. If the return value is NULL, an exception shall havebeen set. If not NULL, the return value is interpreted as the returnvalue of the function as exposed in Python. The function must return a newreference.
PyCFunctionWithKeywords
¶Type of the functions used to implement Python callables in C that takekeyword arguments: they take three PyObject*
parameters and returnone such value. See PyCFunction
above for the meaning of the returnvalue.
PyMethodDef
¶Structure used to describe a method of an extension type. This structure hasfour fields:
Field | C Type | Meaning |
---|---|---|
ml_name | char * | name of the method |
ml_meth | PyCFunction | pointer to the Cimplementation |
ml_flags | int | flag bits indicating how thecall should be constructed |
ml_doc | char * | points to the contents of thedocstring |
The ml_meth
is a C function pointer. The functions may be of differenttypes, but they always return PyObject*
. If the function is not ofthe PyCFunction
, the compiler will require a cast in the method table.Even though PyCFunction
defines the first parameter asPyObject*
, it is common that the method implementation uses thespecific C type of the self object.
The ml_flags
field is a bitfield which can include the following flags.The individual flags indicate either a calling convention or a bindingconvention. Of the calling convention flags, only METH_VARARGS
andMETH_KEYWORDS
can be combined. Any of the calling convention flagscan be combined with a binding flag.
METH_VARARGS
¶This is the typical calling convention, where the methods have the typePyCFunction
. The function expects two PyObject*
values.The first one is the self object for methods; for module functions, it isthe module object. The second parameter (often called args) is a tupleobject representing all arguments. This parameter is typically processedusing PyArg_ParseTuple()
or PyArg_UnpackTuple()
.
METH_KEYWORDS
¶Methods with these flags must be of type PyCFunctionWithKeywords
.The function expects three parameters: self, args, and a dictionary ofall the keyword arguments. The flag must be combined withMETH_VARARGS
, and the parameters are typically processed usingPyArg_ParseTupleAndKeywords()
.
METH_NOARGS
¶Methods without parameters don’t need to check whether arguments are given ifthey are listed with the METH_NOARGS
flag. They need to be of typePyCFunction
. The first parameter is typically named self and willhold a reference to the module or object instance. In all cases the secondparameter will be NULL.
METH_O
¶Methods with a single object argument can be listed with the METH_O
flag, instead of invoking PyArg_ParseTuple()
with a 'O'
argument.They have the type PyCFunction
, with the self parameter, and aPyObject*
parameter representing the single argument.
These two constants are not used to indicate the calling convention but thebinding when use with methods of classes. These may not be used for functionsdefined for modules. At most one of these flags may be set for any givenmethod.
METH_CLASS
¶The method will be passed the type object as the first parameter ratherthan an instance of the type. This is used to create class methods,similar to what is created when using the classmethod()
built-infunction.
METH_STATIC
¶The method will be passed NULL as the first parameter rather than aninstance of the type. This is used to create static methods, similar towhat is created when using the staticmethod()
built-in function.
One other constant controls whether a method is loaded in place of anotherdefinition with the same method name.
METH_COEXIST
¶The method will be loaded in place of existing definitions. WithoutMETH_COEXIST, the default is to skip repeated definitions. Since slotwrappers are loaded before the method table, the existence of asq_contains slot, for example, would generate a wrapped method named__contains__()
and preclude the loading of a correspondingPyCFunction with the same name. With the flag defined, the PyCFunctionwill be loaded in place of the wrapper object and will co-exist with theslot. This is helpful because calls to PyCFunctions are optimized morethan wrapper object calls.
PyMemberDef
¶Python Wrapper Class
Structure which describes an attribute of a type which corresponds to a Cstruct member. Its fields are:
Field | C Type | Meaning |
---|---|---|
name | char * | name of the member |
type | int | the type of the member in theC struct |
offset | Py_ssize_t | the offset in bytes that themember is located on thetype’s object struct |
flags | int | flag bits indicating if thefield should be read-only orwritable |
doc | char * | points to the contents of thedocstring |
type
can be one of many T_
macros corresponding to various Ctypes. When the member is accessed in Python, it will be converted to theequivalent Python type.
Macro name | C type |
---|---|
T_SHORT | short |
T_INT | int |
T_LONG | long |
T_FLOAT | float |
T_DOUBLE | double |
T_STRING | char * |
T_OBJECT | PyObject * |
T_OBJECT_EX | PyObject * |
T_CHAR | char |
T_BYTE | char |
T_UBYTE | unsigned char |
T_UINT | unsigned int |
T_USHORT | unsigned short |
T_ULONG | unsigned long |
T_BOOL | char |
T_LONGLONG | long long |
T_ULONGLONG | unsigned long long |
T_PYSSIZET | Py_ssize_t |
T_OBJECT
and T_OBJECT_EX
differ in thatT_OBJECT
returns None
if the member is NULL andT_OBJECT_EX
raises an AttributeError
. Try to useT_OBJECT_EX
over T_OBJECT
because T_OBJECT_EX
handles use of the del
statement on that attribute more correctlythan T_OBJECT
.
flags
can be 0
for write and read access or READONLY
forread-only access. Using T_STRING
for type
impliesREADONLY
. Only T_OBJECT
and T_OBJECT_EX
members can be deleted. (They are set to NULL).
PyGetSetDef
¶Structure to define property-like access for a type. See also description ofthe PyTypeObject.tp_getset
slot.
Field | C Type | Meaning |
---|---|---|
name | char * | attribute name |
get | getter | C Function to get the attribute |
set | setter | optional C function to set ordelete the attribute, if omittedthe attribute is readonly |
doc | char * | optional docstring |
closure | void * | optional function pointer,providing additional data forgetter and setter |
The get
function takes one PyObject*
parameter (theinstance) and a function pointer (the associated closure
):
It should return a new reference on success or NULL with a set exceptionon failure.
set
functions take two PyObject*
parameters (the instance andthe value to be set) and a function pointer (the associated closure
):
In case the attribute should be deleted the second parameter is NULL.Should return 0
on success or -1
with a set exception on failure.
In Python every class can have instance attributes. By default Pythonuses a dict to store an object’s instance attributes. This is reallyhelpful as it allows setting arbitrary new attributes at runtime.
However, for small classes with known attributes it might be abottleneck. The dict
wastes a lot of RAM. Python can’t just allocatea static amount of memory at object creation to store all theattributes. Therefore it sucks a lot of RAM if you create a lot ofobjects (I am talking in thousands and millions). Still there is a wayto circumvent this issue. It involves the usage of __slots__
totell Python not to use a dict, and only allocate space for a fixed setof attributes. Here is an example with and without __slots__
:
Without__slots__
:
With__slots__
:
The second piece of code will reduce the burden on your RAM. Some peoplehave seen almost 40 to 50% reduction in RAM usage by using thistechnique.
On a sidenote, you might want to give PyPy a try. It does all of theseoptimizations by default.
How To Use Python Wrapper
Below you can see an example showing exact memory usage with and without __slots__
done in IPython thanks to https://github.com/ianozsvald/ipython_memory_usage