Embedding Python in C

Barry Scott barry at barrys-emacs.org
Thu Jul 18 03:16:31 EDT 2019



> On 17 Jul 2019, at 19:39, Jesse Ibarra <jesse.ibarra.1996 at gmail.com> wrote:
> 
> On Wednesday, July 17, 2019 at 11:55:28 AM UTC-6, Barry Scott wrote:
>>> On 17 Jul 2019, at 16:57,  wrote:
>>> 
>>> I am using Python3.6:
>>> 
>>> [jibarra at redsky ~]$ python3.6
>>> Python 3.6.8 (default, Apr 25 2019, 21:02:35) 
>>> [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
>>> Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 
>>> I am referencing:https://docs.python.org/3.6/extending/embedding.html#beyond-very-high-level-embedding-an-overview
>>> 
>>> Is there a way to call a shared C lib using PyObjects?
>> 
>> If what you want to call is simple enough then you can use the ctypes library
>> that ships with python.
>> 
>> If the code you want to call is more complex you will want to use one of a number of libraries to help
>> you create a module that you can import.
>> 
>> I use PyCXX for this purpose that allows me to write C++ code that can call C++ and C libs and interface
>> easily with python. Home page http://cxx.sourceforge.net/ <http://cxx.sourceforge.net/> the source kit contains demo code that you shows
>> how to cerate a module, a class and function etc. 
>> 
>> Example code: https://sourceforge.net/p/cxx/code/HEAD/tree/trunk/CXX/Demo/Python3/simple.cxx <https://sourceforge.net/p/cxx/code/HEAD/tree/trunk/CXX/Demo/Python3/simple.cxx>
>> 
>> Barry
>> PyCXX maintainer
>> 
>>> 
>>> Please advise.
>>> 
>>> Thank you.
>>> -- 
>>> https://mail.python.org/mailman/listinfo/python-list
>>> 
> 
> My options seem rather limited, I need to make a Pipeline from (Smalltalk -> C -> Python) then go back (Smalltalk <- C <- Python). Since Smalltalk does not support Python directly I have to settle with the C/Python API (https://docs.python.org/3.6/extending/embedding.html#beyond-very-high-level-embedding-an-overview <https://docs.python.org/3.6/extending/embedding.html#beyond-very-high-level-embedding-an-overview>). Any suggestions?

1. Run a new python process for each "call" you need to make.
2. Start a subprocess running python that talks via pipes to smalltalk and send messages back and forth to get the work done.
3. Write a C (I'd use C++ myself and PyCXX to avoid the complexity of the Python C API) extension to Smalltalk that initialises
    a python interpreter and bridge calls from Smalltalk into Python on that intepreter.

Depending on the performance you need and the amount of data involved will help decide what is a reasonable design to choose.

Barry



> -- 
> https://mail.python.org/mailman/listinfo/python-list <https://mail.python.org/mailman/listinfo/python-list>



More information about the Python-list mailing list