Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f7c356c

Browse filesBrowse files
committed
add test using private function
1 parent 2ca440d commit f7c356c
Copy full SHA for f7c356c

File tree

Expand file treeCollapse file tree

2 files changed

+63
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+63
-1
lines changed

‎Lib/test/test_mmap.py

Copy file name to clipboardExpand all lines: Lib/test/test_mmap.py
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,44 @@ def __exit__(self, exc_type, exc_value, traceback):
10581058
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
10591059
m.write_byte(X())
10601060

1061+
@unittest.skipUnless(os.name == 'nt', 'requires Windows')
1062+
def test_access_violations(self):
1063+
PAGE_NOACCESS = 0x01
1064+
1065+
with open(TESTFN, 'bw+') as f:
1066+
f.write(b'\0'* PAGESIZE)
1067+
f.flush()
1068+
1069+
m = mmap.mmap(f.fileno(), PAGESIZE)
1070+
m._protect(PAGE_NOACCESS, 0, PAGESIZE)
1071+
with self.assertRaises(OSError):
1072+
m.read(PAGESIZE)
1073+
with self.assertRaises(OSError):
1074+
m.read_byte()
1075+
with self.assertRaises(OSError):
1076+
m.readline()
1077+
with self.assertRaises(OSError):
1078+
m.write(b'\0'* PAGESIZE)
1079+
with self.assertRaises(OSError):
1080+
m.write_byte(0)
1081+
with self.assertRaises(OSError):
1082+
m[0] # test mmap_subscript
1083+
with self.assertRaises(OSError):
1084+
m[0:10] # test mmap_subscript
1085+
with self.assertRaises(OSError):
1086+
m[0:10:2] # test mmap_subscript
1087+
with self.assertRaises(OSError):
1088+
m[0] = 1
1089+
with self.assertRaises(OSError):
1090+
m[0:10] = b'\0'* 10
1091+
with self.assertRaises(OSError):
1092+
m[0:10:2] = b'\0'* 5
1093+
with self.assertRaises(OSError):
1094+
m.move(0, 10, 1)
1095+
with self.assertRaises(OSError):
1096+
list(m) # test mmap_item
1097+
1098+
10611099
class LargeMmapTests(unittest.TestCase):
10621100

10631101
def setUp(self):

‎Modules/mmapmodule.c

Copy file name to clipboardExpand all lines: Modules/mmapmodule.c
+25-1Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,27 @@ mmap__sizeof__method(mmap_object *self, void *Py_UNUSED(ignored))
10001000
}
10011001
#endif
10021002

1003+
#if defined(MS_WINDOWS) && defined(Py_DEBUG)
1004+
static PyObject *
1005+
mmap_protect_method(mmap_object *self, PyObject *args) {
1006+
DWORD flNewProtect, flOldProtect;
1007+
Py_ssize_t start, length;
1008+
1009+
CHECK_VALID(NULL);
1010+
1011+
if (!PyArg_ParseTuple(args, "Inn:protect", &flNewProtect, &start, &length)) {
1012+
return NULL;
1013+
}
1014+
1015+
if (VirtualProtect((void *) (self->data + start), length, flNewProtect, &flOldProtect) == 0) {
1016+
PyErr_SetFromWindowsErr(GetLastError());
1017+
return NULL;
1018+
}
1019+
1020+
Py_RETURN_NONE;
1021+
}
1022+
#endif
1023+
10031024
#ifdef HAVE_MADVISE
10041025
static PyObject *
10051026
mmap_madvise_method(mmap_object *self, PyObject *args)
@@ -1069,7 +1090,10 @@ static struct PyMethodDef mmap_object_methods[] = {
10691090
{"__exit__", (PyCFunction) mmap__exit__method, METH_VARARGS},
10701091
#ifdef MS_WINDOWS
10711092
{"__sizeof__", (PyCFunction) mmap__sizeof__method, METH_NOARGS},
1072-
#endif
1093+
#ifdef Py_DEBUG
1094+
{"_protect", (PyCFunction) mmap_protect_method, METH_VARARGS},
1095+
#endif // Py_DEBUG
1096+
#endif // MS_WINDOWS
10731097
{NULL, NULL} /* sentinel */
10741098
};
10751099

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.