cython

Форк
0
/
tryexcept.pyx 
520 строк · 10.8 Кб
1
def single_except(a, x):
2
    """
3
    >>> single_except(ValueError, None)
4
    2
5
    >>> single_except(ValueError, ValueError('test'))
6
    3
7
    >>> single_except(ValueError, TypeError('test'))
8
    Traceback (most recent call last):
9
    TypeError: test
10
    """
11
    cdef int i
12
    try:
13
        i = 1
14
        if x:
15
            raise x
16
        i = 2
17
    except a:
18
        i = 3
19
    return i
20

21
def single_except_builtin(a, x):
22
    """
23
    >>> single_except_builtin(ValueError, None)
24
    2
25
    >>> single_except_builtin(ValueError, ValueError('test'))
26
    3
27
    >>> single_except_builtin(ValueError, TypeError('test'))
28
    Traceback (most recent call last):
29
    TypeError: test
30
    """
31
    cdef int i
32
    try:
33
        i = 1
34
        if x:
35
            raise x
36
        i = 2
37
    except ValueError:
38
        i = 3
39
    return i
40

41
def single_except_expression(a, x):
42
    """
43
    >>> single_except_expression([[ValueError]], None)
44
    2
45
    >>> single_except_expression([[ValueError]], ValueError('test'))
46
    3
47
    >>> single_except_expression([[ValueError]], TypeError('test'))
48
    Traceback (most recent call last):
49
    TypeError: test
50
    """
51
    cdef int i
52
    try:
53
        i = 1
54
        if x:
55
            raise x
56
        i = 2
57
    except a[0][0]:
58
        i = 3
59
    return i
60

61

62
exceptions = (ValueError, TypeError)
63

64

65
def single_except_global_tuple(x):
66
    """
67
    >>> single_except_global_tuple(None)
68
    2
69
    >>> single_except_global_tuple(ValueError('test'))
70
    3
71
    >>> single_except_global_tuple(TypeError('test'))
72
    3
73
    >>> class TypeErrorSubtype(TypeError): pass
74
    >>> single_except_global_tuple(TypeErrorSubtype('test'))
75
    3
76
    >>> single_except_global_tuple(AttributeError('test'))
77
    Traceback (most recent call last):
78
    AttributeError: test
79
    """
80
    cdef int i
81
    try:
82
        i = 1
83
        if x:
84
            raise x
85
        i = 2
86
    except exceptions:
87
        i = 3
88
    return i
89

90

91
def double_except_no_raise(a,b):
92
    """
93
    >>> double_except_no_raise(TypeError, ValueError)
94
    1
95
    """
96
    d = a or b  # mark used
97

98
    cdef int i
99
    try:
100
        i = 1
101
    except a:
102
        i = 2
103
    except b:
104
        i = 3
105
    return i
106

107
def double_except_raise(x, a, b):
108
    """
109
    >>> double_except_raise(None, TypeError, ValueError)
110
    1
111
    >>> double_except_raise(TypeError('test'), TypeError, ValueError)
112
    2
113
    >>> double_except_raise(ValueError('test'), TypeError, ValueError)
114
    3
115
    >>> double_except_raise(None, TypeError, ValueError)
116
    1
117
    """
118
    cdef int i
119
    try:
120
        i = 1
121
        if x:
122
            raise x
123
    except a:
124
        i = 2
125
    except b:
126
        i = 3
127
    return i
128

129
def target_except_no_raise(a):
130
    """
131
    >>> target_except_no_raise(TypeError)
132
    1
133
    """
134
    d = a  # mark used
135

136
    cdef int i
137
    try:
138
        i = 1
139
    except a, b:
140
        i = 2
141
    return i
142

143
def target_except_raise(x, a):
144
    """
145
    >>> target_except_raise(None, TypeError)
146
    1
147
    >>> target_except_raise(TypeError('test'), TypeError)
148
    2
149
    >>> target_except_raise(ValueError('test'), TypeError)
150
    Traceback (most recent call last):
151
    ValueError: test
152
    >>> target_except_raise(None, TypeError)
153
    1
154
    """
155
    cdef int i
156
    try:
157
        i = 1
158
        if x:
159
            raise x
160
    except a, b:
161
        i = 2
162
        assert isinstance(b, a)
163
    return i
164

165
def tuple_except_builtin(x):
166
    """
167
    >>> tuple_except_builtin(None)
168
    1
169
    >>> tuple_except_builtin(TypeError('test'))
170
    2
171
    >>> tuple_except_builtin(ValueError('test'))
172
    2
173
    >>> tuple_except_builtin(IndexError('5'))
174
    Traceback (most recent call last):
175
    IndexError: 5
176
    >>> tuple_except_builtin(None)
177
    1
178
    """
179
    cdef int i
180
    try:
181
        i = 1
182
        if x:
183
            raise x
