@@ -146,7 +146,7 @@ RETURN_VIEW_CB(Exiv2::byte* mmap, $1 ? arg1->size() : 0,
146146 };
147147}
148148
149- // Release memoryviews when mmap is called
149+ // Release memoryviews when mmap or data is called
150150%typemap(check, fragment=" memoryview_funcs" ) bool isWriteable {
151151 release_views (self);
152152}
@@ -157,6 +157,18 @@ RETURN_VIEW_CB(Exiv2::byte* mmap, $1 ? arg1->size() : 0,
157157 release_views (self);
158158%}
159159
160+ // Add data() method for easy access
161+ RETURN_VIEW_CB (Exiv2::byte* data, $1 ? arg1->size () : 0,
162+ arg2 ? PyBUF_WRITE : PyBUF_READ, self,)
163+ %extend Exiv2::BasicIo {
164+ Exiv2::byte* data (bool isWriteable) {
165+ if (!self->isopen ())
166+ self->open ();
167+ self->munmap ();
168+ return self->mmap (isWriteable);
169+ };
170+ }
171+
160172// Enable len(Exiv2::BasicIo)
161173%feature(" python:slot" , " sq_length" , functype=" lenfunc" )
162174 Exiv2::BasicIo::size;
@@ -191,64 +203,6 @@ static void release_ptr(Exiv2::BasicIo* self) {
191203}
192204EXPOSE_OBJECT_BUFFER (Exiv2::BasicIo, true , true )
193205
194- // Wrapper class to provide a context manager for Exiv2::BasicIo::mmap
195- %feature (" docstring" ) DataContext " Data context manager.
196-
197- A simple context manager for *mmap* / *munmap* data access. The
198- *__enter__* method returns a :py:class:`memoryview` of the data."
199- %ignore DataContext::DataContext;
200- %thread DataContext::~DataContext;
201- %inline %{
202- class DataContext {
203- private:
204- Exiv2::BasicIo* parent;
205- bool isWriteable;
206- char * ptr;
207- public:
208- DataContext (Exiv2::BasicIo* parent, bool isWriteable) {
209- ptr = NULL ;
210- this ->parent = parent;
211- this ->isWriteable = isWriteable;
212- if (parent->open ())
213- throw std::runtime_error (" BasicIo.open() failed" );
214- ptr = (char *)parent->mmap (isWriteable);
215- };
216- ~DataContext () {
217- if (ptr) {
218- parent->munmap ();
219- parent->close ();
220- }
221- };
222- PyObject* __enter__ () {
223- return PyMemoryView_FromMemory (
224- ptr, ptr ? parent->size () : 0 ,
225- isWriteable ? PyBUF_WRITE : PyBUF_READ);
226- };
227- bool __exit__ (PyObject* exc_type, PyObject* exc_val, PyObject* exc_tb) {
228- return false ;
229- };
230- };
231- %}
232- KEEP_REFERENCE (DataContext*);
233-
234- // Add Exiv2::BasicIo::data() method to return a context manager
235- %feature (" docstring" ) Exiv2::BasicIo::data " Return a data context manager.
236-
237- This allows easy access to the data using a ``with`` statement.
238- The context manager calls *mmap* when the context is entered and
239- *munmap* when the context is exited.
240- :type isWriteable: bool, optional
241- :param isWriteable: Set to true if the data should be writeable
242- (default is false).
243- :rtype: object
244- :return: A context manager"
245- %newobject Exiv2::BasicIo::data;
246- %extend Exiv2::BasicIo {
247- DataContext* data (bool isWriteable) {
248- return new DataContext ($self, isWriteable);
249- }
250- }
251-
252206// Make enum more Pythonic
253207DEFINE_CLASS_ENUM (BasicIo, Position, " Seek starting positions." ,
254208 " beg" , Exiv2::BasicIo::beg,
0 commit comments