These are chat archives for ipython/ipython

20th
Jan 2016
Dave Hirschfeld
@dhirschfeld
Jan 20 2016 06:52

Has IPython broken exception chaining on Py3 or am I doing something wrong?

In [91]: try:
    ...:     v = {}['a']
    ...: except KeyError as e:
    ...:     raise ValueError('failed') from e
Traceback (most recent call last):

  File "<ipython-input-91-99783b362421>", line 4, in <module>
    raise ValueError('failed') from e

ValueError: failed

A normal Python interpreter:

>>> try:
...     v = {}['a']
... except KeyError as e:
...     raise ValueError('failed') from e
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyError: 'a'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
ValueError: failed
>>>
Ok, it seems like this is only happening in Spyder
Hussain Sultan
@hussainsultan
Jan 20 2016 16:35
Hello. Any ideas on how to add configurations to start a kernel via jupyter_client.manager.start_new_kernel()? I want to set shell colors to `NoColor' without setting up a config file and initialize specific formatters.
Jonathan Frederic
@jdfreder
Jan 20 2016 17:12
@hussainsultan I think the kwargs you pass into that function that aren't explicitly accepted by it eventually end up here: https://github.com/jupyter/jupyter_client/blob/master/jupyter_client/manager.py#L231
Which is to say, they get appended to the cmd used to launch the kernel
Sorry, just the "extra_arguments" key
So jupyter_client.manager.start_new_kernel(..., extra_arguments=["--InteractiveShell.colors = 'NoColor'"])
However, I'm unfamiliar with the particular config traitlet you mention
It has to be one of the kernel for this to work
Hussain Sultan
@hussainsultan
Jan 20 2016 17:17
ahh. let me try that.
@jdfreder Thank you! this worked manager.start_new_kernel(extra_arguments=["--colors='NoColor'"])
Jonathan Frederic
@jdfreder
Jan 20 2016 17:22
Awesome
Jason Grout
@jasongrout
Jan 20 2016 17:51
@minrk or others: just checking - in the message spec, it says that there are default values for an execute_request message's silent parameter (http://jupyter-client.readthedocs.org/en/latest/messaging.html#execute).
Does that mean in your mind that the parameter is optional?
Min RK
@minrk
Jan 20 2016 17:53
I would say so, but I think it's meant more as a recommendation for client APIs with default parameters.
Jason Grout
@jasongrout
Jan 20 2016 17:54
okay, I'm checking, but it looks like silent isn't treated as optional: https://github.com/ipython/ipykernel/blob/60f58112a0068d28c452669d2fd20a5244aab687/ipykernel/kernelbase.py#L351
Jason Grout
@jasongrout
Jan 20 2016 18:01
actually, it would be good to clarify in the messaging spec which fields are optional and which aren't, since some defaults are noted as values in the spec, while other defaults are noted only in the comments
Min RK
@minrk
Jan 20 2016 18:14
Yeah, the I would probably treat all values as required at the spec level, and just use those as API recommendations.
Even if they were optional, no client implementation should leave out any keys that it understands.
I suspect some of that comes from early revisions when we were adding keys.
When a key is added in a minor spec revision, there needs to be a default interpretation when it's undefined.
Jason Grout
@jasongrout
Jan 20 2016 18:17
so new keys are treated as optional until the next major spec revision. That makes sense
okay, so we should clarify the docs, and maybe post to the mailing list to get kernel authors' inputs
Jason Grout
@jasongrout
Jan 20 2016 18:44
@ellisonbg - it looks like you changed the default silent parameter for the kerne.js kernel execute method: jupyter/notebook@f306423
It was the default noted in the spec, false, but you changed it to true. Now the docs are contradictory about the default (https://github.com/jupyter/notebook/blame/master/notebook/static/services/kernels/kernel.js#L700, and then see right below it as well).
Do you know why you changed the default, and are you okay with changing it back to the spec default of silent=false?
(at least change it back in jupyter-js-services - I don't know if it's worth the effort to manage changing the default back in the current notebook)
Min RK
@minrk
Jan 20 2016 20:03
Not worth changing in 4.x, but silent: true default made more sense to me. I don't recall the reason for the switch.
But maybe this is a case where we should update the docs to match the code, rather than the other way around.
Jason Grout
@jasongrout
Jan 20 2016 20:46
sounds like this might be a good thing to take to the mailing list...
Hussain Sultan
@hussainsultan
Jan 20 2016 22:45
This message was deleted
This message was deleted
Hussain Sultan
@hussainsultan
Jan 20 2016 22:59

All:

Consider,

from jupyter_client import manager

(kernel_manager, kernel_client) \
    = manager.start_new_kernel()

def some_other_thread_that_gets_control():
    print "Kernel is alive: {0}".format(kernel_manager.is_alive())
    message = kernel_client.iopub_channel.get_msg()
    print message

print 'stop channels'
kernel_client.stop_channels()
some_other_thread_that_gets_control()

kernel_manager.shutdown_kernel()

Output:

/Users/susie/workspace/MyApp/venv/bin/python /Users/susie/workspace/MyApp/journey/reproduce.py
stop channels
Traceback (most recent call last):
Kernel is alive: True
  File "/Users/suse/workspace/MyApp/journey/reproduce.py", line 15, in <module>
    some_other_thread_that_gets_control()
  File "/Users/susie/workspace/MyApp/journey/reproduce.py", line 10, in get_msg
    message = kernel_client.iopub_channel.get_msg()
  File "/Users/susy/workspace/MyApp/venv/lib/python2.7/site-packages/jupyter_client/blocking/channels.py", line 50, in get_msg
    ready = self.socket.poll(timeout)
AttributeError: 'NoneType' object has no attribute 'poll'

Process finished with exit code 1

We are running in a multiprocessing enviroment and it is causing kernel_client.iopub_channel.get_msg() to be called after kernel_client.stop_channels() and before kernel_manager.shutdown_kernel()

The question I have is why is it advisable to stop_channels before shutdown_kernels.