Defer `platform`, `pprint`, `textwrap` and `html` to their use sites · webzuweb/ipython@14e7a5f · GitHub
Skip to content

Commit 14e7a5f

Browse files
committed
Defer platform, pprint, textwrap and html to their use sites
Four more stdlib imports that every `import IPython` paid for and almost no session used: - `platform` in `IPython.utils.sysinfo`, needed only by `pkg_info()`; - `pprint` in the same module, needed only by `sys_info()`; - `textwrap` in `IPython.core.interactiveshell`, needed only to dedent the banner used when `SOURCE_DATE_EPOCH` is set; - `html` (and with it `html.entities`) in `IPython.core.oinspect`, needed only to build the html mime representation of an inspected object; - `platform` in `IPython.lib.pretty`, needed only to special-case PyPy when pretty-printing a `super` object.
1 parent 6245869 commit 14e7a5f

4 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎IPython/core/interactiveshell.py‎

Lines changed: 2 additions & 1 deletion

‎IPython/core/oinspect.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from inspect import signature
1818
from textwrap import dedent
1919
import ast
20-
import html
2120
import inspect
2221
import io as stdlib_io
2322
import linecache
@@ -574,6 +573,8 @@ def _mime_format(self, text:str, formatter=None) -> dict:
574573
Formatters returning strings are supported but this behavior is deprecated.
575574
576575
"""
576+
import html
577+
577578
defaults = {
578579
"text/plain": text,
579580
"text/html": f"<pre>{html.escape(text)}</pre>",

‎IPython/lib/pretty.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def _repr_pretty_(self, p, cycle):
9696
from contextlib import contextmanager
9797
import datetime
9898
import os
99-
import platform
10099
import re
101100
import sys
102101
import types
@@ -705,6 +704,8 @@ def _super_pprint(obj, p, cycle):
705704
p.pretty(obj.__thisclass__)
706705
p.text(',')
707706
p.breakable()
707+
import platform
708+
708709
if platform.python_implementation() == "PyPy": # In PyPy, super() objects don't have __self__ attributes
709710
dself = obj.__repr__.__self__
710711
p.pretty(None if dself is obj else dself)

‎IPython/utils/sysinfo.py‎

Lines changed: 4 additions & 2 deletions

0 commit comments

Comments
 (0)