Skip to content

Commit

Permalink
Don't eagerly import the console to __init__ ('import code' is expens…
Browse files Browse the repository at this point in the history
…ive'); instead expose it as 'defer_imports.console' - Also adjust benchmark to account for PTH file and discover that it's broken! But the test works fine?
  • Loading branch information
Sachaa-Thanasius committed Sep 3, 2024
1 parent 2295585 commit 7309ac8
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 10 deletions.
8 changes: 1 addition & 7 deletions benchmark/bench_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import time
from pathlib import Path

import defer_imports


class CatchTime:
"""A context manager that measures the time taken to execute its body."""
Expand Down Expand Up @@ -45,12 +43,8 @@ def bench_regular() -> float:


def bench_defer_imports() -> float:
defer_imports.install_defer_import_hook()

with CatchTime() as ct:
import benchmark.sample_defer_imports

defer_imports.uninstall_defer_import_hook()
return ct.elapsed


Expand Down Expand Up @@ -101,7 +95,7 @@ def main() -> None:
version_len = len(version_header)
version_divider = "=" * version_len

benchmark_len = 10
benchmark_len = 14
benchmark_header = "Benchmark".ljust(benchmark_len)
benchmark_divider = "=" * benchmark_len

Expand Down
2 changes: 0 additions & 2 deletions src/defer_imports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
manager.
"""

from ._console import DeferredInteractiveConsole
from ._core import __version__, install_defer_import_hook, uninstall_defer_import_hook, until_use


Expand All @@ -15,5 +14,4 @@
"install_defer_import_hook",
"uninstall_defer_import_hook",
"until_use",
"DeferredInteractiveConsole",
)
2 changes: 1 addition & 1 deletion src/defer_imports/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# SPDX-License-Identifier: MIT

if __name__ == "__main__":
from ._console import DeferredInteractiveConsole
from .console import DeferredInteractiveConsole

raise SystemExit(DeferredInteractiveConsole().interact())
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from ._core import DeferredImportKey, DeferredImportProxy, DeferredInstrumenter


__all__ = ("DeferredInteractiveConsole",)


class DeferredInteractiveConsole(InteractiveConsole):
"""An emulator of the interactive Python interpreter, but with defer_import's compile-time hook baked in to ensure that
defer_imports.until_use works as intended directly in the console.
Expand Down

0 comments on commit 7309ac8

Please sign in to comment.