Teks sumber — English
Terjemahan — Bahasa Indonesia
These formats allow accessing an object as a contiguous chunk of memory. You don't have to provide raw storage for the returned unicode or bytes area.
Formaty te umożliwiają dostęp do obiektu jako ciągłego fragmentu pamięci. Nie musisz zapewniać nieprzetworzonej pamięci dla zwróconego obszaru unicode lub bajtów.
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.
For the conversion to succeed, the *arg* object must match the format and the format must be exhausted. On success, the ``PyArg_Parse*`` functions return true, otherwise they return false and raise an appropriate exception. When the ``PyArg_Parse*`` functions fail due to conversion failure in one of the format units, the variables at the addresses corresponding to that and the following format units are left untouched.
Agar konversi berhasil, objek *arg* harus sesuai dengan format dan formatnya harus habis. Jika berhasil, fungsi ``PyArg_Parse*`` akan mengembalikan nilai true, jika tidak maka akan mengembalikan nilai false dan memunculkan pengecualian yang sesuai. Ketika fungsi ``PyArg_Parse*`` gagal karena kegagalan konversi pada salah satu unit format, variabel pada alamat yang sesuai dengan itu dan unit format berikutnya tidak disentuh.
Parse the parameters of a function that takes only positional parameters into local variables. Returns true on success; on failure, it returns false and raises the appropriate exception.
Parse the parameters of a function that takes both positional and keyword parameters into local variables. The *keywords* argument is a ``NULL``-terminated array of keyword parameter names specified as null-terminated ASCII or UTF-8 encoded C strings. Empty names denote :ref:`positional-only parameters <positional-only_parameter>`. Returns true on success; on failure, it returns false and raises the appropriate exception.
Mengurai parameter fungsi yang mengambil parameter posisi dan kata kunci ke dalam variabel lokal. Argumen * kata kunci * adalah larik ``NULL``-terminated dari nama parameter kata kunci yang ditentukan sebagai string C yang dikodekan dengan ASCII atau UTF-8 yang diakhiri dengan nol. Nama-nama kosong menunjukkan :ref:` parameter khusus posisi <positional-only_parameter>`. Mengembalikan nilai true jika berhasil; jika gagal, mengembalikan nilai false dan memunculkan pengecualian yang sesuai.
Parse the parameter of a function that takes a single positional parameter into a local variable. Returns true on success; on failure, it returns false and raises the appropriate exception.
Mengurai parameter fungsi yang mengambil parameter posisi tunggal menjadi variabel lokal. Mengembalikan nilai true jika berhasil; jika gagal, mengembalikan nilai false dan memunculkan pengecualian yang sesuai.
// 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;
}
Create a new value based on a format string similar to those accepted by the ``PyArg_Parse*`` family of functions and a sequence of values. Returns the value or ``NULL`` in the case of an error; an exception will be raised if ``NULL`` is returned.
Membuat nilai baru berdasarkan string format yang mirip dengan yang diterima oleh keluarga fungsi ``PyArg_Parse*`` dan urutan nilai. Mengembalikan nilai atau ``NULL`` jika terjadi kesalahan; pengecualian akan dimunculkan jika ``NULL`` dikembalikan.
:c:func:`Py_BuildValue` does not always build a tuple. It builds a tuple only if its format string contains two or more format units. If the format string is empty, it returns ``None``; if it contains exactly one format unit, it returns whatever object is described by that format unit. To force it to return a tuple of size 0 or one, parenthesize the format string.
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.
In the following description, the quoted form is the format unit; the entry in (round) parentheses is the Python object type that the format unit will return; and the entry in [square] brackets is the type of the C value(s) to be passed.
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.
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.
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.
If there is an error in the format string, the :exc:`SystemError` exception is set and ``NULL`` returned.