~ibmcharmers/charms/xenial/ibm-cinder-storwize-svc/trunk

« back to all changes in this revision

Viewing changes to .tox/py35/lib/python3.5/site-packages/py/_error.py

  • Committer: Ankammarao
  • Date: 2017-03-06 05:11:42 UTC
  • Revision ID: achittet@in.ibm.com-20170306051142-dpg27z4es1k56hfn
Marked tests folder executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
create errno-specific classes for IO or os calls.
 
3
 
 
4
"""
 
5
import sys, os, errno
 
6
 
 
7
class Error(EnvironmentError):
 
8
    def __repr__(self):
 
9
        return "%s.%s %r: %s " %(self.__class__.__module__,
 
10
                               self.__class__.__name__,
 
11
                               self.__class__.__doc__,
 
12
                               " ".join(map(str, self.args)),
 
13
                               #repr(self.args)
 
14
                                )
 
15
 
 
16
    def __str__(self):
 
17
        s = "[%s]: %s" %(self.__class__.__doc__,
 
18
                          " ".join(map(str, self.args)),
 
19
                          )
 
20
        return s
 
21
 
 
22
_winerrnomap = {
 
23
    2: errno.ENOENT,
 
24
    3: errno.ENOENT,
 
25
    17: errno.EEXIST,
 
26
    18: errno.EXDEV,
 
27
    13: errno.EBUSY, # empty cd drive, but ENOMEDIUM seems unavailiable
 
28
    22: errno.ENOTDIR,
 
29
    20: errno.ENOTDIR,
 
30
    267: errno.ENOTDIR,
 
31
    5: errno.EACCES,  # anything better?
 
32
}
 
33
 
 
34
class ErrorMaker(object):
 
35
    """ lazily provides Exception classes for each possible POSIX errno
 
36
        (as defined per the 'errno' module).  All such instances
 
37
        subclass EnvironmentError.
 
38
    """
 
39
    Error = Error
 
40
    _errno2class = {}
 
41
 
 
42
    def __getattr__(self, name):
 
43
        if name[0] == "_":
 
44
            raise AttributeError(name)
 
45
        eno = getattr(errno, name)
 
46
        cls = self._geterrnoclass(eno)
 
47
        setattr(self, name, cls)
 
48
        return cls
 
49
 
 
50
    def _geterrnoclass(self, eno):
 
51
        try:
 
52
            return self._errno2class[eno]
 
53
        except KeyError:
 
54
            clsname = errno.errorcode.get(eno, "UnknownErrno%d" %(eno,))
 
55
            errorcls = type(Error)(clsname, (Error,),
 
56
                    {'__module__':'py.error',
 
57
                     '__doc__': os.strerror(eno)})
 
58
            self._errno2class[eno] = errorcls
 
59
            return errorcls
 
60
 
 
61
    def checked_call(self, func, *args, **kwargs):
 
62
        """ call a function and raise an errno-exception if applicable. """
 
63
        __tracebackhide__ = True
 
64
        try:
 
65
            return func(*args, **kwargs)
 
66
        except self.Error:
 
67
            raise
 
68
        except (OSError, EnvironmentError):
 
69
            cls, value, tb = sys.exc_info()
 
70
            if not hasattr(value, 'errno'):
 
71
                raise
 
72
            __tracebackhide__ = False
 
73
            errno = value.errno
 
74
            try:
 
75
                if not isinstance(value, WindowsError):
 
76
                    raise NameError
 
77
            except NameError:
 
78
                # we are not on Windows, or we got a proper OSError
 
79
                cls = self._geterrnoclass(errno)
 
80
            else:
 
81
                try:
 
82
                    cls = self._geterrnoclass(_winerrnomap[errno])
 
83
                except KeyError:
 
84
                    raise value
 
85
            raise cls("%s%r" % (func.__name__, args))
 
86
            __tracebackhide__ = True
 
87
            
 
88
 
 
89
error = ErrorMaker()