1.1 --- a/annotate.py Wed Feb 14 21:57:46 2007 +0100
1.2 +++ b/annotate.py Wed Feb 14 23:35:19 2007 +0100
1.3 @@ -131,7 +131,7 @@
1.4 types possible when the means of constructing the namespace may depend on
1.5 run-time behaviour.
1.6
1.7 - Covered: Assign, CheckExc, Conditional, Global, Import, InvokeBlock,
1.8 + Covered: Assign, CheckType, Conditional, Global, Import, InvokeBlock,
1.9 InvokeFunction, LoadAttr, LoadExc, LoadName, LoadRef, LoadTemp,
1.10 Module, Not, Pass, Raise, ReleaseTemp, ReturnFromBlock,
1.11 ReturnFromFunction, StoreAttr, StoreName, StoreTemp, Subprogram,
1.12 @@ -286,25 +286,27 @@
1.13 assign.code = self.dispatches(assign.code)
1.14 return assign
1.15
1.16 - def visitCheckExc(self, checkexc):
1.17 + def visitCheckType(self, checktype):
1.18
1.19 """
1.20 - Return the 'checkexc' node, processing the expression to find the
1.21 + Return the 'checktype' node, processing the expression to find the
1.22 possible types of the exception, and processing each choice to build a
1.23 list of checked types for the exception.
1.24 """
1.25
1.26 - checkexc.expr = self.dispatch(checkexc.expr)
1.27 + inverted = getattr(checktype, "inverted", 0)
1.28 + checktype.expr = self.dispatch(checktype.expr)
1.29 expr_types = self.namespace.types
1.30 choice_types = []
1.31 choices = []
1.32 - for choice in checkexc.choices:
1.33 + for choice in checktype.choices:
1.34 choices.append(self.dispatch(choice))
1.35 choice_types += self.namespace.types
1.36 for expr_type in expr_types:
1.37 - if expr_type.type.get_class() not in choice_types:
1.38 - self._prune_non_accesses(checkexc.expr, expr_type)
1.39 - return checkexc
1.40 + in_choices = expr_type.type.get_class() in choice_types
1.41 + if not inverted and not in_choices or inverted and in_choices:
1.42 + self._prune_non_accesses(checktype.expr, expr_type)
1.43 + return checktype
1.44
1.45 def visitConditional(self, conditional):
1.46
3.1 --- a/simplify.py Wed Feb 14 21:57:46 2007 +0100
3.2 +++ b/simplify.py Wed Feb 14 23:35:19 2007 +0100
3.3 @@ -546,7 +546,7 @@
3.4 method_names = self.comparison_methods[op_name]
3.5 if method_names:
3.6 first_name, alternative_name = method_names
3.7 - invocation = self._visitBinaryOp(new_op, previous, expr, first_name, alternative_name)
3.8 + invocation = self._visitBinaryCompareOp(new_op, previous, expr, first_name, alternative_name)
3.9
3.10 elif op_name == "is":
3.11 invocation = InvokeFunction(
3.12 @@ -1301,7 +1301,7 @@
3.13 else:
3.14 test = Conditional(
3.15 isolate_test=1,
3.16 - test=CheckExc(expr=LoadExc(), choices=self._visitTryExcept(spec))
3.17 + test=CheckType(expr=LoadExc(), choices=self._visitTryExcept(spec))
3.18 )
3.19 test.body = []
3.20
3.21 @@ -1430,6 +1430,95 @@
3.22 def _visitBinary(self, binary, left_name, right_name):
3.23 return self._visitBinaryOp(binary, self.dispatch(binary.left), self.dispatch(binary.right), left_name, right_name)
3.24
3.25 + def _visitBinaryCompareOp(self, binary, left, right, left_name, right_name):
3.26 +
3.27 + """
3.28 + Emulate the current mechanisms by producing nodes as follows:
3.29 +
3.30 + InvokeBlock -> Subprogram -> StoreTemp (expr) -> x.__lt__(y)
3.31 + Conditional (test) -> __is__(LoadTemp, NotImplemented)
3.32 + (body) -> ReleaseTemp
3.33 + StoreTemp (expr) -> y.__gt__(x)
3.34 + Conditional (test) -> __is__(LoadTemp, NotImplemented)
3.35 + (body) -> ReturnFromBlock (expr) -> False
3.36 + (else) -> ReturnFromBlock (expr) -> LoadTemp
3.37 + (else) -> ReturnFromBlock (expr) -> LoadTemp
3.38 + """
3.39 +
3.40 + subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None)
3.41 + self.current_subprograms.append(subprogram)
3.42 +
3.43 + subprogram.code = [
3.44 + StoreTemp(
3.45 + expr=InvokeFunction(
3.46 + binary,
3.47 + expr=LoadAttr(expr=left, name=left_name),
3.48 + args=[right],
3.49 + star=None,
3.50 + dstar=None)
3.51 + ),
3.52 + Conditional(
3.53 + isolate_test=1,
3.54 + test=CheckType(
3.55 + expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")]
3.56 + ),
3.57 + body=[
3.58 + ReleaseTemp(),
3.59 + StoreTemp(
3.60 + expr=InvokeFunction(
3.61 + binary,
3.62 + expr=LoadAttr(expr=right, name=right_name),
3.63 + args=[left],
3.64 + star=None,
3.65 + dstar=None)
3.66 + ),
3.67 + Conditional(
3.68 + isolate_test=1,
3.69 + test=CheckType(
3.70 + expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")]
3.71 + ),
3.72 + body=[
3.73 + ReturnFromBlock(
3.74 + expr=LoadName(name="False")
3.75 + )
3.76 + ],
3.77 + else_=[
3.78 + CheckType(
3.79 + inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")]
3.80 + ),
3.81 + ReturnFromBlock(
3.82 + expr=LoadTemp()
3.83 + )
3.84 + ]
3.85 + )
3.86 + ],
3.87 + else_=[
3.88 + CheckType(
3.89 + inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")]
3.90 + ),
3.91 + ReturnFromBlock(
3.92 + expr=LoadTemp()
3.93 + )
3.94 + ]
3.95 + )
3.96 + ]
3.97 +
3.98 + self.current_subprograms.pop()
3.99 + self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram
3.100 +
3.101 + result = InvokeBlock(
3.102 + binary,
3.103 + produces_result=1,
3.104 + expr=LoadRef(ref=subprogram)
3.105 + )
3.106 +
3.107 + # Make nice annotations for the viewer.
3.108 +
3.109 + binary._left_call = subprogram.code[0].expr
3.110 + binary._right_call = subprogram.code[1].body[1].expr
3.111 +
3.112 + return result
3.113 +
3.114 def _visitBinaryOp(self, binary, left, right, left_name, right_name):
3.115
3.116 """
3.117 @@ -1437,7 +1526,7 @@
3.118
3.119 InvokeBlock -> Subprogram -> Try (body) -> ReturnFromBlock (expr) -> x.__add__(y)
3.120 (else)
3.121 - (handler) -> Conditional (test) -> CheckExc (expr) -> LoadExc
3.122 + (handler) -> Conditional (test) -> CheckType (expr) -> LoadExc
3.123 (choices) -> LoadName TypeError
3.124 (body) -> ReturnFromBlock (expr) -> y.__radd__(x)
3.125 (else)
3.126 @@ -1462,7 +1551,7 @@
3.127 finally_=[],
3.128 handler=[
3.129 Conditional(
3.130 - test=CheckExc(expr=LoadExc(), choices=[LoadName(name="TypeError")]),
3.131 + test=CheckType(expr=LoadExc(), choices=[LoadName(name="TypeError")]),
3.132 body=[
3.133 ReturnFromBlock(
3.134 expr=InvokeFunction(