@@ -61,8 +61,12 @@ def may_raise(error_result=error_handler):
61
61
else :
62
62
def decorator (fun ):
63
63
def wrapper (* args ):
64
- with error_handler :
64
+ typ = val = tb = None
65
+ try :
65
66
return fun (* args )
67
+ except BaseException as e :
68
+ typ , val , tb = sys .exc_info ()
69
+ PyErr_Restore (typ , val , tb )
66
70
return error_result
67
71
wrapper .__name__ = fun .__name__
68
72
return wrapper
@@ -141,7 +145,7 @@ def PyDict_Size(dictObj):
141
145
@may_raise (None )
142
146
def PyDict_Copy (dictObj ):
143
147
if not isinstance (dictObj , dict ):
144
- _PyErr_BadInternalCall (None , None , dictObj )
148
+ __bad_internal_call (None , None , dictObj )
145
149
return dictObj .copy ()
146
150
147
151
@@ -171,7 +175,7 @@ def PyDict_DelItem(dictObj, key):
171
175
@may_raise (- 1 )
172
176
def PyDict_Contains (dictObj , key ):
173
177
if not isinstance (dictObj , dict ):
174
- _PyErr_BadInternalCall (None , None , dictObj )
178
+ __bad_internal_call (None , None , dictObj )
175
179
return key in dictObj
176
180
177
181
@@ -264,14 +268,14 @@ def PyBytes_FromFormat(fmt, args):
264
268
@may_raise
265
269
def PyList_New (size ):
266
270
if size < 0 :
267
- _PyErr_BadInternalCall (None , None , None )
271
+ __bad_internal_call (None , None , None )
268
272
return [None ] * size
269
273
270
274
271
275
@may_raise
272
276
def PyList_GetItem (listObj , pos ):
273
277
if not isinstance (listObj , list ):
274
- _PyErr_BadInternalCall (None , None , listObj )
278
+ __bad_internal_call (None , None , listObj )
275
279
if pos < 0 :
276
280
raise IndexError ("list index out of range" )
277
281
return listObj [pos ]
@@ -280,7 +284,7 @@ def PyList_GetItem(listObj, pos):
280
284
@may_raise (- 1 )
281
285
def PyList_SetItem (listObj , pos , newitem ):
282
286
if not isinstance (listObj , list ):
283
- _PyErr_BadInternalCall (None , None , listObj )
287
+ __bad_internal_call (None , None , listObj )
284
288
if pos < 0 :
285
289
raise IndexError ("list assignment index out of range" )
286
290
listObj [pos ] = newitem
@@ -290,7 +294,7 @@ def PyList_SetItem(listObj, pos, newitem):
290
294
@may_raise (- 1 )
291
295
def PyList_Append (listObj , newitem ):
292
296
if not isinstance (listObj , list ):
293
- _PyErr_BadInternalCall (None , None , listObj )
297
+ __bad_internal_call (None , None , listObj )
294
298
listObj .append (newitem )
295
299
return 0
296
300
@@ -305,14 +309,14 @@ def PyList_AsTuple(listObj):
305
309
@may_raise
306
310
def PyList_GetSlice (listObj , ilow , ihigh ):
307
311
if not isinstance (listObj , list ):
308
- _PyErr_BadInternalCall (None , None , listObj )
312
+ __bad_internal_call (None , None , listObj )
309
313
return listObj [ilow :ihigh ]
310
314
311
315
312
316
@may_raise (- 1 )
313
317
def PyList_Size (listObj ):
314
318
if not isinstance (listObj , list ):
315
- _PyErr_BadInternalCall (None , None , listObj )
319
+ __bad_internal_call (None , None , listObj )
316
320
return len (listObj )
317
321
318
322
@@ -792,21 +796,21 @@ def PyTuple_New(size):
792
796
@may_raise
793
797
def PyTuple_GetItem (t , n ):
794
798
if not isinstance (t , tuple ):
795
- _PyErr_BadInternalCall (None , None , t )
799
+ __bad_internal_call (None , None , t )
796
800
return t [n ]
797
801
798
802
799
803
@may_raise (- 1 )
800
804
def PyTuple_Size (t ):
801
805
if not isinstance (t , tuple ):
802
- _PyErr_BadInternalCall (None , None , t )
806
+ __bad_internal_call (None , None , t )
803
807
return len (t )
804
808
805
809
806
810
@may_raise
807
811
def PyTuple_GetSlice (t , start , end ):
808
812
if not isinstance (t , tuple ):
809
- _PyErr_BadInternalCall (None , None , t )
813
+ __bad_internal_call (None , None , t )
810
814
return t [start :end ]
811
815
812
816
@@ -908,6 +912,11 @@ def PyErr_CreateAndSetException(exception_type, msg):
908
912
909
913
@may_raise (None )
910
914
def _PyErr_BadInternalCall (filename , lineno , obj ):
915
+ __bad_internal_call (filename , lineno , obj )
916
+
917
+
918
+ # IMPORTANT: only call from functions annotated with 'may_raise'
919
+ def __bad_internal_call (filename , lineno , obj ):
911
920
if filename is not None and lineno is not None :
912
921
msg = "{!s}:{!s}: bad argument to internal function" .format (filename , lineno )
913
922
else :
0 commit comments