"Extended and embedded Python Interpreter" 1.3 Back to Example

xiaoxiao2021-04-04  265

1.3 back to the example

Go back to our example function, now you should be able to understand the following statement:

IF (! Pyarg_Parsetuple (ARGS, "S", & Command)

Return NULL;

Dependent

The Pyarg_ParsetUple () function is set up, if an error is detected in the parameter list (exception is set), the function returns

NULL (Function Returns the error ID of the object pointer). Otherwise, the string value of the parameters has been copied to the local variable.

Command. This is an allocated pointer, you should not modify the string pointed to by the pointer (like the variables in standard C, variables

Command should be correctly declared "

Const char * command ").

The following statement calls the UNIX function system (), passed to the function we get from Pyarg_Parsetuple ()

STS = System (Command);

Our spam.system () function must return the value of the STS as a Python object. This can be implemented via the py_buildvalue () function, this function is a bit like the reverse function of the Pyarg_Parsetuple () function, which is a formatted string and any C value and returns a Python object. The details of the PY_BUILDVALUE () function are given later.

Return Py_BuildValue ("I", STS);

In this case, it returns an integer object (indeed, even the integer is an object on the python heap); if you have a C function returns a useless parameter (ie the function returns void), the corresponding Python function must return NONE. You should happen to do this (implemented by py_return_none macro):

PY_INCREF (PY_NONE);

Return py_none;

PY_NONE is the C name of the Python empty object. This is a typical Python object rather than just one.

NULL pointer, as we have seen, in most cases, PY_NONE indicates an error.

转载请注明原文地址:https://www.9cbs.com/read-131902.html

New Post(0)