Skip to content

Commit

Permalink
Fix several issues related to compatibility between older and newer p…
Browse files Browse the repository at this point in the history
…ython versions
  • Loading branch information
vladpunko committed Nov 21, 2021
1 parent 1bc41aa commit 3101bef
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ language: python

jobs:
include:
- os: linux
python: 2.7
env: TOXENV=py27

- os: linux
python: 3.5
env: TOXENV=py35
Expand All @@ -22,6 +26,10 @@ jobs:
python: 3.9
env: TOXENV=py39

- os: linux
python: 3.10-dev
env: TOXENV=py310

# Install the required packages not included in the standard distribution.
install: pip install --no-cache-dir --upgrade tox

Expand Down
12 changes: 4 additions & 8 deletions notebook_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import argparse
import base64
import binascii
import collections
import contextlib
import errno
import glob
import io
Expand Down Expand Up @@ -264,10 +262,8 @@ def _write_python_logos(path):
for logo_name, logo_image in logos_spec.items():
try:
with io.open(os.path.join(path, logo_name), mode="wb") as stream_out:
# Skip this step when an unexpected error has occurred.
with contextlib.suppress(binascii.Error):
# Create a new python logo on the current machine.
stream_out.write(base64.b64decode(logo_image))
# Create a new python logo on the current machine.
stream_out.write(base64.b64decode(logo_image))
except (IOError, OSError) as err:
_logger.error("It's impossible to create python logos on the current machine.")
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
Expand Down Expand Up @@ -369,8 +365,8 @@ def initialize_new_notebook_environment():
try:
from jupyter_core.paths import jupyter_path # noqa
except ImportError:
def jupyter_path(path): # this function is to return a list
return [_get_data_path(path)]
def jupyter_path(subdirs): # this function is to return a list
return [_get_data_path(subdirs)]

# Find and remove all python kernels from the working notebook server.
for path in jupyter_path("kernels"):
Expand Down
24 changes: 17 additions & 7 deletions notebook_environments_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
import subprocess
import sys
import unittest
import unittest.mock as mock

import six
from pyfakefs.fake_filesystem_unittest import TestCase

import notebook_environments

try:
import unittest.mock as mock
except ImportError:
import mock


class SysMock(object):

Expand Down Expand Up @@ -85,8 +90,13 @@ def setUp(self):
json.dump(kernel_spec, stream_out)

def tearDown(self):
# Remove all the fake python kernels after the each test case from the current machine.
self.fs.remove_object(self.data_path)
if os.path.exists(self.data_path):
# Remove all the fake python kernels after the each test case from the current machine.
self.fs.remove_object(self.data_path)

def _assertCountEqual(self, *args, **kwargs):
# An unordered sequence comparison asserting that the same elements.
return six.assertCountEqual(self, *args, **kwargs)

@mock.patch.dict("notebook_environments.os.environ", {}, clear=True)
def test_virtual_environment_active(self, sys_mock):
Expand Down Expand Up @@ -194,7 +204,7 @@ def test_get_kernel_name_error(self, logger_mock, basename_mock, sys_mock):
def test_list_kernels_in(self, sys_mock):
sys_mock.deactivate()

self.assertCountEqual(
self._assertCountEqual( # for python2 and python3
list(notebook_environments._list_kernels_in(self.data_path)),
[
notebook_environments._kernel_info(os.path.basename(kernel_path), kernel_path)
Expand Down Expand Up @@ -242,7 +252,7 @@ def test_write_python_logos(self, logger_mock, sys_mock):
self.assertTrue(os.path.isfile(os.path.join(self.data_path, "logo-32x32.png")))
self.assertTrue(os.path.isfile(os.path.join(self.data_path, "logo-64x64.png")))

with mock.patch("notebook_environments.io.open", new=mock.mock_open()) as open_mock:
with mock.patch("notebook_environments.io.open") as open_mock:
# Raise an operating system exception to test a function fault tolerance.
open_mock.side_effect = OSError(errno.EPERM, "")

Expand Down Expand Up @@ -318,7 +328,7 @@ def test_write_kernel_specification(self, logger_mock, sys_mock):
# Check the correctness of the current settings for the installed kernel system.
self.assertEqual(json.load(stream_in), kernel_spec)

with mock.patch("notebook_environments.io.open", new=mock.mock_open()) as open_mock:
with mock.patch("notebook_environments.io.open") as open_mock:
# Raise an operating system exception to test a function fault tolerance.
open_mock.side_effect = OSError(errno.EPERM, "")

Expand Down Expand Up @@ -531,7 +541,7 @@ def test_remove_dead_kernels_error(self, logger_mock, list_kernels_in_mock, sys_
def test_check_and_remove_broken_kernel(self, remove_dir_mock, sys_mock):
sys_mock.deactivate()

with mock.patch("notebook_environments.io.open", new=mock.mock_open()) as open_mock:
with mock.patch("notebook_environments.io.open") as open_mock:
# Raise an operating system exception to test a function fault tolerance.
open_mock.side_effect = OSError(errno.EPERM, "")

Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"Source code": "https://github.com/vladpunko/notebook-environments",
},

python_requires=">=3.0", # this package is to work on python version 2.7 or later
python_requires=">=2.7", # this package is to work on python version 2.7 or later
platforms=["macOS", "POSIX"],
py_modules=["notebook_environments"],
classifiers=[
Expand All @@ -58,14 +58,15 @@
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX",

# "Programming Language :: Python :: 2",
# "Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",

"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Utilities",
Expand Down
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py35, py36, py37, py38, py39
envlist = py{27,35,36,37,38,39,310}

# Ignore errors related to absence of some python interpreters on the current machine.
skip_missing_interpreters = true
Expand All @@ -8,9 +8,12 @@ skip_missing_interpreters = true
commands = pytest --cov --verbose notebook_environments_test.py
deps =
jupyter-core
pyfakefs<4.5.0 # mock the python file system modules
mock
pytest
pytest-cov
six
py{27,35}: pyfakefs<4.5.0 # mock the python file system modules for some previous interpreter versions
py{36,37,38,39,310}: pyfakefs

[testenv:lint]
commands = python -m pre_commit run --all-files --config .githooks.yml
Expand Down

0 comments on commit 3101bef

Please sign in to comment.