Skip to content

Conversation

@aisk
Copy link
Contributor

@aisk aisk commented Aug 31, 2025

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you for your PR, @aisk. Actually, I already had a patch, I just waited until other mmap issues were resolved. It is very similar to your PR, so let's continue with it.

Please update your PR, few new tests that use SystemError were added.

I think it is worth to add an entry in the "Porting to 3.15" section in What's New.

@unittest.skipUnless(hasattr(mmap.mmap, 'resize'), 'requires mmap.resize')
def test_resize(self):
# Create a file to be mmap'ed.
f = open(TESTFN, 'bw+')
Copy link
Member

Choose a reason for hiding this comment

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

We can simply use with open(...) as f.

Comment on lines 139 to 140
f = open(TESTFN, 'rb')
try:
Copy link
Member

Choose a reason for hiding this comment

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

with

try:
m.resize(2*mapsize)
except SystemError: # resize is not universally supported
except AttributeError: # resize is not universally supported
Copy link
Member

Choose a reason for hiding this comment

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

-            # Ensuring that readonly mmap can't be resized
-            try:
-                m.resize(2*mapsize)
-            except SystemError:   # resize is not universally supported
-                pass
-            except TypeError:
-                pass
-            else:
-                self.fail("Able to resize readonly memory map")
+            if hasattr(m, 'resize'):
+                # Ensuring that readonly mmap can't be resized
+                with self.assertRaises(TypeError):
+                    m.resize(2*mapsize)

try:
m.resize(512)
except SystemError:
except AttributeError:
Copy link
Member

Choose a reason for hiding this comment

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

Simply use if hasattr(m, 'resize').

-            # Try resizing map
-            try:
+            if hasattr(m, 'resize'):
                 m.resize(512)
-            except SystemError:
-                pass
-            else:
-                # resize() is supported

return 0;

}
#endif /* defined(MS_WINDOWS) || defined(HAVE_MREMAP) */
Copy link
Member

Choose a reason for hiding this comment

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

We usually write this simpler:

Suggested change
#endif /* defined(MS_WINDOWS) || defined(HAVE_MREMAP) */
#endif /* MS_WINDOWS || HAVE_MREMAP */

#endif /* UNIX */
}
}
#endif /* defined(MS_WINDOWS) || defined(HAVE_MREMAP) */
Copy link
Member

Choose a reason for hiding this comment

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

As above.

pagefile) will silently create a new map with the original data copied over
up to the length of the new size.

.. availability:: Linux, Windows
Copy link
Member

Choose a reason for hiding this comment

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

It is also available on other platforms (e.g. FreeBSD).

See how this is documented for madvise().

@aisk aisk force-pushed the remove-mmap-resize branch from 91cda99 to b1241ab Compare September 3, 2025 15:05
@aisk
Copy link
Contributor Author

aisk commented Sep 3, 2025

Sorry for the force-push. I messed up the conflict resolution during the merge and had to redo the merge after the push.

@aisk aisk requested a review from AA-Turner as a code owner September 4, 2025 03:17
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

aisk and others added 2 commits September 5, 2025 01:12
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@kumaraditya303 kumaraditya303 merged commit c919d02 into python:main Sep 6, 2025
45 checks passed
lkollar pushed a commit to lkollar/cpython that referenced this pull request Sep 9, 2025
…orms don't support it (python#138276)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants