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
  total Noob question: Why is the code not executed MarkMan 5 594 May-09-2025, 06:15 PM
Last Post: deanhystad
  writing and running code in vscode without saving it akbarza 5 2,840 Mar-03-2025, 08:14 PM
Last Post: Gribouillis
  Problems writing a large text file in python Vilius 4 1,162 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  writing list to csv file problem jacksfrustration 5 2,680 Jul-04-2024, 08:15 PM
Last Post: deanhystad
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 1,544 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 6,020 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  code not working when executed from flask app ThomasDC 1 3,912 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 4,046 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,792 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 4,745 Mar-30-2022, 06:25 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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