# HG changeset patch # User Mads Kiilerich # Date 2023-06-27 11:05:03 # Node ID c5d6a66092e862a0d57d97858ffc657b2b40fa2f # Parent 5ac506ee66900b5015a055cc6889c8fdfb1c40e1 utils: avoid using internal _imp.is_frozen() imp has been deprecated for a long time, and were removed in Python 3.12 . As a workaround, we started using the internal _imp. That is ugly and risky. It seems less risky to get the functionality in some other way. Here, we just inspect if 'origin' of the '__main__' module is set and 'frozen'. That seems to work and do the same, and might be better than using the internal _imp directly. This way of inspecting module attributes seems to work in some test cases, but it is a risky change. This level of importlib doesn't have much documentation, a complicated implementation, and we are dealing with some odd use cases. diff --git a/mercurial/utils/resourceutil.py b/mercurial/utils/resourceutil.py --- a/mercurial/utils/resourceutil.py +++ b/mercurial/utils/resourceutil.py @@ -8,7 +8,6 @@ # GNU General Public License version 2 or any later version. -import _imp import os import sys @@ -24,7 +23,12 @@ def mainfrozen(): return ( hasattr(sys, "frozen") # new py2exe or hasattr(sys, "importers") # old py2exe - or _imp.is_frozen("__main__") # tools/freeze + or getattr( + getattr(sys.modules.get('__main__'), '__spec__', None), + 'origin', + None, + ) + == 'frozen' # tools/freeze ) @@ -44,7 +48,6 @@ if mainfrozen() and getattr(sys, "frozen assert dirs[0] == b"mercurial" return os.path.join(_rootpath, *dirs[1:]) - else: datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__))) _rootpath = os.path.dirname(datapath) @@ -90,7 +93,6 @@ except (ImportError, AttributeError): for p in os.listdir(path): yield pycompat.fsencode(p) - else: from .. import encoding