This document is for an old version of Python that is no longer supported. You should upgrade and read the Python 当前稳定版本的文档.

# 解析参数并构建值变量

在创建你自己的扩展函数和方法时，这些函数是有用的。其它的信息和样例见 扩展和嵌入 Python 解释器 。

这些函数描述的前三个，`PyArg_ParseTuple()`，`PyArg_ParseTupleAndKeywords()`，以及 `PyArg_Parse()`，它们都使用 格式化字符串 来将函数期待的参数告知函数。这些函数都使用相同语法规则的格式化字符串。

一个格式化字符串包含0或者更多的格式单元。一个格式单元用来描述一个Python对象；它通常是一个字符或者由括号括起来的格式单元序列。除了少数例外，一个非括号序列的格式单元通常对应这些函数的具有单一地址的参数。在接下来的描述中，双引号内的表达式是格式单元；圆括号()内的是对应这个格式单元的Python对象类型；方括号[]内的是传递的C变量(变量集)类型。

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. Also, you won’t have to release any memory yourself, except with the `es`, `es#`, `et` and `et#` formats.

`s` (string or Unicode) [const char *]

  Convert a Python string or Unicode object to a C pointer to a character string. You must not provide storage for the string itself; a pointer to an existing string is stored into the character pointer variable whose address you pass. The C string is NUL-terminated. The Python string must not contain embedded NUL bytes; if it does, a `TypeError` exception is raised. Unicode objects are converted to C strings using the default encoding. If this conversion fails, a `UnicodeError` is raised.

`s#` (string, Unicode or any read buffer compatible object) [const char *, int (or `Py_ssize_t`, see below)]

  This variant on `s` stores into two C variables, the first one a pointer to a character string, the second one its length. In this case the Python string may contain embedded null bytes. Unicode objects pass back a pointer to the default encoded string version of the object if such a conversion is possible. All other read-buffer compatible objects pass back a reference to the raw internal data representation.

  Starting with Python 2.5 the type of the length argument can be controlled by defining the macro `PY_SSIZE_T_CLEAN` before including `Python.h`. If the macro is defined, length is a `Py_ssize_t` rather than an int.

`s*` (string, Unicode, or any buffer compatible object) [Py_buffer]

  Similar to `s#`, this code fills a Py_buffer structure provided by the caller. The buffer gets locked, so that the caller can subsequently use the buffer even inside a `Py_BEGIN_ALLOW_THREADS` block; the caller is responsible for calling `PyBuffer_Release` with the structure after it has processed the data.

  2.6 新版功能.

`z` (string, Unicode or `None`) [const char *]

  Like `s`, but the Python object may also be `None`, in which case the C pointer is set to NULL.

`z#` (string, Unicode, `None` or any read buffer compatible object) [const char *, int]

  This is to `s#` as `z` is to `s`.

`z*` (string, Unicode, `None` or any buffer compatible object) [Py_buffer]

  This is to `s*` as `z` is to `s`.

  2.6 新版功能.

`u` (Unicode) [Py_UNICODE *]

  Convert a Python Unicode object to a C pointer to a NUL-terminated buffer of 16-bit Unicode (UTF-16) data. As with `s`, there is no need to provide storage for the Unicode data buffer; a pointer to the existing Unicode data is stored into the `Py_UNICODE` pointer variable whose address you pass.

`u#` (Unicode) [Py_UNICODE *, int]

  This variant on `u` stores into two C variables, the first one a pointer to a Unicode data buffer, the second one its length. Non-Unicode objects are handled by interpreting their read-buffer pointer as pointer to a `Py_UNICODE` array.

`es` (string, Unicode or character buffer compatible object) [const char *encoding, char **buffer]

  This variant on `s` is used for encoding Unicode and objects convertible to Unicode into a character buffer. It only works for encoded data without embedded NUL bytes.

  This format requires two arguments. The first is only used as input, and must be a `const char*` which points to the name of an encoding as a NUL-terminated string, or NULL, in which case the default encoding is used. An exception is raised if the named encoding is not known to Python. The second argument must be a `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.

  `PyArg_ParseTuple()` 会分配一个足够大小的缓冲区，将编码后的数据拷贝进这个缓冲区并且设置 *buffer 引用这个新分配的内存空间。调用者有责任在使用后调用 `PyMem_Free()` 去释放已经分配的缓冲区。

`et` (string, Unicode or character buffer compatible object) [const char *encoding, char **buffer]

  Same as `es` except that 8-bit string objects are passed through without recoding them. Instead, the implementation assumes that the string object uses the encoding passed in as parameter.

`es#` (string, Unicode or character buffer compatible object) [const char *encoding, char **buffer, int *buffer_length]

  This variant on `s#` is used for encoding Unicode and objects convertible to Unicode into a character buffer. Unlike the `es` format, this variant allows input data which contains NUL characters.

  It requires three arguments. The first is only used as input, and must be a `const char*` which points to the name of an encoding as a NUL-terminated string, or NULL, in which case the default encoding is used. An exception is raised if the named encoding is not known to Python. The second argument must be a `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.

  有两种操作方式：

  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 `PyMem_Free()` to free the allocated buffer after usage.

  If *buffer points to a non-NULL pointer (an already allocated buffer), `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 `TypeError` will be set. Note: starting from Python 3.6 a `ValueError` will be set.

  在这两个例子中，*buffer_length 被设置为编码后结尾不为NUL的数据的长度。

