"Extended and Embed Python Interpreter" 1.4 Mogables and Initialization Functions

xiaoxiao2021-04-04  275

1.4 module method and initialization function

Below, I demonstrate how to call spam_system () from the Python program. First, we need to list the name and address in 'Method':

Static pymethoddef spammethods [] = {

...

{"SYSTEM", spam_system, method_varargs,

Execute a shell command. "}

...

{Null, NULL, 0, NULL} / * Sentinel * /

}

Pay attention to the third item ("Meth_VARARGS"). This is a logo to tell the interpreter to use the rules that call the C function. It is generally always "Meth_VARARGS" or "Meth_VARARGS | METH_KEYWORDS"; value 0 indicates that the variable used by the Pyarg_Parsetuple () function is discarded.

When "Meth_VARARGS" is used, the function wants the Python-Level parameter to pass through Pyarg_Parsetuple (). The details of this function are provided below.

If the keyword parameter is passed to a function, the Meth_Keywords of the third field should be set to 1. In this case, the C function should accept the third "pyobject *" parameter, this parameter is a keyword dictionary. Use the Pyarg_ParsetupleAndKeywords () function to analyze the parameters. The method table must be passed to the interpreter in the module initialization function. The initialization function must be named initName (), the name is the module name, and the definition of the module file is Non-Static.

Pymodinit_func

INITSPAM (VOID)

{

(void) PY_INITMODULE ("spam", spammethods);

}

Note: pymodinit_func declaration function Return value type is Void, and declares the specific connection instructions required for the platform, for the C declaration function as the extern "C" type.

When the Python program first imports the SPAM module, the initspam () function is called (see below with respect to embed Python). The INITSPAM () function calls py_initmodule (), py_initmodule () function creates a module object (inserted into the Sys.Modules Dictionary "spam" key) and the object created by the table according to the second incoming parameter (PymethodDef structure) On the basis, insert the built-in functional object. The PY_INITMODULE () function returns a pointer to which it is created (not used here). If the module does not initialize correctly, the function is terminated because of a serious error, so the caller does not have to check the error.

When embedded in Python, INITSPAM () is not automatically called unless there is an item in the _pyimport_inittab table. The earliest to deal with this matter is: After calling py_initialize (), call the INITSPAM () of your static initialization of the static connection module.

int

Main (int Argc, char * argv [])

{

/ * Pass Argv [0] to the python interpreter * /

PY_SETPROGRAMNAME (Argv [0]);

/ * Initialize the python interpreter. Required. * /

Py_initialize ();

/ * Add a static module * /

INITSPAM ();

}

In the Python source package, you can find an example: Demo / Embed / Demo.c.

Note: For some extension modules, the entry point is deleted from the Sys.Modules module, or imports the compiled module in a plurality of interpreters in a process (or in fork () without Exec () intervention. problem. When the internal data structure is initialized, the module author should be careful. Also note that the extension module can use the RELOAD () function, and call the initialization function of the module (in this example is the initspam () function), but if the module is powered by a dynamic load file (.so on unix, .dll on Windows) The module is not loaded again. More module examples are included in the Python Source Package, generally in modules / xxmodule.c. These files can be used as an example of template or simple reading. The source code release includes moduulator.py scripts or Windows installations to provide a simple graphical interface interface declaration function and module object to be implemented, and generate a template that can be filled. The script is located in the Tools / ModuLator / Path, please refer to the ReadMe file for details.

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

New Post(0)