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_multiprocessing.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 astroid
  6. from astroid import exceptions
  7. PY34 = sys.version_info >= (3, 4)
  8. def _multiprocessing_transform():
  9. module = astroid.parse('''
  10. from multiprocessing.managers import SyncManager
  11. def Manager():
  12. return SyncManager()
  13. ''')
  14. if not PY34:
  15. return module
  16. # On Python 3.4, multiprocessing uses a getattr lookup inside contexts,
  17. # in order to get the attributes they need. Since it's extremely
  18. # dynamic, we use this approach to fake it.
  19. node = astroid.parse('''
  20. from multiprocessing.context import DefaultContext, BaseContext
  21. default = DefaultContext()
  22. base = BaseContext()
  23. ''')
  24. try:
  25. context = next(node['default'].infer())
  26. base = next(node['base'].infer())
  27. except exceptions.InferenceError:
  28. return module
  29. for node in (context, base):
  30. for key, value in node.locals.items():
  31. if key.startswith("_"):
  32. continue
  33. value = value[0]
  34. if isinstance(value, astroid.FunctionDef):
  35. # We need to rebound this, since otherwise
  36. # it will have an extra argument (self).
  37. value = astroid.BoundMethod(value, node)
  38. module[key] = value
  39. return module
  40. def _multiprocessing_managers_transform():
  41. return astroid.parse('''
  42. import array
  43. import threading
  44. import multiprocessing.pool as pool
  45. import six
  46. class Namespace(object):
  47. pass
  48. class Value(object):
  49. def __init__(self, typecode, value, lock=True):
  50. self._typecode = typecode
  51. self._value = value
  52. def get(self):
  53. return self._value
  54. def set(self, value):
  55. self._value = value
  56. def __repr__(self):
  57. return '%s(%r, %r)'%(type(self).__name__, self._typecode, self._value)
  58. value = property(get, set)
  59. def Array(typecode, sequence, lock=True):
  60. return array.array(typecode, sequence)
  61. class SyncManager(object):
  62. Queue = JoinableQueue = six.moves.queue.Queue
  63. Event = threading.Event
  64. RLock = threading.RLock
  65. BoundedSemaphore = threading.BoundedSemaphore
  66. Condition = threading.Condition
  67. Barrier = threading.Barrier
  68. Pool = pool.Pool
  69. list = list
  70. dict = dict
  71. Value = Value
  72. Array = Array
  73. Namespace = Namespace
  74. __enter__ = lambda self: self
  75. __exit__ = lambda *args: args
  76. def start(self, initializer=None, initargs=None):
  77. pass
  78. def shutdown(self):
  79. pass
  80. ''')
  81. astroid.register_module_extender(astroid.MANAGER, 'multiprocessing.managers',
  82. _multiprocessing_managers_transform)
  83. astroid.register_module_extender(astroid.MANAGER, 'multiprocessing',
  84. _multiprocessing_transform)