##// END OF EJS Templates
revsetbenchmarks: use a more compact output format with a header...
revsetbenchmarks: use a more compact output format with a header We change the output from: revset #0: draft() 0) wall 0.011989 comb 0.010000 user 0.000000 sys 0.010000 (best of 177) 1) wall 0.012226 comb 0.010000 user 0.000000 sys 0.010000 (best of 193) 2) wall 0.011838 comb 0.020000 user 0.000000 sys 0.020000 (best of 208) to: revset #0: draft() wall comb user sys count 0) 0.012028 0.010000 0.000000 0.010000 170 1) 0.012218 0.010000 0.000000 0.010000 157 2) 0.012622 0.010000 0.000000 0.010000 189 This opens the road to more useful output.

File last commit:

r21637:48ef6800 default
r25534:43e5a681 default
Show More
fix_bytes.py
98 lines | 2.9 KiB | text/x-python | PythonLexer
Renato Cunha
hgfixes: add a fixer to convert plain strings to bytestrings...
r11747 """Fixer that changes plain strings to bytes strings."""
import re
from lib2to3 import fixer_base
from lib2to3.pgen2 import token
from lib2to3.fixer_util import Name
from lib2to3.pygram import python_symbols as syms
_re = re.compile(r'[rR]?[\'\"]')
# XXX: Implementing a blacklist in 2to3 turned out to be more troublesome than
# blacklisting some modules inside the fixers. So, this is what I came with.
Gregory Szorc
fix_bytes: loosen blacklist matching requirements...
r21637 blacklist = ('mercurial/demandimport.py',
Renato Cunha
py3kcompat: added a "compatibility layer" for py3k...
r11748 'mercurial/py3kcompat.py', # valid python 3 already
Renato Cunha
hgfixes: add a fixer to convert plain strings to bytestrings...
r11747 'mercurial/i18n.py',
Gregory Szorc
fix_bytes: loosen blacklist matching requirements...
r21637 )
Renato Cunha
hgfixes: add a fixer to convert plain strings to bytestrings...
r11747
def isdocstring(node):
def isclassorfunction(ancestor):
symbols = (syms.funcdef, syms.classdef)
# if the current node is a child of a function definition, a class
# definition or a file, then it is a docstring
if ancestor.type == syms.simple_stmt:
try:
while True:
if ancestor.type in symbols:
return True
ancestor = ancestor.parent
except AttributeError:
return False
return False
def ismodule(ancestor):
# Our child is a docstring if we are a simple statement, and our
# ancestor is file_input. In other words, our child is a lone string in
# the source file.
try:
if (ancestor.type == syms.simple_stmt and
ancestor.parent.type == syms.file_input):
return True
except AttributeError:
return False
def isdocassignment(ancestor):
# Assigning to __doc__, definitely a string
try:
while True:
if (ancestor.type == syms.expr_stmt and
Name('__doc__') in ancestor.children):
return True
ancestor = ancestor.parent
except AttributeError:
return False
if ismodule(node.parent) or \
isdocassignment(node.parent) or \
isclassorfunction(node.parent):
return True
return False
def shouldtransform(node):
specialnames = ['__main__']
if node.value in specialnames:
return False
ggparent = node.parent.parent.parent
sggparent = str(ggparent)
if 'getattr' in sggparent or \
'hasattr' in sggparent or \
'setattr' in sggparent or \
'encode' in sggparent or \
'decode' in sggparent:
Mads Kiilerich
check-code: indent 4 spaces in py files
r17299 return False
Renato Cunha
hgfixes: add a fixer to convert plain strings to bytestrings...
r11747
return True
class FixBytes(fixer_base.BaseFix):
PATTERN = 'STRING'
def transform(self, node, results):
Gregory Szorc
fix_bytes: loosen blacklist matching requirements...
r21637 # The filename may be prefixed with a build directory.
if self.filename.endswith(blacklist):
Renato Cunha
hgfixes: add a fixer to convert plain strings to bytestrings...
r11747 return
if node.type == token.STRING:
if _re.match(node.value):
if isdocstring(node):
return
if not shouldtransform(node):
return
new = node.clone()
new.value = 'b' + new.value
return new