Commit 86a52e15 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

trace: don't use a tempfile in apply_fixers anymore

parent ed920af2
import lib2to3.fixer_base
import os
class BaseFix(lib2to3.fixer_base.BaseFix):
def start_tree(self, tree, filename):
# See my2to3.trace.apply_fixers
sep = '-' * 5 # Arbitrary
if sep in filename:
filename = filename.split(sep, 1)[1].replace(sep, os.sep)
super(BaseFix, self).start_tree(tree, filename)
import __builtin__ import __builtin__
from collections import defaultdict from collections import defaultdict
from lib2to3.fixer_base import BaseFix
import lib2to3.pgen2 import lib2to3.pgen2
from lib2to3.pygram import python_symbols as syms from lib2to3.pygram import python_symbols as syms
from lib2to3.pytree import Leaf, Node from lib2to3.pytree import Leaf, Node
...@@ -7,7 +8,6 @@ import os ...@@ -7,7 +8,6 @@ import os
import re import re
from my2to3.util import add_future from my2to3.util import add_future
from my2to3.fixes import BaseFix
trace_file_match = re.compile(r"^(.*)\|(.*)\|(.*) <type '(.*)'> / <type '(.*)'>$").match trace_file_match = re.compile(r"^(.*)\|(.*)\|(.*) <type '(.*)'> / <type '(.*)'>$").match
......
# 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 import inspect
from lib2to3.fixer_base import BaseFix
import lib2to3.fixer_util import lib2to3.fixer_util
import lib2to3.pgen2 import lib2to3.pgen2
from lib2to3.pygram import python_symbols as syms from lib2to3.pygram import python_symbols as syms
from lib2to3.pytree import Node from lib2to3.pytree import Node
from my2to3.fixes import BaseFix
from my2to3.trace import create_table, register_tracing_function from my2to3.trace import create_table, register_tracing_function
from my2to3.util import parse_type from my2to3.util import parse_type
......
from lib2to3.fixer_base import BaseFix
from lib2to3.pygram import python_symbols as syms from lib2to3.pygram import python_symbols as syms
from my2to3.fixes import BaseFix
from my2to3.trace import create_table from my2to3.trace import create_table
......
import __builtin__, imp, os, sqlite3, sys, tempfile, types import __builtin__, imp, os, sqlite3, sys, types
from lib2to3.pgen2 import tokenize
from lib2to3.refactor import get_fixers_from_package, RefactoringTool from lib2to3.refactor import get_fixers_from_package, RefactoringTool
database = "traces.db" database = "traces.db"
...@@ -44,20 +45,16 @@ def get_fixers(): ...@@ -44,20 +45,16 @@ def get_fixers():
def apply_fixers(string, name): def apply_fixers(string, name):
# This function is inspired by refactoring_tool.refactor_file
refactoring_tool = RefactoringTool(fixer_names=get_fixers()) refactoring_tool = RefactoringTool(fixer_names=get_fixers())
# A temporary file is used to enjoy the benefits of encoding = tokenize.detect_encoding(lambda: string)[0]
# refactoring_tool.refactor_file (unlike directly using
# refactoring_tool.refactor_string). string += "\n" # Silence certain parse errors
# s = string.decode(encoding)
# XXX: We add the original name via the suffix. Fixers are able to handle this s = refactoring_tool.refactor_string(s, name)
# (see my2to3.fixes.BaseFix). Let's find a better way. # The [:-1] is to take off the \n we added earlier
sep = '-' * 5 # Arbitrary return unicode(s)[:-1].encode(encoding)
with tempfile.NamedTemporaryFile(suffix=sep + name.replace(os.sep, sep)) as f:
f.write(string)
f.flush()
refactoring_tool.refactor_file(f.name, write=True)
f.seek(0)
return f.read()
init_py = '__init__' + os.extsep + 'py' init_py = '__init__' + os.extsep + 'py'
......
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