순환 가비지 수집 지원¶
순환 참조를 포함하는 가비지를 탐지하고 수집하는 파이썬의 지원은 역시 컨테이너일 수 있는 다른 객체의 “컨테이너” 인 객체 형의 지원이 필요합니다. 다른 객체에 대한 참조를 저장하지 않거나, 원자 형(가령 숫자나 문자열)에 대한 참조만 저장하는 형은 가비지 수집에 대한 어떤 명시적인 지원을 제공할 필요가 없습니다.
컨테이너형을 만들려면, 형 객체의 tp_flags 필드가 Py_TPFLAGS_HAVE_GC를 포함해야 하고 tp_traverse 처리기 구현을 제공해야 합니다. 형의 인스턴스가 가변이면, tp_clear 구현도 제공해야 합니다.
Py_TPFLAGS_HAVE_GC이 플래그가 설정된 형의 객체는 여기에 설명된 규칙을 준수해야 합니다. 편의를 위해 이러한 객체를 컨테이너 객체라고 하겠습니다.
컨테이너형의 생성자는 두 가지 규칙을 준수해야 합니다:
객체의 메모리는
PyObject_GC_New나PyObject_GC_NewVar를 사용하여 할당해야 합니다.다른 컨테이너에 대한 참조를 포함할 수 있는 모든 필드가 초기화되면,
PyObject_GC_Track()를 호출해야 합니다.
마찬가지로, 객체의 할당해제자(deallocator)는 비슷한 규칙 쌍을 준수해야 합니다:
다른 컨테이너를 참조하는 필드가 무효화 되기 전에,
PyObject_GC_UnTrack()를 호출해야 합니다.객체의 메모리는
PyObject_GC_Del()를 사용하여 할당 해제되어야 합니다.경고
If a type adds the Py_TPFLAGS_HAVE_GC, then it must implement at least a
tp_traversehandler or explicitly use one from its subclass or subclasses.When calling
PyType_Ready()or some of the APIs that indirectly call it likePyType_FromSpecWithBases()orPyType_FromSpec()the interpreter will automatically populate thetp_flags,tp_traverseandtp_clearfields if the type inherits from a class that implements the garbage collector protocol and the child class does not include thePy_TPFLAGS_HAVE_GCflag.
-
PyObject_GC_New(TYPE, typeobj)¶
PyObject_New와 유사하지만,Py_TPFLAGS_HAVE_GC플래그가 설정된 컨테이너 객체를 위한 것.Do not call this directly to allocate memory for an object; call the type’s
tp_allocslot instead.When populating a type’s
tp_allocslot,PyType_GenericAlloc()is preferred over a custom function that simply calls this macro.Memory allocated by this macro must be freed with
PyObject_GC_Del()(usually called via the object’stp_freeslot).
-
PyObject_GC_NewVar(TYPE, typeobj, size)¶
PyObject_NewVar와 유사하지만,Py_TPFLAGS_HAVE_GC플래그가 설정된 컨테이너 객체를 위한 것.Do not call this directly to allocate memory for an object; call the type’s
tp_allocslot instead.When populating a type’s
tp_allocslot,PyType_GenericAlloc()is preferred over a custom function that simply calls this macro.Memory allocated by this macro must be freed with
PyObject_GC_Del()(usually called via the object’stp_freeslot).
-
PyObject *PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *type, size_t extra_size)¶
- 이것은 불안정 API. It may change without warning in minor releases.
Analogous to
PyObject_GC_Newbut allocates extra_size bytes at the end of the object (at offsettp_basicsize). The allocated memory is initialized to zeros, except for thePython object header.The extra data will be deallocated with the object, but otherwise it is not managed by Python.
Memory allocated by this function must be freed with
PyObject_GC_Del()(usually called via the object’stp_freeslot).경고
The function is marked as unstable because the final mechanism for reserving extra data after an instance is not yet decided. For allocating a variable number of fields, prefer using
PyVarObjectandtp_itemsizeinstead.Added in version 3.12.
-
PyObject_GC_Resize(TYPE, op, newsize)¶
PyObject_NewVar에 의해 할당된 객체의 크기를 변경합니다. 형TYPE*(모든 C 형을 나타냅니다) 의 크기가 조정된 객체나 실패하면NULL을 반환합니다.op는 형 PyVarObject*이어야 하고 아직 수거기가 추적하지 않아야 합니다. newsize는 형
Py_ssize_t이어야 합니다.
-
void PyObject_GC_Track(PyObject *op)¶
- Part of the 안정 ABI.
수거기가 추적하는 컨테이너 객체 집합에 객체 op를 추가합니다. 수거기는 예기치 않은 시간에 실행될 수 있으므로 추적되는 동안 객체가 유효해야 합니다.
tp_traverse처리기가 탐색하는 모든 필드가 유효해지면 호출해야 합니다, 보통 생성자의 끝부분 근처입니다.
-
int PyObject_IS_GC(PyObject *obj)¶
객체가 가비지 수거기 프로토콜을 구현하면 0이 아닌 값을 반환하고, 그렇지 않으면 0을 반환합니다.
이 함수가 0을 반환하면 가비지 수거기가 객체를 추적할 수 없습니다.
-
int PyObject_GC_IsTracked(PyObject *op)¶
- Part of the 안정 ABI 버전 3.9 이후로.
op의 객체 형이 GC 프로토콜을 구현하고 op가 현재 가비지 수거기가 추적 중이면 1을 반환하고 그렇지 않으면 0을 반환합니다.
이것은 파이썬 함수
gc.is_tracked()에 해당합니다.Added in version 3.9.
-
int PyObject_GC_IsFinalized(PyObject *op)¶
- Part of the 안정 ABI 버전 3.9 이후로.
op의 객체 형이 GC 프로토콜을 구현하고 가비지 수거기가 op를 이미 파이널라이즈 했으면 1을 반환하고 그렇지 않으면 0을 반환합니다.
이것은 파이썬 함수
gc.is_finalized()에 해당합니다.Added in version 3.9.
-
void PyObject_GC_Del(void *op)¶
- Part of the 안정 ABI.
PyObject_GC_New나PyObject_GC_NewVar를 사용하여 객체에 할당된 메모리를 해제합니다.Do not call this directly to free an object’s memory; call the type’s
tp_freeslot instead.Do not use this for memory allocated by
PyObject_New,PyObject_NewVar, or related allocation functions; usePyObject_Free()instead.더 보기
PyObject_Free()is the non-GC equivalent of this function.
-
void PyObject_GC_UnTrack(void *op)¶
- Part of the 안정 ABI.
수거기가 추적하는 컨테이너 객체 집합에서 op 객체를 제거합니다.
PyObject_GC_Track()를 이 객체에 대해 다시 호출하여 추적 객체 집합에 다시 추가할 수 있음에 유의하십시오. 할당해제자(tp_dealloc처리기)는tp_traverse처리기에서 사용하는 필드가 무효화 되기 전에 객체에 대해 이 함수를 호출해야 합니다.
버전 3.8에서 변경: _PyObject_GC_TRACK()과 _PyObject_GC_UNTRACK() 매크로는 공용 C API에서 제거되었습니다.
tp_traverse 처리기는 다음과 같은 형의 함수 매개 변수를 받아들입니다:
-
typedef int (*visitproc)(PyObject *object, void *arg)¶
- Part of the 안정 ABI.
tp_traverse처리기에 전달되는 방문자 함수의 형. 이 함수는 탐색하는 객체를 object로,tp_traverse처리기의 세 번째 매개 변수를 arg로 호출되어야 합니다. 파이썬 코어는 순환 가비지 탐지를 구현하기 위해 여러 방문자 함수를 사용합니다; 사용자가 자신의 방문자 함수를 작성해야 할 필요는 없습니다.
tp_clear 처리기는 inquiry 형이거나 객체가 불변이면 NULL이어야 합니다.
-
typedef int (*inquiry)(PyObject *self)¶
- Part of the 안정 ABI.
참조 순환을 생성했을 수 있는 참조를 삭제합니다. 불변 객체는 참조 순환을 직접 생성할 수 없으므로, 이 메서드를 정의 할 필요가 없습니다. 이 메서드를 호출한 후에도 객체가 유효해야 합니다 (단지 참조에 대해
Py_DECREF()를 호출하지 마십시오). 이 객체가 참조 순환에 참여하고 있음을 수거기가 감지하면 이 메서드를 호출합니다.
Traversal¶
tp_traverse 처리기는 다음 형이어야 합니다:
-
typedef int (*traverseproc)(PyObject *self, visitproc visit, void *arg)¶
- Part of the 안정 ABI.
Traversal function for a garbage-collected object, used by the garbage collector to detect reference cycles. Implementations must call the visit function for each object directly contained by self, with the parameters to visit being the contained object and the arg value passed to the handler. The visit function must not be called with a
NULLobject argument. If visit returns a non-zero value, that value should be returned immediately.A typical
tp_traversefunction calls thePy_VISIT()convenience macro on each of the instance’s members that are Python objects that the instance owns. For example, this is a (slightly outdated) traversal function for thethreading.localclass:static int local_traverse(PyObject *op, visitproc visit, void *arg) { localobject *self = (localobject *) op; Py_VISIT(Py_TYPE(self)); Py_VISIT(self->args); Py_VISIT(self->kw); Py_VISIT(self->dict); return 0; }
참고
Py_VISIT()requires the visit and arg parameters tolocal_traverse()to have these specific names; don’t name them just anything.Instances of heap-allocated types hold a reference to their type. Their traversal function must therefore visit the type:
Py_VISIT(Py_TYPE(self));
Alternately, the type may delegate this responsibility by calling
tp_traverseof a heap-allocated superclass (or another heap-allocated type, if applicable). If they do not, the type object may not be garbage-collected.If the
Py_TPFLAGS_MANAGED_DICTbit is set in thetp_flagsfield, the traverse function must callPyObject_VisitManagedDict()like this:int err = PyObject_VisitManagedDict((PyObject*)self, visit, arg); if (err) { return err; }
Only the members that the instance owns (by having strong references to them) must be visited. For instance, if an object supports weak references via the
tp_weaklistslot, the pointer supporting the linked list (what tp_weaklist points to) must not be visited as the instance does not directly own the weak references to itself.The traversal function has a limitation:
경고
The traversal function must not have any side effects. Implementations may not modify the reference counts of any Python objects nor create or destroy any Python objects, directly or indirectly.
This means that most Python C API functions may not be used, since they can raise a new exception, return a new reference to a result object, have internal logic that uses side effects. Also, unless documented otherwise, functions that happen to not have side effects may start having them in future versions, without warning.
For a list of safe functions, see a separate section below.
참고
The
Py_VISIT()call may be skipped for those members that provably cannot participate in reference cycles. In thelocal_traverseexample above, there is also aself->keymember, but it can only beNULLor a Python string and therefore cannot be part of a reference cycle.On the other hand, even if you know a member can never be part of a cycle, as a debugging aid you may want to visit it anyway just so the
gcmodule’sget_referents()function will include it.참고
The
tp_traversefunction can be called from any thread.CPython 구현 상세: Garbage collection is a “stop-the-world” operation: even in free threading builds, only one thread state is attached when
tp_traversehandlers run.버전 3.9에서 변경: Heap-allocated types are expected to visit
Py_TYPE(self)intp_traverse. In earlier versions of Python, due to bug 40217, doing this may lead to crashes in subclasses.
To simplify writing tp_traverse handlers,
a Py_VISIT() macro is provided.
In order to use this macro, the tp_traverse
implementation must name its arguments exactly visit and arg:
-
Py_VISIT(o)¶
If the PyObject* o is not
NULL, call the visit callback, with arguments o and arg. If visit returns a non-zero value, then return it.This corresponds roughly to:
#define Py_VISIT(o) \ if (op) { \ int visit_result = visit(o, arg); \ if (visit_result != 0) { \ return visit_result; \ } \ }
Traversal-safe functions¶
The following functions and macros are safe to use in a
tp_traverse handler:
the visit function passed to
tp_traversePy_TYPE(): if called from atp_traversehandler,Py_TYPE()’s result will be valid for the duration of the handler callPyObject_TypeCheck(),PyType_IsSubtype(),PyType_HasFeature()Py<type>_CheckandPy<type>_CheckExact– for example,PyTuple_Check()
“DuringGC” functions¶
The following functions should only be used in a
tp_traverse handler; calling them in other
contexts may have unintended consequences.
These functions act like their counterparts without the _DuringGC suffix,
but they are guaranteed to not have side effects, they do not set an exception
on failure, and they return/set borrowed references
as detailed in the individual documentation.
Note that these functions may fail (return NULL or -1),
but as they do not set an exception, no error information is available.
In some cases, failure is not distinguishable from a successful NULL result.
-
void *PyObject_GetTypeData_DuringGC(PyObject *o, PyTypeObject *cls)¶
-
void *PyObject_GetItemData_DuringGC(PyObject *o)¶
-
void *PyType_GetModuleState_DuringGC(PyTypeObject *type)¶
-
void *PyModule_GetState_DuringGC(PyObject *module)¶
-
int PyModule_GetToken_DuringGC(PyObject *module, void **result)¶
- Part of the 안정 ABI 버전 3.15 이후로.
See “DuringGC” functions for common information.
Added in version 3.15.0a8 (unreleased).
-
int PyType_GetBaseByToken_DuringGC(PyTypeObject *type, void *tp_token, PyTypeObject **result)¶
- Part of the 안정 ABI 버전 3.15 이후로.
See “DuringGC” functions for common information.
Sets *result to a borrowed reference rather than a strong one. The reference is valid for the duration of the
tp_traversehandler call.Added in version 3.15.0a8 (unreleased).
-
PyObject *PyType_GetModule_DuringGC(PyTypeObject *type)¶
-
PyObject *PyType_GetModuleByToken_DuringGC(PyTypeObject *type, const void *mod_token)¶
- 반환값: 빌린 참조. Part of the 안정 ABI 버전 3.15 이후로.
See “DuringGC” functions for common information.
These functions return a borrowed reference, which is valid for the duration of the
tp_traversehandler call.Added in version 3.15.0a8 (unreleased).
가비지 수거기 상태 제어하기¶
The C-API provides the following functions for controlling garbage collection runs.
-
Py_ssize_t PyGC_Collect(void)¶
- Part of the 안정 ABI.
Perform a full garbage collection, if the garbage collector is enabled. (Note that
gc.collect()runs it unconditionally.)Returns the number of collected + unreachable objects which cannot be collected. If the garbage collector is disabled or already collecting, returns
0immediately. Errors during garbage collection are passed tosys.unraisablehook. This function does not raise exceptions.
-
int PyGC_Enable(void)¶
- Part of the 안정 ABI 버전 3.10 이후로.
Enable the garbage collector: similar to
gc.enable(). Returns the previous state, 0 for disabled and 1 for enabled.Added in version 3.10.
-
int PyGC_Disable(void)¶
- Part of the 안정 ABI 버전 3.10 이후로.
Disable the garbage collector: similar to
gc.disable(). Returns the previous state, 0 for disabled and 1 for enabled.Added in version 3.10.
-
int PyGC_IsEnabled(void)¶
- Part of the 안정 ABI 버전 3.10 이후로.
Query the state of the garbage collector: similar to
gc.isenabled(). Returns the current state, 0 for disabled and 1 for enabled.Added in version 3.10.
가비지 수거기 상태 조회하기¶
The C-API provides the following interface for querying information about the garbage collector.
-
void PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg)¶
- 이것은 불안정 API. It may change without warning in minor releases.
Run supplied callback on all live GC-capable objects. arg is passed through to all invocations of callback.
경고
If new objects are (de)allocated by the callback it is undefined if they will be visited.
Garbage collection is disabled during operation. Explicitly running a collection in the callback may lead to undefined behaviour e.g. visiting the same objects multiple times or not at all.
Added in version 3.12.
-
typedef int (*gcvisitobjects_t)(PyObject *object, void *arg)¶
Type of the visitor function to be passed to
PyUnstable_GC_VisitObjects(). arg is the same as the arg passed toPyUnstable_GC_VisitObjects. Return1to continue iteration, return0to stop iteration. Other return values are reserved for now so behavior on returning anything else is undefined.Added in version 3.12.