Python Forum
C API: Writing Executed Code to a File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C API: Writing Executed Code to a File
#1
I am working on a game that teaches programming through Python. To do this, I am embedding the Python interpreter in a C/C++ game. I am struggling capturing the conversion of the executed code properly however. While my initial thoughts to use C/C++ file streams, that did not work. The problem I'm running into is that I'm not quite sure how to call the API to get the correct data to write to the file. Below is my code.

PyObject  *ioMod, *openedFile;
PyGILState_STATE gilState = PyGILState_Ensure();
//storeOut.open("store.txt");

PyObject* objectsRepresentation = PyObject_Str(Object);
PyObject* encoded = PyUnicode_AsEncodedString(objectsRepresentation, "utf-8", "strict");

ioMod = PyImport_ImportModule("io");
openedFile = PyObject_CallMethod(ioMod, "open", "ss", "store.txt", "wb");
Py_DECREF(ioMod);

PyObject_CallMethod(openedFile, "write", "y", PyObject_Str(encoded));
PyObject_CallMethod(openedFile, "flush", NULL);
PyObject_CallMethod(openedFile, "close", NULL);
Py_DECREF(openedFile);
As it stands, when I type any basic arithmetic function like 2 + 2, it writes the correct result to the "store.txt" file. When a function like 'print' is called however, what gets written to the file is 'None'. I'm unsure where the result is going. I'm also running some error checking in C++ to see what is going on. Running the code object prints the correct value to the debug console everytime. The code is capturing and returning the proper value to the console. But in the file and game it prints 'None' when a Print function is called.

Am I calling the wrong function, or not capturing the data properly?

Thanks for the help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  writing and running code in vscode without saving it akbarza 1 346 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 639 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,177 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  code not working when executed from flask app ThomasDC 1 838 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,309 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,411 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Writing to file ends incorrectly project_science 4 2,642 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,654 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,337 Nov-13-2020, 10:10 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020