Add copy ctor for wxPyCBInputStream and use it to prevent double

deletion of the wxInputStream passed to wxFSFile


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32589 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn 2005-03-03 19:56:42 +00:00
parent c8547e17ca
commit d02ea46cad
7 changed files with 74 additions and 39 deletions

View File

@ -74,6 +74,8 @@ public:
// factory function // factory function
static wxPyCBInputStream* create(PyObject *py, bool block=true); static wxPyCBInputStream* create(PyObject *py, bool block=true);
wxPyCBInputStream(const wxPyCBInputStream& other);
protected: protected:
// can only be created via the factory // can only be created via the factory
wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block); wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block);

View File

@ -111,6 +111,7 @@ inline wxPyCoreAPI* wxPyGetCoreAPIPtr()
#define wxPyOORClientData_dtor(a) (wxPyGetCoreAPIPtr()->p_wxPyOORClientData_dtor(a)) #define wxPyOORClientData_dtor(a) (wxPyGetCoreAPIPtr()->p_wxPyOORClientData_dtor(a))
#define wxPyCBInputStream_create(a, b) (wxPyGetCoreAPIPtr()->p_wxPyCBInputStream_create(a, b)) #define wxPyCBInputStream_create(a, b) (wxPyGetCoreAPIPtr()->p_wxPyCBInputStream_create(a, b))
#define wxPyCBInputStream_copy(a) (wxPyGetCoreAPIPtr()->p_wxPyCBInputStream_copy(a))
#define wxPyInstance_Check(a) (wxPyGetCoreAPIPtr()->p_wxPyInstance_Check(a)) #define wxPyInstance_Check(a) (wxPyGetCoreAPIPtr()->p_wxPyInstance_Check(a))
#define wxPySwigInstance_Check(a) (wxPyGetCoreAPIPtr()->p_wxPySwigInstance_Check(a)) #define wxPySwigInstance_Check(a) (wxPyGetCoreAPIPtr()->p_wxPySwigInstance_Check(a))

View File

