Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
asdl_c.PyTypesVisitor Class Reference
Inheritance diagram for asdl_c.PyTypesVisitor:
Inheritance graph
[legend]
Collaboration diagram for asdl_c.PyTypesVisitor:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def visitModule
def visitProduct
def visitSum
def visitConstructor
def visitType
def visitConstructor
def visitField
def emit
def visit

Public Attributes

 file
 cache
 skip
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Detailed Description

Definition at line 587 of file asdl_c.py.


Member Function Documentation

def asdl_c.EmitVisitor.emit (   self,
  s,
  depth,
  reflow = True 
) [inherited]

Definition at line 89 of file asdl_c.py.

00089 
00090     def emit(self, s, depth, reflow=True):
00091         # XXX reflow long lines?
00092         if reflow:
00093             lines = reflow_lines(s, depth)
00094         else:
00095             lines = [s]
00096         for line in lines:
00097             line = (" " * TABSIZE * depth) + line + "\n"
00098             self.file.write(line)
00099 

Here is the call graph for this function:

def asdl.VisitorBase.visit (   self,
  object,
  args 
) [inherited]

Definition at line 319 of file asdl.py.

00319 
00320     def visit(self, object, *args):
00321         meth = self._dispatch(object)
00322         if meth is None:
00323             return
00324         try:
00325             meth(object, *args)
00326         except Exception:
00327             output("Error visiting" + repr(object))
00328             output(str(sys.exc_info()[1]))
00329             traceback.print_exc()
00330             # XXX hack
00331             if hasattr(self, 'file'):
00332                 self.file.flush()
00333             os._exit(1)

Here is the call graph for this function:

Here is the caller graph for this function:

def asdl_c.PickleVisitor.visitConstructor (   self,
  cons,
  name 
) [inherited]

Reimplemented in asdl_c.ASTModuleVisitor, and asdl_c.PyTypesDeclareVisitor.

Definition at line 347 of file asdl_c.py.

00347 
00348     def visitConstructor(self, cons, name):
00349         pass

Here is the caller graph for this function:

def asdl_c.PyTypesVisitor.visitConstructor (   self,
  cons,
  name,
  simple 
)

Definition at line 886 of file asdl_c.py.

00886 
00887     def visitConstructor(self, cons, name, simple):
00888         if cons.fields:
00889             fields = cons.name.value+"_fields"
00890         else:
00891             fields = "NULL"
00892         self.emit('%s_type = make_type("%s", %s_type, %s, %d);' %
00893                             (cons.name, cons.name, name, fields, len(cons.fields)), 1)
00894         self.emit("if (!%s_type) return 0;" % cons.name, 1)
00895         if simple:
00896             self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" %
00897                              (cons.name, cons.name), 1)
00898             self.emit("if (!%s_singleton) return 0;" % cons.name, 1)
00899 

Here is the call graph for this function:

def asdl_c.PickleVisitor.visitField (   self,
  sum 
) [inherited]

Definition at line 350 of file asdl_c.py.

00350 
00351     def visitField(self, sum):
00352         pass
00353 

Here is the caller graph for this function:

def asdl_c.PyTypesVisitor.visitModule (   self,
  mod 
)

Reimplemented from asdl_c.PickleVisitor.

Definition at line 589 of file asdl_c.py.

