Skip to content

Commit

Permalink
removed default fall through when passed a specific shell
Browse files Browse the repository at this point in the history
  • Loading branch information
sontek committed Oct 11, 2015
1 parent e0a40ab commit b932a45
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
20 changes: 8 additions & 12 deletions docs/narr/commandline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ exposed, and the request is configured to generate urls from the host
.. code-block:: text
$ $VENV/bin/pshell starter/development.ini
Python 2.6.5 (r265:79063, Apr 29 2010, 00:31:32)
Python 2.6.5 (r265:79063, Apr 29 2010, 00:31:32)
[GCC 4.4.3] on linux2
Type "help" for more information.
Expand Down Expand Up @@ -278,10 +278,10 @@ IPython or bpython

If you have `IPython <http://en.wikipedia.org/wiki/IPython>`_ and/or
`bpython <http://bpython-interpreter.org/>`_ in
the interpreter you use to invoke the ``pshell`` command, ``pshell`` will
the interpreter you use to invoke the ``pshell`` command, ``pshell`` will
autodiscover and use the first one found, in this order:
IPython, bpython, standard Python interpreter. However you could
specifically invoke one of your choice with the ``-p choice`` or
IPython, bpython, standard Python interpreter. However you could
specifically invoke one of your choice with the ``-p choice`` or
``--python-shell choice`` option.

.. code-block:: text
Expand All @@ -308,20 +308,16 @@ and then your shell factory should return a function that accepts two arguments,
.. code-block:: python
def ptpython_shell_factory():
try:
from ptpython.repl import embed
def PTPShell(banner, **kwargs):
print(banner)
return embed(**kwargs)
except ImportError:
return None
from ptpython.repl import embed
def PTPShell(banner, **kwargs):
print(banner)
return embed(**kwargs)
def shell(env, help):
PTPShell(banner=help, locals=env)
return shell
.. index::
pair: routes; printing
single: proutes
Expand Down
17 changes: 13 additions & 4 deletions pyramid/scripts/pshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run(self, shell=None):
env = self.bootstrap[0](config_uri, options=parse_vars(self.args[1:]))

# remove the closer from the env
closer = env.pop('closer')
self.closer = env.pop('closer')

# setup help text for default environment
env_help = dict(env)
Expand Down Expand Up @@ -148,7 +148,12 @@ def run(self, shell=None):
help += '\n %-12s %s' % (var, self.object_help[var])

if shell is None:
shell = self.make_shell()
try:
shell = self.make_shell()
except ValueError as e:
self.out(str(e))
self.closer()
return 1

if self.pystartup and os.path.isfile(self.pystartup):
with open(self.pystartup, 'rb') as fp:
Expand All @@ -159,12 +164,12 @@ def run(self, shell=None):
try:
shell(env, help)
finally:
closer()
self.closer()

def make_shell(self):
shells = {}

priority_order = ['ipython', 'bpython']
priority_order = ['ipython', 'bpython', 'python']

for ep in self.pkg_resources.iter_entry_points('pyramid.pshell'):
name = ep.name
Expand All @@ -189,6 +194,10 @@ def make_shell(self):

if factory is not None:
shell = factory()
else:
raise ValueError(
'could not find a shell named "%s"' % user_shell
)

if shell is None:
shell = self.make_default_shell()
Expand Down
24 changes: 14 additions & 10 deletions pyramid/tests/test_scripts/test_pshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def test_command_loads_default_shell(self):

def test_command_loads_default_shell_with_unknown_shell(self):
command = self._makeOne()
out_calls = []

def out(msg):
out_calls.append(msg)

command.out = out

shell = dummy.DummyShell()
bad_shell = dummy.DummyShell()

Expand All @@ -101,21 +108,17 @@ def test_command_loads_default_shell_with_unknown_shell(self):
)

command.make_default_shell = lambda: shell
command.options.python_shell = 'unknow_python_shell'
command.run()
command.options.python_shell = 'unknown_python_shell'
result = command.run()
self.assertEqual(result, 1)
self.assertEqual(
out_calls, ['could not find a shell named "unknown_python_shell"']
)
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
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,
})
self.assertEqual(bad_shell.env, {})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)

def test_command_loads_ipython_v1_1(self):
command = self._makeOne()
Expand Down Expand Up @@ -219,6 +222,7 @@ def test_shell_ordering(self):
{
'ipython': lambda: ipshell,
'bpython': lambda: bpshell,
'python': lambda: dshell,
}
)

Expand Down

0 comments on commit b932a45

Please sign in to comment.