1
- """
2
- create errno-specific classes for IO or os calls.
1
+ """create errno-specific classes for IO or os calls."""
2
+ from __future__ import annotations
3
3
4
- """
5
4
import errno
6
5
import os
7
6
import sys
7
+ from typing import Callable
8
+ from typing import TYPE_CHECKING
9
+ from typing import TypeVar
10
+
11
+ if TYPE_CHECKING :
12
+ from typing_extensions import ParamSpec
13
+
14
+ P = ParamSpec ("P" )
15
+
16
+ R = TypeVar ("R" )
8
17
9
18
10
19
class Error (EnvironmentError ):
11
- def __repr__ (self ):
20
+ def __repr__ (self ) -> str :
12
21
return "{}.{} {!r}: {} " .format (
13
22
self .__class__ .__module__ ,
14
23
self .__class__ .__name__ ,
@@ -17,7 +26,7 @@ def __repr__(self):
17
26
# repr(self.args)
18
27
)
19
28
20
- def __str__ (self ):
29
+ def __str__ (self ) -> str :
21
30
s = "[{}]: {}" .format (
22
31
self .__class__ .__doc__ ,
23
32
" " .join (map (str , self .args )),
@@ -44,60 +53,57 @@ class ErrorMaker:
44
53
subclass EnvironmentError.
45
54
"""
46
55
47
- Error = Error
48
- _errno2class = {}
56
+ _errno2class : dict [int , type [Error ]] = {}
49
57
50
- def __getattr__ (self , name ) :
58
+ def __getattr__ (self , name : str ) -> type [ Error ] :
51
59
if name [0 ] == "_" :
52
60
raise AttributeError (name )
53
61
eno = getattr (errno , name )
54
62
cls = self ._geterrnoclass (eno )
55
63
setattr (self , name , cls )
56
64
return cls
57
65
58
- def _geterrnoclass (self , eno ) :
66
+ def _geterrnoclass (self , eno : int ) -> type [ Error ] :
59
67
try :
60
68
return self ._errno2class [eno ]
61
69
except KeyError :
62
70
clsname = errno .errorcode .get (eno , "UnknownErrno%d" % (eno ,))
63
- errorcls = type (Error )(
71
+ errorcls = type (
64
72
clsname ,
65
73
(Error ,),
66
74
{"__module__" : "py.error" , "__doc__" : os .strerror (eno )},
67
75
)
68
76
self ._errno2class [eno ] = errorcls
69
77
return errorcls
70
78
71
- def checked_call (self , func , * args , ** kwargs ):
72
- """call a function and raise an errno-exception if applicable."""
79
+ def checked_call (
80
+ self , func : Callable [P , R ], * args : P .args , ** kwargs : P .kwargs
81
+ ) -> R :
82
+ """Call a function and raise an errno-exception if applicable."""
73
83
__tracebackhide__ = True
74
84
try :
75
85
return func (* args , ** kwargs )
76
86
except self .Error :
77
87
raise
78
- except OSError :
79
- cls , value , tb = sys .exc_info ()
88
+ except OSError as value :
80
89
if not hasattr (value , "errno" ):
81
90
raise
82
- __tracebackhide__ = False
83
91
errno = value .errno
84
- try :
85
- if not isinstance (value , WindowsError ):
86
- raise NameError
87
- except NameError :
88
- # we are not on Windows, or we got a proper OSError
89
- cls = self ._geterrnoclass (errno )
90
- else :
92
+ if sys .platform == "win32" :
91
93
try :
92
94
cls = self ._geterrnoclass (_winerrnomap [errno ])
93
95
except KeyError :
94
96
raise value
97
+ else :
98
+ # we are not on Windows, or we got a proper OSError
99
+ cls = self ._geterrnoclass (errno )
100
+
95
101
raise cls (f"{ func .__name__ } { args !r} " )
96
- __tracebackhide__ = True
97
102
98
103
99
104
_error_maker = ErrorMaker ()
105
+ checked_call = _error_maker .checked_call
100
106
101
107
102
- def __getattr__ (attr ) :
103
- return getattr (_error_maker , attr )
108
+ def __getattr__ (attr : str ) -> type [ Error ] :
109
+ return getattr (_error_maker , attr ) # type: ignore[no-any-return]
0 commit comments