| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import queue |
| 8 |
import sys |
| 9 |
import unittest |
| 10 |
|
| 11 |
import astroid |
| 12 |
from astroid import builder, nodes |
| 13 |
from astroid.manager import AstroidManager |
| 14 |
|
| 15 |
try: |
| 16 |
import multiprocessing # pylint: disable=unused-import |
| 17 |
|
| 18 |
HAS_MULTIPROCESSING = True |
| 19 |
except ImportError: |
| 20 |
HAS_MULTIPROCESSING = False |
| 21 |
|
| 22 |
|
| 23 |
@unittest.skipUnless( |
| 24 |
HAS_MULTIPROCESSING, |
| 25 |
"multiprocesing is required for this test, but " |
| 26 |
"on some platforms it is missing " |
| 27 |
"(Jython for instance)", |
| 28 |
) |
| 29 |
class MultiprocessingBrainTest(unittest.TestCase): |
| 30 |
def test_multiprocessing_module_attributes(self) -> None: |
| 31 |
# Test that module attributes are working, |
| 32 |
# especially on Python 3.4+, where they are obtained |
| 33 |
# from a context. |
| 34 |
module = builder.extract_node(""" |
| 35 |
import multiprocessing |
| 36 |
""") |
| 37 |
assert isinstance(module, nodes.Import) |
| 38 |
module = module.do_import_module("multiprocessing") |
| 39 |
cpu_count = next(module.igetattr("cpu_count")) |
| 40 |
self.assertIsInstance(cpu_count, astroid.BoundMethod) |
| 41 |
|
| 42 |
def test_module_name(self) -> None: |
| 43 |
module = builder.extract_node(""" |
| 44 |
import multiprocessing |
| 45 |
multiprocessing.SyncManager() |
| 46 |
""") |
| 47 |
inferred_sync_mgr = next(module.infer()) |
| 48 |
module = inferred_sync_mgr.root() |
| 49 |
self.assertEqual(module.name, "multiprocessing.managers") |
| 50 |
|
| 51 |
def test_multiprocessing_manager(self) -> None: |
| 52 |
# Test that we have the proper attributes |
| 53 |
# for a multiprocessing.managers.SyncManager |
| 54 |
module = builder.parse(""" |
| 55 |
import multiprocessing |
| 56 |
manager = multiprocessing.Manager() |
| 57 |
queue = manager.Queue() |
| 58 |
joinable_queue = manager.JoinableQueue() |
| 59 |
event = manager.Event() |
| 60 |
rlock = manager.RLock() |
| 61 |
lock = manager.Lock() |
| 62 |
bounded_semaphore = manager.BoundedSemaphore() |
| 63 |
condition = manager.Condition() |
| 64 |
barrier = manager.Barrier() |
| 65 |
pool = manager.Pool() |
| 66 |
list = manager.list() |
| 67 |
dict = manager.dict() |
| 68 |
value = manager.Value() |
| 69 |
array = manager.Array() |
| 70 |
namespace = manager.Namespace() |
| 71 |
""") |
| 72 |
ast_queue = next(module["queue"].infer()) |
| 73 |
self.assertEqual(ast_queue.qname(), f"{queue.__name__}.Queue") |
| 74 |
|
| 75 |
joinable_queue = next(module["joinable_queue"].infer()) |
| 76 |
self.assertEqual(joinable_queue.qname(), f"{queue.__name__}.Queue") |
| 77 |
|
| 78 |
event = next(module["event"].infer()) |
| 79 |
event_name = "threading.Event" |
| 80 |
self.assertEqual(event.qname(), event_name) |
| 81 |
|
| 82 |
rlock = next(module["rlock"].infer()) |
| 83 |
rlock_name = "threading._RLock" |
| 84 |
self.assertEqual(rlock.qname(), rlock_name) |
| 85 |
|
| 86 |
lock = next(module["lock"].infer()) |
| 87 |
lock_name = "threading.lock" |
| 88 |
self.assertEqual(lock.qname(), lock_name) |
| 89 |
|
| 90 |
bounded_semaphore = next(module["bounded_semaphore"].infer()) |
| 91 |
semaphore_name = "threading.BoundedSemaphore" |
| 92 |
self.assertEqual(bounded_semaphore.qname(), semaphore_name) |
| 93 |
|
| 94 |
pool = next(module["pool"].infer()) |
| 95 |
pool_name = "multiprocessing.pool.Pool" |
| 96 |
self.assertEqual(pool.qname(), pool_name) |
| 97 |
|
| 98 |
for attr in ("list", "dict"): |
| 99 |
obj = next(module[attr].infer()) |
| 100 |
self.assertEqual(obj.qname(), f"builtins.{attr}") |
| 101 |
|
| 102 |
# pypy's implementation of array.__spec__ return None. This causes problems for this inference. |
| 103 |
if not hasattr(sys, "pypy_version_info"): |
| 104 |
array = next(module["array"].infer()) |
| 105 |
self.assertEqual(array.qname(), "array.array") |
| 106 |
|
| 107 |
manager = next(module["manager"].infer()) |
| 108 |
# Verify that we have these attributes |
| 109 |
self.assertTrue(manager.getattr("start")) |
| 110 |
self.assertTrue(manager.getattr("shutdown")) |
| 111 |
|
| 112 |
|
| 113 |
def test_multiprocessing_shadowed_by_local_package(tmp_path, monkeypatch) -> None: |
| 114 |
"""When a local package named ``multiprocessing`` shadows the stdlib one, |
| 115 |
``multiprocessing.context`` cannot be resolved and inference yields |
| 116 |
``Uninferable``. The brain must not crash trying to iterate its locals. |
| 117 |
""" |
| 118 |
pkg = tmp_path / "multiprocessing" |
| 119 |
pkg.mkdir() |
| 120 |
(pkg / "__init__.py").write_text("") |
| 121 |
monkeypatch.syspath_prepend(str(tmp_path)) |
| 122 |
|
| 123 |
manager = AstroidManager() |
| 124 |
# drop any cached stdlib ``multiprocessing`` |
| 125 |
manager.clear_cache() |
| 126 |
try: |
| 127 |
module = manager.ast_from_module_name("multiprocessing") |
| 128 |
assert isinstance(module, nodes.Module) |
| 129 |
finally: |
| 130 |
manager.clear_cache() |