Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test with fd=-1, and that this fails on Windows
  • Loading branch information
encukou committed Jan 9, 2024
commit 0a717f8d6aad1a44dc9dd85c301e8a91c1e6418e
21 changes: 21 additions & 0 deletions Lib/test/test_mmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,27 @@ def test_trackfd_parameter(self):
self.assertEqual(m.closed, True)
self.assertEqual(os.stat(TESTFN).st_size, size)

@unittest.skipIf(os.name == 'nt', 'trackfd not present on Windows')
def test_trackfd_neg1(self):
size = 64
with mmap.mmap(-1, size, trackfd=False) as m:
with self.assertRaises(OSError):
m.size()
with self.assertRaises(TypeError):
m.resize(size // 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work the same for mmap(-1, size, trackfd=True)? I have not found tests for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trackfd=True is the default. It's tested in test_resize_past_pos.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has different behavior. Should trackfd=False have any effect for fd=-1? Should combination fd=-1 and trackfd=False be allowed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually, trackfd=False should be the only possible behaviour for fd=-1 -- the fd is not tracked.
However, I don't think it's worth it to include a third value for “trackfd=default”. As it is, the default (True) works as before. Setting False has the documented effect -- disabling resize. This isn't very useful, but I don't think it's worth handling specially.

self.assertEqual(len(m), size)
m[0] = ord('a')
assert m[0] == ord('a')

@unittest.skipIf(os.name != 'nt', 'trackfd only fails on Windows')
def test_no_trackfd_parameter_on_windows(self):
# 'trackffd' is an invalid keyword argument for this function
size = 64
with self.assertRaises(TypeError):
mmap.mmap(-1, size, trackfd=True)
with self.assertRaises(TypeError):
mmap.mmap(-1, size, trackfd=False)

def test_bad_file_desc(self):
# Try opening a bad file descriptor...
self.assertRaises(OSError, mmap.mmap, -2, 4096)
Expand Down