python parse函数 argument_Python与其他语言结合的参数转换函数PyArg_ParseTuple()

The

arg

argument must be a tuple object containing an argument list passed from Python to a

C function. The

format

argument must be a format string, whose syntax is explained below.

The remaining arguments must be addresses of variables whose type is determined by the

format string. For the conversion to succeed, the

arg

object must match the format and the

format must be exhausted.

Note that while

PyArg_ParseTuple()

checks that the Python arguments have the required

types, it cannot check the validity of the addresses of C variables passed to the call: if you

make mistakes there, your code will probably crash or at least overwrite random bits in

memory. So be careful!

A format string consists of zero or more ``format units''. A format unit describes one Python

object; it is usually a single character or a parenthesized sequence of format units. With a few

exceptions, a format unit that is not a parenthesized sequence normally corresponds to a

single address argument to

PyArg_ParseTuple()

. In the following description, the quoted

form is the format unit; the entry in (round) parentheses is the Python object type that

matches the format unit; and the entry in [square] brackets is the type of the C variable(s)

whose address should be passed. (Use the "

&

"operator to pass a variable's address.)

Note that any Python object references which are provided to the caller are

borrowed

references; do not decrement their reference count!

"

s

" (string or Unicode object) [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

null-terminated. The Python string must not contain embedded null bytes; if it does, a

TypeError

exception is raised. Unicode objects are converted to C strings using the

default encoding. If this conversion fails, an

UnicodeError

is raised.

"

s#

" (string, Unicode or any read buffer compatible object) [char *, int]

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.

"

z

" (string or

None

) [char *]

Like "

s

", but the Python object may also be

None

, in which case the C pointer is set to

NULL

.

"

z#

" (string or

None

or any read buffer compatible object) [char *, int]

This is to "

s#

" as "

z

" is to "

s

".

"

u

" (Unicode object) [Py_UNICODE *]

Convert a Python Unicode object to a C pointer to a null-terminated buffer of 16-bit

Unicode (UTF-16) data. As with "

s

", there is no need to provide storage for the

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值