00589 
00590     def visitModule(self, mod):
00591         self.emit("""
00592 static int
00593 ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
00594 {
00595     Py_ssize_t i, numfields = 0;
00596     int res = -1;
00597     PyObject *key, *value, *fields;
00598     fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
00599     if (!fields)
00600         PyErr_Clear();
00601     if (fields) {
00602         numfields = PySequence_Size(fields);
00603         if (numfields == -1)
00604             goto cleanup;
00605     }
00606     res = 0; /* if no error occurs, this stays 0 to the end */
00607     if (PyTuple_GET_SIZE(args) > 0) {
00608         if (numfields != PyTuple_GET_SIZE(args)) {
00609             PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
00610                          "%zd positional argument%s",
00611                          Py_TYPE(self)->tp_name,
00612                          numfields == 0 ? "" : "either 0 or ",
00613                          numfields, numfields == 1 ? "" : "s");
00614             res = -1;
00615             goto cleanup;
00616         }
00617         for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
00618             /* cannot be reached when fields is NULL */
00619             PyObject *name = PySequence_GetItem(fields, i);
00620             if (!name) {
00621                 res = -1;
00622                 goto cleanup;
00623             }
00624             res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
00625             Py_DECREF(name);
00626             if (res < 0)
00627                 goto cleanup;
00628         }
00629     }
00630     if (kw) {
00631         i = 0;  /* needed by PyDict_Next */
00632         while (PyDict_Next(kw, &i, &key, &value)) {
00633             res = PyObject_SetAttr(self, key, value);
00634             if (res < 0)
00635                 goto cleanup;
00636         }
00637     }
00638   cleanup:
00639     Py_XDECREF(fields);
00640     return res;
00641 }
00642 
00643 /* Pickling support */
00644 static PyObject *
00645 ast_type_reduce(PyObject *self, PyObject *unused)
00646 {
00647     PyObject *res;
00648     PyObject *dict = PyObject_GetAttrString(self, "__dict__");
00649     if (dict == NULL) {
00650         if (PyErr_ExceptionMatches(PyExc_AttributeError))
00651             PyErr_Clear();
00652         else
00653             return NULL;
00654     }
00655     if (dict) {
00656         res = Py_BuildValue("O()O", Py_TYPE(self), dict);
00657         Py_DECREF(dict);
00658         return res;
00659     }
00660     return Py_BuildValue("O()", Py_TYPE(self));
00661 }
00662 
00663 static PyMethodDef ast_type_methods[] = {
00664     {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
00665     {NULL}
00666 };
00667 
00668 static PyTypeObject AST_type = {
00669     PyVarObject_HEAD_INIT(&PyType_Type, 0)
00670     "_ast.AST",
00671     sizeof(PyObject),
00672     0,
00673     0,                       /* tp_dealloc */
00674     0,                       /* tp_print */
00675     0,                       /* tp_getattr */
00676     0,                       /* tp_setattr */
00677     0,                       /* tp_reserved */
00678     0,                       /* tp_repr */
00679     0,                       /* tp_as_number */
00680     0,                       /* tp_as_sequence */
00681     0,                       /* tp_as_mapping */
00682     0,                       /* tp_hash */
00683     0,                       /* tp_call */
00684     0,                       /* tp_str */
00685     PyObject_GenericGetAttr, /* tp_getattro */
00686     PyObject_GenericSetAttr, /* tp_setattro */
00687     0,                       /* tp_as_buffer */
00688     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
00689     0,                       /* tp_doc */
00690     0,                       /* tp_traverse */
00691     0,                       /* tp_clear */
00692     0,                       /* tp_richcompare */
00693     0,                       /* tp_weaklistoffset */
00694     0,                       /* tp_iter */
00695     0,                       /* tp_iternext */
00696     ast_type_methods,        /* tp_methods */
00697     0,                       /* tp_members */
00698     0,                       /* tp_getset */
00699     0,                       /* tp_base */
00700     0,                       /* tp_dict */
00701     0,                       /* tp_descr_get */
00702     0,                       /* tp_descr_set */
00703     0,                       /* tp_dictoffset */
00704     (initproc)ast_type_init, /* tp_init */
00705     PyType_GenericAlloc,     /* tp_alloc */
00706     PyType_GenericNew,       /* tp_new */
00707     PyObject_Del,            /* tp_free */
00708 };
00709 
00710 
00711 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
00712 {
00713     PyObject *fnames, *result;
00714     int i;
00715     fnames = PyTuple_New(num_fields);
00716     if (!fnames) return NULL;
00717     for (i = 0; i < num_fields; i++) {
00718         PyObject *field = PyUnicode_FromString(fields[i]);
00719         if (!field) {
00720             Py_DECREF(fnames);
00721             return NULL;
00722         }
00723         PyTuple_SET_ITEM(fnames, i, field);
00724     }
00725     result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
00726                     type, base, "_fields", fnames, "__module__", "_ast");
00727     Py_DECREF(fnames);
00728     return (PyTypeObject*)result;
00729 }
00730 
00731 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
00732 {
00733     int i, result;
00734     PyObject *s, *l = PyTuple_New(num_fields);
00735     if (!l)
00736         return 0;
00737     for (i = 0; i < num_fields; i++) {
00738         s = PyUnicode_FromString(attrs[i]);
00739         if (!s) {
00740             Py_DECREF(l);
00741             return 0;
00742         }
00743         PyTuple_SET_ITEM(l, i, s);
00744     }
00745     result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
00746     Py_DECREF(l);
00747     return result;
00748 }
00749 
00750 /* Conversion AST -> Python */
00751 
00752 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
00753 {
00754     int i, n = asdl_seq_LEN(seq);
00755     PyObject *result = PyList_New(n);
00756     PyObject *value;
00757     if (!result)
00758         return NULL;
00759     for (i = 0; i < n; i++) {
00760         value = func(asdl_seq_GET(seq, i));
00761         if (!value) {
00762             Py_DECREF(result);
00763             return NULL;
00764         }
00765         PyList_SET_ITEM(result, i, value);
00766     }
00767     return result;
00768 }
00769 
00770 static PyObject* ast2obj_object(void *o)
00771 {
00772     if (!o)
00773         o = Py_None;
00774     Py_INCREF((PyObject*)o);
00775     return (PyObject*)o;
00776 }
00777 #define ast2obj_identifier ast2obj_object
00778 #define ast2obj_string ast2obj_object
00779 
00780 static PyObject* ast2obj_int(long b)
00781 {
00782     return PyLong_FromLong(b);
00783 }
00784 
00785 /* Conversion Python -> AST */
00786 
00787 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
00788 {
00789     if (obj == Py_None)
00790         obj = NULL;
00791     if (obj)
00792         PyArena_AddPyObject(arena, obj);
00793     Py_XINCREF(obj);
00794     *out = obj;
00795     return 0;
00796 }
00797 
00798 static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
00799 {
00800     if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
00801         PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
00802         return 1;
00803     }
00804     return obj2ast_object(obj, out, arena);
00805 }
00806 
00807 static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
00808 {
00809     if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
00810         PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
00811         return 1;
00812     }
00813     return obj2ast_object(obj, out, arena);
00814 }
00815 
00816 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
00817 {
00818     int i;
00819     if (!PyLong_Check(obj)) {
00820         PyObject *s = PyObject_Repr(obj);
00821         if (s == NULL) return 1;
00822         PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
00823                      PyBytes_AS_STRING(s));
00824         Py_DECREF(s);
00825         return 1;
00826     }
00827 
00828     i = (int)PyLong_AsLong(obj);
00829     if (i == -1 && PyErr_Occurred())
00830         return 1;
00831     *out = i;
00832     return 0;
00833 }
00834 
00835 static int add_ast_fields(void)
00836 {
00837     PyObject *empty_tuple, *d;
00838     if (PyType_Ready(&AST_type) < 0)
00839         return -1;
00840     d = AST_type.tp_dict;
00841     empty_tuple = PyTuple_New(0);
00842     if (!empty_tuple ||
00843         PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
00844         PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
00845         Py_XDECREF(empty_tuple);
00846         return -1;
00847     }
00848     Py_DECREF(empty_tuple);
00849     return 0;
00850 }
00851 
00852 """, 0, reflow=False)
00853 
00854         self.emit("static int init_types(void)",0)
00855         self.emit("{", 0)
00856         self.emit("static int initialized;", 1)
00857         self.emit("if (initialized) return 1;", 1)
00858         self.emit("if (add_ast_fields() < 0) return 0;", 1)
00859         for dfn in mod.dfns:
00860             self.visit(dfn)
00861         self.emit("initialized = 1;", 1)
00862         self.emit("return 1;", 1);
00863         self.emit("}", 0)

Here is the call graph for this function:

def asdl_c.PyTypesVisitor.visitProduct (   self,
  prod,
  name 
)

Reimplemented from asdl_c.PickleVisitor.

Definition at line 864 of file asdl_c.py.

00864 
00865     def visitProduct(self, prod, name):
00866         if prod.fields:
00867             fields = name.value+"_fields"
00868         else:
00869             fields = "NULL"
00870         self.emit('%s_type = make_type("%s", &AST_type, %s, %d);' %
00871                         (name, name, fields, len(prod.fields)), 1)
00872         self.emit("if (!%s_type) return 0;" % name, 1)

Here is the call graph for this function:

def asdl_c.PyTypesVisitor.visitSum (   self,
  sum,
  name 
)

Reimplemented from asdl_c.PickleVisitor.

Definition at line 873 of file asdl_c.py.

00873 
00874     def visitSum(self, sum, name):
00875         self.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' %
00876                   (name, name), 1)
00877         self.emit("if (!%s_type) return 0;" % name, 1)
00878         if sum.attributes:
00879             self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
00880                             (name, name, len(sum.attributes)), 1)
00881         else:
00882             self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1)
00883         simple = is_simple(sum)
00884         for t in sum.types:
00885             self.visitConstructor(t, name, simple)

Here is the call graph for this function:

def asdl_c.PickleVisitor.visitType (   self,
  type 
) [inherited]

Definition at line 338 of file asdl_c.py.

00338 
00339     def visitType(self, type):
00340         self.visit(type.value, type.name)

Here is the call graph for this function:


Member Data Documentation

Definition at line 316 of file asdl.py.

Definition at line 86 of file asdl_c.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

asdl.VisitorBase.skip [inherited]

Definition at line 317 of file asdl.py.


The documentation for this class was generated from the following file: