(self)
| 140 | self.assertEqual(m.size(), 512) |
| 141 | |
| 142 | def test_access_parameter(self): |
| 143 | # Test for "access" keyword parameter |
| 144 | mapsize = 10 |
| 145 | with open(TESTFN, "wb") as fp: |
| 146 | fp.write(b"a"*mapsize) |
| 147 | with open(TESTFN, "rb") as f: |
| 148 | m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ) |
| 149 | self.assertEqual(m[:], b'a'*mapsize, "Readonly memory map data incorrect.") |
| 150 | |
| 151 | # Ensuring that readonly mmap can't be slice assigned |
| 152 | try: |
| 153 | m[:] = b'b'*mapsize |
| 154 | except TypeError: |
| 155 | pass |
| 156 | else: |
| 157 | self.fail("Able to write to readonly memory map") |
| 158 | |
| 159 | # Ensuring that readonly mmap can't be item assigned |
| 160 | try: |
| 161 | m[0] = b'b' |
| 162 | except TypeError: |
| 163 | pass |
| 164 | else: |
| 165 | self.fail("Able to write to readonly memory map") |
| 166 | |
| 167 | # Ensuring that readonly mmap can't be write() to |
| 168 | try: |
| 169 | m.seek(0, 0) |
| 170 | m.write(b'abc') |
| 171 | except TypeError: |
| 172 | pass |
| 173 | else: |
| 174 | self.fail("Able to write to readonly memory map") |
| 175 | |
| 176 | # Ensuring that readonly mmap can't be write_byte() to |
| 177 | try: |
| 178 | m.seek(0, 0) |
| 179 | m.write_byte(b'd') |
| 180 | except TypeError: |
| 181 | pass |
| 182 | else: |
| 183 | self.fail("Able to write to readonly memory map") |
| 184 | |
| 185 | if hasattr(m, 'resize'): |
| 186 | # Ensuring that readonly mmap can't be resized |
| 187 | with self.assertRaises(TypeError): |
| 188 | m.resize(2 * mapsize) |
| 189 | with open(TESTFN, "rb") as fp: |
| 190 | self.assertEqual(fp.read(), b'a'*mapsize, |
| 191 | "Readonly memory map data file was modified") |
| 192 | |
| 193 | # Opening mmap with size too big |
| 194 | with open(TESTFN, "r+b") as f: |
| 195 | try: |
| 196 | m = mmap.mmap(f.fileno(), mapsize+1) |
| 197 | except ValueError: |
| 198 | # we do not expect a ValueError on Windows |
| 199 | # CAUTION: This also changes the size of the file on disk, and |
nothing calls this directly
no test coverage detected