pombredanne / ctypesgen

Automatically exported from code.google.com/p/ctypesgen
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Load symbols on demand? #14

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Currently, the generated wrapper loads all the symbols from libxxx.so at 
import. It could consume a lot of memory at import even if we only use a few 
symbols from the library.

Would it be possible to import the symbols on demand.

Something like:

class ModuleName:
    def __init__(self):
        self.__symbols = {
            'sym1': None,
            'sym2': None,
        }
        self.__symbol_info = {
            'sym1': (libobj, retype, argtypes),
            'sym1': (libobj, retype, argtypes),
        }

    def __get__(self, name):
        if name not in self.__symbols:
            raise SomeError
        obj = self.__symbols[name]
        if obj is None:
            obj = self.load_symbols(name)
            self.__symbbols[name] = obj
        return obj

Maybe make the __symbols and __symbol_info into global variables.

Then we can:

    from ModuleName import ModuleName
    ModuleName.sym1(blah, blah)

Original issue reported on code.google.com by mozbug...@gmail.com on 21 May 2011 at 6:30

GoogleCodeExporter commented 9 years ago
Alternatively , we could define the functions by putting something like this in 
the global scope of the generated module for each function we wanted to export:

def malloc(arg1):
    global malloc
    malloc = libc["malloc"]
    malloc.argtypes = [c_int]
    malloc.restype = c_void_p
    return malloc(arg1)

Here I've used libc's malloc() as an example (and probably gotten some of the 
details wrong).

Original comment by TimM...@gmail.com on 21 May 2011 at 6:55