Written by Mike James | |||||
Tuesday, 13 August 2024 | |||||
Page 4 of 4
Embedded DebuggingThere is an advantage in using the embedded approach to developing Python extensions. You can debug your extension by simply running a C program in debug mode. There is no need to separately start a Python program and then attach a C debugger. You can simply allow the C program to load and run the Python interpreter. You can even load the Python program as a file and so make use of an IDE to write it. For example: #define PY_SSIZE_T_CLEAN #include <Python.h> static PyObject *Pi(PyObject *self, PyObject *args) { Using this you can run and debug the Pi.py program saved on disk. import Pi import time N=10000000 t1=time.perf_counter() pi=Pi.myPi(1,N) t2=time.perf_counter() print((t2-t1)*1000) print(pi) At the time of writing, this program doesn’t work under Windows due to a difficulty reading in the file using PyRun_File. A solution is to replace PyRun_File by: char line[1000]; fread(line, 1, 1000, fp); int res=PyRun_SimpleString(line); Of course, the buffer has to be large enough to hold the entire file. Implementing An Embedded APIIf you plan to embed Python into an existing program then what you need to do is implement an API which gives the Python code access to the features you want to expose. You can proceed piecemeal and slowly add features, but it is much better to create an object hierarchy which encapsulates the features. Create a module which has objects that correspond to the data in the program and provide methods to access and process the data. A good example of an object hierarchy for embedded languages can be found in the scripting features of almost any spreadsheet. Summary
Extending & Embedding Python Using CBy Mike JamesBuy from Amazon. ContentsPreface
<ASIN:B0CK3X93KF> To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Google Gets To Keep Chrome 03/09/2025 Despite realizing and ruling that Google was and is a monopoly in search and advertising, Judge Amit Mehta has decided that Google can keep control of one of the biggest tools in maintaining that [ ... ] One Thousand Wooden Block Display 31/08/2025 Would that be 1K woodels? This is a crazy project that almost demands that someone tries to implement an alternative. Of course, you could do it. But first read something about the difficulties. More News
Comments
or email your comment to: comments@i-programmer.info << Prev - Next |
|||||
Last Updated ( Tuesday, 13 August 2024 ) |
Extending & Embedding Python Using C - Embedding