1 /* GObject introspection: scanner
3 * Copyright (C) 2008 Johan Dahlin <johan@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
26 #include "sourcescanner.h"
31 #include "grealpath.h"
36 #define WIN32_LEAN_AND_MEAN
41 DL_EXPORT(void) init_giscanner(void);
43 #define NEW_CLASS(ctype, name, cname) \
44 static const PyMethodDef _Py##cname##_methods[]; \
45 PyTypeObject Py##cname##_Type = { \
46 PyObject_HEAD_INIT(NULL) \
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
52 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
57 0, 0, NULL, NULL, 0, 0, \
61 #define REGISTER_TYPE(d, name, type) \
62 type.ob_type = &PyType_Type; \
63 type.tp_alloc = PyType_GenericAlloc; \
64 type.tp_new = PyType_GenericNew; \
65 if (PyType_Ready (&type)) \
67 PyDict_SetItemString (d, name, (PyObject *)&type); \
75 static PyObject * pygi_source_type_new (GISourceType *type);
79 GISourceSymbol *symbol;
84 GISourceScanner *scanner;
87 NEW_CLASS (PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
88 NEW_CLASS (PyGISourceType, "SourceType", GISourceType);
89 NEW_CLASS (PyGISourceScanner, "SourceScanner", GISourceScanner);
95 pygi_source_symbol_new (GISourceSymbol *symbol)
97 PyGISourceSymbol *self;
105 self = (PyGISourceSymbol *)PyObject_New (PyGISourceSymbol,
106 &PyGISourceSymbol_Type);
107 self->symbol = symbol;
108 return (PyObject*)self;
112 symbol_get_type (PyGISourceSymbol *self,
115 return PyInt_FromLong (self->symbol->type);
119 symbol_get_line (PyGISourceSymbol *self,
122 return PyInt_FromLong (self->symbol->line);
126 symbol_get_private (PyGISourceSymbol *self,
129 return PyBool_FromLong (self->symbol->private);
133 symbol_get_ident (PyGISourceSymbol *self,
137 if (!self->symbol->ident)
143 return PyString_FromString (self->symbol->ident);
147 symbol_get_base_type (PyGISourceSymbol *self,
150 return pygi_source_type_new (self->symbol->base_type);
154 symbol_get_const_int (PyGISourceSymbol *self,
157 if (!self->symbol->const_int_set)
163 return PyLong_FromLongLong ((long long)self->symbol->const_int);
167 symbol_get_const_double (PyGISourceSymbol *self,
170 if (!self->symbol->const_double_set)
175 return PyFloat_FromDouble (self->symbol->const_double);
179 symbol_get_const_string (PyGISourceSymbol *self,
182 if (!self->symbol->const_string)
188 return PyString_FromString (self->symbol->const_string);
192 symbol_get_source_filename (PyGISourceSymbol *self,
195 if (!self->symbol->source_filename)
201 return PyString_FromString (self->symbol->source_filename);
204 static const PyGetSetDef _PyGISourceSymbol_getsets[] = {
206 { "type", (getter)symbol_get_type, NULL, NULL},
208 { "ident", (getter)symbol_get_ident, NULL, NULL},
209 { "base_type", (getter)symbol_get_base_type, NULL, NULL},
210 /* gboolean const_int_set; */
211 { "const_int", (getter)symbol_get_const_int, NULL, NULL},
212 /* gboolean const_double_set; */
213 { "const_double", (getter)symbol_get_const_double, NULL, NULL},
214 { "const_string", (getter)symbol_get_const_string, NULL, NULL},
215 { "source_filename", (getter)symbol_get_source_filename, NULL, NULL},
216 { "line", (getter)symbol_get_line, NULL, NULL},
217 { "private", (getter)symbol_get_private, NULL, NULL},
226 pygi_source_type_new (GISourceType *type)
228 PyGISourceType *self;
236 self = (PyGISourceType *)PyObject_New (PyGISourceType,
237 &PyGISourceType_Type);
239 return (PyObject*)self;
243 type_get_type (PyGISourceType *self,
246 return PyInt_FromLong (self->type->type);
250 type_get_storage_class_specifier (PyGISourceType *self,
253 return PyInt_FromLong (self->type->storage_class_specifier);
257 type_get_type_qualifier (PyGISourceType *self,
260 return PyInt_FromLong (self->type->type_qualifier);
264 type_get_function_specifier (PyGISourceType *self,
267 return PyInt_FromLong (self->type->function_specifier);
271 type_get_name (PyGISourceType *self,
274 if (!self->type->name)
280 return PyString_FromString (self->type->name);
284 type_get_base_type (PyGISourceType *self,
287 return pygi_source_type_new (self->type->base_type);
291 type_get_child_list (PyGISourceType *self,
299 return Py_BuildValue("[]");
301 list = PyList_New (g_list_length (self->type->child_list));
303 for (l = self->type->child_list; l; l = l->next)
305 PyObject *item = pygi_source_symbol_new (l->data);
306 PyList_SetItem (list, i++, item);
314 type_get_is_bitfield (PyGISourceType *self,
317 return PyInt_FromLong (self->type->is_bitfield);
320 static const PyGetSetDef _PyGISourceType_getsets[] = {
321 { "type", (getter)type_get_type, NULL, NULL},
322 { "storage_class_specifier", (getter)type_get_storage_class_specifier, NULL, NULL},
323 { "type_qualifier", (getter)type_get_type_qualifier, NULL, NULL},
324 { "function_specifier", (getter)type_get_function_specifier, NULL, NULL},
325 { "name", (getter)type_get_name, NULL, NULL},
326 { "base_type", (getter)type_get_base_type, NULL, NULL},
327 { "child_list", (getter)type_get_child_list, NULL, NULL},
328 { "is_bitfield", (getter)type_get_is_bitfield, NULL, NULL},
337 pygi_source_scanner_init (PyGISourceScanner *self,
341 if (!PyArg_ParseTuple (args, ":SourceScanner.__init__"))
344 self->scanner = gi_source_scanner_new ();
350 pygi_source_scanner_append_filename (PyGISourceScanner *self,
355 if (!PyArg_ParseTuple (args, "s:SourceScanner.append_filename", &filename))
358 self->scanner->filenames = g_list_append (self->scanner->filenames,
359 g_realpath (filename));
366 pygi_source_scanner_parse_macros (PyGISourceScanner *self,
373 list = PyTuple_GET_ITEM (args, 0);
375 if (!PyList_Check (list))
377 PyErr_SetString (PyExc_RuntimeError, "parse macro takes a list of filenames");
382 for (i = 0; i < PyList_Size (list); ++i)
387 obj = PyList_GetItem (list, i);
388 filename = PyString_AsString (obj);
390 filenames = g_list_append (filenames, filename);
393 gi_source_scanner_parse_macros (self->scanner, filenames);
394 g_list_free (filenames);
401 pygi_source_scanner_parse_file (PyGISourceScanner *self,
407 if (!PyArg_ParseTuple (args, "i:SourceScanner.parse_file", &fd))
411 /* The file descriptor passed to us is from the C library Python
412 * uses. That is msvcr71.dll at least for Python 2.5. This code, at
413 * least if compiled with mingw, uses msvcrt.dll, so we cannot use
414 * the file descriptor directly. So perform appropriate magic.
418 int (*p__get_osfhandle) (int);
421 msvcr71 = GetModuleHandle ("msvcr71.dll");
424 g_print ("No msvcr71.dll loaded.\n");
428 p__get_osfhandle = GetProcAddress (msvcr71, "_get_osfhandle");
429 if (!p__get_osfhandle)
431 g_print ("No _get_osfhandle found in msvcr71.dll.\n");
435 handle = p__get_osfhandle (fd);
436 if (!p__get_osfhandle)
438 g_print ("Could not get OS handle from msvcr71 fd.\n");
442 fd = _open_osfhandle (handle, _O_RDONLY);
445 g_print ("Could not open C fd from OS handle.\n");
451 fp = fdopen (fd, "r");
454 PyErr_SetFromErrno (PyExc_OSError);
458 if (!gi_source_scanner_parse_file (self->scanner, fp))
460 g_print ("Something went wrong during parsing.\n");
469 pygi_source_scanner_lex_filename (PyGISourceScanner *self,
474 if (!PyArg_ParseTuple (args, "s:SourceScanner.lex_filename", &filename))
477 self->scanner->current_filename = g_strdup (filename);
478 if (!gi_source_scanner_lex_filename (self->scanner, filename))
480 g_print ("Something went wrong during lexing.\n");
483 self->scanner->filenames =
484 g_list_append (self->scanner->filenames, g_strdup (filename));
491 pygi_source_scanner_set_macro_scan (PyGISourceScanner *self,
496 if (!PyArg_ParseTuple (args, "b:SourceScanner.set_macro_scan", ¯o_scan))
499 gi_source_scanner_set_macro_scan (self->scanner, macro_scan);
506 pygi_source_scanner_get_symbols (PyGISourceScanner *self)
512 symbols = gi_source_scanner_get_symbols (self->scanner);
513 list = PyList_New (g_slist_length (symbols));
515 for (l = symbols; l; l = l->next)
517 PyObject *item = pygi_source_symbol_new (l->data);
518 PyList_SetItem (list, i++, item);
526 pygi_source_scanner_get_comments (PyGISourceScanner *self)
528 GSList *l, *comments;
532 comments = gi_source_scanner_get_comments (self->scanner);
533 list = PyList_New (g_slist_length (comments));
535 for (l = comments; l; l = l->next)
537 GISourceComment *comment = l->data;
538 PyObject *item = Py_BuildValue ("(ssi)", comment->comment,
541 PyList_SetItem (list, i++, item);
548 static const PyMethodDef _PyGISourceScanner_methods[] = {
549 { "get_comments", (PyCFunction) pygi_source_scanner_get_comments, METH_NOARGS },
550 { "get_symbols", (PyCFunction) pygi_source_scanner_get_symbols, METH_NOARGS },
551 { "append_filename", (PyCFunction) pygi_source_scanner_append_filename, METH_VARARGS },
552 { "parse_file", (PyCFunction) pygi_source_scanner_parse_file, METH_VARARGS },
553 { "parse_macros", (PyCFunction) pygi_source_scanner_parse_macros, METH_VARARGS },
554 { "lex_filename", (PyCFunction) pygi_source_scanner_lex_filename, METH_VARARGS },
555 { "set_macro_scan", (PyCFunction) pygi_source_scanner_set_macro_scan, METH_VARARGS },
560 static int calc_attrs_length(PyObject *attributes, int indent,
569 for (i = 0; i < PyList_Size (attributes); ++i)
571 PyObject *tuple, *pyvalue;
576 tuple = PyList_GetItem (attributes, i);
577 if (PyTuple_GetItem(tuple, 1) == Py_None)
580 if (!PyArg_ParseTuple(tuple, "sO", &attr, &pyvalue))
583 if (PyUnicode_Check(pyvalue)) {
584 s = PyUnicode_AsUTF8String(pyvalue);
588 value = PyString_AsString(s);
589 } else if (PyString_Check(pyvalue)) {
590 value = PyString_AsString(pyvalue);
592 PyErr_SetString(PyExc_TypeError,
593 "value must be string or unicode");
597 escaped = g_markup_escape_text (value, -1);
598 attr_length += 2 + strlen(attr) + strlen(escaped) + 2;
603 return attr_length + indent + self_indent;
606 /* Hall of shame, wasted time debugging the code below
607 * 20min - Johan 2009-02-19
610 pygi_collect_attributes (PyObject *self,
614 PyObject *attributes;
615 int indent, indent_len, i, j, self_indent;
618 GString *attr_value = NULL;
620 PyObject *result = NULL;
622 if (!PyArg_ParseTuple(args, "sO!isi",
623 &tag_name, &PyList_Type, &attributes,
624 &self_indent, &indent_char,
628 if (attributes == Py_None || !PyList_Size(attributes))
629 return PyUnicode_DecodeUTF8("", 0, "strict");
631 len = calc_attrs_length(attributes, indent, self_indent);
635 indent_len = self_indent + strlen(tag_name) + 1;
640 attr_value = g_string_new ("");
642 for (i = 0; i < PyList_Size (attributes); ++i)
644 PyObject *tuple, *pyvalue;
646 char *attr, *value, *escaped;
648 tuple = PyList_GetItem (attributes, i);
650 if (!PyTuple_Check (tuple))
652 PyErr_SetString(PyExc_TypeError,
653 "attribute item must be a tuple");
657 if (!PyTuple_Size (tuple) == 2)
659 PyErr_SetString(PyExc_IndexError,
660 "attribute item must be a tuple of length 2");
664 if (PyTuple_GetItem(tuple, 1) == Py_None)
667 /* this leaks, but we exit after, so */
668 if (!PyArg_ParseTuple(tuple, "sO", &attr, &pyvalue))
671 if (PyUnicode_Check(pyvalue)) {
672 s = PyUnicode_AsUTF8String(pyvalue);
675 value = PyString_AsString(s);
676 } else if (PyString_Check(pyvalue)) {
677 value = PyString_AsString(pyvalue);
679 PyErr_SetString(PyExc_TypeError,
680 "value must be string or unicode");
684 if (indent_len && !first)
686 g_string_append_c (attr_value, '\n');
687 for (j = 0; j < indent_len; j++)
688 g_string_append_c (attr_value, ' ');
690 g_string_append_c (attr_value, ' ');
691 g_string_append (attr_value, attr);
692 g_string_append_c (attr_value, '=');
693 g_string_append_c (attr_value, '\"');
694 escaped = g_markup_escape_text (value, -1);
695 g_string_append (attr_value, escaped);
696 g_string_append_c (attr_value, '\"');
702 result = PyUnicode_DecodeUTF8 (attr_value->str, attr_value->len, "strict");
704 if (attr_value != NULL)
705 g_string_free (attr_value, TRUE);
711 static const PyMethodDef pyscanner_functions[] = {
712 { "collect_attributes",
713 (PyCFunction) pygi_collect_attributes, METH_VARARGS },
714 { NULL, NULL, 0, NULL }
721 gboolean is_uninstalled;
723 /* Hack to avoid having to create a fake directory structure; when
724 * running uninstalled, the module will be in the top builddir,
725 * with no _giscanner prefix.
727 is_uninstalled = g_getenv ("UNINSTALLED_INTROSPECTION_SRCDIR") != NULL;
728 m = Py_InitModule (is_uninstalled ? "_giscanner" : "giscanner._giscanner",
729 (PyMethodDef*)pyscanner_functions);
730 d = PyModule_GetDict (m);
732 PyGISourceScanner_Type.tp_init = (initproc)pygi_source_scanner_init;
733 PyGISourceScanner_Type.tp_methods = (PyMethodDef*)_PyGISourceScanner_methods;
734 REGISTER_TYPE (d, "SourceScanner", PyGISourceScanner_Type);
736 PyGISourceSymbol_Type.tp_getset = (PyGetSetDef*)_PyGISourceSymbol_getsets;
737 REGISTER_TYPE (d, "SourceSymbol", PyGISourceSymbol_Type);
739 PyGISourceType_Type.tp_getset = (PyGetSetDef*)_PyGISourceType_getsets;
740 REGISTER_TYPE (d, "SourceType", PyGISourceType_Type);