Detailed instructions on how to embed Python into C/C ++ to release resources

Source: Internet
Author: User

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.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.