How to embed Python into C/C ++ modules and functions

Source: Internet
Author: User

In the practical application of computer languages, do you have any questions or are interested in the actual operation steps for embedding Python in C/C ++ to release resources? If you have problems or are interested in the operation, you can click the following article.

Embed Python into 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.

  • Python is defined by Graminit. c.
  • Python embeds C ++ to make up for the shortcomings of C ++.
  • Introduction to the powerful functions generated when Python is embedded into C/C ++
  • Python embedded in C/C ++ at a lower level)
  • Embed Python into the C/C ++ tuples for specific applications

Void Py_CLEAR (PyObject * o)

Void Py_DECREF (PyObject * o)

The parameter meanings are as follows.

O: the object to be operated.

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.

Embed Python into 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 is an introduction to how to embed Python into C/C ++ to release resources, and how to embed Python into the C/C ++ module and function-related content.

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.