Pelokalan
Python ID
id en
Masuk
Proyek
c-api__arg.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
These functions are useful when creating your own extension functions and methods. Additional information and examples are available in :ref:`extending-index`.
Fungsi-fungsi ini berguna saat membuat fungsi dan metode ekstensi Anda sendiri. Informasi tambahan dan contoh tersedia di :ref:`extending-index`.
The first three of these functions described, :c:func:`PyArg_ParseTuple`, :c:func:`PyArg_ParseTupleAndKeywords`, and :c:func:`PyArg_Parse`, all use *format strings* which are used to tell the function about the expected arguments. The format strings use the same syntax for each of these functions.
Tiga fungsi pertama dijelaskan yaitu, :c:func:`PyArg_ParseTuple`, :c:func:`PyArg_ParseTupleAndKeywords`, dan :c:func:`PyArg_Parse`, semuanya menggunakan *format string* yang digunakan untuk memberitahu fungsi tentang expected argumen . Format string menggunakan sintaks yang sama untuk setiap fungsi tersebut.
On Python 3.12 and older, the macro :c:macro:`!PY_SSIZE_T_CLEAN` must be defined before including :file:`Python.h` to use all ``#`` variants of formats (``s#``, ``y#``, etc.) explained below. This is not necessary on Python 3.13 and later.
Pada Python 3.12 dan yang lebih lama, makro :c:macro:`!PY_SSIZE_T_CLEAN` harus didefinisikan sebelum :file:`Python.h` untuk menggunakan semua varian ``#`` dari format (``s#``, ``y#``, dll.) yang dijelaskan di bawah ini. Ini tidak diperlukan pada Python 3.13 dan yang lebih baru.
Formats such as ``y*`` and ``s*`` fill a :c:type:`Py_buffer` structure. This locks the underlying buffer so that the caller can subsequently use the buffer even inside a :c:type:`Py_BEGIN_ALLOW_THREADS` block without the risk of mutable data being resized or destroyed. As a result, **you have to call** :c:func:`PyBuffer_Release` after you have finished processing the data (or in any early abort case).
Format seperti ``y*`` dan ``s*`` mengisi struktur :c:type:`Py_buffer`. Hal ini mengunci buffer yang mendasarinya sehingga pemanggil dapat menggunakan buffer tersebut bahkan di dalam blok :c:type:`Py_BEGIN_ALLOW_THREADS` tanpa risiko data diubah ukurannya atau dihancurkan. Akibatnya, **anda harus memanggil** :c:func:`PyBuffer_Release` setelah anda selesai memproses data (atau dalam kasus pembatalan awal).
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 variant on ``s`` is used for encoding Unicode into a character buffer. It only works for encoded data without embedded NUL bytes.
Ten wariant ``s`` jest używany do zakodowania Unicode w buforze znaków. To działa tylko dla zakodowanych danych bez osadzonych znaków NUL.
This format requires two arguments. The first is only used as input, and must be a :c:expr:`const char*` which points to the name of an encoding as a NUL-terminated string, or ``NULL``, in which case ``'utf-8'`` encoding is used. An exception is raised if the named encoding is not known to Python. The second argument must be a :c:expr:`char**`; the value of the pointer it references will be set to a buffer with the contents of the argument text. The text will be encoded in the encoding specified by the first argument.
Format ini membutuhkan dua argumen. Yang pertama hanya digunakan sebagai masukan, dan harus berupa :c:expr:`const char*` yang menunjuk pada nama pengkodean sebagai string yang diakhiri dengan NUL, atau ``NULL``, dalam hal ini pengkodean ``'utf-8'`` yang digunakan. Pengecualian akan muncul jika pengkodean yang disebutkan tidak dikenal oleh Python. Argumen kedua harus berupa :c:expr:`char**`; nilai pointer yang direferensikan akan diset ke buffer dengan isi teks argumen. Teks akan dikodekan dalam pengkodean yang ditentukan oleh argumen pertama.
:c:func:`PyArg_ParseTuple` will allocate a buffer of the needed size, copy the encoded data into this buffer and adjust *\*buffer* to reference the newly allocated storage. The caller is responsible for calling :c:func:`PyMem_Free` to free the allocated buffer after use.
Same as ``es`` except that byte string objects are passed through without recoding them. Instead, the implementation assumes that the byte string object uses the encoding passed in as parameter.
Podobnie jak ``es`` z wyjątkiem tego, że obiekty ciągów znaków są przekazywane dalej bez ich zapisywania. Zamiast tego implementacja zakłada, że obiekt łańcucha znaków wykorzystuje kodowanie przekazane jako parametr.
This variant on ``s#`` is used for encoding Unicode into a character buffer. Unlike the ``es`` format, this variant allows input data which contains NUL characters.
Ten wariant ``s#`` używany jest do kodowania Unicode w buforze znaków. W przeciwieństwie do formatu ``es``, ten wariant pozwala wprowadzać dane zawierające znaki NUL.
It requires three arguments. The first is only used as input, and must be a :c:expr:`const char*` which points to the name of an encoding as a NUL-terminated string, or ``NULL``, in which case ``'utf-8'`` encoding is used. An exception is raised if the named encoding is not known to Python. The second argument must be a :c:expr:`char**`; the value of the pointer it references will be set to a buffer with the contents of the argument text. The text will be encoded in the encoding specified by the first argument. The third argument must be a pointer to an integer; the referenced integer will be set to the number of bytes in the output buffer.
Ini membutuhkan tiga argumen. Yang pertama hanya digunakan sebagai masukan, dan harus berupa :c:expr:`const char*` yang menunjuk pada nama pengkodean sebagai string yang diakhiri dengan NUL, atau ``NULL``, dalam hal ini pengkodean ``'utf-8'`` yang digunakan. Pengecualian akan muncul jika pengkodean yang disebutkan tidak dikenal oleh Python. Argumen kedua harus berupa :c:expr:`char**`; nilai pointer yang direferensikan akan disetel ke buffer dengan isi teks argumen. Teks akan dikodekan dalam pengkodean yang ditentukan oleh argumen pertama. Argumen ketiga harus berupa penunjuk ke sebuah bilangan bulat; bilangan bulat yang direferensikan akan diset ke jumlah byte dalam buffer keluaran.
If *\*buffer* points to a non-``NULL`` pointer (an already allocated buffer), :c:func:`PyArg_ParseTuple` will use this location as the buffer and interpret the initial value of *\*buffer_length* as the buffer size. It will then copy the encoded data into the buffer and NUL-terminate it. If the buffer is not large enough, a :exc:`ValueError` will be set.
Same as ``es#`` except that byte string objects are passed through without recoding them. Instead, the implementation assumes that the byte string object uses the encoding passed in as parameter.
Tak samo, jak w ``es#`` oprócz tego, że obiekty ciągu bajtów są przekazywane do funkcji bez ich zapisywania. Zamiast tego, implementacja zakłada że obiekt ciągu bajtów używa kodowania przekazywanego w parametrze.
``u``, ``u#``, ``Z``, and ``Z#`` are removed because they used a legacy ``Py_UNICODE*`` representation.
``u``, ``u#``,``Z``, dan ``Z#`` dihapus karena menggunakan representasi ``Py_UNICODE*``.
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.
Use :meth:`~object.__index__` if available.
Gunakan :meth:`~object.__index__` jika tersedia.
The list of format units ends here; the string after the colon is used as the function name in error messages (the "associated value" of the exception that :c:func:`PyArg_ParseTuple` raises).
The list of format units ends here; the string after the semicolon is used as the error message *instead* of the default error message. ``:`` and ``;`` mutually exclude each other.
Na tym kończy się lista jednostek formatu; ciąg po średniku jest traktowany jako informacja o błędzie do użycia *zamiast* automatycznej wiadomości o błędzie. Znaki dwukropka ``:`` i średnika ``;`` wzajemnie się wykluczają.
Additional arguments passed to these functions must be addresses of variables whose type is determined by the format string; these are used to store values from the input tuple. There are a few cases, as described in the list of format units above, where these parameters are used as input values; they should match what is specified for the corresponding format unit in that case.
Dodatkowe parametry przekazywane do tych funkcji muszą być adresami zmiennych których typ jest określany przez ciąg formatu; są one używane do przechowywania wartości z krotki wejściowej. Jest parę przypadków, jak opisuje to lista jednostek formatu powyżej, gdzie te parametry są używane jako wprowadzane wartości; w takich przypadku powinny one odpowiadać temu, co jest określone we właściwych im jednostach formatu.
Ensure that the keys in the keywords argument dictionary are strings. This is only needed if :c:func:`PyArg_ParseTupleAndKeywords` is not used, since the latter already does this check.
// Function using METH_O calling convention static PyObject* my_function(PyObject *module, PyObject *arg) { int value; if (!PyArg_Parse(arg, "i:my_function", &value)) { return NULL; } // ... use value ... }
// Fungsi yang menggunakan konvensi pemanggilan METH_O static PyObject * PyObject my_function(PyObject * modul, PyObject * arg) { int value; if (!PyArg_Parse(arg, "i:my_function", &value)) { return NULL; } // ... gunakan nilai ... }
A simpler form of parameter retrieval which does not use a format string to specify the types of the arguments. Functions which use this method to retrieve their parameters should be declared as :c:macro:`METH_VARARGS` in function or method tables. The tuple containing the actual parameters should be passed as *args*; it must actually be a tuple. The length of the tuple must be at least *min* and no more than *max*; *min* and *max* may be equal. Additional arguments must be passed to the function, each of which should be a pointer to a :c:expr:`PyObject*` variable; these will be filled in with the values from *args*; they will contain :term:`borrowed references <borrowed reference>`. The variables which correspond to optional parameters not given by *args* will not be filled in; these should be initialized by the caller. This function returns true on success and false if *args* is not a tuple or contains the wrong number of elements; an exception will be set if there was a failure.
Bentuk pengambilan parameter yang lebih sederhana yang tidak menggunakan string format untuk menentukan jenis argumen. Fungsi yang menggunakan metode ini untuk mengambil parameter harus dideklarasikan sebagai :c:macro:`METH_VARARGS` dalam tabel fungsi atau metode. Tuple yang berisi parameter aktual harus diberikan sebagai * args*; harus benar-benar sebuah tuple. Panjang tuple harus setidaknya *min* dan tidak lebih dari *max*; nilsi *min* dan *max* boleh sama. Argumen tambahan harus dilewatkan ke fungsi, yang masing-masing harus berupa penunjuk ke variabel :c:expr:`PyObject*`; ini akan diisi dengan nilai dari *args*; mereka akan berisi :term: `referensi yang dipinjam <borrowed reference>`. Variabel-variabel yang berhubungan dengan parameter opsional yang tidak diberikan oleh *args* tidak akan diisi; variabel-variabel ini harus diinisialisasi oleh pemanggil. Fungsi ini mengembalikan nilai true jika berhasil dan false jika *args* bukan sebuah tuple atau berisi jumlah elemen yang salah; sebuah pengecualian akan ditetapkan jika terjadi kegagalan.
This is an example of the use of this function, taken from the sources for the :mod:`!_weakref` helper module for weak references::
Ini adalah contoh penggunaan fungsi berikut, yang diambil dari sumber untuk modul pembantu :mod:`!_weakref` untuk referensi yang lemah ::
The characters space, tab, colon and comma are ignored in format strings (but not within format units such as ``s#``). This can be used to make long format strings a tad more readable.
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.
Pass a Python object untouched but create a new :term:`strong reference` to it (i.e. its reference count is incremented by one). If the object passed in is a ``NULL`` pointer, it is assumed that this was caused because the call producing the argument found an error and set an exception. Therefore, :c:func:`Py_BuildValue` will return ``NULL`` but won't raise an exception. If no exception has been raised yet, :exc:`SystemError` is set.
Mengoper objek Python tanpa disentuh tetapi membuat :term:`referensi kuat` baru ke objek tersebut (misalnya, jumlah referensinya bertambah satu). Jika objek yang dioper adalah sebuah penunjuk ``NULL``, diasumsikan bahwa hal ini disebabkan karena pemanggilan yang menghasilkan argumen tersebut menemukan kesalahan dan membuat pengecualian. Oleh karena itu, :c:func:`Py_BuildValue` akan mengembalikan ``NULL`` tetapi tidak akan memunculkan pengecualian. Jika tidak ada pengecualian yang dibangkitkan, :exc:`SystemError` akan di-set.
Same as ``O``, except it doesn't create a new :term:`strong reference`. Useful when the object is created by a call to an object constructor in the argument list.
Sama seperti ``O``, kecuali tidak membuat :term:`strong reference` yang baru. Berguna ketika objek dibuat dengan pemanggilan ke konstruktor objek dalam daftar argumen.