From 3e9c0d2462d922b6b00680ac9b52c495e24ef208 Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Sat, 24 May 2008 14:44:32 +0000 Subject: Revert back to using temporary files to send in headers. Allow Functions 2008-05-24 Johan Dahlin * 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 | 14 +++++++++++ giscanner/ast.py | 1 + giscanner/girwriter.py | 4 +++- giscanner/giscannermodule.c | 26 +++++++++++++++------ giscanner/glibtransformer.py | 12 ++++++++-- giscanner/sourcescanner.py | 55 ++++++++++++++++++++++++++------------------ 6 files changed, 79 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9136b003..bdc9f2ce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2008-05-24 Johan Dahlin + + * 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 * giscanner/glibtransformer.py: diff --git a/giscanner/ast.py b/giscanner/ast.py index aa99db51..ff1fa84d 100644 --- a/giscanner/ast.py +++ b/giscanner/ast.py @@ -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) diff --git a/giscanner/girwriter.py b/giscanner/girwriter.py index 162a0898..494ec512 100644 --- a/giscanner/girwriter.py +++ b/giscanner/girwriter.py @@ -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 diff --git a/giscanner/giscannermodule.c b/giscanner/giscannermodule.c index ce5c4561..df258404 100644 --- a/giscanner/giscannermodule.c +++ b/giscanner/giscannermodule.c @@ -360,28 +360,39 @@ pygi_source_scanner_init (PyGISourceScanner *self, return 0; } +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 }, diff --git a/giscanner/glibtransformer.py b/giscanner/glibtransformer.py index e4385e8d..51aa87cb 100644 --- a/giscanner/glibtransformer.py +++ b/giscanner/glibtransformer.py @@ -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): diff --git a/giscanner/sourcescanner.py b/giscanner/sourcescanner.py index 1aed1202..c459c524 100644 --- a/giscanner/sourcescanner.py +++ b/giscanner/sourcescanner.py @@ -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) -- cgit v1.2.1