comparison mercurial/utils/resourceutil.py @ 51808: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 f4733654f144
comparison
equal deleted inserted replaced
51807:16d63d7799fa 51808:c87c56ad6913
6 # 6 #
7 # This software may be used and distributed according to the terms of the 7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version. 8 # GNU General Public License version 2 or any later version.
9 9
10 10
11 import _imp
12 import os 11 import os
13 import sys 12 import sys
14 import typing 13 import typing
15 14
16 from .. import pycompat 15 from .. import pycompat
30 (portable, not much used). 29 (portable, not much used).
31 """ 30 """
32 return ( 31 return (
33 hasattr(sys, "frozen") # new py2exe 32 hasattr(sys, "frozen") # new py2exe
34 or hasattr(sys, "importers") # old py2exe 33 or hasattr(sys, "importers") # old py2exe
35 or _imp.is_frozen("__main__") # tools/freeze 34 or getattr(
35 getattr(sys.modules.get('__main__'), '__spec__', None),
36 'origin',
37 None,
38 )
39 == 'frozen' # tools/freeze
36 ) 40 )
37 41
38 42
39 # the location of data files matching the source code 43 # the location of data files matching the source code
40 if mainfrozen() and getattr(sys, "frozen", None) != "macosx_app": 44 if mainfrozen() and getattr(sys, "frozen", None) != "macosx_app":