News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Getting a return value from the Python interpreter

Started by jj2007, December 01, 2023, 09:09:15 PM

Previous topic - Next topic

jj2007

include \masm32\MasmBasic\MasmBasic.inc    ; based on Du Brisingr Arget's answer
  Init
  SetDllFolder "\Python"        ; adjust to your setup; downloaded from here
  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: 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? to get an idea.

jack

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

jj2007

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
  Init
  SetDllFolder "\Python"      ; adjust to your setup; downloaded from here
  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


jj2007

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