def _write_structs(self, base_type): f = self.f f.write("class %ss:\n" % base_type) f.write(" pass\n") f.write("\n") cparse_base_type = {"struct": cparser.CStruct, "union": cparser.CUnion}[base_type] last_log_time = time.time() for i, content in enumerate(self.state.contentlist): if time.time() - last_log_time > 2.0: last_log_time = time.time() perc_compl = 100.0 * i / len(self.state.contentlist) cur_content_s = "%s %s" % (content.__class__.__name__, (getattr(content, "name", None) or "<noname>")) cur_file_s = getattr(content, "defPos", "<unknown source>") print "Compile %ss... (%.0f%%) (%s) (%s)" % (base_type, perc_compl, cur_content_s, cur_file_s) if isinstance(content, cparser.CTypedef): content = content.type if not isinstance(content, cparse_base_type): continue if cparser.isExternDecl(content): content = self.state.getResolvedDecl(content) if cparser.isExternDecl(content): # Dummy placeholder. self._write_struct_dummy_extern(content) continue else: # We have a full declaration available. # It will be written later when we come to it in this loop. continue # we will write it later self._try_write_struct(content) f.write("\n\n")
def _write_structs(self, base_type): f = self.f f.write("class %ss:\n" % base_type) f.write(" pass\n") f.write("\n") cparse_base_type = { "struct": cparser.CStruct, "union": cparser.CUnion }[base_type] last_log_time = time.time() for i, content in enumerate(self.state.contentlist): if time.time() - last_log_time > 2.0: last_log_time = time.time() perc_compl = 100.0 * i / len(self.state.contentlist) cur_content_s = "%s %s" % (content.__class__.__name__, (getattr(content, "name", None) or "<noname>")) cur_file_s = getattr(content, "defPos", "<unknown source>") print "Compile %ss... (%.0f%%) (%s) (%s)" % ( base_type, perc_compl, cur_content_s, cur_file_s) if isinstance(content, cparser.CTypedef): content = content.type if not isinstance(content, cparse_base_type): continue if cparser.isExternDecl(content): content = self.state.getResolvedDecl(content) if cparser.isExternDecl(content): # Dummy placeholder. self._write_struct_dummy_extern(content) continue else: # We have a full declaration available. # It will be written later when we come to it in this loop. continue # we will write it later self._try_write_struct(content) f.write("\n\n")
def write_globals(self): self._py_in_globals = True f = self.f f.write("class g:\n") last_log_time = time.time() count = count_incomplete = 0 for i, content in enumerate(self.state.contentlist): if time.time() - last_log_time > 2.0: last_log_time = time.time() perc_compl = 100.0 * i / len(self.state.contentlist) cur_content_s = "%s %s" % (content.__class__.__name__, (getattr(content, "name", None) or "<noname>")) cur_file_s = getattr(content, "defPos", "<unknown source>") print "Compile... (%.0f%%) (%s) (%s)" % (perc_compl, cur_content_s, cur_file_s) if isinstance(content, (cparser.CStruct, cparser.CUnion)): continue # Handled in the other loops. try: if cparser.isExternDecl(content): content = self.state.getResolvedDecl(content) if cparser.isExternDecl(content): # We will write some dummy placeholder. count_incomplete += 1 else: # We have a full declaration available. continue # we will write it later count += 1 if content.name: fix_name(content) if content.name in self._py_globals: print "Error (ignored): %r defined twice, earlier as %r, now as %r" % ( content.name, self._py_globals[content.name], content) continue self._py_globals[content.name] = content else: continue if isinstance(content, cparser.CFunc): funcEnv = self.interpreter._translateFuncToPyAst(content, noBodyMode="code-with-exception") pyAst = funcEnv.astNode assert isinstance(pyAst, ast.FunctionDef) pyAst.decorator_list.append(ast.Name(id="staticmethod", ctx=ast.Load())) Unparser(pyAst, indent=1, file=f) f.write("\n") elif isinstance(content, (cparser.CStruct, cparser.CUnion)): pass # Handled in the other loops. elif isinstance(content, cparser.CTypedef): f.write(" %s = %s\n" % (content.name, self.get_py_type(content.type))) elif isinstance(content, cparser.CVarDecl): # See cparser.interpreter.GlobalScope.getVar() for reference. decl_type, bodyAst, bodyType = \ self.interpreter.globalScope._getDeclTypeBodyAstAndType(content) pyEmptyAst = self.interpreter.globalScope._getEmptyValueAst(decl_type) self._fixup_global_g_inner(pyEmptyAst) f.write(" %s = " % content.name) Unparser(pyEmptyAst, file=f) f.write("\n") bodyValueAst = self.interpreter.globalScope._getVarBodyValueAst( content, decl_type, bodyAst, bodyType) if bodyValueAst is not None: self._fixup_global_g_inner(bodyValueAst) f.write(" helpers.assign(%s, " % content.name) Unparser(bodyValueAst, file=f) f.write(")\n") elif isinstance(content, cparser.CEnum): int_type_name = content.getMinCIntType() f.write(" %s = ctypes_wrapped.%s\n" % (content.name, stdint_ctypes_name(int_type_name))) else: raise Exception("unexpected content type: %s" % type(content)) except Exception as exc: print "!!! Exception while compiling %r" % content if content.name: f.write(" %s = 'Compile exception ' %r\n" % (content.name, str(exc))) sys.excepthook(*sys.exc_info()) # We continue... f.write("\n\n") self._py_in_globals = False
def write_globals(self): self._py_in_globals = True f = self.f f.write("class g:\n") last_log_time = time.time() count = count_incomplete = 0 for i, content in enumerate(self.state.contentlist): if time.time() - last_log_time > 2.0: last_log_time = time.time() perc_compl = 100.0 * i / len(self.state.contentlist) cur_content_s = "%s %s" % (content.__class__.__name__, (getattr(content, "name", None) or "<noname>")) cur_file_s = getattr(content, "defPos", "<unknown source>") print "Compile... (%.0f%%) (%s) (%s)" % ( perc_compl, cur_content_s, cur_file_s) if isinstance(content, (cparser.CStruct, cparser.CUnion)): continue # Handled in the other loops. try: if cparser.isExternDecl(content): content = self.state.getResolvedDecl(content) if cparser.isExternDecl(content): # We will write some dummy placeholder. count_incomplete += 1 else: # We have a full declaration available. continue # we will write it later count += 1 if content.name: fix_name(content) if content.name in self._py_globals: print "Error (ignored): %r defined twice, earlier as %r, now as %r" % ( content.name, self._py_globals[content.name], content) continue self._py_globals[content.name] = content else: continue if isinstance(content, cparser.CFunc): funcEnv = self.interpreter._translateFuncToPyAst( content, noBodyMode="code-with-exception") pyAst = funcEnv.astNode assert isinstance(pyAst, ast.FunctionDef) pyAst.decorator_list.append( ast.Name(id="staticmethod", ctx=ast.Load())) Unparser(pyAst, indent=1, file=f) f.write("\n") elif isinstance(content, (cparser.CStruct, cparser.CUnion)): pass # Handled in the other loops. elif isinstance(content, cparser.CTypedef): f.write(" %s = %s\n" % (content.name, self.get_py_type(content.type))) elif isinstance(content, cparser.CVarDecl): # See cparser.interpreter.GlobalScope.getVar() for reference. decl_type, bodyAst, bodyType = \ self.interpreter.globalScope._getDeclTypeBodyAstAndType(content) pyEmptyAst = self.interpreter.globalScope._getEmptyValueAst( decl_type) self._fixup_global_g_inner(pyEmptyAst) f.write(" %s = " % content.name) Unparser(pyEmptyAst, file=f) f.write("\n") bodyValueAst = self.interpreter.globalScope._getVarBodyValueAst( content, decl_type, bodyAst, bodyType) if bodyValueAst is not None: self._fixup_global_g_inner(bodyValueAst) f.write(" helpers.assign(%s, " % content.name) Unparser(bodyValueAst, file=f) f.write(")\n") elif isinstance(content, cparser.CEnum): int_type_name = content.getMinCIntType() f.write(" %s = ctypes_wrapped.%s\n" % (content.name, stdint_ctypes_name(int_type_name))) else: raise Exception("unexpected content type: %s" % type(content)) except Exception as exc: print "!!! Exception while compiling %r" % content if content.name: f.write(" %s = 'Compile exception ' %r\n" % (content.name, str(exc))) sys.excepthook(*sys.exc_info()) # We continue... f.write("\n\n") self._py_in_globals = False