@ -323,7 +323,7 @@ void wxPyClientData_dtor(wxPyClientData* self);
void wxPyUserData_dtor(wxPyUserData* self); void wxPyUserData_dtor(wxPyUserData* self);
void wxPyOORClientData_dtor(wxPyOORClientData* self); void wxPyOORClientData_dtor(wxPyOORClientData* self);
wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block); wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block);
wxPyCBInputStream* wxPyCBInputStream_copy(wxPyCBInputStream* other);
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Export a C API in a struct. Other modules will be able to load this from // Export a C API in a struct. Other modules will be able to load this from
@ -389,7 +389,8 @@ struct wxPyCoreAPI {
void (*p_wxPyOORClientData_dtor)(wxPyOORClientData*); void (*p_wxPyOORClientData_dtor)(wxPyOORClientData*);
wxPyCBInputStream* (*p_wxPyCBInputStream_create)(PyObject *py, bool block); wxPyCBInputStream* (*p_wxPyCBInputStream_create)(PyObject *py, bool block);
wxPyCBInputStream* (*p_wxPyCBInputStream_copy)(wxPyCBInputStream* other);
bool (*p_wxPyInstance_Check)(PyObject* obj); bool (*p_wxPyInstance_Check)(PyObject* obj);
bool (*p_wxPySwigInstance_Check)(PyObject* obj); bool (*p_wxPySwigInstance_Check)(PyObject* obj);

View File

@ -183,7 +183,8 @@ static wxPyCoreAPI API = {
wxPyOORClientData_dtor, wxPyOORClientData_dtor,
wxPyCBInputStream_create, wxPyCBInputStream_create,
wxPyCBInputStream_copy,
wxPyInstance_Check, wxPyInstance_Check,
wxPySwigInstance_Check, wxPySwigInstance_Check,

View File

@ -27,8 +27,8 @@ class wxFSFile : public wxObject
{ {
public: public:
%pythonAppend wxFSFile %pythonAppend wxFSFile
"self.thisown = 0 # It will normally be deleted by the user of the wxFileSystem"; "self.thisown = 0 # It will normally be deleted by the user of the wx.FileSystem";
wxFSFile(wxInputStream *stream, const wxString& loc, wxFSFile(wxInputStream *stream, const wxString& loc,
const wxString& mimetype, const wxString& anchor, const wxString& mimetype, const wxString& anchor,
wxDateTime modif); wxDateTime modif);

View File

@ -23,8 +23,15 @@
%newgroup %newgroup
// typemaps for wxInputStream // Typemaps for wxInputStream
%typemap(in) wxInputStream* (wxPyInputStream* temp, bool created) { //
// We assume that input params taking a wxInputStream& will *not* take
// ownership of the stream and so we manage it in the typemaps. On the other
// hand, when a paramter expects a wxInputStream* then it does take ownership
// (such as wxFSFile) and so the typemap will make a copy of the stream object
// to give to it.
%typemap(in) wxInputStream& (wxPyInputStream* temp, bool created) {
if (wxPyConvertSwigPtr($input, (void **)&temp, wxT("wxPyInputStream"))) { if (wxPyConvertSwigPtr($input, (void **)&temp, wxT("wxPyInputStream"))) {
$1 = temp->m_wxis; $1 = temp->m_wxis;
created = false; created = false;
@ -32,21 +39,29 @@
PyErr_Clear(); // clear the failure of the wxPyConvert above PyErr_Clear(); // clear the failure of the wxPyConvert above
$1 = wxPyCBInputStream_create($input, false); $1 = wxPyCBInputStream_create($input, false);
if ($1 == NULL) { if ($1 == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected wxInputStream or Python file-like object."); PyErr_SetString(PyExc_TypeError, "Expected wx.InputStream or Python file-like object.");
SWIG_fail; SWIG_fail;
} }
created = true; created = true;
} }
} }
%typemap(freearg) wxInputStream* { %typemap(freearg) wxInputStream& { if (created$argnum) delete $1; }
if (created$argnum)
delete $1;
%typemap(in) wxInputStream* (wxPyInputStream* temp) {
if (wxPyConvertSwigPtr($input, (void **)&temp, wxT("wxPyInputStream"))) {
$1 = wxPyCBInputStream_copy((wxPyCBInputStream*)temp->m_wxis);
} else {
PyErr_Clear(); // clear the failure of the wxPyConvert above
$1 = wxPyCBInputStream_create($input, true);
if ($1 == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected wx.InputStream or Python file-like object.");
SWIG_fail;
}
}
} }
%typemap(in) wxInputStream& = wxInputStream*;
%typemap(freearg) wxInputStream& = wxInputStream*;
%typemap(out) wxInputStream* { %typemap(out) wxInputStream* {
wxPyInputStream * _ptr = NULL; wxPyInputStream * _ptr = NULL;

View File

@ -387,10 +387,10 @@ void wxPyApp::_BootstrapApp()
PyObject* retval = NULL; PyObject* retval = NULL;
PyObject* pyint = NULL; PyObject* pyint = NULL;
// Only initialize wxWidgets once // Only initialize wxWidgets once
if (! haveInitialized) { if (! haveInitialized) {
// Get any command-line args passed to this program from the sys module // Get any command-line args passed to this program from the sys module
int argc = 0; int argc = 0;
char** argv = NULL; char** argv = NULL;
@ -429,7 +429,7 @@ void wxPyApp::_BootstrapApp()
#if defined(__WXGTK__) && PY_VERSION_HEX < 0x02040000 #if defined(__WXGTK__) && PY_VERSION_HEX < 0x02040000
setlocale(LC_NUMERIC, "C"); setlocale(LC_NUMERIC, "C");
#endif #endif
// The stock objects were all NULL when they were loaded into // The stock objects were all NULL when they were loaded into
// SWIG generated proxies, so re-init those now... // SWIG generated proxies, so re-init those now...
wxPy_ReinitStockObjects(3); wxPy_ReinitStockObjects(3);
@ -437,7 +437,7 @@ void wxPyApp::_BootstrapApp()
wxPyEndBlockThreads(blocked); wxPyEndBlockThreads(blocked);
haveInitialized = true; haveInitialized = true;
} }
// It's now ok to generate exceptions for assertion errors. // It's now ok to generate exceptions for assertion errors.
wxPythonApp->SetStartupComplete(true); wxPythonApp->SetStartupComplete(true);
@ -534,7 +534,7 @@ void __wxPyPreStart(PyObject* moduleDict)
#ifdef __WXMSW__ #ifdef __WXMSW__
// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
// | _CRTDBG_CHECK_ALWAYS_DF // | _CRTDBG_CHECK_ALWAYS_DF
// | _CRTDBG_DELAY_FREE_MEM_DF // | _CRTDBG_DELAY_FREE_MEM_DF
// ); // );
#endif #endif
@ -602,7 +602,7 @@ PyObject* __wxPySetDictionary(PyObject* /* self */, PyObject* args)
wxPyNoAppError = PyErr_NewException("wx._core.PyNoAppError", wxPyNoAppError = PyErr_NewException("wx._core.PyNoAppError",
PyExc_RuntimeError, NULL); PyExc_RuntimeError, NULL);
PyDict_SetItemString(wxPython_dict, "PyNoAppError", wxPyNoAppError); PyDict_SetItemString(wxPython_dict, "PyNoAppError", wxPyNoAppError);
#ifdef __WXMOTIF__ #ifdef __WXMOTIF__
@ -665,13 +665,13 @@ PyObject* __wxPySetDictionary(PyObject* /* self */, PyObject* args)
#else #else
_AddInfoString("wx-assertions-off"); _AddInfoString("wx-assertions-off");
#endif #endif
#undef _AddInfoString #undef _AddInfoString
PyObject* PlatInfoTuple = PyList_AsTuple(PlatInfo); PyObject* PlatInfoTuple = PyList_AsTuple(PlatInfo);
Py_DECREF(PlatInfo); Py_DECREF(PlatInfo);
PyDict_SetItemString(wxPython_dict, "PlatformInfo", PlatInfoTuple); PyDict_SetItemString(wxPython_dict, "PlatformInfo", PlatInfoTuple);
RETURN_NONE(); RETURN_NONE();
} }
@ -785,7 +785,7 @@ void wxPy_ReinitStockObjects(int pass)
if (pass == 1) { } \ if (pass == 1) { } \
else if (pass == 2) { rsoPass2(#name); } \ else if (pass == 2) { rsoPass2(#name); } \
else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); } else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); }
REINITOBJ(wxNORMAL_FONT, wxFont); REINITOBJ(wxNORMAL_FONT, wxFont);
REINITOBJ(wxSMALL_FONT, wxFont); REINITOBJ(wxSMALL_FONT, wxFont);
@ -1099,7 +1099,7 @@ void wxPySaveThreadState(PyThreadState* tstate) {
// (i.e. ActiveX controls) will (incorrectly IMHO) use a transient // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
// tstate which will then be garbage the next time we try to use // tstate which will then be garbage the next time we try to use
// it... // it...
wxPyTMutex->Unlock(); wxPyTMutex->Unlock();
return; return;
} }
@ -1140,7 +1140,7 @@ void wxPyEndAllowThreads(PyThreadState* saved) {
bool wxPyBeginBlockThreads() { bool wxPyBeginBlockThreads() {
#ifdef WXP_WITH_THREAD #ifdef WXP_WITH_THREAD
// This works in for 2.3, maybe a good alternative to find the needed tstate? // This works in for 2.3, maybe a good alternative to find the needed tstate?
// PyThreadState *check = PyGILState_GetThisThreadState(); // PyThreadState *check = PyGILState_GetThisThreadState();
PyThreadState *current = _PyThreadState_Current; PyThreadState *current = _PyThreadState_Current;
@ -1287,7 +1287,7 @@ PyObject* wxPyInputStream::readlines(int sizehint) {
bool blocked = wxPyBeginBlockThreads(); bool blocked = wxPyBeginBlockThreads();
pylist = PyList_New(0); pylist = PyList_New(0);
wxPyEndBlockThreads(blocked); wxPyEndBlockThreads(blocked);
if (!pylist) { if (!pylist) {
bool blocked = wxPyBeginBlockThreads(); bool blocked = wxPyBeginBlockThreads();
PyErr_NoMemory(); PyErr_NoMemory();
@ -1343,6 +1343,17 @@ wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool
: wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block) : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block)
{} {}
wxPyCBInputStream::wxPyCBInputStream(const wxPyCBInputStream& other)
{
m_read = other.m_read;
m_seek = other.m_seek;
m_tell = other.m_tell;
m_block = other.m_block;
Py_INCREF(m_read);
Py_INCREF(m_seek);
Py_INCREF(m_tell);
}
wxPyCBInputStream::~wxPyCBInputStream() { wxPyCBInputStream::~wxPyCBInputStream() {
bool blocked=false; bool blocked=false;
@ -1380,6 +1391,10 @@ wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block) {
return wxPyCBInputStream::create(py, block); return wxPyCBInputStream::create(py, block);
} }
wxPyCBInputStream* wxPyCBInputStream_copy(wxPyCBInputStream* other) {
return new wxPyCBInputStream(*other);
}
PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) { PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) {
if (!PyObject_HasAttrString(py, name)) if (!PyObject_HasAttrString(py, name))
return NULL; return NULL;
@ -1449,7 +1464,7 @@ wxFileOffset wxPyCBInputStream::OnSysSeek(wxFileOffset off, wxSeekMode mode) {
PyTuple_SET_ITEM(arglist, 1, PyInt_FromLong(mode)); PyTuple_SET_ITEM(arglist, 1, PyInt_FromLong(mode));
PyObject* result = PyEval_CallObject(m_seek, arglist); PyObject* result = PyEval_CallObject(m_seek, arglist);
Py_DECREF(arglist); Py_DECREF(arglist);
Py_XDECREF(result); Py_XDECREF(result);
@ -1535,7 +1550,7 @@ void wxPyCallback::EventThunker(wxEvent& event) {
s_preName = PyString_FromString(wxPy_PRECALLINIT); s_preName = PyString_FromString(wxPy_PRECALLINIT);
s_postName = PyString_FromString(wxPy_POSTCALLCLEANUP); s_postName = PyString_FromString(wxPy_POSTCALLCLEANUP);
} }
// Check if the event object needs some preinitialization // Check if the event object needs some preinitialization
if (PyObject_HasAttr(arg, s_preName)) { if (PyObject_HasAttr(arg, s_preName)) {
result = PyObject_CallMethodObjArgs(arg, s_preName, arg, NULL); result = PyObject_CallMethodObjArgs(arg, s_preName, arg, NULL);
@ -1544,9 +1559,9 @@ void wxPyCallback::EventThunker(wxEvent& event) {
PyErr_Clear(); // Just in case... PyErr_Clear(); // Just in case...
} else { } else {
PyErr_Print(); PyErr_Print();
} }
} }
// Call the event handler, passing the event object // Call the event handler, passing the event object
tuple = PyTuple_New(1); tuple = PyTuple_New(1);
PyTuple_SET_ITEM(tuple, 0, arg); // steals ref to arg PyTuple_SET_ITEM(tuple, 0, arg); // steals ref to arg
@ -1566,7 +1581,7 @@ void wxPyCallback::EventThunker(wxEvent& event) {
PyErr_Clear(); // Just in case... PyErr_Clear(); // Just in case...
} else { } else {
PyErr_Print(); PyErr_Print();
} }
} }
if ( checkSkip ) { if ( checkSkip ) {
@ -1886,7 +1901,7 @@ long wxPyGetWinHandle(wxWindow* win) {
#if defined(__WXGTK__) || defined(__WXX11) #if defined(__WXGTK__) || defined(__WXX11)
return (long)GetXWindow(win); return (long)GetXWindow(win);
#endif #endif
#ifdef __WXMAC__ #ifdef __WXMAC__
//return (long)MAC_WXHWND(win->MacGetTopLevelWindowRef()); //return (long)MAC_WXHWND(win->MacGetTopLevelWindowRef());
return (long)win->GetHandle(); return (long)win->GetHandle();
@ -1927,7 +1942,7 @@ wxString* wxString_in_helper(PyObject* source) {
if (PyUnicode_Check(source)) { if (PyUnicode_Check(source)) {
str = PyUnicode_AsEncodedString(source, wxPyDefaultEncoding, "strict"); str = PyUnicode_AsEncodedString(source, wxPyDefaultEncoding, "strict");
if (PyErr_Occurred()) return NULL; if (PyErr_Occurred()) return NULL;
} }
else if (!PyString_Check(source)) { else if (!PyString_Check(source)) {
str = PyObject_Str(source); str = PyObject_Str(source);
if (PyErr_Occurred()) return NULL; if (PyErr_Occurred()) return NULL;
@ -1935,11 +1950,11 @@ wxString* wxString_in_helper(PyObject* source) {
char* tmpPtr; int tmpSize; char* tmpPtr; int tmpSize;
PyString_AsStringAndSize(str, &tmpPtr, &tmpSize); PyString_AsStringAndSize(str, &tmpPtr, &tmpSize);
target = new wxString(tmpPtr, tmpSize); target = new wxString(tmpPtr, tmpSize);
if (!PyString_Check(source)) if (!PyString_Check(source))
Py_DECREF(str); Py_DECREF(str);
#endif // wxUSE_UNICODE #endif // wxUSE_UNICODE
return target; return target;
} }
@ -1955,13 +1970,13 @@ wxString Py2wxString(PyObject* source)
if (!PyUnicode_Check(source)) { if (!PyUnicode_Check(source)) {
uni = PyUnicode_FromEncodedObject(source, wxPyDefaultEncoding, "strict"); uni = PyUnicode_FromEncodedObject(source, wxPyDefaultEncoding, "strict");
if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear?
} }
size_t len = PyUnicode_GET_SIZE(uni); size_t len = PyUnicode_GET_SIZE(uni);
if (len) { if (len) {
PyUnicode_AsWideChar((PyUnicodeObject*)uni, target.GetWriteBuf(len), len); PyUnicode_AsWideChar((PyUnicodeObject*)uni, target.GetWriteBuf(len), len);
target.UngetWriteBuf(); target.UngetWriteBuf();
} }
if (!PyUnicode_Check(source)) if (!PyUnicode_Check(source))
Py_DECREF(uni); Py_DECREF(uni);
#else #else
@ -1970,7 +1985,7 @@ wxString Py2wxString(PyObject* source)
if (PyUnicode_Check(source)) { if (PyUnicode_Check(source)) {
str = PyUnicode_AsEncodedString(source, wxPyDefaultEncoding, "strict"); str = PyUnicode_AsEncodedString(source, wxPyDefaultEncoding, "strict");
if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear?
} }
else if (!PyString_Check(source)) { else if (!PyString_Check(source)) {
str = PyObject_Str(source); str = PyObject_Str(source);
if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear?
@ -1978,7 +1993,7 @@ wxString Py2wxString(PyObject* source)
char* tmpPtr; int tmpSize; char* tmpPtr; int tmpSize;
PyString_AsStringAndSize(str, &tmpPtr, &tmpSize); PyString_AsStringAndSize(str, &tmpPtr, &tmpSize);
target = wxString(tmpPtr, tmpSize); target = wxString(tmpPtr, tmpSize);
if (!PyString_Check(source)) if (!PyString_Check(source))
Py_DECREF(str); Py_DECREF(str);
#endif // wxUSE_UNICODE #endif // wxUSE_UNICODE