Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-45459: Rename buffer.h to pybuffer.h #31201

Merged
merged 2 commits into from
Feb 22, 2022
Merged

bpo-45459: Rename buffer.h to pybuffer.h #31201

merged 2 commits into from
Feb 22, 2022

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Feb 7, 2022

Rename Include/buffer.h header file to Include/pybuffer.h to avoid
conflits with projects having an existing buffer.h header file.

  • Incude pybuffer.h before object.h in Python.h.
  • Remove #include "buffer.h" from Include/cpython/object.h.
  • Add a forward declaration of the PyObject type in pybuffer.h to fix
    an inter-dependency issue.

https://bugs.python.org/issue45459

Rename Include/buffer.h header file to Include/pybuffer.h to avoid
conflits with projects having an existing buffer.h header file.

* Incude pybuffer.h before object.h in Python.h.
* Remove #include "buffer.h" from Include/cpython/object.h.
* Add a forward declaration of the PyObject type in pybuffer.h to fix
  an inter-dependency issue.
@vstinner
Copy link
Member Author

vstinner commented Feb 7, 2022

cc @rdb @pmp-p @tiran

@@ -17,6 +17,9 @@ extern "C" {
*
*/

// Forward declaration
typedef struct _object PyObject;
Copy link
Member

Choose a reason for hiding this comment

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

That's ugly. I had a forward declaration in my first PR. @encukou asked me to remove it.

Copy link
Member Author

Choose a reason for hiding this comment

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

How is it "ugly"? Could you be more specific?

There are many similar code used in Python header files to fix inter-dependency issues. See for example Include/pystate.h.

Copy link
Member

Choose a reason for hiding this comment

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

I didn't realize the reason it was there. It's ugly, but it might be a good way to solve the issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

I elaborated the comment to explain why this "Forward declaration" is needed. Is it better?

Copy link
Member

Choose a reason for hiding this comment

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

Why not forward-define Py_buffer in object.h instead? That's a smaller change anyway, and removes the ordering requirement completely, rather than simply forcing it to happen in a certain way.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see Py_buffer as a C structure which is unrelated to Python objects. It's like a low-level thing "under" PyObject. In practice, it contains a PyObject* pointer and pybuffer.h contains other function definitions which use PyObject*.

I don't think that the order matters much, both orders work.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why not forward-define Py_buffer in object.h instead?

It's not possible: the Py_buffer struct has no name on purpose, see @tiran's comment.

@bedevere-bot
Copy link

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@vstinner
Copy link
Member Author

vstinner commented Feb 8, 2022

@tiran: Do you prefer to have a forward declaration of Py_buffer in object.h, or a forward declaration of PyObject in pybuffer.h? Well, I wrote my opinion on the order in a comment above, but I don't really care.

Or if you prefer, I can put all of these definitions in a new single header file and make sure that it's one of the first included header. As I said, there are other header files using such forward declarations to break cycles of dependencies between two header files.

In C, it's ok to use struct MyStruct before MyStruct is defined. What is not ok is to use MyType before MyType is defined. typedef struct MyStruct MyType; statement must come first.

@tiran
Copy link
Member

tiran commented Feb 8, 2022

@encukou asked me to make Py_buffer an anon struct typedef. C does not support forward declaration of anon struct. You either have to give the Py_buffer struct a name again or keep your approach.

@vstinner
Copy link
Member Author

vstinner commented Feb 8, 2022

You either have to give the Py_buffer struct a name again or keep your approach.

Well, this PR works and doesn't require to give a name to the anonymous Py_buffer struct :-)

@tiran: GitHub still tells me that there is "1 change requested" from your side. Can you either remove your -1 vote or vote +1 (approve)?

@vstinner vstinner merged commit 66b3cd7 into python:main Feb 22, 2022
@bedevere-bot
Copy link

@vstinner: Please replace # with GH- in the commit message next time. Thanks!

@vstinner vstinner deleted the pybuffer_h branch February 22, 2022 22:11
@vstinner
Copy link
Member Author

I merged my fix. Thanks for reviews.

It's unclear to me why the Py_buffer struct has no name, but well, I'm fine with no giving it a name :-)

Sign up for free to 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.

6 participants