stdin.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Monkey-patching for pep8 and pycodestyle."""
  2. try:
  3. import pep8
  4. except ImportError:
  5. pep8 = None
  6. try:
  7. import pycodestyle
  8. except ImportError:
  9. pycodestyle = None
  10. from flake8_polyfill import version
  11. __all__ = ('monkey_patch',)
  12. modules = {
  13. 'pep8': [pep8],
  14. 'pycodestyle': [pycodestyle],
  15. 'all': [pep8, pycodestyle],
  16. }
  17. def monkey_patch(which):
  18. """Monkey-patch the specified module with the appropriate stdin.
  19. On Flake8 2.5 and lower, Flake8 would would monkey-patch
  20. ``pep8.stdin_get_value`` for everyone. This avoided problems where
  21. stdin might be exhausted.
  22. On Flake8 2.6, Flake8 stopped patching ``pep8`` and started
  23. monkey-patching ``pycodestyle.stdin_get_value``.
  24. On Flake8 3.x, Flake8 has no need to monkey patch either ``pep8`` or
  25. ``pycodestyle``.
  26. This function accepts three parameters:
  27. - pep8
  28. - pycodestyle
  29. - all
  30. "all" is a special value that will monkey-patch both "pep8" and
  31. "pycodestyle".
  32. :param str which:
  33. The name of the module to patch.
  34. :returns:
  35. Nothing.
  36. :rtype:
  37. NoneType
  38. """
  39. if (2, 0) <= version.version_info < (3, 0):
  40. from flake8.engine import pep8 as _pep8
  41. stdin_get_value = _pep8.stdin_get_value
  42. elif (3, 0) <= version.version_info < (4, 0):
  43. from flake8 import utils
  44. stdin_get_value = utils.stdin_get_value
  45. for module in modules[which]:
  46. if module is None:
  47. continue
  48. module.stdin_get_value = stdin_get_value