File: error.py

package info (click to toggle)
codespeak-lib 0.9.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,212 kB
  • ctags: 5,409
  • sloc: python: 33,390; ansic: 961; xml: 582; makefile: 90
file content (79 lines) | stat: -rw-r--r-- 2,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

import py
import errno

class Error(EnvironmentError):
    __module__ = 'py.error'

    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)
                                )

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

_winerrnomap = {
    2: errno.ENOENT, 
    3: errno.ENOENT, 
    17: errno.EEXIST,
    22: errno.ENOTDIR,
    267: errno.ENOTDIR,
    5: errno.EACCES,  # anything better?
}
# note: 'py.std' may not be imported yet at all, because 
# the 'error' module in this file is imported very early.
# This is dependent on dict order.

ModuleType = type(py)

class py_error(ModuleType):
    """ py.error lazily provides higher level Exception classes
        for each possible POSIX errno (as defined per
        the 'errno' module.  All such Exceptions derive
        from py.error.Error, which itself is a subclass
        of EnvironmentError.
    """
    Error = Error

    def _getwinerrnoclass(cls, eno): 
        return cls._geterrnoclass(_winerrnomap[eno]) 
    _getwinerrnoclass = classmethod(_getwinerrnoclass) 

    def _geterrnoclass(eno, _errno2class = {}):
        try:
            return _errno2class[eno]
        except KeyError:
            clsname = py.std.errno.errorcode.get(eno, "UnknownErrno%d" %(eno,))
            cls = type(Error)(clsname, (Error,),
                    {'__module__':'py.error',
                     '__doc__': py.std.os.strerror(eno)})
            _errno2class[eno] = cls
            return cls
    _geterrnoclass = staticmethod(_geterrnoclass)

    def __getattr__(self, name):
        eno = getattr(py.std.errno, name)
        cls = self._geterrnoclass(eno)
        setattr(self, name, cls)
        return cls

    def getdict(self, done=[]):
        try:
            return done[0]
        except IndexError:
            for name in py.std.errno.errorcode.values():
                hasattr(self, name)   # force attribute to be loaded, ignore errors
            dictdescr = ModuleType.__dict__['__dict__']
            done.append(dictdescr.__get__(self))
            return done[0]

    __dict__ = property(getdict)
    del getdict

error = py_error('py.error', py_error.__doc__)