Python setuptools.sandbox() Examples

The following are 20 code examples of setuptools.sandbox(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module setuptools , or try the search function .
Example #1
Source File: ez_setup.py    From vpython-jupyter with MIT License 6 votes vote down vote up
def _no_sandbox(function):
    def __no_sandbox(*args, **kw):
        try:
            from setuptools.sandbox import DirectorySandbox
            if not hasattr(DirectorySandbox, '_old'):
                def violation(*args):
                    pass
                DirectorySandbox._old = DirectorySandbox._violation
                DirectorySandbox._violation = violation
                patched = True
            else:
                patched = False
        except ImportError:
            patched = False

        try:
            return function(*args, **kw)
        finally:
            if patched:
                DirectorySandbox._violation = DirectorySandbox._old
                del DirectorySandbox._old

    return __no_sandbox 
Example #2
Source File: ez_setup.py    From drf-haystack with MIT License 6 votes vote down vote up
def _no_sandbox(function):
    def __no_sandbox(*args, **kw):
        try:
            from setuptools.sandbox import DirectorySandbox
            if not hasattr(DirectorySandbox, '_old'):
                def violation(*args):
                    pass
                DirectorySandbox._old = DirectorySandbox._violation
                DirectorySandbox._violation = violation
                patched = True
            else:
                patched = False
        except ImportError:
            patched = False

        try:
            return function(*args, **kw)
        finally:
            if patched:
                DirectorySandbox._violation = DirectorySandbox._old
                del DirectorySandbox._old

    return __no_sandbox 
Example #3
Source File: distribute_setup.py    From PySimulator with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _no_sandbox(function):
    def __no_sandbox(*args, **kw):
        try:
            from setuptools.sandbox import DirectorySandbox
            if not hasattr(DirectorySandbox, '_old'):
                def violation(*args):
                    pass
                DirectorySandbox._old = DirectorySandbox._violation
                DirectorySandbox._violation = violation
                patched = True
            else:
                patched = False
        except ImportError:
            patched = False

        try:
            return function(*args, **kw)
        finally:
            if patched:
                DirectorySandbox._violation = DirectorySandbox._old
                del DirectorySandbox._old

    return __no_sandbox 
Example #4
Source File: distribute_setup.py    From gui-o-matic with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _no_sandbox(function):
    def __no_sandbox(*args, **kw):
        try:
            from setuptools.sandbox import DirectorySandbox
            if not hasattr(DirectorySandbox, '_old'):
                def violation(*args):
                    pass
                DirectorySandbox._old = DirectorySandbox._violation
                DirectorySandbox._violation = violation
                patched = True
            else:
                patched = False
        except ImportError:
            patched = False

        try:
            return function(*args, **kw)
        finally:
            if patched:
                DirectorySandbox._violation = DirectorySandbox._old
                del DirectorySandbox._old

    return __no_sandbox 
Example #5
Source File: distribute_setup.py    From appengine-mapreduce with Apache License 2.0 6 votes vote down vote up
def _no_sandbox(function):
    def __no_sandbox(*args, **kw):
        try:
            from setuptools.sandbox import DirectorySandbox
            if not hasattr(DirectorySandbox, '_old'):
                def violation(*args):
                    pass
                DirectorySandbox._old = DirectorySandbox._violation
                DirectorySandbox._violation = violation
                patched = True
            else:
                patched = False
        except ImportError:
            patched = False

        try:
            return function(*args, **kw)
        finally:
            if patched:
                DirectorySandbox._violation = DirectorySandbox._old
                del DirectorySandbox._old

    return __no_sandbox 
Example #6
Source File: test_sandbox.py    From setuptools with MIT License 6 votes vote down vote up
def test_unpickleable_exception_when_hiding_setuptools(self):
        """
        As revealed in #440, an infinite recursion can occur if an unpickleable
        exception while setuptools is hidden. Ensure this doesn't happen.
        """

        class ExceptionUnderTest(Exception):
            """
            An unpickleable exception (not in globals).
            """

        with pytest.raises(setuptools.sandbox.UnpickleableException) as caught:
            with setuptools.sandbox.save_modules():
                setuptools.sandbox.hide_setuptools()
                raise ExceptionUnderTest()

        msg, = caught.value.args
        assert msg == 'ExceptionUnderTest()' 
Example #7
Source File: distribute_setup.py    From gluttony with MIT License 6 votes vote down vote up
def _no_sandbox(function):
    def __no_sandbox(*args, **kw):
        try:
            from setuptools.sandbox import DirectorySandbox
            if not hasattr(DirectorySandbox, '_old'):
                def violation(*args):
                    pass
                DirectorySandbox._old = DirectorySandbox._violation
                DirectorySandbox._violation = violation
                patched = True
            else:
                patched = False
        except ImportError:
            patched = False

        try:
            return function(*args, **kw)
        finally:
            if patched:
                DirectorySandbox._violation = DirectorySandbox._old
                del DirectorySandbox._old

    return __no_sandbox 
Example #8
Source File: distribute_setup.py    From poclbm with GNU General Public License v3.0 6 votes vote down vote up
def _no_sandbox(function):
    def __no_sandbox(*args, **kw):
        try:
            from setuptools.sandbox import DirectorySandbox
            if not hasattr(DirectorySandbox, '_old'):
                def violation(*args):
                    pass
                DirectorySandbox._old = DirectorySandbox._violation
                DirectorySandbox._violation = violation
                patched = True
            else:
                patched = False
        except ImportError:
            patched = False

        try:
            return function(*args, **kw)
        finally:
            if patched:
                DirectorySandbox._violation = DirectorySandbox._old
                del DirectorySandbox._old

    return __no_sandbox 
Example #9
Source File: ez_setup.py    From nested-dict with MIT License 6 votes vote down vote up
def _no_sandbox(function):
    def __no_sandbox(*args, **kw):
        try:
            from setuptools.sandbox import DirectorySandbox
            if not hasattr(DirectorySandbox, '_old'):
                def violation(*args):
                    pass
                DirectorySandbox._old = DirectorySandbox._violation
                DirectorySandbox._violation = violation
                patched = True
            else:
                patched = False
        except ImportError:
            patched = False

        try:
            return function(*args, **kw)
        finally:
            if patched:
                DirectorySandbox._violation = DirectorySandbox._old
                del DirectorySandbox._old

    return __no_sandbox 
Example #10
Source File: test_sandbox.py    From setuptools with MIT License 5 votes vote down vote up
def test_setup_py_with_BOM(self):
        """
        It should be possible to execute a setup.py with a Byte Order Mark
        """
        target = pkg_resources.resource_filename(
            __name__,
            'script-with-bom.py')
        namespace = types.ModuleType('namespace')
        setuptools.sandbox._execfile(target, vars(namespace))
        assert namespace.result == 'passed' 
Example #11
Source File: test_sandbox.py    From setuptools with MIT License 5 votes vote down vote up
def test_setup_py_with_CRLF(self, tmpdir):
        setup_py = tmpdir / 'setup.py'
        with setup_py.open('wb') as stream:
            stream.write(b'"degenerate script"\r\n')
        setuptools.sandbox._execfile(str(setup_py), globals()) 
Example #12
Source File: test_sandbox.py    From setuptools with MIT License 5 votes vote down vote up
def test_exception_trapped(self):
        with setuptools.sandbox.ExceptionSaver():
            raise ValueError("details") 
Example #13
Source File: test_sandbox.py    From setuptools with MIT License 5 votes vote down vote up
def test_exception_resumed(self):
        with setuptools.sandbox.ExceptionSaver() as saved_exc:
            raise ValueError("details")

        with pytest.raises(ValueError) as caught:
            saved_exc.resume()

        assert isinstance(caught.value, ValueError)
        assert str(caught.value) == 'details' 
Example #14
Source File: test_sandbox.py    From setuptools with MIT License 5 votes vote down vote up
def test_no_exception_passes_quietly(self):
        with setuptools.sandbox.ExceptionSaver() as saved_exc:
            pass

        saved_exc.resume() 
Example #15
Source File: test_sandbox.py    From setuptools with MIT License 5 votes vote down vote up
def test_unpickleable_exception(self):
        class CantPickleThis(Exception):
            "This Exception is unpickleable because it's not in globals"
            def __repr__(self):
                return 'CantPickleThis%r' % (self.args,)

        with setuptools.sandbox.ExceptionSaver() as saved_exc:
            raise CantPickleThis('detail')

        with pytest.raises(setuptools.sandbox.UnpickleableException) as caught:
            saved_exc.resume()

        assert str(caught.value) == "CantPickleThis('detail',)" 
Example #16
Source File: test_sandbox.py    From setuptools with MIT License 5 votes vote down vote up
def test_devnull(self, tmpdir):
        with setuptools.sandbox.DirectorySandbox(str(tmpdir)):
            self._file_writer(os.devnull) 
Example #17
Source File: test_sandbox.py    From setuptools with MIT License 5 votes vote down vote up
def test_sandbox_violation_raised_hiding_setuptools(self, tmpdir):
        """
        When in a sandbox with setuptools hidden, a SandboxViolation
        should reflect a proper exception and not be wrapped in
        an UnpickleableException.
        """

        def write_file():
            "Trigger a SandboxViolation by writing outside the sandbox"
            with open('/etc/foo', 'w'):
                pass

        with pytest.raises(setuptools.sandbox.SandboxViolation) as caught:
            with setuptools.sandbox.save_modules():
                setuptools.sandbox.hide_setuptools()
                with setuptools.sandbox.DirectorySandbox(str(tmpdir)):
                    write_file()

        cmd, args, kwargs = caught.value.args
        assert cmd == 'open'
        assert args == ('/etc/foo', 'w')
        assert kwargs == {}

        msg = str(caught.value)
        assert 'open' in msg
        assert "('/etc/foo', 'w')" in msg 
Example #18
Source File: test_easy_install.py    From setuptools with MIT License 5 votes vote down vote up
def user_install_setup_context(self, *args, **kwargs):
        """
        Wrap sandbox.setup_context to patch easy_install in that context to
        appear as user-installed.
        """
        with self.orig_context(*args, **kwargs):
            import setuptools.command.easy_install as ei
            ei.__file__ = site.USER_SITE
            yield 
Example #19
Source File: test_easy_install.py    From setuptools with MIT License 5 votes vote down vote up
def test_setup_requires_honors_fetch_params(self, mock_index, monkeypatch):
        """
        When easy_install installs a source distribution which specifies
        setup_requires, it should honor the fetch parameters (such as
        index-url, and find-links).
        """
        monkeypatch.setenv(str('PIP_RETRIES'), str('0'))
        monkeypatch.setenv(str('PIP_TIMEOUT'), str('0'))
        with contexts.quiet():
            # create an sdist that has a build-time dependency.
            with TestSetupRequires.create_sdist() as dist_file:
                with contexts.tempdir() as temp_install_dir:
                    with contexts.environment(PYTHONPATH=temp_install_dir):
                        ei_params = [
                            '--index-url', mock_index.url,
                            '--exclude-scripts',
                            '--install-dir', temp_install_dir,
                            dist_file,
                        ]
                        with sandbox.save_argv(['easy_install']):
                            # attempt to install the dist. It should
                            # fail because it doesn't exist.
                            with pytest.raises(SystemExit):
                                easy_install_pkg.main(ei_params)
        # there should have been one requests to the server
        assert [r.path for r in mock_index.requests] == ['/does-not-exist/'] 
Example #20
Source File: distribute_setup.py    From sample-gp-tools with Apache License 2.0 5 votes vote down vote up
def _rename_path(path):
    new_name = path + '.OLD.%s' % time.time()
    log.warn('Renaming %s into %s', path, new_name)
    try:
        from setuptools.sandbox import DirectorySandbox
        def _violation(*args):
            pass
        DirectorySandbox._violation = _violation
    except ImportError:
        pass

    os.rename(path, new_name)
    return new_name