|
7 | 7 | import json
|
8 | 8 | import email
|
9 | 9 | import types
|
10 |
| -import inspect |
11 | 10 | import pathlib
|
12 | 11 | import operator
|
13 | 12 | import textwrap
|
@@ -232,9 +231,26 @@ def matches(self, **params):
|
232 | 231 | >>> ep.matches(attr='bong')
|
233 | 232 | True
|
234 | 233 | """
|
| 234 | + self._disallow_dist(params) |
235 | 235 | attrs = (getattr(self, param) for param in params)
|
236 | 236 | return all(map(operator.eq, params.values(), attrs))
|
237 | 237 |
|
| 238 | + @staticmethod |
| 239 | + def _disallow_dist(params): |
| 240 | + """ |
| 241 | + Querying by dist is not allowed (dist objects are not comparable). |
| 242 | + >>> EntryPoint(name='fan', value='fav', group='fag').matches(dist='foo') |
| 243 | + Traceback (most recent call last): |
| 244 | + ... |
| 245 | + ValueError: "dist" is not suitable for matching... |
| 246 | + """ |
| 247 | + if "dist" in params: |
| 248 | + raise ValueError( |
| 249 | + '"dist" is not suitable for matching. ' |
| 250 | + "Instead, use Distribution.entry_points.select() on a " |
| 251 | + "located distribution." |
| 252 | + ) |
| 253 | + |
238 | 254 | def _key(self):
|
239 | 255 | return self.name, self.value, self.group
|
240 | 256 |
|
@@ -378,6 +394,17 @@ def locate_file(self, path: str | os.PathLike[str]) -> SimplePath:
|
378 | 394 | """
|
379 | 395 | Given a path to a file in this distribution, return a SimplePath
|
380 | 396 | to it.
|
| 397 | +
|
| 398 | + This method is used by callers of ``Distribution.files()`` to |
| 399 | + locate files within the distribution. If it's possible for a |
| 400 | + Distribution to represent files in the distribution as |
| 401 | + ``SimplePath`` objects, it should implement this method |
| 402 | + to resolve such objects. |
| 403 | +
|
| 404 | + Some Distribution providers may elect not to resolve SimplePath |
| 405 | + objects within the distribution by raising a |
| 406 | + NotImplementedError, but consumers of such a Distribution would |
| 407 | + be unable to invoke ``Distribution.files()``. |
381 | 408 | """
|
382 | 409 |
|
383 | 410 | @classmethod
|
@@ -1136,11 +1163,10 @@ def _get_toplevel_name(name: PackagePath) -> str:
|
1136 | 1163 | >>> _get_toplevel_name(PackagePath('foo.dist-info'))
|
1137 | 1164 | 'foo.dist-info'
|
1138 | 1165 | """
|
1139 |
| - return _topmost(name) or ( |
1140 |
| - # python/typeshed#10328 |
1141 |
| - inspect.getmodulename(name) # type: ignore |
1142 |
| - or str(name) |
1143 |
| - ) |
| 1166 | + # Defer import of inspect for performance (python/cpython#118761) |
| 1167 | + import inspect |
| 1168 | + |
| 1169 | + return _topmost(name) or (inspect.getmodulename(name) or str(name)) |
1144 | 1170 |
|
1145 | 1171 |
|
1146 | 1172 | def _top_level_inferred(dist):
|
|
0 commit comments