8000 gh-102500: Implement PEP 688 by JelleZijlstra · Pull Request #102521 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-102500: Implement PEP 688 #102521

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 51 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
ca76251
try 1
JelleZijlstra Oct 2, 2022
901b459
progress
JelleZijlstra Oct 3, 2022
c5407c7
this is better
JelleZijlstra Oct 4, 2022
86f2000
this seems to work
JelleZijlstra Oct 4, 2022
ac10887
one more test
JelleZijlstra Oct 5, 2022
2563016
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Oct 5, 2022
530a160
additions
JelleZijlstra Oct 5, 2022
de3a4bc
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Oct 12, 2022
116f72e
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Oct 21, 2022
207d2fd
introduce __mutable_buffer__ (rather hackily)
JelleZijlstra Oct 21, 2022
aee2c33
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Nov 6, 2022
007fdc1
Rip out __mutable_buffer__
JelleZijlstra Nov 6, 2022
46a9239
__release_buffer__ calls mv.release()
JelleZijlstra Nov 8, 2022
3020fee
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Nov 8, 2022
be9bf45
additional test
JelleZijlstra Nov 8, 2022
a6bf0e8
undo stray change
JelleZijlstra Nov 8, 2022
04d267e
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Dec 18, 2022
04d0a42
throw an error if already released
JelleZijlstra Dec 18, 2022
bb6d076
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Mar 8, 2023
5f755fe
Fix compiler warning
JelleZijlstra Mar 8, 2023
0b02e63
news
JelleZijlstra Mar 8, 2023
8e4db43
fix some tests
JelleZijlstra Mar 8, 2023
6c863bc
More tests. Add flags= argument to memoryview
JelleZijlstra Mar 10, 2023
3b4b7d6
Make memoryview flags arg private
JelleZijlstra Mar 11, 2023
f295012
regen global objects
JelleZijlstra Mar 11, 2023
1660ae4
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Apr 5, 2023
0af94ac
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Apr 5, 2023
b5ea908
Ignore new C globals for now
JelleZijlstra Apr 5, 2023
b22bfa9
not static (should not have committed this)
JelleZijlstra Apr 5, 2023
69e8f7c
Use tabs not spaces
JelleZijlstra Apr 5, 2023
4ca7a7c
Address Kumar's feedback
JelleZijlstra Apr 10, 2023
fd2d716
Merge branch 'main' into pep688v2
JelleZijlstra Apr 10, 2023
a70e12d
Address another piece of feedback
JelleZijlstra Apr 13, 2023
96c9253
Use a classmethod instead of a new arg to the memoryview constructor
JelleZijlstra Apr 13, 2023
1f7f7a0
fix typo
JelleZijlstra Apr 13, 2023
7665dde
Merge branch 'main' into pep688v2
JelleZijlstra Apr 25, 2023
e99c188
Merge branch 'main' into pep688v2
JelleZijlstra Apr 26, 2023
33691ea
Add Py_SAFE_DOWNCAST
JelleZijlstra Apr 26, 2023
8f5073f
Add some test cases (thanks Shantanu)
JelleZijlstra Apr 26, 2023
b69dc7c
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Apr 26, 2023
9d33003
Merge remote-tracking branch 'upstream/main' into pep688v2
JelleZijlstra Apr 29, 2023
cecb6a5
Remove spurious global strings
JelleZijlstra Apr 29, 2023
9b941a0
Merge branch 'main' into pep688v2
JelleZijlstra May 4, 2023
b22d66f
Merge branch 'main' of https://github.com/python/cpython into pep688v2
kumaraditya303 May 4, 2023
0f77bbb
fixup global objects
kumaraditya303 May 4, 2023
61f54ce
minor fixes
kumaraditya303 May 4, 2023
9f2d16b
newlines
JelleZijlstra May 4, 2023
5e0d2de
Rename variable
JelleZijlstra May 4, 2023
b380632
Check for INT_MAX
JelleZijlstra May 4, 2023
8502b98
Remove tp_new
JelleZijlstra May 4, 2023
b51465f
regen-all
JelleZijlstra May 4, 2023
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
Prev Previous commit
Next Next commit
Rip out __mutable_buffer__
  • Loading branch information
