Skip to content

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

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

Merged
merged 2 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "pymacro.h"
#include "pymath.h"
#include "pymem.h"
#include "pybuffer.h"
#include "object.h"
#include "objimpl.h"
#include "typeslots.h"
Expand All @@ -50,7 +51,6 @@
#include "longobject.h"
#include "cpython/longintrepr.h"
#include "boolobject.h"
#include "buffer.h"
#include "floatobject.h"
#include "complexobject.h"
#include "rangeobject.h"
Expand Down
2 changes: 0 additions & 2 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# error "this header file must not be included directly"
#endif

#include "buffer.h" // for Py_buffer, included after PyObject has been defined

PyAPI_FUNC(void) _Py_NewReference(PyObject *op);

#ifdef Py_TRACE_REFS
Expand Down
4 changes: 4 additions & 0 deletions Include/buffer.h → Include/pybuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ extern "C" {
*
*/

// Forward declaration to be able to include pybuffer.h before object.h:
// pybuffer.h uses PyObject and object.h uses Py_buffer.
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.


typedef struct {
void *buf;
PyObject *obj; /* owned reference */
Expand Down
2 changes: 1 addition & 1 deletion Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,6 @@ PYTHON_HEADERS= \
$(srcdir)/Include/abstract.h \
$(srcdir)/Include/bltinmodule.h \
$(srcdir)/Include/boolobject.h \
$(srcdir)/Include/buffer.h \
$(srcdir)/Include/bytearrayobject.h \
$(srcdir)/Include/bytesobject.h \
$(srcdir)/Include/ceval.h \
Expand Down Expand Up @@ -1472,6 +1471,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/osdefs.h \
$(srcdir)/Include/osmodule.h \
$(srcdir)/Include/patchlevel.h \
$(srcdir)/Include/pybuffer.h \
$(srcdir)/Include/pycapsule.h \
$(srcdir)/Include/pydtrace.h \
$(srcdir)/Include/pyerrors.h \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rename ``Include/buffer.h`` header file to ``Include/pybuffer.h`` to avoid
conflits with projects having an existing ``buffer.h`` header file. Patch by
Victor Stinner.
1 change: 1 addition & 0 deletions PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@
<ClInclude Include="..\Include\osmodule.h" />
<ClInclude Include="..\Include\patchlevel.h" />
<ClInclude Include="..\Include\py_curses.h" />
<ClInclude Include="..\Include\pybuffer.h" />
<ClInclude Include="..\Include\pycapsule.h" />
<ClInclude Include="..\Include\pyerrors.h" />
<ClInclude Include="..\Include\pyexpat.h" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
<ClInclude Include="..\Include\py_curses.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\pybuffer.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\pycapsule.h">
<Filter>Include</Filter>
</ClInclude>
Expand Down