184
    except (TypeError, ValueError):
185
        i = 2
186
    return i
187

188
def normal_and_bare_except_no_raise(a):
189
    """
190
    >>> normal_and_bare_except_no_raise(TypeError)
191
    1
192
    """
193
    d = a  # mark used
194

195
    cdef int i
196
    try:
197
        i = 1
198
    except a:
199
        i = 2
200
    except:
201
        i = 3
202
    return i
203

204
def normal_and_bare_except_raise(x, a):
205
    """
206
    >>> normal_and_bare_except_raise(None, TypeError)
207
    1
208
    >>> normal_and_bare_except_raise(TypeError('test'), TypeError)
209
    2
210
    >>> normal_and_bare_except_raise(ValueError('test'), TypeError)
211
    3
212
    >>> normal_and_bare_except_raise(TypeError('test'), (TypeError, ValueError))
213
    2
214
    >>> normal_and_bare_except_raise(ValueError('test'), (TypeError, ValueError))
215
    2
216
    >>> normal_and_bare_except_raise(None, TypeError)
217
    1
218
    """
219
    cdef int i
220
    try:
221
        i = 1
222
        if x:
223
            raise x
224
    except a:
225
        i = 2
226
    except:
227
        i = 3
228
    return i
229

230
def tuple_except_index_target_no_raise(a, b, c):
231
    """
232
    >>> l = [None, None]
233
    >>> tuple_except_index_target_no_raise(TypeError, ValueError, l)
234
    1
235
    >>> l
236
    [None, None]
237
    """
238
    d = a or b or c  # mark used
239

240
    cdef int i
241
    try:
242
        i = 1
243
    except (a, b), c[1]:
244
        i = 2
245
    return i
246

247
def tuple_except_index_target_raise(x, a, b, c):
248
    """
249
    >>> l = [None, None]
250
    >>> tuple_except_index_target_raise(None, TypeError, ValueError, l)
251
    1
252
    >>> l
253
    [None, None]
254
    >>> tuple_except_index_target_raise(TypeError('test'), TypeError, ValueError, l)
255
    2
256
    >>> l[0] is None, isinstance(l[1], TypeError)
257
    (True, True)
258
    >>> tuple_except_index_target_raise(ValueError('test'), TypeError, ValueError, l)
259
    2
260
    >>> l[0] is None, isinstance(l[1], ValueError)
261
    (True, True)
262
    >>> tuple_except_index_target_raise(IndexError('5'), TypeError, ValueError, l)
263
    Traceback (most recent call last):
264
    IndexError: 5
265
    >>> tuple_except_index_target_raise(None, TypeError, ValueError, l)
266
    1
267
    >>> l[0] is None, isinstance(l[1], ValueError)
268
    (True, True)
269
    """
270
    cdef int i
271
    try:
272
        i = 1
273
        if x:
274
            raise x
275
    except (a, b), c[1]:
276
        i = 2
277
        assert isinstance(c[1], (a,b))
278
    return i
279

280
def loop_bare_except_no_raise(a, b, int c):
281
    """
282
    >>> loop_bare_except_no_raise(TypeError, range(2), 2)
283
    (1, 3528)
284
    """
285
    cdef int i = 1
286
    for a in b:
287
        try:
288
            c = c * 42
289
        except:
290
            i = 17
291
    return i,c
292

293
def loop_bare_except_raise(a, b, int c):
294
    """
295
    >>> loop_bare_except_raise(TypeError, range(2), 2)
296
    (1, 3528)
297
    >>> loop_bare_except_raise(TypeError, range(3), 2)
298
    (17, 148176)
299
    >>> loop_bare_except_raise(TypeError, range(4), 2)
300
    (17, 6223392)
301
    """
302
    cdef int i = 1
303
    for a in b:
304
        try:
305
            c = c * 42
306
            if a == 2:
307
                raise TypeError('test')
308
        except:
309
            i = 17
310
    return i,c
311

312
def bare_except_reraise_no_raise(l):
313
    """
314
    >>> l = [None]
315
    >>> bare_except_reraise_no_raise(l)
316
    1
317
    >>> l
318
    [None]
319
    """
320
    d = l  # mark used
321

322
    cdef int i
323
    try:
324
        i = 1
325
    except:
326
        l[0] = 2
327
        raise
328
    return i
329

330
def bare_except_reraise_raise(x, l):
331
    """
332
    >>> l = [None]
333
    >>> bare_except_reraise_raise(None, l)
334
    1
335
    >>> l
336
    [None]
337
    >>> bare_except_reraise_raise(TypeError('test'), l)
338
    Traceback (most recent call last):
339
    TypeError: test
340
    >>> l
341
    [2]
342
    >>> l = [None]
343
    >>> bare_except_reraise_raise(None, l)
344
    1
345
    >>> l
346
    [None]
347
    """
