Skip to content

Commit

Permalink
Improve import time of pprint
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Aug 5, 2024
1 parent 1422500 commit 6919c19
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
"""

import collections as _collections
import dataclasses as _dataclasses
import re
import sys as _sys
import types as _types
from io import StringIO as _StringIO
Expand Down Expand Up @@ -179,6 +177,9 @@ def _format(self, object, stream, indent, allowance, context, level):
max_width = self._width - indent - allowance
if len(rep) > max_width:
p = self._dispatch.get(type(object).__repr__, None)
# Lazy import to improve module import time
import dataclasses as _dataclasses

if p is not None:
context[objid] = 1
p(self, object, stream, indent, allowance, context, level + 1)
Expand All @@ -197,6 +198,9 @@ def _format(self, object, stream, indent, allowance, context, level):
stream.write(rep)

def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
# Lazy import to improve module import time
import dataclasses as _dataclasses

cls_name = object.__class__.__name__
indent += len(cls_name) + 1
items = [(f.name, getattr(object, f.name)) for f in _dataclasses.fields(object) if f.repr]
Expand Down Expand Up @@ -291,6 +295,9 @@ def _pprint_str(self, object, stream, indent, allowance, context, level):
if len(rep) <= max_width1:
chunks.append(rep)
else:
# Lazy import to improve module import time
import re

# A list of alternating (non-space, space) strings
parts = re.findall(r'\S*\s*', line)
assert parts
Expand Down

0 comments on commit 6919c19

Please sign in to comment.