Skip to content

gh-138205: Remove the resize method on mmap object on platforms don't support it - #138276

Merged
kumaraditya303 merged 15 commits into
python:mainfrom
aisk:remove-mmap-resize
Sep 6, 2025
Merged

gh-138205: Remove the resize method on mmap object on platforms don't support it#138276
kumaraditya303 merged 15 commits into
python:mainfrom
aisk:remove-mmap-resize

Conversation

@aisk

@aiskaisk commented Aug 31, 2025

Copy link
Copy Markdown
Member

@serhiy-storchakaserhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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.

Comment threadLib/test/test_mmap.py Outdated
@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
Copy Markdown
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 threadLib/test/test_mmap.py Outdated
Comment on lines +139 to +140
f = open(TESTFN, 'rb')
try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

with

Comment threadLib/test/test_mmap.py Outdated
try:
m.resize(2*mapsize)
except SystemError: # resize is not universally supported
except AttributeError: # resize is not universally supported

Copy link
Copy Markdown
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)

Comment threadLib/test/test_mmap.py Outdated
try:
m.resize(512)
except SystemError:
except AttributeError:

Copy link
Copy Markdown
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

Comment threadModules/mmapmodule.c Outdated
return 0;

}
#endif /* defined(MS_WINDOWS) || defined(HAVE_MREMAP) */

Copy link
Copy Markdown
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 */

Comment threadModules/mmapmodule.c Outdated
#endif /* UNIX */
}
}
#endif /* defined(MS_WINDOWS) || defined(HAVE_MREMAP) */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As above.

Comment threadDoc/library/mmap.rst Outdated
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
Copy Markdown
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
aiskforce-pushed the remove-mmap-resize branch from 91cda99 to b1241abCompareSeptember 3, 2025 15:05
@aisk

aisk commented Sep 3, 2025

Copy link
Copy Markdown
MemberAuthor

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 ownerSeptember 4, 2025 03:17

@serhiy-storchakaserhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. 👍

Comment threadDoc/library/mmap.rst Outdated
aiskand others added 2 commits September 4, 2025 14:21
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

@vstinnervstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment threadLib/test/test_mmap.py Outdated
Comment threadLib/test/test_mmap.py Outdated
aiskand others added 2 commits September 5, 2025 01:12
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
@kumaraditya303
kumaraditya303 merged commit c919d02 into python:mainSep 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 <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for freeto 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

@aisk@vstinner@serhiy-storchaka@kumaraditya303