From: mk on
Hello,

I wonder if there is a way to load C extension from in-memory object,
not from the file on the disk?

I'm asking bc I would like to download C extensions over network and
load them into Python interpreter (without storing the C extension in
file on the disk).

I googled for this but there appear only methods of loading compiled
Python (bytecode) modules.

Regards,
mk

From: Martin v. Loewis on

> I wonder if there is a way to load C extension from in-memory object,
> not from the file on the disk?
>
> I'm asking bc I would like to download C extensions over network and
> load them into Python interpreter (without storing the C extension in
> file on the disk).
>
> I googled for this but there appear only methods of loading compiled
> Python (bytecode) modules.

First, it depends on your operating system. None of the standard
operating systems supports loading shared libraries from memory; they
all need a file name.

Of course, your operating system may provide support for RAM disks. So
if you store the extension onto a RAM disk, you can load it from there -
from memory.

It may be possible to extend the Python interpreter to not rely on
shared libraries anymore for extension modules. Such an interpreter
likely wouldn't use standard shared libraries anymore, so you might
then have to recompile the extensions to make them loadable from memory.
However, it also might be possible to reimplement the shared library
loader of the operating system, in which case you could then run
regular extension modules directly from memory.

Regards,
Martin
From: Roger Binns on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 05/13/2010 06:14 AM, mk wrote:
> I wonder if there is a way to load C extension from in-memory object,
> not from the file on the disk?
>
> I'm asking bc I would like to download C extensions over network and
> load them into Python interpreter (without storing the C extension in
> file on the disk).

You could download the C source over the network, compile and use that. It
has the advantage of working on all platforms where you have a compiler so
you wouldn't have to have the same extension compiled for Mac, Linux,
Windows, 32 and 64 bit.

If the local machine doesn't have a compiler you can even use libtcc or
something newer. For example see this 3 year old page, as well as links at
the bottom:

http://www.cs.tut.fi/~ask/cinpy/

Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkv0z4UACgkQmOOfHg372QQ1UQCg2+ODiHMzi36//gG3gK6j4Uib
2u8AoMI2LXU7BFYa7Jn5b+vF42/EnNYU
=RWO+
-----END PGP SIGNATURE-----

From: Christian Heimes on
mk wrote:
> I wonder if there is a way to load C extension from in-memory object,
> not from the file on the disk?

No, that's not possible since Python depends on the operating system. A
lot of operating systems require a physical file to load the shared
library from. Python uses dlopen() on most Un*x platforms and
LoadLibraryEx() on Windows.