Commit 8ab2c9bf authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

trace_division: append the caller's id to `division_traced`'s parameters

This id is a unique number that distinguishes callers from the same line.
parent 83a10ac5
# https://lab.nexedi.com/nexedi/erp5/snippets/475
from collections import defaultdict
import lib2to3.fixer_base
import lib2to3.fixer_util
import lib2to3.pgen2
......@@ -10,12 +11,13 @@ class FixTraceDivision(lib2to3.fixer_base.BaseFix):
division_traced can be a function that looks up the stack and record the operand types, for example:
def division_traced(dividend, divisor):
def division_traced(id, dividend, divisor):
import inspect
previous_frame = inspect.currentframe().f_back
print ("{}:{} {} / {}".format(
print ("{}:{}:{} {} / {}".format(
previous_frame.f_code.co_filename,
previous_frame.f_lineno,
id,
type(dividend),
type(divisor)
))
......@@ -25,6 +27,9 @@ class FixTraceDivision(lib2to3.fixer_base.BaseFix):
__builtin__.division_traced = division_traced
"""
def start_tree(self, tree, filename):
super(FixTraceDivision, self).start_tree(tree, filename)
self.ids = defaultdict(int)
def match(self, node):
return (
......@@ -32,12 +37,16 @@ class FixTraceDivision(lib2to3.fixer_base.BaseFix):
node.children[1].type == lib2to3.pgen2.token.SLASH)
def transform(self, node, results):
lineno = node.get_lineno()
node.replace(
lib2to3.fixer_util.Call(
lib2to3.fixer_util.Name("division_traced"),
args=(
lib2to3.fixer_util.Number(self.ids[lineno]),
lib2to3.fixer_util.Comma(),
node.children[0].clone(),
lib2to3.fixer_util.Comma(),
node.children[2].clone(),
),
prefix=node.prefix))
self.ids[lineno] += 1
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