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

Another increment for static analysis #659

Merged
merged 3 commits into from
Sep 12, 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
1 change: 0 additions & 1 deletion trio/_core/_ki.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inspect
import signal
import sys
import threading
from contextlib import contextmanager
from functools import wraps

Expand Down
8 changes: 1 addition & 7 deletions trio/_core/_multierror.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import traceback
import textwrap
import warnings
import types
from contextlib import contextmanager

import attr

Expand Down Expand Up @@ -186,11 +184,7 @@ def __new__(cls, exceptions):
return self

def __str__(self):
def format_child(exc):
#return "{}: {}".format(exc.__class__.__name__, exc)
return repr(exc)

return ", ".join(format_child(exc) for exc in self.exceptions)
return ", ".join(repr(exc) for exc in self.exceptions)

def __repr__(self):
return "<MultiError: {}>".format(self)
Expand Down
8 changes: 3 additions & 5 deletions trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from ._entry_queue import EntryQueue, TrioToken
from ._exceptions import (TrioInternalError, RunFinishedError, Cancelled)
from ._ki import (
LOCALS_KEY_KI_PROTECTION_ENABLED, currently_ki_protected, ki_manager,
enable_ki_protection
LOCALS_KEY_KI_PROTECTION_ENABLED, ki_manager, enable_ki_protection
)
from ._multierror import MultiError
from ._traps import (
Expand Down Expand Up @@ -445,9 +444,8 @@ def _add_exc(self, exc):
self.cancel_scope.cancel()

def _check_nursery_closed(self):
if (
not self._nested_child_running and not self._children
and not self._pending_starts
if not any(
[self._nested_child_running, self._children, self._pending_starts]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this is an improvement in readability? Consistency-wise I don't think Trio ever uses any() over a literal list of bools and I personally avoid that style because I feel it's less expressive.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I do find it reads more naturally this way, as starting with "not any" makes reading the three attributes easier. It's also closer to English grammar!

However, if there's any doubt or disagreement I'd rather take it out and get the rest merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I guess I'd do it the way I originally did it? But they're both fine and let's focus on more important things :-)

):
self._closed = True
if self._parent_waiting_in_aexit:
Expand Down
1 change: 0 additions & 1 deletion trio/_core/tests/test_multierror.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from .tutil import slow

from ..._deprecate import TrioDeprecationWarning
from .._multierror import MultiError, concat_tb


Expand Down
2 changes: 1 addition & 1 deletion trio/_deprecate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from functools import wraps, partial
from functools import wraps
from types import ModuleType
import warnings

Expand Down
41 changes: 25 additions & 16 deletions trio/_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import idna as _idna

from . import _core
from ._deprecate import deprecated
from ._threads import run_sync_in_worker_thread
from ._util import fspath

Expand Down Expand Up @@ -80,7 +79,7 @@ async def __aexit__(self, etype, value, tb):
if _sys.platform == "win32":
# See https://github.com/python-trio/trio/issues/39
# (you can still get it from stdlib socket, of course, if you want it)
del SO_REUSEADDR
globals().pop("SO_REUSEADDR", None)
__all__.remove("SO_REUSEADDR")

# As of at least 3.6, python on Windows is missing IPPROTO_IPV6
Expand Down Expand Up @@ -211,7 +210,8 @@ async def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
# with the _NUMERIC_ONLY flags set, and then only spawn a thread if that
# fails with EAI_NONAME:
def numeric_only_failure(exc):
return isinstance(exc, gaierror) and exc.errno == EAI_NONAME
return isinstance(exc, _stdlib_socket.gaierror) and \
exc.errno == _stdlib_socket.EAI_NONAME

async with _try_sync(numeric_only_failure):
return _stdlib_socket.getaddrinfo(
Expand Down Expand Up @@ -328,7 +328,12 @@ def socketpair(*args, **kwargs):

@_wraps(_stdlib_socket.socket, assigned=(), updated=())
@_add_to_all
def socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None):
def socket(
family=_stdlib_socket.AF_INET,
type=_stdlib_socket.SOCK_STREAM,
proto=0,
fileno=None
):
"""Create a new trio socket, like :func:`socket.socket`.

This function's behavior can be customized using
Expand Down Expand Up @@ -468,8 +473,8 @@ async def bind(self, address):
await _core.checkpoint()
address = await self._resolve_local_address(address)
if (
hasattr(_stdlib_socket, "AF_UNIX") and self.family == AF_UNIX
and address[0]
hasattr(_stdlib_socket, "AF_UNIX")
and self.family == _stdlib_socket.AF_UNIX and address[0]
):
# Use a thread for the filesystem traversal (unless it's an
# abstract domain socket)
Expand All @@ -485,7 +490,7 @@ def shutdown(self, flag):
# no need to worry about return value b/c always returns None:
self._sock.shutdown(flag)
# only do this if the call succeeded:
if flag in [SHUT_WR, SHUT_RDWR]:
if flag in [_stdlib_socket.SHUT_WR, _stdlib_socket.SHUT_RDWR]:
self._did_shutdown_SHUT_WR = True

async def wait_writable(self):
Expand All @@ -500,18 +505,18 @@ async def wait_writable(self):
# etc.
async def _resolve_address(self, address, flags):
# Do some pre-checking (or exit early for non-IP sockets)
if self._sock.family == AF_INET:
if self._sock.family == _stdlib_socket.AF_INET:
if not isinstance(address, tuple) or not len(address) == 2:
await _core.checkpoint()
raise ValueError("address should be a (host, port) tuple")
elif self._sock.family == AF_INET6:
elif self._sock.family == _stdlib_socket.AF_INET6:
if not isinstance(address, tuple) or not 2 <= len(address) <= 4:
await _core.checkpoint()
raise ValueError(
"address should be a (host, port, [flowinfo, [scopeid]]) "
"tuple"
)
elif self._sock.family == AF_UNIX:
elif self._sock.family == _stdlib_socket.AF_UNIX:
await _core.checkpoint()
# unwrap path-likes
return fspath(address)
Expand All @@ -532,9 +537,11 @@ async def _resolve_address(self, address, flags):
# for ipv6 address resolution on travis-ci, which as of 2017-03-07 has
# no ipv6.
# flags |= AI_ADDRCONFIG
if self._sock.family == AF_INET6:
if not self._sock.getsockopt(IPPROTO_IPV6, IPV6_V6ONLY):
flags |= AI_V4MAPPED
if self._sock.family == _stdlib_socket.AF_INET6:
if not self._sock.getsockopt(
IPPROTO_IPV6, _stdlib_socket.IPV6_V6ONLY
):
flags |= _stdlib_socket.AI_V4MAPPED
gai_res = await getaddrinfo(
host, port, self._sock.family, self.type, self._sock.proto, flags
)
Expand All @@ -545,7 +552,7 @@ async def _resolve_address(self, address, flags):
(*_, normed), *_ = gai_res
# The above ignored any flowid and scopeid in the passed-in address,
# so restore them if present:
if self._sock.family == AF_INET6:
if self._sock.family == _stdlib_socket.AF_INET6:
normed = list(normed)
assert len(normed) == 4
if len(address) >= 3:
Expand All @@ -557,7 +564,7 @@ async def _resolve_address(self, address, flags):

# Returns something appropriate to pass to bind()
async def _resolve_local_address(self, address):
return await self._resolve_address(address, AI_PASSIVE)
return await self._resolve_address(address, _stdlib_socket.AI_PASSIVE)

# Returns something appropriate to pass to connect()/sendto()/sendmsg()
async def _resolve_remote_address(self, address):
Expand Down Expand Up @@ -699,7 +706,9 @@ async def connect(self, address):
self._sock.close()
raise
# Okay, the connect finished, but it might have failed:
err = self._sock.getsockopt(SOL_SOCKET, SO_ERROR)
err = self._sock.getsockopt(
_stdlib_socket.SOL_SOCKET, _stdlib_socket.SO_ERROR
)
if err != 0:
raise OSError(err, "Error in connect: " + _os.strerror(err))

Expand Down
11 changes: 7 additions & 4 deletions trio/_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,9 @@ async def _retry(self, fn, *args, ignore_want_read=False):
ret = fn(*args)
except _stdlib_ssl.SSLWantReadError:
want_read = True
except (SSLError, CertificateError) as exc:
except (
_stdlib_ssl.SSLError, _stdlib_ssl.CertificateError
) as exc:
self._state = _State.BROKEN
raise BrokenStreamError from exc
else:
Expand Down Expand Up @@ -629,9 +631,10 @@ async def receive_some(self, max_bytes):
# SSLSyscallError instead of SSLEOFError (e.g. on my linux
# laptop, but not on appveyor). Thanks openssl.
if (
self._https_compatible and
isinstance(exc.__cause__,
(SSLEOFError, SSLSyscallError))
self._https_compatible and isinstance(
exc.__cause__,
(_stdlib_ssl.SSLEOFError, _stdlib_ssl.SSLSyscallError)
)
):
return b""
else:
Expand Down
1 change: 0 additions & 1 deletion trio/testing/_checkpoints.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from contextlib import contextmanager

from .. import _core
from .._deprecate import deprecated

__all__ = ["assert_checkpoints", "assert_no_checkpoints"]

Expand Down
3 changes: 0 additions & 3 deletions trio/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import signal

import pytest
Expand All @@ -11,9 +10,7 @@

# Delete when catch_signals is removed
async def test_catch_signals(recwarn):
print = lambda *args: None
orig = signal.getsignal(signal.SIGILL)
print(orig)
with catch_signals([signal.SIGILL]) as queue:
# Raise it a few times, to exercise signal coalescing, both at the
# call_soon level and at the SignalQueue level
Expand Down
1 change: 0 additions & 1 deletion trio/tests/test_ssl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

from pathlib import Path
import threading
import socket as stdlib_socket
import ssl as stdlib_ssl
Expand Down