Skip to content

Don't segfault on Tensor.__delitem__#12726

Closed
t-vi wants to merge 1 commit intopytorch:masterfrom
t-vi:delitem_segfault
Closed

Don't segfault on Tensor.__delitem__#12726
t-vi wants to merge 1 commit intopytorch:masterfrom
t-vi:delitem_segfault

Conversation

@t-vi
Copy link
Copy Markdown
Collaborator

@t-vi t-vi commented Oct 16, 2018

The mapping protocol stipulates that when __delitem__ is called, this is passed to __setitem__ (well, the same function in the C extension interface) with NULL data.

PyTorch master crashes in this situation, with this patch, it does not anymore.

Test code (careful, sefaults your interpreter):

import torch
a = torch.randn(5)
del a[2]

The mapping protocol stipulates that when __delitem__
is called, this is passed to "__setitem__" (well, the same
function in the C extension interface) with NULL data.

https://docs.python.org/3/c-api/typeobj.html#c.PyMappingMethods.mp_ass_subscript

PyTorch master crashes in this situation, with the patch, it does
not anymore.
Copy link
Copy Markdown
Member

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

colesbury has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
The mapping protocol stipulates that when `__delitem__` is called, this is passed to `__setitem__` [(well, the same function in the C extension interface)](https://docs.python.org/3/c-api/typeobj.html#c.PyMappingMethods.mp_ass_subscript) with NULL data.

PyTorch master crashes in this situation, with this patch, it does not anymore.

Test code (careful, sefaults your interpreter):
```python
import torch
a = torch.randn(5)
del a[2]
```
Pull Request resolved: pytorch#12726

Differential Revision: D10414244

Pulled By: colesbury

fbshipit-source-id: c49716e1a0a3d9a117ce88fc394858f1df36ed79
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.

4 participants