gh-86682: Adds sys._getframemodulename as an alternative to using _getframe (GH-99520)

Also updates calls in collections, doctest, enum, and typing modules to use _getframemodulename first when available.
This commit is contained in:
Steve Dower 2023-01-13 11:31:06 +00:00 committed by GitHub
parent 94fc7706b7
commit b5d4347950
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 200 additions and 14 deletions

View file

@ -862,13 +862,15 @@ class EnumType(type):
member_name, member_value = item
classdict[member_name] = member_value
# TODO: replace the frame hack if a blessed way to know the calling
# module is ever developed
if module is None:
try:
module = sys._getframe(2).f_globals['__name__']
except (AttributeError, ValueError, KeyError):
pass
module = sys._getframemodulename(2)
except AttributeError:
# Fall back on _getframe if _getframemodulename is missing
try:
module = sys._getframe(2).f_globals['__name__']
except (AttributeError, ValueError, KeyError):
pass
if module is None:
_make_class_unpicklable(classdict)
else: