Pelokalan
Python ID
id en
Masuk
Proyek
library__ctypes.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
For example, on Linux::
You can also use a callable Python object (a function or a class for example) as the :attr:`~_CFuncPtr.restype` attribute, if the foreign function returns an integer. The callable will be called with the *integer* the C function returns, and the result of this call will be used as the result of your function call. This is useful to check for error return values and automatically raise an exception::
Usually, ctypes does strict type checking. This means, if you have ``POINTER(c_int)`` in the :attr:`~_CFuncPtr.argtypes` list of a function or as the type of a member field in a structure definition, only instances of exactly the same type are accepted. There are some exceptions to this rule, where ctypes accepts other objects. For example, you can pass compatible array instances instead of pointer types. So, for ``POINTER(c_int)``, ctypes accepts an array of c_int::
On Windows, the ``.DLL`` suffix may be missing. (For details, see `LoadLibraryExW`_ documentation.) Other platform-specific prefixes and suffixes (for example, ``lib``, ``.so``, ``.dylib``, or version numbers) must be present in *name*; they are not added automatically. See :ref:`ctypes-finding-shared-libraries` for more information.
The *use_errno* parameter, when set to true, enables a ctypes mechanism that allows accessing the system :data:`errno` error number in a safe way. :mod:`!ctypes` maintains a thread-local copy of the system's :data:`errno` variable; if you call foreign functions created with ``use_errno=True`` then the :data:`errno` value before the function call is swapped with the ctypes private copy, the same happens immediately after the function call. This mechanism only works for libraries that use the same C runtime library as the interpreter itself. A library linked against a different C runtime (for example, a different CRT on Windows) has its own :data:`errno`, which :mod:`!ctypes` cannot observe.
If you need to free memory, for example, allocated by an extension module with a call to the ``free(void *)``, it is important that you use the function in the same library that allocated the memory.
The exact output of this function will be system dependent. On most platforms, the first entry of this list represents the current process itself, which may be an empty string. For example, on glibc-based Linux, the return may look like::
When a foreign function is called, each actual argument is passed to the :meth:`~_CData.from_param` class method of the items in the :attr:`argtypes` tuple, this method allows adapting the actual argument to an object that the foreign function accepts. For example, a :class:`c_char_p` item in the :attr:`argtypes` tuple will adapt a bytes object passed as an argument to a C pointer to its contents. It does not accept a :class:`str` object.
For example, this callable raises an exception when the foreign function signals failure with a negative return value, and otherwise leaves the result to :mod:`!ctypes`::
On Windows, when a foreign function call raises a system exception (for example, due to an access violation), it will be captured and replaced with a suitable Python exception. Further, an auditing event ``ctypes.set_exception`` with argument ``code`` will be raised, allowing an audit hook to replace the exception with its own.
Foreign functions can also be created by instantiating function prototypes. Function prototypes are similar to function prototypes in C; they describe a function (return type, argument types, calling convention) without defining an implementation. The factory functions must be called with the desired result type and the argument types of the function, and can be used as decorator factories, and as such, be applied to functions through the ``@wrapper`` syntax. See :ref:`ctypes-callback-functions` for examples.
For example::
Sebagai contoh::
Types marked \* in the summary may be (or always are) aliases of a different :class:`_SimpleCData` subclass, and will not necessarily use the listed type code. For example, if the platform's :c:expr:`long`, :c:expr:`long long` and :c:expr:`time_t` C types are the same, then :class:`c_long`, :class:`c_longlong` and :class:`c_time_t` all refer to a single class, :class:`c_long`, whose :attr:`_type_` code is ``'l'``. The ``'q'`` code will be unused.
Fundamental data types, when returned as foreign function call results, or, for example, by retrieving structure field members or array items, are transparently converted to native Python types. In other words, if a foreign function has a :attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will receive a Python bytes object or ``None`` for a ``NULL`` pointer, *not* a :class:`c_char_p` instance.
Additionally, the :attr:`!_fields_` class variable must be defined before the structure or union type is first used, for example when an instance or subclass is created. Later assignments to :attr:`!_fields_` will raise an :exc:`AttributeError`. If :attr:`!_fields_` has not been set before such use, the structure or union will have no own fields, as if :attr:`!_fields_` was empty.
If not set explicitly, ``ctypes`` will use a default that matches the platform conventions. This default may change in future Python releases (for example, when a new platform gains official support, or when a difference between similar platforms is found). Currently the default will be:
Descriptor for fields of a :class:`Structure` and :class:`Union`. For example::