Skip to content

Commit 57ba48c

Browse files
Add a data() method to exiv2.PreviewImage
This is identical to the pData() method, which is now deprecated.
1 parent d103c0f commit 57ba48c

File tree

5 files changed

+177
-61
lines changed

5 files changed

+177
-61
lines changed

CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Changes in v0.18.0:
2323
2/ Added binary wheels for Linux on arm64.
2424
3/ Exiv2 struct member names with a trailing underscore have more Pythonic
2525
aliases without the underscore.
26+
4/ Add data() method to exiv2.PreviewImage, deprecate pData() method.
2627

2728
Changes in v0.17.3:
2829
1/ Binary wheels incorporate libexiv2 v0.28.5.

src/interface/preview.i

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ EXPOSE_OBJECT_BUFFER(Exiv2::PreviewImage, false, false)
9292
RETURN_VIEW(Exiv2::byte* pData, arg1->size(), PyBUF_READ,
9393
Exiv2::PreviewImage::pData)
9494

95+
// Add data() alias of pData()
96+
RETURN_VIEW(Exiv2::byte* data, arg1->size(), PyBUF_READ,
97+
Exiv2::PreviewImage::data)
98+
%extend Exiv2::PreviewImage {
99+
const Exiv2::byte* data() {
100+
return $self->pData();
101+
};
102+
}
103+
104+
// Deprecate pData() in favour of data() since 2025-07-02
105+
%extend Exiv2::PreviewImage {
106+
const Exiv2::byte* pData() {
107+
PyErr_WarnEx(PyExc_DeprecationWarning,
108+
"Please use data() instead of pData().", 1);
109+
return $self->pData();
110+
};
111+
}
112+
%ignore Exiv2::PreviewImage::pData;
113+
95114
// Give Exiv2::PreviewProperties dict-like behaviour
96115
STRUCT_DICT(Exiv2::PreviewProperties, false, true)
97116

src/swig-0_27_7/preview_wrap.cxx

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5749,6 +5749,14 @@ SWIG_From_size_t (size_t value)
57495749
#endif
57505750
}
57515751

5752+
SWIGINTERN Exiv2::byte const *Exiv2_PreviewImage_data(Exiv2::PreviewImage *self){
5753+
return self->pData();
5754+
}
5755+
SWIGINTERN Exiv2::byte const *Exiv2_PreviewImage_pData(Exiv2::PreviewImage *self){
5756+
PyErr_WarnEx(PyExc_DeprecationWarning,
5757+
"Please use data() instead of pData().", 1);
5758+
return self->pData();
5759+
}
57525760

57535761
namespace swig {
57545762
template <> struct traits< Exiv2::PreviewProperties > {
@@ -6317,29 +6325,6 @@ SWIGINTERN PyObject *_wrap_PreviewImage_copy(PyObject *self, PyObject *args) {
63176325
}
63186326

63196327

6320-
SWIGINTERN PyObject *_wrap_PreviewImage_pData(PyObject *self, PyObject *args) {
6321-
PyObject *resultobj = 0;
6322-
Exiv2::PreviewImage *arg1 = (Exiv2::PreviewImage *) 0 ;
6323-
void *argp1 = 0 ;
6324-
int res1 = 0 ;
6325-
Exiv2::byte *result = 0 ;
6326-
6327-
if (!SWIG_Python_UnpackTuple(args, "PreviewImage_pData", 0, 0, 0)) SWIG_fail;
6328-
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_Exiv2__PreviewImage, 0 | 0 );
6329-
if (!SWIG_IsOK(res1)) {
6330-
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "PreviewImage_pData" "', argument " "1"" of type '" "Exiv2::PreviewImage const *""'");
6331-
}
6332-
arg1 = reinterpret_cast< Exiv2::PreviewImage * >(argp1);
6333-
result = (Exiv2::byte *)((Exiv2::PreviewImage const *)arg1)->pData();
6334-
6335-
resultobj = PyMemoryView_FromMemory((char*)result, arg1->size(), PyBUF_READ);
6336-
6337-
return resultobj;
6338-
fail:
6339-
return NULL;
6340-
}
6341-
6342-
63436328
SWIGINTERN PyObject *_wrap_PreviewImage_size(PyObject *self, PyObject *args) {
63446329
PyObject *resultobj = 0;
63456330
Exiv2::PreviewImage *arg1 = (Exiv2::PreviewImage *) 0 ;
@@ -6555,6 +6540,60 @@ SWIGINTERN PyObject *_wrap_PreviewImage___len__(PyObject *self, PyObject *args)
65556540
}
65566541

65576542

6543+
SWIGINTERN PyObject *_wrap_PreviewImage_data(PyObject *self, PyObject *args) {
6544+
PyObject *resultobj = 0;
6545+
Exiv2::PreviewImage *arg1 = (Exiv2::PreviewImage *) 0 ;
6546+
void *argp1 = 0 ;
6547+
int res1 = 0 ;
6548+
Exiv2::byte *result = 0 ;
6549+
6550+
if (!SWIG_Python_UnpackTuple(args, "PreviewImage_data", 0, 0, 0)) SWIG_fail;
6551+
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_Exiv2__PreviewImage, 0 | 0 );
6552+
if (!SWIG_IsOK(res1)) {
6553+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "PreviewImage_data" "', argument " "1"" of type '" "Exiv2::PreviewImage *""'");
6554+
}
6555+
arg1 = reinterpret_cast< Exiv2::PreviewImage * >(argp1);
6556+
{
6557+
try {
6558+
result = (Exiv2::byte *)Exiv2_PreviewImage_data(arg1);
6559+
}
6560+
catch(std::exception const& e) {
6561+
_set_python_exception();
6562+
SWIG_fail;
6563+
}
6564+
}
6565+
6566+
resultobj = PyMemoryView_FromMemory((char*)result, arg1->size(), PyBUF_READ);
6567+
6568+
return resultobj;
6569+
fail:
6570+
return NULL;
6571+
}
6572+
6573+
6574+
SWIGINTERN PyObject *_wrap_PreviewImage_pData(PyObject *self, PyObject *args) {
6575+
PyObject *resultobj = 0;
6576+
Exiv2::PreviewImage *arg1 = (Exiv2::PreviewImage *) 0 ;
6577+
void *argp1 = 0 ;
6578+
int res1 = 0 ;
6579+
Exiv2::byte *result = 0 ;
6580+
6581+
if (!SWIG_Python_UnpackTuple(args, "PreviewImage_pData", 0, 0, 0)) SWIG_fail;
6582+
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_Exiv2__PreviewImage, 0 | 0 );
6583+
if (!SWIG_IsOK(res1)) {
6584+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "PreviewImage_pData" "', argument " "1"" of type '" "Exiv2::PreviewImage *""'");
6585+
}
6586+
arg1 = reinterpret_cast< Exiv2::PreviewImage * >(argp1);
6587+
result = (Exiv2::byte *)Exiv2_PreviewImage_pData(arg1);
6588+
6589+
resultobj = PyMemoryView_FromMemory((char*)result, arg1->size(), PyBUF_READ);
6590+
6591+
return resultobj;
6592+
fail:
6593+
return NULL;
6594+
}
6595+
6596+
65586597
SWIGPY_DESTRUCTOR_CLOSURE(_wrap_delete_PreviewImage) /* defines _wrap_delete_PreviewImage_destructor_closure */
65596598

