entry = self.entry
if entry is None:
return # There was an error earlier
+
+ # is_pyglobal seems to be True for module level-globals only.
+ # We use this to access class->tp_dict if necessary.
if entry.is_pyglobal:
namespace = self.entry.namespace_cname
- if Options.intern_names:
- code.put_error_if_neg(self.pos,
- 'PyObject_SetAttr(%s, %s, %s)' % (
- namespace,
- entry.interned_cname,
- rhs.py_result()))
- else:
- code.put_error_if_neg(self.pos,
- 'PyObject_SetAttrString(%s, "%s", %s)' % (
- namespace,
- entry.name,
- rhs.py_result()))
- if debug_disposal_code:
- print "NameNode.generate_assignment_code:"
- print "...generating disposal code for", rhs
- rhs.generate_disposal_code(code)
+ if entry.is_member:
+ # if we entry is a member we have to cheat: SetAttr does not work
+ # on types, so we create a descriptor which is then added to tp_dict
+ if Options.intern_names:
+ code.put_error_if_neg(self.pos,
+ 'PyDict_SetItem(%s->tp_dict, %s, %s)' % (
+ namespace,
+ entry.interned_cname,
+ rhs.py_result()))
+ else:
+ code.put_error_if_neg(self.pos,
+ 'PyDict_SetItemString(%s->tp_dict, %s, %s)' % (
+ namespace,
+ entry.name,
+ rhs.py_result()))
+
+ else:
+ if Options.intern_names:
+ code.put_error_if_neg(self.pos,
+ 'PyObject_SetAttr(%s, %s, %s)' % (
+ namespace,
+ entry.interned_cname,
+ rhs.py_result()))
+ else:
+ code.put_error_if_neg(self.pos,
+ 'PyObject_SetAttrString(%s, "%s", %s)' % (
+ namespace,
+ entry.name,
+ rhs.py_result()))
+ if debug_disposal_code:
+ print "NameNode.generate_assignment_code:"
+ print "...generating disposal code for", rhs
+ rhs.generate_disposal_code(code)
+
else:
if self.type.is_pyobject:
#print "NameNode.generate_assignment_code: to", self.name ###
# or class attribute during
# class construction
# is_special boolean Is a special class method
+ # is_member boolean Is an assigned class member
# is_variable boolean Is a variable
# is_cfunction boolean Is a C function
# is_cmethod boolean Is a C method of an extension type
is_cglobal = 0
is_pyglobal = 0
is_special = 0
+ is_member = 0
is_variable = 0
is_cfunction = 0
is_cmethod = 0
return self.outer_scope.add_string_const(value)
def lookup(self, name):
- print "*** Looking for", name, "in ClassScope", self
- print self.entries
res = Scope.lookup(self, name)
- print "got", res, res.type
return res
# Add an entry for a class attribute.
entry = Scope.declare_var(self, name, type, pos,
cname, visibility, is_cdef)
- entry.is_pyglobal = 1
+ entry.is_member = 1
+ entry.is_pyglobal = 1 # xxx: is_pyglobal changes behaviour in so many places that
+ # I keep it in for now. is_member should be enough
+ # later on
entry.namespace_cname = "(PyObject *)%s" % self.parent_type.typeptr_cname
if Options.intern_names:
entry.interned_cname = self.intern(name)