Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4013627

Browse files
committed
add structured exception handling to mmap_read_method
1 parent 7e87d30 commit 4013627

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Modules/mmapmodule.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,17 @@ mmap_read_line_method(mmap_object *self,
291291
return result;
292292
}
293293

294+
#if defined(MS_WIN32) && !defined(DONT_USE_SEH)
295+
static DWORD HandlePageException(EXCEPTION_POINTERS *ptrs, EXCEPTION_RECORD *record)
296+
{
297+
*record = *ptrs->ExceptionRecord;
298+
if (ptrs->ExceptionRecord->ExceptionCode == EXCEPTION_IN_PAGE_ERROR) {
299+
return EXCEPTION_EXECUTE_HANDLER;
300+
}
301+
return EXCEPTION_CONTINUE_SEARCH;
302+
}
303+
#endif
304+
294305
static PyObject *
295306
mmap_read_method(mmap_object *self,
296307
PyObject *args)
@@ -307,8 +318,23 @@ mmap_read_method(mmap_object *self,
307318
remaining = (self->pos < self->size) ? self->size - self->pos : 0;
308319
if (num_bytes < 0 || num_bytes > remaining)
309320
num_bytes = remaining;
321+
322+
#if defined(MS_WIN32) && !defined(DONT_USE_SEH)
323+
EXCEPTION_RECORD record;
324+
__try {
325+
result = PyBytes_FromStringAndSize(&self->data[self->pos], num_bytes);
326+
self->pos += num_bytes;
327+
}
328+
__except (HandlePageException(GetExceptionInformation(), &record)) {
329+
NTSTATUS code = record.ExceptionInformation[2];
330+
PyErr_SetFromWindowsErr(code);
331+
result = NULL;
332+
}
333+
#else
310334
result = PyBytes_FromStringAndSize(&self->data[self->pos], num_bytes);
311335
self->pos += num_bytes;
336+
#endif
337+
312338
return result;
313339
}
314340

0 commit comments

Comments
 (0)