`et#` (string, Unicode or character buffer compatible object) [const char *encoding, char **buffer, int *buffer_length]

  Same as `es#` except that string objects are passed through without recoding them. Instead, the implementation assumes that the string object uses the encoding passed in as parameter.

`b` (integer) [unsigned char]

  将一个非负的Python整型转化成一个无符号的微整型，存储在一个C `unsigned char` 类型中。

`B` (integer) [unsigned char]

  将一个Python整型转化成一个微整型并不检查溢出问题，存储在一个C `unsigned char` 类型中。

  2.3 新版功能.

`h` (integer) [short int]

  将一个Python整型转化成一个C `short int` 短整型。

`H` (integer) [unsigned short int]

  将一个Python整型转化成一个C `unsigned short int` 无符号短整型，并不检查溢出问题。

  2.3 新版功能.

`i` (integer) [int]

  将一个Python整型转化成一个C `int` 整型。

`I` (integer) [unsigned int]

  将一个Python整型转化成一个C `unsigned int` 无符号整型，并不检查溢出问题。

  2.3 新版功能.

`l` (integer) [long int]

  将一个Python整型转化成一个C `long int` 长整型。

`k` (integer) [unsigned long]

  Convert a Python integer or long integer to a C `unsigned long` without overflow checking.

  2.3 新版功能.

`L` (integer) [PY_LONG_LONG]

  Convert a Python integer to a C `long long`. This format is only available on platforms that support `long long` (or `_int64` on Windows).

`K` (integer) [unsigned PY_LONG_LONG]

  Convert a Python integer or long integer to a C `unsigned long long` without overflow checking. This format is only available on platforms that support `unsigned long long` (or `unsigned _int64` on Windows).

  2.3 新版功能.

`n` (integer) [Py_ssize_t]

  Convert a Python integer or long integer to a C `Py_ssize_t`.

  2.5 新版功能.

`c` (string of length 1) [char]

  Convert a Python character, represented as a string of length 1, to a C `char`.

`f` (float) [float]

  将一个Python浮点数转化成一个C `float` 浮点数。

`d` (float) [double]

  将一个Python浮点数转化成一个C `double` 双精度浮点数。

`D` (complex) [Py_complex]

  将一个Python复数类型转化成一个C `Py_complex` Python复数类型。

`O` (object) [PyObject *]

  Store a Python object (without any conversion) in a C object pointer. The C program thus receives the actual object that was passed. The object’s reference count is not increased. The pointer stored is not NULL.

`O!` (object) [typeobject, PyObject *]

  将一个Python对象存入一个C指针。和 `O` 类似，但是需要两个C参数：第一个是Python类型对象的地址，第二个是存储对象指针的C变量( `PyObject*` 变量)的地址。如果Python对象类型不对，会抛出 `TypeError` 异常。

`O&` (object) [converter, anything]

  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 `void *`. The converter function in turn is called as follows:

      status = converter(object, address);

  where object is the Python object to be converted and address is the `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.

`S` (string) [PyStringObject *]

  Like `O` but requires that the Python object is a string object. Raises `TypeError` if the object is not a string object. The C variable may also be declared as `PyObject*`.

`U` (Unicode string) [PyUnicodeObject *]

  Like `O` but requires that the Python object is a Unicode object. Raises `TypeError` if the object is not a Unicode object. The C variable may also be declared as `PyObject*`.

`t#` (read-only character buffer) [char *, int]

  Like `s#`, but accepts any object which implements the read-only buffer interface. The `char*` variable is set to point to the first byte of the buffer, and the `int` is set to the length of the buffer. Only single-segment buffer objects are accepted; `TypeError` is raised for all others.

`w` (read-write character buffer) [char *]

  Similar to `s`, but accepts any object which implements the read-write buffer interface. The caller must determine the length of the buffer by other means, or use `w#` instead. Only single-segment buffer objects are accepted; `TypeError` is raised for all others.

