PATH:
opt
/
alt
/
python313
/
lib64
/
python3.13
"""Thread-local objects. (Note that this module provides a Python version of the threading.local class. Depending on the version of Python you're using, there may be a faster one available. You should always import the `local` class from `threading`.) """ from weakref import ref from contextlib import contextmanager __all__ = ["local"] # We need to use objects from the threading module, but the threading # module may also want to use our `local` class, if support for locals # isn't compiled in to the `thread` module. This creates potential problems # with circular imports. For that reason, we don't import `threading` # until the bottom of this file (a hack sufficient to worm around the # potential problems). Note that all platforms on CPython do have support # for locals in the `thread` module, and there is no circular import problem # then, so problems introduced by fiddling the order of imports here won't # manifest. class _localimpl: """A class managing thread-local dicts""" __slots__ = 'key', 'dicts', 'localargs', 'locallock', '__weakref__' def __init__(self): # The key used in the Thread objects' attribute dicts. # We keep it a string for speed but make it unlikely to clash with # a "real" attribute. self.key = '_threading_local._localimpl.' + str(id(self)) # { id(Thread) -> (ref(Thread), thread-local dict) } self.dicts = {} def get_dict(self): """Return the dict for the current thread. Raises KeyError if none defined.""" thread = current_thread() return self.dicts[id(thread)][1] def create_dict(self): """Create a new dict for the current thread, and return it.""" localdict = {} key = self.key thread = current_thread() idt = id(thread) def local_deleted(_, key=key): # When the localimpl is deleted, remove the thread attribute. thread = wrthread() if thread is not None: del thread.__dict__[key] def thread_deleted(_, idt=idt): # When the thread is deleted, remove the local dict. # Note that this is suboptimal if the thread object gets # caught in a reference loop. We would like to be called # as soon as the OS-level thread ends instead. local = wrlocal() if local is not None: dct = local.dicts.pop(idt) wrlocal = ref(self, local_deleted) wrthread = ref(thread, thread_deleted) thread.__dict__[key] = wrlocal self.dicts[idt] = wrthread, localdict return localdict @contextmanager def _patch(self): impl = object.__getattribute__(self, '_local__impl') try: dct = impl.get_dict() except KeyError: dct = impl.create_dict() args, kw = impl.localargs self.__init__(*args, **kw) with impl.locallock: object.__setattr__(self, '__dict__', dct) yield class local: __slots__ = '_local__impl', '__dict__' def __new__(cls, /, *args, **kw): if (args or kw) and (cls.__init__ is object.__init__): raise TypeError("Initialization arguments are not supported") self = object.__new__(cls) impl = _localimpl() impl.localargs = (args, kw) impl.locallock = RLock() object.__setattr__(self, '_local__impl', impl) # We need to create the thread dict in anticipation of # __init__ being called, to make sure we don't call it # again ourselves. impl.create_dict() return self def __getattribute__(self, name): with _patch(self): return object.__getattribute__(self, name) def __setattr__(self, name, value): if name == '__dict__': raise AttributeError( "%r object attribute '__dict__' is read-only" % self.__class__.__name__) with _patch(self): return object.__setattr__(self, name, value) def __delattr__(self, name): if name == '__dict__': raise AttributeError( "%r object attribute '__dict__' is read-only" % self.__class__.__name__) with _patch(self): return object.__delattr__(self, name) from threading import current_thread, RLock
[+]
..
[-] opcode.py
[edit]
[-] token.py
[edit]
[-] keyword.py
[edit]
[-] runpy.py
[edit]
[-] smtplib.py
[edit]
[-] colorsys.py
[edit]
[+]
dbm
[-] posixpath.py
[edit]
[-] bz2.py
[edit]
[-] pydoc.py
[edit]
[-] pkgutil.py
[edit]
[-] selectors.py
[edit]
[-] warnings.py
[edit]
[-] sre_constants.py
[edit]
[-] ipaddress.py
[edit]
[-] pty.py
[edit]
[-] sched.py
[edit]
[-] copyreg.py
[edit]
[-] quopri.py
[edit]
[+]
ensurepip
[+]
collections
[-] getpass.py
[edit]
[-] locale.py
[edit]
[-] shlex.py
[edit]
[-] heapq.py
[edit]
[-] csv.py
[edit]
[+]
ctypes
[-] threading.py
[edit]
[-] pstats.py
[edit]
[+]
lib-dynload
[-] subprocess.py
[edit]
[-] linecache.py
[edit]
[-] base64.py
[edit]
[-] _sysconfigdata__linux_x86_64-linux-gnu.py
[edit]
[-] genericpath.py
[edit]
[-] platform.py
[edit]
[+]
config-3.13-x86_64-linux-gnu
[+]
curses
[-] _compression.py
[edit]
[+]
__pycache__
[-] codecs.py
[edit]
[-] mailbox.py
[edit]
[-] signal.py
[edit]
[-] doctest.py
[edit]
[+]
sysconfig
[-] string.py
[edit]
[-] ast.py
[edit]
[+]
multiprocessing
[-] zipapp.py
[edit]
[+]
zipfile
[-] __future__.py
[edit]
[-] difflib.py
[edit]
[+]
wsgiref
[-] pickle.py
[edit]
[+]
importlib
[-] weakref.py
[edit]
[-] random.py
[edit]
[-] _osx_support.py
[edit]
[-] bdb.py
[edit]
[-] statistics.py
[edit]
[-] hashlib.py
[edit]
[-] graphlib.py
[edit]
[+]
http
[-] _opcode_metadata.py
[edit]
[-] tracemalloc.py
[edit]
[+]
unittest
[-] abc.py
[edit]
[-] hmac.py
[edit]
[-] tokenize.py
[edit]
[-] imaplib.py
[edit]
[-] pdb.py
[edit]
[+]
logging
[-] contextlib.py
[edit]
[-] _strptime.py
[edit]
[-] queue.py
[edit]
[-] this.py
[edit]
[-] os.py
[edit]
[-] netrc.py
[edit]
[-] _threading_local.py
[edit]
[+]
tomllib
[-] stat.py
[edit]
[-] pickletools.py
[edit]
[-] enum.py
[edit]
[-] cmd.py
[edit]
[-] rlcompleter.py
[edit]
[+]
asyncio
[-] _pylong.py
[edit]
[-] _weakrefset.py
[edit]
[-] io.py
[edit]
[-] profile.py
[edit]
[+]
_pyrepl
[-] socketserver.py
[edit]
[+]
sqlite3
[-] decimal.py
[edit]
[-] copy.py
[edit]
[+]
html
[-] bisect.py
[edit]
[-] site.py
[edit]
[-] pprint.py
[edit]
[+]
venv
[-] optparse.py
[edit]
[-] dataclasses.py
[edit]
[-] _pydatetime.py
[edit]
[-] _pyio.py
[edit]
[-] poplib.py
[edit]
[-] glob.py
[edit]
[-] configparser.py
[edit]
[+]
email
[-] fnmatch.py
[edit]
[-] pyclbr.py
[edit]
[-] antigravity.py
[edit]
[-] ftplib.py
[edit]
[-] _ios_support.py
[edit]
[-] getopt.py
[edit]
[-] gettext.py
[edit]
[-] compileall.py
[edit]
[+]
re
[+]
xmlrpc
[-] uuid.py
[edit]
[-] inspect.py
[edit]
[-] ntpath.py
[edit]
[-] fileinput.py
[edit]
[-] gzip.py
[edit]
[+]
site-packages
[-] symtable.py
[edit]
[-] dis.py
[edit]
[-] _sysconfigdata_d_linux_x86_64-linux-gnu.py
[edit]
[-] datetime.py
[edit]
[-] tempfile.py
[edit]
[-] LICENSE.txt
[edit]
[-] _markupbase.py
[edit]
[-] _android_support.py
[edit]
[-] functools.py
[edit]
[-] nturl2path.py
[edit]
[-] timeit.py
[edit]
[+]
json
[-] secrets.py
[edit]
[-] numbers.py
[edit]
[+]
urllib
[-] _compat_pickle.py
[edit]
[-] sre_compile.py
[edit]
[-] socket.py
[edit]
[-] _collections_abc.py
[edit]
[-] __hello__.py
[edit]
[-] cProfile.py
[edit]
[-] trace.py
[edit]
[-] argparse.py
[edit]
[-] traceback.py
[edit]
[-] operator.py
[edit]
[-] calendar.py
[edit]
[-] struct.py
[edit]
[-] webbrowser.py
[edit]
[+]
encodings
[-] types.py
[edit]
[-] _pydecimal.py
[edit]
[-] tarfile.py
[edit]
[-] _aix_support.py
[edit]
[-] plistlib.py
[edit]
[-] textwrap.py
[edit]
[-] tty.py
[edit]
[-] _colorize.py
[edit]
[-] _apple_support.py
[edit]
[-] zipimport.py
[edit]
[-] fractions.py
[edit]
[-] modulefinder.py
[edit]
[-] mimetypes.py
[edit]
[+]
zoneinfo
[-] shelve.py
[edit]
[-] typing.py
[edit]
[-] _sitebuiltins.py
[edit]
[-] code.py
[edit]
[-] py_compile.py
[edit]
[-] wave.py
[edit]
[+]
pathlib
[-] stringprep.py
[edit]
[-] reprlib.py
[edit]
[-] filecmp.py
[edit]
[-] sre_parse.py
[edit]
[-] contextvars.py
[edit]
[-] _py_abc.py
[edit]
[-] lzma.py
[edit]
[+]
xml
[-] ssl.py
[edit]
[+]
concurrent
[-] shutil.py
[edit]
[-] codeop.py
[edit]
[+]
pydoc_data
[-] tabnanny.py
[edit]