mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-25 18:33:44 +03:00
Replace b('str') with b'str' in Python 3
This avoids an encode() call for each of these constants. Use a custom 2to3 fixer in setup.py to perform the conversion.
This commit is contained in:
parent
3ba0631dbb
commit
332acccc6e
20
scripts/fix_b.py
Normal file
20
scripts/fix_b.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
"""Fixer to change b('string') into b'string'."""
|
||||
# Author: Daniele Varrazzo
|
||||
|
||||
import token
|
||||
from lib2to3 import fixer_base
|
||||
from lib2to3.pytree import Leaf
|
||||
|
||||
class FixB(fixer_base.BaseFix):
|
||||
|
||||
PATTERN = """
|
||||
power< wrapper='b' trailer< '(' arg=[any] ')' > rest=any* >
|
||||
"""
|
||||
|
||||
def transform(self, node, results):
|
||||
arg = results['arg']
|
||||
wrapper = results["wrapper"]
|
||||
if len(arg) == 1 and arg[0].type == token.STRING:
|
||||
b = Leaf(token.STRING, 'b' + arg[0].value, prefix=wrapper.prefix)
|
||||
node.children = [ b ] + results['rest']
|
||||
|
6
setup.py
6
setup.py
|
@ -58,6 +58,12 @@ try:
|
|||
from distutils.command.build_py import build_py_2to3 as build_py
|
||||
except ImportError:
|
||||
from distutils.command.build_py import build_py
|
||||
else:
|
||||
# Configure distutils to run our custom 2to3 fixers as well
|
||||
from lib2to3.refactor import get_fixers_from_package
|
||||
build_py.fixer_names = get_fixers_from_package('lib2to3.fixes')
|
||||
build_py.fixer_names.append('fix_b')
|
||||
sys.path.insert(0, 'scripts')
|
||||
|
||||
try:
|
||||
import configparser
|
||||
|
|
Loading…
Reference in New Issue
Block a user