Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(171187)

Side by Side Diff: Objects/memoryobject.c

Issue 13411: Hashable memoryviews
Patch Set: Created 7 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Objects/bytesobject.c ('k') | Objects/object.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Memoryview object implementation */ 2 /* Memoryview object implementation */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 #define IS_RELEASED(memobj) \ 6 #define IS_RELEASED(memobj) \
7 (((PyMemoryViewObject *) memobj)->view.buf == NULL) 7 (((PyMemoryViewObject *) memobj)->view.buf == NULL)
8 8
9 #define CHECK_RELEASED(memobj) \ 9 #define CHECK_RELEASED(memobj) \
10 if (IS_RELEASED(memobj)) { \ 10 if (IS_RELEASED(memobj)) { \
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 77
78 if (info->buf == NULL) { 78 if (info->buf == NULL) {
79 PyErr_SetString(PyExc_ValueError, 79 PyErr_SetString(PyExc_ValueError,
80 "cannot make memory view from a buffer with a NULL data pointer"); 80 "cannot make memory view from a buffer with a NULL data pointer");
81 return NULL; 81 return NULL;
82 } 82 }
83 mview = (PyMemoryViewObject *) 83 mview = (PyMemoryViewObject *)
84 PyObject_GC_New(PyMemoryViewObject, &PyMemoryView_Type); 84 PyObject_GC_New(PyMemoryViewObject, &PyMemoryView_Type);
85 if (mview == NULL) 85 if (mview == NULL)
86 return NULL; 86 return NULL;
87 mview->hash = -1;
87 dup_buffer(&mview->view, info); 88 dup_buffer(&mview->view, info);
88 /* NOTE: mview->view.obj should already have been incref'ed as 89 /* NOTE: mview->view.obj should already have been incref'ed as
89 part of PyBuffer_FillInfo(). */ 90 part of PyBuffer_FillInfo(). */
90 _PyObject_GC_TRACK(mview); 91 _PyObject_GC_TRACK(mview);
91 return (PyObject *)mview; 92 return (PyObject *)mview;
92 } 93 }
93 94
94 PyObject * 95 PyObject *
95 PyMemoryView_FromObject(PyObject *base) 96 PyMemoryView_FromObject(PyObject *base)
96 { 97 {
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 PyObject_GC_Del(self); 504 PyObject_GC_Del(self);
504 } 505 }
505 506
506 static PyObject * 507 static PyObject *
507 memory_repr(PyMemoryViewObject *self) 508 memory_repr(PyMemoryViewObject *self)
508 { 509 {
509 if (IS_RELEASED(self)) 510 if (IS_RELEASED(self))
510 return PyUnicode_FromFormat("<released memory at %p>", self); 511 return PyUnicode_FromFormat("<released memory at %p>", self);
511 else 512 else
512 return PyUnicode_FromFormat("<memory at %p>", self); 513 return PyUnicode_FromFormat("<memory at %p>", self);
514 }
515
516 static Py_hash_t
517 memory_hash(PyMemoryViewObject *self)
518 {
519 if (self->hash == -1) {
520 Py_buffer *view = &self->view;
521 CHECK_RELEASED_INT(self);
522 if (view->ndim > 1) {
523 PyErr_SetString(PyExc_NotImplementedError,
524 "can't hash multi-dimensional memoryview object");
525 return -1;
526 }
527 if (view->strides && view->strides[0] != view->itemsize) {
528 PyErr_SetString(PyExc_NotImplementedError,
529 "can't hash strided memoryview object");
530 return -1;
531 }
532 if (!view->readonly) {
533 PyErr_SetString(PyExc_ValueError,
534 "can't hash writable memoryview object");
535 return -1;
536 }
537 if (view->obj != NULL && PyObject_Hash(view->obj) == -1) {
538 /* Keep the original error message */
539 return -1;
540 }
541 /* Can't fail */
542 self->hash = _Py_HashBytes((unsigned char *) view->buf, view->len);
543 }
544 return self->hash;
513 } 545 }
514 546
515 /* Sequence methods */ 547 /* Sequence methods */
516 static Py_ssize_t 548 static Py_ssize_t
517 memory_length(PyMemoryViewObject *self) 549 memory_length(PyMemoryViewObject *self)
518 { 550 {
519 CHECK_RELEASED_INT(self); 551 CHECK_RELEASED_INT(self);
520 return get_shape0(&self->view); 552 return get_shape0(&self->view);
521 } 553 }
522 554
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 0, 854 0,
823 (destructor)memory_dealloc, /* tp_dealloc */ 855 (destructor)memory_dealloc, /* tp_dealloc */
824 0, /* tp_print */ 856 0, /* tp_print */
825 0, /* tp_getattr */ 857 0, /* tp_getattr */
826 0, /* tp_setattr */ 858 0, /* tp_setattr */
827 0, /* tp_reserved */ 859 0, /* tp_reserved */
828 (reprfunc)memory_repr, /* tp_repr */ 860 (reprfunc)memory_repr, /* tp_repr */
829 0, /* tp_as_number */ 861 0, /* tp_as_number */
830 &memory_as_sequence, /* tp_as_sequence */ 862 &memory_as_sequence, /* tp_as_sequence */
831 &memory_as_mapping, /* tp_as_mapping */ 863 &memory_as_mapping, /* tp_as_mapping */
832 0, /* tp_hash */ 864 (hashfunc)memory_hash, /* tp_hash */
833 0, /* tp_call */ 865 0, /* tp_call */
834 0, /* tp_str */ 866 0, /* tp_str */
835 PyObject_GenericGetAttr, /* tp_getattro */ 867 PyObject_GenericGetAttr, /* tp_getattro */
836 0, /* tp_setattro */ 868 0, /* tp_setattro */
837 &memory_as_buffer, /* tp_as_buffer */ 869 &memory_as_buffer, /* tp_as_buffer */
838 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 870 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
839 memory_doc, /* tp_doc */ 871 memory_doc, /* tp_doc */
840 (traverseproc)memory_traverse, /* tp_traverse */ 872 (traverseproc)memory_traverse, /* tp_traverse */
841 (inquiry)memory_clear, /* tp_clear */ 873 (inquiry)memory_clear, /* tp_clear */
842 memory_richcompare, /* tp_richcompare */ 874 memory_richcompare, /* tp_richcompare */
843 0, /* tp_weaklistoffset */ 875 0, /* tp_weaklistoffset */
844 0, /* tp_iter */ 876 0, /* tp_iter */
845 0, /* tp_iternext */ 877 0, /* tp_iternext */
846 memory_methods, /* tp_methods */ 878 memory_methods, /* tp_methods */
847 0, /* tp_members */ 879 0, /* tp_members */
848 memory_getsetlist, /* tp_getset */ 880 memory_getsetlist, /* tp_getset */
849 0, /* tp_base */ 881 0, /* tp_base */
850 0, /* tp_dict */ 882 0, /* tp_dict */
851 0, /* tp_descr_get */ 883 0, /* tp_descr_get */
852 0, /* tp_descr_set */ 884 0, /* tp_descr_set */
853 0, /* tp_dictoffset */ 885 0, /* tp_dictoffset */
854 0, /* tp_init */ 886 0, /* tp_init */
855 0, /* tp_alloc */ 887 0, /* tp_alloc */
856 memory_new, /* tp_new */ 888 memory_new, /* tp_new */
857 }; 889 };
OLDNEW
« no previous file with comments | « Objects/bytesobject.c ('k') | Objects/object.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+