Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-02

Python interaction... how?

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Darius Blaszyk

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

Reply by email

leledumbo
to Darius Blaszyk
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).

Reply by email

Michael Van Canneyt
to leledumbo
leledumbo wrote 12 lines
> 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.

Reply by email

Darius Blaszyk
to Michael Van Canneyt

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

<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free.
www.avg.com
<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Fri, Feb 16, 2018 at 9:59 AM, Michael Van Canneyt <

Show quoted text 31 lines
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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Michael Van Canneyt
to Darius Blaszyk
Darius Blaszyk wrote 8 lines
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.

Reply by email