JelleZijlstra committed Nov 6, 2022
commit 007fdc115383b42b78ddf28bdcd47714de9c53e7
4 changes: 0 additions & 4 deletions Include/internal/pycore_abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ _PyIndex_Check(PyObject *obj)
PyObject *_PyNumber_PowerNoMod(PyObject *lhs, PyObject *rhs);
PyObject *_PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs);

PyAPI_FUNC(PyObject *) _PyBuffer_MutableBufferGetter(PyObject *instance, void *ignored);

#define _PY_BUFFER_MUTABLE_BUFFER_GETSET { "__mutable_buffer__", _PyBuffer_MutableBufferGetter, NULL, NULL },

#ifdef __cplusplus
}
#endif
Expand Down
14 changes: 1 addition & 13 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _f(): pass
"Mapping", "MutableMapping",
"MappingView", "KeysView", "ItemsView", "ValuesView",
"Sequence", "MutableSequence",
"ByteString", "Buffer", "MutableBuffer",
"ByteString", "Buffer",
]

# This module has been renamed from collections.abc to _collections_abc to
Expand Down Expand Up @@ -454,18 +454,6 @@ def __subclasshook__(cls, C):
return NotImplemented


class MutableBuffer(Buffer):

__slots__ = ()
__mutable_buffer__ = True

@classmethod
def __subclasshook__(cls, C):
if cls is MutableBuffer:
return _check_methods(C, "__buffer__", "__mutable_buffer__")
return NotImplemented


