Skip to content

Commit 1bc0227

Browse files
committed
faster check for dunder name
1 parent d1959d2 commit 1bc0227

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Lib/pydoc.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,10 +1721,10 @@ def locate(path, forceload=0):
17211721
object = getattr(object, part)
17221722
except AttributeError:
17231723
return None
1724-
if re.match(r"^__\w+__$", path) and not isinstance(object, (type, type(__import__))):
1725-
# if we're looking up a special variable, don't grab the result from
1726-
# the builtins module, because it's probably not what the user wanted
1727-
# (if it is, they can look up builtins.whatever)
1724+
if _is_dunder_name(path) and not isinstance(object, (type, type(__import__))):
1725+
# if we're looking up a special variable and we don't find a class or a
1726+
# function, it's probably not what the user wanted (if it is, they can
1727+
# look up builtins.whatever)
17281728
return None
17291729
return object
17301730

@@ -1739,7 +1739,7 @@ def resolve(thing, forceload=0):
17391739
if isinstance(thing, str):
17401740
object = locate(thing, forceload)
17411741
if object is None:
1742-
if re.match(r'^__\w+__$', thing):
1742+
if _is_dunder_name(thing):
17431743
special = "Use help('specialnames') for a list of special names for which help is available.\n"
17441744
else:
17451745
special = ""
@@ -1849,6 +1849,9 @@ def _introdoc():
18491849
enter "q", "quit" or "exit".
18501850
''')
18511851

1852+
def _is_dunder_name(x):
1853+
return isinstance(x, str) and len(x) > 4 and x[:2] == x[-2:] == '__'
1854+
18521855
def collect_dunders(symbols):
18531856
dunders = {
18541857
'__name__': ('name_equals_main', ''),

0 commit comments

Comments
 (0)