Skip to content

Commit

Permalink
Merge pull request #753 from jmfrank63/migrate_hazmat_issue_542
Browse files Browse the repository at this point in the history
Migrate hazmat to faked explicit importing and reexporting, to aid static analysis (issue #542)
  • Loading branch information
njsmith authored Dec 15, 2018
2 parents d7bc005 + 0585e14 commit 6d48060
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 58 deletions.
96 changes: 41 additions & 55 deletions trio/hazmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,50 @@
# this lists all possible symbols from trio._core, and then we prune those that
# aren't available on this system. After that we add some symbols from trio/*.py.

__all__ = [
"cancel_shielded_checkpoint",
"Abort",
"wait_task_rescheduled",
"enable_ki_protection",
"disable_ki_protection",
"currently_ki_protected",
"Task",
"checkpoint",
"current_task",
"current_root_task",
"checkpoint_if_cancelled",
"spawn_system_task",
"reschedule",
"remove_instrument",
"add_instrument",
"current_clock",
"current_statistics",
"ParkingLot",
"UnboundedQueue",
"RunVar",
"wait_writable",
"wait_readable",
"notify_fd_close",
"wait_socket_readable",
"wait_socket_writable",
"notify_socket_close",
"TrioToken",
"current_trio_token",
"temporarily_detach_coroutine_object",
"permanently_detach_coroutine_object",
"reattach_detached_coroutine_object",
# kqueue symbols
"current_kqueue",
"monitor_kevent",
"wait_kevent",
# windows symbols
"current_iocp",
"register_with_iocp",
"wait_overlapped",
"monitor_completion_key",
]
# Generally available symbols
from ._core import (
cancel_shielded_checkpoint, Abort, wait_task_rescheduled,
enable_ki_protection, disable_ki_protection, currently_ki_protected, Task,
checkpoint, current_task, ParkingLot, UnboundedQueue, RunVar, TrioToken,
current_trio_token, temporarily_detach_coroutine_object,
permanently_detach_coroutine_object, reattach_detached_coroutine_object,
current_statistics, reschedule, remove_instrument, add_instrument,
current_clock, current_root_task, checkpoint_if_cancelled,
spawn_system_task, wait_socket_readable, wait_socket_writable,
notify_socket_close
)

# Unix-specific symbols
try:
from ._core import (
wait_writable,
wait_readable,
notify_fd_close,
)
except ImportError:
pass

# Kqueue-specific symbols
try:
from ._core import (
current_kqueue,
monitor_kevent,
wait_kevent,
)
except ImportError:
pass

# Windows symbols
try:
from ._core import (
current_iocp, register_with_iocp, wait_overlapped,
monitor_completion_key
)
except ImportError:
pass

from . import _core
# Some hazmat symbols are platform specific
for _sym in list(__all__):
if hasattr(_core, _sym):
globals()[_sym] = getattr(_core, _sym)
else:
# Fool static analysis (at least PyCharm's) into thinking that we're
# not modifying __all__, so it can trust the static list up above.
# https://github.com/python-trio/trio/pull/316#issuecomment-328255867
# This was useful in September 2017. If it's not September 2017 then
# who knows.
remove_from_all = __all__.remove
remove_from_all(_sym)

# Import bits from trio/*.py
if sys.platform.startswith("win"):
from ._wait_for_object import WaitForSingleObject
__all__ += ["WaitForSingleObject"]
3 changes: 0 additions & 3 deletions trio/tests/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ def public_namespaces(module):

NAMESPACES = list(public_namespaces(trio))

# Not yet set up for static analysis:
NAMESPACES.remove("trio.hazmat")


# pylint/jedi often have trouble with alpha releases, where Python's internals
# are in flux, grammar may not have settled down, etc.
Expand Down

0 comments on commit 6d48060

Please sign in to comment.