class _CallableGenericAlias(GenericAlias):
""" Represent `Callable[argtypes, resulttype]`.

Expand Down
11 changes: 1 addition & 10 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from collections.abc import Set, MutableSet
from collections.abc import Mapping, MutableMapping, KeysView, ItemsView, ValuesView
from collections.abc import Sequence, MutableSequence
from collections.abc import ByteString, Buffer, MutableBuffer
from collections.abc import ByteString, Buffer


class TestUserObjects(unittest.TestCase):
Expand Down Expand Up @@ -1953,15 +1953,6 @@ def test_Buffer(self):
self.assertFalse(issubclass(sample, Buffer))
self.validate_abstract_methods(Buffer, '__buffer__')

def test_MutableBuffer(self):
for sample in [bytearray, memoryview]:
self.assertIsInstance(sample(b"x"), MutableBuffer)
self.assertTrue(issubclass(sample, MutableBuffer))
for sample in [bytes, str, list, tuple]:
self.assertNotIsInstance(sample(), MutableBuffer)
self.assertFalse(issubclass(sample, MutableBuffer))
self.validate_abstract_methods(MutableBuffer, '__buffer__', '__release_buffer__')

def test_MutableSequence(self):
for sample in [tuple, str, bytes]:
self.assertNotIsInstance(sample(), MutableSequence)
Expand Down
17 changes: 4 additions & 13 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ bytes(cdata)
#include "ctypes.h"

#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_abstract.h" // _PY_BUFFER_MUTABLE_BUFFER_GETSET

PyObject *PyExc_ArgError = NULL;

Expand Down Expand Up @@ -2793,11 +2792,6 @@ static PyBufferProcs PyCData_as_buffer = {
NULL,
};

static PyGetSetDef PyCData_getset[] = {
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{NULL}
};

/*
* CData objects are mutable, so they cannot be hashable!
*/
Expand Down Expand Up @@ -2907,7 +2901,7 @@ PyTypeObject PyCData_Type = {
0, /* tp_iternext */
PyCData_methods, /* tp_methods */
PyCData_members, /* tp_members */
PyCData_getset, /* tp_getset */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down Expand Up @@ -3330,7 +3324,6 @@ static PyGetSetDef PyCFuncPtr_getsets[] = {
{ "argtypes", (getter)PyCFuncPtr_get_argtypes,
(setter)PyCFuncPtr_set_argtypes,
"specify the argument types", NULL },
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{ NULL, NULL }
};

Expand Down Expand Up @@ -4455,7 +4448,7 @@ static PyTypeObject Struct_Type = {
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
PyCData_getset, /* tp_getset */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down Expand Up @@ -4497,7 +4490,7 @@ static PyTypeObject Union_Type = {
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
PyCData_getset, /* tp_getset */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down Expand Up @@ -4817,7 +4810,7 @@ PyTypeObject PyCArray_Type = {
0, /* tp_iternext */
Array_methods, /* tp_methods */
0, /* tp_members */
PyCData_getset, /* tp_getset */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down Expand Up @@ -4948,7 +4941,6 @@ Simple_get_value(CDataObject *self, void *Py_UNUSED(ignored))
static PyGetSetDef Simple_getsets[] = {
{ "value", (getter)Simple_get_value, (setter)Simple_set_value,
"current value", NULL },
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{ NULL, NULL }
};

Expand Down Expand Up @@ -5192,7 +5184,6 @@ static PyGetSetDef Pointer_getsets[] = {
{ "contents", (getter)Pointer_get_contents,
(setter)Pointer_set_contents,
"the object this pointer points to (read-write)", NULL },
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{ NULL, NULL }
};

Expand Down
8 changes: 1 addition & 7 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Python.h"
#include "pycore_abstract.h" // _PY_BUFFER_MUTABLE_BUFFER_GETSET
#include "pycore_object.h"
#include <stddef.h> // offsetof()
#include "_iomodule.h"
Expand Down Expand Up @@ -1125,11 +1124,6 @@ static PyBufferProcs bytesiobuf_as_buffer = {
(releasebufferproc) bytesiobuf_releasebuffer,
};

static PyGetSetDef bytesiobuf_getset[] = {
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{NULL}
};

Py_EXPORTED_SYMBOL PyTypeObject _PyBytesIOBuffer_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_io._BytesIOBuffer", /*tp_name*/
Expand Down Expand Up @@ -1160,7 +1154,7 @@ Py_EXPORTED_SYMBOL PyTypeObject _PyBytesIOBuffer_Type = {
0, /*tp_iternext*/
0, /*tp_methods*/
0, /*tp_members*/
bytesiobuf_getset, /*tp_getset*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
Expand Down
2 changes: 0 additions & 2 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_abstract.h" // _PY_BUFFER_MUTABLE_BUFFER_GETSET
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_bytesobject.h" // _PyBytes_Repeat
#include "structmember.h" // PyMemberDef
Expand Down Expand Up @@ -2278,7 +2277,6 @@ static PyGetSetDef array_getsets [] = {
"the typecode character used to create the array"},
{"itemsize", (getter) array_get_itemsize, N EF5E ULL,
"the size, in bytes, of one array item"},
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{NULL}
};

Expand Down
2 changes: 0 additions & 2 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "pycore_abstract.h" // _PY_BUFFER_MUTABLE_BUFFER_GETSET
#include "pycore_bytesobject.h" // _PyBytes_Find()
#include "pycore_fileutils.h" // _Py_stat_struct
#include "structmember.h" // PyMemberDef
Expand Down Expand Up @@ -888,7 +887,6 @@ static struct PyMethodDef mmap_object_methods[] = {

static PyGetSetDef mmap_object_getset[] = {
{"closed", (getter) mmap_closed_get, NULL, NULL},
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{NULL}
};

Expand Down
5 changes: 0 additions & 5 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2967,8 +2967,3 @@ _Py_FreeCharPArray(char *const array[])
}
PyMem_Free((void*)array);
}

PyObject *
_PyBuffer_MutableBufferGetter(PyObject *instance, void *ignored) {
Py_RETURN_TRUE;
}
9 changes: 2 additions & 7 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check(), _PY_BUFFER_MUTABLE_BUFFER_GETSET
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_bytes_methods.h"
#include "pycore_bytesobject.h"
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
Expand Down Expand Up @@ -2289,11 +2289,6 @@ Construct a mutable bytearray object from:\n\

static PyObject *bytearray_iter(PyObject *seq);

static PyGetSetDef bytearray_getset[] = {
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{NULL}
};

PyTypeObject PyByteArray_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"bytearray",
Expand Down Expand Up @@ -2325,7 +2320,7 @@ PyTypeObject PyByteArray_Type = {
0, /* tp_iternext */
bytearray_methods, /* tp_methods */
0, /* tp_members */
bytearray_getset, /* tp_getset */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down
3 changes: 1 addition & 2 deletions Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check(), _PY_BUFFER_MUTABLE_BUFFER_GETSET
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_strhex.h" // _Py_strhex_with_sep()
#include <stddef.h> // offsetof()
Expand Down Expand Up @@ -3186,7 +3186,6 @@ static PyGetSetDef memory_getsetlist[] = {
{"c_contiguous", (getter)memory_c_contiguous, NULL, memory_c_contiguous_doc},
{"f_contiguous", (getter)memory_f_contiguous, NULL, memory_f_contiguous_doc},
{"contiguous", (getter)memory_contiguous, NULL, memory_contiguous_doc},
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{NULL, NULL, NULL, NULL},
};

Expand Down
6 changes: 0 additions & 6 deletions Objects/picklebufobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_abstract.h" // _PY_BUFFER_MUTABLE_BUFFER_GETSET
#include <stddef.h>

typedef struct {
Expand Down Expand Up @@ -204,10 +203,6 @@ static PyMethodDef picklebuf_methods[] = {
{NULL, NULL}
};

static PyGetSetDef picklebuf_getset[] = {
_PY_BUFFER_MUTABLE_BUFFER_GETSET
{NULL}
};
PyTypeObject PyPickleBuffer_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "pickle.PickleBuffer",
Expand All @@ -221,5 +216,4 @@ PyTypeObject PyPickleBuffer_Type = {
.tp_weaklistoffset = offsetof(PyPickleBufferObject, weakreflist),
.tp_as_buffer = &picklebuf_as_buffer,
.tp_methods = picklebuf_methods,
.tp_getset = picklebuf_getset,
};
30 changes: 10 additions & 20 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "Python.h"
#include "pycore_call.h"
#include "pycore_abstract.h" // _PyBuffer_MutableBufferGetter
#include "pycore_code.h" // CO_FAST_FREE
#include "pycore_compile.h" // _Py_Mangle()
#include "pycore_initconfig.h" // _PyStatus_OK()
Expand Down Expand Up @@ -6196,24 +6195,15 @@ type_add_getset(PyTypeObject *type)

PyObject *dict = type->tp_dict;
for (; gsp->name != NULL; gsp++) {
if (gsp->get == _PyBuffer_MutableBufferGetter) {
PyObject *name = PyUnicode_FromString(gsp->name);
if (PyDict_SetDefault(dict, name, Py_True) == NULL) {
Py_DECREF(name);
return -1;
}
Py_DECREF(name);
} else {
PyObject *descr = PyDescr_NewGetSet(type, gsp);
if (descr == NULL) {
return -1;
}
if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
Py_DECREF(descr);
return -1;
}
PyObject *descr = PyDescr_NewGetSet(type, gsp);
if (descr == NULL) {
return -1;
}
if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
Py_DECREF(descr);
return -1;
}
Py_DECREF(descr);
}
return 0;
}
Expand Down Expand Up @@ -8565,7 +8555,7 @@ bufferwrapper_releasebuf(PyObject *self, Py_buffer *view)

assert(PyMemoryView_Check(bw->mv));
Py_TYPE(bw->mv)->tp_as_buffer->bf_releasebuffer(bw->mv, view);
if (Py_TYPE(bw->obj)->tp_as_buffer != NULL
if (Py_TYPE(bw->obj)->tp_as_buffer != NULL
&& Py_TYPE(bw->obj)->tp_as_buffer->bf_releasebuffer != NULL) {
Py_TYPE(bw->obj)->tp_as_buffer->bf_releasebuffer(bw->obj, view);
}
Expand All @@ -8589,7 +8579,7 @@ PyTypeObject _PyBufferWrapper_Type = {
.tp_as_buffer = &bufferwrapper_as_buffer,
};

static int
static int
slot_bf_getbuffer(PyObject *self, Py_buffer *buffer, int flags)
{
PyObject *flags_obj = PyLong_FromLong(flags);
Expand Down Expand Up @@ -8837,7 +8827,7 @@ static slotdef slotdefs[] = {
"Return a buffer object that exposes the underlying memory of the object."),
BUFSLOT(__release_buffer__, bf_releasebuffer, slot_bf_releasebuffer, wrap_releasebuffer,
"__release_buffer__($self, /)\n--\n\n"
"Release the buffer object that exposes the underlying memory of the object."),
"Release the buffer object that exposes the underlying memory of the object."),

AMSLOT(__await__, am_await, slot_am_await, wrap_unaryfunc,
"__await__($self, /)\n--\n\nReturn an iterator to be used in await expression."),
Expand Down
0