Skip to content

Commit

Permalink
Merge pull request #339 from minrk/fix-backward-incompat
Browse files Browse the repository at this point in the history
avoid calling private method in subclasses of KernelSpecManager
  • Loading branch information
takluyver authored Jan 23, 2018
2 parents 162cf6f + 7dfa6c4 commit 07d02be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
9 changes: 8 additions & 1 deletion jupyter_client/kernelspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,14 @@ def get_all_specs(self):
res = {}
for kname, resource_dir in d.items():
try:
spec = self._get_kernel_spec_by_name(kname, resource_dir)
if self.__class__ is KernelSpecManager:
spec = self._get_kernel_spec_by_name(kname, resource_dir)
else:
# avoid calling private methods in subclasses,
# which may have overridden find_kernel_specs
# and get_kernel_spec, but not the newer get_all_specs
spec = self.get_kernel_spec(kname)

res[kname] = {
"resource_dir": resource_dir,
"spec": spec.to_dict()
Expand Down
35 changes: 33 additions & 2 deletions jupyter_client/tests/test_kernelspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import copy
import io
import json
from logging import StreamHandler
import os
from os.path import join as pjoin
from subprocess import Popen, PIPE, STDOUT
import sys
import tempfile
import unittest

import pytest
Expand Down Expand Up @@ -156,7 +158,7 @@ def test_validate_kernel_name(self):
'Haskell-1-2-3',
]:
assert kernelspec._is_valid_kernel_name(good)

for bad in [
'has space',
u'ünicode',
Expand All @@ -165,4 +167,33 @@ def test_validate_kernel_name(self):
]:
assert not kernelspec._is_valid_kernel_name(bad)


def test_subclass(self):
"""Test get_all_specs in subclasses that override find_kernel_specs"""
ksm = self.ksm
resource_dir = tempfile.gettempdir()
native_name = kernelspec.NATIVE_KERNEL_NAME
native_kernel = ksm.get_kernel_spec(native_name)

class MyKSM(kernelspec.KernelSpecManager):
def get_kernel_spec(self, name):
spec = copy.copy(native_kernel)
if name == 'fake':
spec.name = name
spec.resource_dir = resource_dir
elif name == native_name:
pass
else:
raise KeyError(name)
return spec

def find_kernel_specs(self):
return {
'fake': resource_dir,
native_name: native_kernel.resource_dir,
}

# ensure that get_all_specs doesn't raise if only
# find_kernel_specs and get_kernel_spec are defined
myksm = MyKSM()
specs = myksm.get_all_specs()
assert sorted(specs) == ['fake', native_name]

0 comments on commit 07d02be

Please sign in to comment.