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 # https://lab.nexedi.com/nexedi/erp5/snippets/475
from collections import defaultdict from collections import defaultdict
import inspect
from lib2to3.fixer_base import BaseFix from lib2to3.fixer_base import BaseFix
import lib2to3.fixer_util import lib2to3.fixer_util
import lib2to3.pgen2 import lib2to3.pgen2
...@@ -11,21 +10,17 @@ from my2to3.trace import create_table, register_tracing_function ...@@ -11,21 +10,17 @@ from my2to3.trace import create_table, register_tracing_function
from my2to3.util import parse_type 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 = create_table("division", "filename", "lineno", "id", "dividend_type", "divisor_type")
trace_modified = create_table("division_modified", "filename", "lineno", "id") trace_modified = create_table("division_modified", "filename", "lineno", "id")
@register_tracing_function @register_tracing_function
def division_traced(id, dividend, divisor): def division_traced(filename, lineno, 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
trace( trace(
previous_frame.f_code.co_filename, filename,
previous_frame.f_lineno, lineno,
id, id_,
parse_type(type(dividend)), parse_type(type(dividend)),
parse_type(type(divisor)) parse_type(type(divisor))
) )
...@@ -80,6 +75,10 @@ class FixDivisionTrace(BaseFix): ...@@ -80,6 +75,10 @@ class FixDivisionTrace(BaseFix):
new_node = lib2to3.fixer_util.Call( new_node = lib2to3.fixer_util.Call(
lib2to3.fixer_util.Name("division_traced"), lib2to3.fixer_util.Name("division_traced"),
args=( 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.Number(id_),
lib2to3.fixer_util.Comma(), lib2to3.fixer_util.Comma(),
dividend, dividend,
......
...@@ -8,38 +8,38 @@ class testFixDivisionTrace(FixerTestCase): ...@@ -8,38 +8,38 @@ class testFixDivisionTrace(FixerTestCase):
def test_simple_division(self): def test_simple_division(self):
b = """x / y""" b = """x / y"""
a = """division_traced(0,x , y)""" a = """division_traced("<string>",1,0,x , y)"""
self.check(b, a) self.check(b, a)
def test_nested_divisions_with_parentheses_1(self): def test_nested_divisions_with_parentheses_1(self):
b = """(x / y) / z""" 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) self.check(b, a)
def test_nested_divisions_with_parentheses_2(self): def test_nested_divisions_with_parentheses_2(self):
b = """x / (y / z)""" 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) self.check(b, a)
def test_inline_division_1(self): def test_inline_division_1(self):
b = """1 / 2 / 3""" 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) self.check(b, a)
def test_inline_division_2(self): def test_inline_division_2(self):
b = """1 / 2 * 3""" b = """1 / 2 * 3"""
a = """division_traced(0,1 , 2) * 3""" a = """division_traced("<string>",1,0,1 , 2) * 3"""
self.check(b, a) self.check(b, a)
def test_inline_division_3(self): def test_inline_division_3(self):
b = """1 * 2 / 3""" b = """1 * 2 / 3"""
a = """division_traced(0,1 * 2 , 3)""" a = """division_traced("<string>",1,0,1 * 2 , 3)"""
self.check(b, a) self.check(b, a)
def test_division_on_line_continuation(self): def test_division_on_line_continuation(self):
b = """x \ b = """x \
/ y""" / y"""
a = """ division_traced(0,x \ a = """ division_traced("<string>",1,0,x \
, y)""" , y)"""
self.check(b, a) self.check(b, a)
...@@ -47,7 +47,7 @@ class testFixDivisionTrace(FixerTestCase): ...@@ -47,7 +47,7 @@ class testFixDivisionTrace(FixerTestCase):
b = """foo = \ b = """foo = \
x / y""" x / y"""
a = """foo = \ a = """foo = \
division_traced(0,x , y)""" division_traced("<string>",1,0,x , y)"""
self.check(b, a) 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