changeset 51750:c87c56ad6913

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.
author Mads Kiilerich <mads@kiilerich.com>
date Tue, 27 Jun 2023 13:05:03 +0200
parents 16d63d7799fa
children 9b8c71d0b785
files mercurial/utils/resourceutil.py
diffstat 1 files changed, 6 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/utils/resourceutil.py	Mon Jul 22 18:20:03 2024 +0200
+++ b/mercurial/utils/resourceutil.py	Tue Jun 27 13:05:03 2023 +0200
@@ -8,7 +8,6 @@
 # GNU General Public License version 2 or any later version.
 
 
-import _imp
 import os
 import sys
 import typing
@@ -32,7 +31,12 @@
     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
     )