$23 GRAYBYTE WORDPRESS FILE MANAGER $57

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 104.21.15.130 | ADMIN IP 216.73.217.149
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/cloudlinux/venv/lib/python3.11/site-packages/pyfakefs/

HOME
Current File : /opt/cloudlinux/venv/lib/python3.11/site-packages/pyfakefs//mox3_stubout.py
# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This is a fork of the pymox library intended to work with Python 3.
The file was modified by [email protected] and [email protected]

Previously, pyfakefs used just this file from the mox3 library.
However, mox3 will soon be decommissioned, yet standard mock cannot
be used because of the problem described in pyfakefs #182 and
mock issue 250 (https://github.com/testing-cabal/mock/issues/250).
Therefore just this file was forked from mox3 and incorporated
into pyfakefs.
"""

import inspect


class StubOutForTesting:
    """Sample Usage:

    You want os.path.exists() to always return true during testing.

    stubs = StubOutForTesting()
    stubs.Set(os.path, 'exists', lambda x: 1)
        ...
    stubs.UnsetAll()

    The above changes os.path.exists into a lambda that returns 1.    Once
    the ... part of the code finishes, the UnsetAll() looks up the old value
    of os.path.exists and restores it.

    """

    def __init__(self):
        self.cache = []
        self.stubs = []

    def __del__(self):
        self.smart_unset_all()
        self.unset_all()

    def smart_set(self, obj, attr_name, new_attr):
        """Replace obj.attr_name with new_attr.

        This method is smart and works at the module, class, and instance level
        while preserving proper inheritance. It will not stub out C types
        however unless that has been explicitly allowed by the type.

        This method supports the case where attr_name is a staticmethod or a
        classmethod of obj.

        If obj is an instance, then it is its class that will actually be
        stubbed. Note that the method Set() does not do that: if obj is an
        instance, it (and not its class) will be stubbed.

        Raises AttributeError if the attribute cannot be found.
        """
        if inspect.ismodule(obj) or (
            not inspect.isclass(obj) and attr_name in obj.__dict__
        ):
            orig_obj = obj
            if attr_name in obj.__dict__:
                orig_attr = obj.__dict__[attr_name]
            else:
                orig_attr = None

        else:
            if not inspect.isclass(obj):
                mro = list(inspect.getmro(obj.__class__))
            else:
                mro = list(inspect.getmro(obj))

            mro.reverse()

            orig_attr = None

            for cls in mro:
                try:
                    orig_obj = cls
                    orig_attr = obj.__dict__[attr_name]
                except KeyError:
                    continue

        if orig_attr is None:
            raise AttributeError("Attribute not found.")

        self.stubs.append((orig_obj, attr_name, orig_attr))
        orig_obj.__dict__[attr_name] = new_attr

    def smart_unset_all(self):
        """Reverses all the SmartSet() calls.

        Restores things to their original definition. Its okay to call
        SmartUnsetAll() repeatedly, as later calls have no effect if no
        SmartSet() calls have been made.
        """
        self.stubs.reverse()

        for obj, attr_name, old_attr in self.stubs:
            obj.__dict__[attr_name] = old_attr

        self.stubs = []

    def set(self, parent, child_name, new_child):
        """Replace child_name's old definition with new_child.

        Replace definition in the context of the given parent. The parent could
        be a module when the child is a function at module scope. Or the parent
        could be a class when a class' method is being replaced. The named
        child is set to new_child, while the prior definition is saved away
        for later, when unset_all() is called.

        This method supports the case where child_name is a staticmethod or a
        classmethod of parent.
        """
        old_child = getattr(parent, child_name)

        old_attribute = parent.__dict__.get(child_name)
        if old_attribute is not None:
            if isinstance(old_attribute, staticmethod):
                old_child = staticmethod(old_child)
            elif isinstance(old_attribute, classmethod):
                old_child = classmethod(old_child.__func__)

        self.cache.append((parent, old_child, child_name))
        parent.__dict__[child_name] = new_child

    def unset_all(self):
        """Reverses all the Set() calls.

        Restores things to their original definition. Its okay to call
        unset_all() repeatedly, as later calls have no effect if no Set()
        calls have been made.
        """
        # Undo calls to set() in reverse order, in case set() was called on the
        # same arguments repeatedly (want the original call to be last one
        # undone)
        self.cache.reverse()

        for parent, old_child, child_name in self.cache:
            parent.__dict__[child_name] = old_child
        self.cache = []


Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
9 Apr 2026 8.31 AM
root / root
0755
__pycache__
--
11 Feb 2026 9.30 AM
root / root
0755
pytest_tests
--
11 Feb 2026 9.30 AM
root / root
0755
tests
--
11 Feb 2026 9.30 AM
root / root
0755
__init__.py
0.055 KB
20 Jan 2026 1.01 PM
root / root
0644
_version.py
0.022 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_file.py
49.826 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_filesystem.py
127.073 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_filesystem_shutil.py
6.636 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_filesystem_unittest.py
52.166 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_io.py
6.306 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_legacy_modules.py
3.81 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_open.py
14.952 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_os.py
54.217 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_path.py
20.329 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_pathlib.py
41.795 KB
20 Jan 2026 1.01 PM
root / root
0644
fake_scandir.py
8.772 KB
20 Jan 2026 1.01 PM
root / root
0644
helpers.py
16.716 KB
20 Jan 2026 1.01 PM
root / root
0644
legacy_packages.py
0.877 KB
20 Jan 2026 1.01 PM
root / root
0644
mox3_stubout.py
5.267 KB
20 Jan 2026 1.01 PM
root / root
0644
patched_packages.py
6.439 KB
20 Jan 2026 1.01 PM
root / root
0644
py.typed
0.066 KB
20 Jan 2026 1.01 PM
root / root
0644
pytest_plugin.py
2.659 KB
20 Jan 2026 1.01 PM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025 CONTACT ME
Static GIF