The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python FAQ. How can I execute arbitrary Python statements from C?
By Guest on 7th November 2022 07:54:18 AM | Syntax: PYTHON | Views: 170



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. How can I execute arbitrary Python statements from C?
  2. =====================================================
  3.  
  4. The highest-level function to do this is "PyRun_SimpleString()" which
  5. takes a single string argument to be executed in the context of the
  6. module "__main__" and returns "0" for success and "-1" when an
  7. exception occurred (including "SyntaxError").  If you want more
  8. control, use "PyRun_String()"; see the source for
  9. "PyRun_SimpleString()" in "Python/pythonrun.c".
  10.  
  11.  
  12. How can I evaluate an arbitrary Python expression from C?
  13. =========================================================
  14.  
  15. Call the function "PyRun_String()" from the previous question with the
  16. start symbol "Py_eval_input"; it parses an expression, evaluates it
  17. and returns its value.
  18.  
  19.  
  20. How do I extract C values from a Python object?
  21. ===============================================
  22.  
  23. That depends on the object's type.  If it's a tuple, "PyTuple_Size()"
  24. returns its length and "PyTuple_GetItem()" returns the item at a
  25. specified index.  Lists have similar functions, "PyListSize()" and
  26. "PyList_GetItem()".
  27.  
  28. For bytes, "PyBytes_Size()" returns its length and
  29. "PyBytes_AsStringAndSize()" provides a pointer to its value and its
  30. length.  Note that Python bytes objects may contain null bytes so C's
  31. "strlen()" should not be used.
  32.  
  33. To test the type of an object, first make sure it isn't "NULL", and
  34. then use "PyBytes_Check()", "PyTuple_Check()", "PyList_Check()", etc.
  35.  
  36. There is also a high-level API to Python objects which is provided by
  37. the so-called 'abstract' interface -- read "Include/abstract.h" for
  38. further details.  It allows interfacing with any kind of Python
  39. sequence using calls like "PySequence_Length()",
  40. "PySequence_GetItem()", etc. as well as many other useful protocols
  41. such as numbers ("PyNumber_Index()" et al.) and mappings in the
  42. PyMapping APIs.
  43.  
  44.  
  45. Related video:
  46. Cython Tutorial - Bridging between Python and C/C++ for performance gains
  47. https://www.youtube.com/watch?v=mXuEoqK4bEc
  48.  
  49. Cython: Blend the Best of Python and C++
  50. 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
  • Recent Pastes