can't use .format() on jython :(

--HG--
branch : trunk
This commit is contained in:
Benjamin Peterson 2009-10-27 16:45:51 -05:00
parent 92cab2bae7
commit 1f01fafec7
1 changed files with 38 additions and 38 deletions

View File

@ -66,9 +66,9 @@ def getfailure(failure):
lines = explanation.splitlines() lines = explanation.splitlines()
if not lines: if not lines:
lines.append("") lines.append("")
lines[0] += " << {0}".format(value) lines[0] += " << %s" % (value,)
explanation = "\n".join(lines) explanation = "\n".join(lines)
text = "{0}: {1}".format(failure.cause[0].__name__, explanation) text = "%s: %s" % (failure.cause[0].__name__, explanation)
if text.startswith("AssertionError: assert "): if text.startswith("AssertionError: assert "):
text = text[16:] text = text[16:]
return text return text
@ -99,10 +99,10 @@ operator_map = {
} }
unary_map = { unary_map = {
ast.Not : "not {0}", ast.Not : "not %s",
ast.Invert : "~{0}", ast.Invert : "~%s",
ast.USub : "-{0}", ast.USub : "-%s",
ast.UAdd : "+{0}" ast.UAdd : "+%s"
} }
@ -147,7 +147,7 @@ class DebugInterpreter(ast.NodeVisitor):
def visit_Name(self, name): def visit_Name(self, name):
explanation, result = self.generic_visit(name) explanation, result = self.generic_visit(name)
# See if the name is local. # See if the name is local.
source = "{0!r} in locals() is not globals()".format(name.id) source = "%r in locals() is not globals()" % (name.id,)
co = self._compile(source) co = self._compile(source)
try: try:
local = self.frame.eval(co) local = self.frame.eval(co)
@ -167,9 +167,9 @@ class DebugInterpreter(ast.NodeVisitor):
break break
next_explanation, next_result = self.visit(next_op) next_explanation, next_result = self.visit(next_op)
op_symbol = operator_map[op.__class__] op_symbol = operator_map[op.__class__]
explanation = "{0} {1} {2}".format(left_explanation, op_symbol, explanation = "%s %s %s" % (left_explanation, op_symbol,
next_explanation) next_explanation)
source = "__exprinfo_left {0} __exprinfo_right".format(op_symbol) source = "__exprinfo_left %s __exprinfo_right" % (op_symbol,)
co = self._compile(source) co = self._compile(source)
try: try:
result = self.frame.eval(co, __exprinfo_left=left_result, result = self.frame.eval(co, __exprinfo_left=left_result,
@ -196,8 +196,8 @@ class DebugInterpreter(ast.NodeVisitor):
def visit_UnaryOp(self, unary): def visit_UnaryOp(self, unary):
pattern = unary_map[unary.op.__class__] pattern = unary_map[unary.op.__class__]
operand_explanation, operand_result = self.visit(unary.operand) operand_explanation, operand_result = self.visit(unary.operand)
explanation = pattern.format(operand_explanation) explanation = pattern % (operand_explanation,)
co = self._compile(pattern.format("__exprinfo_expr")) co = self._compile(pattern % ("__exprinfo_expr",))
try: try:
result = self.frame.eval(co, __exprinfo_expr=operand_result) result = self.frame.eval(co, __exprinfo_expr=operand_result)
except Exception: except Exception:
@ -208,9 +208,9 @@ class DebugInterpreter(ast.NodeVisitor):
left_explanation, left_result = self.visit(binop.left) left_explanation, left_result = self.visit(binop.left)
right_explanation, right_result = self.visit(binop.right) right_explanation, right_result = self.visit(binop.right)
symbol = operator_map[binop.op.__class__] symbol = operator_map[binop.op.__class__]
explanation = "({0} {1} {2})".format(left_explanation, symbol, explanation = "(%s %s %s)" % (left_explanation, symbol,
right_explanation) right_explanation)
source = "__exprinfo_left {0} __exprinfo_right".format(symbol) source = "__exprinfo_left %s __exprinfo_right" % (symbol,)
co = self._compile(source) co = self._compile(source)
try: try:
result = self.frame.eval(co, __exprinfo_left=left_result, result = self.frame.eval(co, __exprinfo_left=left_result,
@ -226,33 +226,33 @@ class DebugInterpreter(ast.NodeVisitor):
arguments = [] arguments = []
for arg in call.args: for arg in call.args:
arg_explanation, arg_result = self.visit(arg) arg_explanation, arg_result = self.visit(arg)
arg_name = "__exprinfo_{0}".format(len(ns)) arg_name = "__exprinfo_%s" % (len(ns),)
ns[arg_name] = arg_result ns[arg_name] = arg_result
arguments.append(arg_name) arguments.append(arg_name)
arg_explanations.append(arg_explanation) arg_explanations.append(arg_explanation)
for keyword in call.keywords: for keyword in call.keywords:
arg_explanation, arg_result = self.visit(keyword.value) arg_explanation, arg_result = self.visit(keyword.value)
arg_name = "__exprinfo_{0}".format(len(ns)) arg_name = "__exprinfo_%s" % (len(ns),)
ns[arg_name] = arg_result ns[arg_name] = arg_result
keyword_source = "{0}={{0}}".format(keyword.id) keyword_source = "%s=%%s" % (keyword.id)
arguments.append(keyword_source.format(arg_name)) arguments.append(keyword_source % (arg_name,))
arg_explanations.append(keyword_source.format(arg_explanation)) arg_explanations.append(keyword_source % (arg_explanation,))
if call.starargs: if call.starargs:
arg_explanation, arg_result = self.visit(call.starargs) arg_explanation, arg_result = self.visit(call.starargs)
arg_name = "__exprinfo_star" arg_name = "__exprinfo_star"
ns[arg_name] = arg_result ns[arg_name] = arg_result
arguments.append("*{0}".format(arg_name)) arguments.append("*%s" % (arg_name,))
arg_explanations.append("*{0}".format(arg_explanation)) arg_explanations.append("*%s" % (arg_explanation,))
if call.kwargs: if call.kwargs:
arg_explanation, arg_result = self.visit(call.kwargs) arg_explanation, arg_result = self.visit(call.kwargs)
arg_name = "__exprinfo_kwds" arg_name = "__exprinfo_kwds"
ns[arg_name] = arg_result ns[arg_name] = arg_result
arguments.append("**{0}".format(arg_name)) arguments.append("**%s" % (arg_name,))
arg_explanations.append("**{0}".format(arg_explanation)) arg_explanations.append("**%s" % (arg_explanation,))
args_explained = ", ".join(arg_explanations) args_explained = ", ".join(arg_explanations)
explanation = "{0}({1})".format(func_explanation, args_explained) explanation = "%s(%s)" % (func_explanation, args_explained)
args = ", ".join(arguments) args = ", ".join(arguments)
source = "__exprinfo_func({0})".format(args) source = "__exprinfo_func(%s)" % (args,)
co = self._compile(source) co = self._compile(source)
try: try:
result = self.frame.eval(co, **ns) result = self.frame.eval(co, **ns)
@ -269,14 +269,14 @@ class DebugInterpreter(ast.NodeVisitor):
except Exception: except Exception:
is_bool = False is_bool = False
if not is_bool: if not is_bool:
pattern = "{0}\n{{{0} = {1}\n}}" pattern = "%s\n{%s = %s\n}"
rep = self.frame.repr(result) rep = self.frame.repr(result)
explanation = pattern.format(rep, explanation) explanation = pattern % (rep, rep, explanation)
return explanation, result return explanation, result
def _is_builtin_name(self, name): def _is_builtin_name(self, name):
pattern = "{0!r} not in globals() and {0!r} not in locals()" pattern = "%r not in globals() and %r not in locals()"
source = pattern.format(name.id) source = pattern % (name.id, name.id)
co = self._compile(source) co = self._compile(source)
try: try:
return self.frame.eval(co) return self.frame.eval(co)
@ -287,16 +287,16 @@ class DebugInterpreter(ast.NodeVisitor):
if not isinstance(attr.ctx, ast.Load): if not isinstance(attr.ctx, ast.Load):
return self.generic_visit(attr) return self.generic_visit(attr)
source_explanation, source_result = self.visit(attr.value) source_explanation, source_result = self.visit(attr.value)
explanation = "{0}.{1}".format(source_explanation, attr.attr) explanation = "%s.%s" % (source_explanation, attr.attr)
source = "__exprinfo_expr.{0}".format(attr.attr) source = "__exprinfo_expr.%s" % (attr.attr,)
co = self._compile(source) co = self._compile(source)
try: try:
result = self.frame.eval(co, __exprinfo_expr=source_result) result = self.frame.eval(co, __exprinfo_expr=source_result)
except Exception: except Exception:
raise Failure(explanation) raise Failure(explanation)
# Check if the attr is from an instance. # Check if the attr is from an instance.
source = "{0!r} in getattr(__exprinfo_expr, '__dict__', {{}})" source = "%r in getattr(__exprinfo_expr, '__dict__', {})"
source = source.format(attr.attr) source = source % (attr.attr,)
co = self._compile(source) co = self._compile(source)
try: try:
from_instance = self.frame.eval(co, __exprinfo_expr=source_result) from_instance = self.frame.eval(co, __exprinfo_expr=source_result)
@ -304,8 +304,8 @@ class DebugInterpreter(ast.NodeVisitor):
from_instance = True from_instance = True
if from_instance: if from_instance:
rep = self.frame.repr(result) rep = self.frame.repr(result)
pattern = "{0}\n{{{0} = {1}\n}}" pattern = "%s\n{%s = %s\n}"
explanation = pattern.format(rep, explanation) explanation = pattern % (rep, rep, explanation)
return explanation, result return explanation, result
def visit_Assert(self, assrt): def visit_Assert(self, assrt):
@ -313,7 +313,7 @@ class DebugInterpreter(ast.NodeVisitor):
if test_explanation.startswith("False\n{False =") and \ if test_explanation.startswith("False\n{False =") and \
test_explanation.endswith("\n"): test_explanation.endswith("\n"):
test_explanation = test_explanation[15:-2] test_explanation = test_explanation[15:-2]
explanation = "assert {0}".format(test_explanation) explanation = "assert %s" % (test_explanation,)
if not test_result: if not test_result:
try: try:
raise BuiltinAssertionError raise BuiltinAssertionError
@ -323,7 +323,7 @@ class DebugInterpreter(ast.NodeVisitor):
def visit_Assign(self, assign): def visit_Assign(self, assign):
value_explanation, value_result = self.visit(assign.value) value_explanation, value_result = self.visit(assign.value)
explanation = "... = {0}".format(value_explanation) explanation = "... = %s" % (value_explanation,)
name = ast.Name("__exprinfo_expr", ast.Load(), assign.value.lineno, name = ast.Name("__exprinfo_expr", ast.Load(), assign.value.lineno,
assign.value.col_offset) assign.value.col_offset)
new_assign = ast.Assign(assign.targets, name, assign.lineno, new_assign = ast.Assign(assign.targets, name, assign.lineno,