Python interaction... how?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Python interaction... how?

Darius Blaszyk-2
Hi all,

I just cannot wrap my head around this one. I would like to implement scripting support to a pure FPC application. The application implements some modifier on an internal data structure which will be exposed to python as a module. The data structure itself is managed by the calling application. How to do this?

Something like:
- FPC app has integer variable a=1
- FPC app calls python script
- Python script adds 10 to a eg pymod.increase_counter(10)
- FPC app variable a is now 11

Does anyone know of a minimal example or pseudo code or just an explanation?

Rgds. Darius
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Python interaction... how?

Free Pascal - General mailing list
> Does anyone know of a minimal example or pseudo code or just an
explanation?

Python actually provides a Python.h that you can convert to a Pascal unit.
But inside, it includes a bunch of other header files and conversion might
be a tedious task. If you made it, though, simply call:

Py_Initialize();
Py_WhateverThereAreMultipleChoicesHere(ProbablyAPythonScriptAsCString);
Py_Finalize();

and you're done.

But if I have to, I would take the external program approach instead using
its interpreter's -c option through TProcess. Communication will then be
done through pipes. It's probably slower, but quicker to work. If you take
the header conversion approach, however, that might benefit everyone else
needing to do the same, just like how the Lua bindings are done (but they're
a lot cleaner that makes conversion simpler).



--
Sent from: http://free-pascal-general.1045716.n5.nabble.com/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Python interaction... how?

Michael Van Canneyt


On Fri, 16 Feb 2018, leledumbo via fpc-pascal wrote:

>> Does anyone know of a minimal example or pseudo code or just an
> explanation?
>
> Python actually provides a Python.h that you can convert to a Pascal unit.
> But inside, it includes a bunch of other header files and conversion might
> be a tedious task. If you made it, though, simply call:
>
> Py_Initialize();
> Py_WhateverThereAreMultipleChoicesHere(ProbablyAPythonScriptAsCString);
> Py_Finalize();
>
> and you're done.

This task has been done for you. See:

http://wiki.freepascal.org/Python4Delphi

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Python interaction... how?

Darius Blaszyk-2
Thanks for the tips and hints! I found this page also explaining well on how to embed python bi-directionally in FPC. https://docs.python.org/3.4/extending/embedding.html

The P4D code was very helpful as well. I stripped all the object and component code and now have a working FPC only python binding for Python 3.6. I'm still cleaning up the code and testing the demo apps from the python site, but if someone is interested I can post a link here.

Darius

Virus-free. www.avg.com

On Fri, Feb 16, 2018 at 9:59 AM, Michael Van Canneyt <[hidden email]> wrote:


On Fri, 16 Feb 2018, leledumbo via fpc-pascal wrote:

Does anyone know of a minimal example or pseudo code or just an
explanation?

Python actually provides a Python.h that you can convert to a Pascal unit.
But inside, it includes a bunch of other header files and conversion might
be a tedious task. If you made it, though, simply call:

Py_Initialize();
Py_WhateverThereAreMultipleChoicesHere(ProbablyAPythonScriptAsCString);
Py_Finalize();

and you're done.

This task has been done for you. See:

http://wiki.freepascal.org/Python4Delphi

Michael.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Python interaction... how?

Michael Van Canneyt


On Sat, 17 Feb 2018, Darius Blaszyk wrote:

> Thanks for the tips and hints! I found this page also explaining well on
> how to embed python bi-directionally in FPC.
> https://docs.python.org/3.4/extending/embedding.html
>
> The P4D code was very helpful as well. I stripped all the object and
> component code and now have a working FPC only python binding for Python
> 3.6. I'm still cleaning up the code and testing the demo apps from the
> python site, but if someone is interested I can post a link here.

Maybe put it in the FPC wiki page that I referred to, so it will not get
lost ?

http://wiki.freepascal.org/Python4Delphi

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal