Are you interested in the actual application of embedding C/C ++ in Python to release resources? Do you want to know how to operate the internal related functions? And the actual introduction of its function prototype? The following is a detailed description of the article.
Python embeds C/C ++ to release resources
Python uses the reference counting mechanism to manage the memory for automatic garbage collection. When using Python objects in C/C ++, the reference count should be correctly processed; otherwise, memory leakage may easily occur. Python/C APIs provide macros such as Py_CLEAR () and Py_DECREF () to operate the reference count.
After using functions in Python/C APIs to create a list, tuples, and dictionaries, reference counts of these objects are generated in the memory. After completing the operation on the object, you should use macros such as Py_CLEAR () and Py_DECREF () to destroy these objects. The prototype is as follows.
Void Py_CLEAR (PyObject * o)
Void Py_DECREF (PyObject * o)
The parameter meanings are as follows.
· O: the object to be operated.
- Python embedded in C/C + has very powerful application functions
- Python unicode ascii code on windows
- Introduction to two common functions in lower-level Python embedding
- A detailed introduction to the operations on tuples embedded in C/C ++ using Python
- Python embedding C/C ++ to create a dictionary
For Py_CLEAR (), the parameter can be a NULL pointer. In this case, Py_CLEAR () does not perform any operation. For Py_DECREF (), the parameter cannot be a NULL pointer. Otherwise, an error occurs.
Python Embedded C/C ++ modules and functions
You can use the PyImport_Import () function in the Python/c api to import the Python module to the C program. The PyImport_Import () function returns a module object. The function prototype is as follows.
PyObject * PyImport_Import (PyObject * name)
The parameter meanings are as follows.
· Name: name of the module to be imported.
Using the PyObject_CallObject () and PyObject_CallFunction () functions in Python/C APIs, you can call functions in Python in C Programs. The parameter prototypes are as follows.
PyObject * PyObject_CallObject (PyObject * callable_object, PyObject * args)
PyObject * PyObject_CallFunction (PyObject * callable, char * format ,...)
For the PyObject_CallObject () function, the parameter meanings are as follows.
· Callable_object: The function object to be called.
· Args: list of parameters in the format of tuples.
The parameter meanings of PyObject_CallFunction () are as follows.
· Callable_object: The function object to be called.
· Format: Specifies the parameter type.
...: The parameter passed to the function.
Use the PyModule_GetDict () function in Python/c api to obtain the function list in the Python module. The PyModule_GetDict () function returns a dictionary. The keyword in the dictionary is the function name and the value is the call address of the function. The function prototype is as follows.
PyObject * PyModule_GetDict (PyObject * module)
The parameter meanings are as follows.
· Module: The imported module object.
The above content introduces how to embed C/C ++ in Python to release resources and modules and functions.