@@ -290,6 +290,24 @@ filter_page_exception_method(mmap_object *self, EXCEPTION_POINTERS *ptrs,
290
290
}
291
291
return EXCEPTION_CONTINUE_SEARCH ;
292
292
}
293
+
294
+ static void
295
+ _PyErr_SetFromNTSTATUS (ULONG status )
296
+ {
297
+ #if defined(MS_WINDOWS_DESKTOP ) || defined(MS_WINDOWS_SYSTEM )
298
+ PyErr_SetFromWindowsErr (LsaNtStatusToWinError ((NTSTATUS )status ));
299
+ #else
300
+ if (status & 0x80000000 ) {
301
+ // HRESULT-shaped codes are supported by PyErr_SetFromWindowsErr
302
+ PyErr_SetFromWindowsErr ((int )status );
303
+ }
304
+ else {
305
+ // No mapping for NTSTATUS values, so just return it for diagnostic purposes
306
+ // If we provide it as winerror it could incorrectly change the type of the exception.
307
+ PyErr_Format (PyExc_OSError , "Operating system error NTSTATUS=0x%08lX" , status );
308
+ }
309
+ #endif
310
+ }
293
311
#endif
294
312
295
313
#if defined(MS_WINDOWS ) && !defined(DONT_USE_SEH )
@@ -303,9 +321,7 @@ do { \
303
321
assert(record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR || \
304
322
record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION); \
305
323
if (record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { \
306
- NTSTATUS status = (NTSTATUS) record.ExceptionInformation[2]; \
307
- ULONG code = LsaNtStatusToWinError(status); \
308
- PyErr_SetFromWindowsErr(code); \
324
+ _PyErr_SetFromNTSTATUS((ULONG)record.ExceptionInformation[2]); \
309
325
} \
310
326
else if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { \
311
327
PyErr_SetFromWindowsErr(ERROR_NOACCESS); \
@@ -332,9 +348,7 @@ do { \
332
348
assert(record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR || \
333
349
record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION); \
334
350
if (record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { \
335
- NTSTATUS status = (NTSTATUS) record.ExceptionInformation[2]; \
336
- ULONG code = LsaNtStatusToWinError(status); \
337
- PyErr_SetFromWindowsErr(code); \
351
+ _PyErr_SetFromNTSTATUS((ULONG)record.ExceptionInformation[2]); \
338
352
} \
339
353
else if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { \
340
354
PyErr_SetFromWindowsErr(ERROR_NOACCESS); \
0 commit comments