65606599
SWIGPY_LENFUNC_CLOSURE(_wrap_PreviewImage___len__) /* defines _wrap_PreviewImage___len___lenfunc_closure */
@@ -7110,13 +7149,6 @@ SWIGINTERN PyMethodDef SwigPyBuiltin__Exiv2__PreviewImage_methods[] = {
71107149
"Return a copy of the preview image data. The caller owns\n"
71117150
" this copy and %DataBuf ensures that it will be deleted.\n"
71127151
"" },
7113-
{ "pData", _wrap_PreviewImage_pData, METH_NOARGS, "\n"
7114-
"Returns a temporary Python memoryview of the object's data.\n"
7115-
"\n"
7116-
"WARNING: do not resize or delete the object while using the view.\n"
7117-
"\n"
7118-
":rtype: memoryview\n"
7119-
"" },
71207152
{ "size", _wrap_PreviewImage_size, METH_NOARGS, "Return the size of the preview image in bytes." },
71217153
{ "writeFile", _wrap_PreviewImage_writeFile, METH_O, "\n"
71227154
"Write the thumbnail image to a file.\n"
@@ -7142,6 +7174,20 @@ SWIGINTERN PyMethodDef SwigPyBuiltin__Exiv2__PreviewImage_methods[] = {
71427174
{ "height", _wrap_PreviewImage_height, METH_NOARGS, "Return the height of the preview image in pixels." },
71437175
{ "id", _wrap_PreviewImage_id, METH_NOARGS, "Return the preview image type identifier." },
71447176
{ "__len__", _wrap_PreviewImage___len__, METH_NOARGS, "" },
7177+
{ "data", _wrap_PreviewImage_data, METH_NOARGS, "\n"
7178+
"Returns a temporary Python memoryview of the object's data.\n"
7179+
"\n"
7180+
"WARNING: do not resize or delete the object while using the view.\n"
7181+
"\n"
7182+
":rtype: memoryview\n"
7183+
"" },
7184+
{ "pData", _wrap_PreviewImage_pData, METH_NOARGS, "\n"
7185+
"Returns a temporary Python memoryview of the object's data.\n"
7186+
"\n"
7187+
"WARNING: do not resize or delete the object while using the view.\n"
7188+
"\n"
7189+
":rtype: memoryview\n"
7190+
"" },
71457191
{ NULL, NULL, 0, NULL } /* Sentinel */
71467192
};
71477193

src/swig-0_28_5/preview_wrap.cxx

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5749,6 +5749,14 @@ SWIG_AsPtr_std_string (PyObject * obj, std::string **val)
57495749
SWIGINTERN size_t Exiv2_PreviewImage___len__(Exiv2::PreviewImage *self){
57505750
return self->size();
57515751
}
5752+
SWIGINTERN Exiv2::byte const *Exiv2_PreviewImage_data(Exiv2::PreviewImage *self){
5753+
return self->pData();
5754+
}
5755+
SWIGINTERN Exiv2::byte const *Exiv2_PreviewImage_pData(Exiv2::PreviewImage *self){
5756+
PyErr_WarnEx(PyExc_DeprecationWarning,
5757+
"Please use data() instead of pData().", 1);
5758+
return self->pData();
5759+
}
57525760

57535761
namespace swig {
57545762
template <> struct traits< Exiv2::PreviewProperties > {
@@ -6317,29 +6325,6 @@ SWIGINTERN PyObject *_wrap_PreviewImage_copy(PyObject *self, PyObject *args) {
63176325
}
63186326

63196327

6320-
SWIGINTERN PyObject *_wrap_PreviewImage_pData(PyObject *self, PyObject *args) {
6321-
PyObject *resultobj = 0;
6322-
Exiv2::PreviewImage *arg1 = (Exiv2::PreviewImage *) 0 ;
6323-
void *argp1 = 0 ;
6324-
int res1 = 0 ;
6325-
Exiv2::byte *result = 0 ;
6326-
6327-
if (!SWIG_Python_UnpackTuple(args, "PreviewImage_pData", 0, 0, 0)) SWIG_fail;
6328-
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_Exiv2__PreviewImage, 0 | 0 );
6329-
if (!SWIG_IsOK(res1)) {
6330-
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "PreviewImage_pData" "', argument " "1"" of type '" "Exiv2::PreviewImage const *""'");
6331-
}
6332-
arg1 = reinterpret_cast< Exiv2::PreviewImage * >(argp1);
6333-
result = (Exiv2::byte *)((Exiv2::PreviewImage const *)arg1)->pData();
6334-
6335-
resultobj = PyMemoryView_FromMemory((char*)result, arg1->size(), PyBUF_READ);
6336-
6337-
return resultobj;
6338-
fail:
6339-
return NULL;
6340-
}
6341-
6342-
63436328
SWIGINTERN PyObject *_wrap_PreviewImage_size(PyObject *self, PyObject *args) {
63446329
PyObject *resultobj = 0;
63456330
Exiv2::PreviewImage *arg1 = (Exiv2::PreviewImage *) 0 ;
@@ -6555,6 +6540,60 @@ SWIGINTERN PyObject *_wrap_PreviewImage___len__(PyObject *self, PyObject *args)
65556540
}
65566541

65576542

6543+
SWIGINTERN PyObject *_wrap_PreviewImage_data(PyObject *self, PyObject *args) {
6544+
PyObject *resultobj = 0;
6545+
Exiv2::PreviewImage *arg1 = (Exiv2::PreviewImage *) 0 ;
6546+
void *argp1 = 0 ;
6547+
int res1 = 0 ;
6548+
Exiv2::byte *result = 0 ;
6549+
6550+
if (!SWIG_Python_UnpackTuple(args, "PreviewImage_data", 0, 0, 0)) SWIG_fail;
6551+
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_Exiv2__PreviewImage, 0 | 0 );
6552+
if (!SWIG_IsOK(res1)) {
6553+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "PreviewImage_data" "', argument " "1"" of type '" "Exiv2::PreviewImage *""'");
6554+
}
6555+
arg1 = reinterpret_cast< Exiv2::PreviewImage * >(argp1);
6556+
{
6557+
try {
6558+
result = (Exiv2::byte *)Exiv2_PreviewImage_data(arg1);
6559+
}
6560+
catch(std::exception const& e) {
6561+
_set_python_exception();
6562+
SWIG_fail;
6563+
}
6564+
}
6565+
6566+
resultobj = PyMemoryView_FromMemory((char*)result, arg1->size(), PyBUF_READ);
6567+
6568+
return resultobj;
6569+
fail:
6570+
return NULL;
6571+
}
6572+
6573+
6574+
SWIGINTERN PyObject *_wrap_PreviewImage_pData(PyObject *self, PyObject *args) {
6575+
PyObject *resultobj = 0;
6576+
Exiv2::PreviewImage *arg1 = (Exiv2::PreviewImage *) 0 ;
6577+
void *argp1 = 0 ;
6578+
int res1 = 0 ;
6579+
Exiv2::byte *result = 0 ;
6580+
6581+
if (!SWIG_Python_UnpackTuple(args, "PreviewImage_pData", 0, 0, 0)) SWIG_fail;
6582+
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_Exiv2__PreviewImage, 0 | 0 );
6583+
if (!SWIG_IsOK(res1)) {
6584+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "PreviewImage_pData" "', argument " "1"" of type '" "Exiv2::PreviewImage *""'");
6585+
}
6586+
arg1 = reinterpret_cast< Exiv2::PreviewImage * >(argp1);
6587+
result = (Exiv2::byte *)Exiv2_PreviewImage_pData(arg1);
6588+
6589+
resultobj = PyMemoryView_FromMemory((char*)result, arg1->size(), PyBUF_READ);
6590+
6591+
return resultobj;
6592+
fail:
6593+
return NULL;
6594+
}
6595+
6596+
65586597
SWIGPY_DESTRUCTOR_CLOSURE(_wrap_delete_PreviewImage) /* defines _wrap_delete_PreviewImage_destructor_closure */
65596598

65606599
SWIGPY_LENFUNC_CLOSURE(_wrap_PreviewImage___len__) /* defines _wrap_PreviewImage___len___lenfunc_closure */
@@ -7110,13 +7149,6 @@ SWIGINTERN PyMethodDef SwigPyBuiltin__Exiv2__PreviewImage_methods[] = {
71107149
"Return a copy of the preview image data. The caller owns\n"
71117150
" this copy and %DataBuf ensures that it will be deleted.\n"
71127151
"" },
7113-
{ "pData", _wrap_PreviewImage_pData, METH_NOARGS, "\n"
7114-
"Returns a temporary Python memoryview of the object's data.\n"
7115-
"\n"
7116-
"WARNING: do not resize or delete the object while using the view.\n"
7117-
"\n"
7118-
":rtype: memoryview\n"
7119-
"" },
71207152
{ "size", _wrap_PreviewImage_size, METH_NOARGS, "Return the size of the preview image in bytes." },
71217153
{ "writeFile", _wrap_PreviewImage_writeFile, METH_O, "\n"
71227154
"Write the thumbnail image to a file.\n"
@@ -7142,6 +7174,20 @@ SWIGINTERN PyMethodDef SwigPyBuiltin__Exiv2__PreviewImage_methods[] = {
71427174
{ "height", _wrap_PreviewImage_height, METH_NOARGS, "Return the height of the preview image in pixels." },
71437175
{ "id", _wrap_PreviewImage_id, METH_NOARGS, "Return the preview image type identifier." },
71447176
{ "__len__", _wrap_PreviewImage___len__, METH_NOARGS, "" },
7177+
{ "data", _wrap_PreviewImage_data, METH_NOARGS, "\n"
7178+
"Returns a temporary Python memoryview of the object's data.\n"
7179+
"\n"
7180+
"WARNING: do not resize or delete the object while using the view.\n"
7181+
"\n"
7182+
":rtype: memoryview\n"
7183+
"" },
7184+
{ "pData", _wrap_PreviewImage_pData, METH_NOARGS, "\n"
7185+
"Returns a temporary Python memoryview of the object's data.\n"
7186+
"\n"
7187+
"WARNING: do not resize or delete the object while using the view.\n"
7188+
"\n"
7189+
":rtype: memoryview\n"
7190+
"" },
71457191
{ NULL, NULL, 0, NULL } /* Sentinel */
71467192
};
71477193

tests/test_preview.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ def test_PreviewImage(self):
4949
self.assertEqual(len(preview), preview.size())
5050
copy = preview.copy()
5151
self.assertIsInstance(copy, exiv2.DataBuf)
52-
with preview.pData() as data:
52+
with self.assertWarns(DeprecationWarning):
53+
with preview.pData() as data:
54+
self.check_result(data, memoryview, copy)
55+
self.assertEqual(data[:10], b'\xff\xd8\xff\xe0\x00\x10JFIF')
56+
with preview.data() as data:
5357
self.check_result(data, memoryview, copy)
5458
self.assertEqual(data[:10], b'\xff\xd8\xff\xe0\x00\x10JFIF')
5559
self.assertEqual(memoryview(preview), copy)

0 commit comments

Comments
 (0)