Mercurial > cpython-lru_cache-weakref
annotate Lib/dummy_threading.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 1163f55dad95 |
| children | 7fe3a8648ce2 |
| rev | line source |
|---|---|
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Faux ``threading`` version using ``dummy_thread`` instead of ``thread``. |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 The module ``_dummy_threading`` is added to ``sys.modules`` in order |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 to not have ``threading`` considered imported. Had ``threading`` been |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 directly imported it would have made all subsequent imports succeed |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 regardless of whether ``thread`` was available which is not desired. |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 """ |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 from sys import modules as sys_modules |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 import dummy_thread |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 # Declaring now so as to not have to nest ``try``s to get proper clean-up. |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 holding_thread = False |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 holding_threading = False |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
16 holding__threading_local = False |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 try: |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 # Could have checked if ``thread`` was not in sys.modules and gone |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 # a different route, but decided to mirror technique used with |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 # ``threading`` below. |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 if 'thread' in sys_modules: |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 held_thread = sys_modules['thread'] |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 holding_thread = True |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 # Must have some module named ``thread`` that implements its API |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 # in order to initially import ``threading``. |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 sys_modules['thread'] = sys_modules['dummy_thread'] |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 if 'threading' in sys_modules: |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 # If ``threading`` is already imported, might as well prevent |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 # trying to import it more than needed by saving it if it is |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 # already imported before deleting it. |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 held_threading = sys_modules['threading'] |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 holding_threading = True |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 del sys_modules['threading'] |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
36 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
37 if '_threading_local' in sys_modules: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
38 # If ``_threading_local`` is already imported, might as well prevent |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
39 # trying to import it more than needed by saving it if it is |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
40 # already imported before deleting it. |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
41 held__threading_local = sys_modules['_threading_local'] |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
42 holding__threading_local = True |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
43 del sys_modules['_threading_local'] |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32673
diff
changeset
|
44 |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 import threading |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 # Need a copy of the code kept somewhere... |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 sys_modules['_dummy_threading'] = sys_modules['threading'] |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 del sys_modules['threading'] |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
49 sys_modules['_dummy__threading_local'] = sys_modules['_threading_local'] |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
50 del sys_modules['_threading_local'] |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 from _dummy_threading import * |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 from _dummy_threading import __all__ |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 finally: |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 # Put back ``threading`` if we overwrote earlier |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
56 |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 if holding_threading: |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 sys_modules['threading'] = held_threading |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 del held_threading |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 del holding_threading |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
62 # Put back ``_threading_local`` if we overwrote earlier |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
63 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
64 if holding__threading_local: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
65 sys_modules['_threading_local'] = held__threading_local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
66 del held__threading_local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
67 del holding__threading_local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
26890
diff
changeset
|
68 |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 # Put back ``thread`` if we overwrote, else del the entry we made |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 if holding_thread: |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 sys_modules['thread'] = held_thread |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 del held_thread |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 else: |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 del sys_modules['thread'] |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 del holding_thread |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 del dummy_thread |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 del sys_modules |
