Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

brain_subprocess.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (c) 2016 Claudiu Popa <pcmanticore@gmail.com>
  2. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  3. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  4. import sys
  5. import textwrap
  6. import six
  7. import astroid
  8. PY34 = sys.version_info >= (3, 4)
  9. PY36 = sys.version_info >= (3, 6)
  10. def _subprocess_transform():
  11. if six.PY3:
  12. communicate = (bytes('string', 'ascii'), bytes('string', 'ascii'))
  13. communicate_signature = 'def communicate(self, input=None, timeout=None)'
  14. if PY36:
  15. init = """
  16. def __init__(self, args, bufsize=0, executable=None,
  17. stdin=None, stdout=None, stderr=None,
  18. preexec_fn=None, close_fds=False, shell=False,
  19. cwd=None, env=None, universal_newlines=False,
  20. startupinfo=None, creationflags=0, restore_signals=True,
  21. start_new_session=False, pass_fds=(), *,
  22. encoding=None, errors=None):
  23. pass
  24. """
  25. else:
  26. init = """
  27. def __init__(self, args, bufsize=0, executable=None,
  28. stdin=None, stdout=None, stderr=None,
  29. preexec_fn=None, close_fds=False, shell=False,
  30. cwd=None, env=None, universal_newlines=False,
  31. startupinfo=None, creationflags=0, restore_signals=True,
  32. start_new_session=False, pass_fds=()):
  33. pass
  34. """
  35. else:
  36. communicate = ('string', 'string')
  37. communicate_signature = 'def communicate(self, input=None)'
  38. init = """
  39. def __init__(self, args, bufsize=0, executable=None,
  40. stdin=None, stdout=None, stderr=None,
  41. preexec_fn=None, close_fds=False, shell=False,
  42. cwd=None, env=None, universal_newlines=False,
  43. startupinfo=None, creationflags=0):
  44. pass
  45. """
  46. if PY34:
  47. wait_signature = 'def wait(self, timeout=None)'
  48. else:
  49. wait_signature = 'def wait(self)'
  50. if six.PY3:
  51. ctx_manager = '''
  52. def __enter__(self): return self
  53. def __exit__(self, *args): pass
  54. '''
  55. else:
  56. ctx_manager = ''
  57. code = textwrap.dedent('''
  58. class Popen(object):
  59. returncode = pid = 0
  60. stdin = stdout = stderr = file()
  61. %(communicate_signature)s:
  62. return %(communicate)r
  63. %(wait_signature)s:
  64. return self.returncode
  65. def poll(self):
  66. return self.returncode
  67. def send_signal(self, signal):
  68. pass
  69. def terminate(self):
  70. pass
  71. def kill(self):
  72. pass
  73. %(ctx_manager)s
  74. ''' % {'communicate': communicate,
  75. 'communicate_signature': communicate_signature,
  76. 'wait_signature': wait_signature,
  77. 'ctx_manager': ctx_manager})
  78. init_lines = textwrap.dedent(init).splitlines()
  79. indented_init = '\n'.join([' ' * 4 + line for line in init_lines])
  80. code += indented_init
  81. return astroid.parse(code)
  82. astroid.register_module_extender(astroid.MANAGER, 'subprocess', _subprocess_transform)