fix_getcwd.py 873 B

1234567891011121314151617181920212223242526
  1. u"""
  2. Fixer for os.getcwd() -> os.getcwdu().
  3. Also warns about "from os import getcwd", suggesting the above form.
  4. """
  5. from lib2to3 import fixer_base
  6. from lib2to3.fixer_util import Name
  7. class FixGetcwd(fixer_base.BaseFix):
  8. PATTERN = u"""
  9. power< 'os' trailer< dot='.' name='getcwd' > any* >
  10. |
  11. import_from< 'from' 'os' 'import' bad='getcwd' >
  12. """
  13. def transform(self, node, results):
  14. if u"name" in results:
  15. name = results[u"name"]
  16. name.replace(Name(u"getcwdu", prefix=name.prefix))
  17. elif u"bad" in results:
  18. # Can't convert to getcwdu and then expect to catch every use.
  19. self.cannot_convert(node, u"import os, use os.getcwd() instead.")
  20. return
  21. else:
  22. raise ValueError(u"For some reason, the pattern matcher failed.")