I have been read pickle source code recently.
The following code in copy_reg
make me confused:
_HEAPTYPE = 1<<9
def _reduce_ex(self, proto):
assert proto < 2
for base in self.__class__.__mro__:
if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
break
else:
base = object # not really reachable
if base is object:
state = None
So what does __flags__
using for?
I found it is defined in type
object:
type.__flags__ = 2148423147
I was tried to search it in the official doc, but nothing was found.
But interesting thing is that __class__.__flags__ & _HEAPTYPE
is always 0
when the __class__
is python internal type. And the result will be 1
when __class__
is a subclass of python internal type.
Can anyone help me to solve this puzzle?
__flags__
is a wrapper, to access CPython type object structure membertp_flags
, constants used to compose this flag defined in object.h, following is quoted from the source:See more detail on python document on
tp_flags
.Subclass of python built-in type, like other user defined type, allocated on heap with
PyType_GenericAlloc()
.to break down
type.__flags__
:yields:
__flags__
is to be viewed as binary, if you want to see the flags.These type flags are defined here in Python's source code.