comparison planemo/lib/python3.7/site-packages/libfuturize/fixes/fix_oldstr_wrap.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 """
2 For the ``future`` package.
3
4 Adds this import line:
5
6 from past.builtins import str as oldstr
7
8 at the top and wraps any unadorned string literals 'abc' or explicit byte-string
9 literals b'abc' in oldstr() calls so the code has the same behaviour on Py3 as
10 on Py2.6/2.7.
11 """
12
13 from __future__ import unicode_literals
14 import re
15 from lib2to3 import fixer_base
16 from lib2to3.pgen2 import token
17 from lib2to3.fixer_util import syms
18 from 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
25 class 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