Teks sumber — English
Terjemahan — Bahasa Indonesia
There are three ways strings and buffers can be converted to C:
Ada tiga cara string dan buffer dapat dikonversi ke C:
Convert a Unicode object to a C pointer to a character string. A pointer to an existing string is stored in the character pointer variable whose address you pass. The C string is NUL-terminated. The Python string must not contain embedded null code points; if it does, a :exc:`ValueError` exception is raised. Unicode objects are converted to C strings using ``'utf-8'`` encoding. If this conversion fails, a :exc:`UnicodeError` is raised.
This format does not accept :term:`bytes-like objects <bytes-like object>`. If you want to accept filesystem paths and convert them to C character strings, it is preferable to use the ``O&`` format with :c:func:`PyUnicode_FSConverter` as *converter*.
This format accepts Unicode objects as well as bytes-like objects. It fills a :c:type:`Py_buffer` structure provided by the caller. In this case the resulting C string may contain embedded NUL bytes. Unicode objects are converted to C strings using ``'utf-8'`` encoding.
Like ``s*``, except that it provides a :ref:`borrowed buffer <c-arg-borrowed-buffer>`. The result is stored into two C variables, the first one a pointer to a C string, the second one its length. The string may contain embedded null bytes. Unicode objects are converted to C strings using ``'utf-8'`` encoding.
Seperti ``s*``, kecuali bahwa ia menyediakan buffer :ref:`pinjaman <c-arg-borrowed-buffer>`. Hasilnya disimpan ke dalam dua variabel C, yang pertama adalah penunjuk ke sebuah string C, yang kedua adalah panjangnya. String tersebut dapat berisi byte null yang disematkan. Objek Unicode dikonversi ke string C menggunakan pengkodean ``'utf-8'``.
This format converts a bytes-like object to a C pointer to a :ref:`borrowed <c-arg-borrowed-buffer>` character string; it does not accept Unicode objects. The bytes buffer must not contain embedded null bytes; if it does, a :exc:`ValueError` exception is raised.
Format ini mengkonversi objek bytes-like ke C-pointer ke string karakter :ref:` yang dipinjam dari <c-arg-borrowed-buffer>`; format ini tidak menerima objek Unicode. Buffer byte tidak boleh berisi byte null yang disematkan; jika ya, pengecualian :exc:`ValueError` akan dimunculkan.
These formats allow representing Python numbers or single characters as C numbers. Formats that require :class:`int`, :class:`float` or :class:`complex` can also use the corresponding special methods :meth:`~object.__index__`, :meth:`~object.__float__` or :meth:`~object.__complex__` to convert the Python object to the required type.
Format-format ini memungkinkan untuk merepresentasikan angka Python atau karakter tunggal sebagai angka C. Format yang memerlukan :class:`int`, :class:`float` atau :class:`complex` juga dapat menggunakan metode khusus :meth:`~object.__index__`, :meth:`~object.__float__` atau :meth:`~object.__complex__` untuk mengonversi objek Python ke jenis yang diperlukan.
Convert a nonnegative Python integer to an unsigned tiny integer, stored in a C :c:expr:`unsigned char`.
Mengonversi bilangan bulat Python nonnegatif menjadi bilangan bulat kecil unsigned, disimpan dalam C: c: expr: `unsigned char`.
Convert a Python integer to a tiny integer without overflow checking, stored in a C :c:expr:`unsigned char`. Convert a Python integer to a C :c:expr:`unsigned char`.
Convert a Python integer to a C :c:expr:`short int`.
Mengonversi bilangan bulat Python menjadi C: c: expr: `short int`.
Convert a Python integer to a C :c:expr:`unsigned short int`.
Convert a Python integer to a plain C :c:expr:`int`.
Mengonversi bilangan bulat Python ke bahasa C biasa :c:expr:`int`.
Convert a Python integer to a C :c:expr:`unsigned int`.
Convert a Python integer to a C :c:expr:`long int`.
Mengonversi bilangan bulat Python menjadi C: c: expr: `long int`.
Convert a Python integer to a C :c:expr:`unsigned long`.
Convert a Python integer to a C :c:expr:`long long`.
Mengonversi bilangan bulat Python menjadi C :c:expr:`long long`.
Convert a Python integer to a C :c:expr:`unsigned long long`.
Convert a Python integer to a C :c:type:`Py_ssize_t`.
Convert a Python byte, represented as a :class:`bytes` or :class:`bytearray` object of length 1, to a C :c:expr:`char`.
Mengonversi byte Python, direpresentasikan sebagai objek :class:`bytes` atau :class:`bytearray` dengan panjang 1, ke C :c:expr:`char`.
Convert a Python character, represented as a :class:`str` object of length 1, to a C :c:expr:`int`.
Mengonversi karakter Python, direpresentasikan sebagai objek :class:`str` dengan panjang 1, menjadi C :c:expr:`int`.
Convert a Python floating-point number to a C :c:expr:`float`.
Mengonversi angka floating-point Python ke C :c:expr:`float`.
Convert a Python floating-point number to a C :c:expr:`double`.
Mengonversi angka floating-point Python ke C :c:expr:`double`.
Convert a Python complex number to a C :c:type:`Py_complex` structure.
``O&`` (object) [*converter*, *address*]
``O&`` (objek) [*converter*, *adress*]
Convert a Python object to a C variable through a *converter* function. This takes two arguments: the first is a function, the second is the address of a C variable (of arbitrary type), converted to :c:expr:`void *`. The *converter* function in turn is called as follows::
Mengonversi objek Python ke variabel C melalui fungsi *converter*. Ini membutuhkan dua argumen: yang pertama adalah fungsi, yang kedua adalah alamat variabel C (dari tipe sembarang), yang dikonversi menjadi :c:expr:`void *`. Fungsi *converter* pada gilirannya dipanggil sebagai berikut::
status = converter(object, address);
status = converter(objek, adress);
where *object* is the Python object to be converted and *address* is the :c:expr:`void*` argument that was passed to the ``PyArg_Parse*`` function. The returned *status* should be ``1`` for a successful conversion and ``0`` if the conversion has failed. When the conversion fails, the *converter* function should raise an exception and leave the content of *address* unmodified.
di mana *object* adalah objek Python yang akan dikonversi dan *address* adalah argumen :c:expr:`void*` yang diteruskan ke fungsi ``PyArg_Parse*``. *status* yang dikembalikan harus ``1`` untuk konversi yang berhasil dan ``0`` jika konversi gagal. Ketika konversi gagal, fungsi *converter* akan memunculkan pengecualian dan membiarkan konten *address* tidak diubah.
If the *converter* returns :c:macro:`!Py_CLEANUP_SUPPORTED`, it may get called a second time if the argument parsing eventually fails, giving the converter a chance to release any memory that it had already allocated. In this second call, the *object* parameter will be ``NULL``; *address* will have the same value as in the original call.
Jika *converter* mengembalikan :c:macro:`!Py_CLEANUP_SUPPORTED`, ia mungkin akan dipanggil untuk kedua kalinya jika penguraian argumen pada akhirnya gagal, memberikan kesempatan kepada konverter untuk melepaskan memori yang telah dialokasikan. Pada pemanggilan kedua ini, parameter *object* akan menjadi ``NULL``; *address* akan memiliki nilai yang sama dengan pemanggilan pertama.
Examples of converters: :c:func:`PyUnicode_FSConverter` and :c:func:`PyUnicode_FSDecoder`.
Contoh konverter: :c:func:`PyUnicode_FSConverter` dan :c:func:`PyUnicode_FSDecoder`.
Tests the value passed in for truth (a boolean **p**\ redicate) and converts the result to its equivalent C true/false integer value. Sets the int to ``1`` if the expression was true and ``0`` if it was false. This accepts any valid Python value. See :ref:`truth` for more information about how Python tests values for truth.
If *items* contains format units which store a :ref:`borrowed buffer <c-arg-borrowed-buffer>` (``s``, ``s#``, ``z``, ``z#``, ``y``, or ``y#``) or a :term:`borrowed reference` (``S``, ``Y``, ``U``, ``O``, or ``O!``), the object must be a Python tuple. The *converter* for the ``O&`` format unit in *items* must not store a borrowed buffer or a borrowed reference.
Jika *items* berisi unit format yang menyimpan :ref:`borrowed buffer <c-arg-borrowed-buffer>` (``s``, ``s#``, ``z``, ``z#``, ``y``, atau ``y#``) atau :term:`borrowed reference` (``S``, ``Y``, ``U``, ``O``, atau ``O!``), maka objek tersebut haruslah sebuah tuple Python. *converter* untuk unit format ``O&`` dalam *items* tidak boleh menyimpan buffer yang dipinjam atau referensi yang dipinjam.
Convert a null-terminated C string to a Python :class:`str` object using ``'utf-8'`` encoding. If the C string pointer is ``NULL``, ``None`` is used.
Convert a C string and its length to a Python :class:`str` object using ``'utf-8'`` encoding. If the C string pointer is ``NULL``, the length is ignored and ``None`` is returned.
This converts a C string to a Python :class:`bytes` object. If the C string pointer is ``NULL``, ``None`` is returned.
This converts a C string and its lengths to a Python object. If the C string pointer is ``NULL``, ``None`` is returned.
Convert a null-terminated :c:type:`wchar_t` buffer of Unicode (UTF-16 or UCS-4) data to a Python Unicode object. If the Unicode buffer pointer is ``NULL``, ``None`` is returned.
Mengonversi buffer :c:type:`wchar_t` yang diakhiri dengan nol dari data Unicode (UTF-16 atau UCS-4) menjadi objek Python Unicode. Jika penunjuk buffer Unicode adalah ``NULL``, ``None`` akan dikembalikan.
Convert a Unicode (UTF-16 or UCS-4) data buffer and its length to a Python Unicode object. If the Unicode buffer pointer is ``NULL``, the length is ignored and ``None`` is returned.
Convert a plain C :c:expr:`int` to a Python integer object.
Mengonversi C biasa :c:expr:`int` ke objek bilangan bulat Python.
Convert a plain C :c:expr:`char` to a Python integer object.
Mengonversi C biasa :c:expr:`char` ke objek bilangan bulat Python.
Convert a plain C :c:expr:`short int` to a Python integer object.
Mengkonversi C biasa: c:expr:`short int` menjadi objek bilangan bulat Python.
Convert a C :c:expr:`long int` to a Python integer object.
Mengonversi C :c:expr:`long int` menjadi objek bilangan bulat Python.
Convert a C :c:expr:`unsigned char` to a Python integer object.
Mengonversi C: c: expr: `unsigned char` menjadi objek bilangan bulat Python.
Convert a C :c:expr:`unsigned short int` to a Python integer object.
Mengkonversi C :c:expr:`unsigned short int` menjadi objek integer Python.
Convert a C :c:expr:`unsigned int` to a Python integer object.
Mengonversi C :c:expr:`unsigned int` menjadi objek bilangan bulat Python.
Convert a C :c:expr:`unsigned long` to a Python integer object.
Mengonversi C :c:expr:`unsigned long` menjadi objek integer Python.
Convert a C :c:expr:`long long` to a Python integer object.
Mengonversi C :c:expr:`long long` menjadi objek bilangan bulat Python.
Convert a C :c:expr:`unsigned long long` to a Python integer object.
Mengkonversi C :c:expr:`unsigned long long` menjadi objek integer Python.
Convert a C :c:type:`Py_ssize_t` to a Python integer.
Convert a C :c:expr:`int` to a Python :class:`bool` object.
Mengonversi C :c:expr:`int` ke objek Python :class:`bool`.
Be aware that this format requires an ``int`` argument. Unlike most other contexts in C, variadic arguments are not coerced to a suitable type automatically. You can convert another type (for example, a pointer or a float) to a suitable ``int`` value using ``(x) ? 1 : 0`` or ``!!x``.
Perlu diketahui bahwa format ini membutuhkan argumen ``int``. Tidak seperti kebanyakan konteks lain dalam C, argumen variadic tidak dipaksa ke tipe yang sesuai secara otomatis. Anda dapat mengkonversi tipe lain (misalnya, pointer atau float) ke nilai ``int`` yang sesuai dengan menggunakan ``(x) ? 1 : 0`` atau ``!!x``.
Convert a C :c:expr:`int` representing a byte to a Python :class:`bytes` object of length 1.
Mengonversi C :c:expr:`int` yang mewakili byte menjadi objek Python :class:`bytes` dengan panjang 1.
Convert a C :c:expr:`int` representing a character to Python :class:`str` object of length 1.
Mengonversi C :c:expr:`int` yang mewakili karakter ke objek Python :class:`str` dengan panjang 1.
Convert a C :c:expr:`double` to a Python floating-point number.
Mengonversi C :c:expr:`double` ke angka floating-point Python.
Convert a C :c:expr:`float` to a Python floating-point number.
Mengonversi C :c:expr:`float` ke angka floating-point Python.
Convert a C :c:type:`Py_complex` structure to a Python complex number.
``O&`` (object) [*converter*, *anything*]
``O&`` (object) [*converter*, *anything*]
Convert *anything* to a Python object through a *converter* function. The function is called with *anything* (which should be compatible with :c:expr:`void*`) as its argument and should return a "new" Python object, or ``NULL`` if an error occurred.
Mengonversi *anything* menjadi objek Python melalui fungsi *converter*. Fungsi ini dipanggil dengan *anything* (yang seharusnya kompatibel dengan :c:expr:`void*`) sebagai argumennya dan akan mengembalikan objek Python "new", atau ``NULL`` jika terjadi kesalahan.
Convert a sequence of C values to a Python tuple with the same number of items.
Convert a sequence of C values to a Python list with the same number of items.
Convert a sequence of C values to a Python dictionary. Each pair of consecutive C values adds one item to the dictionary, serving as key and value, respectively.