How to view definition of functions of a dynamic library file (.dylib)? - python

So I have a dynamic library file named "libxlearn_api.dylib". I can load this library from within python code by
lib = ctypes.cdll.LoadLibrary(/path/to/lib)
I can also see what functions are defined in the above library by dir(lib).
But, this just gives the list of names of the functions in it.
I need to view the description/definition of these functions(how they are written/implemented, i.e the actual code.)
How can I do this?

I need to view the description/definition of these functions(how they are written/implemented, i.e the actual code.)
In general, you cannot, because the dynamic library might be proprietary (this is the case for many GUI related libraries on MacOSX, read about Aqua).
If the library is free software, or if by some (legal) means you've got its source code (perhaps after signing some NDA and having paid some money), you might study its source code. Then you'll better build that library from source code (to be sure that the binary library and the source code matches), otherwise you need to trust the provider of the binary library that it corresponds exactly to the source code you've got.
BTW, xlearn is free software. Perhaps it is the same as your binary dynamic library (but you need to check). I recommend to be able to build it from its source code (in a terminal, using commands). See its installation guide, section: installing from source code.

Related

Reduce Python static library size

I'm trying to build a minimal Python static library for distribution in an app. This is the C API I need to link to, I believe it may be called CPython as well. I don't need to package actual Python scripts, just link to the library itself.
I've built 3.7.4 by doing:
./configure --disable-shared
make
This does build a library libpython3.7m.a which is 12.4MB. Is it possible to reduce this size at all? I need an absolute barebones distribution without any of the normal packages. Literally this is just for a scripting bridge and doesn't need any of the usual Python functionality.
If you're trying to link to Python's Extension API, you won't find much luck in shaving off size from the libpython shared object. This is because the Extension API uses the same types and headers as the core interpreter (which is why it's called extensions instead of something else like plugins), granting the extensions runtime access to the inner mechanics of the interpreter that normal python scripts wouldn't allow. Basically, the libpython object is just the interpreter compiled without an entry point and an additional frontend so that the expression of simple python operations (e.g. parsing function arguments) stays a clean development experience.
If you're using Python 3.2+, there is offered a "Limited API" for the version of python you are trying to target, but the point of this is to guarantee a stable ABI as the extensions API evolves, not to reduce the size of the compilation unit.
I would also like to mention that the term CPython is just a way to disambiguate the vanilla Python distribution from similar but incompatible interpreters like Pypy.

Creating a Python type in C using an external library: ctypes or setuptools?

I'm writing some sort of Python C extension. It uses my own *.so library and headers from another project (let's say they're in /usr/local/lib/otherproject.so and /usr/local/include/otherproject.h).
I don't know which strategy to follow. I came up with two:
As a pure Python extension
Write a Python C extension just as described in the official docs. The problem here is that I don't know how to link with my own library and headers; to compile, I write a setup.py file and run python3.4 setup.py build. I don't know if I can include some option to the former command, or if I can write something in setup.py to include my headers and binaries (if so, I will also have to worry about making this distributable?).
With ctypes
Write a C library (with my other project's build system). Include Python by passing '/usr/include/python2.7' to find headers and the python2.7 binary. Then use ctypes to wrap around that library and get the functions, types, objects, etc. The inconvenience here is that I need to manually wrap around every single function/type/variable from ctypes; I don't think I can use PyModule_AddObject since I'm not creating the module in C but in the Python wrapper (with ctypes).
Also, I tried the second approach, but I could not successfully get my custom PyTypeObject from ctypes. If the second approach sounds good to any more expert brain here on SO, I would post the code to get any help =).
The second approach also yields problems with distribution. And if you create a Python object in C you should do it in the context of a module. For scenarios where distribution is problematic, you could link this module statically instead.
For your issue with linking you'll find more information about Library options in the documentation. Since your library resides in a directory which should be in the standard library search path, you'd only need to define your library with the libraries option of the Extension class:
mymodule_ext = Extension('mymodule', ['mymodule.c'], libraries=['otherproject'])
If you're not using the standard lib* prefix you'd need to use libraries=[':otherproject.so'] instead.

Using libexif in Python

I'm writing a Python-based [web] application that needs to be able to read and write EXIF data.
libexif seems to have all the right ingredients, but I can't work out how (or if) I could access it access it by using Python's ctypes library? I'm new to C, suppose I need see a .so for this to work?
You need to be running on an os that you can obtain the required library, to download the .h files, (usually the -dev package gives you these).
Then you need to work your way through the ctypes tutorial found here which explains all the steps you need to take.

Python4Delphi-powered program, how to deploy it?

My Delphi program uses Python4Delphi, and the Python script uses some standard libs. When deploying my program I don't want an entire Python installation, and it must work with python27.dll. What are the minimal set of necessary files? The document in Python4Delphi is dated and it's not clear to me...
Thanks for your help.
When I did this, I made the list myself, of what I needed for my embedded python application to work.
I remember this worked with python15.dll:
PythonXX.dll should work, without any other external files other than the Visual C++ Runtime DLLs, which require a side-by-side manifest (see the p4d wiki page) to work.
If you want to IMPORT something, then you need to ship it and anything it depends on. That means, either you pick part of the python standard libraries you want, or you pick all of it. There is no way you need all of Python's standard libraries. But I wouldn't want to live without OS and a a few other key ones. BUt the decision is yours.

Can i take package of cpython?

I used cpython api to load py from C/C++.
But, if i want not setup cpython in client, can I take package dll of cpython in my program?
How to do that?
Installer-builders like PyInstaller (cross-platform) and py2exe (Windows only) basically do that job for you in a general way, except that the executable at the heart of the produced package is their own instead of yours.
But basically, you can imitate their behavior in terms of setting up a .zip file with all the Python library modules you need (or just zip up everything in the standard python library if you want to allow python code running form your app to import anything from there), and follow the simple advice in the Embedding Python in Another Application section of the Python docs.
Note that embedding Python equals extending Python plus a little bit of code to initialize and finalize the interpreter itself and a little bit of packaging as I just mentioned; if you've never writted Python extensions I would suggest practicing that first since it's the most substantial part of the task (not all that hard with helpers such as boost python, but more work if you choose to do it as the "bare C" level instead).
You don't need to setup Python to embed it in applications. The core of the Python interpreter is available as a shared library which you can dynamically load in your application and distribute with it.
Read on embedding Python in the official docs. Also, this article seems nice and comprehensive for Linux. For Windows, read the notes here.
Here's another SO question that discusses this issue.
The Python license is probably hard to understand for a non-lawyer, non-native English speaker. So yes, you can redistribute the unmodified DLL as it contains the copyright notice within it.
It would be polite to give credit like "This program contains the Python Language Interpreter version X.XX http://python.org for more information" or similar somewhere in the program or documentation.

Categories