diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2011-01-12 17:02:40 +0000 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2011-01-13 11:37:40 +0000 |
commit | 332acccc6ec1fedc7a460089a695a772fe341232 (patch) | |
tree | 362c93fcd94b39801f500291772dffd4ac23bf03 /scripts/fix_b.py | |
parent | 3ba0631dbb75514f51552e9168524e38a3efa787 (diff) | |
download | psycopg2-332acccc6ec1fedc7a460089a695a772fe341232.tar.gz |
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.
Diffstat (limited to 'scripts/fix_b.py')
-rw-r--r-- | scripts/fix_b.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/scripts/fix_b.py b/scripts/fix_b.py new file mode 100644 index 0000000..ccc8e1c --- /dev/null +++ b/scripts/fix_b.py @@ -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'] + |