O02, following the type pointer - #132
Merged
Merged
Conversation
The second lesson of the object model part. O01 stopped at the second word of the header, which is a pointer to the type. This one follows it. The lesson reads the type struct from Python: basicsize and itemsize and what they do and do not predict, the flags word and the one bit behind cannot set attribute of immutable type, and the two offset fields where a negative number means one thing except when it is minus one, which is a sentinel meaning the interpreter manages the dict itself and the pointer lives three words in front of the object. It closes with the class statement, which compiles to a call to __build_class__ and nothing else. Three glossary terms: static type, heap type and metaclass. Six diagrams. Two cells differ between 3.14 and 3.15 and both carry a note.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
O01 read the sixteen bytes at the front of an object and stopped at the second word, which is a pointer to the type. This lesson follows it.
What is on the other end is the largest struct in the interpreter, and almost all of it is answers to questions about instances rather than about the type. Most of those answers are readable from Python with no C at all.
What the lesson covers:
typein at most two steps, andtypepoints at itself, because the chain has to end somewhere and a loop is cheaper than a special case every lookup would have to check fortp_basicsizeandtp_itemsizeare the two numbers that say how big one instance is, and multiplying them out matches__sizeof__for tuples and bytes and misses badly forlist, whose items are a separate allocationtype.__basicsize__is nine hundred odd bytes becausePyType_Typesets its own basicsize tosizeof(PyHeapTypeObject), so an instance oftypeis a class and a class carries all the operator tables inlinePy_TPFLAGS_IMMUTABLETYPEis the entire reasonint.nope = 1raises andPlain.nope = 1does nottp_dictoffsethas four meanings: zero, a positive offset, a negative offset counted from the end of a var sized object, and exactly-1, which is a sentinel and not an offset, asserted against in the code that resolves the ordinary ones-1means is that the pointer lives in front of the object, three pointers back, with the weakref list one further back and the two words of GC pre header in between, which is whysys.getsizeofreports thirty two bytes more than an instance claims for itself__build_class__runs it, and the metaclass turns the namespace into a type, sotype("Greeter", (), {"hi": hi})comes out with the same flags, basicsize and mroEverything is cited against
v3.15.0rc1:_typeobject,PyHeapTypeObject,PyType_Type,Py_TPFLAGS_HEAPTYPE,tp_dictoffset,tp_weaklistoffset,_PyObject_ComputedDictPointer,MANAGED_DICT_OFFSET,_PyType_PreHeaderSize,_PyType_AllocNoTrackandbuiltin___build_class__.Six diagrams. The notebook runs in Colab and all nine of its cells run in the browser probe, which is now at 30 lessons and 319 cells.
Two cells differ between 3.14 and 3.15 and both say so:
type.__basicsize__moved from 936 to 944, and the implicit return at the end of a module went fromLOAD_CONSTtoLOAD_COMMON_CONSTANT. Two more carry a note about the browser, where a pointer is four bytes and__weakrefoffset__reads minus sixteen.Three glossary terms went in: static type, heap type and metaclass.
type objectpicked up a citation and two cross references while I was there. GLOSSARY.md is at 111.Part of #23.