Revert back to using temporary files to send in headers. Allow Functions
authorJohan Dahlin <jdahlin@async.com.br>
Sat, 24 May 2008 14:44:32 +0000 (14:44 +0000)
committerJohan Dahlin <johan@src.gnome.org>
Sat, 24 May 2008 14:44:32 +0000 (14:44 +0000)
2008-05-24  Johan Dahlin  <jdahlin@async.com.br>

    * giscanner/ast.py:
    * giscanner/girwriter.py:
    * giscanner/giscannermodule.c
    (pygi_source_scanner_append_filename),
    (pygi_source_scanner_parse_file):
    * giscanner/glibtransformer.py:
    * giscanner/sourcescanner.py:
    Revert back to using temporary files to send in headers.
    Allow Functions to be passed in as callbacks, add a couple
    of try/excepts missing features.
    We can now scan pango

svn path=/trunk/; revision=274

ChangeLog
giscanner/ast.py
giscanner/girwriter.py
giscanner/giscannermodule.c
giscanner/glibtransformer.py
giscanner/sourcescanner.py

index 9136b00..bdc9f2c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2008-05-24  Johan Dahlin  <jdahlin@async.com.br>
+
+       * giscanner/ast.py:
+       * giscanner/girwriter.py:
+       * giscanner/giscannermodule.c
+       (pygi_source_scanner_append_filename),
+       (pygi_source_scanner_parse_file):
+       * giscanner/glibtransformer.py:
+       * giscanner/sourcescanner.py:
+       Revert back to using temporary files to send in headers.
+       Allow Functions to be passed in as callbacks, add a couple
+       of try/excepts missing features.
+       We can now scan pango
+
 2008-05-08  Johan Dahlin  <johan@gnome.org>
 
        * giscanner/glibtransformer.py:
index aa99db5..ff1fa84 100644 (file)
@@ -211,6 +211,7 @@ class Property(Node):
             self.name, self.type, self.value)
 
 
+# FIXME: Inherit from Function
 class Callback(Node):
     def __init__(self, name, retval, parameters):
         Node.__init__(self, name)
index 162a089..494ec51 100644 (file)
@@ -190,6 +190,7 @@ class GIRWriter(XMLWriter):
             self._write_type(prop.type)
 
     def _write_callback(self, callback):
+        # FIXME: reuse _write_function
         attrs = [('name', callback.name)]
         with self.tagcontext('callback', attrs):
             self._write_return_type(callback.retval)
@@ -206,7 +207,8 @@ class GIRWriter(XMLWriter):
             self.write_tag('record', attrs)
 
     def _write_field(self, field):
-        if isinstance(field, Callback):
+        # FIXME: Just function
+        if isinstance(field, (Callback, Function)):
             self._write_callback(field)
             return
 
index ce5c456..df25840 100644 (file)
@@ -361,27 +361,38 @@ pygi_source_scanner_init (PyGISourceScanner *self,
 }
 
 static PyObject *
