27
27
import time
28
28
import re
29
29
import types
30
- from typing import Callable , Dict , Iterable , List , Protocol , Optional , TypeVar
30
+ from typing import (
31
+ TYPE_CHECKING ,
32
+ List ,
33
+ Protocol ,
34
+ Callable ,
35
+ Dict ,
36
+ Iterable ,
37
+ Optional ,
38
+ TypeVar ,
39
+ )
31
40
import zipfile
32
41
import zipimport
33
42
import warnings
76
85
from pkg_resources .extern .packaging import version as _packaging_version
77
86
from pkg_resources .extern .platformdirs import user_cache_dir as _user_cache_dir
78
87
79
- # declare some globals that will be defined later to
80
- # satisfy the linters.
81
- require = None
82
- working_set = None
83
- add_activation_listener = None
84
- cleanup_resources = None
85
- resource_stream = None
86
- set_extraction_path = None
87
- resource_isdir = None
88
- resource_string = None
89
- iter_entry_points = None
90
- resource_listdir = None
91
- resource_filename = None
92
- resource_exists = None
93
-
94
88
95
89
warnings .warn (
96
90
"pkg_resources is deprecated as an API. "
@@ -1610,14 +1604,15 @@ def _validate_resource_path(path):
1610
1604
os .path .pardir in path .split (posixpath .sep )
1611
1605
or posixpath .isabs (path )
1612
1606
or ntpath .isabs (path )
1607
+ or path .startswith ("\\ " )
1613
1608
)
1614
1609
if not invalid :
1615
1610
return
1616
1611
1617
1612
msg = "Use of .. or absolute path in a resource path is not allowed."
1618
1613
1619
1614
# Aggressively disallow Windows absolute paths
1620
- if ntpath .isabs (path ) and not posixpath .isabs (path ):
1615
+ if ( path . startswith ( " \\ " ) or ntpath .isabs (path ) ) and not posixpath .isabs (path ):
1621
1616
raise ValueError (msg )
1622
1617
1623
1618
# for compatibility, warn; in future
@@ -3257,6 +3252,15 @@ def _mkstemp(*args, **kw):
3257
3252
warnings .filterwarnings ("ignore" , category = PEP440Warning , append = True )
3258
3253
3259
3254
3255
+ class PkgResourcesDeprecationWarning (Warning ):
3256
+ """
3257
+ Base class for warning about deprecations in ``pkg_resources``
3258
+
3259
+ This class is not derived from ``DeprecationWarning``, and as such is
3260
+ visible by default.
3261
+ """
3262
+
3263
+
3260
3264
# from jaraco.functools 1.3
3261
3265
def _call_aside (f , * args , ** kwargs ):
3262
3266
f (* args , ** kwargs )
@@ -3275,15 +3279,6 @@ def _initialize(g=globals()):
3275
3279
)
3276
3280
3277
3281
3278
- class PkgResourcesDeprecationWarning (Warning ):
3279
- """
3280
- Base class for warning about deprecations in ``pkg_resources``
3281
-
3282
- This class is not derived from ``DeprecationWarning``, and as such is
3283
- visible by default.
3284
- """
3285
-
3286
-
3287
3282
@_call_aside
3288
3283
def _initialize_master_working_set ():
3289
3284
"""
@@ -3320,6 +3315,26 @@ def _initialize_master_working_set():
3320
3315
globals ().update (locals ())
3321
3316
3322
3317
3318
+ if TYPE_CHECKING :
3319
+ # All of these are set by the @_call_aside methods above
3320
+ __resource_manager = ResourceManager () # Won't exist at runtime
3321
+ resource_exists = __resource_manager .resource_exists
3322
+ resource_isdir = __resource_manager .resource_isdir
3323
+ resource_filename = __resource_manager .resource_filename
3324
+ resource_stream = __resource_manager .resource_stream
3325
+ resource_string = __resource_manager .resource_string
3326
+ resource_listdir = __resource_manager .resource_listdir
3327
+ set_extraction_path = __resource_manager .set_extraction_path
3328
+ cleanup_resources = __resource_manager .cleanup_resources
3329
+
3330
+ working_set = WorkingSet ()
3331
+ require = working_set .require
3332
+ iter_entry_points = working_set .iter_entry_points
3333
+ add_activation_listener = working_set .subscribe
3334
+ run_script = working_set .run_script
3335
+ run_main = run_script
3336
+
3337
+
3323
3338
# ---- Ported from ``setuptools`` to avoid introducing an import inter-dependency ----
3324
3339
LOCALE_ENCODING = "locale" if sys .version_info >= (3 , 10 ) else None
3325
3340
0 commit comments