Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 25 additions & 14 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@
button.pack(side=BOTTOM)
tk.mainloop()
"""

import _tkinter # If this fails your Python may not be configured for Tk
from tkinter.constants import *
import collections
import enum
import sys
import types

import _tkinter # If this fails your Python may not be configured for Tk
lazy import sys
lazy import types
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy import re
TclError = _tkinter.TclError
from tkinter.constants import *
import re

wantobjects = 1
_debug = False # set to True to print executed Tcl/Tk commands
Expand All @@ -50,22 +49,33 @@
WRITABLE = _tkinter.WRITABLE
EXCEPTION = _tkinter.EXCEPTION

_magic_re = None
_space_re = None

_magic_re = re.compile(r'([\\{}])')
_space_re = re.compile(r'([\s])', re.ASCII)
def _get_magic_re():
"""Internal function."""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this docstring (and below) doesn't add any info. (you can say that this function hides the re import to make it lazy

Suggested change
"""Internal function."""

global _magic_re
if _magic_re is None:
_magic_re = re.compile(r'([\\{}])')
return _magic_re

def _get_space_re():
"""Internal function."""
global _space_re
if _space_re is None:
_space_re = re.compile(r'([\s])', re.ASCII)
return _space_re

def _join(value):
"""Internal function."""
return ' '.join(map(_stringify, value))


def _stringify(value):
"""Internal function."""
if isinstance(value, (list, tuple)):
if len(value) == 1:
value = _stringify(value[0])
if _magic_re.search(value):
if _get_magic_re().search(value):
value = '{%s}' % value
else:
value = '{%s}' % _join(value)
Expand All @@ -76,14 +86,14 @@ def _stringify(value):
value = str(value)
if not value:
value = '{}'
elif _magic_re.search(value):
elif _get_magic_re().search(value):
# add '\' before special characters and spaces
value = _magic_re.sub(r'\\\1', value)
value = _get_magic_re().sub(r'\\\1', value)
value = value.replace('\n', r'\n')
value = _space_re.sub(r'\\\1', value)
value = _get_space_re().sub(r'\\\1', value)
if value[0] == '"':
value = '\\' + value
elif value[0] == '"' or _space_re.search(value):
elif value[0] == '"' or _get_space_re().search(value):
value = '{%s}' % value
return value

Expand Down Expand Up @@ -5094,6 +5104,7 @@ def _test():

__all__ = [name for name, obj in globals().items()
if not name.startswith('_') and not isinstance(obj, types.ModuleType)
and not isinstance(obj, types.LazyImportType)
and name not in {'wantobjects'}]

if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Reduce the import time of :mod:`tkinter` with lazy imports. Patch by Rihaan
Meher.
Loading