summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2014-11-17 20:12:08 -0600
committerMichael Merickel <michael@merickel.org>2014-11-17 20:12:08 -0600
commit2b59a35c4b5ec0f7052aacce3ffa66ce74cbae56 (patch)
treed6d9cf9c2a3d60f1fbdf5707bc095c95e7ab4f9a
parent92d84360ca61c54008fe81f9943eddf72fd44caf (diff)
parent0c5e5ac9bbb9d0e1bc4aa8f3497f2c7f54ca96d3 (diff)
downloadpyramid-2b59a35c4b5ec0f7052aacce3ffa66ce74cbae56.tar.gz
pyramid-2b59a35c4b5ec0f7052aacce3ffa66ce74cbae56.tar.bz2
pyramid-2b59a35c4b5ec0f7052aacce3ffa66ce74cbae56.zip
Merge pull request #1448 from Pylons/feature.pshell-pythonstartup
enable PYTHONSTARTUP support in pshell
-rw-r--r--CHANGES.txt4
-rw-r--r--pyramid/scripts/pshell.py9
-rw-r--r--pyramid/tests/test_scripts/pystartup.py1
-rw-r--r--pyramid/tests/test_scripts/test_pshell.py23
4 files changed, 37 insertions, 0 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index fc1431c94..bcd4498c5 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/1448
+
- Make it simple to define notfound and forbidden views that wish to use
the default exception-response view but with altered predicates and other
configuration options. The ``view`` argument is now optional in
diff --git a/pyramid/scripts/pshell.py b/pyramid/scripts/pshell.py
index 12b078677..1168ba78a 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,6 +53,7 @@ class PShellCommand(object):
loaded_objects = {}
object_help = {}
setup = None
+ pystartup = os.environ.get('PYTHONSTARTUP')
def __init__(self, argv, quiet=False):
self.quiet = quiet
@@ -144,6 +147,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..a6ba2eaea 100644
--- a/pyramid/tests/test_scripts/test_pshell.py
+++ b/pyramid/tests/test_scripts/test_pshell.py
@@ -1,3 +1,4 @@
+import os
import unittest
from pyramid.tests.test_scripts import dummy
@@ -24,6 +25,9 @@ class TestPShellCommand(unittest.TestCase):
self.options.python_shell = ''
self.options.setup = None
cmd.options = self.options
+ # default to None to prevent side-effects from running tests in
+ # unknown environments
+ cmd.pystartup = None
return cmd
def test_make_default_shell(self):
@@ -369,6 +373,25 @@ class TestPShellCommand(unittest.TestCase):
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
+ def test_command_loads_pythonstartup(self):
+ command = self._makeOne()
+ command.pystartup = (
+ os.path.abspath(
+ os.path.join(
+ os.path.dirname(__file__),
+ 'pystartup.py')))
+ 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)
class Test_main(unittest.TestCase):
def _callFUT(self, argv):