Skip to content

gh-150207: Raise MemoryError on tokenizer allocation failure instead of crashing#150275

Open
grantlouisherman wants to merge 3 commits into
python:mainfrom
grantlouisherman:fix-150207-type-system-crash
Open

gh-150207: Raise MemoryError on tokenizer allocation failure instead of crashing#150275
grantlouisherman wants to merge 3 commits into
python:mainfrom
grantlouisherman:fix-150207-type-system-crash

Conversation

@grantlouisherman
Copy link
Copy Markdown
Contributor

@grantlouisherman grantlouisherman commented May 22, 2026

Original report was around this snippet of code:

MB_512 = 512 * 1024 * 1024
resource.setrlimit(resource.RLIMIT_AS, (MB_512, MB_512)) 
eval("A"* 420000000)

Throwing a system error. Based off the write up made in #150207 I felt like there was a fair case to actually show the user a Mem error instead of a system crash.

... MB_512 = 512 * 1024 * 1024
... resource.setrlimit(resource.RLIMIT_AS, (MB_512, MB_512))
... eval("A"* 420000000)
...
Traceback (most recent call last):
  File "<python-input-0>", line 4, in <module>
    eval("A"* 420000000)
    ~~~~^^^^^^^^^^^^^^^^
MemoryError
>>>

I think this does provide a bit nicer of an experience but I think I need an actual adult to verify this for me. I also dont know the larger ramifications but it felt worth to throw something up

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 22, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@grantlouisherman
Copy link
Copy Markdown
Contributor Author

Hey @aisk @stestagg let me know your thoughts

@stestagg
Copy link
Copy Markdown

This seems reasonable to me.

One thing that's important is that any callers of these that try to recover from an allocation error know that there's a py exception to clear. In this case, I don't think there is one, just adding here in case I missed something!

@grantlouisherman
Copy link
Copy Markdown
Contributor Author

@pablogsal @lysnikolaou do these changes seem reasonable?

@grantlouisherman grantlouisherman force-pushed the fix-150207-type-system-crash branch 3 times, most recently from 76be14c to 37ed2a6 Compare May 29, 2026 18:04
grantlouisherman and others added 2 commits May 31, 2026 21:33
@grantlouisherman grantlouisherman force-pushed the fix-150207-type-system-crash branch from 37ed2a6 to 55820e5 Compare June 1, 2026 01:33
@aisk
Copy link
Copy Markdown
Member

aisk commented Jun 1, 2026

@grantlouisherman Hi, please don't use force push in the future, see the Dev Guide.

@aisk aisk changed the title gh-150207: type-crash(): make helpers.c in tokenizer throw a no mem error gh-150207: make helpers.c in tokenizer throw a no mem error Jun 1, 2026
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-05-22-21-52-38.gh-issue-150207.l2BUtI.rst Outdated
@aisk
Copy link
Copy Markdown
Member

aisk commented Jun 1, 2026

I guess there is another missing check here:

if ((tok->buf = (char *)PyMem_Malloc(BUFSIZ)) == NULL) {
_PyTokenizer_Free(tok);
return NULL;

@aisk aisk changed the title gh-150207: make helpers.c in tokenizer throw a no mem error gh-150207: Raise MemoryError on tokenizer allocation failure instead of crashing Jun 1, 2026
…e-150207.l2BUtI.rst

Co-authored-by: AN Long <aisk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants