Can Python be written in FreeBASIC?

General discussion for topics related to the FreeBASIC project or its community.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Can Python be written in FreeBASIC?

Post by BasicCoder2 »

Have spent my spare time trying to learn to use the Python language as it is used in some hardware projects I am interested in.
The problem is being able to write a Python version of a FreeBASIC program and/or write a FreeBASIC version of a Python program.

I noticed that Python is written some other language like C or Java so is it possible to write Python in FreeBASIC?

.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Can Python be written in FreeBASIC?

Post by dodicat »

Isn't Python written in C?
I think the procedures e.t.c. are contained in a .dll file.

You could make a .bi file binding the Python .dll features (Lists and other stuff), to FreeBASIC .
It would be quite hard work, but a good project, especially if you know both languages.

I have not used Python myself, and only skiff over FreeBASIC (avoiding the complicated things where possible).
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Can Python be written in FreeBASIC?

Post by TeeEmCee »

CPython is written in C, which is why it's called CPython.

CPython is a bit of a pain to embed in other programs. It was just not designed to be nice to embed. If you just want to execute a block of Python code that's quite easy; just use PyRun_SimpleString.

But if you want to embed CPython and pass data back and forth between Python code and external code I highly recommend using either Boost.Python or pybind11. They are fantastic libraries that make it easy to embed Python. pybind11 is a lot like Boost.Python without a dependency on Boost.

Unfortunately, they are both C++ libraries, so my recommendation is unlikely to be popular in these parts. Nonetheless, I am currently using pybind11 to embed CPython in a Freebasic program, keeping the C++11 glue code in a separate module. The idea is that my program will be composed out of .py and .bas files which can call functions in each other seemlessly, without needing to know anything about the CPython API, thanks to the cffi python library and compile-time generation of header files. I'm only just starting on that, but I can post my code here when it's working. This just a prototype but I might end up using the CPython API directly instead of pybind11, since it's only glue code anyway, and C++11 can be a problem on some platforms.

Otherwise, just use fbfrog to translate the Cpython headers to FB. Thankfully, a subset of the ABI is stable between releases, which means that you don't need to re-translate the CPython headers for each CPython version, as long as you stick to the subset. Note that the API is mostly stable between releases, but that only helps C code, or if you generating the .bi headers as part of your build system. Without Py_LIMITED_API, the headers aren't compatible between versions.

BTW, I found this to be a nice article about embedding Python and Lua (in a C program) and comparing the two.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Can Python be written in FreeBASIC?

Post by dkl »

Translating a program from Python to FB or vice-versa is probably very difficult, because the languages are so different. For example, Python has dynamic types, garbage collection, special syntax for built-in lists/maps, exceptions... all of which doesn't exist in FB.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Can Python be written in FreeBASIC?

Post by datwill310 »

dodicat wrote:Isn't Python written in C?
I think the procedures e.t.c. are contained in a .dll file.

You could make a .bi file binding the Python .dll features (Lists and other stuff), to FreeBASIC .
Isn't that illegal? I'm not trying to get anyone in trouble, to be clear, but I know of some languages (like Pure BASIC) which prohibit this action.

EDIT - Maybe I'm wrong: here's what the PB FAQ page says:
PB FAQ wrote:Is it allowed to use DLLs made with PureBasic in other projects ?
Generally yes. You can make DLLs including PureBasic commands for your own projects without any restrictions. But it's not allowed to release simple "wrapper" Dlls to include PureBasic commands in other programming languages.
https://www.purebasic.com/faq.php
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Can Python be written in FreeBASIC?

Post by caseih »

Yeah Boost will be of no use to FreeBASIC programmers since it's about C++, which FB has little support for. But actually the Python C API is not that bad really. The beauty of it is that there's no difference whether you embed Python in your C program or whether you are extending Python with a C module. It's kind of a neat thing. You'd spend most of your time converting between C-type variables and Python objects.

The original question seems ambiguous. Is he asking whether you can write a Python interpreter in FreeBASIC? The answer to that is sure, there's no reason something couldn't be written in FB if it can be implemented in any other language, especially C, since FB is nearly 1:1 with C. Much effort would be required! Or was his question can he extend Python with code written in FB (or embed Python in his FB programs), and the answer to that is, yes, provided you can get .bi equivalents of all the libpython header files.
datwill310 wrote:
dodicat wrote:You could make a .bi file binding the Python .dll features (Lists and other stuff), to FreeBASIC .
Isn't that illegal? I'm not trying to get anyone in trouble, to be clear, but I know of some languages (like Pure BASIC) which prohibit this action.
EDIT - Maybe I'm wrong: here's what the PB FAQ page says:
I'm not sure what the Pure BASIC license has to do with Python. There are lots of licenses in the world. The Python intepreter (and it's dll, libpython.dll) are released under the license that is here: https://docs.python.org/3/license.html. The terms are pretty liberal, almost like the BSD license. Python is embedded in a number of project, open source and commercial. So you may indeed freely embed or extend Python with FB if you so chose. The only pitfall here is that not all third-party modules are under the same terms as Python itself. In fact many are much more restrictive (from a proprietary point of view).

I think FB would be a great language for writing extensions to Python, but no one has yet produced .bi files for libpython. The Python object model does not map 1:1 with FB, so if you were going to embed Python in your project and expose your own program's objects to Python, you'd have to create wrappers that would interface between the Python world and the FB world. In most bindings I've seen, what ends up happening is the binding layer creates a thin Python object that is essentially kept in sync with the other language's object. When one is deleted, the other must be deleted too. Sounds convoluted, and it is to a point.

Another possible way to use FB and Python together is to make normal C-style DLLs with FB and then use them from Python with the ctypes mechanism.

Back on Pure BASIC for a moment, that FAQ is kind of interesting. I'm not sure such a distinction is even legally enforceable, since if you make a dll that uses a PB command, thus exposing it in some fashion, you are by definition making that available to the other language. I suspect the actual license terms are much more clear, though. Unfortunately they don't have their actual runtime license posted anywhere. I think I'll stick to more standard tools, myself, with clear licensing.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Can Python be written in FreeBASIC?

Post by caseih »

sadly fbfrog cannot translate the Python .h files. It's getting hung up on a symbol __WORDSIZE, which is really supposed to be internal to glibc and gcc. Even though Python's .h files are importing a linux header file that defines that, fbfrog isn't seeing it for some reason.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Can Python be written in FreeBASIC?

Post by BasicCoder2 »

caseih wrote:The original question seems ambiguous. Is he asking whether you can write a Python interpreter in FreeBASIC?
That was the main question as in practice I understand the Python interpreter to be written in languages like C and Java.
Or was his question can he extend Python with code written in FB (or embed Python in his FB programs), and the answer to that is, yes, provided you can get .bi equivalents of all the libpython header files.
I have no need to extend Python only to translate FreeBASIC programs to a Python equivalent. In theory this requires learning Python and any imports required to duplicate FreeBasic graphics.

I think translating a Python program into a FreeBASIC program would require a lot more work and the ability of FreeBASIC to use any of the import libraries used in the Python program. For this reason it seems to me there is no choice but to forget FreeBASIC as an option as only Python provides the libraries required in the hardware projects.

Really I hate Python as my projects haven't required all that list stuff and wished there were libraries for plain old C which I used to use back on the old DOS machines.

.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Can Python be written in FreeBASIC?

Post by TeeEmCee »

Here are two a handful of options. Firstly, if's those Python libraries are for hardware control, they may be implemented as a python extension written in C, so that you can use their C source code directly from FB (you might have to adapt it heavily to remove the Python api functions). However it's quite possible that it really is implemented in pure Python.

Secondly, you can write your program in FB, except for a few parts written in Python to interact with the Python library. Basically, writing a FB wrapper around the API for the Python library. You can do this by embedding Python in a FB program or vice versa (as a Python extension).

If you don't like Python and can't get plain FB to work, there are also many other languages that can use python libraries, like C++, Ruby, Lua (or here), Julia, Matlab and of course, C (using Cython instead of the CPython API). Cython or pybind11 are good for writing C-Python glue code.
Last edited by TeeEmCee on Nov 27, 2016 5:37, edited 1 time in total.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Can Python be written in FreeBASIC?

Post by sancho2 »

datwill310 wrote:Isn't that illegal?
This is a terrible way to phrase it.
No it is not illegal. Nowhere is it illegal.
Does it violate the PB EULA, possibly. But that does not make it "illegal".
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Can Python be written in FreeBASIC?

Post by TeeEmCee »

I've managed to create a partially working Python.bi header for python 3.2+ on 64 bit GNU/Linux (and probably LP64 systems in general). One of the difficulties is that Python.h includes a config.h which is specific to the host system (SIZEOF_LONG, etc), and Python.h contains checks to prevent you from using a config.h file incompatible with the host system, which fails due to fbfrog's attempt to create a multi-arch header. So to support other systems you would either have to feed fbfrog at least three different sets of headers (for LLP64, LP64, and ILP32 systems) and have it splice them together, or manually edit it.

Header: http://pastebin.com/dZv9pBtb
The header contains 38 TODOs.

.fbfrog file to generate the header:

Code: Select all

-o .
-target linux-x86_64
/usr/include/python3.5m/Python.h

# PyMemberDef is defined in structmember.h, which is not included by
# default, and also forward declared and used in descrobject.h, but
# fbfrog drops the forward decl if the definition is missing
/usr/include/python3.5m/structmember.h

# Same for _node
/usr/include/python3.5m/node.h

-define NULL 0
-fbfroginclude limits.h

# Python 3.2 or later, stable ABI
-define Py_LIMITED_API 0x03020000
-rename_ destructor 

# In the original header, there are typedefs like "typedef struct _typeobject PyTypeObject;"
# which are subtypes of PyObject but the definition of which is hidden by Py_LIMITED_API.
# (Note there are various "extern incomplete_type foo;" which FB doesn't allow).
-rename _typeobject PyObject
-rename _longobject PyObject
-rename _frame PyObject

-addforwarddecl symtable 

-addforwarddecl PyModuleDef_Slot

-removeinclude "assert.h"
-removeinclude "unistd.h"
-removeinclude "inttypes.h"
-removeinclude "sys/select.h"
-removeinclude "sys/time.h"
-removeinclude "sys/stat.h"
-remove PyMem_MALLOC
-remove PyMem_REALLOC
-remove PyMem_FREE
-remove PyMem_NEW
-remove PyMem_RESIZE
-remove PyMem_DEL
-remove PyObject_MALLOC
-remove PyObject_REALLOC
-remove PyObject_DEL
-remove PyObject_FREE
-remove PyObject_INIT
-remove PyObject_NEW
-remove PyODict_SIZE
-remove PyThreadState_GET

-rename_ _Py_IDENTIFIER

# Py_DECREF, etc, are macros which are more efficient inline implementations of Py_DecRef, etc,
# but it's easier to just remove these.
-remove Py_DECREF

-setarraysize _Py_SwappedOp 1

-undefbeforedecl PyErr_BadInternalCall

# The following are functions which were renamed, the original header double-declares them.
# Maybe more compatible to use the old name?
-undefbeforedecl PyObject_Length
-undefbeforedecl PyMapping_Length
-undefbeforedecl PySequence_Length
-undefbeforedecl PySequence_In

# _Py_stat decl requires stat from sys/stat.h
-remove _Py_stat

-rename_ bufsiz

-rename_ TYPE
-rename_ STR
(Edit: the "-target linux-x86_64" option is something I've added to my fork of fbfrog; hopefully it ends up in the official version)

Example:

Code: Select all

#include "Python.bi"

    Py_Initialize()

    dim as PyObject ptr code = Py_CompileString( _
            !"from time import time,ctime\n" _
            "print('Today is', ctime(time()))", "<input>", Py_file_input)
    if code = NULL then
        PyErr_Print()
        end 1
    end if

    dim as PyObject ptr pdict = PyDict_New()
    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins())
    dim as PyObject ptr ret = PyEval_EvalCode(code, pdict, pdict)
    if code = NULL then
        PyErr_Print()
        end 1
    end if
    
    Py_Finalize()
All of the simple PyRun_* functions are removed when using Py_LIMITED_API :(

I'm not going to use this header myself; better to write glue code in C/C++ and not have to worry about header translation problems. Also, I'm going away for two weeks, so if someone is interested they should take over.
Last edited by TeeEmCee on Nov 28, 2016 10:22, edited 5 times in total.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Can Python be written in FreeBASIC?

Post by datwill310 »

sancho2 wrote:
datwill310 wrote:Isn't that illegal?
This is a terrible way to phrase it.
No it is not illegal. Nowhere is it illegal.
Does it violate the PB EULA, possibly. But that does not make it "illegal".
I am sorry to have used this phrase: I'm not all too educated in that area (I didn't know that most copyright law infringements were not illegal). I was wrong in bringing in a whole other language into this forum as well.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Can Python be written in FreeBASIC?

Post by BasicCoder2 »

The answer to the post subject then is yes but would be a major undertaking.
Writing a FB version of Python projects using imported libraries would be all too hard for someone with my limited skills.
Writing a Python version of any FB program written with plain vanilla BASIC commands and FB graphics seems easy enough.
.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Can Python be written in FreeBASIC?

Post by sancho2 »

@Datwill:
A little too much wine on my part last night. I apologize.
I think however that it is a bit of a leap to say that violating the license (in this way) would equate copyright infringement.
Last edited by sancho2 on Nov 28, 2016 8:03, edited 1 time in total.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Can Python be written in FreeBASIC?

Post by caseih »

Python is a very high level language so all hardware access is done via c libraries and calls. I'm not sure what kind of hardware libraries you are taking about that are based on Python. I'm sure under it there are c libraries or c code and that could be ported to fb or used from fb. What is this hardware project you're writing on, basiccoder?

Personally I'm pretty excited about a project called micro Python which is a subset of Python that can run directly in microcontrollers. Pretty neat stuff as Python is so expressive and quick to develop in. Still an interpreter though so it won't compile replace the need for C in embedded programming.

As far as it goes you can use Python code from within fb, if you use the API that teeemcee wrapped with fbfrog. Like I said it's the same process to use Python from within your fb program as it is to extend Python with fb. Thanks for the .bi files by the way. They may come in handy some day.
Post Reply