diff --git a/.gitignore b/.gitignore index d4fd55c..7c05519 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ ctypeslib.egg-info/ ctypeslib2.egg-info/ *.gitignore~ *.so +.*.swp diff --git a/ctypeslib/codegen/codegenerator.py b/ctypeslib/codegen/codegenerator.py index f27a019..7d9d9fa 100644 --- a/ctypeslib/codegen/codegenerator.py +++ b/ctypeslib/codegen/codegenerator.py @@ -226,7 +226,11 @@ def Macro(self, macro): body = float_value # what about integers you ask ? body token that represents token are Integer here. # either it's just a thing we gonna print, or we need to have a registered item - print("%s = %s # macro" % (macro.name, body), file=self.stream) + if sum(x=='(' for x in body) != sum(x==')' for x in body): + # unbalanced parens means comment + print("# %s = %s # macro" % (macro.name, body), file=self.stream) + else: + print("%s = %s # macro" % (macro.name, body), file=self.stream) self.macros += 1 self.names.append(macro.name) # This is why we need to have token types all the way here. @@ -515,10 +519,10 @@ def _get_undefined_head_dependencies(self, struct): """Return head dependencies on other record types. Head dependencies is exclusive of body dependency. It's one or the other. """ - r = set() + r = dict() for m in struct.members: if isinstance(m.type, typedesc.PointerType) and typedesc.is_record(m.type.typ): - r.add(m.type) + r[m.type] = None # remove all already defined heads r = [_ for _ in r if _.name not in self.head_generated] return r @@ -527,14 +531,14 @@ def _get_undefined_body_dependencies(self, struct): """Return head dependencies on other record types. Head dependencies is exclusive of body dependency. It's one or the other. """ - r = set() + r = dict() for m in struct.members: if isinstance(m.type, typedesc.ArrayType) and typedesc.is_record(m.type.typ): - r.add(m.type.typ) + r[m.type.typ] = None elif typedesc.is_record(m.type): - r.add(m.type) + r[m.type] = None elif m.type not in self.done: - r.add(m.type) + r[m.type] = None # remove all already defined bodies r = [_ for _ in r if _.name not in self.body_generated] return r @@ -850,14 +854,14 @@ class LibraryStub: if self.generate_locations and func.location: print("# %s %s" % func.location, file=self.stream) # Generate the function decl code - print("%s = %s.%s" % (func.name, libname, func.name), file=self.stream) - print( - "%s.restype = %s" % (func.name, self.type_name(func.returns)), - file=self.stream, - ) + print("try:", file=self.stream) + print(" %s = %s.%s" % (func.name, libname, func.name), file=self.stream) + print(" %s.restype = %s" % (func.name, self.type_name(func.returns)), file=self.stream) if self.generate_comments: print("# %s(%s)" % (func.name, ", ".join(argnames)), file=self.stream) - print("%s.argtypes = [%s]" % (func.name, ", ".join(args)), file=self.stream) + print(" %s.argtypes = [%s]" % (func.name, ", ".join(args)), file=self.stream) + print("except AttributeError:", file=self.stream) + print(" pass", file=self.stream) if self.generate_docstrings: diff --git a/ctypeslib/library.py b/ctypeslib/library.py index ca38167..1ae5706 100644 --- a/ctypeslib/library.py +++ b/ctypeslib/library.py @@ -41,6 +41,7 @@ def _get_symbols(self, nm): for line in output.split('\n'): fields = line.split(' ', 2) if len(fields) >= 3 and fields[1] in ("T", "D", "G", "R", "S"): + if '@@' in fields[2]: fields[2] = fields[2].split('@@')[0] self.__symbols[fields[2]] = fields[0] def __getattr__(self, name):