def include(code, persist=True, compilerflags=None): """This function replaces the *calling module* with a dynamic module that generates code on demand. The code is generated from type descriptions that are created by gccxml compiling the C code 'code'. If <persist> is True, generated code is appended to the module's source code, otherwise the generated code is executed and then thrown away. The calling module must load all the shared libraries that it uses *BEFORE* this function is called. NOTE: - the calling module MUST contain 'from ctypes import *', and, on windows, also 'from ctypes.wintypes import *'. """ compilerflags = compilerflags or ["-c"] # create a hash for the code, and use that as basename for the # files we have to create fullcode = "/* compilerflags: %r */\n%s" % (compilerflags, code) hashval = md5(fullcode).hexdigest() fnm = os.path.abspath(os.path.join(gen_dir, hashval)) h_file = fnm + ".h" xml_file = fnm + ".xml" tdesc_file = fnm + ".typedesc.bz2" if not os.path.exists(h_file): open(h_file, "w").write(fullcode) if is_newer(h_file, tdesc_file): if is_newer(h_file, xml_file): print("# Compiling into...", xml_file, file=sys.stderr) from ctypeslib import h2xml h2xml.compile_to_xml([ "h2xml", "-I", os.path.dirname(fnm), "-q", h_file, "-o", xml_file ] + list(compilerflags)) if is_newer(xml_file, tdesc_file): print("# Parsing XML file and compressing type descriptions...", file=sys.stderr) decls = gccxmlparser.parse(xml_file) ofi = bz2.BZ2File(tdesc_file, "w") data = cPickle.dump(decls, ofi, -1) os.remove(xml_file) # not needed any longer. frame = sys._getframe(1) glob = frame.f_globals name = glob["__name__"] mod = sys.modules[name] sys.modules[name] = DynamicModule(mod, tdesc_file, persist=persist)
def include(code, persist=True, compilerflags=None): """This function replaces the *calling module* with a dynamic module that generates code on demand. The code is generated from type descriptions that are created by gccxml compiling the C code 'code'. If <persist> is True, generated code is appended to the module's source code, otherwise the generated code is executed and then thrown away. The calling module must load all the shared libraries that it uses *BEFORE* this function is called. NOTE: - the calling module MUST contain 'from ctypes import *', and, on windows, also 'from ctypes.wintypes import *'. """ compilerflags = compilerflags or ["-c"] # create a hash for the code, and use that as basename for the # files we have to create fullcode = "/* compilerflags: %r */\n%s" % (compilerflags, code) hashval = md5(fullcode).hexdigest() fnm = os.path.abspath(os.path.join(gen_dir, hashval)) h_file = fnm + ".h" xml_file = fnm + ".xml" tdesc_file = fnm + ".typedesc.bz2" if not os.path.exists(h_file): open(h_file, "w").write(fullcode) if is_newer(h_file, tdesc_file): if is_newer(h_file, xml_file): print >>sys.stderr, "# Compiling into...", xml_file from ctypeslib import h2xml h2xml.compile_to_xml( ["h2xml", "-I", os.path.dirname(fnm), "-q", h_file, "-o", xml_file] + list(compilerflags) ) if is_newer(xml_file, tdesc_file): print >>sys.stderr, "# Parsing XML file and compressing type descriptions..." decls = gccxmlparser.parse(xml_file) ofi = bz2.BZ2File(tdesc_file, "w") data = cPickle.dump(decls, ofi, -1) os.remove(xml_file) # not needed any longer. frame = sys._getframe(1) glob = frame.f_globals name = glob["__name__"] mod = sys.modules[name] sys.modules[name] = DynamicModule(mod, tdesc_file, persist=persist)
def query_items(xml): # XXX: support filter xml_items = parse(xml) #items = {} keep = set() named = {} locations = {} for it in xml_items: #items[it] = it keep.add(it) if hasattr(it, 'name'): named[it] = it.name if hasattr(it, 'location'): locations[it] = it.location return keep, named, locations