87dd3c50de310f94ca3eeaf42b199dddf2677f36
[software/python-on-guile.git] / modules / language / python / exceptions.scm
1 (define-module (language python exceptions)
2 #:use-module (oop pf-objects)
3 #:use-module (oop goops)
4 #:export (StopIteration GeneratorExit RuntimeError TabError
5 Exception ValueError TypeError
6 IndexError KeyError AttributeError ArgumentError
7 SyntaxError SystemException
8 OSError ProcessLookupError PermissionError
9 None NotImplemented NotImplementedError
10 AssertionError ImportError
11 ModuleNotFoundError BlockingIOError
12 InterruptedError BaseException
13 ZeroDivisionError
14 OverflowError RecursionError
15 Warning DeprecationWarning BytesWarning
16 ResourceWarning UserWarning UnicodeTranslateError
17 UnicodeDecodeError LookupError IndentationError
18 KeyboardInterrupt MemoryError NameError
19 EOFError UnicodeError UnicodeEncodeError
20 FileExistsError FileNotFoundError IsADirectoryError
21 EnvironmentError ConnectionError NotADirectoryError
22 ConnectionResetError ChildProcessError TimeOutError
23 BrokenPipeError ConnectionAbortedError
24 ConnectionRefusedError ArithmeticError))
25
26 (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
27
28 (define-python-class Exception ()
29 (define __init__
30 (case-lambda
31 ((self)
32 (values))
33 ((self val . l)
34 (set self 'value val))))
35
36 (define __repr__
37 (lambda (self)
38 (aif it (rawref self 'value #f)
39 (format #f "~a:~a"
40 (rawref self '__name__) it)
41 (format #f "~a"
42 (rawref self '__name__))))))
43
44 (define-python-class Warning ()
45 (define __init__
46 (case-lambda
47 ((self)
48 (values))
49 ((self val . l)
50 (set self 'value val))))
51
52 (define __repr__
53 (lambda (self)
54 (aif it (rawref self 'value #f)
55 (format #f "~a:~a"
56 (rawref self '__name__) it)
57 (format #f "~a"
58 (rawref self '__name__))))))
59
60 (define-syntax define-er
61 (syntax-rules ()
62 ((_ nm k)
63 (define-python-class nm (Exception)))
64 ((_ nm w k)
65 (define-python-class nm w))))
66
67 (define-syntax define-er2
68 (syntax-rules ()
69 ((_ nm k)
70 (define-python-class nm (BaseException)))
71 ((_ nm w k)
72 (define-python-class nm w))))
73
74 (define StopIteration 'StopIteration)
75 (define GeneratorExit 'GeneratorExit)
76 (define-er EnvironmentError 'EnvironmentError)
77
78 (define-er EOFError 'EOFError)
79 (define-er MemoryError 'MemoryError)
80 (define-er NameError 'NameError)
81
82 (define-er ValueError 'ValueError)
83 (define-python-class UnicodeError (ValueError))
84 (define-python-class UnicodeDecodeError (UnicodeError))
85 (define-python-class UnicodeEncodeError (UnicodeError))
86 (define-python-class UnicodeTranslateError (UnicodeError))
87
88 (define-er LookupError 'LookupError)
89 (define-python-class IndexError (LookupError))
90 (define-python-class KeyError (LookupError))
91
92 (define-er ArithmeticError 'OverflowError)
93 (define-python-class OverflowError (ArithmeticError))
94 (define-python-class ZeroDivisionError (ArithmeticError))
95
96
97 (define-er KeyboardInterrupt 'KeyboardInterrupt)
98 (define-er BaseException 'BaseException)
99 (define-er SystemException 'SystemException)
100 (define-er RuntimeError 'RuntimeError)
101 (define-python-class NotImplementedError (RuntimeError))
102 (define-python-class RecursionError (RuntimeError))
103
104
105 (define-er ArgumentError 'IndexError)
106
107
108 (define-er OSError 'OSError)
109 (define-python-class BlockingIOError (OSError))
110 (define-python-class ChildProcessError (OSError))
111 (define-python-class ConnectionError (OSError))
112 (define-python-class BrokenPipeError (ConnectionError))
113 (define-python-class ConnectionAbortedError (ConnectionError))
114 (define-python-class ConnectionRefusedError (ConnectionError))
115 (define-python-class ConnectionResetError (ConnectionError))
116 (define-python-class FileExistsError (OSError))
117 (define-python-class FileNotFoundError (OSError))
118 (define-python-class InterruptedError (OSError))
119 (define-python-class IsADirectoryError (OSError))
120 (define-python-class NotADirectoryError (OSError))
121 (define-python-class PermissionError (OSError))
122 (define-python-class ProcessLookupError (OSError))
123 (define-python-class TimeOutError (OSError))
124 (define None 'None)
125
126
127 (define-er TypeError 'TypeError)
128 (define-er AttributeError 'AttributeError)
129 (define-er SyntaxError 'SyntaxError)
130 (define-python-class IndentationError (SyntaxError))
131 (define-python-class TabError (IndentationError))
132
133 (define-er RunTimeError 'RunTimeError)
134
135 (define AssertionError 'AssertionError)
136 (define-er ImportError 'ImportError)
137 (define-er ModuleNotFoundError (ImportError) 'ModuleNotFoundError)
138
139 (define NotImplemented (list 'NotImplemented))
140
141
142
143
144 (define-syntax define-wr
145 (syntax-rules ()
146 ((_ nm k)
147 (define-python-class nm (Warning)))
148 ((_ nm w k)
149 (define-python-class nm w))))
150
151 (define-wr BytesWarning 'BytesWarning)
152 (define-wr DepricationWarning 'DeprecationWarning)
153 (define-wr ResourceWarning 'ResourceWarning)
154 (define-wr UserWarning 'UserWarning)