`w#` (read-write character buffer) [char *, Py_ssize_t]

  Like `s#`, but accepts any object which implements the read-write buffer interface. The `char *` variable is set to point to the first byte of the buffer, and the `Py_ssize_t` is set to the length of the buffer. Only single-segment buffer objects are accepted; `TypeError` is raised for all others.

`w*` (read-write byte-oriented buffer) [Py_buffer]

  This is to `w` what `s*` is to `s`.

  2.6 新版功能.

`(items)` (tuple) [matching-items]

  对象必须是Python序列，它的长度是 items 中格式单元的数量。C参数必须对应 items 中每一个独立的格式单元。序列中的格式单元可能有嵌套。

  注解

  Prior to Python version 1.5.2, this format specifier only accepted a tuple containing the individual parameters, not an arbitrary sequence. Code which previously caused `TypeError` to be raised here may now proceed without an exception. This is not expected to be a problem for existing code.

It is possible to pass Python long integers where integers are requested; however no proper range checking is done — the most significant bits are silently truncated when the receiving field is too small to receive the value (actually, the semantics are inherited from downcasts in C — your mileage may vary).

格式化字符串中还有一些其他的字符具有特殊的涵义。这些可能并不嵌套在圆括号中。它们是：

`|`

  Indicates that the remaining arguments in the Python argument list are optional. The C variables corresponding to optional arguments should be initialized to their default value — when an optional argument is not specified, `PyArg_ParseTuple()` does not touch the contents of the corresponding C variable(s).

`:`

  格式单元的列表结束标志；冒号后的字符串被用来作为错误消息中的函数名(`PyArg_ParseTuple()` 函数引发的“关联值”异常)。

`;`

  格式单元的列表结束标志；分号后的字符串被用来作为错误消息取代默认的错误消息。 `:` 和 `;` 相互排斥。

注意任何由调用者提供的Python对象引用是 借来的 引用；不要递减它们的引用计数！

传递给这些函数的附加参数必须是由格式化字符串确定的变量的地址；这些都是用来存储输入元组的值。有一些情况，如上面的格式单元列表中所描述的，这些参数作为输入值使用；在这种情况下，它们应该匹配指定的相应的格式单元。

为了转换成功，arg 对象必须匹配格式并且格式必须用尽。成功的话，`PyArg_Parse*()` 函数返回true，反之它们返回false并且引发一个合适的异常。当 `PyArg_Parse*()` 函数因为某一个格式单元转化失败而失败时，对应的以及后续的格式单元地址内的变量都不会被使用。

int `PyArg_ParseTuple`(PyObject *args, const char *format, ...)

  解析一个函数的参数，表达式中的参数按参数位置顺序存入局部变量中。成功返回true；失败返回false并且引发相应的异常。

int `PyArg_VaParse`(PyObject *args, const char *format, va_list vargs)

  和 `PyArg_ParseTuple()` 相同，然而它接受一个va_list类型的参数而不是可变数量的参数集。

int `PyArg_ParseTupleAndKeywords`(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...)

  Parse the parameters of a function that takes both positional and keyword parameters into local variables. Returns true on success; on failure, it returns false and raises the appropriate exception.

int `PyArg_VaParseTupleAndKeywords`(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs)

  和 `PyArg_ParseTupleAndKeywords()` 相同，然而它接受一个va_list类型的参数而不是可变数量的参数集。

int `PyArg_Parse`(PyObject *args, const char *format, ...)

  Function used to deconstruct the argument lists of “old-style” functions — these are functions which use the `METH_OLDARGS` parameter parsing method. This is not recommended for use in parameter parsing in new code, and most code in the standard interpreter has been modified to no longer use this for that purpose. It does remain a convenient way to decompose other tuples, however, and may continue to be used for that purpose.

int `PyArg_UnpackTuple`(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)

  一个不使用格式化字符串指定参数类型的简单形式的参数检索。使用这种方法来检索参数的函数应该在函数或者方法表中声明 `METH_VARARGS`。包含实际参数的元组应该以 args 形式被传入；它必须是一个实际的元组。元组的长度必须至少是 min 并且不超过 max； min 和 max 可能相同。额外的参数必须传递给函数，每一个参数必须是一个指向 `PyObject*` 类型变量的指针；它们将被赋值为 args 的值；它们将包含借来的引用。不在 args 里面的可选参数不会被赋值；由调用者完成初始化。函数成功则返回true并且如果 args 不是元组或者包含错误数量的元素则返回false；如果失败了会引发一个异常。

  这是一个使用此函数的示例，取自 `_weakref` 帮助模块用来弱化引用的源代码：

      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;
      }

  这个例子中调用 `PyArg_UnpackTuple()` 完全等价于调用 `PyArg_ParseTuple()`:

      PyArg_ParseTuple(args, "O|O:ref", &object, &callback)

  2.2 新版功能.

  在 2.5 版更改: This function used an `int` type for min and max. This might require changes in your code for properly supporting 64-bit systems.

