Python Forum
Calling C functions with PyObjects - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Calling C functions with PyObjects (/thread-19833.html)



Calling C functions with PyObjects - jibarra - Jul-16-2019

I am using Python3.6:
[jibarra@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#pure-embedding

Is there a way to call a C function in the same file using PyObjects?

Please advise.

Thank you.


RE: Calling C functions with PyObjects - Gribouillis - Jul-17-2019

Can you explain the question? From the C file, you can call C functions as in any other C program. There is no problem at all.


RE: Calling C functions with PyObjects - jibarra - Jul-17-2019

Can I use PyImport_Import to import a C file, then call a C function within that file with a PyObject?


RE: Calling C functions with PyObjects - Gribouillis - Jul-17-2019

PyImport_Import() is only a way to run a python import statement from C. I think
PyRun_SimpleString("import foo");
is more or less equivalent to
name = PyUnicode_FromString('foo');
PyImport_Import(name);
although from my experience, calling the python import statement is more robust.


RE: Calling C functions with PyObjects - jibarra - Jul-17-2019

Example:
File1.c:
Init Pyobjects
Call File2 Function:

File2.c:
Some function.


RE: Calling C functions with PyObjects - Gribouillis - Jul-17-2019

If you want to call a function from another C file, I think you can simply compile the file as a shared library and link the library with your extension module.


RE: Calling C functions with PyObjects - jibarra - Jul-17-2019

I have created a shared library. Can I then call those function in the library with PyObjects?