Author: Ronan Lamy <[email protected]>
Branch:
Changeset: r70387:e504b1f999df
Date: 2014-04-01 23:40 +0100
http://bitbucket.org/pypy/pypy/changeset/e504b1f999df/
Log: merge branch ast-issue1673
diff --git a/pypy/doc/whatsnew-head.rst b/pypy/doc/whatsnew-head.rst
--- a/pypy/doc/whatsnew-head.rst
+++ b/pypy/doc/whatsnew-head.rst
@@ -130,3 +130,7 @@
.. branch: latest-improve-doc
Fix broken links in documentation
+
+.. branch: ast-issue1673
+fix ast classes __dict__ are always empty problem and fix the ast deepcopy
issue when
+there is missing field
\ No newline at end of file
diff --git a/pypy/interpreter/astcompiler/ast.py
b/pypy/interpreter/astcompiler/ast.py
--- a/pypy/interpreter/astcompiler/ast.py
+++ b/pypy/interpreter/astcompiler/ast.py
@@ -49,13 +49,19 @@
w_type = space.type(self)
w_fields = w_type.getdictvalue(space, "_fields")
for w_name in space.fixedview(w_fields):
- space.setitem(w_dict, w_name,
+ try:
+ space.setitem(w_dict, w_name,
space.getattr(self, w_name))
+ except OperationError:
+ pass
w_attrs = space.findattr(w_type, space.wrap("_attributes"))
if w_attrs:
for w_name in space.fixedview(w_attrs):
- space.setitem(w_dict, w_name,
+ try:
+ space.setitem(w_dict, w_name,
space.getattr(self, w_name))
+ except OperationError:
+ pass
return space.newtuple([space.type(self),
space.newtuple([]),
w_dict])
@@ -3011,7 +3017,8 @@
w_self.setdictvalue(space, 'lineno', w_new_value)
w_self.initialization_state &= ~1
return
- w_self.deldictvalue(space, 'lineno')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'lineno', w_new_value)
w_self.initialization_state |= 1
def stmt_del_lineno(space, w_self):
@@ -3038,7 +3045,8 @@
w_self.setdictvalue(space, 'col_offset', w_new_value)
w_self.initialization_state &= ~2
return
- w_self.deldictvalue(space, 'col_offset')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'col_offset', w_new_value)
w_self.initialization_state |= 2
def stmt_del_col_offset(space, w_self):
@@ -3074,7 +3082,8 @@
w_self.setdictvalue(space, 'name', w_new_value)
w_self.initialization_state &= ~4
return
- w_self.deldictvalue(space, 'name')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'name', w_new_value)
w_self.initialization_state |= 4
def FunctionDef_del_name(space, w_self):
@@ -3201,7 +3210,8 @@
w_self.setdictvalue(space, 'name', w_new_value)
w_self.initialization_state &= ~4
return
- w_self.deldictvalue(space, 'name')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'name', w_new_value)
w_self.initialization_state |= 4
def ClassDef_del_name(space, w_self):
@@ -3665,7 +3675,8 @@
w_self.setdictvalue(space, 'nl', w_new_value)
w_self.initialization_state &= ~16
return
- w_self.deldictvalue(space, 'nl')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'nl', w_new_value)
w_self.initialization_state |= 16
def Print_del_nl(space, w_self):
@@ -4571,7 +4582,8 @@
w_self.setdictvalue(space, 'module', w_new_value)
w_self.initialization_state &= ~4
return
- w_self.deldictvalue(space, 'module')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'module', w_new_value)
w_self.initialization_state |= 4
def ImportFrom_del_module(space, w_self):
@@ -4620,7 +4632,8 @@
w_self.setdictvalue(space, 'level', w_new_value)
w_self.initialization_state &= ~16
return
- w_self.deldictvalue(space, 'level')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'level', w_new_value)
w_self.initialization_state |= 16
def ImportFrom_del_level(space, w_self):
@@ -4938,7 +4951,8 @@
w_self.setdictvalue(space, 'lineno', w_new_value)
w_self.initialization_state &= ~1
return
- w_self.deldictvalue(space, 'lineno')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'lineno', w_new_value)
w_self.initialization_state |= 1
def expr_del_lineno(space, w_self):
@@ -4965,7 +4979,8 @@
w_self.setdictvalue(space, 'col_offset', w_new_value)
w_self.initialization_state &= ~2
return
- w_self.deldictvalue(space, 'col_offset')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'col_offset', w_new_value)
w_self.initialization_state |= 2
def expr_del_col_offset(space, w_self):
@@ -6292,7 +6307,8 @@
w_self.setdictvalue(space, 'n', w_new_value)
w_self.initialization_state &= ~4
return
- w_self.deldictvalue(space, 'n')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'n', w_new_value)
w_self.initialization_state |= 4
def Num_del_n(space, w_self):
@@ -6343,7 +6359,8 @@
w_self.setdictvalue(space, 's', w_new_value)
w_self.initialization_state &= ~4
return
- w_self.deldictvalue(space, 's')
+ # need to save the original object too
+ w_self.setdictvalue(space, 's', w_new_value)
w_self.initialization_state |= 4
def Str_del_s(space, w_self):
@@ -6423,7 +6440,8 @@
w_self.setdictvalue(space, 'attr', w_new_value)
w_self.initialization_state &= ~8
return
- w_self.deldictvalue(space, 'attr')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'attr', w_new_value)
w_self.initialization_state |= 8
def Attribute_del_attr(space, w_self):
@@ -6618,7 +6636,8 @@
w_self.setdictvalue(space, 'id', w_new_value)
w_self.initialization_state &= ~4
return
- w_self.deldictvalue(space, 'id')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'id', w_new_value)
w_self.initialization_state |= 4
def Name_del_id(space, w_self):
@@ -6853,7 +6872,8 @@
w_self.setdictvalue(space, 'value', w_new_value)
w_self.initialization_state &= ~4
return
- w_self.deldictvalue(space, 'value')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'value', w_new_value)
w_self.initialization_state |= 4
def Const_del_value(space, w_self):
@@ -7521,7 +7541,8 @@
w_self.setdictvalue(space, 'lineno', w_new_value)
w_self.initialization_state &= ~1
return
- w_self.deldictvalue(space, 'lineno')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'lineno', w_new_value)
w_self.initialization_state |= 1
def excepthandler_del_lineno(space, w_self):
@@ -7548,7 +7569,8 @@
w_self.setdictvalue(space, 'col_offset', w_new_value)
w_self.initialization_state &= ~2
return
- w_self.deldictvalue(space, 'col_offset')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'col_offset', w_new_value)
w_self.initialization_state |= 2
def excepthandler_del_col_offset(space, w_self):
@@ -7716,7 +7738,8 @@
w_self.setdictvalue(space, 'vararg', w_new_value)
w_self.initialization_state &= ~2
return
- w_self.deldictvalue(space, 'vararg')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'vararg', w_new_value)
w_self.initialization_state |= 2
def arguments_del_vararg(space, w_self):
@@ -7746,7 +7769,8 @@
w_self.setdictvalue(space, 'kwarg', w_new_value)
w_self.initialization_state &= ~4
return
- w_self.deldictvalue(space, 'kwarg')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'kwarg', w_new_value)
w_self.initialization_state |= 4
def arguments_del_kwarg(space, w_self):
@@ -7824,7 +7848,8 @@
w_self.setdictvalue(space, 'arg', w_new_value)
w_self.initialization_state &= ~1
return
- w_self.deldictvalue(space, 'arg')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'arg', w_new_value)
w_self.initialization_state |= 1
def keyword_del_arg(space, w_self):
@@ -7905,7 +7930,8 @@
w_self.setdictvalue(space, 'name', w_new_value)
w_self.initialization_state &= ~1
return
- w_self.deldictvalue(space, 'name')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'name', w_new_value)
w_self.initialization_state |= 1
def alias_del_name(space, w_self):
@@ -7935,7 +7961,8 @@
w_self.setdictvalue(space, 'asname', w_new_value)
w_self.initialization_state &= ~2
return
- w_self.deldictvalue(space, 'asname')
+ # need to save the original object too
+ w_self.setdictvalue(space, 'asname', w_new_value)
w_self.initialization_state |= 2
def alias_del_asname(space, w_self):
diff --git a/pypy/interpreter/astcompiler/tools/asdl_py.py
b/pypy/interpreter/astcompiler/tools/asdl_py.py
--- a/pypy/interpreter/astcompiler/tools/asdl_py.py
+++ b/pypy/interpreter/astcompiler/tools/asdl_py.py
@@ -459,6 +459,7 @@
self.emit("raise OperationError(space.w_TypeError, "
"space.w_None)", 3)
else:
+ save_original_object = True
level = 2
if field.opt and field.type.value != "int":
self.emit("if space.is_w(w_new_value, space.w_None):", 2)
@@ -596,13 +597,19 @@
w_type = space.type(self)
w_fields = w_type.getdictvalue(space, "_fields")
for w_name in space.fixedview(w_fields):
- space.setitem(w_dict, w_name,
+ try:
+ space.setitem(w_dict, w_name,
space.getattr(self, w_name))
+ except OperationError:
+ pass
w_attrs = space.findattr(w_type, space.wrap("_attributes"))
if w_attrs:
for w_name in space.fixedview(w_attrs):
- space.setitem(w_dict, w_name,
+ try:
+ space.setitem(w_dict, w_name,
space.getattr(self, w_name))
+ except OperationError:
+ pass
return space.newtuple([space.type(self),
space.newtuple([]),
w_dict])
diff --git a/pypy/module/_ast/test/test_ast.py
b/pypy/module/_ast/test/test_ast.py
--- a/pypy/module/_ast/test/test_ast.py
+++ b/pypy/module/_ast/test/test_ast.py
@@ -387,3 +387,40 @@
mod.body[0].body[0].handlers[0].lineno = 7
mod.body[0].body[0].handlers[1].lineno = 6
code = compile(mod, "<test>", "exec")
+
+ def test_dict_astNode(self):
+ import ast
+ num_node = ast.Num(n=2, lineno=2, col_offset=3)
+ dict_res = num_node.__dict__
+
+ assert dict_res == {'n':2, 'lineno':2, 'col_offset':3}
+
+ def test_issue1673_Num_notfullinit(self):
+ import ast
+ import copy
+ num_node = ast.Num(n=2,lineno=2)
+ assert num_node.n == 2
+ assert num_node.lineno == 2
+ num_node2 = copy.deepcopy(num_node)
+
+ def test_issue1673_Num_fullinit(self):
+ import ast
+ import copy
+ num_node = ast.Num(n=2,lineno=2,col_offset=3)
+ num_node2 = copy.deepcopy(num_node)
+ assert num_node.n == num_node2.n
+ assert num_node.lineno == num_node2.lineno
+ assert num_node.col_offset == num_node2.col_offset
+ dict_res = num_node2.__dict__
+ assert dict_res == {'n':2, 'lineno':2, 'col_offset':3}
+
+ def test_issue1673_Str(self):
+ import ast
+ import copy
+ str_node = ast.Str(n=2,lineno=2)
+ assert str_node.n == 2
+ assert str_node.lineno == 2
+ str_node2 = copy.deepcopy(str_node)
+ dict_res = str_node2.__dict__
+ assert dict_res == {'n':2, 'lineno':2}
+
\ No newline at end of file
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit