Back to index

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

List of all members.

Public Member Functions

def funcHeader
def sumTrailer
def simpleSum
def buildArgs
def complexSum
def visitAttributeDeclaration
def visitSum
def visitProduct
def visitFieldDeclaration
def isSimpleSum
def isNumeric
def isSimpleType
def visitField
def visitModule
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 362 of file asdl_c.py.


Member Function Documentation

def asdl_c.Obj2ModVisitor.buildArgs (   self,
  fields 
)

Definition at line 399 of file asdl_c.py.

00399 
00400     def buildArgs(self, fields):
00401         return ", ".join(fields + ["arena"])

Here is the caller graph for this function:

def asdl_c.Obj2ModVisitor.complexSum (   self,
  sum,
  name 
)

Definition at line 402 of file asdl_c.py.

00402 
00403     def complexSum(self, sum, name):
00404         self.funcHeader(name)
00405         self.emit("PyObject *tmp = NULL;", 1)
00406         for a in sum.attributes:
00407             self.visitAttributeDeclaration(a, name, sum=sum)
00408         self.emit("", 0)
00409         # XXX: should we only do this for 'expr'?
00410         self.emit("if (obj == Py_None) {", 1)
00411         self.emit("*out = NULL;", 2)
00412         self.emit("return 0;", 2)
00413         self.emit("}", 1)
00414         for a in sum.attributes:
00415             self.visitField(a, name, sum=sum, depth=1)
00416         for t in sum.types:
00417             line = "isinstance = PyObject_IsInstance(obj, (PyObject*)%s_type);"
00418             self.emit(line % (t.name,), 1)
00419             self.emit("if (isinstance == -1) {", 1)
00420             self.emit("return 1;", 2)
00421             self.emit("}", 1)
00422             self.emit("if (isinstance) {", 1)
00423             for f in t.fields:
00424                 self.visitFieldDeclaration(f, t.name, sum=sum, depth=2)
00425             self.emit("", 0)
00426             for f in t.fields:
00427                 self.visitField(f, t.name, sum=sum, depth=2)
00428             args = [f.name.value for f in t.fields] + [a.name.value for a in sum.attributes]
00429             self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2)
00430             self.emit("if (*out == NULL) goto failed;", 2)
00431             self.emit("return 0;", 2)
00432             self.emit("}", 1)
00433         self.sumTrailer(name, True)

Here is the call graph for this function:

Here is the caller graph for this function:

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_c.Obj2ModVisitor.funcHeader (   self,
  name 
)

Definition at line 363 of file asdl_c.py.

00363 
00364     def funcHeader(self, name):
00365         ctype = get_c_type(name)
00366         self.emit("int", 0)
00367         self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
00368         self.emit("{", 0)
00369         self.emit("int isinstance;", 1)
00370         self.emit("", 0)

Here is the call graph for this function:

Here is the caller graph for this function:

def asdl_c.Obj2ModVisitor.isNumeric (   self,
  field 
)

Definition at line 480 of file asdl_c.py.

00480 
00481     def isNumeric(self, field):
00482         return get_c_type(field.type) in ("int", "bool")

Here is the call graph for this function:

Here is the caller graph for this function:

def asdl_c.Obj2ModVisitor.isSimpleSum (   self,
  field 
)

Definition at line 475 of file asdl_c.py.

00475 
00476     def isSimpleSum(self, field):
00477         # XXX can the members of this list be determined automatically?
00478         return field.type.value in ('expr_context', 'boolop', 'operator',
00479                                     'unaryop', 'cmpop')

Here is the caller graph for this function:

def asdl_c.Obj2ModVisitor.isSimpleType (   self,
  field 
)

Definition at line 483 of file asdl_c.py.

00483 
00484     def isSimpleType(self, field):
00485         return self.isSimpleSum(field) or self.isNumeric(field)

Here is the call graph for this function:

Here is the caller graph for this function:

def asdl_c.Obj2ModVisitor.simpleSum (   self,
  sum,
  name 
)

Definition at line 384 of file asdl_c.py.

00384 
00385     def simpleSum(self, sum, name):
00386         self.funcHeader(name)
00387         for t in sum.types:
00388             line = ("isinstance = PyObject_IsInstance(obj, "
00389                     "(PyObject *)%s_type);")
00390             self.emit(line % (t.name,), 1)
00391             self.emit("if (isinstance == -1) {", 1)
00392             self.emit("return 1;", 2)
00393             self.emit("}", 1)
00394             self.emit("if (isinstance) {", 1)
00395             self.emit("*out = %s;" % t.name, 2)
00396             self.emit("return 0;", 2)
00397             self.emit("}", 1)
00398         self.sumTrailer(name)

Here is the call graph for this function:

Here is the caller graph for this function:

def asdl_c.Obj2ModVisitor.sumTrailer (   self,
  name,
  add_label = False 
)

Definition at line 371 of file asdl_c.py.

00371 
00372     def sumTrailer(self, name, add_label=False):
00373         self.emit("", 0)
00374         # there's really nothing more we can do if this fails ...
00375         error = "expected some sort of %s, but got %%R" % name
00376         format = "PyErr_Format(PyExc_TypeError, \"%s\", obj);"
00377         self.emit(format % error, 1, reflow=False)
00378         if add_label:
00379             self.emit("failed:", 1)
00380             self.emit("Py_XDECREF(tmp);", 1)
00381         self.emit("return 1;", 1)
00382         self.emit("}", 0)
00383         self.emit("", 0)

Here is the call graph for this function:

Here is the caller 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.Obj2ModVisitor.visitAttributeDeclaration (   self,
  a,
  name,
  sum = sum 
)

Definition at line 434 of file asdl_c.py.

