Skip to content

Commit

Permalink
Sync with main
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Sep 17, 2024
2 parents cf37b55 + b5cb375 commit affe09b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1
with:
package_name: nbconvert
package_spec: pip install -e ".[test]"
package_spec: -e ".[test]"

jupyter_server:
runs-on: ubuntu-latest
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
fail_under: 78

docs:
runs-on: windows-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
Expand All @@ -89,8 +89,9 @@ jobs:
# If this fails run `hatch run docs:api` locally
# and commit.
git status --porcelain
git status -s | grep "A" && exit 1
git status -s | grep "M" && exit 1
if git status -s | grep "^\s*[AM]"; then
exit 1
fi
echo "API docs done"
- run: hatch run docs:build

Expand Down
4 changes: 2 additions & 2 deletions jupyter_client/jsonutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from datetime import datetime
from typing import Any, Optional, Union

from dateutil.parser import parse as _dateutil_parse
from dateutil.parser import isoparse as _dateutil_parse
from dateutil.tz import tzlocal

next_attr_name = "__next__" # Not sure what downstream library uses this, but left it to be safe
Expand All @@ -28,7 +28,7 @@

# holy crap, strptime is not threadsafe.
# Calling it once at import seems to help.
datetime.strptime("1", "%d") # noqa
datetime.strptime("2000-01-01", "%Y-%m-%d") # noqa

# -----------------------------------------------------------------------------
# Classes and functions
Expand Down
40 changes: 38 additions & 2 deletions jupyter_client/localinterfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ def _load_ips_ipconfig() -> None:
_populate_from_list(addrs)


def _load_ips_psutil() -> None:
"""load ip addresses with netifaces"""
import psutil

global LOCALHOST
local_ips = []
public_ips = []

# dict of iface_name: address_list, eg
# {"lo": [snicaddr(family=<AddressFamily.AF_INET>, address="127.0.0.1",
# ...), snicaddr(family=<AddressFamily.AF_INET6>, ...)]}
for iface, ifaddresses in psutil.net_if_addrs().items():
for address_data in ifaddresses:
if address_data.family == socket.AF_INET:
addr = address_data.address
if not (iface.startswith("lo") or addr.startswith("127.")):
public_ips.append(addr)
elif not LOCALHOST:
LOCALHOST = addr
local_ips.append(addr)
if not LOCALHOST:
# we never found a loopback interface (can this ever happen?), assume common default
LOCALHOST = "127.0.0.1"
local_ips.insert(0, LOCALHOST)
local_ips.extend(["0.0.0.0", ""]) # noqa
LOCAL_IPS[:] = _uniq_stable(local_ips)
PUBLIC_IPS[:] = _uniq_stable(public_ips)


def _load_ips_netifaces() -> None:
"""load ip addresses with netifaces"""
import netifaces # type: ignore[import-not-found]
Expand Down Expand Up @@ -227,13 +256,20 @@ def _load_ips(suppress_exceptions: bool = True) -> None:
This function will only ever be called once.
It will use netifaces to do it quickly if available.
If will use psutil to do it quickly if available.
If not, it will use netifaces to do it quickly if available.
Then it will fallback on parsing the output of ifconfig / ip addr / ipconfig, as appropriate.
Finally, it will fallback on socket.gethostbyname_ex, which can be slow.
"""

try:
# first priority, use netifaces
# first priority, use psutil
try:
return _load_ips_psutil()
except ImportError:
pass

# second priority, use netifaces
try:
return _load_ips_netifaces()
except ImportError:
Expand Down
29 changes: 24 additions & 5 deletions tests/signalkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def shutdown_request(self, stream, ident, parent):
if os.environ.get("NO_SHUTDOWN_REPLY") != "1":
await super().shutdown_request(stream, ident, parent)

def do_execute(
async def do_execute(
self, code, silent, store_history=True, user_expressions=None, allow_stdin=False
):
code = code.strip()
Expand All @@ -47,12 +47,31 @@ def do_execute(
elif code == "env":
reply["user_expressions"]["env"] = os.getenv("TEST_VARS", "")
elif code == "sleep":
try:
time.sleep(10)
except KeyboardInterrupt:
reply["user_expressions"]["interrupted"] = True
import ipykernel

if ipykernel.version_info < (7, 0):
# ipykernel before anyio.
try:
time.sleep(10)
except KeyboardInterrupt:
reply["user_expressions"]["interrupted"] = True
else:
reply["user_expressions"]["interrupted"] = False
else:
# ipykernel after anyio.
from anyio import create_task_group, open_signal_receiver, sleep

async def signal_handler(cancel_scope, reply):
with open_signal_receiver(signal.SIGINT) as signals:
async for _ in signals:
reply["user_expressions"]["interrupted"] = True
cancel_scope.cancel()
return

reply["user_expressions"]["interrupted"] = False
async with create_task_group() as tg:
tg.start_soon(signal_handler, tg.cancel_scope, reply)
tg.start_soon(sleep, 10)
else:
reply["status"] = "error"
reply["ename"] = "Error"
Expand Down

0 comments on commit affe09b

Please sign in to comment.