Skip to content

Conversation

@Tapeline
Copy link
Contributor

@Tapeline Tapeline commented Sep 17, 2025

@Tapeline Tapeline marked this pull request as ready for review September 17, 2025 21:02
UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg)
);
name = PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames,
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it is better no to break the declaration and the assignment?

PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg)
);
PyObject *name;
name = PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames,
Copy link
Member

Choose a reason for hiding this comment

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

PyTuple_GetItem can fail. We should be able to expect that it can't, so let's use PyTuple_GET_ITEM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't we leave this as is? This seems like an unrelated change.

Copy link
Member

Choose a reason for hiding this comment

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

No, the new usage of PyTuple_GetItem is potentially dangerous. Initially, it was used alongside _PyEval_FormatExcCheckArg, which would safely check for NULL to propagate exceptions during the lookup. Now, we're calling PyMapping_HasKeyWithError with a potentially NULL argument, which isn't safe. There are two solutions:

  1. Use PyTuple_GET_ITEM, which prevents failure entirely. This can possibly crash if someone messed with the code object, but that's possible already.
  2. Check the result of PyTuple_GetItem before calling PyMapping_HasKeyWithError.

My suggestion was choosing the former, but I missed that we were using PyTuple_GetItem initially. I think you're right that we should continue using that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the new usage of PyTuple_GetItem is potentially dangerous

I missed that we were using PyTuple_GetItem initially. I think you're right that we should continue using that.

So what should we do? Do we leave this as is?

Copy link
Member

Choose a reason for hiding this comment

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

Let's do this:

Check the result of PyTuple_GetItem before calling PyMapping_HasKeyWithError.

Try to fit C code in reasonable width
Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

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

The other two tests also assume that the user correctly typed the intended name and meant to delete a builtin rather than a masking use. I think this will be true less than half the time.

Comment on lines +842 to +846
def f():
del all

with self.assertRaisesRegex(UnboundLocalError, "cannot delete builtin 'all'"):
f()
Copy link
Member

Choose a reason for hiding this comment

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

Within a function it does not matter whether a name not in a global statement is or is not in globals or builtins. It only matters that it is an unbound local. Leave this case alone.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we do so, should we also revert changes in DELETE_FAST implementation?

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.

5 participants