Commit e95b1c58 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Remove the namedtuple workaround

Fix a couple misc issues along the way
parent 6af09380
...@@ -407,38 +407,6 @@ def namedtuple(typename, field_names, verbose=False, rename=False): ...@@ -407,38 +407,6 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
return result return result
# Pyston change: use this hacky / slow? version of namedtuple while we work
# on exec support
def namedtuple(name, fields):
if isinstance(fields, str):
fields = fields.split()
assert isinstance(fields, list)
for f in fields:
assert isinstance(f, str)
class NamedTuple(object):
def __init__(self, *args):
assert len(args) == len(fields)
for i in xrange(len(fields)):
setattr(self, fields[i], args[i])
def __getitem__(self, idx):
assert 0 <= idx < len(fields)
return getattr(self, fields[idx])
def __repr__(self):
s = name + "("
first = True
for f in fields:
if not first:
s += ", "
first = False
s += "%s=%r" % (f, getattr(self, f))
s += ")"
return s
return NamedTuple
######################################################################## ########################################################################
### Counter ### Counter
######################################################################## ########################################################################
......
...@@ -657,7 +657,7 @@ public: ...@@ -657,7 +657,7 @@ public:
} }
bool visit_exec(AST_Exec* node) override { bool visit_exec(AST_Exec* node) override {
if (node->locals == NULL) { if (node->globals == NULL) {
doBareExec(node); doBareExec(node);
} }
return false; return false;
......
...@@ -462,6 +462,10 @@ void AST_Exec::accept(ASTVisitor* v) { ...@@ -462,6 +462,10 @@ void AST_Exec::accept(ASTVisitor* v) {
if (body) if (body)
body->accept(v); body->accept(v);
if (globals)
globals->accept(v);
if (locals)
locals->accept(v);
} }
void AST_Exec::accept_stmt(StmtVisitor* v) { void AST_Exec::accept_stmt(StmtVisitor* v) {
......
...@@ -13,3 +13,9 @@ d = collections.defaultdict(lambda: []) ...@@ -13,3 +13,9 @@ d = collections.defaultdict(lambda: [])
print d[1] print d[1]
d[2].append(3) d[2].append(3)
print sorted(d.items()) print sorted(d.items())
NT = collections.namedtuple("NT", ["field1", "field2"])
print NT.__name__
n = NT(1, "hi")
print n.field1, n.field2, len(n), list(n), n[0], n[-1]
print n
...@@ -118,3 +118,10 @@ exec s ...@@ -118,3 +118,10 @@ exec s
a = 5 a = 5
exec """print eval('a')""" in {'a': 6}, {} exec """print eval('a')""" in {'a': 6}, {}
exec """exec 'print a' """ in {'a': 6}, {} exec """exec 'print a' """ in {'a': 6}, {}
# test ordering:
def show(obj, msg):
print msg
return obj
exec show("print 'in exec'", "body") in show(None, "globals"), show(None, "locals")
s = """
def f():
a = 1
exec "a = 2"
def g():
print a
g()
f()
"""
try:
exec s
except Exception as e:
print repr(e)
s = """
def f():
a = 1
d = {}
exec "a = 2" in {}, d
def g():
print a
g()
print d
f()
"""
try:
exec s
except Exception as e:
print repr(e)
# expected: fail
# - surprisingly, this is allowed? doesn't seem very different from exec_syntax_error.py
# We print 1, CPython and PyPy print 2
s = """
def f():
a = 1
exec "a = 2" in None
def g():
print a
g()
f()
"""
try:
exec s
except Exception as e:
print repr(e)
# expected: fail
# - needs sys._getframe
import collections
NT = collections.namedtuple("NT", ["field1", "field2"])
print NT
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment