User-defined object creation

I am trying to understand some implementation details of CPython. The built-in objects, such as int, str, tuple, list, etc., are defined in the CPython’s Objects folder. However, I don’t see any files related to user-defined objects. So, I have the following question.

How does CPython internally create an instance object from a user-defined class?

For example, built-in objects/classes typically have a new function, such as PyTuple_New() and PyLong_New(), defined in the respective file. For user-defined classes, I imagine that the parser will first convert the class definition (the Python code) into some C data structures. Then, some C APIs will be used to inspect the data structures and find the correct way to instantiate the class.

If the logic sounds correct, I wonder

  1. What data structures are used to represent the class definition? What files define them?
  2. What C APIs are used to create instance objects from those data structures? Where can I find an example of this logic in the CPython source code, if possible?

Thank you!

A type object, see typeobject

In the end, a call of the type instance, see type_call. I don’t know if there is a nice wrapper for this operation outside of the wrappers for calls in general, because of the ability to have potentially arbitrary metaclass.

Note that this is a quite complex process, I would also suggest to read the Data model docs

1 Like

Objects in general are represented as PyObject, in object.c. The attributes are stored in a specially optimized kind of dictionary, which is pointed at by tp_dict.

The classes themselves are represented as PyTypeObject, implemented in typeobject.c.

First, it follows the logic of the class’ __new__, which will normally fall through to the built-in implementation of object.__new__, which allocates a plain PyObject in C. Then when __init__ is called, that potentially sets attributes on the already-created object.

However, at least part of the actual allocation logic is in gc.c rather than object.c, and there are also a bunch of macro wrappers in headers like objimpl.h.

1 Like