Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid calling private method in subclasses of KernelSpecManager #339

Merged
merged 2 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]