Do you tryed my minimal "python.bi" file ?Cretin Ho wrote:I wanted to try writing a toy extension for Python in FBC. The point is just to prove it's possible or not. If no one ever think about it then my work will be much more difficult :)
The primary point are if you write in python: import MyModule
A extension with "MyModule.pyd" are loaded in the process space of the python environment.
Then initMyModule() from your exported FreeBASIC code are called to init your extension in the python script parsing AST.
NOTE: initMyModule() is "init" and the extension name "MyModule"
for example if your extension are named 2DVectorEngine "init2DVectorEngine" must be exported !
By the way initXYZ() is the one and only exported "sub" and visible by python !
(In contrast to a DLL wrapper where everything has to be exported)
The complete interface of your extension are registered and shared wth python with one call of Py_InitModule().
I tested only python 2.7 (but with Windows and Linux 32/64-bit) let me know if you run in to a problems with python 3.x
Maybe only one function declare and the #define of PYTHON_API_VERSION in python.bi must be changed for 3.x !
Joshy
Code: Select all
sub initMyModule() export
methods(0).ml_name = @"beep" ' <-- add a method "beep" (the visible name) without any argument
methods(0).ml_meth = @exportBeep ' <-- the real name of your code
methods(0).ml_flags = METH_NOARGS
methods(0).ml_doc = @"Test calling beep."
Code: Select all
methods(1).ml_name = @"add" <-- add a function "add" (the visible name) with arguments
methods(1).ml_meth = @exportAdd ' <-- the real name of your code
methods(1).ml_flags = METH_VARARGS
methods(1).ml_doc = @"Test of adding int numbers."
' last methods(2) is the NULL terminator
Code: Select all
Py_InitModule4("MyModule",@methods(0)) ' <-- register your methods, functions, objects, classes etc.
end sub