fix_bytes.py 685 B

123456789101112131415161718192021222324
  1. """Optional fixer that changes all unprefixed string literals "..." to b"...".
  2. br'abcd' is a SyntaxError on Python 2 but valid on Python 3.
  3. ur'abcd' is a SyntaxError on Python 3 but valid on Python 2.
  4. """
  5. from __future__ import unicode_literals
  6. import re
  7. from lib2to3.pgen2 import token
  8. from lib2to3 import fixer_base
  9. _literal_re = re.compile(r"[^bBuUrR]?[\'\"]")
  10. class FixBytes(fixer_base.BaseFix):
  11. BM_compatible = True
  12. PATTERN = "STRING"
  13. def transform(self, node, results):
  14. if node.type == token.STRING:
  15. if _literal_re.match(node.value):
  16. new = node.clone()
  17. new.value = u'b' + new.value
  18. return new