Skip to content

Commit 826cd56

Browse files
committed
Drop ipython_genutils requirement
* use `from traitlets.utils import filefind` * `ensure_dir_exists` -> `os.makedirs(..., exist_ok=True)` * `buffer_to_bytes` - use in-place copy * `TemporaryDirectory` - use from `tempfile` * `TemporaryWorkingDirectory` - make private copy * `str_to_bytes` - use `str.encode("utf-8")` for test cases
1 parent 7268d55 commit 826cd56

File tree

8 files changed

+55
-23
lines changed

8 files changed

+55
-23
lines changed

ipykernel/connect.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import sys
88
from subprocess import Popen, PIPE
99

10-
1110
import jupyter_client
1211
from jupyter_client import write_connection_file
1312

@@ -21,7 +20,7 @@ def get_connection_file(app=None):
2120
app : IPKernelApp instance [optional]
2221
If unspecified, the currently running app will be used
2322
"""
24-
from ipython_genutils.path import filefind
23+
from traitlets.utils import filefind
2524
if app is None:
2625
from ipykernel.kernelapp import IPKernelApp
2726
if not IPKernelApp.initialized():

ipykernel/kernelapp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
from IPython.core.shellapp import (
2727
InteractiveShellApp, shell_flags, shell_aliases
2828
)
29-
from ipython_genutils.path import filefind, ensure_dir_exists
3029
from traitlets import (
3130
Any, Instance, Dict, Unicode, Integer, Bool, DottedObjectName, Type, default
3231
)
3332
from traitlets.utils.importstring import import_item
33+
from traitlets.utils import filefind
3434
from jupyter_core.paths import jupyter_runtime_dir
3535
from jupyter_client import write_connection_file
3636
from jupyter_client.connect import ConnectionFileMixin
@@ -260,7 +260,7 @@ def init_connection_file(self):
260260
except IOError:
261261
self.log.debug("Connection file not found: %s", self.connection_file)
262262
# This means I own it, and I'll create it in this directory:
263-
ensure_dir_exists(os.path.dirname(self.abs_connection_file), 0o700)
263+
os.makedirs(os.path.dirname(self.abs_connection_file), mode=0o700, exist_ok=True)
264264
# Also, I will clean it up:
265265
atexit.register(self.cleanup_connection_file)
266266
return

ipykernel/pickleutil.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
# Copyright (c) IPython Development Team.
44
# Distributed under the terms of the Modified BSD License.
5-
5+
import typing
66
import warnings
7+
78
warnings.warn("ipykernel.pickleutil is deprecated. It has moved to ipyparallel.",
89
DeprecationWarning,
910
stacklevel=2
@@ -15,7 +16,6 @@
1516
from types import FunctionType
1617

1718
from traitlets.utils.importstring import import_item
18-
from ipython_genutils.py3compat import buffer_to_bytes
1919

2020
# This registers a hook when it's imported
2121
from ipyparallel.serialize import codeutil # noqa F401
@@ -279,11 +279,18 @@ def get_object(self, g=None):
279279

280280

281281
class CannedBytes(CannedObject):
282-
wrap = staticmethod(buffer_to_bytes)
282+
@staticmethod
283+
def wrap(buf: typing.Union[memoryview, bytes, typing.SupportsBytes]) -> bytes:
284+
"""Cast a buffer or memoryview object to bytes"""
285+
if isinstance(buf, memoryview):
286+
return buf.tobytes()
287+
if not isinstance(buf, bytes):
288+
return bytes(buf)
289+
return buf
283290

284291
def __init__(self, obj):
285292
self.buffers = [obj]
286-
293+
287294
def get_object(self, g=None):
288295
data = self.buffers[0]
289296
return self.wrap(data)

ipykernel/tests/test_connect.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@
66
import errno
77
import json
88
import os
9+
from tempfile import TemporaryDirectory
910
from unittest.mock import patch
1011

1112
import pytest
1213
import zmq
1314

1415
from traitlets.config import Config
15-
from ipython_genutils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory
16-
from ipython_genutils.py3compat import str_to_bytes
1716
from ipykernel import connect
1817
from ipykernel.kernelapp import IPKernelApp
1918

19+
from .utils import TemporaryWorkingDirectory
2020

21-
sample_info = dict(ip='1.2.3.4', transport='ipc',
22-
shell_port=1, hb_port=2, iopub_port=3, stdin_port=4, control_port=5,
23-
key=b'abc123', signature_scheme='hmac-md5',
24-
)
21+
22+
sample_info = {
23+
'ip': '1.2.3.4',
24+
'transport': 'ipc',
25+
'shell_port': 1,
26+
'hb_port': 2,
27+
'iopub_port': 3,
28+
'stdin_port': 4,
29+
'control_port': 5,
30+
'key': b'abc123',
31+
'signature_scheme': 'hmac-md5',
32+
}
2533

2634

2735
class DummyKernelApp(IPKernelApp):
@@ -60,12 +68,12 @@ def test_get_connection_info():
6068
info = connect.get_connection_info(cf, unpack=True)
6169
assert isinstance(json_info, str)
6270

63-
sub_info = {k:v for k,v in info.items() if k in sample_info}
71+
sub_info = {k: v for k, v in info.items() if k in sample_info}
6472
assert sub_info == sample_info
6573

6674
info2 = json.loads(json_info)
67-
info2['key'] = str_to_bytes(info2['key'])
68-
sub_info2 = {k:v for k,v in info.items() if k in sample_info}
75+
info2['key'] = info2['key'].encode("utf-8")
76+
sub_info2 = {k: v for k, v in info.items() if k in sample_info}
6977
assert sub_info2 == sample_info
7078

7179

ipykernel/tests/test_embed_kernel.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from jupyter_client import BlockingKernelClient
1616
from jupyter_core import paths
17-
from ipython_genutils import py3compat
1817

1918

2019
SETUP_TIMEOUT = 60
@@ -41,7 +40,7 @@ def connection_file_ready(connection_file):
4140
except ValueError:
4241
return False
4342

44-
kernel = Popen([sys.executable, '-c', cmd], stdout=PIPE, stderr=PIPE)
43+
kernel = Popen([sys.executable, '-c', cmd], stdout=PIPE, stderr=PIPE, encoding="utf-8")
4544
try:
4645
connection_file = os.path.join(
4746
paths.jupyter_runtime_dir(),
@@ -58,8 +57,7 @@ def connection_file_ready(connection_file):
5857
time.sleep(0.1)
5958

6059
if kernel.poll() is not None:
61-
o,e = kernel.communicate()
62-
e = py3compat.cast_unicode(e)
60+
o, e = kernel.communicate()
6361
raise IOError("Kernel failed to start:\n%s" % e)
6462

6563
if not os.path.exists(connection_file):

ipykernel/tests/test_kernel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os.path
99
import sys
1010
import time
11+
from tempfile import TemporaryDirectory
1112

1213
from flaky import flaky
1314
import pytest
@@ -16,7 +17,6 @@
1617
from IPython.testing import decorators as dec, tools as tt
1718
import IPython
1819
from IPython.paths import locate_profile
19-
from ipython_genutils.tempdir import TemporaryDirectory
2020

2121
from .utils import (
2222
new_kernel, kernel, TIMEOUT, assemble_output, execute,

ipykernel/tests/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# Distributed under the terms of the Modified BSD License.
55

66
import atexit
7+
import os
78
import sys
9+
from tempfile import TemporaryDirectory
810
from time import time
911

1012
from contextlib import contextmanager
@@ -180,3 +182,22 @@ def wait_for_idle(kc):
180182
content = msg['content']
181183
if msg_type == 'status' and content['execution_state'] == 'idle':
182184
break
185+
186+
187+
class TemporaryWorkingDirectory(TemporaryDirectory):
188+
"""
189+
Creates a temporary directory and sets the cwd to that directory.
190+
Automatically reverts to previous cwd upon cleanup.
191+
Usage example:
192+
193+
with TemporaryWorkingDirectory() as tmpdir:
194+
...
195+
"""
196+
def __enter__(self):
197+
self.old_wd = os.getcwd()
198+
os.chdir(self.name)
199+
return super().__enter__()
200+
201+
def __exit__(self, exc, value, tb):
202+
os.chdir(self.old_wd)
203+
return super().__exit__(exc, value, tb)

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def run(self):
6262
keywords=['Interactive', 'Interpreter', 'Shell', 'Web'],
6363
python_requires='>=3.7',
6464
install_requires=[
65-
"ipython_genutils",
6665
'importlib-metadata<5;python_version<"3.8.0"',
6766
'argcomplete>=1.12.3;python_version<"3.8.0"',
6867
'debugpy>=1.0.0,<2.0',

0 commit comments

Comments
 (0)