Skip to content

[WIP] _PyTuple_ITEMS() now requires PyTupleObject*#10706

Closed
vstinner wants to merge 1 commit intopython:masterfrom
vstinner:tuple_explicit_cast
Closed

[WIP] _PyTuple_ITEMS() now requires PyTupleObject*#10706
vstinner wants to merge 1 commit intopython:masterfrom
vstinner:tuple_explicit_cast

Conversation

@vstinner
Copy link
Copy Markdown
Member

  • Convert _PyTuple_ITEMS() to a static function which requires
    a PyTupleObject* argument.
  • Add an assertion to _PyTuple_CAST() to ensure that the argument is
    a tuple object.

* Convert _PyTuple_ITEMS() to a static function which requires
  a PyTupleObject* argument.
* Add an assertion to _PyTuple_CAST() to ensure that the argument is
  a tuple object.
@rhettinger
Copy link
Copy Markdown
Contributor

Will third-party C code be broken if they don't make these same changes?

@vstinner vstinner changed the title _PyTuple_ITEMS() now requires PyTupleObject* [WIP] _PyTuple_ITEMS() now requires PyTupleObject* Nov 26, 2018
@serhiy-storchaka
Copy link
Copy Markdown
Member

I don't like this. This makes the user code cluttered. The public API should use PyObject*, other types are implementation details. While _PyTuple_ITEMS is not in a public API, PyTuple_GET_ITEM and PyTuple_GET_SIZE are, and they should be called with the PyObject* argument.

@vstinner
Copy link
Copy Markdown
Member Author

Sorry, I shouldn't have posted a public PR for this change. I wasn't sure if it was a good idea or not. But sometimes, seeing the diff in GitHub helps me :-)

Will third-party C code be broken if they don't make these same changes?

The C API must not be modified, so third-party code doesn't have to be modified.

Note: _PyTuple_ITEMS() function is part of the internal API. I introduced it very recently, so no third-party C extensions don't use and cannot use it :-)

Honestly, I'm not sure that this change is worth it. I wasn't sure if the compiler might benefit of it, but when I looked at the machine code, the compiler is always smarter than me and doesn't need my help :-)

I abandon this change in favor of the minimum enhancement of _PyTuple_CAST(): PR #10712.

@vstinner vstinner closed this Nov 26, 2018
@vstinner vstinner deleted the tuple_explicit_cast branch November 26, 2018 09:37
@vstinner
Copy link
Copy Markdown
Member Author

PyTuple_GET_ITEM and PyTuple_GET_SIZE are, and they should be called with the PyObject* argument.

Right. I mostly wrote this PR to ask myself if there is any benefit of passing directly the right type. I don't see any obvious advantage, so _Py*_CAST() macros are here to stay :-)

@serhiy-storchaka
Copy link
Copy Markdown
Member

But sometimes, seeing the diff in GitHub helps me :-)

Tip: you can create PRs for your own fork.

@vstinner
Copy link
Copy Markdown
Member Author

Tip: you can create PRs for your own fork.

Aha, I will try next time. Thanks for the tip :-)

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