Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and asottile committed Oct 19, 2022
1 parent 4da71c7 commit 1b70fac
Show file tree
Hide file tree
Showing 4 changed files with 389 additions and 328 deletions.
56 changes: 33 additions & 23 deletions src/_pytest/_py/error.py
Expand Up @@ -2,41 +2,49 @@
create errno-specific classes for IO or os calls.
"""
import errno
import os
import sys
from types import ModuleType
import sys, os, errno


class Error(EnvironmentError):
def __repr__(self):
return "%s.%s %r: %s " %(self.__class__.__module__,
self.__class__.__name__,
self.__class__.__doc__,
" ".join(map(str, self.args)),
#repr(self.args)
)
return "{}.{} {!r}: {} ".format(
self.__class__.__module__,
self.__class__.__name__,
self.__class__.__doc__,
" ".join(map(str, self.args)),
# repr(self.args)
)

def __str__(self):
s = "[%s]: %s" %(self.__class__.__doc__,
" ".join(map(str, self.args)),
)
s = "[{}]: {}".format(
self.__class__.__doc__,
" ".join(map(str, self.args)),
)
return s


_winerrnomap = {
2: errno.ENOENT,
3: errno.ENOENT,
17: errno.EEXIST,
18: errno.EXDEV,
13: errno.EBUSY, # empty cd drive, but ENOMEDIUM seems unavailiable
13: errno.EBUSY, # empty cd drive, but ENOMEDIUM seems unavailiable
22: errno.ENOTDIR,
20: errno.ENOTDIR,
267: errno.ENOTDIR,
5: errno.EACCES, # anything better?
}


class ErrorMaker(ModuleType):
""" lazily provides Exception classes for each possible POSIX errno
(as defined per the 'errno' module). All such instances
subclass EnvironmentError.
"""lazily provides Exception classes for each possible POSIX errno
(as defined per the 'errno' module). All such instances
subclass EnvironmentError.
"""

Error = Error
_errno2class = {}

Expand All @@ -52,23 +60,25 @@ def _geterrnoclass(self, eno):
try:
return self._errno2class[eno]
except KeyError:
clsname = errno.errorcode.get(eno, "UnknownErrno%d" %(eno,))
errorcls = type(Error)(clsname, (Error,),
{'__module__':'py.error',
'__doc__': os.strerror(eno)})
clsname = errno.errorcode.get(eno, "UnknownErrno%d" % (eno,))
errorcls = type(Error)(
clsname,
(Error,),
{"__module__": "py.error", "__doc__": os.strerror(eno)},
)
self._errno2class[eno] = errorcls
return errorcls

def checked_call(self, func, *args, **kwargs):
""" call a function and raise an errno-exception if applicable. """
"""call a function and raise an errno-exception if applicable."""
__tracebackhide__ = True
try:
return func(*args, **kwargs)
except self.Error:
raise
except (OSError, EnvironmentError):
except OSError:
cls, value, tb = sys.exc_info()
if not hasattr(value, 'errno'):
if not hasattr(value, "errno"):
raise
__tracebackhide__ = False
errno = value.errno
Expand All @@ -83,9 +93,9 @@ def checked_call(self, func, *args, **kwargs):
cls = self._geterrnoclass(_winerrnomap[errno])
except KeyError:
raise value
raise cls("%s%r" % (func.__name__, args))
raise cls(f"{func.__name__}{args!r}")
__tracebackhide__ = True


error = ErrorMaker('_pytest._py.error')
error = ErrorMaker("_pytest._py.error")
sys.modules[error.__name__] = error

0 comments on commit 1b70fac

Please sign in to comment.