+pygi_source_scanner_append_filename (PyGISourceScanner *self,
+                                    PyObject          *args)
+{
+  char *filename;
+
+  if (!PyArg_ParseTuple (args, "s:SourceScanner.append_filename", &filename))
+    return NULL;
+
+  self->scanner->filenames = g_list_append (self->scanner->filenames,
+                                           g_strdup (filename));
+  
+  Py_INCREF (Py_None);
+  return Py_None;
+}
+
+static PyObject *
 pygi_source_scanner_parse_file (PyGISourceScanner *self,
                                PyObject          *args)
 {
   int fd;
-  char *filename;
   FILE *fp;
   
-  if (!PyArg_ParseTuple (args, "is:SourceScanner.parse_file", &fd, &filename))
+  if (!PyArg_ParseTuple (args, "i:SourceScanner.parse_file", &fd))
     return NULL;
 
   fp = fdopen (fd, "r");
   if (!fp)
     {
-      PyErr_SetFromErrnoWithFilename (PyExc_OSError, filename);
+      PyErr_SetFromErrno (PyExc_OSError);
       return NULL;
     }
 
-  self->scanner->filenames =
-    g_list_append (self->scanner->filenames, g_strdup (filename));
-  self->scanner->current_filename = g_strdup (filename);
-
   if (!gi_source_scanner_parse_file (self->scanner, fp))
     {
       g_print ("Something went wrong during parsing.\n");
@@ -479,6 +490,7 @@ pygi_source_scanner_get_directives (PyGISourceScanner *self,
 static const PyMethodDef _PyGISourceScanner_methods[] = {
   { "get_directives", (PyCFunction) pygi_source_scanner_get_directives, METH_VARARGS },
   { "get_symbols", (PyCFunction) pygi_source_scanner_get_symbols, METH_NOARGS },
+  { "append_filename", (PyCFunction) pygi_source_scanner_append_filename, METH_VARARGS },
   { "parse_file", (PyCFunction) pygi_source_scanner_parse_file, METH_VARARGS },
   { "lex_filename", (PyCFunction) pygi_source_scanner_lex_filename, METH_VARARGS },
   { "set_macro_scan", (PyCFunction) pygi_source_scanner_set_macro_scan, METH_VARARGS },
index e4385e8..51aa87c 100644 (file)
@@ -161,7 +161,11 @@ class GLibTransformer(object):
         if func.parameters:
             return False
 
-        func = getattr(self._library, symbol)
+        try:
+            func = getattr(self._library, symbol)
+        except AttributeError:
+            print 'Warning: could not find symbol: %s' % symbol
+            return False
         func.restype = cgobject.GType
         func.argtypes = []
         type_id = func()
@@ -288,7 +292,11 @@ class GLibTransformer(object):
         self._introspect_properties(node, type_id)
         self._introspect_signals(node, type_id)
         self._add_attribute(node)
-        self._remove_attribute(type_name)
+        try:
+            self._remove_attribute(type_name)
+        except KeyError:
+            print 'Warning: could not remove %s' % type_name
+            pass
         self._register_internal_type(type_name, node)
 
     def _introspect_interface(self, type_id, symbol):
index 1aed120..c459c52 100644 (file)
@@ -20,6 +20,7 @@
 
 import os
 import subprocess
+import tempfile
 
 from . import _giscanner
 
@@ -151,6 +152,10 @@ class SourceScanner(object):
                     self._cpp_options.append(opt)
 
     def parse_files(self, filenames):
+        for filename in filenames:
+            filename = os.path.abspath(filename)
+            self._scanner.append_filename(filename)
+
         headers = []
         for filename in filenames:
             if filename.endswith('.c'):
@@ -159,14 +164,12 @@ class SourceScanner(object):
             else:
                 headers.append(filename)
 
-        for filename in headers:
-            self._parse_one(filename)
-            self._filenames.append(filename)
+        self._parse(headers)
+        self._filenames.extend(headers)
 
     def parse_macros(self):
         self._scanner.set_macro_scan(True)
-        for filename in self._filenames:
-            self._parse_one(filename)
+        self._parse(self._filenames)
         self._scanner.set_macro_scan(False)
 
     def get_symbols(self):
@@ -180,16 +183,10 @@ class SourceScanner(object):
 
     # Private
 
-    def _parse_one(self, filename):
-        filename = os.path.abspath(filename)
-        proc = self._preprocess(filename)
-        fd = proc.stdout.fileno()
-        if proc is None:
+    def _parse(self, filenames):
+        if not filenames:
             return
-
-        self._scanner.parse_file(fd, filename)
-
-    def _preprocess(self, filename):
+        
         cpp_args = [
             'cpp',
             '-C',
@@ -198,13 +195,25 @@ class SourceScanner(object):
             '-I.',
             ]
         cpp_args += self._cpp_options
-        proc = subprocess.Popen(
-            cpp_args,
-            bufsize=4096,
-            stdin=subprocess.PIPE,
-            stdout=subprocess.PIPE,
-            stderr=subprocess.STDOUT,
-            )
-        proc.stdin.write('#include <%s>\n' % (filename,))
+        proc = subprocess.Popen(cpp_args,
+                                stdin=subprocess.PIPE,
+                                stdout=subprocess.PIPE)
+                                
+        for filename in filenames:
+            filename = os.path.abspath(filename)
+            proc.stdin.write('#include <%s>\n' % (filename,))
         proc.stdin.close()
-        return proc
+
+        tmp = tempfile.mktemp()
+        fp = open(tmp, 'w+')
+        while True:
+            data = proc.stdout.read(4096)
+            if data is None:
+                break
+            fp.write(data)
+            if len(data) < 4096:
+                break
+        fp.seek(0, 0)
+        assert proc, 'Proc was none'
+        self._scanner.parse_file(fp.fileno())
+        os.unlink(tmp)