Mercurial > public > mercurial-scm > hg
comparison mercurial/utils/resourceutil.py @ 50925:d718eddf01d9
safehasattr: drop usage in favor of hasattr
The two functions should now be equivalent at least in their usage in core.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Thu, 31 Aug 2023 23:56:15 +0200 |
parents | 847f703a4d13 |
children | 32a1c9226dd9 c5d6a66092e8 |
comparison
equal
deleted
inserted
replaced
50924:7a8ea1397816 | 50925:d718eddf01d9 |
---|---|
20 | 20 |
21 The code supports py2exe (most common, Windows only) and tools/freeze | 21 The code supports py2exe (most common, Windows only) and tools/freeze |
22 (portable, not much used). | 22 (portable, not much used). |
23 """ | 23 """ |
24 return ( | 24 return ( |
25 pycompat.safehasattr(sys, "frozen") # new py2exe | 25 hasattr(sys, "frozen") # new py2exe |
26 or pycompat.safehasattr(sys, "importers") # old py2exe | 26 or hasattr(sys, "importers") # old py2exe |
27 or _imp.is_frozen("__main__") # tools/freeze | 27 or _imp.is_frozen("__main__") # tools/freeze |
28 ) | 28 ) |
29 | 29 |
30 | 30 |
31 # the location of data files matching the source code | 31 # the location of data files matching the source code |
57 # importlib.resources exists from Python 3.7; see fallback in except clause | 57 # importlib.resources exists from Python 3.7; see fallback in except clause |
58 # further down | 58 # further down |
59 from importlib import resources # pytype: disable=import-error | 59 from importlib import resources # pytype: disable=import-error |
60 | 60 |
61 # Force loading of the resources module | 61 # Force loading of the resources module |
62 if pycompat.safehasattr(resources, 'files'): | 62 if hasattr(resources, 'files'): |
63 resources.files # pytype: disable=module-attr | 63 resources.files # pytype: disable=module-attr |
64 else: | 64 else: |
65 resources.open_binary # pytype: disable=module-attr | 65 resources.open_binary # pytype: disable=module-attr |
66 | 66 |
67 # py2exe raises an AssertionError if uses importlib.resources | 67 # py2exe raises an AssertionError if uses importlib.resources |
93 | 93 |
94 else: | 94 else: |
95 from .. import encoding | 95 from .. import encoding |
96 | 96 |
97 def open_resource(package, name): | 97 def open_resource(package, name): |
98 if pycompat.safehasattr(resources, 'files'): | 98 if hasattr(resources, 'files'): |
99 return ( | 99 return ( |
100 resources.files( # pytype: disable=module-attr | 100 resources.files( # pytype: disable=module-attr |
101 pycompat.sysstr(package) | 101 pycompat.sysstr(package) |
102 ) | 102 ) |
103 .joinpath(pycompat.sysstr(name)) | 103 .joinpath(pycompat.sysstr(name)) |