since it was needed:
Save it as Python35.pbi
Code: Select all
;
; Python 3.5 for PureBASIC
;
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
Global Python_Libname$ = "python35.dll"
Macro OSPrototype
PrototypeC
EndMacro
CompilerCase #PB_OS_Linux
Global Python_Libname$ = "python.so"
Macro OSPrototype
PrototypeC
EndMacro
CompilerCase #PB_OS_MacOS
Global Python_Libname$ = "python.so"
Macro OSPrototype
PrototypeC
EndMacro
CompilerEndSelect
Global PythonLib.i
Structure _typeobject Align #PB_Structure_AlignC
*ob_base
*tp_name ; For printing, in format "<module>.<name>"
tp_basicsize.l
tp_itemsize.l ; For allocation
; Methods To implement standard operations
*tp_dealloc
*tp_print
*tp_getattr
*tp_setattr
*tp_reserved ; formerly known as tp_compare
*tp_repr
; Method suites For standard classes
*tp_as_number
*tp_as_sequence
*tp_as_mapping
; More standard operations (here For binary compatibility)
*tp_hash
*tp_call
*tp_str
*tp_getattro
*tp_setattro
; Functions To access object As input/output buffer
*tp_as_buffer
; Flags To Define presence of optional/expanded features
tp_flags.l;
*tp_doc ; Documentation string
; Assigned meaning in release 2.0
; call function For all accessible objects
*tp_traverse
; delete references To contained objects
*tp_clear;
; Assigned meaning in release 2.1
; rich comparisons
*tp_richcompare
; weak reference enabler
tp_weaklistoffset.l
; Iterators
*tp_iter
*tp_iternext
; Attribute descriptor And subclassing stuff
*tp_methods
*tp_members
*tp_getset
*tp_base
*tp_dict
*tp_descr_get
*tp_descr_set
*tp_dictoffset
*tp_init
*tp_alloc
*tp_new
*tp_free ; Low-level free-memory routine
*tp_is_gc ; For PyObject_IS_GC
*tp_bases
*tp_mro ; method resolution order
*tp_cache
*tp_subclasses
*tp_weaklist
*tp_del
; Type attribute cache version tag. Added in version 2.6
tp_version_tag.u
CompilerIf 1 = 0 ;COUNT_ALLOCS
; these must be last And never explicitly initialized
tp_allocs.l
tp_frees.l
tp_maxalloc.l
*tp_prev
*tp_next
CompilerEndIf
EndStructure
Structure PythonObject Align #PB_Structure_AlignC
*_ob_next
*_ob_prev
ob_refcnt.l
*ob_type._typeobject
EndStructure
Macro Python_INCREF(op)
op\ob_refcnt + 1
EndMacro
Macro Python_XINCREF(op)
If op : Python_INCREF(op) : EndIf
EndMacro
Macro Python_DECREF(op)
op\ob_refcnt - 1
If op\ob_refcnt <> 0
; Don't know how to do it
Else
; Don't know how to do it
EndIf
EndMacro
Macro Python_XDECREF(op)
If op : Python_DECREF(op) : EndIf
EndMacro
Macro Python_CLEAR()
If op
*_py_tmp = op
op = #Null
Py_DECREF(*_py_tmp)
EndIf
EndMacro
OSPrototype.i Py_GetProgramFullPath()
OSPrototype.i Py_GetVersion()
OSPrototype.i Py_GetPlatform()
OSPrototype.i Py_GetCopyright()
OSPrototype.i Py_GetCompiler()
OSPrototype.i Py_GetBuildInfo()
OSPrototype Py_SetPythonHome(*String.p-Unicode)
OSPrototype.i Py_GetPythonHome()
OSPrototype Py_SetPath(*String.p-unicode)
OSPrototype.i Py_GetPath()
OSPrototype Py_SetProgramName(*String.p-Unicode)
OSPrototype.i Py_GetProgramName()
OSPrototype Py_Initialize()
OSPrototype Py_InitializeEx(initsigs.l)
OSPrototype Py_Finalize()
OSPrototype Py_IncRef(*PyObject)
OSPrototype Py_DecRef(*PyObject)
OSPrototype.i PyCallable_Check(*PyObject)
OSPrototype.l PyDict_GetItemString(*PyObject, String.p-Ascii)
OSPrototype PyErr_Print()
OSPrototype.l PyErr_Occurred()
OSPrototype PyErr_Clear()
OSPrototype.l PyImport_AddModule(String.p-Ascii)
OSPrototype.i PyImport_Import(*PyObject)
OSPrototype.l PyLong_AsLong(*PyObject)
OSPrototype.l PyLong_FromLong(Value.l)
OSPrototype.l PyModule_GetDict(*PyObject)
OSPrototype.l PyObject_GetAttrString(*PyObject, String.p-Ascii)
OSPrototype.l PyObject_CallObject(*PyObject1, *pyObject2)
OSPrototype.i PyRun_SimpleString(String.p-Ascii)
OSPrototype.l PyTuple_New(Len.l)
OSPrototype.i PyTuple_SetItem(*PyObject1, Size.l, *PyObject2)
OSPrototype.i PyUnicode_FromString(String.p-UTF8)
OSPrototype.i PyUnicode_FromObject(*PyObject)
OSPrototype.i PyUnicode_AsUnicode(*PyObject)
OSPrototype.i PyArg_Parse(*args, format.p-Ascii, *char)
Procedure.i PythonInit()
PythonLib = OpenLibrary(#PB_Any, Python_Libname$)
If PythonLib
Global Pyth_GetProgramFullPath.Py_GetProgramFullPath = GetFunction(PythonLib, "Py_GetProgramFullPath")
Global Pyth_GetVersion.Py_GetVersion = GetFunction(PythonLib, "Py_GetVersion")
Global Pyth_GetPlatform.Py_GetPlatform = GetFunction(PythonLib, "Py_GetPlatform")
Global Pyth_GetCopyright.Py_GetCopyright = GetFunction(PythonLib, "Py_GetCopyright")
Global Pyth_GetCompiler.Py_GetCompiler = GetFunction(PythonLib, "Py_GetCompiler")
Global Pyth_GetBuildInfo.Py_GetBuildInfo = GetFunction(PythonLib, "Py_GetBuildInfo")
Global Python_SetPythonHome.Py_SetPythonHome = GetFunction(PythonLib, "Py_SetPythonHome")
Global Pyth_GetPythonHome.Py_GetPythonHome = GetFunction(PythonLib, "Py_GetPythonHome")
Global Python_SetPath.Py_SetPath = GetFunction(PythonLib, "Py_SetPath")
Global Pyth_GetPath.Py_GetPath = GetFunction(PythonLib, "Py_GetPath")
Global Python_SetProgramName.Py_SetProgramName = GetFunction(PythonLib, "Py_SetProgramName")
Global Pyth_GetProgramName.Py_GetProgramName = GetFunction(PythonLib, "Py_GetProgramName")
Global Python_Initialize.Py_Initialize = GetFunction(PythonLib, "Py_Initialize")
Global Python_InitializeEx.Py_InitializeEx = GetFunction(PythonLib, "Py_InitializeEx")
Global Python_Finalize.Py_Finalize = GetFunction(PythonLib, "Py_Finalize")
Global Python_IncRef.Py_IncRef = GetFunction(PythonLib, "Py_IncRef")
Global Python_DecRef.Py_DecRef = GetFunction(PythonLib, "Py_DecRef")
Global PythonCallable_Check.PyCallable_Check = GetFunction(PythonLib, "PyCallable_Check")
Global PythonDict_GetItemString.PyDict_GetItemString = GetFunction(PythonLib, "PyDict_GetItemString")
Global PythonErr_Print.PyErr_Print = GetFunction(PythonLib, "PyErr_Print")
Global PythonErr_Occurred.PyErr_Occurred = GetFunction(PythonLib, "PyErr_Occurred")
Global PythonErr_Clear.PyErr_Clear = GetFunction(PythonLib, "PyErr_Clear")
Global PythonImport_AddModule.PyImport_AddModule = GetFunction(PythonLib, "PyImport_AddModule")
Global PythonImport_Import.PyImport_Import = GetFunction(PythonLib, "PyImport_Import")
Global PythonLong_AsLong.PyLong_AsLong = GetFunction(PythonLib, "PyLong_AsLong")
Global PythonLong_FromLong.PyLong_FromLong = GetFunction(PythonLib, "PyLong_FromLong")
Global PythonModule_GetDict.PyModule_GetDict = GetFunction(PythonLib, "PyModule_GetDict")
Global PythonObject_GetAttrString.PyObject_GetAttrString = GetFunction(PythonLib, "PyObject_GetAttrString")
Global PythonObject_CallObject.PyObject_CallObject = GetFunction(PythonLib, "PyObject_CallObject")
Global PythonRun_SimpleString.PyRun_SimpleString = GetFunction(PythonLib, "PyRun_SimpleString")
Global PythonTuple_New.PyTuple_New = GetFunction(PythonLib, "PyTuple_New")
Global PythonTuple_SetItem.PyTuple_SetItem = GetFunction(PythonLib, "PyTuple_SetItem")
Global PythonUnicode_FromString.PyUnicode_FromString = GetFunction(PythonLib, "PyUnicode_InternFromString")
Global PythonUnicode_FromObject.PyUnicode_FromObject = GetFunction(PythonLib, "PyUnicode_FromObject")
Global PythonUnicode_AsUnicode.PyUnicode_AsUnicode = GetFunction(PythonLib, "PyUnicode_AsUnicode")
Global PythonArg_Parse.PyArg_Parse = GetFunction(PythonLib, "PyArg_Parse")
EndIf
ProcedureReturn PythonLib
EndProcedure
Procedure.s Python_GetProgramFullPath()
ProcedureReturn PeekS(Pyth_GetProgramFullPath(), -1, #PB_Unicode)
EndProcedure
Procedure.s Python_GetVersion()
ProcedureReturn PeekS(Pyth_GetVersion(), -1, #PB_Ascii)
EndProcedure
Procedure.s Python_GetPlatform()
ProcedureReturn PeekS(Pyth_GetPlatform(), -1, #PB_Ascii)
EndProcedure
Procedure.s Python_GetCopyright()
ProcedureReturn PeekS(Pyth_GetCopyright(), -1, #PB_Ascii)
EndProcedure
Procedure.s Python_GetCompiler()
ProcedureReturn PeekS(Pyth_GetCompiler(), -1, #PB_Ascii)
EndProcedure
Procedure.s Python_GetBuildInfo()
ProcedureReturn PeekS(Pyth_GetBuildInfo(), -1, #PB_Ascii)
EndProcedure
Procedure.s Python_GetPythonHome()
Protected Result$, *String
*String = Pyth_GetPythonHome()
If *String
Result$ = PeekS(*String, -1, #PB_Unicode)
EndIf
ProcedureReturn Result$
EndProcedure
Procedure.s Python_GetPath()
Protected Result$, *String
*String = Pyth_GetPath()
If *String
Result$ = PeekS(*String, -1, #PB_Unicode)
EndIf
ProcedureReturn Result$
EndProcedure
Procedure.s Python_GetProgramName()
Protected Result$, *String
*String = Pyth_GetProgramName()
If *String
Result$ = PeekS(*String, -1, #PB_Unicode)
EndIf
ProcedureReturn Result$
EndProcedure
Procedure PythonClose()
If IsLibrary(PythonLib)
CloseLibrary(PythonLib)
PythonLib = #Null
EndIf
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
;-Demo
Define pystring$, *module, *dictionary, *result, Result.l, ProgramFilename$
OpenConsole()
If PythonInit()
Debug "Version:" + #LF$ + Python_GetVersion() + #LF$
Debug "Platform:" + #LF$ + Python_GetPlatform() + #LF$
Debug "Copyright:" + #LF$ + Python_GetCopyright() + #LF$
Debug "Compiler:" + #LF$ + Python_GetCompiler() + #LF$
Debug "BuildInfo:" + #LF$ + Python_GetBuildInfo() + #LF$
pystring$ = GetPathPart(ProgramFilename())
Python_SetPythonHome(@pystring$)
Debug "PythonHome: " + #LF$ + Python_GetPythonHome() + #LF$
pystring$ = GetPathPart(ProgramFilename()) + "lib\"
Python_SetPath(@pystring$)
Debug "PythonPath: " + #LF$ + Python_GetPath() + #LF$
pystring$ = ProgramFilename()
Python_SetProgramName(@pystring$)
Debug "ProgramFilename: " + #LF$ + Python_GetProgramName() + #LF$
Debug "ProgramFullPath: " + #LF$ + Python_GetProgramFullPath() + #LF$
Python_Initialize()
pystring$ = "from time import time, ctime" + #LF$
pystring$ + "print('Hello PB World from Python!')" + #LF$
pystring$ + "print('Today is', ctime(time()))"
PythonRun_SimpleString(pystring$)
PythonRun_SimpleString("result = 5 ** 2")
*module = PythonImport_AddModule("__main__")
*dictionary = PythonModule_GetDict(*module)
*result = PythonDict_GetItemString(*dictionary, "result")
Result = PythonLong_AsLong(*result)
MessageRequester("Python", Str(Result))
Python_Finalize()
PythonClose()
EndIf
CompilerEndIf
What is really new with 3.5:
You don't need to set environment variables for the PATH stuff.
Btw. the example in the wrapper needs the library stuff below the code in a directory called 'lib'
Code: Select all
pystring$ = GetPathPart(ProgramFilename()) + "lib\"
Python_SetPath(@pystring$)