diff options
| author | Michael Merickel <michael@merickel.org> | 2014-11-11 01:34:37 -0600 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2014-11-11 01:35:28 -0600 |
| commit | f3a5679992c51ed3067bb6f5b577dad9fe4274ff (patch) | |
| tree | e870e34761d7ac0866dc408aab30daee9b5b874b | |
| parent | 1b584cf8850e9c75694b0aee7501a49c9c70de63 (diff) | |
| download | pyramid-f3a5679992c51ed3067bb6f5b577dad9fe4274ff.tar.gz pyramid-f3a5679992c51ed3067bb6f5b577dad9fe4274ff.tar.bz2 pyramid-f3a5679992c51ed3067bb6f5b577dad9fe4274ff.zip | |
enable PYTHONSTARTUP support in pshell
Fixes #1299
| -rw-r--r-- | CHANGES.txt | 4 | ||||
| -rw-r--r-- | pyramid/scripts/pshell.py | 10 | ||||
| -rw-r--r-- | pyramid/tests/test_scripts/pystartup.py | 1 | ||||
| -rw-r--r-- | pyramid/tests/test_scripts/test_pshell.py | 28 |
4 files changed, 43 insertions, 0 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index cf2cced51..4bd438bd7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -24,6 +24,10 @@ Features ``431 Request Header Fields Too Large`` in ``pyramid.httpexceptions``. See https://github.com/Pylons/pyramid/pull/1372/files +- The ``pshell`` script will now load a ``PYTHONSTARTUP`` file if one is + defined in the environment prior to launching the interpreter. + See https://github.com/Pylons/pyramid/pull/1299 + Bug Fixes --------- diff --git a/pyramid/scripts/pshell.py b/pyramid/scripts/pshell.py index 12b078677..ef462239b 100644 --- a/pyramid/scripts/pshell.py +++ b/pyramid/scripts/pshell.py @@ -1,9 +1,11 @@ from code import interact import optparse +import os import sys import textwrap from pyramid.compat import configparser +from pyramid.compat import exec_ from pyramid.util import DottedNameResolver from pyramid.paster import bootstrap @@ -51,10 +53,12 @@ class PShellCommand(object): loaded_objects = {} object_help = {} setup = None + pystartup = None def __init__(self, argv, quiet=False): self.quiet = quiet self.options, self.args = self.parser.parse_args(argv[1:]) + self.pystartup = os.environ.get('PYTHONSTARTUP') def pshell_file_config(self, filename): config = self.ConfigParser() @@ -144,6 +148,12 @@ class PShellCommand(object): if shell is None: shell = self.make_shell() + if self.pystartup and os.path.isfile(self.pystartup): + with open(self.pystartup, 'rb') as fp: + exec_(fp.read().decode('utf-8'), env) + if '__builtins__' in env: + del env['__builtins__'] + try: shell(env, help) finally: diff --git a/pyramid/tests/test_scripts/pystartup.py b/pyramid/tests/test_scripts/pystartup.py new file mode 100644 index 000000000..c4e5bcc80 --- /dev/null +++ b/pyramid/tests/test_scripts/pystartup.py @@ -0,0 +1 @@ +foo = 1 diff --git a/pyramid/tests/test_scripts/test_pshell.py b/pyramid/tests/test_scripts/test_pshell.py index 7cb130c41..86858a709 100644 --- a/pyramid/tests/test_scripts/test_pshell.py +++ b/pyramid/tests/test_scripts/test_pshell.py @@ -369,6 +369,34 @@ class TestPShellCommand(unittest.TestCase): self.assertTrue(self.bootstrap.closer.called) self.assertTrue(shell.help) + def test_command_loads_pythonstartup(self): + import os + marker = object() + old_pystartup = os.environ.get('PYTHONSTARTUP', marker) + os.environ['PYTHONSTARTUP'] = ( + os.path.abspath( + os.path.join( + os.path.dirname(__file__), + 'pystartup.py'))) + try: + command = self._makeOne() + shell = dummy.DummyShell() + command.run(shell) + self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') + self.assertEqual(shell.env, { + 'app':self.bootstrap.app, 'root':self.bootstrap.root, + 'registry':self.bootstrap.registry, + 'request':self.bootstrap.request, + 'root_factory':self.bootstrap.root_factory, + 'foo':1, + }) + self.assertTrue(self.bootstrap.closer.called) + self.assertTrue(shell.help) + finally: # pragma: no cover + if old_pystartup is not marker: + os.environ['PYTHONSTARTUP'] = old_pystartup + else: + del os.environ['PYTHONSTARTUP'] class Test_main(unittest.TestCase): def _callFUT(self, argv): |
