Teks sumber — English
Terjemahan — Bahasa Indonesia
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).
The ``es``, ``es#``, ``et`` and ``et#`` formats allocate the result buffer. **You have to call** :c:func:`PyMem_Free` after you have finished processing the data (or in any early abort case).
Format ``es``, ``es#``, ``et`` dan ``et#`` mengalokasikan buffer hasil. **anda harus menghubungi** :c:func:`PyMem_Free` setelah anda selesai memproses data (atau dalam kasus pembatalan awal).
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.
This format accepts any object which implements the read-write buffer interface. It fills a :c:type:`Py_buffer` structure provided by the caller. The buffer may contain embedded null bytes. The caller has to call :c:func:`PyBuffer_Release` when it is done with the buffer.
: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.
If *\*buffer* points a ``NULL`` pointer, the function will allocate a buffer of the needed size, copy the encoded data into this buffer and set *\*buffer* to reference the newly allocated storage. The caller is responsible for calling :c:func:`PyMem_Free` to free the allocated buffer after usage.
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::
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.
Note that any Python object references which are provided to the caller are *borrowed* references; do not release them (i.e. do not decrement their reference count)!
Perhatikan bahwa setiap referensi objek Python yang diberikan kepada pemanggil adalah referensi *borrowed*; jangan lepaskan mereka (yaitu jangan mengurangi jumlah referensi mereka)!
// 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 ...
}
Parse the parameters of a function that takes only array parameters into local variables (that is, a function using the :c:macro:`METH_FASTCALL` calling convention). Returns true on success; on failure, it returns false and raises the appropriate exception.
Parse the parameters of a function that takes both array and keyword parameters into local variables (that is, a function using the :c:macro:`METH_FASTCALL` ``|`` :c:macro:`METH_KEYWORDS` calling convention). Returns true on success; on failure, it returns false and raises the appropriate exception.
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.
static PyObject *
weakref_ref(PyObject *self, PyObject *args)
{
PyObject *object;
PyObject *callback = NULL;
PyObject *result = NULL;
if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
result = PyWeakref_NewRef(object, callback);
}
return result;
}
static PyObject * PyObject
weakref_ref(PyObject * self, PyObject * args)
{
PyObject * object;
PyObject * callback = NULL;
PyObject * result = NULL;
if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
result = PyWeakref_NewRef(object, callback);
}
return result;
}
The call to :c:func:`PyArg_UnpackTuple` in this example is entirely equivalent to this call to :c:func:`PyArg_ParseTuple`::
PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
When memory buffers are passed as parameters to supply data to build objects, as for the ``s`` and ``s#`` formats, the required data is copied. Buffers provided by the caller are never referenced by the objects created by :c:func:`Py_BuildValue`. In other words, if your code invokes :c:func:`malloc` and passes the allocated memory to :c:func:`Py_BuildValue`, your code is responsible for calling :c:func:`free` for that memory once :c:func:`Py_BuildValue` returns.
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``.
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.
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.