1""" 2For the ``future`` package. 3 4Adds this import line: 5 6 from past.builtins import str as oldstr 7 8at the top and wraps any unadorned string literals 'abc' or explicit byte-string 9literals b'abc' in oldstr() calls so the code has the same behaviour on Py3 as 10on Py2.6/2.7. 11""" 12 13from __future__ import unicode_literals 14import re 15from lib2to3 import fixer_base 16from lib2to3.pgen2 import token 17from lib2to3.fixer_util import syms 18from libfuturize.fixer_util import (future_import, touch_import_top, 19 wrap_in_fn_call) 20 21 22_literal_re = re.compile(r"[^uUrR]?[\'\"]") 23 24 25class FixOldstrWrap(fixer_base.BaseFix): 26 BM_compatible = True 27 PATTERN = "STRING" 28 29 def transform(self, node, results): 30 if node.type == token.STRING: 31 touch_import_top(u'past.types', u'oldstr', node) 32 if _literal_re.match(node.value): 33 new = node.clone() 34 # Strip any leading space or comments: 35 # TODO: check: do we really want to do this? 36 new.prefix = u'' 37 new.value = u'b' + new.value 38 wrapped = wrap_in_fn_call("oldstr", [new], prefix=node.prefix) 39 return wrapped 40