348
    cdef int i
349
    try:
350
        i = 1
351
        if x:
352
            raise x
353
    except:
354
        l[0] = 2
355
        raise
356
    return i
357

358
def except_as_no_raise(a):
359
    """
360
    >>> except_as_no_raise(TypeError)
361
    1
362
    """
363
    d = a  # mark used
364

365
    try:
366
        i = 1
367
    except a as b:
368
        i = 2
369
    return i
370

371
def except_as_raise(x, a):
372
    """
373
    >>> except_as_raise(None, TypeError)
374
    1
375
    >>> except_as_raise(TypeError('test'), TypeError)
376
    2
377
    >>> except_as_raise(ValueError('test'), TypeError)
378
    Traceback (most recent call last):
379
    ValueError: test
380
    >>> except_as_raise(None, TypeError)
381
    1
382
    """
383
    try:
384
        i = 1
385
        if x:
386
            raise x
387
    except a as b:
388
        i = 2
389
        assert isinstance(b, a)
390
    return i
391

392
def except_as_no_raise_does_not_touch_target(a):
393
    """
394
    >>> i,b = except_as_no_raise_does_not_touch_target(TypeError)
395
    >>> i
396
    1
397
    >>> b
398
    1
399
    """
400
    d = a  # mark used
401

402
    b = 1
403
    try:
404
        i = 1
405
    except a as b:
406
        i = 2
407
    return i, b
408

409
def except_as_raise_does_not_delete_target(x, a):
410
    """
411
    >>> except_as_raise_does_not_delete_target(None, TypeError)
412
    1
413
    >>> except_as_raise_does_not_delete_target(TypeError('test'), TypeError)
414
    2
415
    >>> except_as_raise_does_not_delete_target(ValueError('test'), TypeError)
416
    Traceback (most recent call last):
417
    ValueError: test
418
    >>> except_as_raise_does_not_delete_target(None, TypeError)
419
    1
420
    """
421
    b = 1
422
    try:
423
        i = 1
424
        if x:
425
            raise x
426
    except a as b:
427
        i = 2
428
        assert isinstance(b, a)
429

430
    # exception variable leaks with Py2 except-as semantics
431
    if x:
432
        assert isinstance(b, a)
433
    else:
434
        assert b == 1
435
    return i
436

437
def except_as_raise_with_empty_except(x, a):
438
    """
439
    >>> except_as_raise_with_empty_except(None, TypeError)
440
    >>> except_as_raise_with_empty_except(TypeError('test'), TypeError)
441
    >>> except_as_raise_with_empty_except(ValueError('test'), TypeError)
442
    Traceback (most recent call last):
443
    ValueError: test
444
    >>> except_as_raise_with_empty_except(None, TypeError)
445
    """
446
    try:
447
        if x:
448
            raise x
449
        b = 1
450
    except a as b:
451
        pass
452
    if x:
453
        assert isinstance(b, a)
454
    else:
455
        assert b == 1
456

457
def complete_except_as_no_raise(a, b):
458
    """
459
    >>> complete_except_as_no_raise(TypeError, ValueError)
460
    5
461
    """
462
    d = a or b  # mark used
463

464
    try:
465
        i = 1
466
    except (a, b) as c:
467
        i = 2
468
    except (b, a) as c:
469
        i = 3
470
    except:
471
        i = 4
472
    else:
473
        i = 5
474
    return i
475

476
def complete_except_as_raise(x, a, b):
477
    """
478
    >>> complete_except_as_raise(None, TypeError, ValueError)
479
    5
480
    >>> complete_except_as_raise(TypeError('test'), TypeError, ValueError)
481
    2
482
    >>> complete_except_as_raise(ValueError('test'), TypeError, ValueError)
483
    2
484
    >>> complete_except_as_raise(IndexError('5'), TypeError, ValueError)
485
    4
486
    >>> complete_except_as_raise(None, TypeError, ValueError)
487
    5
488
    """
489
    try:
490
        i = 1
491
        if x:
492
            raise x
493
    except (a, b) as c:
494
        i = 2
495
        assert isinstance(c, (a, b))
496
    except (b, a) as c:
497
        i = 3
498
        assert isinstance(c, (a, b))
499
    except:
500
        i = 4
501
    else:
502
        i = 5
503
    return i
504

505

506
def try_except_raise_new(initial, catch, throw):
507
    """
508
    >>> try_except_raise_new(None, TypeError, ValueError)
509
    >>> try_except_raise_new(TypeError, IndexError, ValueError)
510
    Traceback (most recent call last):
511
    TypeError
512
    >>> try_except_raise_new(TypeError, TypeError, ValueError)
513
    Traceback (most recent call last):
514
    ValueError
515
    """
516
    try:
517
        if initial is not None:
518
            raise initial
519
    except catch:
520
        raise throw
521

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.