00434 
00435     def visitAttributeDeclaration(self, a, name, sum=sum):
00436         ctype = get_c_type(a.type)
00437         self.emit("%s %s;" % (ctype, a.name), 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.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.Obj2ModVisitor.visitField (   self,
  field,
  name,
  sum = None,
  prod = None,
  depth = 0 
)

Definition at line 486 of file asdl_c.py.

00486 
00487     def visitField(self, field, name, sum=None, prod=None, depth=0):
00488         ctype = get_c_type(field.type)
00489         self.emit("if (PyObject_HasAttrString(obj, \"%s\")) {" % field.name, depth)
00490         self.emit("int res;", depth+1)
00491         if field.seq:
00492             self.emit("Py_ssize_t len;", depth+1)
00493             self.emit("Py_ssize_t i;", depth+1)
00494         self.emit("tmp = PyObject_GetAttrString(obj, \"%s\");" % field.name, depth+1)
00495         self.emit("if (tmp == NULL) goto failed;", depth+1)
00496         if field.seq:
00497             self.emit("if (!PyList_Check(tmp)) {", depth+1)
00498             self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
00499                       "be a list, not a %%.200s\", tmp->ob_type->tp_name);" %
00500                       (name, field.name),
00501                       depth+2, reflow=False)
00502             self.emit("goto failed;", depth+2)
00503             self.emit("}", depth+1)
00504             self.emit("len = PyList_GET_SIZE(tmp);", depth+1)
00505             if self.isSimpleType(field):
00506                 self.emit("%s = asdl_int_seq_new(len, arena);" % field.name, depth+1)
00507             else:
00508                 self.emit("%s = asdl_seq_new(len, arena);" % field.name, depth+1)
00509             self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
00510             self.emit("for (i = 0; i < len; i++) {", depth+1)
00511             self.emit("%s value;" % ctype, depth+2)
00512             self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &value, arena);" %
00513                       field.type, depth+2, reflow=False)
00514             self.emit("if (res != 0) goto failed;", depth+2)
00515             self.emit("asdl_seq_SET(%s, i, value);" % field.name, depth+2)
00516             self.emit("}", depth+1)
00517         else:
00518             self.emit("res = obj2ast_%s(tmp, &%s, arena);" %
00519                       (field.type, field.name), depth+1)
00520             self.emit("if (res != 0) goto failed;", depth+1)
00521 
00522         self.emit("Py_XDECREF(tmp);", depth+1)
00523         self.emit("tmp = NULL;", depth+1)
00524         self.emit("} else {", depth)
00525         if not field.opt:
00526             message = "required field \\\"%s\\\" missing from %s" % (field.name, name)
00527             format = "PyErr_SetString(PyExc_TypeError, \"%s\");"
00528             self.emit(format % message, depth+1, reflow=False)
00529             self.emit("return 1;", depth+1)
00530         else:
00531             if self.isNumeric(field):
00532                 self.emit("%s = 0;" % field.name, depth+1)
00533             elif not self.isSimpleType(field):
00534                 self.emit("%s = NULL;" % field.name, depth+1)
00535             else:
00536                 raise TypeError("could not determine the default value for %s" % field.name)
00537         self.emit("}", depth)
00538 

Here is the call graph for this function:

def asdl_c.Obj2ModVisitor.visitFieldDeclaration (   self,
  field,
  name,
  sum = None,
  prod = None,
  depth = 0 
)

Definition at line 464 of file asdl_c.py.

00464 
00465     def visitFieldDeclaration(self, field, name, sum=None, prod=None, depth=0):
00466         ctype = get_c_type(field.type)
00467         if field.seq:
00468             if self.isSimpleType(field):
00469                 self.emit("asdl_int_seq* %s;" % field.name, depth)
00470             else:
00471                 self.emit("asdl_seq* %s;" % field.name, depth)
00472         else:
00473             ctype = get_c_type(field.type)
00474             self.emit("%s %s;" % (ctype, field.name), depth)

Here is the call graph for this function:

Here is the caller graph for this function:

def asdl_c.PickleVisitor.visitModule (   self,
  mod 
) [inherited]

Reimplemented in asdl_c.ASTModuleVisitor, and asdl_c.PyTypesVisitor.

Definition at line 334 of file asdl_c.py.

00334 
00335     def visitModule(self, mod):
00336         for dfn in mod.dfns:
00337             self.visit(dfn)

Here is the call graph for this function:

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

Reimplemented from asdl_c.PickleVisitor.

Definition at line 444 of file asdl_c.py.

00444 
00445     def visitProduct(self, prod, name):
00446         ctype = get_c_type(name)
00447         self.emit("int", 0)
00448         self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
00449         self.emit("{", 0)
00450         self.emit("PyObject* tmp = NULL;", 1)
00451         for f in prod.fields:
00452             self.visitFieldDeclaration(f, name, prod=prod, depth=1)
00453         self.emit("", 0)
00454         for f in prod.fields:
00455             self.visitField(f, name, prod=prod, depth=1)
00456         args = [f.name.value for f in prod.fields]
00457         self.emit("*out = %s(%s);" % (name, self.buildArgs(args)), 1)
00458         self.emit("return 0;", 1)
00459         self.emit("failed:", 0)
00460         self.emit("Py_XDECREF(tmp);", 1)
00461         self.emit("return 1;", 1)
00462         self.emit("}", 0)
00463         self.emit("", 0)

Here is the call graph for this function:

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

Reimplemented from asdl_c.PickleVisitor.

Definition at line 438 of file asdl_c.py.

00438 
00439     def visitSum(self, sum, name):
00440         if is_simple(sum):
00441             self.simpleSum(sum, name)
00442         else:
00443             self.complexSum(sum, name)

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: