Skip to content

Avoid unclean interpreter shutdown #942

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

Merged
merged 3 commits into from
Jul 6, 2023
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
3 changes: 1 addition & 2 deletions src/neo4j/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ def __getattr__(name):
if name in (
"log", "Config", "PoolConfig", "SessionConfig", "WorkspaceConfig"
):
from ._meta import deprecation_warn
deprecation_warn(
_deprecation_warn(
"Importing {} from neo4j is deprecated without replacement. It's "
"internal and will be removed in a future version."
.format(name),
Expand Down
5 changes: 2 additions & 3 deletions src/neo4j/_async/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from ..._meta import USER_AGENT
from ...addressing import ResolvedAddress
from ...api import (
Auth,
ServerInfo,
Version,
)
Expand Down Expand Up @@ -178,7 +179,6 @@ def _to_auth_dict(cls, auth):
if not auth:
return {}
elif isinstance(auth, tuple) and 2 <= len(auth) <= 3:
from ...api import Auth
return vars(Auth("basic", *auth))
else:
try:
Expand Down Expand Up @@ -837,8 +837,7 @@ async def _set_defunct_write(self, error=None, silent=False):
await self._set_defunct(message, error=error, silent=silent)

async def _set_defunct(self, message, error=None, silent=False):
from ._pool import AsyncBoltPool
direct_driver = isinstance(self.pool, AsyncBoltPool)
direct_driver = getattr(self.pool, "is_direct_pool", False)
user_cancelled = isinstance(error, asyncio.CancelledError)

if error:
Expand Down
10 changes: 10 additions & 0 deletions src/neo4j/_async/io/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def __init__(self, opener, pool_config, workspace_config):
self.lock = AsyncCooperativeRLock()
self.cond = AsyncCondition(self.lock)

@property
@abc.abstractmethod
def is_direct_pool(self) -> bool:
...

async def __aenter__(self):
return self

Expand Down Expand Up @@ -490,6 +495,8 @@ async def close(self):

class AsyncBoltPool(AsyncIOPool):

is_direct_pool = True

@classmethod
def open(cls, address, *, pool_config, workspace_config):
"""Create a new BoltPool
Expand Down Expand Up @@ -536,6 +543,8 @@ class AsyncNeo4jPool(AsyncIOPool):
""" Connection pool with routing table.
"""

is_direct_pool = False

@classmethod
def open(cls, *addresses, pool_config, workspace_config,
routing_context=None):
Expand Down Expand Up @@ -578,6 +587,7 @@ def __init__(self, opener, pool_config, workspace_config, address):
self.address = address
self.routing_tables = {}
self.refresh_lock = AsyncRLock()
self.is_direct_pool = False

def __repr__(self):
""" The representation shows the initial routing addresses.
Expand Down
5 changes: 2 additions & 3 deletions src/neo4j/_sync/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from ..._meta import USER_AGENT
from ...addressing import ResolvedAddress
from ...api import (
Auth,
ServerInfo,
Version,
)
Expand Down Expand Up @@ -178,7 +179,6 @@ def _to_auth_dict(cls, auth):
if not auth:
return {}
elif isinstance(auth, tuple) and 2 <= len(auth) <= 3:
from ...api import Auth
return vars(Auth("basic", *auth))
else:
try:
Expand Down Expand Up @@ -837,8 +837,7 @@ def _set_defunct_write(self, error=None, silent=False):
self._set_defunct(message, error=error, silent=silent)

def _set_defunct(self, message, error=None, silent=False):
from ._pool import BoltPool
direct_driver = isinstance(self.pool, BoltPool)
direct_driver = getattr(self.pool, "is_direct_pool", False)
user_cancelled = isinstance(error, asyncio.CancelledError)

if error:
Expand Down
10 changes: 10 additions & 0 deletions src/neo4j/_sync/io/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def __init__(self, opener, pool_config, workspace_config):
self.lock = CooperativeRLock()
self.cond = Condition(self.lock)

@property
@abc.abstractmethod
def is_direct_pool(self) -> bool:
...

def __enter__(self):
return self

Expand Down Expand Up @@ -487,6 +492,8 @@ def close(self):

class BoltPool(IOPool):

is_direct_pool = True

@classmethod
def open(cls, address, *, pool_config, workspace_config):
"""Create a new BoltPool
Expand Down Expand Up @@ -533,6 +540,8 @@ class Neo4jPool(IOPool):
""" Connection pool with routing table.
"""

is_direct_pool = False

@classmethod
def open(cls, *addresses, pool_config, workspace_config,
routing_context=None):
Expand Down Expand Up @@ -575,6 +584,7 @@ def __init__(self, opener, pool_config, workspace_config, address):
self.address = address
self.routing_tables = {}
self.refresh_lock = RLock()
self.is_direct_pool = False

def __repr__(self):
""" The representation shows the initial routing addresses.
Expand Down
2 changes: 1 addition & 1 deletion src/neo4j/time/_clock_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Structure,
)
from platform import uname
from time import time

from . import (
Clock,
Expand Down Expand Up @@ -53,7 +54,6 @@ def available(cls):
return True

def utc_time(self):
from time import time
seconds, nanoseconds = nano_divmod(int(time() * 1000000), 1000000)
return ClockTime(seconds, nanoseconds * 1000)

Expand Down
2 changes: 1 addition & 1 deletion testkitbackend/_async/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import datetime
import json
import re
import ssl
import warnings
from os import path

Expand Down Expand Up @@ -56,7 +57,6 @@ def load_config():
config = json.load(fd)
skips = config["skips"]
features = [k for k, v in config["features"].items() if v is True]
import ssl
if ssl.HAS_TLSv1_3:
features += ["Feature:TLS:1.3"]
return skips, features
Expand Down
2 changes: 1 addition & 1 deletion testkitbackend/_sync/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import datetime
import json
import re
import ssl
import warnings
from os import path

Expand Down Expand Up @@ -56,7 +57,6 @@ def load_config():
config = json.load(fd)
skips = config["skips"]
features = [k for k, v in config["features"].items() if v is True]
import ssl
if ssl.HAS_TLSv1_3:
features += ["Feature:TLS:1.3"]
return skips, features
Expand Down
11 changes: 2 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@


import asyncio
import warnings
import sys
from functools import wraps
from os import environ

import pytest
import pytest_asyncio

from neo4j import (
AsyncGraphDatabase,
ExperimentalWarning,
GraphDatabase,
)
from neo4j._exceptions import BoltHandshakeError
from neo4j._sync.io import Bolt
from neo4j.exceptions import ServiceUnavailable
from neo4j.debug import watch

from . import env

Expand Down Expand Up @@ -189,8 +185,5 @@ def _():

@pytest.fixture
def watcher():
import sys

from neo4j.debug import watch
with watch("neo4j", out=sys.stdout, colour=True):
yield
2 changes: 2 additions & 0 deletions tests/unit/async_/io/test_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def timedout(self):


class AsyncFakeBoltPool(AsyncIOPool):
is_direct_pool = False

def __init__(self, address, *, auth=None, **config):
config["auth"] = static_auth(None)
self.pool_config, self.workspace_config = Config.consume_chain(config, PoolConfig, WorkspaceConfig)
Expand Down
6 changes: 1 addition & 5 deletions tests/unit/common/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import pytest

import neo4j.api
from neo4j.addressing import Address
from neo4j.exceptions import ConfigurationError


Expand Down Expand Up @@ -276,9 +277,6 @@ def test_version_to_bytes_with_valid_bolt_version(


def test_serverinfo_initialization() -> None:

from neo4j.addressing import Address

address = Address(("bolt://localhost", 7687))
version = neo4j.Version(3, 0)

Expand All @@ -301,8 +299,6 @@ def test_serverinfo_initialization() -> None:
def test_serverinfo_with_metadata(
test_input, expected_agent, protocol_version
) -> None:
from neo4j.addressing import Address

address = Address(("bolt://localhost", 7687))
version = neo4j.Version(*protocol_version)

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/sync/io/test_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def timedout(self):


class FakeBoltPool(IOPool):
is_direct_pool = False

def __init__(self, address, *, auth=None, **config):
config["auth"] = static_auth(None)
self.pool_config, self.workspace_config = Config.consume_chain(config, PoolConfig, WorkspaceConfig)
Expand Down