assassinlike commited on
Commit
ed80bb0
·
verified ·
1 Parent(s): d75f424

Add files using upload-large-folder tool

Browse files
Files changed (20) hide show
  1. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/__info__.py +221 -0
  2. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/__init__.py +66 -0
  3. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/connection.py +975 -0
  4. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/context.py +376 -0
  5. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/forkserver.py +347 -0
  6. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/heap.py +337 -0
  7. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/pool.py +957 -0
  8. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/popen_fork.py +83 -0
  9. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/popen_forkserver.py +74 -0
  10. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/popen_spawn_posix.py +72 -0
  11. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/popen_spawn_win32.py +132 -0
  12. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/process.py +439 -0
  13. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/reduction.py +284 -0
  14. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/shared_memory.py +534 -0
  15. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/sharedctypes.py +240 -0
  16. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/spawn.py +301 -0
  17. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/synchronize.py +403 -0
  18. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/util.py +491 -0
  19. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/nvidia_curand_cu12-10.3.2.106.dist-info/RECORD +32 -0
  20. data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/nvidia_curand_cu12-10.3.2.106.dist-info/top_level.txt +1 -0
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/__info__.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ #
3
+ # Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
4
+ # Copyright (c) 2023 The Uncertainty Quantification Foundation.
5
+ # License: 3-clause BSD. The full license text is available at:
6
+ # - https://github.com/uqfoundation/multiprocess/blob/master/LICENSE
7
+ '''
8
+ -----------------------------------------------------------------
9
+ multiprocess: better multiprocessing and multithreading in Python
10
+ -----------------------------------------------------------------
11
+
12
+ About Multiprocess
13
+ ==================
14
+
15
+ ``multiprocess`` is a fork of ``multiprocessing``. ``multiprocess`` extends ``multiprocessing`` to provide enhanced serialization, using `dill`. ``multiprocess`` leverages ``multiprocessing`` to support the spawning of processes using the API of the Python standard library's ``threading`` module. ``multiprocessing`` has been distributed as part of the standard library since Python 2.6.
16
+
17
+ ``multiprocess`` is part of ``pathos``, a Python framework for heterogeneous computing.
18
+ ``multiprocess`` is in active development, so any user feedback, bug reports, comments,
19
+ or suggestions are highly appreciated. A list of issues is located at https://github.com/uqfoundation/multiprocess/issues, with a legacy list maintained at https://uqfoundation.github.io/project/pathos/query.
20
+
21
+
22
+ Major Features
23
+ ==============
24
+
25
+ ``multiprocess`` enables:
26
+
27
+ - objects to be transferred between processes using pipes or multi-producer/multi-consumer queues
28
+ - objects to be shared between processes using a server process or (for simple data) shared memory
29
+
30
+ ``multiprocess`` provides:
31
+
32
+ - equivalents of all the synchronization primitives in ``threading``
33
+ - a ``Pool`` class to facilitate submitting tasks to worker processes
34
+ - enhanced serialization, using ``dill``
35
+
36
+
37
+ Current Release
38
+ ===============
39
+
40
+ The latest released version of ``multiprocess`` is available from:
41
+
42
+ https://pypi.org/project/multiprocess
43
+
44
+ ``multiprocess`` is distributed under a 3-clause BSD license, and is a fork of ``multiprocessing``.
45
+
46
+
47
+ Development Version
48
+ ===================
49
+
50
+ You can get the latest development version with all the shiny new features at:
51
+
52
+ https://github.com/uqfoundation
53
+
54
+ If you have a new contribution, please submit a pull request.
55
+
56
+
57
+ Installation
58
+ ============
59
+
60
+ ``multiprocess`` can be installed with ``pip``::
61
+
62
+ $ pip install multiprocess
63
+
64
+ For Python 2, a C compiler is required to build the included extension module from source. Python 3 and binary installs do not require a C compiler.
65
+
66
+
67
+ Requirements
68
+ ============
69
+
70
+ ``multiprocess`` requires:
71
+
72
+ - ``python`` (or ``pypy``), **>=3.7**
73
+ - ``setuptools``, **>=42**
74
+ - ``dill``, **>=0.3.7**
75
+
76
+
77
+ Basic Usage
78
+ ===========
79
+
80
+ The ``multiprocess.Process`` class follows the API of ``threading.Thread``.
81
+ For example ::
82
+
83
+ from multiprocess import Process, Queue
84
+
85
+ def f(q):
86
+ q.put('hello world')
87
+
88
+ if __name__ == '__main__':
89
+ q = Queue()
90
+ p = Process(target=f, args=[q])
91
+ p.start()
92
+ print (q.get())
93
+ p.join()
94
+
95
+ Synchronization primitives like locks, semaphores and conditions are
96
+ available, for example ::
97
+
98
+ >>> from multiprocess import Condition
99
+ >>> c = Condition()
100
+ >>> print (c)
101
+ <Condition(<RLock(None, 0)>), 0>
102
+ >>> c.acquire()
103
+ True
104
+ >>> print (c)
105
+ <Condition(<RLock(MainProcess, 1)>), 0>
106
+
107
+ One can also use a manager to create shared objects either in shared
108
+ memory or in a server process, for example ::
109
+
110
+ >>> from multiprocess import Manager
111
+ >>> manager = Manager()
112
+ >>> l = manager.list(range(10))
113
+ >>> l.reverse()
114
+ >>> print (l)
115
+ [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
116
+ >>> print (repr(l))
117
+ <Proxy[list] object at 0x00E1B3B0>
118
+
119
+ Tasks can be offloaded to a pool of worker processes in various ways,
120
+ for example ::
121
+
122
+ >>> from multiprocess import Pool
123
+ >>> def f(x): return x*x
124
+ ...
125
+ >>> p = Pool(4)
126
+ >>> result = p.map_async(f, range(10))
127
+ >>> print (result.get(timeout=1))
128
+ [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
129
+
130
+ When ``dill`` is installed, serialization is extended to most objects,
131
+ for example ::
132
+
133
+ >>> from multiprocess import Pool
134
+ >>> p = Pool(4)
135
+ >>> print (p.map(lambda x: (lambda y:y**2)(x) + x, xrange(10)))
136
+ [0, 2, 6, 12, 20, 30, 42, 56, 72, 90]
137
+
138
+
139
+ More Information
140
+ ================
141
+
142
+ Probably the best way to get started is to look at the documentation at
143
+ http://multiprocess.rtfd.io. Also see ``multiprocess.tests`` for scripts that
144
+ demonstrate how ``multiprocess`` can be used to leverge multiple processes
145
+ to execute Python in parallel. You can run the test suite with
146
+ ``python -m multiprocess.tests``. As ``multiprocess`` conforms to the
147
+ ``multiprocessing`` interface, the examples and documentation found at
148
+ http://docs.python.org/library/multiprocessing.html also apply to
149
+ ``multiprocess`` if one will ``import multiprocessing as multiprocess``.
150
+ See https://github.com/uqfoundation/multiprocess/tree/master/py3.11/examples
151
+ for a set of examples that demonstrate some basic use cases and benchmarking
152
+ for running Python code in parallel. Please feel free to submit a ticket on
153
+ github, or ask a question on stackoverflow (**@Mike McKerns**). If you would
154
+ like to share how you use ``multiprocess`` in your work, please send an email
155
+ (to **mmckerns at uqfoundation dot org**).
156
+
157
+
158
+ Citation
159
+ ========
160
+
161
+ If you use ``multiprocess`` to do research that leads to publication, we ask that you
162
+ acknowledge use of ``multiprocess`` by citing the following in your publication::
163
+
164
+ M.M. McKerns, L. Strand, T. Sullivan, A. Fang, M.A.G. Aivazis,
165
+ "Building a framework for predictive science", Proceedings of
166
+ the 10th Python in Science Conference, 2011;
167
+ http://arxiv.org/pdf/1202.1056
168
+
169
+ Michael McKerns and Michael Aivazis,
170
+ "pathos: a framework for heterogeneous computing", 2010- ;
171
+ https://uqfoundation.github.io/project/pathos
172
+
173
+ Please see https://uqfoundation.github.io/project/pathos or
174
+ http://arxiv.org/pdf/1202.1056 for further information.
175
+
176
+ '''
177
+
178
+ __all__ = []
179
+ __version__ = '0.70.15'
180
+ __author__ = 'Mike McKerns'
181
+
182
+ __license__ = '''
183
+ Copyright (c) 2008-2016 California Institute of Technology.
184
+ Copyright (c) 2016-2023 The Uncertainty Quantification Foundation.
185
+ All rights reserved.
186
+
187
+ This software forks the python package "multiprocessing". Licence and
188
+ copyright information for multiprocessing can be found in "COPYING".
189
+
190
+ This software is available subject to the conditions and terms laid
191
+ out below. By downloading and using this software you are agreeing
192
+ to the following conditions.
193
+
194
+ Redistribution and use in source and binary forms, with or without
195
+ modification, are permitted provided that the following conditions
196
+ are met:
197
+
198
+ - Redistributions of source code must retain the above copyright
199
+ notice, this list of conditions and the following disclaimer.
200
+
201
+ - Redistributions in binary form must reproduce the above copyright
202
+ notice, this list of conditions and the following disclaimer in the
203
+ documentation and/or other materials provided with the distribution.
204
+
205
+ - Neither the names of the copyright holders nor the names of any of
206
+ the contributors may be used to endorse or promote products derived
207
+ from this software without specific prior written permission.
208
+
209
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
210
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
212
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
213
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
214
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
216
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
217
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
218
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
219
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
220
+
221
+ '''
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/__init__.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Package analogous to 'threading.py' but using processes
3
+ #
4
+ # multiprocessing/__init__.py
5
+ #
6
+ # This package is intended to duplicate the functionality (and much of
7
+ # the API) of threading.py but uses processes instead of threads. A
8
+ # subpackage 'multiprocessing.dummy' has the same API but is a simple
9
+ # wrapper for 'threading'.
10
+ #
11
+ # Original: Copyright (c) 2006-2008, R Oudkerk
12
+ # Original: Licensed to PSF under a Contributor Agreement.
13
+ # Forked by Mike McKerns, to support enhanced serialization.
14
+
15
+ # author, version, license, and long description
16
+ try: # the package is installed
17
+ from .__info__ import __version__, __author__, __doc__, __license__
18
+ except: # pragma: no cover
19
+ import os
20
+ import sys
21
+ root = os.path.dirname(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
22
+ sys.path.append(root)
23
+ # get distribution meta info
24
+ from version import (__version__, __author__,
25
+ get_license_text, get_readme_as_rst)
26
+ __license__ = get_license_text(os.path.join(root, 'LICENSE'))
27
+ __license__ = "\n%s" % __license__
28
+ __doc__ = get_readme_as_rst(os.path.join(root, 'README.md'))
29
+ del os, sys, root, get_license_text, get_readme_as_rst
30
+
31
+
32
+ import sys
33
+ from . import context
34
+
35
+ #
36
+ # Copy stuff from default context
37
+ #
38
+
39
+ __all__ = [x for x in dir(context._default_context) if not x.startswith('_')]
40
+ globals().update((name, getattr(context._default_context, name)) for name in __all__)
41
+
42
+ #
43
+ # XXX These should not really be documented or public.
44
+ #
45
+
46
+ SUBDEBUG = 5
47
+ SUBWARNING = 25
48
+
49
+ #
50
+ # Alias for main module -- will be reset by bootstrapping child processes
51
+ #
52
+
53
+ if '__main__' in sys.modules:
54
+ sys.modules['__mp_main__'] = sys.modules['__main__']
55
+
56
+
57
+ def license():
58
+ """print license"""
59
+ print (__license__)
60
+ return
61
+
62
+ def citation():
63
+ """print citation"""
64
+ print (__doc__[-491:-118])
65
+ return
66
+
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/connection.py ADDED
@@ -0,0 +1,975 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # A higher level module for using sockets (or Windows named pipes)
3
+ #
4
+ # multiprocessing/connection.py
5
+ #
6
+ # Copyright (c) 2006-2008, R Oudkerk
7
+ # Licensed to PSF under a Contributor Agreement.
8
+ #
9
+
10
+ __all__ = [ 'Client', 'Listener', 'Pipe', 'wait' ]
11
+
12
+ import io
13
+ import os
14
+ import sys
15
+ import socket
16
+ import struct
17
+ import time
18
+ import tempfile
19
+ import itertools
20
+
21
+ try:
22
+ import _multiprocess as _multiprocessing
23
+ except ImportError:
24
+ import _multiprocessing
25
+
26
+ from . import util
27
+
28
+ from . import AuthenticationError, BufferTooShort
29
+ from .context import reduction
30
+ _ForkingPickler = reduction.ForkingPickler
31
+
32
+ try:
33
+ import _winapi
34
+ from _winapi import WAIT_OBJECT_0, WAIT_ABANDONED_0, WAIT_TIMEOUT, INFINITE
35
+ except ImportError:
36
+ if sys.platform == 'win32':
37
+ raise
38
+ _winapi = None
39
+
40
+ #
41
+ #
42
+ #
43
+
44
+ BUFSIZE = 8192
45
+ # A very generous timeout when it comes to local connections...
46
+ CONNECTION_TIMEOUT = 20.
47
+
48
+ _mmap_counter = itertools.count()
49
+
50
+ default_family = 'AF_INET'
51
+ families = ['AF_INET']
52
+
53
+ if hasattr(socket, 'AF_UNIX'):
54
+ default_family = 'AF_UNIX'
55
+ families += ['AF_UNIX']
56
+
57
+ if sys.platform == 'win32':
58
+ default_family = 'AF_PIPE'
59
+ families += ['AF_PIPE']
60
+
61
+
62
+ def _init_timeout(timeout=CONNECTION_TIMEOUT):
63
+ return getattr(time,'monotonic',time.time)() + timeout
64
+
65
+ def _check_timeout(t):
66
+ return getattr(time,'monotonic',time.time)() > t
67
+
68
+ #
69
+ #
70
+ #
71
+
72
+ def arbitrary_address(family):
73
+ '''
74
+ Return an arbitrary free address for the given family
75
+ '''
76
+ if family == 'AF_INET':
77
+ return ('localhost', 0)
78
+ elif family == 'AF_UNIX':
79
+ return tempfile.mktemp(prefix='listener-', dir=util.get_temp_dir())
80
+ elif family == 'AF_PIPE':
81
+ return tempfile.mktemp(prefix=r'\\.\pipe\pyc-%d-%d-' %
82
+ (os.getpid(), next(_mmap_counter)), dir="")
83
+ else:
84
+ raise ValueError('unrecognized family')
85
+
86
+ def _validate_family(family):
87
+ '''
88
+ Checks if the family is valid for the current environment.
89
+ '''
90
+ if sys.platform != 'win32' and family == 'AF_PIPE':
91
+ raise ValueError('Family %s is not recognized.' % family)
92
+
93
+ if sys.platform == 'win32' and family == 'AF_UNIX':
94
+ # double check
95
+ if not hasattr(socket, family):
96
+ raise ValueError('Family %s is not recognized.' % family)
97
+
98
+ def address_type(address):
99
+ '''
100
+ Return the types of the address
101
+
102
+ This can be 'AF_INET', 'AF_UNIX', or 'AF_PIPE'
103
+ '''
104
+ if type(address) == tuple:
105
+ return 'AF_INET'
106
+ elif type(address) is str and address.startswith('\\\\'):
107
+ return 'AF_PIPE'
108
+ elif type(address) is str or util.is_abstract_socket_namespace(address):
109
+ return 'AF_UNIX'
110
+ else:
111
+ raise ValueError('address type of %r unrecognized' % address)
112
+
113
+ #
114
+ # Connection classes
115
+ #
116
+
117
+ class _ConnectionBase:
118
+ _handle = None
119
+
120
+ def __init__(self, handle, readable=True, writable=True):
121
+ handle = handle.__index__()
122
+ if handle < 0:
123
+ raise ValueError("invalid handle")
124
+ if not readable and not writable:
125
+ raise ValueError(
126
+ "at least one of `readable` and `writable` must be True")
127
+ self._handle = handle
128
+ self._readable = readable
129
+ self._writable = writable
130
+
131
+ # XXX should we use util.Finalize instead of a __del__?
132
+
133
+ def __del__(self):
134
+ if self._handle is not None:
135
+ self._close()
136
+
137
+ def _check_closed(self):
138
+ if self._handle is None:
139
+ raise OSError("handle is closed")
140
+
141
+ def _check_readable(self):
142
+ if not self._readable:
143
+ raise OSError("connection is write-only")
144
+
145
+ def _check_writable(self):
146
+ if not self._writable:
147
+ raise OSError("connection is read-only")
148
+
149
+ def _bad_message_length(self):
150
+ if self._writable:
151
+ self._readable = False
152
+ else:
153
+ self.close()
154
+ raise OSError("bad message length")
155
+
156
+ @property
157
+ def closed(self):
158
+ """True if the connection is closed"""
159
+ return self._handle is None
160
+
161
+ @property
162
+ def readable(self):
163
+ """True if the connection is readable"""
164
+ return self._readable
165
+
166
+ @property
167
+ def writable(self):
168
+ """True if the connection is writable"""
169
+ return self._writable
170
+
171
+ def fileno(self):
172
+ """File descriptor or handle of the connection"""
173
+ self._check_closed()
174
+ return self._handle
175
+
176
+ def close(self):
177
+ """Close the connection"""
178
+ if self._handle is not None:
179
+ try:
180
+ self._close()
181
+ finally:
182
+ self._handle = None
183
+
184
+ def send_bytes(self, buf, offset=0, size=None):
185
+ """Send the bytes data from a bytes-like object"""
186
+ self._check_closed()
187
+ self._check_writable()
188
+ m = memoryview(buf)
189
+ if m.itemsize > 1:
190
+ m = m.cast('B')
191
+ n = m.nbytes
192
+ if offset < 0:
193
+ raise ValueError("offset is negative")
194
+ if n < offset:
195
+ raise ValueError("buffer length < offset")
196
+ if size is None:
197
+ size = n - offset
198
+ elif size < 0:
199
+ raise ValueError("size is negative")
200
+ elif offset + size > n:
201
+ raise ValueError("buffer length < offset + size")
202
+ self._send_bytes(m[offset:offset + size])
203
+
204
+ def send(self, obj):
205
+ """Send a (picklable) object"""
206
+ self._check_closed()
207
+ self._check_writable()
208
+ self._send_bytes(_ForkingPickler.dumps(obj))
209
+
210
+ def recv_bytes(self, maxlength=None):
211
+ """
212
+ Receive bytes data as a bytes object.
213
+ """
214
+ self._check_closed()
215
+ self._check_readable()
216
+ if maxlength is not None and maxlength < 0:
217
+ raise ValueError("negative maxlength")
218
+ buf = self._recv_bytes(maxlength)
219
+ if buf is None:
220
+ self._bad_message_length()
221
+ return buf.getvalue()
222
+
223
+ def recv_bytes_into(self, buf, offset=0):
224
+ """
225
+ Receive bytes data into a writeable bytes-like object.
226
+ Return the number of bytes read.
227
+ """
228
+ self._check_closed()
229
+ self._check_readable()
230
+ with memoryview(buf) as m:
231
+ # Get bytesize of arbitrary buffer
232
+ itemsize = m.itemsize
233
+ bytesize = itemsize * len(m)
234
+ if offset < 0:
235
+ raise ValueError("negative offset")
236
+ elif offset > bytesize:
237
+ raise ValueError("offset too large")
238
+ result = self._recv_bytes()
239
+ size = result.tell()
240
+ if bytesize < offset + size:
241
+ raise BufferTooShort(result.getvalue())
242
+ # Message can fit in dest
243
+ result.seek(0)
244
+ result.readinto(m[offset // itemsize :
245
+ (offset + size) // itemsize])
246
+ return size
247
+
248
+ def recv(self):
249
+ """Receive a (picklable) object"""
250
+ self._check_closed()
251
+ self._check_readable()
252
+ buf = self._recv_bytes()
253
+ return _ForkingPickler.loads(buf.getbuffer())
254
+
255
+ def poll(self, timeout=0.0):
256
+ """Whether there is any input available to be read"""
257
+ self._check_closed()
258
+ self._check_readable()
259
+ return self._poll(timeout)
260
+
261
+ def __enter__(self):
262
+ return self
263
+
264
+ def __exit__(self, exc_type, exc_value, exc_tb):
265
+ self.close()
266
+
267
+
268
+ if _winapi:
269
+
270
+ class PipeConnection(_ConnectionBase):
271
+ """
272
+ Connection class based on a Windows named pipe.
273
+ Overlapped I/O is used, so the handles must have been created
274
+ with FILE_FLAG_OVERLAPPED.
275
+ """
276
+ _got_empty_message = False
277
+
278
+ def _close(self, _CloseHandle=_winapi.CloseHandle):
279
+ _CloseHandle(self._handle)
280
+
281
+ def _send_bytes(self, buf):
282
+ ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True)
283
+ try:
284
+ if err == _winapi.ERROR_IO_PENDING:
285
+ waitres = _winapi.WaitForMultipleObjects(
286
+ [ov.event], False, INFINITE)
287
+ assert waitres == WAIT_OBJECT_0
288
+ except:
289
+ ov.cancel()
290
+ raise
291
+ finally:
292
+ nwritten, err = ov.GetOverlappedResult(True)
293
+ assert err == 0
294
+ assert nwritten == len(buf)
295
+
296
+ def _recv_bytes(self, maxsize=None):
297
+ if self._got_empty_message:
298
+ self._got_empty_message = False
299
+ return io.BytesIO()
300
+ else:
301
+ bsize = 128 if maxsize is None else min(maxsize, 128)
302
+ try:
303
+ ov, err = _winapi.ReadFile(self._handle, bsize,
304
+ overlapped=True)
305
+ try:
306
+ if err == _winapi.ERROR_IO_PENDING:
307
+ waitres = _winapi.WaitForMultipleObjects(
308
+ [ov.event], False, INFINITE)
309
+ assert waitres == WAIT_OBJECT_0
310
+ except:
311
+ ov.cancel()
312
+ raise
313
+ finally:
314
+ nread, err = ov.GetOverlappedResult(True)
315
+ if err == 0:
316
+ f = io.BytesIO()
317
+ f.write(ov.getbuffer())
318
+ return f
319
+ elif err == _winapi.ERROR_MORE_DATA:
320
+ return self._get_more_data(ov, maxsize)
321
+ except OSError as e:
322
+ if e.winerror == _winapi.ERROR_BROKEN_PIPE:
323
+ raise EOFError
324
+ else:
325
+ raise
326
+ raise RuntimeError("shouldn't get here; expected KeyboardInterrupt")
327
+
328
+ def _poll(self, timeout):
329
+ if (self._got_empty_message or
330
+ _winapi.PeekNamedPipe(self._handle)[0] != 0):
331
+ return True
332
+ return bool(wait([self], timeout))
333
+
334
+ def _get_more_data(self, ov, maxsize):
335
+ buf = ov.getbuffer()
336
+ f = io.BytesIO()
337
+ f.write(buf)
338
+ left = _winapi.PeekNamedPipe(self._handle)[1]
339
+ assert left > 0
340
+ if maxsize is not None and len(buf) + left > maxsize:
341
+ self._bad_message_length()
342
+ ov, err = _winapi.ReadFile(self._handle, left, overlapped=True)
343
+ rbytes, err = ov.GetOverlappedResult(True)
344
+ assert err == 0
345
+ assert rbytes == left
346
+ f.write(ov.getbuffer())
347
+ return f
348
+
349
+
350
+ class Connection(_ConnectionBase):
351
+ """
352
+ Connection class based on an arbitrary file descriptor (Unix only), or
353
+ a socket handle (Windows).
354
+ """
355
+
356
+ if _winapi:
357
+ def _close(self, _close=_multiprocessing.closesocket):
358
+ _close(self._handle)
359
+ _write = _multiprocessing.send
360
+ _read = _multiprocessing.recv
361
+ else:
362
+ def _close(self, _close=os.close):
363
+ _close(self._handle)
364
+ _write = os.write
365
+ _read = os.read
366
+
367
+ def _send(self, buf, write=_write):
368
+ remaining = len(buf)
369
+ while True:
370
+ n = write(self._handle, buf)
371
+ remaining -= n
372
+ if remaining == 0:
373
+ break
374
+ buf = buf[n:]
375
+
376
+ def _recv(self, size, read=_read):
377
+ buf = io.BytesIO()
378
+ handle = self._handle
379
+ remaining = size
380
+ while remaining > 0:
381
+ chunk = read(handle, remaining)
382
+ n = len(chunk)
383
+ if n == 0:
384
+ if remaining == size:
385
+ raise EOFError
386
+ else:
387
+ raise OSError("got end of file during message")
388
+ buf.write(chunk)
389
+ remaining -= n
390
+ return buf
391
+
392
+ def _send_bytes(self, buf):
393
+ n = len(buf)
394
+ if n > 0x7fffffff:
395
+ pre_header = struct.pack("!i", -1)
396
+ header = struct.pack("!Q", n)
397
+ self._send(pre_header)
398
+ self._send(header)
399
+ self._send(buf)
400
+ else:
401
+ # For wire compatibility with 3.7 and lower
402
+ header = struct.pack("!i", n)
403
+ if n > 16384:
404
+ # The payload is large so Nagle's algorithm won't be triggered
405
+ # and we'd better avoid the cost of concatenation.
406
+ self._send(header)
407
+ self._send(buf)
408
+ else:
409
+ # Issue #20540: concatenate before sending, to avoid delays due
410
+ # to Nagle's algorithm on a TCP socket.
411
+ # Also note we want to avoid sending a 0-length buffer separately,
412
+ # to avoid "broken pipe" errors if the other end closed the pipe.
413
+ self._send(header + buf)
414
+
415
+ def _recv_bytes(self, maxsize=None):
416
+ buf = self._recv(4)
417
+ size, = struct.unpack("!i", buf.getvalue())
418
+ if size == -1:
419
+ buf = self._recv(8)
420
+ size, = struct.unpack("!Q", buf.getvalue())
421
+ if maxsize is not None and size > maxsize:
422
+ return None
423
+ return self._recv(size)
424
+
425
+ def _poll(self, timeout):
426
+ r = wait([self], timeout)
427
+ return bool(r)
428
+
429
+
430
+ #
431
+ # Public functions
432
+ #
433
+
434
+ class Listener(object):
435
+ '''
436
+ Returns a listener object.
437
+
438
+ This is a wrapper for a bound socket which is 'listening' for
439
+ connections, or for a Windows named pipe.
440
+ '''
441
+ def __init__(self, address=None, family=None, backlog=1, authkey=None):
442
+ family = family or (address and address_type(address)) \
443
+ or default_family
444
+ address = address or arbitrary_address(family)
445
+
446
+ _validate_family(family)
447
+ if family == 'AF_PIPE':
448
+ self._listener = PipeListener(address, backlog)
449
+ else:
450
+ self._listener = SocketListener(address, family, backlog)
451
+
452
+ if authkey is not None and not isinstance(authkey, bytes):
453
+ raise TypeError('authkey should be a byte string')
454
+
455
+ self._authkey = authkey
456
+
457
+ def accept(self):
458
+ '''
459
+ Accept a connection on the bound socket or named pipe of `self`.
460
+
461
+ Returns a `Connection` object.
462
+ '''
463
+ if self._listener is None:
464
+ raise OSError('listener is closed')
465
+ c = self._listener.accept()
466
+ if self._authkey:
467
+ deliver_challenge(c, self._authkey)
468
+ answer_challenge(c, self._authkey)
469
+ return c
470
+
471
+ def close(self):
472
+ '''
473
+ Close the bound socket or named pipe of `self`.
474
+ '''
475
+ listener = self._listener
476
+ if listener is not None:
477
+ self._listener = None
478
+ listener.close()
479
+
480
+ @property
481
+ def address(self):
482
+ return self._listener._address
483
+
484
+ @property
485
+ def last_accepted(self):
486
+ return self._listener._last_accepted
487
+
488
+ def __enter__(self):
489
+ return self
490
+
491
+ def __exit__(self, exc_type, exc_value, exc_tb):
492
+ self.close()
493
+
494
+
495
+ def Client(address, family=None, authkey=None):
496
+ '''
497
+ Returns a connection to the address of a `Listener`
498
+ '''
499
+ family = family or address_type(address)
500
+ _validate_family(family)
501
+ if family == 'AF_PIPE':
502
+ c = PipeClient(address)
503
+ else:
504
+ c = SocketClient(address)
505
+
506
+ if authkey is not None and not isinstance(authkey, bytes):
507
+ raise TypeError('authkey should be a byte string')
508
+
509
+ if authkey is not None:
510
+ answer_challenge(c, authkey)
511
+ deliver_challenge(c, authkey)
512
+
513
+ return c
514
+
515
+
516
+ if sys.platform != 'win32':
517
+
518
+ def Pipe(duplex=True):
519
+ '''
520
+ Returns pair of connection objects at either end of a pipe
521
+ '''
522
+ if duplex:
523
+ s1, s2 = socket.socketpair()
524
+ s1.setblocking(True)
525
+ s2.setblocking(True)
526
+ c1 = Connection(s1.detach())
527
+ c2 = Connection(s2.detach())
528
+ else:
529
+ fd1, fd2 = os.pipe()
530
+ c1 = Connection(fd1, writable=False)
531
+ c2 = Connection(fd2, readable=False)
532
+
533
+ return c1, c2
534
+
535
+ else:
536
+
537
+ def Pipe(duplex=True):
538
+ '''
539
+ Returns pair of connection objects at either end of a pipe
540
+ '''
541
+ address = arbitrary_address('AF_PIPE')
542
+ if duplex:
543
+ openmode = _winapi.PIPE_ACCESS_DUPLEX
544
+ access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE
545
+ obsize, ibsize = BUFSIZE, BUFSIZE
546
+ else:
547
+ openmode = _winapi.PIPE_ACCESS_INBOUND
548
+ access = _winapi.GENERIC_WRITE
549
+ obsize, ibsize = 0, BUFSIZE
550
+
551
+ h1 = _winapi.CreateNamedPipe(
552
+ address, openmode | _winapi.FILE_FLAG_OVERLAPPED |
553
+ _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE,
554
+ _winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
555
+ _winapi.PIPE_WAIT,
556
+ 1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER,
557
+ # default security descriptor: the handle cannot be inherited
558
+ _winapi.NULL
559
+ )
560
+ h2 = _winapi.CreateFile(
561
+ address, access, 0, _winapi.NULL, _winapi.OPEN_EXISTING,
562
+ _winapi.FILE_FLAG_OVERLAPPED, _winapi.NULL
563
+ )
564
+ _winapi.SetNamedPipeHandleState(
565
+ h2, _winapi.PIPE_READMODE_MESSAGE, None, None
566
+ )
567
+
568
+ overlapped = _winapi.ConnectNamedPipe(h1, overlapped=True)
569
+ _, err = overlapped.GetOverlappedResult(True)
570
+ assert err == 0
571
+
572
+ c1 = PipeConnection(h1, writable=duplex)
573
+ c2 = PipeConnection(h2, readable=duplex)
574
+
575
+ return c1, c2
576
+
577
+ #
578
+ # Definitions for connections based on sockets
579
+ #
580
+
581
+ class SocketListener(object):
582
+ '''
583
+ Representation of a socket which is bound to an address and listening
584
+ '''
585
+ def __init__(self, address, family, backlog=1):
586
+ self._socket = socket.socket(getattr(socket, family))
587
+ try:
588
+ # SO_REUSEADDR has different semantics on Windows (issue #2550).
589
+ if os.name == 'posix':
590
+ self._socket.setsockopt(socket.SOL_SOCKET,
591
+ socket.SO_REUSEADDR, 1)
592
+ self._socket.setblocking(True)
593
+ self._socket.bind(address)
594
+ self._socket.listen(backlog)
595
+ self._address = self._socket.getsockname()
596
+ except OSError:
597
+ self._socket.close()
598
+ raise
599
+ self._family = family
600
+ self._last_accepted = None
601
+
602
+ if family == 'AF_UNIX' and not util.is_abstract_socket_namespace(address):
603
+ # Linux abstract socket namespaces do not need to be explicitly unlinked
604
+ self._unlink = util.Finalize(
605
+ self, os.unlink, args=(address,), exitpriority=0
606
+ )
607
+ else:
608
+ self._unlink = None
609
+
610
+ def accept(self):
611
+ s, self._last_accepted = self._socket.accept()
612
+ s.setblocking(True)
613
+ return Connection(s.detach())
614
+
615
+ def close(self):
616
+ try:
617
+ self._socket.close()
618
+ finally:
619
+ unlink = self._unlink
620
+ if unlink is not None:
621
+ self._unlink = None
622
+ unlink()
623
+
624
+
625
+ def SocketClient(address):
626
+ '''
627
+ Return a connection object connected to the socket given by `address`
628
+ '''
629
+ family = address_type(address)
630
+ with socket.socket( getattr(socket, family) ) as s:
631
+ s.setblocking(True)
632
+ s.connect(address)
633
+ return Connection(s.detach())
634
+
635
+ #
636
+ # Definitions for connections based on named pipes
637
+ #
638
+
639
+ if sys.platform == 'win32':
640
+
641
+ class PipeListener(object):
642
+ '''
643
+ Representation of a named pipe
644
+ '''
645
+ def __init__(self, address, backlog=None):
646
+ self._address = address
647
+ self._handle_queue = [self._new_handle(first=True)]
648
+
649
+ self._last_accepted = None
650
+ util.sub_debug('listener created with address=%r', self._address)
651
+ self.close = util.Finalize(
652
+ self, PipeListener._finalize_pipe_listener,
653
+ args=(self._handle_queue, self._address), exitpriority=0
654
+ )
655
+
656
+ def _new_handle(self, first=False):
657
+ flags = _winapi.PIPE_ACCESS_DUPLEX | _winapi.FILE_FLAG_OVERLAPPED
658
+ if first:
659
+ flags |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE
660
+ return _winapi.CreateNamedPipe(
661
+ self._address, flags,
662
+ _winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
663
+ _winapi.PIPE_WAIT,
664
+ _winapi.PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE,
665
+ _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL
666
+ )
667
+
668
+ def accept(self):
669
+ self._handle_queue.append(self._new_handle())
670
+ handle = self._handle_queue.pop(0)
671
+ try:
672
+ ov = _winapi.ConnectNamedPipe(handle, overlapped=True)
673
+ except OSError as e:
674
+ if e.winerror != _winapi.ERROR_NO_DATA:
675
+ raise
676
+ # ERROR_NO_DATA can occur if a client has already connected,
677
+ # written data and then disconnected -- see Issue 14725.
678
+ else:
679
+ try:
680
+ res = _winapi.WaitForMultipleObjects(
681
+ [ov.event], False, INFINITE)
682
+ except:
683
+ ov.cancel()
684
+ _winapi.CloseHandle(handle)
685
+ raise
686
+ finally:
687
+ _, err = ov.GetOverlappedResult(True)
688
+ assert err == 0
689
+ return PipeConnection(handle)
690
+
691
+ @staticmethod
692
+ def _finalize_pipe_listener(queue, address):
693
+ util.sub_debug('closing listener with address=%r', address)
694
+ for handle in queue:
695
+ _winapi.CloseHandle(handle)
696
+
697
+ def PipeClient(address):
698
+ '''
699
+ Return a connection object connected to the pipe given by `address`
700
+ '''
701
+ t = _init_timeout()
702
+ while 1:
703
+ try:
704
+ _winapi.WaitNamedPipe(address, 1000)
705
+ h = _winapi.CreateFile(
706
+ address, _winapi.GENERIC_READ | _winapi.GENERIC_WRITE,
707
+ 0, _winapi.NULL, _winapi.OPEN_EXISTING,
708
+ _winapi.FILE_FLAG_OVERLAPPED, _winapi.NULL
709
+ )
710
+ except OSError as e:
711
+ if e.winerror not in (_winapi.ERROR_SEM_TIMEOUT,
712
+ _winapi.ERROR_PIPE_BUSY) or _check_timeout(t):
713
+ raise
714
+ else:
715
+ break
716
+ else:
717
+ raise
718
+
719
+ _winapi.SetNamedPipeHandleState(
720
+ h, _winapi.PIPE_READMODE_MESSAGE, None, None
721
+ )
722
+ return PipeConnection(h)
723
+
724
+ #
725
+ # Authentication stuff
726
+ #
727
+
728
+ MESSAGE_LENGTH = 20
729
+
730
+ CHALLENGE = b'#CHALLENGE#'
731
+ WELCOME = b'#WELCOME#'
732
+ FAILURE = b'#FAILURE#'
733
+
734
+ def deliver_challenge(connection, authkey):
735
+ import hmac
736
+ if not isinstance(authkey, bytes):
737
+ raise ValueError(
738
+ "Authkey must be bytes, not {0!s}".format(type(authkey)))
739
+ message = os.urandom(MESSAGE_LENGTH)
740
+ connection.send_bytes(CHALLENGE + message)
741
+ digest = hmac.new(authkey, message, 'md5').digest()
742
+ response = connection.recv_bytes(256) # reject large message
743
+ if response == digest:
744
+ connection.send_bytes(WELCOME)
745
+ else:
746
+ connection.send_bytes(FAILURE)
747
+ raise AuthenticationError('digest received was wrong')
748
+
749
+ def answer_challenge(connection, authkey):
750
+ import hmac
751
+ if not isinstance(authkey, bytes):
752
+ raise ValueError(
753
+ "Authkey must be bytes, not {0!s}".format(type(authkey)))
754
+ message = connection.recv_bytes(256) # reject large message
755
+ assert message[:len(CHALLENGE)] == CHALLENGE, 'message = %r' % message
756
+ message = message[len(CHALLENGE):]
757
+ digest = hmac.new(authkey, message, 'md5').digest()
758
+ connection.send_bytes(digest)
759
+ response = connection.recv_bytes(256) # reject large message
760
+ if response != WELCOME:
761
+ raise AuthenticationError('digest sent was rejected')
762
+
763
+ #
764
+ # Support for using xmlrpclib for serialization
765
+ #
766
+
767
+ class ConnectionWrapper(object):
768
+ def __init__(self, conn, dumps, loads):
769
+ self._conn = conn
770
+ self._dumps = dumps
771
+ self._loads = loads
772
+ for attr in ('fileno', 'close', 'poll', 'recv_bytes', 'send_bytes'):
773
+ obj = getattr(conn, attr)
774
+ setattr(self, attr, obj)
775
+ def send(self, obj):
776
+ s = self._dumps(obj)
777
+ self._conn.send_bytes(s)
778
+ def recv(self):
779
+ s = self._conn.recv_bytes()
780
+ return self._loads(s)
781
+
782
+ def _xml_dumps(obj):
783
+ return xmlrpclib.dumps((obj,), None, None, None, 1).encode('utf-8')
784
+
785
+ def _xml_loads(s):
786
+ (obj,), method = xmlrpclib.loads(s.decode('utf-8'))
787
+ return obj
788
+
789
+ class XmlListener(Listener):
790
+ def accept(self):
791
+ global xmlrpclib
792
+ import xmlrpc.client as xmlrpclib
793
+ obj = Listener.accept(self)
794
+ return ConnectionWrapper(obj, _xml_dumps, _xml_loads)
795
+
796
+ def XmlClient(*args, **kwds):
797
+ global xmlrpclib
798
+ import xmlrpc.client as xmlrpclib
799
+ return ConnectionWrapper(Client(*args, **kwds), _xml_dumps, _xml_loads)
800
+
801
+ #
802
+ # Wait
803
+ #
804
+
805
+ if sys.platform == 'win32':
806
+
807
+ def _exhaustive_wait(handles, timeout):
808
+ # Return ALL handles which are currently signalled. (Only
809
+ # returning the first signalled might create starvation issues.)
810
+ L = list(handles)
811
+ ready = []
812
+ while L:
813
+ res = _winapi.WaitForMultipleObjects(L, False, timeout)
814
+ if res == WAIT_TIMEOUT:
815
+ break
816
+ elif WAIT_OBJECT_0 <= res < WAIT_OBJECT_0 + len(L):
817
+ res -= WAIT_OBJECT_0
818
+ elif WAIT_ABANDONED_0 <= res < WAIT_ABANDONED_0 + len(L):
819
+ res -= WAIT_ABANDONED_0
820
+ else:
821
+ raise RuntimeError('Should not get here')
822
+ ready.append(L[res])
823
+ L = L[res+1:]
824
+ timeout = 0
825
+ return ready
826
+
827
+ _ready_errors = {_winapi.ERROR_BROKEN_PIPE, _winapi.ERROR_NETNAME_DELETED}
828
+
829
+ def wait(object_list, timeout=None):
830
+ '''
831
+ Wait till an object in object_list is ready/readable.
832
+
833
+ Returns list of those objects in object_list which are ready/readable.
834
+ '''
835
+ if timeout is None:
836
+ timeout = INFINITE
837
+ elif timeout < 0:
838
+ timeout = 0
839
+ else:
840
+ timeout = int(timeout * 1000 + 0.5)
841
+
842
+ object_list = list(object_list)
843
+ waithandle_to_obj = {}
844
+ ov_list = []
845
+ ready_objects = set()
846
+ ready_handles = set()
847
+
848
+ try:
849
+ for o in object_list:
850
+ try:
851
+ fileno = getattr(o, 'fileno')
852
+ except AttributeError:
853
+ waithandle_to_obj[o.__index__()] = o
854
+ else:
855
+ # start an overlapped read of length zero
856
+ try:
857
+ ov, err = _winapi.ReadFile(fileno(), 0, True)
858
+ except OSError as e:
859
+ ov, err = None, e.winerror
860
+ if err not in _ready_errors:
861
+ raise
862
+ if err == _winapi.ERROR_IO_PENDING:
863
+ ov_list.append(ov)
864
+ waithandle_to_obj[ov.event] = o
865
+ else:
866
+ # If o.fileno() is an overlapped pipe handle and
867
+ # err == 0 then there is a zero length message
868
+ # in the pipe, but it HAS NOT been consumed...
869
+ if ov and sys.getwindowsversion()[:2] >= (6, 2):
870
+ # ... except on Windows 8 and later, where
871
+ # the message HAS been consumed.
872
+ try:
873
+ _, err = ov.GetOverlappedResult(False)
874
+ except OSError as e:
875
+ err = e.winerror
876
+ if not err and hasattr(o, '_got_empty_message'):
877
+ o._got_empty_message = True
878
+ ready_objects.add(o)
879
+ timeout = 0
880
+
881
+ ready_handles = _exhaustive_wait(waithandle_to_obj.keys(), timeout)
882
+ finally:
883
+ # request that overlapped reads stop
884
+ for ov in ov_list:
885
+ ov.cancel()
886
+
887
+ # wait for all overlapped reads to stop
888
+ for ov in ov_list:
889
+ try:
890
+ _, err = ov.GetOverlappedResult(True)
891
+ except OSError as e:
892
+ err = e.winerror
893
+ if err not in _ready_errors:
894
+ raise
895
+ if err != _winapi.ERROR_OPERATION_ABORTED:
896
+ o = waithandle_to_obj[ov.event]
897
+ ready_objects.add(o)
898
+ if err == 0:
899
+ # If o.fileno() is an overlapped pipe handle then
900
+ # a zero length message HAS been consumed.
901
+ if hasattr(o, '_got_empty_message'):
902
+ o._got_empty_message = True
903
+
904
+ ready_objects.update(waithandle_to_obj[h] for h in ready_handles)
905
+ return [o for o in object_list if o in ready_objects]
906
+
907
+ else:
908
+
909
+ import selectors
910
+
911
+ # poll/select have the advantage of not requiring any extra file
912
+ # descriptor, contrarily to epoll/kqueue (also, they require a single
913
+ # syscall).
914
+ if hasattr(selectors, 'PollSelector'):
915
+ _WaitSelector = selectors.PollSelector
916
+ else:
917
+ _WaitSelector = selectors.SelectSelector
918
+
919
+ def wait(object_list, timeout=None):
920
+ '''
921
+ Wait till an object in object_list is ready/readable.
922
+
923
+ Returns list of those objects in object_list which are ready/readable.
924
+ '''
925
+ with _WaitSelector() as selector:
926
+ for obj in object_list:
927
+ selector.register(obj, selectors.EVENT_READ)
928
+
929
+ if timeout is not None:
930
+ deadline = getattr(time,'monotonic',time.time)() + timeout
931
+
932
+ while True:
933
+ ready = selector.select(timeout)
934
+ if ready:
935
+ return [key.fileobj for (key, events) in ready]
936
+ else:
937
+ if timeout is not None:
938
+ timeout = deadline - getattr(time,'monotonic',time.time)()
939
+ if timeout < 0:
940
+ return ready
941
+
942
+ #
943
+ # Make connection and socket objects shareable if possible
944
+ #
945
+
946
+ if sys.platform == 'win32':
947
+ def reduce_connection(conn):
948
+ handle = conn.fileno()
949
+ with socket.fromfd(handle, socket.AF_INET, socket.SOCK_STREAM) as s:
950
+ from . import resource_sharer
951
+ ds = resource_sharer.DupSocket(s)
952
+ return rebuild_connection, (ds, conn.readable, conn.writable)
953
+ def rebuild_connection(ds, readable, writable):
954
+ sock = ds.detach()
955
+ return Connection(sock.detach(), readable, writable)
956
+ reduction.register(Connection, reduce_connection)
957
+
958
+ def reduce_pipe_connection(conn):
959
+ access = ((_winapi.FILE_GENERIC_READ if conn.readable else 0) |
960
+ (_winapi.FILE_GENERIC_WRITE if conn.writable else 0))
961
+ dh = reduction.DupHandle(conn.fileno(), access)
962
+ return rebuild_pipe_connection, (dh, conn.readable, conn.writable)
963
+ def rebuild_pipe_connection(dh, readable, writable):
964
+ handle = dh.detach()
965
+ return PipeConnection(handle, readable, writable)
966
+ reduction.register(PipeConnection, reduce_pipe_connection)
967
+
968
+ else:
969
+ def reduce_connection(conn):
970
+ df = reduction.DupFd(conn.fileno())
971
+ return rebuild_connection, (df, conn.readable, conn.writable)
972
+ def rebuild_connection(df, readable, writable):
973
+ fd = df.detach()
974
+ return Connection(fd, readable, writable)
975
+ reduction.register(Connection, reduce_connection)
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/context.py ADDED
@@ -0,0 +1,376 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import threading
4
+
5
+ from . import process
6
+ from . import reduction
7
+
8
+ __all__ = ()
9
+
10
+ #
11
+ # Exceptions
12
+ #
13
+
14
+ class ProcessError(Exception):
15
+ pass
16
+
17
+ class BufferTooShort(ProcessError):
18
+ pass
19
+
20
+ class TimeoutError(ProcessError):
21
+ pass
22
+
23
+ class AuthenticationError(ProcessError):
24
+ pass
25
+
26
+ #
27
+ # Base type for contexts. Bound methods of an instance of this type are included in __all__ of __init__.py
28
+ #
29
+
30
+ class BaseContext(object):
31
+
32
+ ProcessError = ProcessError
33
+ BufferTooShort = BufferTooShort
34
+ TimeoutError = TimeoutError
35
+ AuthenticationError = AuthenticationError
36
+
37
+ current_process = staticmethod(process.current_process)
38
+ parent_process = staticmethod(process.parent_process)
39
+ active_children = staticmethod(process.active_children)
40
+
41
+ def cpu_count(self):
42
+ '''Returns the number of CPUs in the system'''
43
+ num = os.cpu_count()
44
+ if num is None:
45
+ raise NotImplementedError('cannot determine number of cpus')
46
+ else:
47
+ return num
48
+
49
+ def Manager(self):
50
+ '''Returns a manager associated with a running server process
51
+
52
+ The managers methods such as `Lock()`, `Condition()` and `Queue()`
53
+ can be used to create shared objects.
54
+ '''
55
+ from .managers import SyncManager
56
+ m = SyncManager(ctx=self.get_context())
57
+ m.start()
58
+ return m
59
+
60
+ def Pipe(self, duplex=True):
61
+ '''Returns two connection object connected by a pipe'''
62
+ from .connection import Pipe
63
+ return Pipe(duplex)
64
+
65
+ def Lock(self):
66
+ '''Returns a non-recursive lock object'''
67
+ from .synchronize import Lock
68
+ return Lock(ctx=self.get_context())
69
+
70
+ def RLock(self):
71
+ '''Returns a recursive lock object'''
72
+ from .synchronize import RLock
73
+ return RLock(ctx=self.get_context())
74
+
75
+ def Condition(self, lock=None):
76
+ '''Returns a condition object'''
77
+ from .synchronize import Condition
78
+ return Condition(lock, ctx=self.get_context())
79
+
80
+ def Semaphore(self, value=1):
81
+ '''Returns a semaphore object'''
82
+ from .synchronize import Semaphore
83
+ return Semaphore(value, ctx=self.get_context())
84
+
85
+ def BoundedSemaphore(self, value=1):
86
+ '''Returns a bounded semaphore object'''
87
+ from .synchronize import BoundedSemaphore
88
+ return BoundedSemaphore(value, ctx=self.get_context())
89
+
90
+ def Event(self):
91
+ '''Returns an event object'''
92
+ from .synchronize import Event
93
+ return Event(ctx=self.get_context())
94
+
95
+ def Barrier(self, parties, action=None, timeout=None):
96
+ '''Returns a barrier object'''
97
+ from .synchronize import Barrier
98
+ return Barrier(parties, action, timeout, ctx=self.get_context())
99
+
100
+ def Queue(self, maxsize=0):
101
+ '''Returns a queue object'''
102
+ from .queues import Queue
103
+ return Queue(maxsize, ctx=self.get_context())
104
+
105
+ def JoinableQueue(self, maxsize=0):
106
+ '''Returns a queue object'''
107
+ from .queues import JoinableQueue
108
+ return JoinableQueue(maxsize, ctx=self.get_context())
109
+
110
+ def SimpleQueue(self):
111
+ '''Returns a queue object'''
112
+ from .queues import SimpleQueue
113
+ return SimpleQueue(ctx=self.get_context())
114
+
115
+ def Pool(self, processes=None, initializer=None, initargs=(),
116
+ maxtasksperchild=None):
117
+ '''Returns a process pool object'''
118
+ from .pool import Pool
119
+ return Pool(processes, initializer, initargs, maxtasksperchild,
120
+ context=self.get_context())
121
+
122
+ def RawValue(self, typecode_or_type, *args):
123
+ '''Returns a shared object'''
124
+ from .sharedctypes import RawValue
125
+ return RawValue(typecode_or_type, *args)
126
+
127
+ def RawArray(self, typecode_or_type, size_or_initializer):
128
+ '''Returns a shared array'''
129
+ from .sharedctypes import RawArray
130
+ return RawArray(typecode_or_type, size_or_initializer)
131
+
132
+ def Value(self, typecode_or_type, *args, lock=True):
133
+ '''Returns a synchronized shared object'''
134
+ from .sharedctypes import Value
135
+ return Value(typecode_or_type, *args, lock=lock,
136
+ ctx=self.get_context())
137
+
138
+ def Array(self, typecode_or_type, size_or_initializer, *, lock=True):
139
+ '''Returns a synchronized shared array'''
140
+ from .sharedctypes import Array
141
+ return Array(typecode_or_type, size_or_initializer, lock=lock,
142
+ ctx=self.get_context())
143
+
144
+ def freeze_support(self):
145
+ '''Check whether this is a fake forked process in a frozen executable.
146
+ If so then run code specified by commandline and exit.
147
+ '''
148
+ if sys.platform == 'win32' and getattr(sys, 'frozen', False):
149
+ from .spawn import freeze_support
150
+ freeze_support()
151
+
152
+ def get_logger(self):
153
+ '''Return package logger -- if it does not already exist then
154
+ it is created.
155
+ '''
156
+ from .util import get_logger
157
+ return get_logger()
158
+
159
+ def log_to_stderr(self, level=None):
160
+ '''Turn on logging and add a handler which prints to stderr'''
161
+ from .util import log_to_stderr
162
+ return log_to_stderr(level)
163
+
164
+ def allow_connection_pickling(self):
165
+ '''Install support for sending connections and sockets
166
+ between processes
167
+ '''
168
+ # This is undocumented. In previous versions of multiprocessing
169
+ # its only effect was to make socket objects inheritable on Windows.
170
+ from . import connection
171
+
172
+ def set_executable(self, executable):
173
+ '''Sets the path to a python.exe or pythonw.exe binary used to run
174
+ child processes instead of sys.executable when using the 'spawn'
175
+ start method. Useful for people embedding Python.
176
+ '''
177
+ from .spawn import set_executable
178
+ set_executable(executable)
179
+
180
+ def set_forkserver_preload(self, module_names):
181
+ '''Set list of module names to try to load in forkserver process.
182
+ This is really just a hint.
183
+ '''
184
+ from .forkserver import set_forkserver_preload
185
+ set_forkserver_preload(module_names)
186
+
187
+ def get_context(self, method=None):
188
+ if method is None:
189
+ return self
190
+ try:
191
+ ctx = _concrete_contexts[method]
192
+ except KeyError:
193
+ raise ValueError('cannot find context for %r' % method) from None
194
+ ctx._check_available()
195
+ return ctx
196
+
197
+ def get_start_method(self, allow_none=False):
198
+ return self._name
199
+
200
+ def set_start_method(self, method, force=False):
201
+ raise ValueError('cannot set start method of concrete context')
202
+
203
+ @property
204
+ def reducer(self):
205
+ '''Controls how objects will be reduced to a form that can be
206
+ shared with other processes.'''
207
+ return globals().get('reduction')
208
+
209
+ @reducer.setter
210
+ def reducer(self, reduction):
211
+ globals()['reduction'] = reduction
212
+
213
+ def _check_available(self):
214
+ pass
215
+
216
+ #
217
+ # Type of default context -- underlying context can be set at most once
218
+ #
219
+
220
+ class Process(process.BaseProcess):
221
+ _start_method = None
222
+ @staticmethod
223
+ def _Popen(process_obj):
224
+ return _default_context.get_context().Process._Popen(process_obj)
225
+
226
+ @staticmethod
227
+ def _after_fork():
228
+ return _default_context.get_context().Process._after_fork()
229
+
230
+ class DefaultContext(BaseContext):
231
+ Process = Process
232
+
233
+ def __init__(self, context):
234
+ self._default_context = context
235
+ self._actual_context = None
236
+
237
+ def get_context(self, method=None):
238
+ if method is None:
239
+ if self._actual_context is None:
240
+ self._actual_context = self._default_context
241
+ return self._actual_context
242
+ else:
243
+ return super().get_context(method)
244
+
245
+ def set_start_method(self, method, force=False):
246
+ if self._actual_context is not None and not force:
247
+ raise RuntimeError('context has already been set')
248
+ if method is None and force:
249
+ self._actual_context = None
250
+ return
251
+ self._actual_context = self.get_context(method)
252
+
253
+ def get_start_method(self, allow_none=False):
254
+ if self._actual_context is None:
255
+ if allow_none:
256
+ return None
257
+ self._actual_context = self._default_context
258
+ return self._actual_context._name
259
+
260
+ def get_all_start_methods(self):
261
+ if sys.platform == 'win32':
262
+ return ['spawn']
263
+ else:
264
+ methods = ['spawn', 'fork'] if sys.platform == 'darwin' else ['fork', 'spawn']
265
+ if reduction.HAVE_SEND_HANDLE:
266
+ methods.append('forkserver')
267
+ return methods
268
+
269
+
270
+ #
271
+ # Context types for fixed start method
272
+ #
273
+
274
+ if sys.platform != 'win32':
275
+
276
+ class ForkProcess(process.BaseProcess):
277
+ _start_method = 'fork'
278
+ @staticmethod
279
+ def _Popen(process_obj):
280
+ from .popen_fork import Popen
281
+ return Popen(process_obj)
282
+
283
+ class SpawnProcess(process.BaseProcess):
284
+ _start_method = 'spawn'
285
+ @staticmethod
286
+ def _Popen(process_obj):
287
+ from .popen_spawn_posix import Popen
288
+ return Popen(process_obj)
289
+
290
+ @staticmethod
291
+ def _after_fork():
292
+ # process is spawned, nothing to do
293
+ pass
294
+
295
+ class ForkServerProcess(process.BaseProcess):
296
+ _start_method = 'forkserver'
297
+ @staticmethod
298
+ def _Popen(process_obj):
299
+ from .popen_forkserver import Popen
300
+ return Popen(process_obj)
301
+
302
+ class ForkContext(BaseContext):
303
+ _name = 'fork'
304
+ Process = ForkProcess
305
+
306
+ class SpawnContext(BaseContext):
307
+ _name = 'spawn'
308
+ Process = SpawnProcess
309
+
310
+ class ForkServerContext(BaseContext):
311
+ _name = 'forkserver'
312
+ Process = ForkServerProcess
313
+ def _check_available(self):
314
+ if not reduction.HAVE_SEND_HANDLE:
315
+ raise ValueError('forkserver start method not available')
316
+
317
+ _concrete_contexts = {
318
+ 'fork': ForkContext(),
319
+ 'spawn': SpawnContext(),
320
+ 'forkserver': ForkServerContext(),
321
+ }
322
+ if sys.platform == 'darwin':
323
+ # bpo-33725: running arbitrary code after fork() is no longer reliable
324
+ # on macOS since macOS 10.14 (Mojave). Use spawn by default instead.
325
+ _default_context = DefaultContext(_concrete_contexts['fork']) #FIXME: spawn
326
+ else:
327
+ _default_context = DefaultContext(_concrete_contexts['fork'])
328
+
329
+ else:
330
+
331
+ class SpawnProcess(process.BaseProcess):
332
+ _start_method = 'spawn'
333
+ @staticmethod
334
+ def _Popen(process_obj):
335
+ from .popen_spawn_win32 import Popen
336
+ return Popen(process_obj)
337
+
338
+ @staticmethod
339
+ def _after_fork():
340
+ # process is spawned, nothing to do
341
+ pass
342
+
343
+ class SpawnContext(BaseContext):
344
+ _name = 'spawn'
345
+ Process = SpawnProcess
346
+
347
+ _concrete_contexts = {
348
+ 'spawn': SpawnContext(),
349
+ }
350
+ _default_context = DefaultContext(_concrete_contexts['spawn'])
351
+
352
+ #
353
+ # Force the start method
354
+ #
355
+
356
+ def _force_start_method(method):
357
+ _default_context._actual_context = _concrete_contexts[method]
358
+
359
+ #
360
+ # Check that the current thread is spawning a child process
361
+ #
362
+
363
+ _tls = threading.local()
364
+
365
+ def get_spawning_popen():
366
+ return getattr(_tls, 'spawning_popen', None)
367
+
368
+ def set_spawning_popen(popen):
369
+ _tls.spawning_popen = popen
370
+
371
+ def assert_spawning(obj):
372
+ if get_spawning_popen() is None:
373
+ raise RuntimeError(
374
+ '%s objects should only be shared between processes'
375
+ ' through inheritance' % type(obj).__name__
376
+ )
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/forkserver.py ADDED
@@ -0,0 +1,347 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import errno
2
+ import os
3
+ import selectors
4
+ import signal
5
+ import socket
6
+ import struct
7
+ import sys
8
+ import threading
9
+ import warnings
10
+
11
+ from . import connection
12
+ from . import process
13
+ from .context import reduction
14
+ from . import resource_tracker
15
+ from . import spawn
16
+ from . import util
17
+
18
+ __all__ = ['ensure_running', 'get_inherited_fds', 'connect_to_new_process',
19
+ 'set_forkserver_preload']
20
+
21
+ #
22
+ #
23
+ #
24
+
25
+ MAXFDS_TO_SEND = 256
26
+ SIGNED_STRUCT = struct.Struct('q') # large enough for pid_t
27
+
28
+ #
29
+ # Forkserver class
30
+ #
31
+
32
+ class ForkServer(object):
33
+
34
+ def __init__(self):
35
+ self._forkserver_address = None
36
+ self._forkserver_alive_fd = None
37
+ self._forkserver_pid = None
38
+ self._inherited_fds = None
39
+ self._lock = threading.Lock()
40
+ self._preload_modules = ['__main__']
41
+
42
+ def _stop(self):
43
+ # Method used by unit tests to stop the server
44
+ with self._lock:
45
+ self._stop_unlocked()
46
+
47
+ def _stop_unlocked(self):
48
+ if self._forkserver_pid is None:
49
+ return
50
+
51
+ # close the "alive" file descriptor asks the server to stop
52
+ os.close(self._forkserver_alive_fd)
53
+ self._forkserver_alive_fd = None
54
+
55
+ os.waitpid(self._forkserver_pid, 0)
56
+ self._forkserver_pid = None
57
+
58
+ if not util.is_abstract_socket_namespace(self._forkserver_address):
59
+ os.unlink(self._forkserver_address)
60
+ self._forkserver_address = None
61
+
62
+ def set_forkserver_preload(self, modules_names):
63
+ '''Set list of module names to try to load in forkserver process.'''
64
+ if not all(type(mod) is str for mod in self._preload_modules):
65
+ raise TypeError('module_names must be a list of strings')
66
+ self._preload_modules = modules_names
67
+
68
+ def get_inherited_fds(self):
69
+ '''Return list of fds inherited from parent process.
70
+
71
+ This returns None if the current process was not started by fork
72
+ server.
73
+ '''
74
+ return self._inherited_fds
75
+
76
+ def connect_to_new_process(self, fds):
77
+ '''Request forkserver to create a child process.
78
+
79
+ Returns a pair of fds (status_r, data_w). The calling process can read
80
+ the child process's pid and (eventually) its returncode from status_r.
81
+ The calling process should write to data_w the pickled preparation and
82
+ process data.
83
+ '''
84
+ self.ensure_running()
85
+ if len(fds) + 4 >= MAXFDS_TO_SEND:
86
+ raise ValueError('too many fds')
87
+ with socket.socket(socket.AF_UNIX) as client:
88
+ client.connect(self._forkserver_address)
89
+ parent_r, child_w = os.pipe()
90
+ child_r, parent_w = os.pipe()
91
+ allfds = [child_r, child_w, self._forkserver_alive_fd,
92
+ resource_tracker.getfd()]
93
+ allfds += fds
94
+ try:
95
+ reduction.sendfds(client, allfds)
96
+ return parent_r, parent_w
97
+ except:
98
+ os.close(parent_r)
99
+ os.close(parent_w)
100
+ raise
101
+ finally:
102
+ os.close(child_r)
103
+ os.close(child_w)
104
+
105
+ def ensure_running(self):
106
+ '''Make sure that a fork server is running.
107
+
108
+ This can be called from any process. Note that usually a child
109
+ process will just reuse the forkserver started by its parent, so
110
+ ensure_running() will do nothing.
111
+ '''
112
+ with self._lock:
113
+ resource_tracker.ensure_running()
114
+ if self._forkserver_pid is not None:
115
+ # forkserver was launched before, is it still running?
116
+ pid, status = os.waitpid(self._forkserver_pid, os.WNOHANG)
117
+ if not pid:
118
+ # still alive
119
+ return
120
+ # dead, launch it again
121
+ os.close(self._forkserver_alive_fd)
122
+ self._forkserver_address = None
123
+ self._forkserver_alive_fd = None
124
+ self._forkserver_pid = None
125
+
126
+ cmd = ('from multiprocess.forkserver import main; ' +
127
+ 'main(%d, %d, %r, **%r)')
128
+
129
+ if self._preload_modules:
130
+ desired_keys = {'main_path', 'sys_path'}
131
+ data = spawn.get_preparation_data('ignore')
132
+ data = {x: y for x, y in data.items() if x in desired_keys}
133
+ else:
134
+ data = {}
135
+
136
+ with socket.socket(socket.AF_UNIX) as listener:
137
+ address = connection.arbitrary_address('AF_UNIX')
138
+ listener.bind(address)
139
+ if not util.is_abstract_socket_namespace(address):
140
+ os.chmod(address, 0o600)
141
+ listener.listen()
142
+
143
+ # all client processes own the write end of the "alive" pipe;
144
+ # when they all terminate the read end becomes ready.
145
+ alive_r, alive_w = os.pipe()
146
+ try:
147
+ fds_to_pass = [listener.fileno(), alive_r]
148
+ cmd %= (listener.fileno(), alive_r, self._preload_modules,
149
+ data)
150
+ exe = spawn.get_executable()
151
+ args = [exe] + util._args_from_interpreter_flags()
152
+ args += ['-c', cmd]
153
+ pid = util.spawnv_passfds(exe, args, fds_to_pass)
154
+ except:
155
+ os.close(alive_w)
156
+ raise
157
+ finally:
158
+ os.close(alive_r)
159
+ self._forkserver_address = address
160
+ self._forkserver_alive_fd = alive_w
161
+ self._forkserver_pid = pid
162
+
163
+ #
164
+ #
165
+ #
166
+
167
+ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None):
168
+ '''Run forkserver.'''
169
+ if preload:
170
+ if '__main__' in preload and main_path is not None:
171
+ process.current_process()._inheriting = True
172
+ try:
173
+ spawn.import_main_path(main_path)
174
+ finally:
175
+ del process.current_process()._inheriting
176
+ for modname in preload:
177
+ try:
178
+ __import__(modname)
179
+ except ImportError:
180
+ pass
181
+
182
+ util._close_stdin()
183
+
184
+ sig_r, sig_w = os.pipe()
185
+ os.set_blocking(sig_r, False)
186
+ os.set_blocking(sig_w, False)
187
+
188
+ def sigchld_handler(*_unused):
189
+ # Dummy signal handler, doesn't do anything
190
+ pass
191
+
192
+ handlers = {
193
+ # unblocking SIGCHLD allows the wakeup fd to notify our event loop
194
+ signal.SIGCHLD: sigchld_handler,
195
+ # protect the process from ^C
196
+ signal.SIGINT: signal.SIG_IGN,
197
+ }
198
+ old_handlers = {sig: signal.signal(sig, val)
199
+ for (sig, val) in handlers.items()}
200
+
201
+ # calling os.write() in the Python signal handler is racy
202
+ signal.set_wakeup_fd(sig_w)
203
+
204
+ # map child pids to client fds
205
+ pid_to_fd = {}
206
+
207
+ with socket.socket(socket.AF_UNIX, fileno=listener_fd) as listener, \
208
+ selectors.DefaultSelector() as selector:
209
+ _forkserver._forkserver_address = listener.getsockname()
210
+
211
+ selector.register(listener, selectors.EVENT_READ)
212
+ selector.register(alive_r, selectors.EVENT_READ)
213
+ selector.register(sig_r, selectors.EVENT_READ)
214
+
215
+ while True:
216
+ try:
217
+ while True:
218
+ rfds = [key.fileobj for (key, events) in selector.select()]
219
+ if rfds:
220
+ break
221
+
222
+ if alive_r in rfds:
223
+ # EOF because no more client processes left
224
+ assert os.read(alive_r, 1) == b'', "Not at EOF?"
225
+ raise SystemExit
226
+
227
+ if sig_r in rfds:
228
+ # Got SIGCHLD
229
+ os.read(sig_r, 65536) # exhaust
230
+ while True:
231
+ # Scan for child processes
232
+ try:
233
+ pid, sts = os.waitpid(-1, os.WNOHANG)
234
+ except ChildProcessError:
235
+ break
236
+ if pid == 0:
237
+ break
238
+ child_w = pid_to_fd.pop(pid, None)
239
+ if child_w is not None:
240
+ returncode = os.waitstatus_to_exitcode(sts)
241
+ # Send exit code to client process
242
+ try:
243
+ write_signed(child_w, returncode)
244
+ except BrokenPipeError:
245
+ # client vanished
246
+ pass
247
+ os.close(child_w)
248
+ else:
249
+ # This shouldn't happen really
250
+ warnings.warn('forkserver: waitpid returned '
251
+ 'unexpected pid %d' % pid)
252
+
253
+ if listener in rfds:
254
+ # Incoming fork request
255
+ with listener.accept()[0] as s:
256
+ # Receive fds from client
257
+ fds = reduction.recvfds(s, MAXFDS_TO_SEND + 1)
258
+ if len(fds) > MAXFDS_TO_SEND:
259
+ raise RuntimeError(
260
+ "Too many ({0:n}) fds to send".format(
261
+ len(fds)))
262
+ child_r, child_w, *fds = fds
263
+ s.close()
264
+ pid = os.fork()
265
+ if pid == 0:
266
+ # Child
267
+ code = 1
268
+ try:
269
+ listener.close()
270
+ selector.close()
271
+ unused_fds = [alive_r, child_w, sig_r, sig_w]
272
+ unused_fds.extend(pid_to_fd.values())
273
+ code = _serve_one(child_r, fds,
274
+ unused_fds,
275
+ old_handlers)
276
+ except Exception:
277
+ sys.excepthook(*sys.exc_info())
278
+ sys.stderr.flush()
279
+ finally:
280
+ os._exit(code)
281
+ else:
282
+ # Send pid to client process
283
+ try:
284
+ write_signed(child_w, pid)
285
+ except BrokenPipeError:
286
+ # client vanished
287
+ pass
288
+ pid_to_fd[pid] = child_w
289
+ os.close(child_r)
290
+ for fd in fds:
291
+ os.close(fd)
292
+
293
+ except OSError as e:
294
+ if e.errno != errno.ECONNABORTED:
295
+ raise
296
+
297
+
298
+ def _serve_one(child_r, fds, unused_fds, handlers):
299
+ # close unnecessary stuff and reset signal handlers
300
+ signal.set_wakeup_fd(-1)
301
+ for sig, val in handlers.items():
302
+ signal.signal(sig, val)
303
+ for fd in unused_fds:
304
+ os.close(fd)
305
+
306
+ (_forkserver._forkserver_alive_fd,
307
+ resource_tracker._resource_tracker._fd,
308
+ *_forkserver._inherited_fds) = fds
309
+
310
+ # Run process object received over pipe
311
+ parent_sentinel = os.dup(child_r)
312
+ code = spawn._main(child_r, parent_sentinel)
313
+
314
+ return code
315
+
316
+
317
+ #
318
+ # Read and write signed numbers
319
+ #
320
+
321
+ def read_signed(fd):
322
+ data = b''
323
+ length = SIGNED_STRUCT.size
324
+ while len(data) < length:
325
+ s = os.read(fd, length - len(data))
326
+ if not s:
327
+ raise EOFError('unexpected EOF')
328
+ data += s
329
+ return SIGNED_STRUCT.unpack(data)[0]
330
+
331
+ def write_signed(fd, n):
332
+ msg = SIGNED_STRUCT.pack(n)
333
+ while msg:
334
+ nbytes = os.write(fd, msg)
335
+ if nbytes == 0:
336
+ raise RuntimeError('should not get here')
337
+ msg = msg[nbytes:]
338
+
339
+ #
340
+ #
341
+ #
342
+
343
+ _forkserver = ForkServer()
344
+ ensure_running = _forkserver.ensure_running
345
+ get_inherited_fds = _forkserver.get_inherited_fds
346
+ connect_to_new_process = _forkserver.connect_to_new_process
347
+ set_forkserver_preload = _forkserver.set_forkserver_preload
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/heap.py ADDED
@@ -0,0 +1,337 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Module which supports allocation of memory from an mmap
3
+ #
4
+ # multiprocessing/heap.py
5
+ #
6
+ # Copyright (c) 2006-2008, R Oudkerk
7
+ # Licensed to PSF under a Contributor Agreement.
8
+ #
9
+
10
+ import bisect
11
+ from collections import defaultdict
12
+ import mmap
13
+ import os
14
+ import sys
15
+ import tempfile
16
+ import threading
17
+
18
+ from .context import reduction, assert_spawning
19
+ from . import util
20
+
21
+ __all__ = ['BufferWrapper']
22
+
23
+ #
24
+ # Inheritable class which wraps an mmap, and from which blocks can be allocated
25
+ #
26
+
27
+ if sys.platform == 'win32':
28
+
29
+ import _winapi
30
+
31
+ class Arena(object):
32
+ """
33
+ A shared memory area backed by anonymous memory (Windows).
34
+ """
35
+
36
+ _rand = tempfile._RandomNameSequence()
37
+
38
+ def __init__(self, size):
39
+ self.size = size
40
+ for i in range(100):
41
+ name = 'pym-%d-%s' % (os.getpid(), next(self._rand))
42
+ buf = mmap.mmap(-1, size, tagname=name)
43
+ if _winapi.GetLastError() == 0:
44
+ break
45
+ # We have reopened a preexisting mmap.
46
+ buf.close()
47
+ else:
48
+ raise FileExistsError('Cannot find name for new mmap')
49
+ self.name = name
50
+ self.buffer = buf
51
+ self._state = (self.size, self.name)
52
+
53
+ def __getstate__(self):
54
+ assert_spawning(self)
55
+ return self._state
56
+
57
+ def __setstate__(self, state):
58
+ self.size, self.name = self._state = state
59
+ # Reopen existing mmap
60
+ self.buffer = mmap.mmap(-1, self.size, tagname=self.name)
61
+ # XXX Temporarily preventing buildbot failures while determining
62
+ # XXX the correct long-term fix. See issue 23060
63
+ #assert _winapi.GetLastError() == _winapi.ERROR_ALREADY_EXISTS
64
+
65
+ else:
66
+
67
+ class Arena(object):
68
+ """
69
+ A shared memory area backed by a temporary file (POSIX).
70
+ """
71
+
72
+ if sys.platform == 'linux':
73
+ _dir_candidates = ['/dev/shm']
74
+ else:
75
+ _dir_candidates = []
76
+
77
+ def __init__(self, size, fd=-1):
78
+ self.size = size
79
+ self.fd = fd
80
+ if fd == -1:
81
+ # Arena is created anew (if fd != -1, it means we're coming
82
+ # from rebuild_arena() below)
83
+ self.fd, name = tempfile.mkstemp(
84
+ prefix='pym-%d-'%os.getpid(),
85
+ dir=self._choose_dir(size))
86
+ os.unlink(name)
87
+ util.Finalize(self, os.close, (self.fd,))
88
+ os.ftruncate(self.fd, size)
89
+ self.buffer = mmap.mmap(self.fd, self.size)
90
+
91
+ def _choose_dir(self, size):
92
+ # Choose a non-storage backed directory if possible,
93
+ # to improve performance
94
+ for d in self._dir_candidates:
95
+ st = os.statvfs(d)
96
+ if st.f_bavail * st.f_frsize >= size: # enough free space?
97
+ return d
98
+ return util.get_temp_dir()
99
+
100
+ def reduce_arena(a):
101
+ if a.fd == -1:
102
+ raise ValueError('Arena is unpicklable because '
103
+ 'forking was enabled when it was created')
104
+ return rebuild_arena, (a.size, reduction.DupFd(a.fd))
105
+
106
+ def rebuild_arena(size, dupfd):
107
+ return Arena(size, dupfd.detach())
108
+
109
+ reduction.register(Arena, reduce_arena)
110
+
111
+ #
112
+ # Class allowing allocation of chunks of memory from arenas
113
+ #
114
+
115
+ class Heap(object):
116
+
117
+ # Minimum malloc() alignment
118
+ _alignment = 8
119
+
120
+ _DISCARD_FREE_SPACE_LARGER_THAN = 4 * 1024 ** 2 # 4 MB
121
+ _DOUBLE_ARENA_SIZE_UNTIL = 4 * 1024 ** 2
122
+
123
+ def __init__(self, size=mmap.PAGESIZE):
124
+ self._lastpid = os.getpid()
125
+ self._lock = threading.Lock()
126
+ # Current arena allocation size
127
+ self._size = size
128
+ # A sorted list of available block sizes in arenas
129
+ self._lengths = []
130
+
131
+ # Free block management:
132
+ # - map each block size to a list of `(Arena, start, stop)` blocks
133
+ self._len_to_seq = {}
134
+ # - map `(Arena, start)` tuple to the `(Arena, start, stop)` block
135
+ # starting at that offset
136
+ self._start_to_block = {}
137
+ # - map `(Arena, stop)` tuple to the `(Arena, start, stop)` block
138
+ # ending at that offset
139
+ self._stop_to_block = {}
140
+
141
+ # Map arenas to their `(Arena, start, stop)` blocks in use
142
+ self._allocated_blocks = defaultdict(set)
143
+ self._arenas = []
144
+
145
+ # List of pending blocks to free - see comment in free() below
146
+ self._pending_free_blocks = []
147
+
148
+ # Statistics
149
+ self._n_mallocs = 0
150
+ self._n_frees = 0
151
+
152
+ @staticmethod
153
+ def _roundup(n, alignment):
154
+ # alignment must be a power of 2
155
+ mask = alignment - 1
156
+ return (n + mask) & ~mask
157
+
158
+ def _new_arena(self, size):
159
+ # Create a new arena with at least the given *size*
160
+ length = self._roundup(max(self._size, size), mmap.PAGESIZE)
161
+ # We carve larger and larger arenas, for efficiency, until we
162
+ # reach a large-ish size (roughly L3 cache-sized)
163
+ if self._size < self._DOUBLE_ARENA_SIZE_UNTIL:
164
+ self._size *= 2
165
+ util.info('allocating a new mmap of length %d', length)
166
+ arena = Arena(length)
167
+ self._arenas.append(arena)
168
+ return (arena, 0, length)
169
+
170
+ def _discard_arena(self, arena):
171
+ # Possibly delete the given (unused) arena
172
+ length = arena.size
173
+ # Reusing an existing arena is faster than creating a new one, so
174
+ # we only reclaim space if it's large enough.
175
+ if length < self._DISCARD_FREE_SPACE_LARGER_THAN:
176
+ return
177
+ blocks = self._allocated_blocks.pop(arena)
178
+ assert not blocks
179
+ del self._start_to_block[(arena, 0)]
180
+ del self._stop_to_block[(arena, length)]
181
+ self._arenas.remove(arena)
182
+ seq = self._len_to_seq[length]
183
+ seq.remove((arena, 0, length))
184
+ if not seq:
185
+ del self._len_to_seq[length]
186
+ self._lengths.remove(length)
187
+
188
+ def _malloc(self, size):
189
+ # returns a large enough block -- it might be much larger
190
+ i = bisect.bisect_left(self._lengths, size)
191
+ if i == len(self._lengths):
192
+ return self._new_arena(size)
193
+ else:
194
+ length = self._lengths[i]
195
+ seq = self._len_to_seq[length]
196
+ block = seq.pop()
197
+ if not seq:
198
+ del self._len_to_seq[length], self._lengths[i]
199
+
200
+ (arena, start, stop) = block
201
+ del self._start_to_block[(arena, start)]
202
+ del self._stop_to_block[(arena, stop)]
203
+ return block
204
+
205
+ def _add_free_block(self, block):
206
+ # make block available and try to merge with its neighbours in the arena
207
+ (arena, start, stop) = block
208
+
209
+ try:
210
+ prev_block = self._stop_to_block[(arena, start)]
211
+ except KeyError:
212
+ pass
213
+ else:
214
+ start, _ = self._absorb(prev_block)
215
+
216
+ try:
217
+ next_block = self._start_to_block[(arena, stop)]
218
+ except KeyError:
219
+ pass
220
+ else:
221
+ _, stop = self._absorb(next_block)
222
+
223
+ block = (arena, start, stop)
224
+ length = stop - start
225
+
226
+ try:
227
+ self._len_to_seq[length].append(block)
228
+ except KeyError:
229
+ self._len_to_seq[length] = [block]
230
+ bisect.insort(self._lengths, length)
231
+
232
+ self._start_to_block[(arena, start)] = block
233
+ self._stop_to_block[(arena, stop)] = block
234
+
235
+ def _absorb(self, block):
236
+ # deregister this block so it can be merged with a neighbour
237
+ (arena, start, stop) = block
238
+ del self._start_to_block[(arena, start)]
239
+ del self._stop_to_block[(arena, stop)]
240
+
241
+ length = stop - start
242
+ seq = self._len_to_seq[length]
243
+ seq.remove(block)
244
+ if not seq:
245
+ del self._len_to_seq[length]
246
+ self._lengths.remove(length)
247
+
248
+ return start, stop
249
+
250
+ def _remove_allocated_block(self, block):
251
+ arena, start, stop = block
252
+ blocks = self._allocated_blocks[arena]
253
+ blocks.remove((start, stop))
254
+ if not blocks:
255
+ # Arena is entirely free, discard it from this process
256
+ self._discard_arena(arena)
257
+
258
+ def _free_pending_blocks(self):
259
+ # Free all the blocks in the pending list - called with the lock held.
260
+ while True:
261
+ try:
262
+ block = self._pending_free_blocks.pop()
263
+ except IndexError:
264
+ break
265
+ self._add_free_block(block)
266
+ self._remove_allocated_block(block)
267
+
268
+ def free(self, block):
269
+ # free a block returned by malloc()
270
+ # Since free() can be called asynchronously by the GC, it could happen
271
+ # that it's called while self._lock is held: in that case,
272
+ # self._lock.acquire() would deadlock (issue #12352). To avoid that, a
273
+ # trylock is used instead, and if the lock can't be acquired
274
+ # immediately, the block is added to a list of blocks to be freed
275
+ # synchronously sometimes later from malloc() or free(), by calling
276
+ # _free_pending_blocks() (appending and retrieving from a list is not
277
+ # strictly thread-safe but under CPython it's atomic thanks to the GIL).
278
+ if os.getpid() != self._lastpid:
279
+ raise ValueError(
280
+ "My pid ({0:n}) is not last pid {1:n}".format(
281
+ os.getpid(),self._lastpid))
282
+ if not self._lock.acquire(False):
283
+ # can't acquire the lock right now, add the block to the list of
284
+ # pending blocks to free
285
+ self._pending_free_blocks.append(block)
286
+ else:
287
+ # we hold the lock
288
+ try:
289
+ self._n_frees += 1
290
+ self._free_pending_blocks()
291
+ self._add_free_block(block)
292
+ self._remove_allocated_block(block)
293
+ finally:
294
+ self._lock.release()
295
+
296
+ def malloc(self, size):
297
+ # return a block of right size (possibly rounded up)
298
+ if size < 0:
299
+ raise ValueError("Size {0:n} out of range".format(size))
300
+ if sys.maxsize <= size:
301
+ raise OverflowError("Size {0:n} too large".format(size))
302
+ if os.getpid() != self._lastpid:
303
+ self.__init__() # reinitialize after fork
304
+ with self._lock:
305
+ self._n_mallocs += 1
306
+ # allow pending blocks to be marked available
307
+ self._free_pending_blocks()
308
+ size = self._roundup(max(size, 1), self._alignment)
309
+ (arena, start, stop) = self._malloc(size)
310
+ real_stop = start + size
311
+ if real_stop < stop:
312
+ # if the returned block is larger than necessary, mark
313
+ # the remainder available
314
+ self._add_free_block((arena, real_stop, stop))
315
+ self._allocated_blocks[arena].add((start, real_stop))
316
+ return (arena, start, real_stop)
317
+
318
+ #
319
+ # Class wrapping a block allocated out of a Heap -- can be inherited by child process
320
+ #
321
+
322
+ class BufferWrapper(object):
323
+
324
+ _heap = Heap()
325
+
326
+ def __init__(self, size):
327
+ if size < 0:
328
+ raise ValueError("Size {0:n} out of range".format(size))
329
+ if sys.maxsize <= size:
330
+ raise OverflowError("Size {0:n} too large".format(size))
331
+ block = BufferWrapper._heap.malloc(size)
332
+ self._state = (block, size)
333
+ util.Finalize(self, BufferWrapper._heap.free, args=(block,))
334
+
335
+ def create_memoryview(self):
336
+ (arena, start, stop), size = self._state
337
+ return memoryview(arena.buffer)[start:start+size]
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/pool.py ADDED
@@ -0,0 +1,957 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Module providing the `Pool` class for managing a process pool
3
+ #
4
+ # multiprocessing/pool.py
5
+ #
6
+ # Copyright (c) 2006-2008, R Oudkerk
7
+ # Licensed to PSF under a Contributor Agreement.
8
+ #
9
+
10
+ __all__ = ['Pool', 'ThreadPool']
11
+
12
+ #
13
+ # Imports
14
+ #
15
+
16
+ import collections
17
+ import itertools
18
+ import os
19
+ import queue
20
+ import threading
21
+ import time
22
+ import traceback
23
+ import types
24
+ import warnings
25
+
26
+ # If threading is available then ThreadPool should be provided. Therefore
27
+ # we avoid top-level imports which are liable to fail on some systems.
28
+ from . import util
29
+ from . import get_context, TimeoutError
30
+ from .connection import wait
31
+
32
+ #
33
+ # Constants representing the state of a pool
34
+ #
35
+
36
+ INIT = "INIT"
37
+ RUN = "RUN"
38
+ CLOSE = "CLOSE"
39
+ TERMINATE = "TERMINATE"
40
+
41
+ #
42
+ # Miscellaneous
43
+ #
44
+
45
+ job_counter = itertools.count()
46
+
47
+ def mapstar(args):
48
+ return list(map(*args))
49
+
50
+ def starmapstar(args):
51
+ return list(itertools.starmap(args[0], args[1]))
52
+
53
+ #
54
+ # Hack to embed stringification of remote traceback in local traceback
55
+ #
56
+
57
+ class RemoteTraceback(Exception):
58
+ def __init__(self, tb):
59
+ self.tb = tb
60
+ def __str__(self):
61
+ return self.tb
62
+
63
+ class ExceptionWithTraceback:
64
+ def __init__(self, exc, tb):
65
+ tb = traceback.format_exception(type(exc), exc, tb)
66
+ tb = ''.join(tb)
67
+ self.exc = exc
68
+ self.tb = '\n"""\n%s"""' % tb
69
+ def __reduce__(self):
70
+ return rebuild_exc, (self.exc, self.tb)
71
+
72
+ def rebuild_exc(exc, tb):
73
+ exc.__cause__ = RemoteTraceback(tb)
74
+ return exc
75
+
76
+ #
77
+ # Code run by worker processes
78
+ #
79
+
80
+ class MaybeEncodingError(Exception):
81
+ """Wraps possible unpickleable errors, so they can be
82
+ safely sent through the socket."""
83
+
84
+ def __init__(self, exc, value):
85
+ self.exc = repr(exc)
86
+ self.value = repr(value)
87
+ super(MaybeEncodingError, self).__init__(self.exc, self.value)
88
+
89
+ def __str__(self):
90
+ return "Error sending result: '%s'. Reason: '%s'" % (self.value,
91
+ self.exc)
92
+
93
+ def __repr__(self):
94
+ return "<%s: %s>" % (self.__class__.__name__, self)
95
+
96
+
97
+ def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None,
98
+ wrap_exception=False):
99
+ if (maxtasks is not None) and not (isinstance(maxtasks, int)
100
+ and maxtasks >= 1):
101
+ raise AssertionError("Maxtasks {!r} is not valid".format(maxtasks))
102
+ put = outqueue.put
103
+ get = inqueue.get
104
+ if hasattr(inqueue, '_writer'):
105
+ inqueue._writer.close()
106
+ outqueue._reader.close()
107
+
108
+ if initializer is not None:
109
+ initializer(*initargs)
110
+
111
+ completed = 0
112
+ while maxtasks is None or (maxtasks and completed < maxtasks):
113
+ try:
114
+ task = get()
115
+ except (EOFError, OSError):
116
+ util.debug('worker got EOFError or OSError -- exiting')
117
+ break
118
+
119
+ if task is None:
120
+ util.debug('worker got sentinel -- exiting')
121
+ break
122
+
123
+ job, i, func, args, kwds = task
124
+ try:
125
+ result = (True, func(*args, **kwds))
126
+ except Exception as e:
127
+ if wrap_exception and func is not _helper_reraises_exception:
128
+ e = ExceptionWithTraceback(e, e.__traceback__)
129
+ result = (False, e)
130
+ try:
131
+ put((job, i, result))
132
+ except Exception as e:
133
+ wrapped = MaybeEncodingError(e, result[1])
134
+ util.debug("Possible encoding error while sending result: %s" % (
135
+ wrapped))
136
+ put((job, i, (False, wrapped)))
137
+
138
+ task = job = result = func = args = kwds = None
139
+ completed += 1
140
+ util.debug('worker exiting after %d tasks' % completed)
141
+
142
+ def _helper_reraises_exception(ex):
143
+ 'Pickle-able helper function for use by _guarded_task_generation.'
144
+ raise ex
145
+
146
+ #
147
+ # Class representing a process pool
148
+ #
149
+
150
+ class _PoolCache(dict):
151
+ """
152
+ Class that implements a cache for the Pool class that will notify
153
+ the pool management threads every time the cache is emptied. The
154
+ notification is done by the use of a queue that is provided when
155
+ instantiating the cache.
156
+ """
157
+ def __init__(self, /, *args, notifier=None, **kwds):
158
+ self.notifier = notifier
159
+ super().__init__(*args, **kwds)
160
+
161
+ def __delitem__(self, item):
162
+ super().__delitem__(item)
163
+
164
+ # Notify that the cache is empty. This is important because the
165
+ # pool keeps maintaining workers until the cache gets drained. This
166
+ # eliminates a race condition in which a task is finished after the
167
+ # the pool's _handle_workers method has enter another iteration of the
168
+ # loop. In this situation, the only event that can wake up the pool
169
+ # is the cache to be emptied (no more tasks available).
170
+ if not self:
171
+ self.notifier.put(None)
172
+
173
+ class Pool(object):
174
+ '''
175
+ Class which supports an async version of applying functions to arguments.
176
+ '''
177
+ _wrap_exception = True
178
+
179
+ @staticmethod
180
+ def Process(ctx, *args, **kwds):
181
+ return ctx.Process(*args, **kwds)
182
+
183
+ def __init__(self, processes=None, initializer=None, initargs=(),
184
+ maxtasksperchild=None, context=None):
185
+ # Attributes initialized early to make sure that they exist in
186
+ # __del__() if __init__() raises an exception
187
+ self._pool = []
188
+ self._state = INIT
189
+
190
+ self._ctx = context or get_context()
191
+ self._setup_queues()
192
+ self._taskqueue = queue.SimpleQueue()
193
+ # The _change_notifier queue exist to wake up self._handle_workers()
194
+ # when the cache (self._cache) is empty or when there is a change in
195
+ # the _state variable of the thread that runs _handle_workers.
196
+ self._change_notifier = self._ctx.SimpleQueue()
197
+ self._cache = _PoolCache(notifier=self._change_notifier)
198
+ self._maxtasksperchild = maxtasksperchild
199
+ self._initializer = initializer
200
+ self._initargs = initargs
201
+
202
+ if processes is None:
203
+ processes = os.cpu_count() or 1
204
+ if processes < 1:
205
+ raise ValueError("Number of processes must be at least 1")
206
+ if maxtasksperchild is not None:
207
+ if not isinstance(maxtasksperchild, int) or maxtasksperchild <= 0:
208
+ raise ValueError("maxtasksperchild must be a positive int or None")
209
+
210
+ if initializer is not None and not callable(initializer):
211
+ raise TypeError('initializer must be a callable')
212
+
213
+ self._processes = processes
214
+ try:
215
+ self._repopulate_pool()
216
+ except Exception:
217
+ for p in self._pool:
218
+ if p.exitcode is None:
219
+ p.terminate()
220
+ for p in self._pool:
221
+ p.join()
222
+ raise
223
+
224
+ sentinels = self._get_sentinels()
225
+
226
+ self._worker_handler = threading.Thread(
227
+ target=Pool._handle_workers,
228
+ args=(self._cache, self._taskqueue, self._ctx, self.Process,
229
+ self._processes, self._pool, self._inqueue, self._outqueue,
230
+ self._initializer, self._initargs, self._maxtasksperchild,
231
+ self._wrap_exception, sentinels, self._change_notifier)
232
+ )
233
+ self._worker_handler.daemon = True
234
+ self._worker_handler._state = RUN
235
+ self._worker_handler.start()
236
+
237
+
238
+ self._task_handler = threading.Thread(
239
+ target=Pool._handle_tasks,
240
+ args=(self._taskqueue, self._quick_put, self._outqueue,
241
+ self._pool, self._cache)
242
+ )
243
+ self._task_handler.daemon = True
244
+ self._task_handler._state = RUN
245
+ self._task_handler.start()
246
+
247
+ self._result_handler = threading.Thread(
248
+ target=Pool._handle_results,
249
+ args=(self._outqueue, self._quick_get, self._cache)
250
+ )
251
+ self._result_handler.daemon = True
252
+ self._result_handler._state = RUN
253
+ self._result_handler.start()
254
+
255
+ self._terminate = util.Finalize(
256
+ self, self._terminate_pool,
257
+ args=(self._taskqueue, self._inqueue, self._outqueue, self._pool,
258
+ self._change_notifier, self._worker_handler, self._task_handler,
259
+ self._result_handler, self._cache),
260
+ exitpriority=15
261
+ )
262
+ self._state = RUN
263
+
264
+ # Copy globals as function locals to make sure that they are available
265
+ # during Python shutdown when the Pool is destroyed.
266
+ def __del__(self, _warn=warnings.warn, RUN=RUN):
267
+ if self._state == RUN:
268
+ _warn(f"unclosed running multiprocessing pool {self!r}",
269
+ ResourceWarning, source=self)
270
+ if getattr(self, '_change_notifier', None) is not None:
271
+ self._change_notifier.put(None)
272
+
273
+ def __repr__(self):
274
+ cls = self.__class__
275
+ return (f'<{cls.__module__}.{cls.__qualname__} '
276
+ f'state={self._state} '
277
+ f'pool_size={len(self._pool)}>')
278
+
279
+ def _get_sentinels(self):
280
+ task_queue_sentinels = [self._outqueue._reader]
281
+ self_notifier_sentinels = [self._change_notifier._reader]
282
+ return [*task_queue_sentinels, *self_notifier_sentinels]
283
+
284
+ @staticmethod
285
+ def _get_worker_sentinels(workers):
286
+ return [worker.sentinel for worker in
287
+ workers if hasattr(worker, "sentinel")]
288
+
289
+ @staticmethod
290
+ def _join_exited_workers(pool):
291
+ """Cleanup after any worker processes which have exited due to reaching
292
+ their specified lifetime. Returns True if any workers were cleaned up.
293
+ """
294
+ cleaned = False
295
+ for i in reversed(range(len(pool))):
296
+ worker = pool[i]
297
+ if worker.exitcode is not None:
298
+ # worker exited
299
+ util.debug('cleaning up worker %d' % i)
300
+ worker.join()
301
+ cleaned = True
302
+ del pool[i]
303
+ return cleaned
304
+
305
+ def _repopulate_pool(self):
306
+ return self._repopulate_pool_static(self._ctx, self.Process,
307
+ self._processes,
308
+ self._pool, self._inqueue,
309
+ self._outqueue, self._initializer,
310
+ self._initargs,
311
+ self._maxtasksperchild,
312
+ self._wrap_exception)
313
+
314
+ @staticmethod
315
+ def _repopulate_pool_static(ctx, Process, processes, pool, inqueue,
316
+ outqueue, initializer, initargs,
317
+ maxtasksperchild, wrap_exception):
318
+ """Bring the number of pool processes up to the specified number,
319
+ for use after reaping workers which have exited.
320
+ """
321
+ for i in range(processes - len(pool)):
322
+ w = Process(ctx, target=worker,
323
+ args=(inqueue, outqueue,
324
+ initializer,
325
+ initargs, maxtasksperchild,
326
+ wrap_exception))
327
+ w.name = w.name.replace('Process', 'PoolWorker')
328
+ w.daemon = True
329
+ w.start()
330
+ pool.append(w)
331
+ util.debug('added worker')
332
+
333
+ @staticmethod
334
+ def _maintain_pool(ctx, Process, processes, pool, inqueue, outqueue,
335
+ initializer, initargs, maxtasksperchild,
336
+ wrap_exception):
337
+ """Clean up any exited workers and start replacements for them.
338
+ """
339
+ if Pool._join_exited_workers(pool):
340
+ Pool._repopulate_pool_static(ctx, Process, processes, pool,
341
+ inqueue, outqueue, initializer,
342
+ initargs, maxtasksperchild,
343
+ wrap_exception)
344
+
345
+ def _setup_queues(self):
346
+ self._inqueue = self._ctx.SimpleQueue()
347
+ self._outqueue = self._ctx.SimpleQueue()
348
+ self._quick_put = self._inqueue._writer.send
349
+ self._quick_get = self._outqueue._reader.recv
350
+
351
+ def _check_running(self):
352
+ if self._state != RUN:
353
+ raise ValueError("Pool not running")
354
+
355
+ def apply(self, func, args=(), kwds={}):
356
+ '''
357
+ Equivalent of `func(*args, **kwds)`.
358
+ Pool must be running.
359
+ '''
360
+ return self.apply_async(func, args, kwds).get()
361
+
362
+ def map(self, func, iterable, chunksize=None):
363
+ '''
364
+ Apply `func` to each element in `iterable`, collecting the results
365
+ in a list that is returned.
366
+ '''
367
+ return self._map_async(func, iterable, mapstar, chunksize).get()
368
+
369
+ def starmap(self, func, iterable, chunksize=None):
370
+ '''
371
+ Like `map()` method but the elements of the `iterable` are expected to
372
+ be iterables as well and will be unpacked as arguments. Hence
373
+ `func` and (a, b) becomes func(a, b).
374
+ '''
375
+ return self._map_async(func, iterable, starmapstar, chunksize).get()
376
+
377
+ def starmap_async(self, func, iterable, chunksize=None, callback=None,
378
+ error_callback=None):
379
+ '''
380
+ Asynchronous version of `starmap()` method.
381
+ '''
382
+ return self._map_async(func, iterable, starmapstar, chunksize,
383
+ callback, error_callback)
384
+
385
+ def _guarded_task_generation(self, result_job, func, iterable):
386
+ '''Provides a generator of tasks for imap and imap_unordered with
387
+ appropriate handling for iterables which throw exceptions during
388
+ iteration.'''
389
+ try:
390
+ i = -1
391
+ for i, x in enumerate(iterable):
392
+ yield (result_job, i, func, (x,), {})
393
+ except Exception as e:
394
+ yield (result_job, i+1, _helper_reraises_exception, (e,), {})
395
+
396
+ def imap(self, func, iterable, chunksize=1):
397
+ '''
398
+ Equivalent of `map()` -- can be MUCH slower than `Pool.map()`.
399
+ '''
400
+ self._check_running()
401
+ if chunksize == 1:
402
+ result = IMapIterator(self)
403
+ self._taskqueue.put(
404
+ (
405
+ self._guarded_task_generation(result._job, func, iterable),
406
+ result._set_length
407
+ ))
408
+ return result
409
+ else:
410
+ if chunksize < 1:
411
+ raise ValueError(
412
+ "Chunksize must be 1+, not {0:n}".format(
413
+ chunksize))
414
+ task_batches = Pool._get_tasks(func, iterable, chunksize)
415
+ result = IMapIterator(self)
416
+ self._taskqueue.put(
417
+ (
418
+ self._guarded_task_generation(result._job,
419
+ mapstar,
420
+ task_batches),
421
+ result._set_length
422
+ ))
423
+ return (item for chunk in result for item in chunk)
424
+
425
+ def imap_unordered(self, func, iterable, chunksize=1):
426
+ '''
427
+ Like `imap()` method but ordering of results is arbitrary.
428
+ '''
429
+ self._check_running()
430
+ if chunksize == 1:
431
+ result = IMapUnorderedIterator(self)
432
+ self._taskqueue.put(
433
+ (
434
+ self._guarded_task_generation(result._job, func, iterable),
435
+ result._set_length
436
+ ))
437
+ return result
438
+ else:
439
+ if chunksize < 1:
440
+ raise ValueError(
441
+ "Chunksize must be 1+, not {0!r}".format(chunksize))
442
+ task_batches = Pool._get_tasks(func, iterable, chunksize)
443
+ result = IMapUnorderedIterator(self)
444
+ self._taskqueue.put(
445
+ (
446
+ self._guarded_task_generation(result._job,
447
+ mapstar,
448
+ task_batches),
449
+ result._set_length
450
+ ))
451
+ return (item for chunk in result for item in chunk)
452
+
453
+ def apply_async(self, func, args=(), kwds={}, callback=None,
454
+ error_callback=None):
455
+ '''
456
+ Asynchronous version of `apply()` method.
457
+ '''
458
+ self._check_running()
459
+ result = ApplyResult(self, callback, error_callback)
460
+ self._taskqueue.put(([(result._job, 0, func, args, kwds)], None))
461
+ return result
462
+
463
+ def map_async(self, func, iterable, chunksize=None, callback=None,
464
+ error_callback=None):
465
+ '''
466
+ Asynchronous version of `map()` method.
467
+ '''
468
+ return self._map_async(func, iterable, mapstar, chunksize, callback,
469
+ error_callback)
470
+
471
+ def _map_async(self, func, iterable, mapper, chunksize=None, callback=None,
472
+ error_callback=None):
473
+ '''
474
+ Helper function to implement map, starmap and their async counterparts.
475
+ '''
476
+ self._check_running()
477
+ if not hasattr(iterable, '__len__'):
478
+ iterable = list(iterable)
479
+
480
+ if chunksize is None:
481
+ chunksize, extra = divmod(len(iterable), len(self._pool) * 4)
482
+ if extra:
483
+ chunksize += 1
484
+ if len(iterable) == 0:
485
+ chunksize = 0
486
+
487
+ task_batches = Pool._get_tasks(func, iterable, chunksize)
488
+ result = MapResult(self, chunksize, len(iterable), callback,
489
+ error_callback=error_callback)
490
+ self._taskqueue.put(
491
+ (
492
+ self._guarded_task_generation(result._job,
493
+ mapper,
494
+ task_batches),
495
+ None
496
+ )
497
+ )
498
+ return result
499
+
500
+ @staticmethod
501
+ def _wait_for_updates(sentinels, change_notifier, timeout=None):
502
+ wait(sentinels, timeout=timeout)
503
+ while not change_notifier.empty():
504
+ change_notifier.get()
505
+
506
+ @classmethod
507
+ def _handle_workers(cls, cache, taskqueue, ctx, Process, processes,
508
+ pool, inqueue, outqueue, initializer, initargs,
509
+ maxtasksperchild, wrap_exception, sentinels,
510
+ change_notifier):
511
+ thread = threading.current_thread()
512
+
513
+ # Keep maintaining workers until the cache gets drained, unless the pool
514
+ # is terminated.
515
+ while thread._state == RUN or (cache and thread._state != TERMINATE):
516
+ cls._maintain_pool(ctx, Process, processes, pool, inqueue,
517
+ outqueue, initializer, initargs,
518
+ maxtasksperchild, wrap_exception)
519
+
520
+ current_sentinels = [*cls._get_worker_sentinels(pool), *sentinels]
521
+
522
+ cls._wait_for_updates(current_sentinels, change_notifier)
523
+ # send sentinel to stop workers
524
+ taskqueue.put(None)
525
+ util.debug('worker handler exiting')
526
+
527
+ @staticmethod
528
+ def _handle_tasks(taskqueue, put, outqueue, pool, cache):
529
+ thread = threading.current_thread()
530
+
531
+ for taskseq, set_length in iter(taskqueue.get, None):
532
+ task = None
533
+ try:
534
+ # iterating taskseq cannot fail
535
+ for task in taskseq:
536
+ if thread._state != RUN:
537
+ util.debug('task handler found thread._state != RUN')
538
+ break
539
+ try:
540
+ put(task)
541
+ except Exception as e:
542
+ job, idx = task[:2]
543
+ try:
544
+ cache[job]._set(idx, (False, e))
545
+ except KeyError:
546
+ pass
547
+ else:
548
+ if set_length:
549
+ util.debug('doing set_length()')
550
+ idx = task[1] if task else -1
551
+ set_length(idx + 1)
552
+ continue
553
+ break
554
+ finally:
555
+ task = taskseq = job = None
556
+ else:
557
+ util.debug('task handler got sentinel')
558
+
559
+ try:
560
+ # tell result handler to finish when cache is empty
561
+ util.debug('task handler sending sentinel to result handler')
562
+ outqueue.put(None)
563
+
564
+ # tell workers there is no more work
565
+ util.debug('task handler sending sentinel to workers')
566
+ for p in pool:
567
+ put(None)
568
+ except OSError:
569
+ util.debug('task handler got OSError when sending sentinels')
570
+
571
+ util.debug('task handler exiting')
572
+
573
+ @staticmethod
574
+ def _handle_results(outqueue, get, cache):
575
+ thread = threading.current_thread()
576
+
577
+ while 1:
578
+ try:
579
+ task = get()
580
+ except (OSError, EOFError):
581
+ util.debug('result handler got EOFError/OSError -- exiting')
582
+ return
583
+
584
+ if thread._state != RUN:
585
+ assert thread._state == TERMINATE, "Thread not in TERMINATE"
586
+ util.debug('result handler found thread._state=TERMINATE')
587
+ break
588
+
589
+ if task is None:
590
+ util.debug('result handler got sentinel')
591
+ break
592
+
593
+ job, i, obj = task
594
+ try:
595
+ cache[job]._set(i, obj)
596
+ except KeyError:
597
+ pass
598
+ task = job = obj = None
599
+
600
+ while cache and thread._state != TERMINATE:
601
+ try:
602
+ task = get()
603
+ except (OSError, EOFError):
604
+ util.debug('result handler got EOFError/OSError -- exiting')
605
+ return
606
+
607
+ if task is None:
608
+ util.debug('result handler ignoring extra sentinel')
609
+ continue
610
+ job, i, obj = task
611
+ try:
612
+ cache[job]._set(i, obj)
613
+ except KeyError:
614
+ pass
615
+ task = job = obj = None
616
+
617
+ if hasattr(outqueue, '_reader'):
618
+ util.debug('ensuring that outqueue is not full')
619
+ # If we don't make room available in outqueue then
620
+ # attempts to add the sentinel (None) to outqueue may
621
+ # block. There is guaranteed to be no more than 2 sentinels.
622
+ try:
623
+ for i in range(10):
624
+ if not outqueue._reader.poll():
625
+ break
626
+ get()
627
+ except (OSError, EOFError):
628
+ pass
629
+
630
+ util.debug('result handler exiting: len(cache)=%s, thread._state=%s',
631
+ len(cache), thread._state)
632
+
633
+ @staticmethod
634
+ def _get_tasks(func, it, size):
635
+ it = iter(it)
636
+ while 1:
637
+ x = tuple(itertools.islice(it, size))
638
+ if not x:
639
+ return
640
+ yield (func, x)
641
+
642
+ def __reduce__(self):
643
+ raise NotImplementedError(
644
+ 'pool objects cannot be passed between processes or pickled'
645
+ )
646
+
647
+ def close(self):
648
+ util.debug('closing pool')
649
+ if self._state == RUN:
650
+ self._state = CLOSE
651
+ self._worker_handler._state = CLOSE
652
+ self._change_notifier.put(None)
653
+
654
+ def terminate(self):
655
+ util.debug('terminating pool')
656
+ self._state = TERMINATE
657
+ self._terminate()
658
+
659
+ def join(self):
660
+ util.debug('joining pool')
661
+ if self._state == RUN:
662
+ raise ValueError("Pool is still running")
663
+ elif self._state not in (CLOSE, TERMINATE):
664
+ raise ValueError("In unknown state")
665
+ self._worker_handler.join()
666
+ self._task_handler.join()
667
+ self._result_handler.join()
668
+ for p in self._pool:
669
+ p.join()
670
+
671
+ @staticmethod
672
+ def _help_stuff_finish(inqueue, task_handler, size):
673
+ # task_handler may be blocked trying to put items on inqueue
674
+ util.debug('removing tasks from inqueue until task handler finished')
675
+ inqueue._rlock.acquire()
676
+ while task_handler.is_alive() and inqueue._reader.poll():
677
+ inqueue._reader.recv()
678
+ time.sleep(0)
679
+
680
+ @classmethod
681
+ def _terminate_pool(cls, taskqueue, inqueue, outqueue, pool, change_notifier,
682
+ worker_handler, task_handler, result_handler, cache):
683
+ # this is guaranteed to only be called once
684
+ util.debug('finalizing pool')
685
+
686
+ # Notify that the worker_handler state has been changed so the
687
+ # _handle_workers loop can be unblocked (and exited) in order to
688
+ # send the finalization sentinel all the workers.
689
+ worker_handler._state = TERMINATE
690
+ change_notifier.put(None)
691
+
692
+ task_handler._state = TERMINATE
693
+
694
+ util.debug('helping task handler/workers to finish')
695
+ cls._help_stuff_finish(inqueue, task_handler, len(pool))
696
+
697
+ if (not result_handler.is_alive()) and (len(cache) != 0):
698
+ raise AssertionError(
699
+ "Cannot have cache with result_hander not alive")
700
+
701
+ result_handler._state = TERMINATE
702
+ change_notifier.put(None)
703
+ outqueue.put(None) # sentinel
704
+
705
+ # We must wait for the worker handler to exit before terminating
706
+ # workers because we don't want workers to be restarted behind our back.
707
+ util.debug('joining worker handler')
708
+ if threading.current_thread() is not worker_handler:
709
+ worker_handler.join()
710
+
711
+ # Terminate workers which haven't already finished.
712
+ if pool and hasattr(pool[0], 'terminate'):
713
+ util.debug('terminating workers')
714
+ for p in pool:
715
+ if p.exitcode is None:
716
+ p.terminate()
717
+
718
+ util.debug('joining task handler')
719
+ if threading.current_thread() is not task_handler:
720
+ task_handler.join()
721
+
722
+ util.debug('joining result handler')
723
+ if threading.current_thread() is not result_handler:
724
+ result_handler.join()
725
+
726
+ if pool and hasattr(pool[0], 'terminate'):
727
+ util.debug('joining pool workers')
728
+ for p in pool:
729
+ if p.is_alive():
730
+ # worker has not yet exited
731
+ util.debug('cleaning up worker %d' % p.pid)
732
+ p.join()
733
+
734
+ def __enter__(self):
735
+ self._check_running()
736
+ return self
737
+
738
+ def __exit__(self, exc_type, exc_val, exc_tb):
739
+ self.terminate()
740
+
741
+ #
742
+ # Class whose instances are returned by `Pool.apply_async()`
743
+ #
744
+
745
+ class ApplyResult(object):
746
+
747
+ def __init__(self, pool, callback, error_callback):
748
+ self._pool = pool
749
+ self._event = threading.Event()
750
+ self._job = next(job_counter)
751
+ self._cache = pool._cache
752
+ self._callback = callback
753
+ self._error_callback = error_callback
754
+ self._cache[self._job] = self
755
+
756
+ def ready(self):
757
+ return self._event.is_set()
758
+
759
+ def successful(self):
760
+ if not self.ready():
761
+ raise ValueError("{0!r} not ready".format(self))
762
+ return self._success
763
+
764
+ def wait(self, timeout=None):
765
+ self._event.wait(timeout)
766
+
767
+ def get(self, timeout=None):
768
+ self.wait(timeout)
769
+ if not self.ready():
770
+ raise TimeoutError
771
+ if self._success:
772
+ return self._value
773
+ else:
774
+ raise self._value
775
+
776
+ def _set(self, i, obj):
777
+ self._success, self._value = obj
778
+ if self._callback and self._success:
779
+ self._callback(self._value)
780
+ if self._error_callback and not self._success:
781
+ self._error_callback(self._value)
782
+ self._event.set()
783
+ del self._cache[self._job]
784
+ self._pool = None
785
+
786
+ __class_getitem__ = classmethod(types.GenericAlias)
787
+
788
+ AsyncResult = ApplyResult # create alias -- see #17805
789
+
790
+ #
791
+ # Class whose instances are returned by `Pool.map_async()`
792
+ #
793
+
794
+ class MapResult(ApplyResult):
795
+
796
+ def __init__(self, pool, chunksize, length, callback, error_callback):
797
+ ApplyResult.__init__(self, pool, callback,
798
+ error_callback=error_callback)
799
+ self._success = True
800
+ self._value = [None] * length
801
+ self._chunksize = chunksize
802
+ if chunksize <= 0:
803
+ self._number_left = 0
804
+ self._event.set()
805
+ del self._cache[self._job]
806
+ else:
807
+ self._number_left = length//chunksize + bool(length % chunksize)
808
+
809
+ def _set(self, i, success_result):
810
+ self._number_left -= 1
811
+ success, result = success_result
812
+ if success and self._success:
813
+ self._value[i*self._chunksize:(i+1)*self._chunksize] = result
814
+ if self._number_left == 0:
815
+ if self._callback:
816
+ self._callback(self._value)
817
+ del self._cache[self._job]
818
+ self._event.set()
819
+ self._pool = None
820
+ else:
821
+ if not success and self._success:
822
+ # only store first exception
823
+ self._success = False
824
+ self._value = result
825
+ if self._number_left == 0:
826
+ # only consider the result ready once all jobs are done
827
+ if self._error_callback:
828
+ self._error_callback(self._value)
829
+ del self._cache[self._job]
830
+ self._event.set()
831
+ self._pool = None
832
+
833
+ #
834
+ # Class whose instances are returned by `Pool.imap()`
835
+ #
836
+
837
+ class IMapIterator(object):
838
+
839
+ def __init__(self, pool):
840
+ self._pool = pool
841
+ self._cond = threading.Condition(threading.Lock())
842
+ self._job = next(job_counter)
843
+ self._cache = pool._cache
844
+ self._items = collections.deque()
845
+ self._index = 0
846
+ self._length = None
847
+ self._unsorted = {}
848
+ self._cache[self._job] = self
849
+
850
+ def __iter__(self):
851
+ return self
852
+
853
+ def next(self, timeout=None):
854
+ with self._cond:
855
+ try:
856
+ item = self._items.popleft()
857
+ except IndexError:
858
+ if self._index == self._length:
859
+ self._pool = None
860
+ raise StopIteration from None
861
+ self._cond.wait(timeout)
862
+ try:
863
+ item = self._items.popleft()
864
+ except IndexError:
865
+ if self._index == self._length:
866
+ self._pool = None
867
+ raise StopIteration from None
868
+ raise TimeoutError from None
869
+
870
+ success, value = item
871
+ if success:
872
+ return value
873
+ raise value
874
+
875
+ __next__ = next # XXX
876
+
877
+ def _set(self, i, obj):
878
+ with self._cond:
879
+ if self._index == i:
880
+ self._items.append(obj)
881
+ self._index += 1
882
+ while self._index in self._unsorted:
883
+ obj = self._unsorted.pop(self._index)
884
+ self._items.append(obj)
885
+ self._index += 1
886
+ self._cond.notify()
887
+ else:
888
+ self._unsorted[i] = obj
889
+
890
+ if self._index == self._length:
891
+ del self._cache[self._job]
892
+ self._pool = None
893
+
894
+ def _set_length(self, length):
895
+ with self._cond:
896
+ self._length = length
897
+ if self._index == self._length:
898
+ self._cond.notify()
899
+ del self._cache[self._job]
900
+ self._pool = None
901
+
902
+ #
903
+ # Class whose instances are returned by `Pool.imap_unordered()`
904
+ #
905
+
906
+ class IMapUnorderedIterator(IMapIterator):
907
+
908
+ def _set(self, i, obj):
909
+ with self._cond:
910
+ self._items.append(obj)
911
+ self._index += 1
912
+ self._cond.notify()
913
+ if self._index == self._length:
914
+ del self._cache[self._job]
915
+ self._pool = None
916
+
917
+ #
918
+ #
919
+ #
920
+
921
+ class ThreadPool(Pool):
922
+ _wrap_exception = False
923
+
924
+ @staticmethod
925
+ def Process(ctx, *args, **kwds):
926
+ from .dummy import Process
927
+ return Process(*args, **kwds)
928
+
929
+ def __init__(self, processes=None, initializer=None, initargs=()):
930
+ Pool.__init__(self, processes, initializer, initargs)
931
+
932
+ def _setup_queues(self):
933
+ self._inqueue = queue.SimpleQueue()
934
+ self._outqueue = queue.SimpleQueue()
935
+ self._quick_put = self._inqueue.put
936
+ self._quick_get = self._outqueue.get
937
+
938
+ def _get_sentinels(self):
939
+ return [self._change_notifier._reader]
940
+
941
+ @staticmethod
942
+ def _get_worker_sentinels(workers):
943
+ return []
944
+
945
+ @staticmethod
946
+ def _help_stuff_finish(inqueue, task_handler, size):
947
+ # drain inqueue, and put sentinels at its head to make workers finish
948
+ try:
949
+ while True:
950
+ inqueue.get(block=False)
951
+ except queue.Empty:
952
+ pass
953
+ for i in range(size):
954
+ inqueue.put(None)
955
+
956
+ def _wait_for_updates(self, sentinels, change_notifier, timeout):
957
+ time.sleep(timeout)
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/popen_fork.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import signal
3
+
4
+ from . import util
5
+
6
+ __all__ = ['Popen']
7
+
8
+ #
9
+ # Start child process using fork
10
+ #
11
+
12
+ class Popen(object):
13
+ method = 'fork'
14
+
15
+ def __init__(self, process_obj):
16
+ util._flush_std_streams()
17
+ self.returncode = None
18
+ self.finalizer = None
19
+ self._launch(process_obj)
20
+
21
+ def duplicate_for_child(self, fd):
22
+ return fd
23
+
24
+ def poll(self, flag=os.WNOHANG):
25
+ if self.returncode is None:
26
+ try:
27
+ pid, sts = os.waitpid(self.pid, flag)
28
+ except OSError:
29
+ # Child process not yet created. See #1731717
30
+ # e.errno == errno.ECHILD == 10
31
+ return None
32
+ if pid == self.pid:
33
+ self.returncode = os.waitstatus_to_exitcode(sts)
34
+ return self.returncode
35
+
36
+ def wait(self, timeout=None):
37
+ if self.returncode is None:
38
+ if timeout is not None:
39
+ from multiprocess.connection import wait
40
+ if not wait([self.sentinel], timeout):
41
+ return None
42
+ # This shouldn't block if wait() returned successfully.
43
+ return self.poll(os.WNOHANG if timeout == 0.0 else 0)
44
+ return self.returncode
45
+
46
+ def _send_signal(self, sig):
47
+ if self.returncode is None:
48
+ try:
49
+ os.kill(self.pid, sig)
50
+ except ProcessLookupError:
51
+ pass
52
+ except OSError:
53
+ if self.wait(timeout=0.1) is None:
54
+ raise
55
+
56
+ def terminate(self):
57
+ self._send_signal(signal.SIGTERM)
58
+
59
+ def kill(self):
60
+ self._send_signal(signal.SIGKILL)
61
+
62
+ def _launch(self, process_obj):
63
+ code = 1
64
+ parent_r, child_w = os.pipe()
65
+ child_r, parent_w = os.pipe()
66
+ self.pid = os.fork()
67
+ if self.pid == 0:
68
+ try:
69
+ os.close(parent_r)
70
+ os.close(parent_w)
71
+ code = process_obj._bootstrap(parent_sentinel=child_r)
72
+ finally:
73
+ os._exit(code)
74
+ else:
75
+ os.close(child_w)
76
+ os.close(child_r)
77
+ self.finalizer = util.Finalize(self, util.close_fds,
78
+ (parent_r, parent_w,))
79
+ self.sentinel = parent_r
80
+
81
+ def close(self):
82
+ if self.finalizer is not None:
83
+ self.finalizer()
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/popen_forkserver.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import os
3
+
4
+ from .context import reduction, set_spawning_popen
5
+ if not reduction.HAVE_SEND_HANDLE:
6
+ raise ImportError('No support for sending fds between processes')
7
+ from . import forkserver
8
+ from . import popen_fork
9
+ from . import spawn
10
+ from . import util
11
+
12
+
13
+ __all__ = ['Popen']
14
+
15
+ #
16
+ # Wrapper for an fd used while launching a process
17
+ #
18
+
19
+ class _DupFd(object):
20
+ def __init__(self, ind):
21
+ self.ind = ind
22
+ def detach(self):
23
+ return forkserver.get_inherited_fds()[self.ind]
24
+
25
+ #
26
+ # Start child process using a server process
27
+ #
28
+
29
+ class Popen(popen_fork.Popen):
30
+ method = 'forkserver'
31
+ DupFd = _DupFd
32
+
33
+ def __init__(self, process_obj):
34
+ self._fds = []
35
+ super().__init__(process_obj)
36
+
37
+ def duplicate_for_child(self, fd):
38
+ self._fds.append(fd)
39
+ return len(self._fds) - 1
40
+
41
+ def _launch(self, process_obj):
42
+ prep_data = spawn.get_preparation_data(process_obj._name)
43
+ buf = io.BytesIO()
44
+ set_spawning_popen(self)
45
+ try:
46
+ reduction.dump(prep_data, buf)
47
+ reduction.dump(process_obj, buf)
48
+ finally:
49
+ set_spawning_popen(None)
50
+
51
+ self.sentinel, w = forkserver.connect_to_new_process(self._fds)
52
+ # Keep a duplicate of the data pipe's write end as a sentinel of the
53
+ # parent process used by the child process.
54
+ _parent_w = os.dup(w)
55
+ self.finalizer = util.Finalize(self, util.close_fds,
56
+ (_parent_w, self.sentinel))
57
+ with open(w, 'wb', closefd=True) as f:
58
+ f.write(buf.getbuffer())
59
+ self.pid = forkserver.read_signed(self.sentinel)
60
+
61
+ def poll(self, flag=os.WNOHANG):
62
+ if self.returncode is None:
63
+ from multiprocess.connection import wait
64
+ timeout = 0 if flag == os.WNOHANG else None
65
+ if not wait([self.sentinel], timeout):
66
+ return None
67
+ try:
68
+ self.returncode = forkserver.read_signed(self.sentinel)
69
+ except (OSError, EOFError):
70
+ # This should not happen usually, but perhaps the forkserver
71
+ # process itself got killed
72
+ self.returncode = 255
73
+
74
+ return self.returncode
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/popen_spawn_posix.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import os
3
+
4
+ from .context import reduction, set_spawning_popen
5
+ from . import popen_fork
6
+ from . import spawn
7
+ from . import util
8
+
9
+ __all__ = ['Popen']
10
+
11
+
12
+ #
13
+ # Wrapper for an fd used while launching a process
14
+ #
15
+
16
+ class _DupFd(object):
17
+ def __init__(self, fd):
18
+ self.fd = fd
19
+ def detach(self):
20
+ return self.fd
21
+
22
+ #
23
+ # Start child process using a fresh interpreter
24
+ #
25
+
26
+ class Popen(popen_fork.Popen):
27
+ method = 'spawn'
28
+ DupFd = _DupFd
29
+
30
+ def __init__(self, process_obj):
31
+ self._fds = []
32
+ super().__init__(process_obj)
33
+
34
+ def duplicate_for_child(self, fd):
35
+ self._fds.append(fd)
36
+ return fd
37
+
38
+ def _launch(self, process_obj):
39
+ from . import resource_tracker
40
+ tracker_fd = resource_tracker.getfd()
41
+ self._fds.append(tracker_fd)
42
+ prep_data = spawn.get_preparation_data(process_obj._name)
43
+ fp = io.BytesIO()
44
+ set_spawning_popen(self)
45
+ try:
46
+ reduction.dump(prep_data, fp)
47
+ reduction.dump(process_obj, fp)
48
+ finally:
49
+ set_spawning_popen(None)
50
+
51
+ parent_r = child_w = child_r = parent_w = None
52
+ try:
53
+ parent_r, child_w = os.pipe()
54
+ child_r, parent_w = os.pipe()
55
+ cmd = spawn.get_command_line(tracker_fd=tracker_fd,
56
+ pipe_handle=child_r)
57
+ self._fds.extend([child_r, child_w])
58
+ self.pid = util.spawnv_passfds(spawn.get_executable(),
59
+ cmd, self._fds)
60
+ self.sentinel = parent_r
61
+ with open(parent_w, 'wb', closefd=False) as f:
62
+ f.write(fp.getbuffer())
63
+ finally:
64
+ fds_to_close = []
65
+ for fd in (parent_r, parent_w):
66
+ if fd is not None:
67
+ fds_to_close.append(fd)
68
+ self.finalizer = util.Finalize(self, util.close_fds, fds_to_close)
69
+
70
+ for fd in (child_r, child_w):
71
+ if fd is not None:
72
+ os.close(fd)
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/popen_spawn_win32.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import msvcrt
3
+ import signal
4
+ import sys
5
+ import _winapi
6
+
7
+ from .context import reduction, get_spawning_popen, set_spawning_popen
8
+ from . import spawn
9
+ from . import util
10
+
11
+ __all__ = ['Popen']
12
+
13
+ #
14
+ #
15
+ #
16
+
17
+ TERMINATE = 0x10000
18
+ WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))
19
+ WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")
20
+
21
+
22
+ def _path_eq(p1, p2):
23
+ return p1 == p2 or os.path.normcase(p1) == os.path.normcase(p2)
24
+
25
+ WINENV = not _path_eq(sys.executable, sys._base_executable)
26
+
27
+
28
+ def _close_handles(*handles):
29
+ for handle in handles:
30
+ _winapi.CloseHandle(handle)
31
+
32
+
33
+ #
34
+ # We define a Popen class similar to the one from subprocess, but
35
+ # whose constructor takes a process object as its argument.
36
+ #
37
+
38
+ class Popen(object):
39
+ '''
40
+ Start a subprocess to run the code of a process object
41
+ '''
42
+ method = 'spawn'
43
+
44
+ def __init__(self, process_obj):
45
+ prep_data = spawn.get_preparation_data(process_obj._name)
46
+
47
+ # read end of pipe will be duplicated by the child process
48
+ # -- see spawn_main() in spawn.py.
49
+ #
50
+ # bpo-33929: Previously, the read end of pipe was "stolen" by the child
51
+ # process, but it leaked a handle if the child process had been
52
+ # terminated before it could steal the handle from the parent process.
53
+ rhandle, whandle = _winapi.CreatePipe(None, 0)
54
+ wfd = msvcrt.open_osfhandle(whandle, 0)
55
+ cmd = spawn.get_command_line(parent_pid=os.getpid(),
56
+ pipe_handle=rhandle)
57
+
58
+ python_exe = spawn.get_executable()
59
+
60
+ # bpo-35797: When running in a venv, we bypass the redirect
61
+ # executor and launch our base Python.
62
+ if WINENV and _path_eq(python_exe, sys.executable):
63
+ cmd[0] = python_exe = sys._base_executable
64
+ env = os.environ.copy()
65
+ env["__PYVENV_LAUNCHER__"] = sys.executable
66
+ else:
67
+ env = None
68
+
69
+ cmd = ' '.join('"%s"' % x for x in cmd)
70
+
71
+ with open(wfd, 'wb', closefd=True) as to_child:
72
+ # start process
73
+ try:
74
+ hp, ht, pid, tid = _winapi.CreateProcess(
75
+ python_exe, cmd,
76
+ None, None, False, 0, env, None, None)
77
+ _winapi.CloseHandle(ht)
78
+ except:
79
+ _winapi.CloseHandle(rhandle)
80
+ raise
81
+
82
+ # set attributes of self
83
+ self.pid = pid
84
+ self.returncode = None
85
+ self._handle = hp
86
+ self.sentinel = int(hp)
87
+ self.finalizer = util.Finalize(self, _close_handles,
88
+ (self.sentinel, int(rhandle)))
89
+
90
+ # send information to child
91
+ set_spawning_popen(self)
92
+ try:
93
+ reduction.dump(prep_data, to_child)
94
+ reduction.dump(process_obj, to_child)
95
+ finally:
96
+ set_spawning_popen(None)
97
+
98
+ def duplicate_for_child(self, handle):
99
+ assert self is get_spawning_popen()
100
+ return reduction.duplicate(handle, self.sentinel)
101
+
102
+ def wait(self, timeout=None):
103
+ if self.returncode is None:
104
+ if timeout is None:
105
+ msecs = _winapi.INFINITE
106
+ else:
107
+ msecs = max(0, int(timeout * 1000 + 0.5))
108
+
109
+ res = _winapi.WaitForSingleObject(int(self._handle), msecs)
110
+ if res == _winapi.WAIT_OBJECT_0:
111
+ code = _winapi.GetExitCodeProcess(self._handle)
112
+ if code == TERMINATE:
113
+ code = -signal.SIGTERM
114
+ self.returncode = code
115
+
116
+ return self.returncode
117
+
118
+ def poll(self):
119
+ return self.wait(timeout=0)
120
+
121
+ def terminate(self):
122
+ if self.returncode is None:
123
+ try:
124
+ _winapi.TerminateProcess(int(self._handle), TERMINATE)
125
+ except OSError:
126
+ if self.wait(timeout=1.0) is None:
127
+ raise
128
+
129
+ kill = terminate
130
+
131
+ def close(self):
132
+ self.finalizer()
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/process.py ADDED
@@ -0,0 +1,439 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Module providing the `Process` class which emulates `threading.Thread`
3
+ #
4
+ # multiprocessing/process.py
5
+ #
6
+ # Copyright (c) 2006-2008, R Oudkerk
7
+ # Licensed to PSF under a Contributor Agreement.
8
+ #
9
+
10
+ __all__ = ['BaseProcess', 'current_process', 'active_children',
11
+ 'parent_process']
12
+
13
+ #
14
+ # Imports
15
+ #
16
+
17
+ import os
18
+ import sys
19
+ import signal
20
+ import itertools
21
+ import threading
22
+ from _weakrefset import WeakSet
23
+
24
+ #
25
+ #
26
+ #
27
+
28
+ try:
29
+ ORIGINAL_DIR = os.path.abspath(os.getcwd())
30
+ except OSError:
31
+ ORIGINAL_DIR = None
32
+
33
+ #
34
+ # Public functions
35
+ #
36
+
37
+ def current_process():
38
+ '''
39
+ Return process object representing the current process
40
+ '''
41
+ return _current_process
42
+
43
+ def active_children():
44
+ '''
45
+ Return list of process objects corresponding to live child processes
46
+ '''
47
+ _cleanup()
48
+ return list(_children)
49
+
50
+
51
+ def parent_process():
52
+ '''
53
+ Return process object representing the parent process
54
+ '''
55
+ return _parent_process
56
+
57
+ #
58
+ #
59
+ #
60
+
61
+ def _cleanup():
62
+ # check for processes which have finished
63
+ for p in list(_children):
64
+ if (child_popen := p._popen) and child_popen.poll() is not None:
65
+ _children.discard(p)
66
+
67
+ #
68
+ # The `Process` class
69
+ #
70
+
71
+ class BaseProcess(object):
72
+ '''
73
+ Process objects represent activity that is run in a separate process
74
+
75
+ The class is analogous to `threading.Thread`
76
+ '''
77
+ def _Popen(self):
78
+ raise NotImplementedError
79
+
80
+ def __init__(self, group=None, target=None, name=None, args=(), kwargs={},
81
+ *, daemon=None):
82
+ assert group is None, 'group argument must be None for now'
83
+ count = next(_process_counter)
84
+ self._identity = _current_process._identity + (count,)
85
+ self._config = _current_process._config.copy()
86
+ self._parent_pid = os.getpid()
87
+ self._parent_name = _current_process.name
88
+ self._popen = None
89
+ self._closed = False
90
+ self._target = target
91
+ self._args = tuple(args)
92
+ self._kwargs = dict(kwargs)
93
+ self._name = name or type(self).__name__ + '-' + \
94
+ ':'.join(str(i) for i in self._identity)
95
+ if daemon is not None:
96
+ self.daemon = daemon
97
+ _dangling.add(self)
98
+
99
+ def _check_closed(self):
100
+ if self._closed:
101
+ raise ValueError("process object is closed")
102
+
103
+ def run(self):
104
+ '''
105
+ Method to be run in sub-process; can be overridden in sub-class
106
+ '''
107
+ if self._target:
108
+ self._target(*self._args, **self._kwargs)
109
+
110
+ def start(self):
111
+ '''
112
+ Start child process
113
+ '''
114
+ self._check_closed()
115
+ assert self._popen is None, 'cannot start a process twice'
116
+ assert self._parent_pid == os.getpid(), \
117
+ 'can only start a process object created by current process'
118
+ assert not _current_process._config.get('daemon'), \
119
+ 'daemonic processes are not allowed to have children'
120
+ _cleanup()
121
+ self._popen = self._Popen(self)
122
+ self._sentinel = self._popen.sentinel
123
+ # Avoid a refcycle if the target function holds an indirect
124
+ # reference to the process object (see bpo-30775)
125
+ del self._target, self._args, self._kwargs
126
+ _children.add(self)
127
+
128
+ def terminate(self):
129
+ '''
130
+ Terminate process; sends SIGTERM signal or uses TerminateProcess()
131
+ '''
132
+ self._check_closed()
133
+ self._popen.terminate()
134
+
135
+ def kill(self):
136
+ '''
137
+ Terminate process; sends SIGKILL signal or uses TerminateProcess()
138
+ '''
139
+ self._check_closed()
140
+ self._popen.kill()
141
+
142
+ def join(self, timeout=None):
143
+ '''
144
+ Wait until child process terminates
145
+ '''
146
+ self._check_closed()
147
+ assert self._parent_pid == os.getpid(), 'can only join a child process'
148
+ assert self._popen is not None, 'can only join a started process'
149
+ res = self._popen.wait(timeout)
150
+ if res is not None:
151
+ _children.discard(self)
152
+
153
+ def is_alive(self):
154
+ '''
155
+ Return whether process is alive
156
+ '''
157
+ self._check_closed()
158
+ if self is _current_process:
159
+ return True
160
+ assert self._parent_pid == os.getpid(), 'can only test a child process'
161
+
162
+ if self._popen is None:
163
+ return False
164
+
165
+ returncode = self._popen.poll()
166
+ if returncode is None:
167
+ return True
168
+ else:
169
+ _children.discard(self)
170
+ return False
171
+
172
+ def close(self):
173
+ '''
174
+ Close the Process object.
175
+
176
+ This method releases resources held by the Process object. It is
177
+ an error to call this method if the child process is still running.
178
+ '''
179
+ if self._popen is not None:
180
+ if self._popen.poll() is None:
181
+ raise ValueError("Cannot close a process while it is still running. "
182
+ "You should first call join() or terminate().")
183
+ self._popen.close()
184
+ self._popen = None
185
+ del self._sentinel
186
+ _children.discard(self)
187
+ self._closed = True
188
+
189
+ @property
190
+ def name(self):
191
+ return self._name
192
+
193
+ @name.setter
194
+ def name(self, name):
195
+ assert isinstance(name, str), 'name must be a string'
196
+ self._name = name
197
+
198
+ @property
199
+ def daemon(self):
200
+ '''
201
+ Return whether process is a daemon
202
+ '''
203
+ return self._config.get('daemon', False)
204
+
205
+ @daemon.setter
206
+ def daemon(self, daemonic):
207
+ '''
208
+ Set whether process is a daemon
209
+ '''
210
+ assert self._popen is None, 'process has already started'
211
+ self._config['daemon'] = daemonic
212
+
213
+ @property
214
+ def authkey(self):
215
+ return self._config['authkey']
216
+
217
+ @authkey.setter
218
+ def authkey(self, authkey):
219
+ '''
220
+ Set authorization key of process
221
+ '''
222
+ self._config['authkey'] = AuthenticationString(authkey)
223
+
224
+ @property
225
+ def exitcode(self):
226
+ '''
227
+ Return exit code of process or `None` if it has yet to stop
228
+ '''
229
+ self._check_closed()
230
+ if self._popen is None:
231
+ return self._popen
232
+ return self._popen.poll()
233
+
234
+ @property
235
+ def ident(self):
236
+ '''
237
+ Return identifier (PID) of process or `None` if it has yet to start
238
+ '''
239
+ self._check_closed()
240
+ if self is _current_process:
241
+ return os.getpid()
242
+ else:
243
+ return self._popen and self._popen.pid
244
+
245
+ pid = ident
246
+
247
+ @property
248
+ def sentinel(self):
249
+ '''
250
+ Return a file descriptor (Unix) or handle (Windows) suitable for
251
+ waiting for process termination.
252
+ '''
253
+ self._check_closed()
254
+ try:
255
+ return self._sentinel
256
+ except AttributeError:
257
+ raise ValueError("process not started") from None
258
+
259
+ def __repr__(self):
260
+ exitcode = None
261
+ if self is _current_process:
262
+ status = 'started'
263
+ elif self._closed:
264
+ status = 'closed'
265
+ elif self._parent_pid != os.getpid():
266
+ status = 'unknown'
267
+ elif self._popen is None:
268
+ status = 'initial'
269
+ else:
270
+ exitcode = self._popen.poll()
271
+ if exitcode is not None:
272
+ status = 'stopped'
273
+ else:
274
+ status = 'started'
275
+
276
+ info = [type(self).__name__, 'name=%r' % self._name]
277
+ if self._popen is not None:
278
+ info.append('pid=%s' % self._popen.pid)
279
+ info.append('parent=%s' % self._parent_pid)
280
+ info.append(status)
281
+ if exitcode is not None:
282
+ exitcode = _exitcode_to_name.get(exitcode, exitcode)
283
+ info.append('exitcode=%s' % exitcode)
284
+ if self.daemon:
285
+ info.append('daemon')
286
+ return '<%s>' % ' '.join(info)
287
+
288
+ ##
289
+
290
+ def _bootstrap(self, parent_sentinel=None):
291
+ from . import util, context
292
+ global _current_process, _parent_process, _process_counter, _children
293
+
294
+ try:
295
+ if self._start_method is not None:
296
+ context._force_start_method(self._start_method)
297
+ _process_counter = itertools.count(1)
298
+ _children = set()
299
+ util._close_stdin()
300
+ old_process = _current_process
301
+ _current_process = self
302
+ _parent_process = _ParentProcess(
303
+ self._parent_name, self._parent_pid, parent_sentinel)
304
+ if threading._HAVE_THREAD_NATIVE_ID:
305
+ threading.main_thread()._set_native_id()
306
+ try:
307
+ self._after_fork()
308
+ finally:
309
+ # delay finalization of the old process object until after
310
+ # _run_after_forkers() is executed
311
+ del old_process
312
+ util.info('child process calling self.run()')
313
+ try:
314
+ self.run()
315
+ exitcode = 0
316
+ finally:
317
+ util._exit_function()
318
+ except SystemExit as e:
319
+ if e.code is None:
320
+ exitcode = 0
321
+ elif isinstance(e.code, int):
322
+ exitcode = e.code
323
+ else:
324
+ sys.stderr.write(str(e.code) + '\n')
325
+ exitcode = 1
326
+ except:
327
+ exitcode = 1
328
+ import traceback
329
+ sys.stderr.write('Process %s:\n' % self.name)
330
+ traceback.print_exc()
331
+ finally:
332
+ threading._shutdown()
333
+ util.info('process exiting with exitcode %d' % exitcode)
334
+ util._flush_std_streams()
335
+
336
+ return exitcode
337
+
338
+ @staticmethod
339
+ def _after_fork():
340
+ from . import util
341
+ util._finalizer_registry.clear()
342
+ util._run_after_forkers()
343
+
344
+
345
+ #
346
+ # We subclass bytes to avoid accidental transmission of auth keys over network
347
+ #
348
+
349
+ class AuthenticationString(bytes):
350
+ def __reduce__(self):
351
+ from .context import get_spawning_popen
352
+ if get_spawning_popen() is None:
353
+ raise TypeError(
354
+ 'Pickling an AuthenticationString object is '
355
+ 'disallowed for security reasons'
356
+ )
357
+ return AuthenticationString, (bytes(self),)
358
+
359
+
360
+ #
361
+ # Create object representing the parent process
362
+ #
363
+
364
+ class _ParentProcess(BaseProcess):
365
+
366
+ def __init__(self, name, pid, sentinel):
367
+ self._identity = ()
368
+ self._name = name
369
+ self._pid = pid
370
+ self._parent_pid = None
371
+ self._popen = None
372
+ self._closed = False
373
+ self._sentinel = sentinel
374
+ self._config = {}
375
+
376
+ def is_alive(self):
377
+ from multiprocess.connection import wait
378
+ return not wait([self._sentinel], timeout=0)
379
+
380
+ @property
381
+ def ident(self):
382
+ return self._pid
383
+
384
+ def join(self, timeout=None):
385
+ '''
386
+ Wait until parent process terminates
387
+ '''
388
+ from multiprocess.connection import wait
389
+ wait([self._sentinel], timeout=timeout)
390
+
391
+ pid = ident
392
+
393
+ #
394
+ # Create object representing the main process
395
+ #
396
+
397
+ class _MainProcess(BaseProcess):
398
+
399
+ def __init__(self):
400
+ self._identity = ()
401
+ self._name = 'MainProcess'
402
+ self._parent_pid = None
403
+ self._popen = None
404
+ self._closed = False
405
+ self._config = {'authkey': AuthenticationString(os.urandom(32)),
406
+ 'semprefix': '/mp'}
407
+ # Note that some versions of FreeBSD only allow named
408
+ # semaphores to have names of up to 14 characters. Therefore
409
+ # we choose a short prefix.
410
+ #
411
+ # On MacOSX in a sandbox it may be necessary to use a
412
+ # different prefix -- see #19478.
413
+ #
414
+ # Everything in self._config will be inherited by descendant
415
+ # processes.
416
+
417
+ def close(self):
418
+ pass
419
+
420
+
421
+ _parent_process = None
422
+ _current_process = _MainProcess()
423
+ _process_counter = itertools.count(1)
424
+ _children = set()
425
+ del _MainProcess
426
+
427
+ #
428
+ # Give names to some return codes
429
+ #
430
+
431
+ _exitcode_to_name = {}
432
+
433
+ for name, signum in list(signal.__dict__.items()):
434
+ if name[:3]=='SIG' and '_' not in name:
435
+ _exitcode_to_name[-signum] = f'-{name}'
436
+ del name, signum
437
+
438
+ # For debug and leak testing
439
+ _dangling = WeakSet()
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/reduction.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Module which deals with pickling of objects.
3
+ #
4
+ # multiprocessing/reduction.py
5
+ #
6
+ # Copyright (c) 2006-2008, R Oudkerk
7
+ # Licensed to PSF under a Contributor Agreement.
8
+ #
9
+
10
+ from abc import ABCMeta
11
+ import copyreg
12
+ import functools
13
+ import io
14
+ import os
15
+ try:
16
+ import dill as pickle
17
+ except ImportError:
18
+ import pickle
19
+ import socket
20
+ import sys
21
+
22
+ from . import context
23
+
24
+ __all__ = ['send_handle', 'recv_handle', 'ForkingPickler', 'register', 'dump']
25
+
26
+
27
+ HAVE_SEND_HANDLE = (sys.platform == 'win32' or
28
+ (hasattr(socket, 'CMSG_LEN') and
29
+ hasattr(socket, 'SCM_RIGHTS') and
30
+ hasattr(socket.socket, 'sendmsg')))
31
+
32
+ #
33
+ # Pickler subclass
34
+ #
35
+
36
+ class ForkingPickler(pickle.Pickler):
37
+ '''Pickler subclass used by multiprocess.'''
38
+ _extra_reducers = {}
39
+ _copyreg_dispatch_table = copyreg.dispatch_table
40
+
41
+ def __init__(self, *args, **kwds):
42
+ super().__init__(*args, **kwds)
43
+ self.dispatch_table = self._copyreg_dispatch_table.copy()
44
+ self.dispatch_table.update(self._extra_reducers)
45
+
46
+ @classmethod
47
+ def register(cls, type, reduce):
48
+ '''Register a reduce function for a type.'''
49
+ cls._extra_reducers[type] = reduce
50
+
51
+ @classmethod
52
+ def dumps(cls, obj, protocol=None, *args, **kwds):
53
+ buf = io.BytesIO()
54
+ cls(buf, protocol, *args, **kwds).dump(obj)
55
+ return buf.getbuffer()
56
+
57
+ loads = pickle.loads
58
+
59
+ register = ForkingPickler.register
60
+
61
+ def dump(obj, file, protocol=None, *args, **kwds):
62
+ '''Replacement for pickle.dump() using ForkingPickler.'''
63
+ ForkingPickler(file, protocol, *args, **kwds).dump(obj)
64
+
65
+ #
66
+ # Platform specific definitions
67
+ #
68
+
69
+ if sys.platform == 'win32':
70
+ # Windows
71
+ __all__ += ['DupHandle', 'duplicate', 'steal_handle']
72
+ import _winapi
73
+
74
+ def duplicate(handle, target_process=None, inheritable=False,
75
+ *, source_process=None):
76
+ '''Duplicate a handle. (target_process is a handle not a pid!)'''
77
+ current_process = _winapi.GetCurrentProcess()
78
+ if source_process is None:
79
+ source_process = current_process
80
+ if target_process is None:
81
+ target_process = current_process
82
+ return _winapi.DuplicateHandle(
83
+ source_process, handle, target_process,
84
+ 0, inheritable, _winapi.DUPLICATE_SAME_ACCESS)
85
+
86
+ def steal_handle(source_pid, handle):
87
+ '''Steal a handle from process identified by source_pid.'''
88
+ source_process_handle = _winapi.OpenProcess(
89
+ _winapi.PROCESS_DUP_HANDLE, False, source_pid)
90
+ try:
91
+ return _winapi.DuplicateHandle(
92
+ source_process_handle, handle,
93
+ _winapi.GetCurrentProcess(), 0, False,
94
+ _winapi.DUPLICATE_SAME_ACCESS | _winapi.DUPLICATE_CLOSE_SOURCE)
95
+ finally:
96
+ _winapi.CloseHandle(source_process_handle)
97
+
98
+ def send_handle(conn, handle, destination_pid):
99
+ '''Send a handle over a local connection.'''
100
+ dh = DupHandle(handle, _winapi.DUPLICATE_SAME_ACCESS, destination_pid)
101
+ conn.send(dh)
102
+
103
+ def recv_handle(conn):
104
+ '''Receive a handle over a local connection.'''
105
+ return conn.recv().detach()
106
+
107
+ class DupHandle(object):
108
+ '''Picklable wrapper for a handle.'''
109
+ def __init__(self, handle, access, pid=None):
110
+ if pid is None:
111
+ # We just duplicate the handle in the current process and
112
+ # let the receiving process steal the handle.
113
+ pid = os.getpid()
114
+ proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False, pid)
115
+ try:
116
+ self._handle = _winapi.DuplicateHandle(
117
+ _winapi.GetCurrentProcess(),
118
+ handle, proc, access, False, 0)
119
+ finally:
120
+ _winapi.CloseHandle(proc)
121
+ self._access = access
122
+ self._pid = pid
123
+
124
+ def detach(self):
125
+ '''Get the handle. This should only be called once.'''
126
+ # retrieve handle from process which currently owns it
127
+ if self._pid == os.getpid():
128
+ # The handle has already been duplicated for this process.
129
+ return self._handle
130
+ # We must steal the handle from the process whose pid is self._pid.
131
+ proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False,
132
+ self._pid)
133
+ try:
134
+ return _winapi.DuplicateHandle(
135
+ proc, self._handle, _winapi.GetCurrentProcess(),
136
+ self._access, False, _winapi.DUPLICATE_CLOSE_SOURCE)
137
+ finally:
138
+ _winapi.CloseHandle(proc)
139
+
140
+ else:
141
+ # Unix
142
+ __all__ += ['DupFd', 'sendfds', 'recvfds']
143
+ import array
144
+
145
+ # On MacOSX we should acknowledge receipt of fds -- see Issue14669
146
+ ACKNOWLEDGE = sys.platform == 'darwin'
147
+
148
+ def sendfds(sock, fds):
149
+ '''Send an array of fds over an AF_UNIX socket.'''
150
+ fds = array.array('i', fds)
151
+ msg = bytes([len(fds) % 256])
152
+ sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)])
153
+ if ACKNOWLEDGE and sock.recv(1) != b'A':
154
+ raise RuntimeError('did not receive acknowledgement of fd')
155
+
156
+ def recvfds(sock, size):
157
+ '''Receive an array of fds over an AF_UNIX socket.'''
158
+ a = array.array('i')
159
+ bytes_size = a.itemsize * size
160
+ msg, ancdata, flags, addr = sock.recvmsg(1, socket.CMSG_SPACE(bytes_size))
161
+ if not msg and not ancdata:
162
+ raise EOFError
163
+ try:
164
+ if ACKNOWLEDGE:
165
+ sock.send(b'A')
166
+ if len(ancdata) != 1:
167
+ raise RuntimeError('received %d items of ancdata' %
168
+ len(ancdata))
169
+ cmsg_level, cmsg_type, cmsg_data = ancdata[0]
170
+ if (cmsg_level == socket.SOL_SOCKET and
171
+ cmsg_type == socket.SCM_RIGHTS):
172
+ if len(cmsg_data) % a.itemsize != 0:
173
+ raise ValueError
174
+ a.frombytes(cmsg_data)
175
+ if len(a) % 256 != msg[0]:
176
+ raise AssertionError(
177
+ "Len is {0:n} but msg[0] is {1!r}".format(
178
+ len(a), msg[0]))
179
+ return list(a)
180
+ except (ValueError, IndexError):
181
+ pass
182
+ raise RuntimeError('Invalid data received')
183
+
184
+ def send_handle(conn, handle, destination_pid):
185
+ '''Send a handle over a local connection.'''
186
+ with socket.fromfd(conn.fileno(), socket.AF_UNIX, socket.SOCK_STREAM) as s:
187
+ sendfds(s, [handle])
188
+
189
+ def recv_handle(conn):
190
+ '''Receive a handle over a local connection.'''
191
+ with socket.fromfd(conn.fileno(), socket.AF_UNIX, socket.SOCK_STREAM) as s:
192
+ return recvfds(s, 1)[0]
193
+
194
+ def DupFd(fd):
195
+ '''Return a wrapper for an fd.'''
196
+ popen_obj = context.get_spawning_popen()
197
+ if popen_obj is not None:
198
+ return popen_obj.DupFd(popen_obj.duplicate_for_child(fd))
199
+ elif HAVE_SEND_HANDLE:
200
+ from . import resource_sharer
201
+ return resource_sharer.DupFd(fd)
202
+ else:
203
+ raise ValueError('SCM_RIGHTS appears not to be available')
204
+
205
+ #
206
+ # Try making some callable types picklable
207
+ #
208
+
209
+ def _reduce_method(m):
210
+ if m.__self__ is None:
211
+ return getattr, (m.__class__, m.__func__.__name__)
212
+ else:
213
+ return getattr, (m.__self__, m.__func__.__name__)
214
+ class _C:
215
+ def f(self):
216
+ pass
217
+ register(type(_C().f), _reduce_method)
218
+
219
+
220
+ def _reduce_method_descriptor(m):
221
+ return getattr, (m.__objclass__, m.__name__)
222
+ register(type(list.append), _reduce_method_descriptor)
223
+ register(type(int.__add__), _reduce_method_descriptor)
224
+
225
+
226
+ def _reduce_partial(p):
227
+ return _rebuild_partial, (p.func, p.args, p.keywords or {})
228
+ def _rebuild_partial(func, args, keywords):
229
+ return functools.partial(func, *args, **keywords)
230
+ register(functools.partial, _reduce_partial)
231
+
232
+ #
233
+ # Make sockets picklable
234
+ #
235
+
236
+ if sys.platform == 'win32':
237
+ def _reduce_socket(s):
238
+ from .resource_sharer import DupSocket
239
+ return _rebuild_socket, (DupSocket(s),)
240
+ def _rebuild_socket(ds):
241
+ return ds.detach()
242
+ register(socket.socket, _reduce_socket)
243
+
244
+ else:
245
+ def _reduce_socket(s):
246
+ df = DupFd(s.fileno())
247
+ return _rebuild_socket, (df, s.family, s.type, s.proto)
248
+ def _rebuild_socket(df, family, type, proto):
249
+ fd = df.detach()
250
+ return socket.socket(family, type, proto, fileno=fd)
251
+ register(socket.socket, _reduce_socket)
252
+
253
+
254
+ class AbstractReducer(metaclass=ABCMeta):
255
+ '''Abstract base class for use in implementing a Reduction class
256
+ suitable for use in replacing the standard reduction mechanism
257
+ used in multiprocess.'''
258
+ ForkingPickler = ForkingPickler
259
+ register = register
260
+ dump = dump
261
+ send_handle = send_handle
262
+ recv_handle = recv_handle
263
+
264
+ if sys.platform == 'win32':
265
+ steal_handle = steal_handle
266
+ duplicate = duplicate
267
+ DupHandle = DupHandle
268
+ else:
269
+ sendfds = sendfds
270
+ recvfds = recvfds
271
+ DupFd = DupFd
272
+
273
+ _reduce_method = _reduce_method
274
+ _reduce_method_descriptor = _reduce_method_descriptor
275
+ _rebuild_partial = _rebuild_partial
276
+ _reduce_socket = _reduce_socket
277
+ _rebuild_socket = _rebuild_socket
278
+
279
+ def __init__(self, *args):
280
+ register(type(_C().f), _reduce_method)
281
+ register(type(list.append), _reduce_method_descriptor)
282
+ register(type(int.__add__), _reduce_method_descriptor)
283
+ register(functools.partial, _reduce_partial)
284
+ register(socket.socket, _reduce_socket)
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/shared_memory.py ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Provides shared memory for direct access across processes.
2
+
3
+ The API of this package is currently provisional. Refer to the
4
+ documentation for details.
5
+ """
6
+
7
+
8
+ __all__ = [ 'SharedMemory', 'ShareableList' ]
9
+
10
+
11
+ from functools import partial
12
+ import mmap
13
+ import os
14
+ import errno
15
+ import struct
16
+ import secrets
17
+ import types
18
+
19
+ if os.name == "nt":
20
+ import _winapi
21
+ _USE_POSIX = False
22
+ else:
23
+ import _posixshmem
24
+ _USE_POSIX = True
25
+
26
+ from . import resource_tracker
27
+
28
+ _O_CREX = os.O_CREAT | os.O_EXCL
29
+
30
+ # FreeBSD (and perhaps other BSDs) limit names to 14 characters.
31
+ _SHM_SAFE_NAME_LENGTH = 14
32
+
33
+ # Shared memory block name prefix
34
+ if _USE_POSIX:
35
+ _SHM_NAME_PREFIX = '/psm_'
36
+ else:
37
+ _SHM_NAME_PREFIX = 'wnsm_'
38
+
39
+
40
+ def _make_filename():
41
+ "Create a random filename for the shared memory object."
42
+ # number of random bytes to use for name
43
+ nbytes = (_SHM_SAFE_NAME_LENGTH - len(_SHM_NAME_PREFIX)) // 2
44
+ assert nbytes >= 2, '_SHM_NAME_PREFIX too long'
45
+ name = _SHM_NAME_PREFIX + secrets.token_hex(nbytes)
46
+ assert len(name) <= _SHM_SAFE_NAME_LENGTH
47
+ return name
48
+
49
+
50
+ class SharedMemory:
51
+ """Creates a new shared memory block or attaches to an existing
52
+ shared memory block.
53
+
54
+ Every shared memory block is assigned a unique name. This enables
55
+ one process to create a shared memory block with a particular name
56
+ so that a different process can attach to that same shared memory
57
+ block using that same name.
58
+
59
+ As a resource for sharing data across processes, shared memory blocks
60
+ may outlive the original process that created them. When one process
61
+ no longer needs access to a shared memory block that might still be
62
+ needed by other processes, the close() method should be called.
63
+ When a shared memory block is no longer needed by any process, the
64
+ unlink() method should be called to ensure proper cleanup."""
65
+
66
+ # Defaults; enables close() and unlink() to run without errors.
67
+ _name = None
68
+ _fd = -1
69
+ _mmap = None
70
+ _buf = None
71
+ _flags = os.O_RDWR
72
+ _mode = 0o600
73
+ _prepend_leading_slash = True if _USE_POSIX else False
74
+
75
+ def __init__(self, name=None, create=False, size=0):
76
+ if not size >= 0:
77
+ raise ValueError("'size' must be a positive integer")
78
+ if create:
79
+ self._flags = _O_CREX | os.O_RDWR
80
+ if size == 0:
81
+ raise ValueError("'size' must be a positive number different from zero")
82
+ if name is None and not self._flags & os.O_EXCL:
83
+ raise ValueError("'name' can only be None if create=True")
84
+
85
+ if _USE_POSIX:
86
+
87
+ # POSIX Shared Memory
88
+
89
+ if name is None:
90
+ while True:
91
+ name = _make_filename()
92
+ try:
93
+ self._fd = _posixshmem.shm_open(
94
+ name,
95
+ self._flags,
96
+ mode=self._mode
97
+ )
98
+ except FileExistsError:
99
+ continue
100
+ self._name = name
101
+ break
102
+ else:
103
+ name = "/" + name if self._prepend_leading_slash else name
104
+ self._fd = _posixshmem.shm_open(
105
+ name,
106
+ self._flags,
107
+ mode=self._mode
108
+ )
109
+ self._name = name
110
+ try:
111
+ if create and size:
112
+ os.ftruncate(self._fd, size)
113
+ stats = os.fstat(self._fd)
114
+ size = stats.st_size
115
+ self._mmap = mmap.mmap(self._fd, size)
116
+ except OSError:
117
+ self.unlink()
118
+ raise
119
+
120
+ resource_tracker.register(self._name, "shared_memory")
121
+
122
+ else:
123
+
124
+ # Windows Named Shared Memory
125
+
126
+ if create:
127
+ while True:
128
+ temp_name = _make_filename() if name is None else name
129
+ # Create and reserve shared memory block with this name
130
+ # until it can be attached to by mmap.
131
+ h_map = _winapi.CreateFileMapping(
132
+ _winapi.INVALID_HANDLE_VALUE,
133
+ _winapi.NULL,
134
+ _winapi.PAGE_READWRITE,
135
+ (size >> 32) & 0xFFFFFFFF,
136
+ size & 0xFFFFFFFF,
137
+ temp_name
138
+ )
139
+ try:
140
+ last_error_code = _winapi.GetLastError()
141
+ if last_error_code == _winapi.ERROR_ALREADY_EXISTS:
142
+ if name is not None:
143
+ raise FileExistsError(
144
+ errno.EEXIST,
145
+ os.strerror(errno.EEXIST),
146
+ name,
147
+ _winapi.ERROR_ALREADY_EXISTS
148
+ )
149
+ else:
150
+ continue
151
+ self._mmap = mmap.mmap(-1, size, tagname=temp_name)
152
+ finally:
153
+ _winapi.CloseHandle(h_map)
154
+ self._name = temp_name
155
+ break
156
+
157
+ else:
158
+ self._name = name
159
+ # Dynamically determine the existing named shared memory
160
+ # block's size which is likely a multiple of mmap.PAGESIZE.
161
+ h_map = _winapi.OpenFileMapping(
162
+ _winapi.FILE_MAP_READ,
163
+ False,
164
+ name
165
+ )
166
+ try:
167
+ p_buf = _winapi.MapViewOfFile(
168
+ h_map,
169
+ _winapi.FILE_MAP_READ,
170
+ 0,
171
+ 0,
172
+ 0
173
+ )
174
+ finally:
175
+ _winapi.CloseHandle(h_map)
176
+ try:
177
+ size = _winapi.VirtualQuerySize(p_buf)
178
+ finally:
179
+ _winapi.UnmapViewOfFile(p_buf)
180
+ self._mmap = mmap.mmap(-1, size, tagname=name)
181
+
182
+ self._size = size
183
+ self._buf = memoryview(self._mmap)
184
+
185
+ def __del__(self):
186
+ try:
187
+ self.close()
188
+ except OSError:
189
+ pass
190
+
191
+ def __reduce__(self):
192
+ return (
193
+ self.__class__,
194
+ (
195
+ self.name,
196
+ False,
197
+ self.size,
198
+ ),
199
+ )
200
+
201
+ def __repr__(self):
202
+ return f'{self.__class__.__name__}({self.name!r}, size={self.size})'
203
+
204
+ @property
205
+ def buf(self):
206
+ "A memoryview of contents of the shared memory block."
207
+ return self._buf
208
+
209
+ @property
210
+ def name(self):
211
+ "Unique name that identifies the shared memory block."
212
+ reported_name = self._name
213
+ if _USE_POSIX and self._prepend_leading_slash:
214
+ if self._name.startswith("/"):
215
+ reported_name = self._name[1:]
216
+ return reported_name
217
+
218
+ @property
219
+ def size(self):
220
+ "Size in bytes."
221
+ return self._size
222
+
223
+ def close(self):
224
+ """Closes access to the shared memory from this instance but does
225
+ not destroy the shared memory block."""
226
+ if self._buf is not None:
227
+ self._buf.release()
228
+ self._buf = None
229
+ if self._mmap is not None:
230
+ self._mmap.close()
231
+ self._mmap = None
232
+ if _USE_POSIX and self._fd >= 0:
233
+ os.close(self._fd)
234
+ self._fd = -1
235
+
236
+ def unlink(self):
237
+ """Requests that the underlying shared memory block be destroyed.
238
+
239
+ In order to ensure proper cleanup of resources, unlink should be
240
+ called once (and only once) across all processes which have access
241
+ to the shared memory block."""
242
+ if _USE_POSIX and self._name:
243
+ _posixshmem.shm_unlink(self._name)
244
+ resource_tracker.unregister(self._name, "shared_memory")
245
+
246
+
247
+ _encoding = "utf8"
248
+
249
+ class ShareableList:
250
+ """Pattern for a mutable list-like object shareable via a shared
251
+ memory block. It differs from the built-in list type in that these
252
+ lists can not change their overall length (i.e. no append, insert,
253
+ etc.)
254
+
255
+ Because values are packed into a memoryview as bytes, the struct
256
+ packing format for any storable value must require no more than 8
257
+ characters to describe its format."""
258
+
259
+ # The shared memory area is organized as follows:
260
+ # - 8 bytes: number of items (N) as a 64-bit integer
261
+ # - (N + 1) * 8 bytes: offsets of each element from the start of the
262
+ # data area
263
+ # - K bytes: the data area storing item values (with encoding and size
264
+ # depending on their respective types)
265
+ # - N * 8 bytes: `struct` format string for each element
266
+ # - N bytes: index into _back_transforms_mapping for each element
267
+ # (for reconstructing the corresponding Python value)
268
+ _types_mapping = {
269
+ int: "q",
270
+ float: "d",
271
+ bool: "xxxxxxx?",
272
+ str: "%ds",
273
+ bytes: "%ds",
274
+ None.__class__: "xxxxxx?x",
275
+ }
276
+ _alignment = 8
277
+ _back_transforms_mapping = {
278
+ 0: lambda value: value, # int, float, bool
279
+ 1: lambda value: value.rstrip(b'\x00').decode(_encoding), # str
280
+ 2: lambda value: value.rstrip(b'\x00'), # bytes
281
+ 3: lambda _value: None, # None
282
+ }
283
+
284
+ @staticmethod
285
+ def _extract_recreation_code(value):
286
+ """Used in concert with _back_transforms_mapping to convert values
287
+ into the appropriate Python objects when retrieving them from
288
+ the list as well as when storing them."""
289
+ if not isinstance(value, (str, bytes, None.__class__)):
290
+ return 0
291
+ elif isinstance(value, str):
292
+ return 1
293
+ elif isinstance(value, bytes):
294
+ return 2
295
+ else:
296
+ return 3 # NoneType
297
+
298
+ def __init__(self, sequence=None, *, name=None):
299
+ if name is None or sequence is not None:
300
+ sequence = sequence or ()
301
+ _formats = [
302
+ self._types_mapping[type(item)]
303
+ if not isinstance(item, (str, bytes))
304
+ else self._types_mapping[type(item)] % (
305
+ self._alignment * (len(item) // self._alignment + 1),
306
+ )
307
+ for item in sequence
308
+ ]
309
+ self._list_len = len(_formats)
310
+ assert sum(len(fmt) <= 8 for fmt in _formats) == self._list_len
311
+ offset = 0
312
+ # The offsets of each list element into the shared memory's
313
+ # data area (0 meaning the start of the data area, not the start
314
+ # of the shared memory area).
315
+ self._allocated_offsets = [0]
316
+ for fmt in _formats:
317
+ offset += self._alignment if fmt[-1] != "s" else int(fmt[:-1])
318
+ self._allocated_offsets.append(offset)
319
+ _recreation_codes = [
320
+ self._extract_recreation_code(item) for item in sequence
321
+ ]
322
+ requested_size = struct.calcsize(
323
+ "q" + self._format_size_metainfo +
324
+ "".join(_formats) +
325
+ self._format_packing_metainfo +
326
+ self._format_back_transform_codes
327
+ )
328
+
329
+ self.shm = SharedMemory(name, create=True, size=requested_size)
330
+ else:
331
+ self.shm = SharedMemory(name)
332
+
333
+ if sequence is not None:
334
+ _enc = _encoding
335
+ struct.pack_into(
336
+ "q" + self._format_size_metainfo,
337
+ self.shm.buf,
338
+ 0,
339
+ self._list_len,
340
+ *(self._allocated_offsets)
341
+ )
342
+ struct.pack_into(
343
+ "".join(_formats),
344
+ self.shm.buf,
345
+ self._offset_data_start,
346
+ *(v.encode(_enc) if isinstance(v, str) else v for v in sequence)
347
+ )
348
+ struct.pack_into(
349
+ self._format_packing_metainfo,
350
+ self.shm.buf,
351
+ self._offset_packing_formats,
352
+ *(v.encode(_enc) for v in _formats)
353
+ )
354
+ struct.pack_into(
355
+ self._format_back_transform_codes,
356
+ self.shm.buf,
357
+ self._offset_back_transform_codes,
358
+ *(_recreation_codes)
359
+ )
360
+
361
+ else:
362
+ self._list_len = len(self) # Obtains size from offset 0 in buffer.
363
+ self._allocated_offsets = list(
364
+ struct.unpack_from(
365
+ self._format_size_metainfo,
366
+ self.shm.buf,
367
+ 1 * 8
368
+ )
369
+ )
370
+
371
+ def _get_packing_format(self, position):
372
+ "Gets the packing format for a single value stored in the list."
373
+ position = position if position >= 0 else position + self._list_len
374
+ if (position >= self._list_len) or (self._list_len < 0):
375
+ raise IndexError("Requested position out of range.")
376
+
377
+ v = struct.unpack_from(
378
+ "8s",
379
+ self.shm.buf,
380
+ self._offset_packing_formats + position * 8
381
+ )[0]
382
+ fmt = v.rstrip(b'\x00')
383
+ fmt_as_str = fmt.decode(_encoding)
384
+
385
+ return fmt_as_str
386
+
387
+ def _get_back_transform(self, position):
388
+ "Gets the back transformation function for a single value."
389
+
390
+ if (position >= self._list_len) or (self._list_len < 0):
391
+ raise IndexError("Requested position out of range.")
392
+
393
+ transform_code = struct.unpack_from(
394
+ "b",
395
+ self.shm.buf,
396
+ self._offset_back_transform_codes + position
397
+ )[0]
398
+ transform_function = self._back_transforms_mapping[transform_code]
399
+
400
+ return transform_function
401
+
402
+ def _set_packing_format_and_transform(self, position, fmt_as_str, value):
403
+ """Sets the packing format and back transformation code for a
404
+ single value in the list at the specified position."""
405
+
406
+ if (position >= self._list_len) or (self._list_len < 0):
407
+ raise IndexError("Requested position out of range.")
408
+
409
+ struct.pack_into(
410
+ "8s",
411
+ self.shm.buf,
412
+ self._offset_packing_formats + position * 8,
413
+ fmt_as_str.encode(_encoding)
414
+ )
415
+
416
+ transform_code = self._extract_recreation_code(value)
417
+ struct.pack_into(
418
+ "b",
419
+ self.shm.buf,
420
+ self._offset_back_transform_codes + position,
421
+ transform_code
422
+ )
423
+
424
+ def __getitem__(self, position):
425
+ position = position if position >= 0 else position + self._list_len
426
+ try:
427
+ offset = self._offset_data_start + self._allocated_offsets[position]
428
+ (v,) = struct.unpack_from(
429
+ self._get_packing_format(position),
430
+ self.shm.buf,
431
+ offset
432
+ )
433
+ except IndexError:
434
+ raise IndexError("index out of range")
435
+
436
+ back_transform = self._get_back_transform(position)
437
+ v = back_transform(v)
438
+
439
+ return v
440
+
441
+ def __setitem__(self, position, value):
442
+ position = position if position >= 0 else position + self._list_len
443
+ try:
444
+ item_offset = self._allocated_offsets[position]
445
+ offset = self._offset_data_start + item_offset
446
+ current_format = self._get_packing_format(position)
447
+ except IndexError:
448
+ raise IndexError("assignment index out of range")
449
+
450
+ if not isinstance(value, (str, bytes)):
451
+ new_format = self._types_mapping[type(value)]
452
+ encoded_value = value
453
+ else:
454
+ allocated_length = self._allocated_offsets[position + 1] - item_offset
455
+
456
+ encoded_value = (value.encode(_encoding)
457
+ if isinstance(value, str) else value)
458
+ if len(encoded_value) > allocated_length:
459
+ raise ValueError("bytes/str item exceeds available storage")
460
+ if current_format[-1] == "s":
461
+ new_format = current_format
462
+ else:
463
+ new_format = self._types_mapping[str] % (
464
+ allocated_length,
465
+ )
466
+
467
+ self._set_packing_format_and_transform(
468
+ position,
469
+ new_format,
470
+ value
471
+ )
472
+ struct.pack_into(new_format, self.shm.buf, offset, encoded_value)
473
+
474
+ def __reduce__(self):
475
+ return partial(self.__class__, name=self.shm.name), ()
476
+
477
+ def __len__(self):
478
+ return struct.unpack_from("q", self.shm.buf, 0)[0]
479
+
480
+ def __repr__(self):
481
+ return f'{self.__class__.__name__}({list(self)}, name={self.shm.name!r})'
482
+
483
+ @property
484
+ def format(self):
485
+ "The struct packing format used by all currently stored items."
486
+ return "".join(
487
+ self._get_packing_format(i) for i in range(self._list_len)
488
+ )
489
+
490
+ @property
491
+ def _format_size_metainfo(self):
492
+ "The struct packing format used for the items' storage offsets."
493
+ return "q" * (self._list_len + 1)
494
+
495
+ @property
496
+ def _format_packing_metainfo(self):
497
+ "The struct packing format used for the items' packing formats."
498
+ return "8s" * self._list_len
499
+
500
+ @property
501
+ def _format_back_transform_codes(self):
502
+ "The struct packing format used for the items' back transforms."
503
+ return "b" * self._list_len
504
+
505
+ @property
506
+ def _offset_data_start(self):
507
+ # - 8 bytes for the list length
508
+ # - (N + 1) * 8 bytes for the element offsets
509
+ return (self._list_len + 2) * 8
510
+
511
+ @property
512
+ def _offset_packing_formats(self):
513
+ return self._offset_data_start + self._allocated_offsets[-1]
514
+
515
+ @property
516
+ def _offset_back_transform_codes(self):
517
+ return self._offset_packing_formats + self._list_len * 8
518
+
519
+ def count(self, value):
520
+ "L.count(value) -> integer -- return number of occurrences of value."
521
+
522
+ return sum(value == entry for entry in self)
523
+
524
+ def index(self, value):
525
+ """L.index(value) -> integer -- return first index of value.
526
+ Raises ValueError if the value is not present."""
527
+
528
+ for position, entry in enumerate(self):
529
+ if value == entry:
530
+ return position
531
+ else:
532
+ raise ValueError(f"{value!r} not in this container")
533
+
534
+ __class_getitem__ = classmethod(types.GenericAlias)
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/sharedctypes.py ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Module which supports allocation of ctypes objects from shared memory
3
+ #
4
+ # multiprocessing/sharedctypes.py
5
+ #
6
+ # Copyright (c) 2006-2008, R Oudkerk
7
+ # Licensed to PSF under a Contributor Agreement.
8
+ #
9
+
10
+ import ctypes
11
+ import weakref
12
+
13
+ from . import heap
14
+ from . import get_context
15
+
16
+ from .context import reduction, assert_spawning
17
+ _ForkingPickler = reduction.ForkingPickler
18
+
19
+ __all__ = ['RawValue', 'RawArray', 'Value', 'Array', 'copy', 'synchronized']
20
+
21
+ #
22
+ #
23
+ #
24
+
25
+ typecode_to_type = {
26
+ 'c': ctypes.c_char, 'u': ctypes.c_wchar,
27
+ 'b': ctypes.c_byte, 'B': ctypes.c_ubyte,
28
+ 'h': ctypes.c_short, 'H': ctypes.c_ushort,
29
+ 'i': ctypes.c_int, 'I': ctypes.c_uint,
30
+ 'l': ctypes.c_long, 'L': ctypes.c_ulong,
31
+ 'q': ctypes.c_longlong, 'Q': ctypes.c_ulonglong,
32
+ 'f': ctypes.c_float, 'd': ctypes.c_double
33
+ }
34
+
35
+ #
36
+ #
37
+ #
38
+
39
+ def _new_value(type_):
40
+ size = ctypes.sizeof(type_)
41
+ wrapper = heap.BufferWrapper(size)
42
+ return rebuild_ctype(type_, wrapper, None)
43
+
44
+ def RawValue(typecode_or_type, *args):
45
+ '''
46
+ Returns a ctypes object allocated from shared memory
47
+ '''
48
+ type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
49
+ obj = _new_value(type_)
50
+ ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
51
+ obj.__init__(*args)
52
+ return obj
53
+
54
+ def RawArray(typecode_or_type, size_or_initializer):
55
+ '''
56
+ Returns a ctypes array allocated from shared memory
57
+ '''
58
+ type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
59
+ if isinstance(size_or_initializer, int):
60
+ type_ = type_ * size_or_initializer
61
+ obj = _new_value(type_)
62
+ ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
63
+ return obj
64
+ else:
65
+ type_ = type_ * len(size_or_initializer)
66
+ result = _new_value(type_)
67
+ result.__init__(*size_or_initializer)
68
+ return result
69
+
70
+ def Value(typecode_or_type, *args, lock=True, ctx=None):
71
+ '''
72
+ Return a synchronization wrapper for a Value
73
+ '''
74
+ obj = RawValue(typecode_or_type, *args)
75
+ if lock is False:
76
+ return obj
77
+ if lock in (True, None):
78
+ ctx = ctx or get_context()
79
+ lock = ctx.RLock()
80
+ if not hasattr(lock, 'acquire'):
81
+ raise AttributeError("%r has no method 'acquire'" % lock)
82
+ return synchronized(obj, lock, ctx=ctx)
83
+
84
+ def Array(typecode_or_type, size_or_initializer, *, lock=True, ctx=None):
85
+ '''
86
+ Return a synchronization wrapper for a RawArray
87
+ '''
88
+ obj = RawArray(typecode_or_type, size_or_initializer)
89
+ if lock is False:
90
+ return obj
91
+ if lock in (True, None):
92
+ ctx = ctx or get_context()
93
+ lock = ctx.RLock()
94
+ if not hasattr(lock, 'acquire'):
95
+ raise AttributeError("%r has no method 'acquire'" % lock)
96
+ return synchronized(obj, lock, ctx=ctx)
97
+
98
+ def copy(obj):
99
+ new_obj = _new_value(type(obj))
100
+ ctypes.pointer(new_obj)[0] = obj
101
+ return new_obj
102
+
103
+ def synchronized(obj, lock=None, ctx=None):
104
+ assert not isinstance(obj, SynchronizedBase), 'object already synchronized'
105
+ ctx = ctx or get_context()
106
+
107
+ if isinstance(obj, ctypes._SimpleCData):
108
+ return Synchronized(obj, lock, ctx)
109
+ elif isinstance(obj, ctypes.Array):
110
+ if obj._type_ is ctypes.c_char:
111
+ return SynchronizedString(obj, lock, ctx)
112
+ return SynchronizedArray(obj, lock, ctx)
113
+ else:
114
+ cls = type(obj)
115
+ try:
116
+ scls = class_cache[cls]
117
+ except KeyError:
118
+ names = [field[0] for field in cls._fields_]
119
+ d = {name: make_property(name) for name in names}
120
+ classname = 'Synchronized' + cls.__name__
121
+ scls = class_cache[cls] = type(classname, (SynchronizedBase,), d)
122
+ return scls(obj, lock, ctx)
123
+
124
+ #
125
+ # Functions for pickling/unpickling
126
+ #
127
+
128
+ def reduce_ctype(obj):
129
+ assert_spawning(obj)
130
+ if isinstance(obj, ctypes.Array):
131
+ return rebuild_ctype, (obj._type_, obj._wrapper, obj._length_)
132
+ else:
133
+ return rebuild_ctype, (type(obj), obj._wrapper, None)
134
+
135
+ def rebuild_ctype(type_, wrapper, length):
136
+ if length is not None:
137
+ type_ = type_ * length
138
+ _ForkingPickler.register(type_, reduce_ctype)
139
+ buf = wrapper.create_memoryview()
140
+ obj = type_.from_buffer(buf)
141
+ obj._wrapper = wrapper
142
+ return obj
143
+
144
+ #
145
+ # Function to create properties
146
+ #
147
+
148
+ def make_property(name):
149
+ try:
150
+ return prop_cache[name]
151
+ except KeyError:
152
+ d = {}
153
+ exec(template % ((name,)*7), d)
154
+ prop_cache[name] = d[name]
155
+ return d[name]
156
+
157
+ template = '''
158
+ def get%s(self):
159
+ self.acquire()
160
+ try:
161
+ return self._obj.%s
162
+ finally:
163
+ self.release()
164
+ def set%s(self, value):
165
+ self.acquire()
166
+ try:
167
+ self._obj.%s = value
168
+ finally:
169
+ self.release()
170
+ %s = property(get%s, set%s)
171
+ '''
172
+
173
+ prop_cache = {}
174
+ class_cache = weakref.WeakKeyDictionary()
175
+
176
+ #
177
+ # Synchronized wrappers
178
+ #
179
+
180
+ class SynchronizedBase(object):
181
+
182
+ def __init__(self, obj, lock=None, ctx=None):
183
+ self._obj = obj
184
+ if lock:
185
+ self._lock = lock
186
+ else:
187
+ ctx = ctx or get_context(force=True)
188
+ self._lock = ctx.RLock()
189
+ self.acquire = self._lock.acquire
190
+ self.release = self._lock.release
191
+
192
+ def __enter__(self):
193
+ return self._lock.__enter__()
194
+
195
+ def __exit__(self, *args):
196
+ return self._lock.__exit__(*args)
197
+
198
+ def __reduce__(self):
199
+ assert_spawning(self)
200
+ return synchronized, (self._obj, self._lock)
201
+
202
+ def get_obj(self):
203
+ return self._obj
204
+
205
+ def get_lock(self):
206
+ return self._lock
207
+
208
+ def __repr__(self):
209
+ return '<%s wrapper for %s>' % (type(self).__name__, self._obj)
210
+
211
+
212
+ class Synchronized(SynchronizedBase):
213
+ value = make_property('value')
214
+
215
+
216
+ class SynchronizedArray(SynchronizedBase):
217
+
218
+ def __len__(self):
219
+ return len(self._obj)
220
+
221
+ def __getitem__(self, i):
222
+ with self:
223
+ return self._obj[i]
224
+
225
+ def __setitem__(self, i, value):
226
+ with self:
227
+ self._obj[i] = value
228
+
229
+ def __getslice__(self, start, stop):
230
+ with self:
231
+ return self._obj[start:stop]
232
+
233
+ def __setslice__(self, start, stop, values):
234
+ with self:
235
+ self._obj[start:stop] = values
236
+
237
+
238
+ class SynchronizedString(SynchronizedArray):
239
+ value = make_property('value')
240
+ raw = make_property('raw')
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/spawn.py ADDED
@@ -0,0 +1,301 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Code used to start processes when using the spawn or forkserver
3
+ # start methods.
4
+ #
5
+ # multiprocessing/spawn.py
6
+ #
7
+ # Copyright (c) 2006-2008, R Oudkerk
8
+ # Licensed to PSF under a Contributor Agreement.
9
+ #
10
+
11
+ import os
12
+ import sys
13
+ import runpy
14
+ import types
15
+
16
+ from . import get_start_method, set_start_method
17
+ from . import process
18
+ from .context import reduction
19
+ from . import util
20
+
21
+ __all__ = ['_main', 'freeze_support', 'set_executable', 'get_executable',
22
+ 'get_preparation_data', 'get_command_line', 'import_main_path']
23
+
24
+ #
25
+ # _python_exe is the assumed path to the python executable.
26
+ # People embedding Python want to modify it.
27
+ #
28
+
29
+ if sys.platform != 'win32':
30
+ WINEXE = False
31
+ WINSERVICE = False
32
+ else:
33
+ WINEXE = getattr(sys, 'frozen', False)
34
+ WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")
35
+
36
+ def set_executable(exe):
37
+ global _python_exe
38
+ if sys.platform == 'win32':
39
+ _python_exe = os.fsdecode(exe)
40
+ else:
41
+ _python_exe = os.fsencode(exe)
42
+
43
+ def get_executable():
44
+ return _python_exe
45
+
46
+ if WINSERVICE:
47
+ set_executable(os.path.join(sys.exec_prefix, 'python.exe'))
48
+ else:
49
+ set_executable(sys.executable)
50
+
51
+ #
52
+ #
53
+ #
54
+
55
+ def is_forking(argv):
56
+ '''
57
+ Return whether commandline indicates we are forking
58
+ '''
59
+ if len(argv) >= 2 and argv[1] == '--multiprocessing-fork':
60
+ return True
61
+ else:
62
+ return False
63
+
64
+
65
+ def freeze_support():
66
+ '''
67
+ Run code for process object if this in not the main process
68
+ '''
69
+ if is_forking(sys.argv):
70
+ kwds = {}
71
+ for arg in sys.argv[2:]:
72
+ name, value = arg.split('=')
73
+ if value == 'None':
74
+ kwds[name] = None
75
+ else:
76
+ kwds[name] = int(value)
77
+ spawn_main(**kwds)
78
+ sys.exit()
79
+
80
+
81
+ def get_command_line(**kwds):
82
+ '''
83
+ Returns prefix of command line used for spawning a child process
84
+ '''
85
+ if getattr(sys, 'frozen', False):
86
+ return ([sys.executable, '--multiprocessing-fork'] +
87
+ ['%s=%r' % item for item in kwds.items()])
88
+ else:
89
+ prog = 'from multiprocess.spawn import spawn_main; spawn_main(%s)'
90
+ prog %= ', '.join('%s=%r' % item for item in kwds.items())
91
+ opts = util._args_from_interpreter_flags()
92
+ exe = get_executable()
93
+ return [exe] + opts + ['-c', prog, '--multiprocessing-fork']
94
+
95
+
96
+ def spawn_main(pipe_handle, parent_pid=None, tracker_fd=None):
97
+ '''
98
+ Run code specified by data received over pipe
99
+ '''
100
+ assert is_forking(sys.argv), "Not forking"
101
+ if sys.platform == 'win32':
102
+ import msvcrt
103
+ import _winapi
104
+
105
+ if parent_pid is not None:
106
+ source_process = _winapi.OpenProcess(
107
+ _winapi.SYNCHRONIZE | _winapi.PROCESS_DUP_HANDLE,
108
+ False, parent_pid)
109
+ else:
110
+ source_process = None
111
+ new_handle = reduction.duplicate(pipe_handle,
112
+ source_process=source_process)
113
+ fd = msvcrt.open_osfhandle(new_handle, os.O_RDONLY)
114
+ parent_sentinel = source_process
115
+ else:
116
+ from . import resource_tracker
117
+ resource_tracker._resource_tracker._fd = tracker_fd
118
+ fd = pipe_handle
119
+ parent_sentinel = os.dup(pipe_handle)
120
+ exitcode = _main(fd, parent_sentinel)
121
+ sys.exit(exitcode)
122
+
123
+
124
+ def _main(fd, parent_sentinel):
125
+ with os.fdopen(fd, 'rb', closefd=True) as from_parent:
126
+ process.current_process()._inheriting = True
127
+ try:
128
+ preparation_data = reduction.pickle.load(from_parent)
129
+ prepare(preparation_data)
130
+ self = reduction.pickle.load(from_parent)
131
+ finally:
132
+ del process.current_process()._inheriting
133
+ return self._bootstrap(parent_sentinel)
134
+
135
+
136
+ def _check_not_importing_main():
137
+ if getattr(process.current_process(), '_inheriting', False):
138
+ raise RuntimeError('''
139
+ An attempt has been made to start a new process before the
140
+ current process has finished its bootstrapping phase.
141
+
142
+ This probably means that you are not using fork to start your
143
+ child processes and you have forgotten to use the proper idiom
144
+ in the main module:
145
+
146
+ if __name__ == '__main__':
147
+ freeze_support()
148
+ ...
149
+
150
+ The "freeze_support()" line can be omitted if the program
151
+ is not going to be frozen to produce an executable.''')
152
+
153
+
154
+ def get_preparation_data(name):
155
+ '''
156
+ Return info about parent needed by child to unpickle process object
157
+ '''
158
+ _check_not_importing_main()
159
+ d = dict(
160
+ log_to_stderr=util._log_to_stderr,
161
+ authkey=process.current_process().authkey,
162
+ )
163
+
164
+ if util._logger is not None:
165
+ d['log_level'] = util._logger.getEffectiveLevel()
166
+
167
+ sys_path=sys.path.copy()
168
+ try:
169
+ i = sys_path.index('')
170
+ except ValueError:
171
+ pass
172
+ else:
173
+ sys_path[i] = process.ORIGINAL_DIR
174
+
175
+ d.update(
176
+ name=name,
177
+ sys_path=sys_path,
178
+ sys_argv=sys.argv,
179
+ orig_dir=process.ORIGINAL_DIR,
180
+ dir=os.getcwd(),
181
+ start_method=get_start_method(),
182
+ )
183
+
184
+ # Figure out whether to initialise main in the subprocess as a module
185
+ # or through direct execution (or to leave it alone entirely)
186
+ main_module = sys.modules['__main__']
187
+ main_mod_name = getattr(main_module.__spec__, "name", None)
188
+ if main_mod_name is not None:
189
+ d['init_main_from_name'] = main_mod_name
190
+ elif sys.platform != 'win32' or (not WINEXE and not WINSERVICE):
191
+ main_path = getattr(main_module, '__file__', None)
192
+ if main_path is not None:
193
+ if (not os.path.isabs(main_path) and
194
+ process.ORIGINAL_DIR is not None):
195
+ main_path = os.path.join(process.ORIGINAL_DIR, main_path)
196
+ d['init_main_from_path'] = os.path.normpath(main_path)
197
+
198
+ return d
199
+
200
+ #
201
+ # Prepare current process
202
+ #
203
+
204
+ old_main_modules = []
205
+
206
+ def prepare(data):
207
+ '''
208
+ Try to get current process ready to unpickle process object
209
+ '''
210
+ if 'name' in data:
211
+ process.current_process().name = data['name']
212
+
213
+ if 'authkey' in data:
214
+ process.current_process().authkey = data['authkey']
215
+
216
+ if 'log_to_stderr' in data and data['log_to_stderr']:
217
+ util.log_to_stderr()
218
+
219
+ if 'log_level' in data:
220
+ util.get_logger().setLevel(data['log_level'])
221
+
222
+ if 'sys_path' in data:
223
+ sys.path = data['sys_path']
224
+
225
+ if 'sys_argv' in data:
226
+ sys.argv = data['sys_argv']
227
+
228
+ if 'dir' in data:
229
+ os.chdir(data['dir'])
230
+
231
+ if 'orig_dir' in data:
232
+ process.ORIGINAL_DIR = data['orig_dir']
233
+
234
+ if 'start_method' in data:
235
+ set_start_method(data['start_method'], force=True)
236
+
237
+ if 'init_main_from_name' in data:
238
+ _fixup_main_from_name(data['init_main_from_name'])
239
+ elif 'init_main_from_path' in data:
240
+ _fixup_main_from_path(data['init_main_from_path'])
241
+
242
+ # Multiprocessing module helpers to fix up the main module in
243
+ # spawned subprocesses
244
+ def _fixup_main_from_name(mod_name):
245
+ # __main__.py files for packages, directories, zip archives, etc, run
246
+ # their "main only" code unconditionally, so we don't even try to
247
+ # populate anything in __main__, nor do we make any changes to
248
+ # __main__ attributes
249
+ current_main = sys.modules['__main__']
250
+ if mod_name == "__main__" or mod_name.endswith(".__main__"):
251
+ return
252
+
253
+ # If this process was forked, __main__ may already be populated
254
+ if getattr(current_main.__spec__, "name", None) == mod_name:
255
+ return
256
+
257
+ # Otherwise, __main__ may contain some non-main code where we need to
258
+ # support unpickling it properly. We rerun it as __mp_main__ and make
259
+ # the normal __main__ an alias to that
260
+ old_main_modules.append(current_main)
261
+ main_module = types.ModuleType("__mp_main__")
262
+ main_content = runpy.run_module(mod_name,
263
+ run_name="__mp_main__",
264
+ alter_sys=True)
265
+ main_module.__dict__.update(main_content)
266
+ sys.modules['__main__'] = sys.modules['__mp_main__'] = main_module
267
+
268
+
269
+ def _fixup_main_from_path(main_path):
270
+ # If this process was forked, __main__ may already be populated
271
+ current_main = sys.modules['__main__']
272
+
273
+ # Unfortunately, the main ipython launch script historically had no
274
+ # "if __name__ == '__main__'" guard, so we work around that
275
+ # by treating it like a __main__.py file
276
+ # See https://github.com/ipython/ipython/issues/4698
277
+ main_name = os.path.splitext(os.path.basename(main_path))[0]
278
+ if main_name == 'ipython':
279
+ return
280
+
281
+ # Otherwise, if __file__ already has the setting we expect,
282
+ # there's nothing more to do
283
+ if getattr(current_main, '__file__', None) == main_path:
284
+ return
285
+
286
+ # If the parent process has sent a path through rather than a module
287
+ # name we assume it is an executable script that may contain
288
+ # non-main code that needs to be executed
289
+ old_main_modules.append(current_main)
290
+ main_module = types.ModuleType("__mp_main__")
291
+ main_content = runpy.run_path(main_path,
292
+ run_name="__mp_main__")
293
+ main_module.__dict__.update(main_content)
294
+ sys.modules['__main__'] = sys.modules['__mp_main__'] = main_module
295
+
296
+
297
+ def import_main_path(main_path):
298
+ '''
299
+ Set sys.modules['__main__'] to module at main_path
300
+ '''
301
+ _fixup_main_from_path(main_path)
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/synchronize.py ADDED
@@ -0,0 +1,403 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Module implementing synchronization primitives
3
+ #
4
+ # multiprocessing/synchronize.py
5
+ #
6
+ # Copyright (c) 2006-2008, R Oudkerk
7
+ # Licensed to PSF under a Contributor Agreement.
8
+ #
9
+
10
+ __all__ = [
11
+ 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event'
12
+ ]
13
+
14
+ import threading
15
+ import sys
16
+ import tempfile
17
+ try:
18
+ import _multiprocess as _multiprocessing
19
+ except ImportError:
20
+ import _multiprocessing
21
+ import time
22
+
23
+ from . import context
24
+ from . import process
25
+ from . import util
26
+
27
+ # Try to import the mp.synchronize module cleanly, if it fails
28
+ # raise ImportError for platforms lacking a working sem_open implementation.
29
+ # See issue 3770
30
+ try:
31
+ from _multiprocess import SemLock, sem_unlink
32
+ except ImportError:
33
+ try:
34
+ from _multiprocessing import SemLock, sem_unlink
35
+ except (ImportError):
36
+ raise ImportError("This platform lacks a functioning sem_open" +
37
+ " implementation, therefore, the required" +
38
+ " synchronization primitives needed will not" +
39
+ " function, see issue 3770.")
40
+
41
+ #
42
+ # Constants
43
+ #
44
+
45
+ RECURSIVE_MUTEX, SEMAPHORE = list(range(2))
46
+ SEM_VALUE_MAX = _multiprocessing.SemLock.SEM_VALUE_MAX
47
+
48
+ #
49
+ # Base class for semaphores and mutexes; wraps `_multiprocessing.SemLock`
50
+ #
51
+
52
+ class SemLock(object):
53
+
54
+ _rand = tempfile._RandomNameSequence()
55
+
56
+ def __init__(self, kind, value, maxvalue, *, ctx):
57
+ if ctx is None:
58
+ ctx = context._default_context.get_context()
59
+ name = ctx.get_start_method()
60
+ unlink_now = sys.platform == 'win32' or name == 'fork'
61
+ for i in range(100):
62
+ try:
63
+ sl = self._semlock = _multiprocessing.SemLock(
64
+ kind, value, maxvalue, self._make_name(),
65
+ unlink_now)
66
+ except FileExistsError:
67
+ pass
68
+ else:
69
+ break
70
+ else:
71
+ raise FileExistsError('cannot find name for semaphore')
72
+
73
+ util.debug('created semlock with handle %s' % sl.handle)
74
+ self._make_methods()
75
+
76
+ if sys.platform != 'win32':
77
+ def _after_fork(obj):
78
+ obj._semlock._after_fork()
79
+ util.register_after_fork(self, _after_fork)
80
+
81
+ if self._semlock.name is not None:
82
+ # We only get here if we are on Unix with forking
83
+ # disabled. When the object is garbage collected or the
84
+ # process shuts down we unlink the semaphore name
85
+ from .resource_tracker import register
86
+ register(self._semlock.name, "semaphore")
87
+ util.Finalize(self, SemLock._cleanup, (self._semlock.name,),
88
+ exitpriority=0)
89
+
90
+ @staticmethod
91
+ def _cleanup(name):
92
+ from .resource_tracker import unregister
93
+ sem_unlink(name)
94
+ unregister(name, "semaphore")
95
+
96
+ def _make_methods(self):
97
+ self.acquire = self._semlock.acquire
98
+ self.release = self._semlock.release
99
+
100
+ def __enter__(self):
101
+ return self._semlock.__enter__()
102
+
103
+ def __exit__(self, *args):
104
+ return self._semlock.__exit__(*args)
105
+
106
+ def __getstate__(self):
107
+ context.assert_spawning(self)
108
+ sl = self._semlock
109
+ if sys.platform == 'win32':
110
+ h = context.get_spawning_popen().duplicate_for_child(sl.handle)
111
+ else:
112
+ h = sl.handle
113
+ return (h, sl.kind, sl.maxvalue, sl.name)
114
+
115
+ def __setstate__(self, state):
116
+ self._semlock = _multiprocessing.SemLock._rebuild(*state)
117
+ util.debug('recreated blocker with handle %r' % state[0])
118
+ self._make_methods()
119
+
120
+ @staticmethod
121
+ def _make_name():
122
+ return '%s-%s' % (process.current_process()._config['semprefix'],
123
+ next(SemLock._rand))
124
+
125
+ #
126
+ # Semaphore
127
+ #
128
+
129
+ class Semaphore(SemLock):
130
+
131
+ def __init__(self, value=1, *, ctx):
132
+ SemLock.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX, ctx=ctx)
133
+
134
+ def get_value(self):
135
+ return self._semlock._get_value()
136
+
137
+ def __repr__(self):
138
+ try:
139
+ value = self._semlock._get_value()
140
+ except Exception:
141
+ value = 'unknown'
142
+ return '<%s(value=%s)>' % (self.__class__.__name__, value)
143
+
144
+ #
145
+ # Bounded semaphore
146
+ #
147
+
148
+ class BoundedSemaphore(Semaphore):
149
+
150
+ def __init__(self, value=1, *, ctx):
151
+ SemLock.__init__(self, SEMAPHORE, value, value, ctx=ctx)
152
+
153
+ def __repr__(self):
154
+ try:
155
+ value = self._semlock._get_value()
156
+ except Exception:
157
+ value = 'unknown'
158
+ return '<%s(value=%s, maxvalue=%s)>' % \
159
+ (self.__class__.__name__, value, self._semlock.maxvalue)
160
+
161
+ #
162
+ # Non-recursive lock
163
+ #
164
+
165
+ class Lock(SemLock):
166
+
167
+ def __init__(self, *, ctx):
168
+ SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)
169
+
170
+ def __repr__(self):
171
+ try:
172
+ if self._semlock._is_mine():
173
+ name = process.current_process().name
174
+ if threading.current_thread().name != 'MainThread':
175
+ name += '|' + threading.current_thread().name
176
+ elif self._semlock._get_value() == 1:
177
+ name = 'None'
178
+ elif self._semlock._count() > 0:
179
+ name = 'SomeOtherThread'
180
+ else:
181
+ name = 'SomeOtherProcess'
182
+ except Exception:
183
+ name = 'unknown'
184
+ return '<%s(owner=%s)>' % (self.__class__.__name__, name)
185
+
186
+ #
187
+ # Recursive lock
188
+ #
189
+
190
+ class RLock(SemLock):
191
+
192
+ def __init__(self, *, ctx):
193
+ SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1, ctx=ctx)
194
+
195
+ def __repr__(self):
196
+ try:
197
+ if self._semlock._is_mine():
198
+ name = process.current_process().name
199
+ if threading.current_thread().name != 'MainThread':
200
+ name += '|' + threading.current_thread().name
201
+ count = self._semlock._count()
202
+ elif self._semlock._get_value() == 1:
203
+ name, count = 'None', 0
204
+ elif self._semlock._count() > 0:
205
+ name, count = 'SomeOtherThread', 'nonzero'
206
+ else:
207
+ name, count = 'SomeOtherProcess', 'nonzero'
208
+ except Exception:
209
+ name, count = 'unknown', 'unknown'
210
+ return '<%s(%s, %s)>' % (self.__class__.__name__, name, count)
211
+
212
+ #
213
+ # Condition variable
214
+ #
215
+
216
+ class Condition(object):
217
+
218
+ def __init__(self, lock=None, *, ctx):
219
+ self._lock = lock or ctx.RLock()
220
+ self._sleeping_count = ctx.Semaphore(0)
221
+ self._woken_count = ctx.Semaphore(0)
222
+ self._wait_semaphore = ctx.Semaphore(0)
223
+ self._make_methods()
224
+
225
+ def __getstate__(self):
226
+ context.assert_spawning(self)
227
+ return (self._lock, self._sleeping_count,
228
+ self._woken_count, self._wait_semaphore)
229
+
230
+ def __setstate__(self, state):
231
+ (self._lock, self._sleeping_count,
232
+ self._woken_count, self._wait_semaphore) = state
233
+ self._make_methods()
234
+
235
+ def __enter__(self):
236
+ return self._lock.__enter__()
237
+
238
+ def __exit__(self, *args):
239
+ return self._lock.__exit__(*args)
240
+
241
+ def _make_methods(self):
242
+ self.acquire = self._lock.acquire
243
+ self.release = self._lock.release
244
+
245
+ def __repr__(self):
246
+ try:
247
+ num_waiters = (self._sleeping_count._semlock._get_value() -
248
+ self._woken_count._semlock._get_value())
249
+ except Exception:
250
+ num_waiters = 'unknown'
251
+ return '<%s(%s, %s)>' % (self.__class__.__name__, self._lock, num_waiters)
252
+
253
+ def wait(self, timeout=None):
254
+ assert self._lock._semlock._is_mine(), \
255
+ 'must acquire() condition before using wait()'
256
+
257
+ # indicate that this thread is going to sleep
258
+ self._sleeping_count.release()
259
+
260
+ # release lock
261
+ count = self._lock._semlock._count()
262
+ for i in range(count):
263
+ self._lock.release()
264
+
265
+ try:
266
+ # wait for notification or timeout
267
+ return self._wait_semaphore.acquire(True, timeout)
268
+ finally:
269
+ # indicate that this thread has woken
270
+ self._woken_count.release()
271
+
272
+ # reacquire lock
273
+ for i in range(count):
274
+ self._lock.acquire()
275
+
276
+ def notify(self, n=1):
277
+ assert self._lock._semlock._is_mine(), 'lock is not owned'
278
+ assert not self._wait_semaphore.acquire(
279
+ False), ('notify: Should not have been able to acquire '
280
+ + '_wait_semaphore')
281
+
282
+ # to take account of timeouts since last notify*() we subtract
283
+ # woken_count from sleeping_count and rezero woken_count
284
+ while self._woken_count.acquire(False):
285
+ res = self._sleeping_count.acquire(False)
286
+ assert res, ('notify: Bug in sleeping_count.acquire'
287
+ + '- res should not be False')
288
+
289
+ sleepers = 0
290
+ while sleepers < n and self._sleeping_count.acquire(False):
291
+ self._wait_semaphore.release() # wake up one sleeper
292
+ sleepers += 1
293
+
294
+ if sleepers:
295
+ for i in range(sleepers):
296
+ self._woken_count.acquire() # wait for a sleeper to wake
297
+
298
+ # rezero wait_semaphore in case some timeouts just happened
299
+ while self._wait_semaphore.acquire(False):
300
+ pass
301
+
302
+ def notify_all(self):
303
+ self.notify(n=sys.maxsize)
304
+
305
+ def wait_for(self, predicate, timeout=None):
306
+ result = predicate()
307
+ if result:
308
+ return result
309
+ if timeout is not None:
310
+ endtime = getattr(time,'monotonic',time.time)() + timeout
311
+ else:
312
+ endtime = None
313
+ waittime = None
314
+ while not result:
315
+ if endtime is not None:
316
+ waittime = endtime - getattr(time,'monotonic',time.time)()
317
+ if waittime <= 0:
318
+ break
319
+ self.wait(waittime)
320
+ result = predicate()
321
+ return result
322
+
323
+ #
324
+ # Event
325
+ #
326
+
327
+ class Event(object):
328
+
329
+ def __init__(self, *, ctx):
330
+ self._cond = ctx.Condition(ctx.Lock())
331
+ self._flag = ctx.Semaphore(0)
332
+
333
+ def is_set(self):
334
+ with self._cond:
335
+ if self._flag.acquire(False):
336
+ self._flag.release()
337
+ return True
338
+ return False
339
+
340
+ def set(self):
341
+ with self._cond:
342
+ self._flag.acquire(False)
343
+ self._flag.release()
344
+ self._cond.notify_all()
345
+
346
+ def clear(self):
347
+ with self._cond:
348
+ self._flag.acquire(False)
349
+
350
+ def wait(self, timeout=None):
351
+ with self._cond:
352
+ if self._flag.acquire(False):
353
+ self._flag.release()
354
+ else:
355
+ self._cond.wait(timeout)
356
+
357
+ if self._flag.acquire(False):
358
+ self._flag.release()
359
+ return True
360
+ return False
361
+
362
+ def __repr__(self) -> str:
363
+ set_status = 'set' if self.is_set() else 'unset'
364
+ return f"<{type(self).__qualname__} at {id(self):#x} {set_status}>"
365
+ #
366
+ # Barrier
367
+ #
368
+
369
+ class Barrier(threading.Barrier):
370
+
371
+ def __init__(self, parties, action=None, timeout=None, *, ctx):
372
+ import struct
373
+ from .heap import BufferWrapper
374
+ wrapper = BufferWrapper(struct.calcsize('i') * 2)
375
+ cond = ctx.Condition()
376
+ self.__setstate__((parties, action, timeout, cond, wrapper))
377
+ self._state = 0
378
+ self._count = 0
379
+
380
+ def __setstate__(self, state):
381
+ (self._parties, self._action, self._timeout,
382
+ self._cond, self._wrapper) = state
383
+ self._array = self._wrapper.create_memoryview().cast('i')
384
+
385
+ def __getstate__(self):
386
+ return (self._parties, self._action, self._timeout,
387
+ self._cond, self._wrapper)
388
+
389
+ @property
390
+ def _state(self):
391
+ return self._array[0]
392
+
393
+ @_state.setter
394
+ def _state(self, value):
395
+ self._array[0] = value
396
+
397
+ @property
398
+ def _count(self):
399
+ return self._array[1]
400
+
401
+ @_count.setter
402
+ def _count(self, value):
403
+ self._array[1] = value
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/multiprocess/util.py ADDED
@@ -0,0 +1,491 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Module providing various facilities to other parts of the package
3
+ #
4
+ # multiprocessing/util.py
5
+ #
6
+ # Copyright (c) 2006-2008, R Oudkerk
7
+ # Licensed to PSF under a Contributor Agreement.
8
+ #
9
+
10
+ import os
11
+ import itertools
12
+ import sys
13
+ import weakref
14
+ import atexit
15
+ import threading # we want threading to install it's
16
+ # cleanup function before multiprocessing does
17
+ from subprocess import _args_from_interpreter_flags
18
+
19
+ from . import process
20
+
21
+ __all__ = [
22
+ 'sub_debug', 'debug', 'info', 'sub_warning', 'get_logger',
23
+ 'log_to_stderr', 'get_temp_dir', 'register_after_fork',
24
+ 'is_exiting', 'Finalize', 'ForkAwareThreadLock', 'ForkAwareLocal',
25
+ 'close_all_fds_except', 'SUBDEBUG', 'SUBWARNING',
26
+ ]
27
+
28
+ #
29
+ # Logging
30
+ #
31
+
32
+ NOTSET = 0
33
+ SUBDEBUG = 5
34
+ DEBUG = 10
35
+ INFO = 20
36
+ SUBWARNING = 25
37
+
38
+ LOGGER_NAME = 'multiprocess'
39
+ DEFAULT_LOGGING_FORMAT = '[%(levelname)s/%(processName)s] %(message)s'
40
+
41
+ _logger = None
42
+ _log_to_stderr = False
43
+
44
+ def sub_debug(msg, *args):
45
+ if _logger:
46
+ _logger.log(SUBDEBUG, msg, *args)
47
+
48
+ def debug(msg, *args):
49
+ if _logger:
50
+ _logger.log(DEBUG, msg, *args)
51
+
52
+ def info(msg, *args):
53
+ if _logger:
54
+ _logger.log(INFO, msg, *args)
55
+
56
+ def sub_warning(msg, *args):
57
+ if _logger:
58
+ _logger.log(SUBWARNING, msg, *args)
59
+
60
+ def get_logger():
61
+ '''
62
+ Returns logger used by multiprocess
63
+ '''
64
+ global _logger
65
+ import logging
66
+
67
+ logging._acquireLock()
68
+ try:
69
+ if not _logger:
70
+
71
+ _logger = logging.getLogger(LOGGER_NAME)
72
+ _logger.propagate = 0
73
+
74
+ # XXX multiprocessing should cleanup before logging
75
+ if hasattr(atexit, 'unregister'):
76
+ atexit.unregister(_exit_function)
77
+ atexit.register(_exit_function)
78
+ else:
79
+ atexit._exithandlers.remove((_exit_function, (), {}))
80
+ atexit._exithandlers.append((_exit_function, (), {}))
81
+
82
+ finally:
83
+ logging._releaseLock()
84
+
85
+ return _logger
86
+
87
+ def log_to_stderr(level=None):
88
+ '''
89
+ Turn on logging and add a handler which prints to stderr
90
+ '''
91
+ global _log_to_stderr
92
+ import logging
93
+
94
+ logger = get_logger()
95
+ formatter = logging.Formatter(DEFAULT_LOGGING_FORMAT)
96
+ handler = logging.StreamHandler()
97
+ handler.setFormatter(formatter)
98
+ logger.addHandler(handler)
99
+
100
+ if level:
101
+ logger.setLevel(level)
102
+ _log_to_stderr = True
103
+ return _logger
104
+
105
+
106
+ # Abstract socket support
107
+
108
+ def _platform_supports_abstract_sockets():
109
+ if sys.platform == "linux":
110
+ return True
111
+ if hasattr(sys, 'getandroidapilevel'):
112
+ return True
113
+ return False
114
+
115
+
116
+ def is_abstract_socket_namespace(address):
117
+ if not address:
118
+ return False
119
+ if isinstance(address, bytes):
120
+ return address[0] == 0
121
+ elif isinstance(address, str):
122
+ return address[0] == "\0"
123
+ raise TypeError(f'address type of {address!r} unrecognized')
124
+
125
+
126
+ abstract_sockets_supported = _platform_supports_abstract_sockets()
127
+
128
+ #
129
+ # Function returning a temp directory which will be removed on exit
130
+ #
131
+
132
+ def _remove_temp_dir(rmtree, tempdir):
133
+ rmtree(tempdir)
134
+
135
+ current_process = process.current_process()
136
+ # current_process() can be None if the finalizer is called
137
+ # late during Python finalization
138
+ if current_process is not None:
139
+ current_process._config['tempdir'] = None
140
+
141
+ def get_temp_dir():
142
+ # get name of a temp directory which will be automatically cleaned up
143
+ tempdir = process.current_process()._config.get('tempdir')
144
+ if tempdir is None:
145
+ import shutil, tempfile
146
+ tempdir = tempfile.mkdtemp(prefix='pymp-')
147
+ info('created temp directory %s', tempdir)
148
+ # keep a strong reference to shutil.rmtree(), since the finalizer
149
+ # can be called late during Python shutdown
150
+ Finalize(None, _remove_temp_dir, args=(shutil.rmtree, tempdir),
151
+ exitpriority=-100)
152
+ process.current_process()._config['tempdir'] = tempdir
153
+ return tempdir
154
+
155
+ #
156
+ # Support for reinitialization of objects when bootstrapping a child process
157
+ #
158
+
159
+ _afterfork_registry = weakref.WeakValueDictionary()
160
+ _afterfork_counter = itertools.count()
161
+
162
+ def _run_after_forkers():
163
+ items = list(_afterfork_registry.items())
164
+ items.sort()
165
+ for (index, ident, func), obj in items:
166
+ try:
167
+ func(obj)
168
+ except Exception as e:
169
+ info('after forker raised exception %s', e)
170
+
171
+ def register_after_fork(obj, func):
172
+ _afterfork_registry[(next(_afterfork_counter), id(obj), func)] = obj
173
+
174
+ #
175
+ # Finalization using weakrefs
176
+ #
177
+
178
+ _finalizer_registry = {}
179
+ _finalizer_counter = itertools.count()
180
+
181
+
182
+ class Finalize(object):
183
+ '''
184
+ Class which supports object finalization using weakrefs
185
+ '''
186
+ def __init__(self, obj, callback, args=(), kwargs=None, exitpriority=None):
187
+ if (exitpriority is not None) and not isinstance(exitpriority,int):
188
+ raise TypeError(
189
+ "Exitpriority ({0!r}) must be None or int, not {1!s}".format(
190
+ exitpriority, type(exitpriority)))
191
+
192
+ if obj is not None:
193
+ self._weakref = weakref.ref(obj, self)
194
+ elif exitpriority is None:
195
+ raise ValueError("Without object, exitpriority cannot be None")
196
+
197
+ self._callback = callback
198
+ self._args = args
199
+ self._kwargs = kwargs or {}
200
+ self._key = (exitpriority, next(_finalizer_counter))
201
+ self._pid = os.getpid()
202
+
203
+ _finalizer_registry[self._key] = self
204
+
205
+ def __call__(self, wr=None,
206
+ # Need to bind these locally because the globals can have
207
+ # been cleared at shutdown
208
+ _finalizer_registry=_finalizer_registry,
209
+ sub_debug=sub_debug, getpid=os.getpid):
210
+ '''
211
+ Run the callback unless it has already been called or cancelled
212
+ '''
213
+ try:
214
+ del _finalizer_registry[self._key]
215
+ except KeyError:
216
+ sub_debug('finalizer no longer registered')
217
+ else:
218
+ if self._pid != getpid():
219
+ sub_debug('finalizer ignored because different process')
220
+ res = None
221
+ else:
222
+ sub_debug('finalizer calling %s with args %s and kwargs %s',
223
+ self._callback, self._args, self._kwargs)
224
+ res = self._callback(*self._args, **self._kwargs)
225
+ self._weakref = self._callback = self._args = \
226
+ self._kwargs = self._key = None
227
+ return res
228
+
229
+ def cancel(self):
230
+ '''
231
+ Cancel finalization of the object
232
+ '''
233
+ try:
234
+ del _finalizer_registry[self._key]
235
+ except KeyError:
236
+ pass
237
+ else:
238
+ self._weakref = self._callback = self._args = \
239
+ self._kwargs = self._key = None
240
+
241
+ def still_active(self):
242
+ '''
243
+ Return whether this finalizer is still waiting to invoke callback
244
+ '''
245
+ return self._key in _finalizer_registry
246
+
247
+ def __repr__(self):
248
+ try:
249
+ obj = self._weakref()
250
+ except (AttributeError, TypeError):
251
+ obj = None
252
+
253
+ if obj is None:
254
+ return '<%s object, dead>' % self.__class__.__name__
255
+
256
+ x = '<%s object, callback=%s' % (
257
+ self.__class__.__name__,
258
+ getattr(self._callback, '__name__', self._callback))
259
+ if self._args:
260
+ x += ', args=' + str(self._args)
261
+ if self._kwargs:
262
+ x += ', kwargs=' + str(self._kwargs)
263
+ if self._key[0] is not None:
264
+ x += ', exitpriority=' + str(self._key[0])
265
+ return x + '>'
266
+
267
+
268
+ def _run_finalizers(minpriority=None):
269
+ '''
270
+ Run all finalizers whose exit priority is not None and at least minpriority
271
+
272
+ Finalizers with highest priority are called first; finalizers with
273
+ the same priority will be called in reverse order of creation.
274
+ '''
275
+ if _finalizer_registry is None:
276
+ # This function may be called after this module's globals are
277
+ # destroyed. See the _exit_function function in this module for more
278
+ # notes.
279
+ return
280
+
281
+ if minpriority is None:
282
+ f = lambda p : p[0] is not None
283
+ else:
284
+ f = lambda p : p[0] is not None and p[0] >= minpriority
285
+
286
+ # Careful: _finalizer_registry may be mutated while this function
287
+ # is running (either by a GC run or by another thread).
288
+
289
+ # list(_finalizer_registry) should be atomic, while
290
+ # list(_finalizer_registry.items()) is not.
291
+ keys = [key for key in list(_finalizer_registry) if f(key)]
292
+ keys.sort(reverse=True)
293
+
294
+ for key in keys:
295
+ finalizer = _finalizer_registry.get(key)
296
+ # key may have been removed from the registry
297
+ if finalizer is not None:
298
+ sub_debug('calling %s', finalizer)
299
+ try:
300
+ finalizer()
301
+ except Exception:
302
+ import traceback
303
+ traceback.print_exc()
304
+
305
+ if minpriority is None:
306
+ _finalizer_registry.clear()
307
+
308
+ #
309
+ # Clean up on exit
310
+ #
311
+
312
+ def is_exiting():
313
+ '''
314
+ Returns true if the process is shutting down
315
+ '''
316
+ return _exiting or _exiting is None
317
+
318
+ _exiting = False
319
+
320
+ def _exit_function(info=info, debug=debug, _run_finalizers=_run_finalizers,
321
+ active_children=process.active_children,
322
+ current_process=process.current_process):
323
+ # We hold on to references to functions in the arglist due to the
324
+ # situation described below, where this function is called after this
325
+ # module's globals are destroyed.
326
+
327
+ global _exiting
328
+
329
+ if not _exiting:
330
+ _exiting = True
331
+
332
+ info('process shutting down')
333
+ debug('running all "atexit" finalizers with priority >= 0')
334
+ _run_finalizers(0)
335
+
336
+ if current_process() is not None:
337
+ # We check if the current process is None here because if
338
+ # it's None, any call to ``active_children()`` will raise
339
+ # an AttributeError (active_children winds up trying to
340
+ # get attributes from util._current_process). One
341
+ # situation where this can happen is if someone has
342
+ # manipulated sys.modules, causing this module to be
343
+ # garbage collected. The destructor for the module type
344
+ # then replaces all values in the module dict with None.
345
+ # For instance, after setuptools runs a test it replaces
346
+ # sys.modules with a copy created earlier. See issues
347
+ # #9775 and #15881. Also related: #4106, #9205, and
348
+ # #9207.
349
+
350
+ for p in active_children():
351
+ if p.daemon:
352
+ info('calling terminate() for daemon %s', p.name)
353
+ p._popen.terminate()
354
+
355
+ for p in active_children():
356
+ info('calling join() for process %s', p.name)
357
+ p.join()
358
+
359
+ debug('running the remaining "atexit" finalizers')
360
+ _run_finalizers()
361
+
362
+ atexit.register(_exit_function)
363
+
364
+ #
365
+ # Some fork aware types
366
+ #
367
+
368
+ class ForkAwareThreadLock(object):
369
+ def __init__(self):
370
+ self._lock = threading.Lock()
371
+ self.acquire = self._lock.acquire
372
+ self.release = self._lock.release
373
+ register_after_fork(self, ForkAwareThreadLock._at_fork_reinit)
374
+
375
+ def _at_fork_reinit(self):
376
+ self._lock._at_fork_reinit()
377
+
378
+ def __enter__(self):
379
+ return self._lock.__enter__()
380
+
381
+ def __exit__(self, *args):
382
+ return self._lock.__exit__(*args)
383
+
384
+
385
+ class ForkAwareLocal(threading.local):
386
+ def __init__(self):
387
+ register_after_fork(self, lambda obj : obj.__dict__.clear())
388
+ def __reduce__(self):
389
+ return type(self), ()
390
+
391
+ #
392
+ # Close fds except those specified
393
+ #
394
+
395
+ try:
396
+ MAXFD = os.sysconf("SC_OPEN_MAX")
397
+ except Exception:
398
+ MAXFD = 256
399
+
400
+ def close_all_fds_except(fds):
401
+ fds = list(fds) + [-1, MAXFD]
402
+ fds.sort()
403
+ assert fds[-1] == MAXFD, 'fd too large'
404
+ for i in range(len(fds) - 1):
405
+ os.closerange(fds[i]+1, fds[i+1])
406
+ #
407
+ # Close sys.stdin and replace stdin with os.devnull
408
+ #
409
+
410
+ def _close_stdin():
411
+ if sys.stdin is None:
412
+ return
413
+
414
+ try:
415
+ sys.stdin.close()
416
+ except (OSError, ValueError):
417
+ pass
418
+
419
+ try:
420
+ fd = os.open(os.devnull, os.O_RDONLY)
421
+ try:
422
+ sys.stdin = open(fd, encoding="utf-8", closefd=False)
423
+ except:
424
+ os.close(fd)
425
+ raise
426
+ except (OSError, ValueError):
427
+ pass
428
+
429
+ #
430
+ # Flush standard streams, if any
431
+ #
432
+
433
+ def _flush_std_streams():
434
+ try:
435
+ sys.stdout.flush()
436
+ except (AttributeError, ValueError):
437
+ pass
438
+ try:
439
+ sys.stderr.flush()
440
+ except (AttributeError, ValueError):
441
+ pass
442
+
443
+ #
444
+ # Start a program with only specified fds kept open
445
+ #
446
+
447
+ def spawnv_passfds(path, args, passfds):
448
+ import _posixsubprocess
449
+ import subprocess
450
+ passfds = tuple(sorted(map(int, passfds)))
451
+ errpipe_read, errpipe_write = os.pipe()
452
+ try:
453
+ return _posixsubprocess.fork_exec(
454
+ args, [path], True, passfds, None, None,
455
+ -1, -1, -1, -1, -1, -1, errpipe_read, errpipe_write,
456
+ False, False, -1, None, None, None, -1, None,
457
+ subprocess._USE_VFORK)
458
+ finally:
459
+ os.close(errpipe_read)
460
+ os.close(errpipe_write)
461
+
462
+
463
+ def close_fds(*fds):
464
+ """Close each file descriptor given as an argument"""
465
+ for fd in fds:
466
+ os.close(fd)
467
+
468
+
469
+ def _cleanup_tests():
470
+ """Cleanup multiprocessing resources when multiprocessing tests
471
+ completed."""
472
+
473
+ from test import support
474
+
475
+ # cleanup multiprocessing
476
+ process._cleanup()
477
+
478
+ # Stop the ForkServer process if it's running
479
+ from multiprocess import forkserver
480
+ forkserver._forkserver._stop()
481
+
482
+ # Stop the ResourceTracker process if it's running
483
+ from multiprocess import resource_tracker
484
+ resource_tracker._resource_tracker._stop()
485
+
486
+ # bpo-37421: Explicitly call _run_finalizers() to remove immediately
487
+ # temporary directories created by multiprocessing.util.get_temp_dir().
488
+ _run_finalizers()
489
+ support.gc_collect()
490
+
491
+ support.reap_children()
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/nvidia_curand_cu12-10.3.2.106.dist-info/RECORD ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ nvidia/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ nvidia/__pycache__/__init__.cpython-311.pyc,,
3
+ nvidia/curand/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ nvidia/curand/__pycache__/__init__.cpython-311.pyc,,
5
+ nvidia/curand/include/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ nvidia/curand/include/__pycache__/__init__.cpython-311.pyc,,
7
+ nvidia/curand/include/curand.h,sha256=y393HddG5_OdV-3cK2l2Q_fxzGbEc1cyCcYjkOE3oIk,43966
8
+ nvidia/curand/include/curand_discrete.h,sha256=2qD3BkI622XEu0444wVP7HeYkKAx0Rjr2HDhqU4SA7E,3486
9
+ nvidia/curand/include/curand_discrete2.h,sha256=ZrQTO5R9x83AMX88uq7M8M94DLSC5VEz0PAkfcwtQeg,10883
10
+ nvidia/curand/include/curand_globals.h,sha256=bES1Kx0NrATXk1DReMMkqWrB062nOnaAp39y22wViXU,3717
11
+ nvidia/curand/include/curand_kernel.h,sha256=SjfAeh13ybXIxiekcgczzua02kIAqETopJKRhYvCat8,53133
12
+ nvidia/curand/include/curand_lognormal.h,sha256=-X-iNkJSzWpAYYjogm689EJTZfzore9sxU7ObddljLk,28142
13
+ nvidia/curand/include/curand_mrg32k3a.h,sha256=ZVVREjGNsJQJ-3IzZZ_LKGtGteslicb8E0Aly49BKPs,170296
14
+ nvidia/curand/include/curand_mtgp32.h,sha256=Qhrmx0pHWF-P2Uu5bKwYE9ymEWq3c7qBzCITVMaKMfI,7845
15
+ nvidia/curand/include/curand_mtgp32_host.h,sha256=SXqzmSQkzTLSRJ4pojTg_TNCC3T-G89HdBK-boSDqr4,18274
16
+ nvidia/curand/include/curand_mtgp32_kernel.h,sha256=ajZnXr5ZXnQExElf6LPpigrrKPTmMIZbRyTEnJ-BDhw,13731
17
+ nvidia/curand/include/curand_mtgp32dc_p_11213.h,sha256=7_gGYUH47UugIAEt60vYH5nFa-QUwTpDwSEgLg9cZts,276889
18
+ nvidia/curand/include/curand_normal.h,sha256=lnmYVk2fn0oEVWOytdKhXrHL36GLCjMnB8OnZeCaYcA,26953
19
+ nvidia/curand/include/curand_normal_static.h,sha256=5K4iTC9AuSWCe1LVxuj_0y3BVjtp0bxO6hndv2rbmiw,4727
20
+ nvidia/curand/include/curand_philox4x32_x.h,sha256=T21IP-Rdg3_tSVU9Je4dLKuwEqE4ovfwi7r1hOY92Dw,7166
21
+ nvidia/curand/include/curand_poisson.h,sha256=KrhXOmO_D7aclnj8geIyHqdpSQwWHurS9V_pVtgzodM,25461
22
+ nvidia/curand/include/curand_precalc.h,sha256=I6NZdgT42fMm9qSCtP-rlOAqt4Zsqgal0ajktcPmEak,1392393
23
+ nvidia/curand/include/curand_uniform.h,sha256=gpmRgQu5r6ppgLTg60NXoDdVJS6wMUy6jC5bh8l04e8,17472
24
+ nvidia/curand/lib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
+ nvidia/curand/lib/__pycache__/__init__.cpython-311.pyc,,
26
+ nvidia/curand/lib/libcurand.so.10,sha256=Qah4DXdgqpHMpyMtCF9VGDx-bPrsq8LzLnGSYIMQNfw,96681392
27
+ nvidia_curand_cu12-10.3.2.106.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
28
+ nvidia_curand_cu12-10.3.2.106.dist-info/License.txt,sha256=rW9YU_ugyg0VnQ9Y1JrkmDDC-Mk_epJki5zpCttMbM0,59262
29
+ nvidia_curand_cu12-10.3.2.106.dist-info/METADATA,sha256=TTSGosdrLNURQYusjHa9N5vwOD1zz1DnnZV0im74NT4,1507
30
+ nvidia_curand_cu12-10.3.2.106.dist-info/RECORD,,
31
+ nvidia_curand_cu12-10.3.2.106.dist-info/WHEEL,sha256=-kQi_VMfvRQozZJT7HUPMfY-5vLo0LVTmAylNJ3Ft98,106
32
+ nvidia_curand_cu12-10.3.2.106.dist-info/top_level.txt,sha256=fTkAtiFuL16nUrB9ytDDtpytz2t0B4NvYTnRzwAhO14,7
data/conda_envs/openunlearning_2506/lib/python3.11/site-packages/nvidia_curand_cu12-10.3.2.106.dist-info/top_level.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ nvidia