Commit 00a5ade4 authored by Stefan Behnel's avatar Stefan Behnel

simplify ExprNode.as_none_safe_node() with a sensible default exception type

parent 867ac47a
...@@ -643,7 +643,7 @@ class ExprNode(Node): ...@@ -643,7 +643,7 @@ class ExprNode(Node):
def as_cython_attribute(self): def as_cython_attribute(self):
return None return None
def as_none_safe_node(self, error, message): def as_none_safe_node(self, message, error="PyExc_TypeError"):
# Wraps the node in a NoneCheckNode if it is not known to be # Wraps the node in a NoneCheckNode if it is not known to be
# not-None (e.g. because it is a Python literal). # not-None (e.g. because it is a Python literal).
if self.may_be_none(): if self.may_be_none():
...@@ -5815,7 +5815,6 @@ class PrimaryCmpNode(ExprNode, CmpNode): ...@@ -5815,7 +5815,6 @@ class PrimaryCmpNode(ExprNode, CmpNode):
env.use_utility_code(char_in_bytes_utility_code) env.use_utility_code(char_in_bytes_utility_code)
if not isinstance(self.operand2, (UnicodeNode, BytesNode)): if not isinstance(self.operand2, (UnicodeNode, BytesNode)):
self.operand2 = self.operand2.as_none_safe_node( self.operand2 = self.operand2.as_none_safe_node(
"PyExc_TypeError",
"argument of type 'NoneType' is not iterable") "argument of type 'NoneType' is not iterable")
else: else:
common_type = py_object_type common_type = py_object_type
......
...@@ -173,7 +173,7 @@ class IterationTransform(Visitor.VisitorTransform): ...@@ -173,7 +173,7 @@ class IterationTransform(Visitor.VisitorTransform):
return node return node
unpack_temp_node = UtilNodes.LetRefNode( unpack_temp_node = UtilNodes.LetRefNode(
slice_node.as_none_safe_node("PyExc_TypeError", "'NoneType' is not iterable")) slice_node.as_none_safe_node("'NoneType' is not iterable"))
slice_base_node = ExprNodes.PythonCapiCallNode( slice_base_node = ExprNodes.PythonCapiCallNode(
slice_node.pos, unpack_func, unpack_func_type, slice_node.pos, unpack_func, unpack_func_type,
...@@ -1312,7 +1312,7 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -1312,7 +1312,7 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
return node return node
arg = pos_args[0] arg = pos_args[0]
if arg.type is Builtin.dict_type: if arg.type is Builtin.dict_type:
arg = arg.as_none_safe_node("PyExc_TypeError", "'NoneType' is not iterable") arg = arg.as_none_safe_node("'NoneType' is not iterable")
return ExprNodes.PythonCapiCallNode( return ExprNodes.PythonCapiCallNode(
node.pos, "PyDict_Copy", self.PyDict_Copy_func_type, node.pos, "PyDict_Copy", self.PyDict_Copy_func_type,
args = [arg], args = [arg],
...@@ -1336,7 +1336,7 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -1336,7 +1336,7 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
if not isinstance(list_arg, (ExprNodes.ComprehensionNode, if not isinstance(list_arg, (ExprNodes.ComprehensionNode,
ExprNodes.ListNode)): ExprNodes.ListNode)):
pos_args[0] = list_arg.as_none_safe_node( pos_args[0] = list_arg.as_none_safe_node(
"PyExc_TypeError", "'NoneType' object is not iterable") "'NoneType' object is not iterable")
return ExprNodes.PythonCapiCallNode( return ExprNodes.PythonCapiCallNode(
node.pos, "PyList_AsTuple", self.PyList_AsTuple_func_type, node.pos, "PyList_AsTuple", self.PyList_AsTuple_func_type,
...@@ -1497,7 +1497,7 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -1497,7 +1497,7 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
return node return node
if not arg.is_literal: if not arg.is_literal:
arg = arg.as_none_safe_node( arg = arg.as_none_safe_node(
"PyExc_TypeError", "object of type 'NoneType' has no len()") "object of type 'NoneType' has no len()")
new_node = ExprNodes.PythonCapiCallNode( new_node = ExprNodes.PythonCapiCallNode(
node.pos, cfunc_name, self.PyObject_Size_func_type, node.pos, cfunc_name, self.PyObject_Size_func_type,
args = [arg], args = [arg],
...@@ -1561,7 +1561,6 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -1561,7 +1561,6 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
if not type_arg.type_entry: if not type_arg.type_entry:
# arbitrary variable, needs a None check for safety # arbitrary variable, needs a None check for safety
type_arg = type_arg.as_none_safe_node( type_arg = type_arg.as_none_safe_node(
"PyExc_TypeError",
"object.__new__(X): X is not a type object (NoneType)") "object.__new__(X): X is not a type object (NoneType)")
return ExprNodes.PythonCapiCallNode( return ExprNodes.PythonCapiCallNode(
...@@ -2123,13 +2122,12 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -2123,13 +2122,12 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
self_arg = args[0] self_arg = args[0]
if is_unbound_method: if is_unbound_method:
self_arg = self_arg.as_none_safe_node( self_arg = self_arg.as_none_safe_node(
"PyExc_TypeError",
"descriptor '%s' requires a '%s' object but received a 'NoneType'" % ( "descriptor '%s' requires a '%s' object but received a 'NoneType'" % (
attr_name, node.function.obj.name)) attr_name, node.function.obj.name))
else: else:
self_arg = self_arg.as_none_safe_node( self_arg = self_arg.as_none_safe_node(
"PyExc_AttributeError", "'NoneType' object has no attribute '%s'" % attr_name,
"'NoneType' object has no attribute '%s'" % attr_name) error = "PyExc_AttributeError")
args[0] = self_arg args[0] = self_arg
return ExprNodes.PythonCapiCallNode( return ExprNodes.PythonCapiCallNode(
node.pos, name, func_type, node.pos, name, func_type,
......
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