Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-118761: Improve import time of pprint #122725

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

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Changes from 1 commit
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
Next Next commit
Improve import time of pprint
  • Loading branch information
hugovk committed Aug 6, 2024
commit c9533e6bb7f5302c2e5989ed39de889d239571cc
11 changes: 9 additions & 2 deletions 11 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
hugovk marked this conversation as resolved.
Show resolved Hide resolved

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]
hugovk marked this conversation as resolved.
Show resolved Hide resolved
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.