Commit b6146eb9 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

division_trace: don't use inspect to collect information

Let's pass the data as function parameters. This greatly simplifies when
using monkey-patching to apply fixers on code that is compiled, for
example. Otherwise, we need to also monkey-patch the code that compile
that code (to change the name).
parent f65e0858
# https://lab.nexedi.com/nexedi/erp5/snippets/475
from collections import defaultdict
import inspect
from lib2to3.fixer_base import BaseFix
import lib2to3.fixer_util
import lib2to3.pgen2
......@@ -11,21 +10,17 @@ from my2to3.trace import create_table, register_tracing_function
from my2to3.util import parse_type
# id is used to differentiate the divisions of the same line.
trace = create_table("division", "filename", "lineno", "id", "dividend_type", "divisor_type")
trace_modified = create_table("division_modified", "filename", "lineno", "id")
@register_tracing_function
def division_traced(id, dividend, divisor):
# id is used to differentiate the divisions of the same line. It is not
# possible to obtain this information with inspect.currentframe() only.
previous_frame = inspect.currentframe().f_back
def division_traced(filename, lineno, id_, dividend, divisor):
trace(
previous_frame.f_code.co_filename,
previous_frame.f_lineno,
id,
filename,
lineno,
id_,
parse_type(type(dividend)),
parse_type(type(divisor))
)
......@@ -80,6 +75,10 @@ class FixDivisionTrace(BaseFix):
new_node = lib2to3.fixer_util.Call(
lib2to3.fixer_util.Name("division_traced"),
args=(
lib2to3.fixer_util.String('"%s"' % self.filename),
lib2to3.fixer_util.Comma(),
lib2to3.fixer_util.Number(lineno),
lib2to3.fixer_util.Comma(),
lib2to3.fixer_util.Number(id_),
lib2to3.fixer_util.Comma(),
dividend,
......
......@@ -8,38 +8,38 @@ class testFixDivisionTrace(FixerTestCase):
def test_simple_division(self):
b = """x / y"""
a = """division_traced(0,x , y)"""
a = """division_traced("<string>",1,0,x , y)"""
self.check(b, a)
def test_nested_divisions_with_parentheses_1(self):
b = """(x / y) / z"""
a = """division_traced(1,(division_traced(0,x , y)) , z)"""
a = """division_traced("<string>",1,1,(division_traced("<string>",1,0,x , y)) , z)"""
self.check(b, a)
def test_nested_divisions_with_parentheses_2(self):
b = """x / (y / z)"""
a = """division_traced(1,x , (division_traced(0,y , z)))"""
a = """division_traced("<string>",1,1,x , (division_traced("<string>",1,0,y , z)))"""
self.check(b, a)
def test_inline_division_1(self):
b = """1 / 2 / 3"""
a = """division_traced(1,division_traced(0,1 , 2) , 3)"""
a = """division_traced("<string>",1,1,division_traced("<string>",1,0,1 , 2) , 3)"""
self.check(b, a)
def test_inline_division_2(self):
b = """1 / 2 * 3"""
a = """division_traced(0,1 , 2) * 3"""
a = """division_traced("<string>",1,0,1 , 2) * 3"""
self.check(b, a)
def test_inline_division_3(self):
b = """1 * 2 / 3"""
a = """division_traced(0,1 * 2 , 3)"""
a = """division_traced("<string>",1,0,1 * 2 , 3)"""
self.check(b, a)
def test_division_on_line_continuation(self):
b = """x \
/ y"""
a = """ division_traced(0,x \
a = """ division_traced("<string>",1,0,x \
, y)"""
self.check(b, a)
......@@ -47,7 +47,7 @@ class testFixDivisionTrace(FixerTestCase):
b = """foo = \
x / y"""
a = """foo = \
division_traced(0,x , y)"""
division_traced("<string>",1,0,x , y)"""
self.check(b, a)
......
import unittest
from my2to3.trace import apply_fixers
class testTrace(unittest.TestCase):
def test_apply_fixers(self):
self.assertEqual(apply_fixers("1 / 2", "foo"), 'division_traced("foo",1,0,1 , 2)')
if __name__ == '__main__':
unittest.main()
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