Pelokalan
Python ID
id en
Masuk
Proyek
c-api__module.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
Module Objects
This instance of :c:type:`PyTypeObject` represents the Python module type. This is exposed to Python programs as :py:class:`types.ModuleType`.
Return true if *p* is a module object, or a subtype of a module object. This function always succeeds.
Return true if *p* is a module object, but not a subtype of :c:data:`PyModule_Type`. This function always succeeds.
Return a new module object with :attr:`module.__name__` set to *name*. The module's :attr:`!__name__`, :attr:`~module.__doc__`, :attr:`~module.__package__` and :attr:`~module.__loader__` attributes are filled in (all but :attr:`!__name__` are set to ``None``). The caller is responsible for setting a :attr:`~module.__file__` attribute.
Similar to :c:func:`PyModule_NewObject`, but the name is a UTF-8 encoded string instead of a Unicode object.
Return the dictionary object that implements *module*'s namespace; this object is the same as the :attr:`~object.__dict__` attribute of the module object. If *module* is not a module object (or a subtype of a module object), :exc:`SystemError` is raised and ``NULL`` is returned.
It is recommended extensions use other ``PyModule_*`` and ``PyObject_*`` functions rather than directly manipulate a module's :attr:`~object.__dict__`.
Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to ``'utf-8'``.
Return the name of the file from which *module* was loaded using *module*'s :attr:`~module.__file__` attribute. If this is not defined, or if it is not a string, raise :exc:`SystemError` and return ``NULL``; otherwise return a reference to a Unicode object.
Similar to :c:func:`PyModule_GetFilenameObject` but return the filename encoded to 'utf-8'.
:c:func:`PyModule_GetFilename` raises :exc:`UnicodeEncodeError` on unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
The slots array is usually used to define an extension module's “main” module object (see :ref:`extension-modules` for details). It can also be used to :ref:`create extension modules dynamically <module-from-slots>`.
:c:member:`Slot ID <PySlot.sl_id>` for a function that creates the module object itself. The function must have the signature:
*spec*: a ``ModuleSpec``-like object, meaning that any attributes defined for :py:class:`importlib.machinery.ModuleSpec` have matching semantics. However, any of the attributes may be missing.
The function should return a new module object, or set an error and return ``NULL``.
If ``Py_mod_create`` is not specified, the import machinery will create a normal module object using :c:func:`PyModule_New`. The name is taken from *spec*, not the definition, to allow extension modules to dynamically adjust to their place in the module hierarchy and be imported under different names through symlinks, all while sharing a single module definition.
There is no requirement for the returned object to be an instance of :c:type:`PyModule_Type`. However, some slots may only be used with :c:type:`!PyModule_Type` instances; in particular:
The *spec* argument may be a ``ModuleSpec``-like object, rather than a true :py:class:`~importlib.machinery.ModuleSpec` instance. Note that previous versions of CPython did not enforce this.
The table must be statically allocated (or otherwise guaranteed to outlive the module object).
Extension modules can have *module state* -- a piece of memory that is allocated on module creation, and freed when the module object is deallocated. The module state is specified using :ref:`dedicated slots <ext-module-state-slots>`.
A typical use of module state is storing an exception type -- or indeed *any* type object defined by the module --
Keeping per-module information in attributes and module state, rather than in static globals, makes module objects *isolated* and safer for use in multiple sub-interpreters. It also helps Python do an orderly clean-up when it shuts down.
Extensions that keep references to Python objects as part of module state must implement :c:macro:`Py_mod_state_traverse` and :c:macro:`Py_mod_state_clear` functions to avoid reference leaks.
:c:member:`Slot ID <PySlot.sl_id>` for a traversal function to call during GC traversal of the module object.
The signature of the function, and meanings of the arguments, is similar as for :c:member:`PyTypeObject.tp_traverse`:
:c:member:`Slot ID <PySlot.sl_id>` for a clear function to call during GC clearing of the module object.
Like :c:member:`PyTypeObject.tp_clear`, this function is not *always* called before a module is deallocated. For example, when reference counting is enough to determine that an object is no longer used, the cyclic garbage collector is not involved and the :c:macro:`Py_mod_state_free` function is called directly.
:c:member:`Slot ID <PySlot.sl_id>` for a function to call during deallocation of the module object.
Each module may have an associated *token*: a pointer-sized value intended to identify of the module state's memory layout. This means that if you have a module object, but you are not sure if it “belongs” to your extension, you can check using code like this:
PyObject *module = <the module in question>; void *module_token; if (PyModule_GetToken(module, &module_token) < 0) { return NULL; } if (module_token != your_token) { PyErr_SetString(PyExc_ValueError, "unexpected module"); return NULL; } // This module's state has the expected memory layout; it's safe to cast struct my_state *state = (struct my_state*)PyModule_GetState(module);
Create a new module object, given an array of :ref:`slots <pymoduledef_slot>` and the :py:class:`~importlib.machinery.ModuleSpec` *spec*.
The *spec* argument may be any ``ModuleSpec``-like object, as described in :c:macro:`Py_mod_create` documentation. Currently, the *spec* must have a ``name`` attribute.
The module definition struct, which holds information needed to create a module object.
The type of ``PyModuleDef`` objects.
Create a new module object, given the definition in *def*. This is a macro that calls :c:func:`PyModule_Create2` with *module_api_version* set to :c:macro:`PYTHON_API_VERSION`, or to :c:macro:`PYTHON_ABI_VERSION` if using the :ref:`limited API <limited-c-api>`.
Create a new module object, given the definition in *def*, assuming the API version *module_api_version*. If that version does not match the version of the running interpreter, a :exc:`RuntimeWarning` is emitted.
Create a new module object, given the definition in *def* and the ModuleSpec *spec*, assuming the API version *module_api_version*. If that version does not match the version of the running interpreter, a :exc:`RuntimeWarning` is emitted.
The following functions are provided to help initialize a module object. They are intended for a module's execution slot (:c:data:`Py_mod_exec`), the initialization function for legacy :ref:`single-phase initialization <single-phase-initialization>`, or code that creates modules dynamically.
Add an object to *module* as *name*. This is a convenience function which can be used from the module's initialization function.
static int add_spam(PyObject *module, int value) { PyObject *obj = PyLong_FromLong(value); if (obj == NULL) { return -1; } int res = PyModule_AddObjectRef(module, "spam", obj); Py_DECREF(obj); return res; }
static int add_spam(PyObject *module, int value) { PyObject *obj = PyLong_FromLong(value); int res = PyModule_AddObjectRef(module, "spam", obj); Py_XDECREF(obj); return res; }
The number of different *name* strings passed to this function should be kept small, usually by only using statically allocated strings as *name*. For names that aren't known at compile time, prefer calling :c:func:`PyUnicode_FromString` and :c:func:`PyObject_SetAttr` directly. For more details, see :c:func:`PyUnicode_InternFromString`, which may be used internally to create a key object.
Similar to :c:func:`PyModule_AddObjectRef`, but ":term:`steals <steal>`" a reference to *value* (even on error). It can be called with a result of function that returns a new reference without bothering to check its result or even saving it to a variable.
Similar to :c:func:`PyModule_AddObjectRef`, but :term:`steals <steal>` a reference to *value* on success (if it returns ``0``).
The new :c:func:`PyModule_Add` or :c:func:`PyModule_AddObjectRef` functions are recommended, since it is easy to introduce reference leaks by misusing the :c:func:`PyModule_AddObject` function.
Unlike other functions that steal references, ``PyModule_AddObject()`` only releases the reference to *value* **on success**.
PyObject *obj = PyBytes_FromString(value); if (PyModule_AddObject(module, "spam", obj) < 0) { // If 'obj' is not NULL and PyModule_AddObject() failed, // 'obj' strong reference must be deleted with Py_XDECREF(). // If 'obj' is NULL, Py_XDECREF() does nothing. Py_XDECREF(obj); goto error; } // PyModule_AddObject() stole a reference to obj: // Py_XDECREF(obj) is not needed here.
This is a convenience function that calls :c:func:`PyLong_FromLong` and :c:func:`PyModule_AddObjectRef`; see their documentation for details.
This is a convenience function that calls :c:func:`PyUnicode_InternFromString` and :c:func:`PyModule_AddObjectRef`; see their documentation for details.
Add a type object to *module*. The type object is finalized by calling internally :c:func:`PyType_Ready`. The name of the type object is taken from the last component of :c:member:`~PyTypeObject.tp_name` after dot. Return ``-1`` with an exception set on error, ``0`` on success.
The *functions* array must be statically allocated (or otherwise guaranteed to outlive the module object).
The legacy :ref:`single-phase initialization <single-phase-initialization>` initialization scheme creates singleton modules that can be looked up in the context of the current interpreter. This allows the module object to be retrieved later with only a reference to the module definition.
Returns the module object that was created from *def* for the current interpreter. This method requires that the module object has been attached to the interpreter state with :c:func:`PyState_AddModule` beforehand. In case the corresponding module object is not found or has not been attached to the interpreter state yet, it returns ``NULL``.
Attaches the module object passed to the function to the interpreter state. This allows the module object to be accessible via :c:func:`PyState_FindModule`.
Removes the module object created from *def* from the interpreter state. Return ``-1`` with an exception set on error, ``0`` on success.
object
objek