comparison mercurial/utils/resourceutil.py @ 52106:c5d6a66092e8 stable

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 d718eddf01d9
children 747a1370c598
comparison
equal deleted inserted replaced
52105:5ac506ee6690 52106:c5d6a66092e8
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 13
15 from .. import pycompat 14 from .. import pycompat
16 15
22 (portable, not much used). 21 (portable, not much used).
23 """ 22 """
24 return ( 23 return (
25 hasattr(sys, "frozen") # new py2exe 24 hasattr(sys, "frozen") # new py2exe
26 or hasattr(sys, "importers") # old py2exe 25 or hasattr(sys, "importers") # old py2exe
27 or _imp.is_frozen("__main__") # tools/freeze 26 or getattr(
27 getattr(sys.modules.get('__main__'), '__spec__', None),
28 'origin',
29 None,
30 )
31 == 'frozen' # tools/freeze
28 ) 32 )
29 33
30 34
31 # the location of data files matching the source code 35 # the location of data files matching the source code
32 if mainfrozen() and getattr(sys, "frozen", None) != "macosx_app": 36 if mainfrozen() and getattr(sys, "frozen", None) != "macosx_app":
41 # executable. 45 # executable.
42 def _package_path(package): 46 def _package_path(package):
43 dirs = package.split(b".") 47 dirs = package.split(b".")
44 assert dirs[0] == b"mercurial" 48 assert dirs[0] == b"mercurial"
45 return os.path.join(_rootpath, *dirs[1:]) 49 return os.path.join(_rootpath, *dirs[1:])
46
47 50
48 else: 51 else:
49 datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__))) 52 datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__)))
50 _rootpath = os.path.dirname(datapath) 53 _rootpath = os.path.dirname(datapath)
51 54
88 path = pycompat.fsdecode(_package_path(package)) 91 path = pycompat.fsdecode(_package_path(package))
89 92
90 for p in os.listdir(path): 93 for p in os.listdir(path):
91 yield pycompat.fsencode(p) 94 yield pycompat.fsencode(p)
92 95
93
94 else: 96 else:
95 from .. import encoding 97 from .. import encoding
96 98
97 def open_resource(package, name): 99 def open_resource(package, name):
98 if hasattr(resources, 'files'): 100 if hasattr(resources, 'files'):