Commits

Sebastien Binet  committed 923fb5d

better py3/py2 compatibility

  • Participants
  • Parent commits 5c201fb

Comments (0)

Files changed (3)

File pyclang/cindex.py

     fd.flush()
     return
 
+if sys.version_info.major == 2:
+    def _to_cstring(x): 
+    	return x
+else:
+    def _to_cstring(x): 
+	return bytes(x,	'ascii')
+
 # ctypes doesn't implicitly convert c_void_p to the appropriate wrapper
 # object. This is a problem, because it means that from_parameter will see an
 # integer and pass the wrong value on platforms where int != void*. Work around
     if not decl.kind.is_invalid:
         return decl.spelling
     k = TypeKind
+    if t.kind == k.TYPEDEF_TYPE:
+        return "typedef "+type2cname(t.canonical_type)
     if t.kind == k.POINTER_TYPE:
         return type2cname(t.pointee_type)+"*"
-    if t.kind == k.TYPEDEF_TYPE:
-        return "typedef "+type2cname(t.canonical_type)
     return {
         k.VOID_TYPE:        "void",
         k.BOOL_TYPE:        "bool",
     @property
     def is_typedef(self):
         return self.kind == TypeKind.TYPEDEF_TYPE
+
+    @property
+    def is_valid(self):
+        return self.kind != TypeKind.INVALID_TYPE
     
     @property
     def pointee_type(self):
         """
         arg_array = 0
         if len(args):
-            arg_array = (c_char_p * len(args))(* args)
+            cargs = []
+            for x in args:
+                cargs.append(_to_cstring(x))
+            arg_array = (c_char_p * len(args))(* cargs)
         unsaved_files_array = 0
         if len(unsaved_files):
             unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()

File tests/t1/run.py

-#!/usr/bin/env python2
+#!/usr/bin/env python
 
 try:
     import user
 import ctypes
 import pyclang.cindex as clang
 
+def myprint(*args):
+    import sys
+    sys.stderr.write(' '.join(args) + '\n')
+    
 def get_diag_info(diag):
     return { 'severity' : diag.severity,
              'location' : diag.location,
              'ranges' : diag.ranges,
              'fixits' : diag.fixits }
 
-def get_cursor_id(cursor, cursor_list = []):
+_all_cursors = []
+def get_cursor_id(cursor):
     if cursor is None:
         return None
 
+    global _all_cursors
     # FIXME: This is really slow. It would be nice if the index API exposed
     # something that let us hash cursors.
-    for i,c in enumerate(cursor_list):
+    for i,c in enumerate(_all_cursors):
         if cursor == c:
             return i
-    cursor_list.append(cursor)
-    return len(cursor_list) - 1
+    _all_cursors.append(cursor)
+    return len(_all_cursors) - 1
 
 def get_info(node, depth=0, filter_fct=None):
     if filter_fct is None:
         filter_fct = lambda x: True
-        filter_fct = lambda x: x.location.file
+        #filter_fct = lambda x: x.location.file
         
     if 0: #opts.maxDepth is not None and depth >= opts.maxDepth:
         children = None
     else:
         children = [get_info(c, depth+1, filter_fct)
-                    for c in node.children if filter_fct(c)]
+                    for c in node.children
+                    if filter_fct(c) and c.kind not in (
+                        clang.CursorKind.MACRO_INSTANTIATION,
+                        clang.CursorKind.MACRO_DEFINITION,
+                        clang.CursorKind.INCLUSION_DIRECTIVE)]
     infos= { 'id' : get_cursor_id(node),
              'kind' : node.kind,
              'usr' : node.get_usr(),
 from pprint import pprint
 
 index = clang.Index.create()
-tu = index.parse(None, ("tt.hh",))
+tu = index.parse(None, ("t1.h",))
+#tu = index.parse(None, ("tt.hh",))
+#tu = index.parse(None, ("/usr/include/cblas.h",))
 diags = map(get_diag_info, tu.diagnostics)
 my_kinds = (clang.CursorKind.FUNCTION_DECL,
             clang.CursorKind.PARM_DECL,
             clang.CursorKind.CXXMETHOD,
-            clang.CursorKind.CLASS_DECL)
+            clang.CursorKind.CLASS_DECL,
+            clang.CursorKind.TYPEDEF_DECL,
+            clang.CursorKind.VAR_DECL,)
 my_cursors = []
 my_types = []
 def my_filter(x):
 pprint(nodes)
 
 for idx,c in enumerate(my_cursors):
-    print "---",idx,c.spelling,
-    print "\t\t",c.type.kind,c.type.spelling,c.type.kind.value,c.access_specifier.name
+    myprint("---",idx,c.spelling)
+    myprint("\t\t",c.type.kind,c.type.spelling,c.type.kind.value,c.access_specifier.name)
 
 cc = my_cursors[ 2]
 c0 = my_cursors[ 9]
 c1 = my_cursors[10]
-c2 = my_cursors[11]
-
-print "canonical type:", c2.type.canonical_type.kind
+#c2 = my_cursors[11]
+#print "canonical type:", c2.type.canonical_type.kind
 
 def handle_param(c, parent_cursor):
-    print "  -param:",c.spelling, clang.type2cname(c.type), "(parent=%s)" % (parent_cursor.spelling)
+    myprint("  -param:",c.spelling, clang.type2cname(c.type), "(parent=%s)" % (parent_cursor.spelling))
     return
 
 def handle_function(c):
-    print "=== [%s (%s)] ===" % (c.spelling, c.type.kind)
+    myprint("=== [%s (%s)] ===" % (c.spelling, c.type.kind))
     t = c.type
     rt = t.result_type
-    print " return-type: '%s' '%s'" % (
-        clang.type2cname(rt),
-        clang.type2cname(rt.canonical_type)
-        )
+    myprint(" return-type: '%s'" % (rt.name,))
+
     args = [i for i in c.get_children()
             if i.kind == clang.CursorKind.PARM_DECL]
-    print " args:", len(args)
+
+    myprint(" args:", len(args))
     for i in args:
         handle_param(i, c)
+    s_args = ['%s %s' % (i.type.name,
+                         i.spelling)
+              for i in args]
+    myprint(" -->[%(return_type)s %(fct_name)s(%(fct_args)s)]" % {
+        'return_type': rt.name,
+        'fct_name': c.spelling,
+        'fct_args': ', '.join(s_args),
+        })
     return
 
-print "="*80
-handle_function(cc)
-for f in my_cursors[9:-1]:
-    print "="*80
-    handle_function(f)
+myprint("="*80)
+for i,f in enumerate(my_cursors):
+    if f.kind in (clang.CursorKind.FUNCTION_DECL,
+                  clang.CursorKind.FUNCTION_TEMPLATE):
+        myprint(i,"="*80)
+        handle_function(f)
 
 ntypes = len(my_types)
 my_types = list([t for t in set(my_types)
-                 if t.kind in my_kinds])
-print "="*80
-print "::: types: (%s)" % (len(my_types),)
-for t in my_types:
-    print "-", t.spelling, t.name, ctypes.addressof(t)
+                 if (t.is_builtin or t.is_pointer or
+                     t.kind == clang.TypeKind.RECORD_TYPE or
+                     t.kind != clang.TypeKind.INVALID_TYPE)])
+myprint("="*80)
+myprint("::: types: (%s)" % (len(my_types),))
+for i,t in enumerate(my_types):
+    myprint("-", i, t.spelling, t.name)

File tests/t1/t1.h

 
 typedef int myint_t;
 
+typedef void (*fct_ptr_t)(int i, double d);
+
+float foo2(myint_t mi);
+void foo3(int i, double d);
+fct_ptr_t ff = &foo3;
+
+