2
2
3
3
package py
4
4
5
+ import (
6
+ "fmt"
7
+ )
8
+
5
9
// A python Exception object
6
10
type Exception struct {
7
- Name string // name of the exception FIXME should be part of class machinery
11
+ Base * Type
8
12
Args Object
9
13
Traceback Object
10
14
Context Object
@@ -14,27 +18,108 @@ type Exception struct {
14
18
}
15
19
16
20
var (
17
- // FIXME should be a class probably
18
- ExceptionType = NewType ("exception" , "Common base class for all exceptions" )
19
-
20
- // Some well known exceptions - these should be types?
21
- // FIXME exceptions should be created in builtins probably
22
- // their names should certainly go in there!
23
- NotImplemented = NewException ("NotImplemented" )
24
- StopIteration = NewException ("StopIteration" )
21
+ // Exception heirachy
22
+ BaseException = ObjectType .NewType ("BaseException" , "Common base class for all exceptions" , ExceptionNew , nil )
23
+ SystemExit = BaseException .NewType ("SystemExit" , "Request to exit from the interpreter." , nil , nil )
24
+ KeyboardInterrupt = BaseException .NewType ("KeyboardInterrupt" , "Program interrupted by user." , nil , nil )
25
+ GeneratorExit = BaseException .NewType ("GeneratorExit" , "Request that a generator exit." , nil , nil )
26
+ ExceptionType = BaseException .NewType ("Exception" , "Common base class for all non-exit exceptions." , nil , nil )
27
+ StopIteration = ExceptionType .NewType ("StopIteration" , "Signal the end from iterator.__next__()." , nil , nil )
28
+ ArithmeticError = ExceptionType .NewType ("ArithmeticError" , "Base class for arithmetic errors." , nil , nil )
29
+ FloatingPointError = ArithmeticError .NewType ("FloatingPointError" , "Floating point operation failed." , nil , nil )
30
+ OverflowError = ArithmeticError .NewType ("OverflowError" , "Result too large to be represented." , nil , nil )
31
+ ZeroDivisionError = ArithmeticError .NewType ("ZeroDivisionError" , "Second argument to a division or modulo operation was zero." , nil , nil )
32
+ AssertionError = ExceptionType .NewType ("AssertionError" , "Assertion failed." , nil , nil )
33
+ AttributeError = ExceptionType .NewType ("AttributeError" , "Attribute not found." , nil , nil )
34
+ BufferError = ExceptionType .NewType ("BufferError" , "Buffer error." , nil , nil )
35
+ EOFError = ExceptionType .NewType ("EOFError" , "Read beyond end of file." , nil , nil )
36
+ ImportError = ExceptionType .NewType ("ImportError" , "Import can't find module, or can't find name in module." , nil , nil )
37
+ LookupError = ExceptionType .NewType ("LookupError" , "Base class for lookup errors." , nil , nil )
38
+ IndexError = LookupError .NewType ("IndexError" , "Sequence index out of range." , nil , nil )
39
+ KeyError = LookupError .NewType ("KeyError" , "Mapping key not found." , nil , nil )
40
+ MemoryError = ExceptionType .NewType ("MemoryError" , "Out of memory." , nil , nil )
41
+ NameError = ExceptionType .NewType ("NameError" , "Name not found globally." , nil , nil )
42
+ UnboundLocalError = NameError .NewType ("UnboundLocalError" , "Local name referenced but not bound to a value." , nil , nil )
43
+ OSError = ExceptionType .NewType ("OSError" , "Base class for I/O related errors." , nil , nil )
44
+ BlockingIOError = OSError .NewType ("BlockingIOError" , "I/O operation would block." , nil , nil )
45
+ ChildProcessError = OSError .NewType ("ChildProcessError" , "Child process error." , nil , nil )
46
+ ConnectionError = OSError .NewType ("ConnectionError" , "Connection error." , nil , nil )
47
+ BrokenPipeError = ConnectionError .NewType ("BrokenPipeError" , "Broken pipe." , nil , nil )
48
+ ConnectionAbortedError = ConnectionError .NewType ("ConnectionAbortedError" , "Connection aborted." , nil , nil )
49
+ ConnectionRefusedError = ConnectionError .NewType ("ConnectionRefusedError" , "Connection refused." , nil , nil )
50
+ ConnectionResetError = ConnectionError .NewType ("ConnectionResetError" , "Connection reset." , nil , nil )
51
+ FileExistsError = OSError .NewType ("FileExistsError" , "File already exists." , nil , nil )
52
+ FileNotFoundError = OSError .NewType ("FileNotFoundError" , "File not found." , nil , nil )
53
+ InterruptedError = OSError .NewType ("InterruptedError" , "Interrupted by signal." , nil , nil )
54
+ IsADirectoryError = OSError .NewType ("IsADirectoryError" , "Operation doesn't work on directories." , nil , nil )
55
+ NotADirectoryError = OSError .NewType ("NotADirectoryError" , "Operation only works on directories." , nil , nil )
56
+ PermissionError = OSError .NewType ("PermissionError" , "Not enough permissions." , nil , nil )
57
+ ProcessLookupError = OSError .NewType ("ProcessLookupError" , "Process not found." , nil , nil )
58
+ TimeoutError = OSError .NewType ("TimeoutError" , "Timeout expired." , nil , nil )
59
+ ReferenceError = ExceptionType .NewType ("ReferenceError" , "Weak ref proxy used after referent went away." , nil , nil )
60
+ RuntimeError = ExceptionType .NewType ("RuntimeError" , "Unspecified run-time error." , nil , nil )
61
+ NotImplementedError = RuntimeError .NewType ("NotImplementedError" , "Method or function hasn't been implemented yet." , nil , nil )
62
+ SyntaxError = ExceptionType .NewType ("SyntaxError" , "Invalid syntax." , nil , nil )
63
+ IndentationError = SyntaxError .NewType ("IndentationError" , "Improper indentation." , nil , nil )
64
+ TabError = IndentationError .NewType ("TabError" , "Improper mixture of spaces and tabs." , nil , nil )
65
+ SystemError = ExceptionType .NewType ("SystemError" , "Internal error in the Python interpreter.\n \n Please report this to the Python maintainer, along with the traceback,\n the Python version, and the hardware/OS platform and version." , nil , nil )
66
+ TypeError = ExceptionType .NewType ("TypeError" , "Inappropriate argument type." , nil , nil )
67
+ ValueError = ExceptionType .NewType ("ValueError" , "Inappropriate argument value (of correct type)." , nil , nil )
68
+ UnicodeError = ValueError .NewType ("UnicodeError" , "Unicode related error." , nil , nil )
69
+ UnicodeDecodeError = UnicodeError .NewType ("UnicodeDecodeError" , "Unicode decoding error." , nil , nil )
70
+ UnicodeEncodeError = UnicodeError .NewType ("UnicodeEncodeError" , "Unicode encoding error." , nil , nil )
71
+ UnicodeTranslateError = UnicodeError .NewType ("UnicodeTranslateError" , "Unicode translation error." , nil , nil )
72
+ Warning = ExceptionType .NewType ("Warning" , "Base class for warning categories." , nil , nil )
73
+ DeprecationWarning = Warning .NewType ("DeprecationWarning" , "Base class for warnings about deprecated features." , nil , nil )
74
+ PendingDeprecationWarning = Warning .NewType ("PendingDeprecationWarning" , "Base class for warnings about features which will be deprecated\n in the future." , nil , nil )
75
+ RuntimeWarning = Warning .NewType ("RuntimeWarning" , "Base class for warnings about dubious runtime behavior." , nil , nil )
76
+ SyntaxWarning = Warning .NewType ("SyntaxWarning" , "Base class for warnings about dubious syntax." , nil , nil )
77
+ UserWarning = Warning .NewType ("UserWarning" , "Base class for warnings generated by user code." , nil , nil )
78
+ FutureWarning = Warning .NewType ("FutureWarning" , "Base class for warnings about constructs that will change semantically\n in the future." , nil , nil )
79
+ ImportWarning = Warning .NewType ("ImportWarning" , "Base class for warnings about probable mistakes in module imports" , nil , nil )
80
+ UnicodeWarning = Warning .NewType ("UnicodeWarning" , "Base class for warnings about Unicode related problems, mostly\n related to conversion problems." , nil , nil )
81
+ BytesWarning = Warning .NewType ("BytesWarning" , "Base class for warnings about bytes and buffer related problems, mostly\n related to conversion from str or comparing to str." , nil , nil )
82
+ ResourceWarning = Warning .NewType ("ResourceWarning" , "Base class for warnings about resource usage." , nil , nil )
83
+ // Singleton exceptions
84
+ NotImplemented = NotImplementedError .M__call__ (nil , nil )
25
85
)
26
86
27
87
// Type of this object
28
88
func (o * Exception ) Type () * Type {
29
- return ExceptionType
89
+ return o . Base
30
90
}
31
91
32
- // Define a new exception
33
- //
34
- // FIXME need inheritance machinery to make this work properly
35
- func NewException (name string ) * Exception {
36
- m := & Exception {
37
- Name : name ,
92
+ // RangeNew
93
+ func ExceptionNew (metatype * Type , args Tuple , kwargs StringDict ) Object {
94
+ if len (kwargs ) != 0 {
95
+ // TypeError
96
+ panic (fmt .Sprintf ("TypeError: %s does not take keyword arguments" , metatype .Name ))
97
+ }
98
+ return & Exception {
99
+ Base : metatype ,
100
+ Args : args .Copy (),
38
101
}
39
- return m
40
102
}
103
+
104
+ /*
105
+ #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
106
+
107
+ #define PyType_FastSubclass(t,f) PyType_HasFeature(t,f)
108
+
109
+ #define PyType_Check(op) \
110
+ PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
111
+
112
+ #define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type)
113
+
114
+ #define PyExceptionClass_Check(x) \
115
+ (PyType_Check((x)) && \
116
+ PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
117
+
118
+ #define PyExceptionInstance_Check(x) \
119
+ PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
120
+
121
+ #define PyExceptionClass_Name(x) \
122
+ ((char *)(((PyTypeObject*)(x))->tp_name))
123
+
124
+ #define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
125
+ */
0 commit comments