Teks sumber — English
Terjemahan — Bahasa Indonesia
Code Objects
Objek Kode
Code objects are a low-level detail of the CPython implementation. Each one represents a chunk of executable code that hasn't yet been bound into a function.
Objek kode merupakan detail tingkat rendah dari implementasi CPython. Masing-masing mewakili sekumpulan kode yang dapat dieksekusi dimana belum terikat ke fungsi.
The C structure of the objects used to describe code objects. The fields of this type are subject to change at any time.
Struktur C dari objek yang digunakan untuk menggambarkan objek kode. Jenis dari tipe ini dapat berubah sewaktu-waktu.
This is an instance of :c:type:`PyTypeObject` representing the Python :ref:`code object <code-objects>`.
Return true if *co* is a :ref:`code object <code-objects>`. This function always succeeds.
Return the number of :term:`free (closure) variables <closure variable>` in a code object.
Return the position of the first :term:`free (closure) variable <closure variable>` in a code object.
Return a new code object. If you need a dummy code object to create a frame, use :c:func:`PyCode_NewEmpty` instead.
Return a new empty code object with the specified filename, function name, and first line number. The resulting code object will raise an ``Exception`` if executed.
Return the line number of the instruction that occurs on or before ``byte_offset`` and ends after it. This may be ``O(N)`` in the number of instructions in the code object, so it is not performant for iterating over all the line numbers in a code object.
Equivalent to the Python code ``getattr(co, 'co_code')``. Returns a strong reference to a :c:type:`PyBytesObject` representing the bytecode in a code object. On error, ``NULL`` is returned and an exception is raised.
This ``PyBytesObject`` may be created on-demand by the interpreter and does not necessarily represent the bytecode actually executed by CPython. The primary use case for this function is debuggers and profilers.
Equivalent to the Python code ``getattr(co, 'co_varnames')``. Returns a new reference to a :c:type:`PyTupleObject` containing the names of the local variables. On error, ``NULL`` is returned and an exception is raised.
Equivalent to the Python code ``getattr(co, 'co_cellvars')``. Returns a new reference to a :c:type:`PyTupleObject` containing the names of the local variables that are referenced by nested functions. On error, ``NULL`` is returned and an exception is raised.
Equivalent to the Python code ``getattr(co, 'co_freevars')``. Returns a new reference to a :c:type:`PyTupleObject` containing the names of the :term:`free (closure) variables <closure variable>`. On error, ``NULL`` is returned and an exception is raised.
Register *callback* as a code object watcher for the current interpreter. Return an ID which may be passed to :c:func:`PyCode_ClearWatcher`. In case of error (e.g. no more watcher IDs available), return ``-1`` and set an exception.
Enumeration of possible code object watcher events: - ``PY_CODE_EVENT_CREATE`` - ``PY_CODE_EVENT_DESTROY``
Type of a code object watcher callback function.
If *event* is ``PY_CODE_EVENT_DESTROY``, taking a reference in the callback to the about-to-be-destroyed code object will resurrect it and prevent it from being freed at this time. When the resurrected object is destroyed later, any watcher callbacks active at that time will be called again.
Users of this API should not rely on internal runtime implementation details. Such details may include, but are not limited to, the exact order and timing of creation and destruction of code objects. While changes in these details may result in differences observable by watchers (including whether a callback is invoked or not), it does not change the semantics of the Python code being executed.
Prior to Python 3.10, this function would perform basic optimizations to a code object.
Code Object Flags
Code objects contain a bit-field of flags, which can be retrieved as the :attr:`~codeobject.co_flags` Python attribute (for example using :c:func:`PyObject_GetAttrString`), and set using a *flags* argument to :c:func:`PyUnstable_Code_New` and similar functions.
To support low-level extensions to frame evaluation, such as external just-in-time compilers, it is possible to attach arbitrary extra data to code objects.
Return a new opaque index value used to adding data to code objects.
You generally call this function once (per interpreter). Use the result with :c:func:`PyUnstable_Code_GetExtra` and :c:func:`PyUnstable_Code_SetExtra` to manipulate data on individual code objects.
If *free* is not ``NULL``: when a code object is deallocated, *free* will be called on non-``NULL`` data stored under the new index. Use :c:func:`Py_DecRef` when storing :c:type:`PyObject`.
object
objek
code object
objek kode