python 类型中的 __flags__ 有什么用

What does __flags__ in python type used for

最近看了pickle源码

copy_reg中的以下代码让我感到困惑:

_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

那么 __flags__ 的用途是什么?

我发现它是在 type 对象中定义的:

type.__flags__ = 2148423147

我试图在官方文档中搜索它,但没有找到。

但有趣的是,当 __class__ 是 python 内部类型时,__class__.__flags__ & _HEAPTYPE 总是 0。当 __class__ 是 python 内部类型的子类时,结果将是 1

谁能帮我解开这个谜题?

如果要查看标志,

__flags__ 将被视为二进制文件。

这些类型标志在 Python 的源代码中定义 here

__flags__ 是一个包装器,to access CPython type object structure member tp_flags, constants used to compose this flag defined in object.h,以下引自来源:

Type flags (tp_flags) These flags are used to extend the type structure in a backwards-compatible fashion. Extensions can use the flags to indicate (and test) when a given type structure contains a new feature. The Python core will use these when introducing new functionality between major revisions (to avoid mid-version changes in the PYTHON_API_VERSION).

查看 python document on tp_flags 的更多详细信息。

But interesting thing is that class.flags & _HEAPTYPE is always 0 when the class is a python internal type. And the result will be 1 when class is a subclass of python internal type.

python 内置类型的子类,与其他用户定义的类型一样,在堆上分配 PyType_GenericAlloc()


分解type.__flags__:

import re

def flags_to_name(type_obj):
    tp_flag_consts = {}        
    with open('/path/to/Include/object.h') as f:
        for l in f:
            m = re.search(r'^#define (Py_TPFLAGS_\w+)\s+\(.+?<< (\d+)\)', l.strip())
            if m:
                tp_flag_consts[int(m.group(2))] = m.group(1)
    bin_str = bin(type_obj.__flags__)[2:][::-1]
    return ', '.join(tp_flag_consts[n] for n, c in enumerate(bin_str) if c == '1')

print(flags_to_name(type))

产量:

Py_TPFLAGS_BASETYPE, Py_TPFLAGS_READY, Py_TPFLAGS_HAVE_GC, Py_TPFLAGS_HAVE_VERSION_TAG, Py_TPFLAGS_VALID_VERSION_TAG, Py_TPFLAGS_TYPE_SUBCLASS