The MASM Forum

Projects => MasmBasic & the RichMasm IDE => Topic started by: jj2007 on December 01, 2023, 09:09:15 PM

Title: Getting a return value from the Python interpreter
Post by: jj2007 on December 01, 2023, 09:09:15 PM
include \masm32\MasmBasic\MasmBasic.inc    ; based on Du Brisingr Arget's answer (https://stackoverflow.com/questions/24732753/reading-a-global-variable-of-python-in-c)
  Init
  SetDllFolder "\Python"        ; adjust to your setup; downloaded from here (https://www.python.org/ftp/python/3.8.9/python-3.8.9-embed-win32.zip)
  Dll "python38.dll"        ; python38.dll offers twice as many functions
  Declare void Py_Initialize
  Declare void PyRun_SimpleString, C:1
  Declare PyImport_AddModule, C:1
  Declare PyObject_GetAttrString, C:2
  Declare PyLong_AsLong, C:1
  SetDllFolder                ; no args=go back to previous folder after declaring the functions
  Py_Initialize()
  PyRun_SimpleString("MyLong=123456789")    ; use the interpreter
  Print Str$("MyLong=%i", PyLong_AsLong(PyObject_GetAttrString(PyImport_AddModule("__main__"), "MyLong")))
EndOfCode

Output: MyLong=123456789

Note this is not the same as the approach shown in Assembly uses Python (https://masm32.com/board/index.php?topic=11506.0): there, Python3?.dll is used directly to get access to Python's powerful and fast library functions. The snippet above uses the slow interpreter ("Python shell") instead. Check e.g. What is the difference between a Python shell and a Python interpreter? (https://www.quora.com/What-is-the-difference-between-a-Python-shell-and-a-Python-interpreter) to get an idea.
Title: Re: Getting a return value from the Python interpreter
Post by: jack on December 02, 2023, 12:04:31 AM
Hi jj
the lack of information regarding the retrieving of values from a python script to use in other languages has always turned me off, what good is it to run a python script using the python dll if you can't use the results in you favorite programming language? you might as well just use python.
I would like to see more examples of this, for example how to get a double value
Title: Re: Getting a return value from the Python interpreter
Post by: jj2007 on December 02, 2023, 12:49:11 AM
Quote from: jack on December 02, 2023, 12:04:31 AMhow to get a double value

include \masm32\MasmBasic\MasmBasic.inc   ; based on Du Brisingr Arget's answer (https://stackoverflow.com/questions/24732753/reading-a-global-variable-of-python-in-c)
  Init
  SetDllFolder "\Python"      ; adjust to your setup; downloaded from here (https://www.python.org/ftp/python/3.8.9/python-3.8.9-embed-win32.zip)
  Dll "python38.dll"      ; python38.dll offers twice as many functions
  Declare void Py_Initialize
  Declare void PyRun_SimpleString, C:1
  Declare PyImport_AddModule, C:1
  Declare PyObject_GetAttrString, C:2
  Declare PyLong_AsLong, C:1
  Declare REAL8 PyFloat_AsDouble, C:1
  SetDllFolder            ; no args=go back to previous folder after declaring the functions
  Py_Initialize()
  PyRun_SimpleString("MyDouble=12345.6789")   ; use the interpreter
  Print Str$("MyDouble=%9f", PyFloat_AsDouble(PyObject_GetAttrString(PyImport_AddModule("__main__"), "MyDouble")))

EndOfCode
Title: Re: Getting a return value from the Python interpreter
Post by: jack on December 02, 2023, 02:18:39 AM
thank you  :smiley:
Title: Re: Getting a return value from the Python interpreter
Post by: jj2007 on December 02, 2023, 03:23:52 AM
Here are functions that run strings or collection of strings, i.e. *.py files. Python38.dll has over 1,600 functions, but my feeling is that three quarters are either redundant or dedicated to typecasting of some sorts. It's an ugly mess, and I am really not impressed :cool:

PyRun_AnyFile
PyRun_AnyFileEx
PyRun_AnyFileExFlags
PyRun_AnyFileFlags
PyRun_File
PyRun_FileEx
PyRun_FileExFlags
PyRun_FileFlags
PyRun_SimpleFile
PyRun_SimpleFileEx
PyRun_SimpleFileExFlags
PyRun_SimpleString
PyRun_SimpleStringFlags
PyRun_String
PyRun_StringFlags