22create errno-specific classes for IO or os calls.
33
44"""
5+ import errno
6+ import os
7+ import sys
58from types import ModuleType
6- import sys , os , errno
9+
710
811class Error (EnvironmentError ):
912 def __repr__ (self ):
10- return "%s.%s %r: %s " % (self .__class__ .__module__ ,
11- self .__class__ .__name__ ,
12- self .__class__ .__doc__ ,
13- " " .join (map (str , self .args )),
14- #repr(self.args)
15- )
13+ return "{}.{} {!r}: {} " .format (
14+ self .__class__ .__module__ ,
15+ self .__class__ .__name__ ,
16+ self .__class__ .__doc__ ,
17+ " " .join (map (str , self .args )),
18+ # repr(self.args)
19+ )
1620
1721 def __str__ (self ):
18- s = "[%s]: %s" % (self .__class__ .__doc__ ,
19- " " .join (map (str , self .args )),
20- )
22+ s = "[{}]: {}" .format (
23+ self .__class__ .__doc__ ,
24+ " " .join (map (str , self .args )),
25+ )
2126 return s
2227
28+
2329_winerrnomap = {
2430 2 : errno .ENOENT ,
2531 3 : errno .ENOENT ,
2632 17 : errno .EEXIST ,
2733 18 : errno .EXDEV ,
28- 13 : errno .EBUSY , # empty cd drive, but ENOMEDIUM seems unavailiable
34+ 13 : errno .EBUSY , # empty cd drive, but ENOMEDIUM seems unavailiable
2935 22 : errno .ENOTDIR ,
3036 20 : errno .ENOTDIR ,
3137 267 : errno .ENOTDIR ,
3238 5 : errno .EACCES , # anything better?
3339}
3440
41+
3542class ErrorMaker (ModuleType ):
36- """ lazily provides Exception classes for each possible POSIX errno
37- (as defined per the 'errno' module). All such instances
38- subclass EnvironmentError.
43+ """lazily provides Exception classes for each possible POSIX errno
44+ (as defined per the 'errno' module). All such instances
45+ subclass EnvironmentError.
3946 """
47+
4048 Error = Error
4149 _errno2class = {}
4250
@@ -52,23 +60,25 @@ def _geterrnoclass(self, eno):
5260 try :
5361 return self ._errno2class [eno ]
5462 except KeyError :
55- clsname = errno .errorcode .get (eno , "UnknownErrno%d" % (eno ,))
56- errorcls = type (Error )(clsname , (Error ,),
57- {'__module__' :'py.error' ,
58- '__doc__' : os .strerror (eno )})
63+ clsname = errno .errorcode .get (eno , "UnknownErrno%d" % (eno ,))
64+ errorcls = type (Error )(
65+ clsname ,
66+ (Error ,),
67+ {"__module__" : "py.error" , "__doc__" : os .strerror (eno )},
68+ )
5969 self ._errno2class [eno ] = errorcls
6070 return errorcls
6171
6272 def checked_call (self , func , * args , ** kwargs ):
63- """ call a function and raise an errno-exception if applicable. """
73+ """call a function and raise an errno-exception if applicable."""
6474 __tracebackhide__ = True
6575 try :
6676 return func (* args , ** kwargs )
6777 except self .Error :
6878 raise
69- except ( OSError , EnvironmentError ) :
79+ except OSError :
7080 cls , value , tb = sys .exc_info ()
71- if not hasattr (value , ' errno' ):
81+ if not hasattr (value , " errno" ):
7282 raise
7383 __tracebackhide__ = False
7484 errno = value .errno
@@ -83,9 +93,9 @@ def checked_call(self, func, *args, **kwargs):
8393 cls = self ._geterrnoclass (_winerrnomap [errno ])
8494 except KeyError :
8595 raise value
86- raise cls ("%s%r" % ( func .__name__ , args ) )
96+ raise cls (f" { func .__name__ } { args !r } " )
8797 __tracebackhide__ = True
8898
8999
90- error = ErrorMaker (' _pytest._py.error' )
100+ error = ErrorMaker (" _pytest._py.error" )
91101sys .modules [error .__name__ ] = error
0 commit comments