summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_mmap.py
diff options
context:
space:
mode:
authorDobatymo <Dobatymo@users.noreply.github.com>2024-05-10 09:47:30 (GMT)
committerGitHub <noreply@github.com>2024-05-10 09:47:30 (GMT)
commite85e8deaf3220c8d12b69294e45645aaf20187b9 (patch)
tree2091771e6b00c57455657b7e711ea57a7017c5ac /Lib/test/test_mmap.py
parent7e6fcab20003b07621dc02ea78d6ea2fda500371 (diff)
downloadcpython-e85e8deaf3220c8d12b69294e45645aaf20187b9.zip
cpython-e85e8deaf3220c8d12b69294e45645aaf20187b9.tar.gz
cpython-e85e8deaf3220c8d12b69294e45645aaf20187b9.tar.bz2
gh-118209: Add Windows structured exception handling to mmap module (GH-118213)
Diffstat (limited to 'Lib/test/test_mmap.py')
-rw-r--r--Lib/test/test_mmap.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index ee86227..a1cf538 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -3,6 +3,7 @@ from test.support import (
)
from test.support.import_helper import import_module
from test.support.os_helper import TESTFN, unlink
+from test.support.script_helper import assert_python_ok
import unittest
import errno
import os
@@ -12,6 +13,7 @@ import random
import socket
import string
import sys
+import textwrap
import weakref
# Skip test if we can't import mmap.
@@ -1058,6 +1060,81 @@ class MmapTests(unittest.TestCase):
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
m.write_byte(X())
+ @unittest.skipUnless(os.name == 'nt', 'requires Windows')
+ @unittest.skipUnless(hasattr(mmap.mmap, '_protect'), 'test needs debug build')
+ def test_access_violations(self):
+ from test.support.os_helper import TESTFN
+
+ code = textwrap.dedent("""
+ import faulthandler
+ import mmap
+ import os
+ import sys
+ from contextlib import suppress
+
+ # Prevent logging access violations to stderr.
+ faulthandler.disable()
+
+ PAGESIZE = mmap.PAGESIZE
+ PAGE_NOACCESS = 0x01
+
+ with open(sys.argv[1], 'bw+') as f:
+ f.write(b'A'* PAGESIZE)
+ f.flush()
+
+ m = mmap.mmap(f.fileno(), PAGESIZE)
+ m._protect(PAGE_NOACCESS, 0, PAGESIZE)
+ with suppress(OSError):
+ m.read(PAGESIZE)
+ assert False, 'mmap.read() did not raise'
+ with suppress(OSError):
+ m.read_byte()
+ assert False, 'mmap.read_byte() did not raise'
+ with suppress(OSError):
+ m.readline()
+ assert False, 'mmap.readline() did not raise'
+ with suppress(OSError):
+ m.write(b'A'* PAGESIZE)
+ assert False, 'mmap.write() did not raise'
+ with suppress(OSError):
+ m.write_byte(0)
+ assert False, 'mmap.write_byte() did not raise'
+ with suppress(OSError):
+ m[0] # test mmap_subscript
+ assert False, 'mmap.__getitem__() did not raise'
+ with suppress(OSError):
+ m[0:10] # test mmap_subscript
+ assert False, 'mmap.__getitem__() did not raise'
+ with suppress(OSError):
+ m[0:10:2] # test mmap_subscript
+ assert False, 'mmap.__getitem__() did not raise'
+ with suppress(OSError):
+ m[0] = 1
+ assert False, 'mmap.__setitem__() did not raise'
+ with suppress(OSError):
+ m[0:10] = b'A'* 10
+ assert False, 'mmap.__setitem__() did not raise'
+ with suppress(OSError):
+ m[0:10:2] = b'A'* 5
+ assert False, 'mmap.__setitem__() did not raise'
+ with suppress(OSError):
+ m.move(0, 10, 1)
+ assert False, 'mmap.move() did not raise'
+ with suppress(OSError):
+ list(m) # test mmap_item
+ assert False, 'mmap.__getitem__() did not raise'
+ with suppress(OSError):
+ m.find(b'A')
+ assert False, 'mmap.find() did not raise'
+ with suppress(OSError):
+ m.rfind(b'A')
+ assert False, 'mmap.rfind() did not raise'
+ """)
+ rt, stdout, stderr = assert_python_ok("-c", code, TESTFN)
+ self.assertEqual(stdout.strip(), b'')
+ self.assertEqual(stderr.strip(), b'')
+
+
class LargeMmapTests(unittest.TestCase):
def setUp(self):