PyObject* `Py_BuildValue`(const char *format, ...)
  Return value: New reference.

  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.

  `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 `Py_BuildValue()`. In other words, if your code invokes `malloc()` and passes the allocated memory to `Py_BuildValue()`, your code is responsible for calling `free()` for that memory once `Py_BuildValue()` returns.

  在下面的描述中，双引号的表达式使格式单元；圆括号()内的是格式单元将要返回的Python对象类型；方括号[]内的是传递的C变量(变量集)的类型

  字符例如空格，制表符，冒号和逗号在格式化字符串中会被忽略(但是不包括格式单元，如 `s#`)。这可以使很长的格式化字符串具有更好的可读性。

  `s` (string) [char *]

    Convert a null-terminated C string to a Python object. If the C string pointer is NULL, `None` is used.

  `s#` (string) [char *, int]

    Convert a C string and its length to a Python object. If the C string pointer is NULL, the length is ignored and `None` is returned.

  `z` (string or `None`) [char *]

    和``s``一样。

  `z#` (string or `None`) [char *, int]

    和``s#``一样。

  `u` (Unicode string) [Py_UNICODE *]

    Convert a null-terminated buffer of Unicode (UCS-2 or UCS-4) data to a Python Unicode object. If the Unicode buffer pointer is NULL, `None` is returned.

  `u#` (Unicode string) [Py_UNICODE *, int]

    Convert a Unicode (UCS-2 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.

  `i` (integer) [int]

    将一个C `int` 整型转化成Python整型对象。

  `b` (integer) [char]

    将一个C `char` 字符型转化成Python整型对象。

  `h` (integer) [short int]

    将一个C `short int` 短整型转化成Python整型对象。

  `l` (integer) [long int]

    将一个C `long int` 长整型转化成Python整型对象。

  `B` (integer) [unsigned char]

    将一个C `unsigned char` 无符号字符型转化成Python整型对象。

  `H` (integer) [unsigned short int]

    将一个C `unsigned long` 无符号短整型转化成Python整型对象。

  `I` (integer/long) [unsigned int]

    Convert a C `unsigned int` to a Python integer object or a Python long integer object, if it is larger than `sys.maxint`.

  `k` (integer/long) [unsigned long]

    Convert a C `unsigned long` to a Python integer object or a Python long integer object, if it is larger than `sys.maxint`.

  `L` (long) [PY_LONG_LONG]

    Convert a C `long long` to a Python long integer object. Only available on platforms that support `long long`.

  `K` (long) [unsigned PY_LONG_LONG]

    Convert a C `unsigned long long` to a Python long integer object. Only available on platforms that support `unsigned long long`.

  `n` (int) [Py_ssize_t]

    Convert a C `Py_ssize_t` to a Python integer or long integer.

    2.5 新版功能.

  `c` (string of length 1) [char]

    Convert a C `int` representing a character to a Python string of length 1.

  `d` (float) [double]

    将一个C `double` 双精度浮点数转化为Python浮点数类型数字。

  `f` (float) [float]

    Same as `d`.

  `D` (complex) [Py_complex *]

    将一个C `Py_complex` 类型的结构转化为Python复数类型。

  `O` (object) [PyObject *]

    Pass a Python object untouched (except for its reference count, which 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, `Py_BuildValue()` will return NULL but won’t raise an exception. If no exception has been raised yet, `SystemError` is set.

  `S` (object) [PyObject *]

    和``O``相同。

  `N` (object) [PyObject *]

    Same as `O`, except it doesn’t increment the reference count on the object. Useful when the object is created by a call to an object constructor in the argument list.

  `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 `void *`) as its argument and should return a “new” Python object, or NULL if an error occurred.

  `(items)` (tuple) [matching-items]

    将一个C变量序列转换成Python元组并保持相同的元素数量

  `[items]` (list) [matching-items]

    将一个C变量序列转换成Python列表并保持相同的元素数量

  `{items}` (dictionary) [matching-items]

    将一个C变量序列转换成Python字典。每一对连续的C变量对作为一个元素插入字典中，分别作为关键字和值。

  If there is an error in the format string, the `SystemError` exception is set and NULL returned.

PyObject* `Py_VaBuildValue`(const char *format, va_list vargs)

  和 `Py_BuildValue()` 相同，然而它接受一个va_list类型的参数而不是可变数量的参数集。