- How can I execute arbitrary Python statements from C?
- =====================================================
- The highest-level function to do this is "PyRun_SimpleString()" which
- takes a single string argument to be executed in the context of the
- module "__main__" and returns "0" for success and "-1" when an
- exception occurred (including "SyntaxError"). If you want more
- control, use "PyRun_String()"; see the source for
- "PyRun_SimpleString()" in "Python/pythonrun.c".
- How can I evaluate an arbitrary Python expression from C?
- =========================================================
- Call the function "PyRun_String()" from the previous question with the
- start symbol "Py_eval_input"; it parses an expression, evaluates it
- and returns its value.
- How do I extract C values from a Python object?
- ===============================================
- That depends on the object's type. If it's a tuple, "PyTuple_Size()"
- returns its length and "PyTuple_GetItem()" returns the item at a
- specified index. Lists have similar functions, "PyListSize()" and
- "PyList_GetItem()".
- For bytes, "PyBytes_Size()" returns its length and
- "PyBytes_AsStringAndSize()" provides a pointer to its value and its
- length. Note that Python bytes objects may contain null bytes so C's
- "strlen()" should not be used.
- To test the type of an object, first make sure it isn't "NULL", and
- then use "PyBytes_Check()", "PyTuple_Check()", "PyList_Check()", etc.
- There is also a high-level API to Python objects which is provided by
- the so-called 'abstract' interface -- read "Include/abstract.h" for
- further details. It allows interfacing with any kind of Python
- sequence using calls like "PySequence_Length()",
- "PySequence_GetItem()", etc. as well as many other useful protocols
- such as numbers ("PyNumber_Index()" et al.) and mappings in the
- PyMapping APIs.
- Related video:
- Cython Tutorial - Bridging between Python and C/C++ for performance gains
- https://www.youtube.com/watch?v=mXuEoqK4bEc
- Cython: Blend the Best of Python and C++
- https://www.youtube.com/watch?v=gMvkiQ-gOW8
Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.
Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license