Commit 2491a3ee authored by Vitja Makarov's avatar Vitja Makarov

Fix locals()

parent afe04a7f
...@@ -1269,6 +1269,7 @@ class NameNode(AtomicExprNode): ...@@ -1269,6 +1269,7 @@ class NameNode(AtomicExprNode):
# type_entry Entry For extension type names, the original type entry # type_entry Entry For extension type names, the original type entry
# cf_is_null boolean Is uninitialized before this node # cf_is_null boolean Is uninitialized before this node
# cf_maybe_null boolean Maybe uninitialized before this node # cf_maybe_null boolean Maybe uninitialized before this node
# allow_null boolean Don't raise UnboundLocalError
is_name = True is_name = True
is_cython_module = False is_cython_module = False
...@@ -1279,6 +1280,7 @@ class NameNode(AtomicExprNode): ...@@ -1279,6 +1280,7 @@ class NameNode(AtomicExprNode):
type_entry = None type_entry = None
cf_maybe_null = True cf_maybe_null = True
cf_is_null = False cf_is_null = False
allow_null = False
def create_analysed_rvalue(pos, env, entry): def create_analysed_rvalue(pos, env, entry):
node = NameNode(pos) node = NameNode(pos)
...@@ -1556,7 +1558,7 @@ class NameNode(AtomicExprNode): ...@@ -1556,7 +1558,7 @@ class NameNode(AtomicExprNode):
elif entry.is_local: elif entry.is_local:
if entry.type.is_pyobject: if entry.type.is_pyobject:
if self.cf_maybe_null or self.cf_is_null: if (self.cf_maybe_null or self.cf_is_null) and not self.allow_null:
code.putln('if (%s == 0) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' % code.putln('if (%s == 0) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' %
(entry.cname, entry.name, code.error_goto(self.pos))) (entry.cname, entry.name, code.error_goto(self.pos)))
...@@ -4738,11 +4740,13 @@ class DictNode(ExprNode): ...@@ -4738,11 +4740,13 @@ class DictNode(ExprNode):
# Dictionary constructor. # Dictionary constructor.
# #
# key_value_pairs [DictItemNode] # key_value_pairs [DictItemNode]
# exclude_null_values [boolean] Do not add NULL values to dict
# #
# obj_conversion_errors [PyrexError] used internally # obj_conversion_errors [PyrexError] used internally
subexprs = ['key_value_pairs'] subexprs = ['key_value_pairs']
is_temp = 1 is_temp = 1
exclude_null_values = False
type = dict_type type = dict_type
obj_conversion_errors = [] obj_conversion_errors = []
...@@ -4830,11 +4834,15 @@ class DictNode(ExprNode): ...@@ -4830,11 +4834,15 @@ class DictNode(ExprNode):
for item in self.key_value_pairs: for item in self.key_value_pairs:
item.generate_evaluation_code(code) item.generate_evaluation_code(code)
if self.type.is_pyobject: if self.type.is_pyobject:
if self.exclude_null_values:
code.putln('if (%s) {' % item.value.py_result())
code.put_error_if_neg(self.pos, code.put_error_if_neg(self.pos,
"PyDict_SetItem(%s, %s, %s)" % ( "PyDict_SetItem(%s, %s, %s)" % (
self.result(), self.result(),
item.key.py_result(), item.key.py_result(),
item.value.py_result())) item.value.py_result()))
if self.exclude_null_values:
code.putln('}')
else: else:
code.putln("%s.%s = %s;" % ( code.putln("%s.%s = %s;" % (
self.result(), self.result(),
......
...@@ -2203,9 +2203,9 @@ class TransformBuiltinMethods(EnvTransform): ...@@ -2203,9 +2203,9 @@ class TransformBuiltinMethods(EnvTransform):
return node # nothing to do return node # nothing to do
items = [ ExprNodes.DictItemNode(pos, items = [ ExprNodes.DictItemNode(pos,
key=ExprNodes.StringNode(pos, value=var), key=ExprNodes.StringNode(pos, value=var),
value=ExprNodes.NameNode(pos, name=var)) value=ExprNodes.NameNode(pos, name=var, allow_null=True))
for var in lenv.entries ] for var in lenv.entries ]
return ExprNodes.DictNode(pos, key_value_pairs=items) return ExprNodes.DictNode(pos, key_value_pairs=items, exclude_null_values=True)
else: # dir() else: # dir()
if len(node.args) > 1: if len(node.args) > 1:
error(self.pos, "Builtin 'dir()' called with wrong number of args, expected 0-1, got %d" error(self.pos, "Builtin 'dir()' called with wrong number of args, expected 0-1, got %d"
......
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