Skip to content

Commit b320f11

Browse files
Moved DataContext mmap and open to constructor
1 parent b2f1b03 commit b320f11

File tree

3 files changed

+43
-70
lines changed

3 files changed

+43
-70
lines changed

src/interface/basicio.i

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ UNIQUE_PTR(Exiv2::BasicIo);
4949

5050
// Potentially blocking calls allow Python threads
5151
%thread Exiv2::BasicIo::close;
52+
%thread Exiv2::BasicIo::data;
5253
%thread Exiv2::BasicIo::open;
5354
%thread Exiv2::BasicIo::mmap;
5455
%thread Exiv2::BasicIo::munmap;
@@ -175,43 +176,33 @@ A simple context manager for *mmap* / *munmap* data access. The
175176
*__enter__* method returns a :py:class:`memoryview` of the data."
176177
%ignore DataContext::DataContext;
177178
%thread DataContext::~DataContext;
178-
%thread DataContext::__exit__;
179179
%inline %{
180180
class DataContext {
181181
private:
182182
Exiv2::BasicIo* parent;
183183
bool isWriteable;
184-
bool mapped;
184+
char* ptr;
185185
public:
186-
DataContext(Exiv2::BasicIo* parent, bool isWriteable) :
187-
parent(parent), isWriteable(isWriteable), mapped(false) {};
186+
DataContext(Exiv2::BasicIo* parent, bool isWriteable) {
187+
ptr = NULL;
188+
this->parent = parent;
189+
this->isWriteable = isWriteable;
190+
if (parent->open())
191+
throw std::runtime_error("BasicIo.open() failed");
192+
ptr = (char*)parent->mmap(isWriteable);
193+
};
188194
~DataContext() {
189-
if (mapped) {
195+
if (ptr) {
190196
parent->munmap();
191197
parent->close();
192198
}
193199
};
194200
PyObject* __enter__() {
195-
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
196-
int error = parent->open();
197-
if (error) {
198-
SWIG_PYTHON_THREAD_END_ALLOW;
199-
return PyErr_Format(
200-
PyExc_RuntimeError, "open() returned %d", error);
201-
}
202-
char* ptr = (char*)parent->mmap(isWriteable);
203-
SWIG_PYTHON_THREAD_END_ALLOW;
204-
mapped = true;
205201
return PyMemoryView_FromMemory(
206202
ptr, ptr ? parent->size() : 0,
207203
isWriteable ? PyBUF_WRITE : PyBUF_READ);
208204
};
209205
bool __exit__(PyObject* exc_type, PyObject* exc_val, PyObject* exc_tb) {
210-
if (mapped) {
211-
parent->munmap();
212-
mapped = false;
213-
parent->close();
214-
}
215206
return false;
216207
};
217208
};

src/swig-0_27_7/basicio_wrap.cxx

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,37 +4428,28 @@ class DataContext {
44284428
private:
44294429
Exiv2::BasicIo* parent;
44304430
bool isWriteable;
4431-
bool mapped;
4431+
char* ptr;
44324432
public:
4433-
DataContext(Exiv2::BasicIo* parent, bool isWriteable) :
4434-
parent(parent), isWriteable(isWriteable), mapped(false) {};
4433+
DataContext(Exiv2::BasicIo* parent, bool isWriteable) {
4434+
ptr = NULL;
4435+
this->parent = parent;
4436+
this->isWriteable = isWriteable;
4437+
if (parent->open())
4438+
throw std::runtime_error("BasicIo.open() failed");
4439+
ptr = (char*)parent->mmap(isWriteable);
4440+
};
44354441
~DataContext() {
4436-
if (mapped) {
4442+
if (ptr) {
44374443
parent->munmap();
44384444
parent->close();
44394445
}
44404446
};
44414447
PyObject* __enter__() {
4442-
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
4443-
int error = parent->open();
4444-
if (error) {
4445-
SWIG_PYTHON_THREAD_END_ALLOW;
4446-
return PyErr_Format(
4447-
PyExc_RuntimeError, "open() returned %d", error);
4448-
}
4449-
char* ptr = (char*)parent->mmap(isWriteable);
4450-
SWIG_PYTHON_THREAD_END_ALLOW;
4451-
mapped = true;
44524448
return PyMemoryView_FromMemory(
44534449
ptr, ptr ? parent->size() : 0,
44544450
isWriteable ? PyBUF_WRITE : PyBUF_READ);
44554451
};
44564452
bool __exit__(PyObject* exc_type, PyObject* exc_val, PyObject* exc_tb) {
4457-
if (mapped) {
4458-
parent->munmap();
4459-
mapped = false;
4460-
parent->close();
4461-
}
44624453
return false;
44634454
};
44644455
};
@@ -4928,11 +4919,7 @@ SWIGINTERN PyObject *_wrap_DataContext___exit__(PyObject *self, PyObject *args)
49284919
arg4 = obj3;
49294920
{
49304921
try {
4931-
{
4932-
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
4933-
result = (bool)(arg1)->__exit__(arg2,arg3,arg4);
4934-
SWIG_PYTHON_THREAD_END_ALLOW;
4935-
}
4922+
result = (bool)(arg1)->__exit__(arg2,arg3,arg4);
49364923
}
49374924
catch(std::exception const& e) {
49384925
_set_python_exception();
@@ -5798,7 +5785,11 @@ SWIGINTERN PyObject *_wrap_BasicIo_data(PyObject *self, PyObject *args) {
57985785
}
57995786
{
58005787
try {
5801-
result = (DataContext *)Exiv2_BasicIo_data(arg1,arg2);
5788+
{
5789+
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
5790+
result = (DataContext *)Exiv2_BasicIo_data(arg1,arg2);
5791+
SWIG_PYTHON_THREAD_END_ALLOW;
5792+
}
58025793
}
58035794
catch(std::exception const& e) {
58045795
_set_python_exception();

src/swig-0_28_5/basicio_wrap.cxx

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,37 +4429,28 @@ class DataContext {
44294429
private:
44304430
Exiv2::BasicIo* parent;
44314431
bool isWriteable;
4432-
bool mapped;
4432+
char* ptr;
44334433
public:
4434-
DataContext(Exiv2::BasicIo* parent, bool isWriteable) :
4435-
parent(parent), isWriteable(isWriteable), mapped(false) {};
4434+
DataContext(Exiv2::BasicIo* parent, bool isWriteable) {
4435+
ptr = NULL;
4436+
this->parent = parent;
4437+
this->isWriteable = isWriteable;
4438+
if (parent->open())
4439+
throw std::runtime_error("BasicIo.open() failed");
4440+
ptr = (char*)parent->mmap(isWriteable);
4441+
};
44364442
~DataContext() {
4437-
if (mapped) {
4443+
if (ptr) {
44384444
parent->munmap();
44394445
parent->close();
44404446
}
44414447
};
44424448
PyObject* __enter__() {
4443-
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
4444-
int error = parent->open();
4445-
if (error) {
4446-
SWIG_PYTHON_THREAD_END_ALLOW;
4447-
return PyErr_Format(
4448-
PyExc_RuntimeError, "open() returned %d", error);
4449-
}
4450-
char* ptr = (char*)parent->mmap(isWriteable);
4451-
SWIG_PYTHON_THREAD_END_ALLOW;
4452-
mapped = true;
44534449
return PyMemoryView_FromMemory(
44544450
ptr, ptr ? parent->size() : 0,
44554451
isWriteable ? PyBUF_WRITE : PyBUF_READ);
44564452
};
44574453
bool __exit__(PyObject* exc_type, PyObject* exc_val, PyObject* exc_tb) {
4458-
if (mapped) {
4459-
parent->munmap();
4460-
mapped = false;
4461-
parent->close();
4462-
}
44634454
return false;
44644455
};
44654456
};
@@ -5033,11 +5024,7 @@ SWIGINTERN PyObject *_wrap_DataContext___exit__(PyObject *self, PyObject *args)
50335024
arg4 = obj3;
50345025
{
50355026
try {
5036-
{
5037-
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
5038-
result = (bool)(arg1)->__exit__(arg2,arg3,arg4);
5039-
SWIG_PYTHON_THREAD_END_ALLOW;
5040-
}
5027+
result = (bool)(arg1)->__exit__(arg2,arg3,arg4);
50415028
}
50425029
catch(std::exception const& e) {
50435030
_set_python_exception();
@@ -6066,7 +6053,11 @@ SWIGINTERN PyObject *_wrap_BasicIo_data(PyObject *self, PyObject *args) {
60666053
}
60676054
{
60686055
try {
6069-
result = (DataContext *)Exiv2_BasicIo_data(arg1,arg2);
6056+
{
6057+
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
6058+
result = (DataContext *)Exiv2_BasicIo_data(arg1,arg2);
6059+
SWIG_PYTHON_THREAD_END_ALLOW;
6060+
}
60706061
}
60716062
catch(std::exception const& e) {
60726063
_set_python_exception();

0 commit comments

Comments
 (0)