From f74ff5efa5604fcde3cc5ba9009cb015a6f388cf Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sat, 23 Feb 2002 19:50:01 +0000 Subject: [PATCH] Finished wrapping and providing typemaps for wxInputStream and also added the stream ctor and other methods for wxImage so images can now be loaded from any Python "file-like" object. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@14365 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/CHANGES.txt | 5 + wxPython/demo/Main.py | 1 + wxPython/setup.py | 2 +- wxPython/src/filesys.i | 52 +----- wxPython/src/helpers.cpp | 285 +++++++++++++++++++++++++++++ wxPython/src/helpers.h | 41 ++++- wxPython/src/image.i | 25 +++ wxPython/src/msw/filesys.cpp | 22 ++- wxPython/src/msw/image.cpp | 303 +++++++++++++++++++++++++++++++ wxPython/src/msw/image.py | 20 ++ wxPython/src/msw/streams.cpp | 303 +------------------------------ wxPython/src/streams.i | 343 ++--------------------------------- 12 files changed, 714 insertions(+), 688 deletions(-) diff --git a/wxPython/CHANGES.txt b/wxPython/CHANGES.txt index 110a9a021b..09a4c76094 100644 --- a/wxPython/CHANGES.txt +++ b/wxPython/CHANGES.txt @@ -14,6 +14,11 @@ The calltip window and autocomplete window in wxSTC will now use a wxPopupWindow if available so they can extend beyond the client area of the STC if needed. +Finished wrapping and providing typemaps for wxInputStream and also +added the stream ctor and other methods for wxImage so images can now +be loaded from any Python "file-like" object. + + 2.3.2.1 diff --git a/wxPython/demo/Main.py b/wxPython/demo/Main.py index 87fc85543c..4bda0df3c8 100644 --- a/wxPython/demo/Main.py +++ b/wxPython/demo/Main.py @@ -22,6 +22,7 @@ import images _treeList = [ ('New since last release', ['wxGenericDirCtrl', + 'wxImageFromStream', ]), ('Windows', ['wxFrame', 'wxDialog', 'wxMiniFrame', diff --git a/wxPython/setup.py b/wxPython/setup.py index f2cd352d0a..4d4f531992 100755 --- a/wxPython/setup.py +++ b/wxPython/setup.py @@ -13,7 +13,7 @@ from my_distutils import run_swig, contrib_copy_tree # flags and values that affect this script #---------------------------------------------------------------------- -VERSION = "2.3.3rc" +VERSION = "2.3.3pre" DESCRIPTION = "Cross platform GUI toolkit for Python" AUTHOR = "Robin Dunn" AUTHOR_EMAIL = "Robin Dunn " diff --git a/wxPython/src/filesys.i b/wxPython/src/filesys.i index a37feddb60..9380577a21 100644 --- a/wxPython/src/filesys.i +++ b/wxPython/src/filesys.i @@ -2,7 +2,7 @@ // Name: filesys.i // Purpose: SWIG definitions of the wxFileSystem family of classes // -// Author: Joerg Baumann +// Author: Joerg Baumann and Robin Dunn // // Created: 25-Sept-2000 // RCS-ID: $Id$ @@ -36,56 +36,6 @@ //--------------------------------------------------------------------------- -// // typemaps for wxInputStream: Note wxFSFile object has to do the delete -// // of wxInputStream * -// %typemap(python,in) wxInputStream *stream { -// if (PyInstance_Check($source)) { -// wxPyInputStream* ptr; -// if (SWIG_GetPtrObj($source, (void **) &ptr,"_wxPyInputStream_p")) { -// PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p."); -// return NULL; -// } -// $target = ptr->wxi; -// } else { -// PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p."); -// return NULL; -// } -// } - - -// // typemaps for wxInputStream: Note wxFSFile object has to do the delete -// // of wxInputStream * -// %typemap(python,out) wxInputStream* { -// wxPyInputStream * _ptr = NULL; - -// if ($source) { -// _ptr = new wxPyInputStream($source); -// } -// if (_ptr) { -// char swigptr[64]; -// SWIG_MakePtr(swigptr, _ptr, "_wxPyInputStream_p"); - -// PyObject* classobj = PyDict_GetItemString(wxPython_dict, "wxInputStreamPtr"); -// if (! classobj) { -// Py_INCREF(Py_None); -// $target = Py_None; -// } else { -// PyObject* arg = Py_BuildValue("(s)", swigptr); -// $target = PyInstance_New(classobj, arg, NULL); -// Py_DECREF(arg); - -// // set ThisOwn -// PyObject* one = PyInt_FromLong(1); -// PyObject_SetAttrString($target, "thisown", one); -// Py_DECREF(one); -// } -// } else { -// Py_INCREF(Py_None); -// $target = Py_None; -// } -// } - - class wxFSFile : public wxObject { public: diff --git a/wxPython/src/helpers.cpp b/wxPython/src/helpers.cpp index 340c740037..065e70e0ba 100644 --- a/wxPython/src/helpers.cpp +++ b/wxPython/src/helpers.cpp @@ -537,6 +537,291 @@ void wxPyEndBlockThreads() { //--------------------------------------------------------------------------- +// wxPyInputStream and wxPyCBInputStream methods + +#include +WX_DEFINE_LIST(wxStringPtrList); + + +void wxPyInputStream::close() { + /* do nothing */ +} + +void wxPyInputStream::flush() { + /* do nothing */ +} + +bool wxPyInputStream::eof() { + if (m_wxis) + return m_wxis->Eof(); + else + return TRUE; +} + +wxPyInputStream::~wxPyInputStream() { + /* do nothing */ +} + +wxString* wxPyInputStream::read(int size) { + wxString* s = NULL; + const int BUFSIZE = 1024; + + // check if we have a real wxInputStream to work with + if (!m_wxis) { + PyErr_SetString(PyExc_IOError, "no valid C-wxInputStream"); + return NULL; + } + + if (size < 0) { + // init buffers + char * buf = new char[BUFSIZE]; + if (!buf) { + PyErr_NoMemory(); + return NULL; + } + + s = new wxString(); + if (!s) { + delete buf; + PyErr_NoMemory(); + return NULL; + } + + // read until EOF + while (! m_wxis->Eof()) { + m_wxis->Read(buf, BUFSIZE); + s->Append(buf, m_wxis->LastRead()); + } + delete buf; + + // error check + if (m_wxis->LastError() == wxSTREAM_READ_ERROR) { + delete s; + PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); + return NULL; + } + + } else { // Read only size number of characters + s = new wxString; + if (!s) { + PyErr_NoMemory(); + return NULL; + } + + // read size bytes + m_wxis->Read(s->GetWriteBuf(size+1), size); + s->UngetWriteBuf(m_wxis->LastRead()); + + // error check + if (m_wxis->LastError() == wxSTREAM_READ_ERROR) { + delete s; + PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); + return NULL; + } + } + return s; +} + + +wxString* wxPyInputStream::readline (int size) { + // check if we have a real wxInputStream to work with + if (!m_wxis) { + PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream"); + return NULL; + } + + // init buffer + int i; + char ch; + wxString* s = new wxString; + if (!s) { + PyErr_NoMemory(); + return NULL; + } + + // read until \n or byte limit reached + for (i=ch=0; (ch != '\n') && (!m_wxis->Eof()) && ((size < 0) || (i < size)); i++) { + *s += ch = m_wxis->GetC(); + } + + // errorcheck + if (m_wxis->LastError() == wxSTREAM_READ_ERROR) { + delete s; + PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); + return NULL; + } + return s; +} + + +wxStringPtrList* wxPyInputStream::readlines (int sizehint) { + // check if we have a real wxInputStream to work with + if (!m_wxis) { + PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); + return NULL; + } + + // init list + wxStringPtrList* l = new wxStringPtrList(); + if (!l) { + PyErr_NoMemory(); + return NULL; + } + + // read sizehint bytes or until EOF + int i; + for (i=0; (!m_wxis->Eof()) && ((sizehint < 0) || (i < sizehint));) { + wxString* s = readline(); + if (s == NULL) { + l->DeleteContents(TRUE); + l->Clear(); + return NULL; + } + l->Append(s); + i = i + s->Length(); + } + + // error check + if (m_wxis->LastError() == wxSTREAM_READ_ERROR) { + l->DeleteContents(TRUE); + l->Clear(); + PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); + return NULL; + } + return l; +} + + +void wxPyInputStream::seek(int offset, int whence) { + if (m_wxis) + m_wxis->SeekI(offset, wxSeekMode(whence)); +} + +int wxPyInputStream::tell(){ + if (m_wxis) + return m_wxis->TellI(); + else return 0; +} + + + + +wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block) + : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block) +{} + + +wxPyCBInputStream::~wxPyCBInputStream() { + if (m_block) wxPyBeginBlockThreads(); + Py_XDECREF(m_read); + Py_XDECREF(m_seek); + Py_XDECREF(m_tell); + if (m_block) wxPyEndBlockThreads(); +} + + +wxPyCBInputStream* wxPyCBInputStream::create(PyObject *py, bool block) { + if (block) wxPyBeginBlockThreads(); + + PyObject* read = getMethod(py, "read"); + PyObject* seek = getMethod(py, "seek"); + PyObject* tell = getMethod(py, "tell"); + + if (!read) { + PyErr_SetString(PyExc_TypeError, "Not a file-like object"); + Py_XDECREF(read); + Py_XDECREF(seek); + Py_XDECREF(tell); + if (block) wxPyEndBlockThreads(); + return NULL; + } + + if (block) wxPyEndBlockThreads(); + return new wxPyCBInputStream(read, seek, tell, block); +} + +PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) { + if (!PyObject_HasAttrString(py, name)) + return NULL; + PyObject* o = PyObject_GetAttrString(py, name); + if (!PyMethod_Check(o) && !PyCFunction_Check(o)) { + Py_DECREF(o); + return NULL; + } + return o; +} + + +size_t wxPyCBInputStream::GetSize() const { + wxPyCBInputStream* self = (wxPyCBInputStream*)this; // cast off const + if (m_seek && m_tell) { + off_t temp = self->OnSysTell(); + off_t ret = self->OnSysSeek(0, wxFromEnd); + self->OnSysSeek(temp, wxFromStart); + return ret; + } + else + return 0; +} + + +size_t wxPyCBInputStream::OnSysRead(void *buffer, size_t bufsize) { + if (bufsize == 0) + return 0; + + wxPyBeginBlockThreads(); + PyObject* arglist = Py_BuildValue("(i)", bufsize); + PyObject* result = PyEval_CallObject(m_read, arglist); + Py_DECREF(arglist); + + size_t o = 0; + if ((result != NULL) && PyString_Check(result)) { // TODO: unicode? + o = PyString_Size(result); + if (o == 0) + m_lasterror = wxSTREAM_EOF; + if (o > bufsize) + o = bufsize; + memcpy((char*)buffer, PyString_AsString(result), o); + Py_DECREF(result); + + } + else + m_lasterror = wxSTREAM_READ_ERROR; + wxPyEndBlockThreads(); + m_lastcount = o; + return o; +} + +size_t wxPyCBInputStream::OnSysWrite(const void *buffer, size_t bufsize) { + m_lasterror = wxSTREAM_WRITE_ERROR; + return 0; +} + +off_t wxPyCBInputStream::OnSysSeek(off_t off, wxSeekMode mode) { + wxPyBeginBlockThreads(); + PyObject* arglist = Py_BuildValue("(ii)", off, mode); + PyObject* result = PyEval_CallObject(m_seek, arglist); + Py_DECREF(arglist); + Py_XDECREF(result); + wxPyEndBlockThreads(); + return OnSysTell(); +} + +off_t wxPyCBInputStream::OnSysTell() const { + wxPyBeginBlockThreads(); + PyObject* arglist = Py_BuildValue("()"); + PyObject* result = PyEval_CallObject(m_tell, arglist); + Py_DECREF(arglist); + off_t o = 0; + if (result != NULL) { + o = PyInt_AsLong(result); + Py_DECREF(result); + }; + wxPyEndBlockThreads(); + return o; +} + +//---------------------------------------------------------------------- IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject); diff --git a/wxPython/src/helpers.h b/wxPython/src/helpers.h index ffab121edf..5ae8cc62d8 100644 --- a/wxPython/src/helpers.h +++ b/wxPython/src/helpers.h @@ -77,7 +77,7 @@ void wxPyBeginBlockThreads(); void wxPyEndBlockThreads(); //---------------------------------------------------------------------- -// Handle wxInputStreams by Joerg Baumann +// Handling of wxInputStreams by Joerg Baumann // See stream.i for implementations // list class for return list of strings, e.g. readlines() @@ -85,14 +85,14 @@ WX_DECLARE_LIST(wxString, wxStringPtrList); // C++ class wxPyInputStream to act as base for python class wxInputStream -// Use it in python like a python file object +// You can use it in python like a python file object. class wxPyInputStream { public: // underlying wxInputStream - wxInputStream* wxi; + wxInputStream* m_wxis; public: - wxPyInputStream(wxInputStream* wxi_) : wxi(wxi_) {} + wxPyInputStream(wxInputStream* wxis) : m_wxis(wxis) {} ~wxPyInputStream(); // python file object interface for input files (most of it) @@ -104,7 +104,8 @@ public: wxStringPtrList* readlines(int sizehint=-1); void seek(int offset, int whence=0); int tell(); - /* + + /* do these later? bool isatty(); int fileno(); void truncate(int size=-1); @@ -114,6 +115,36 @@ public: }; + +// This is a wxInputStream that wraps a Python file-like +// object and calls the Python methods as needed. +class wxPyCBInputStream : public wxInputStream { +public: + ~wxPyCBInputStream(); + virtual size_t GetSize() const; + + // factory function + static wxPyCBInputStream* create(PyObject *py, bool block=TRUE); + +protected: + // can only be created via the factory + wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block); + + // wxStreamBase methods + virtual size_t OnSysRead(void *buffer, size_t bufsize); + virtual size_t OnSysWrite(const void *buffer, size_t bufsize); + virtual off_t OnSysSeek(off_t off, wxSeekMode mode); + virtual off_t OnSysTell() const; + + // helper + static PyObject* getMethod(PyObject* py, char* name); + + PyObject* m_read; + PyObject* m_seek; + PyObject* m_tell; + bool m_block; +}; + //---------------------------------------------------------------------- // These are helpers used by the typemaps diff --git a/wxPython/src/image.i b/wxPython/src/image.i index 93afd3115c..457887f7f1 100644 --- a/wxPython/src/image.i +++ b/wxPython/src/image.i @@ -27,6 +27,7 @@ %import _defs.i %import misc.i %import gdi.i +%import streams.i //--------------------------------------------------------------------------- @@ -138,6 +139,10 @@ public: bool SaveFile( const wxString& name, int type ); %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype ); + %name(CanReadStream) static bool CanRead( wxInputStream& stream ); + %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ); + %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ); + bool Ok(); int GetWidth(); int GetHeight(); @@ -209,6 +214,9 @@ public: %new wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype, int index = -1); %new wxImage* wxImageFromBitmap(const wxBitmap &bitmap); %new wxImage* wxImageFromData(int width, int height, unsigned char* data); +%new wxImage* wxImageFromStream(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1); +%new wxImage* wxImageFromStreamMime(wxInputStream& stream, const wxString& mimetype, int index = -1 ); + %{ wxImage* wxEmptyImage(int width=0, int height=0) { if (width == 0 && height == 0) @@ -217,14 +225,17 @@ public: return new wxImage(width, height); } + wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype, int index) { return new wxImage(name, mimetype, index); } + wxImage* wxImageFromBitmap(const wxBitmap &bitmap) { return new wxImage(bitmap); } + wxImage* wxImageFromData(int width, int height, unsigned char* data) { // Copy the source data so the wxImage can clean it up later unsigned char* copy = (unsigned char*)malloc(width*height*3); @@ -235,8 +246,22 @@ public: memcpy(copy, data, width*height*3); return new wxImage(width, height, copy, FALSE); } + + + wxImage* wxImageFromStream(wxInputStream& stream, + long type = wxBITMAP_TYPE_ANY, int index = -1) { + return new wxImage(stream, type, index); + } + + + wxImage* wxImageFromStreamMime(wxInputStream& stream, + const wxString& mimetype, int index = -1 ) { + return new wxImage(stream, mimetype, index); + } %} + + void wxInitAllImageHandlers(); diff --git a/wxPython/src/msw/filesys.cpp b/wxPython/src/msw/filesys.cpp index 722a809fbc..1a48238fae 100644 --- a/wxPython/src/msw/filesys.cpp +++ b/wxPython/src/msw/filesys.cpp @@ -329,6 +329,8 @@ static PyObject *_wrap_new_wxFSFile(PyObject *self, PyObject *args, PyObject *kw wxString * _arg2; wxString * _arg3; wxDateTime * _arg4; + wxPyInputStream * temp; + bool created; PyObject * _obj0 = 0; PyObject * _obj1 = 0; PyObject * _obj2 = 0; @@ -341,16 +343,16 @@ static PyObject *_wrap_new_wxFSFile(PyObject *self, PyObject *args, PyObject *kw if(!PyArg_ParseTupleAndKeywords(args,kwargs,"OOOOO:new_wxFSFile",_kwnames,&_obj0,&_obj1,&_obj2,&_obj3,&_argo4)) return NULL; { - if (PyInstance_Check(_obj0)) { - wxPyInputStream* ptr; - if (SWIG_GetPtrObj(_obj0, (void **) &ptr,"_wxPyInputStream_p")) { - PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p."); + if (SWIG_GetPtrObj(_obj0, (void **) &temp, "_wxPyInputStream_p") == 0) { + _arg0 = temp->m_wxis; + created = FALSE; + } else { + _arg0 = wxPyCBInputStream::create(_obj0, FALSE); + if (_arg0 == NULL) { + PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p or Python file-like object."); return NULL; } - _arg0 = ptr->wxi; - } else { - PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p."); - return NULL; + created = TRUE; } } { @@ -427,6 +429,10 @@ static PyObject *_wrap_new_wxFSFile(PyObject *self, PyObject *args, PyObject *kw Py_INCREF(Py_None); _resultobj = Py_None; } +{ + if (created) + delete _arg0; +} { if (_obj1) delete _arg1; diff --git a/wxPython/src/msw/image.cpp b/wxPython/src/msw/image.cpp index 4a4115c10b..aadd657f22 100644 --- a/wxPython/src/msw/image.cpp +++ b/wxPython/src/msw/image.cpp @@ -98,14 +98,17 @@ static PyObject* t_output_helper(PyObject* target, PyObject* o) { return new wxImage(width, height); } + wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype, int index) { return new wxImage(name, mimetype, index); } + wxImage* wxImageFromBitmap(const wxBitmap &bitmap) { return new wxImage(bitmap); } + wxImage* wxImageFromData(int width, int height, unsigned char* data) { // Copy the source data so the wxImage can clean it up later unsigned char* copy = (unsigned char*)malloc(width*height*3); @@ -117,6 +120,18 @@ static PyObject* t_output_helper(PyObject* target, PyObject* o) { return new wxImage(width, height, copy, FALSE); } + + wxImage* wxImageFromStream(wxInputStream& stream, + long type = wxBITMAP_TYPE_ANY, int index = -1) { + return new wxImage(stream, type, index); + } + + + wxImage* wxImageFromStreamMime(wxInputStream& stream, + const wxString& mimetype, int index = -1 ) { + return new wxImage(stream, mimetype, index); + } + #if 0 extern wxImage wxNullImage; @@ -300,6 +315,125 @@ static PyObject *_wrap_wxImageFromData(PyObject *self, PyObject *args, PyObject return _resultobj; } +static PyObject *_wrap_wxImageFromStream(PyObject *self, PyObject *args, PyObject *kwargs) { + PyObject * _resultobj; + wxImage * _result; + wxInputStream * _arg0; + long _arg1 = (long ) wxBITMAP_TYPE_ANY; + int _arg2 = (int ) -1; + wxPyInputStream * temp; + bool created; + PyObject * _obj0 = 0; + char *_kwnames[] = { "stream","type","index", NULL }; + char _ptemp[128]; + + self = self; + if(!PyArg_ParseTupleAndKeywords(args,kwargs,"O|li:wxImageFromStream",_kwnames,&_obj0,&_arg1,&_arg2)) + return NULL; +{ + if (SWIG_GetPtrObj(_obj0, (void **) &temp, "_wxPyInputStream_p") == 0) { + _arg0 = temp->m_wxis; + created = FALSE; + } else { + _arg0 = wxPyCBInputStream::create(_obj0, FALSE); + if (_arg0 == NULL) { + PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p or Python file-like object."); + return NULL; + } + created = TRUE; + } +} +{ + PyThreadState* __tstate = wxPyBeginAllowThreads(); + _result = (wxImage *)wxImageFromStream(*_arg0,_arg1,_arg2); + + wxPyEndAllowThreads(__tstate); + if (PyErr_Occurred()) return NULL; +} if (_result) { + SWIG_MakePtr(_ptemp, (char *) _result,"_wxImage_p"); + _resultobj = Py_BuildValue("s",_ptemp); + } else { + Py_INCREF(Py_None); + _resultobj = Py_None; + } +{ + if (created) + delete _arg0; +} + return _resultobj; +} + +static PyObject *_wrap_wxImageFromStreamMime(PyObject *self, PyObject *args, PyObject *kwargs) { + PyObject * _resultobj; + wxImage * _result; + wxInputStream * _arg0; + wxString * _arg1; + int _arg2 = (int ) -1; + wxPyInputStream * temp; + bool created; + PyObject * _obj0 = 0; + PyObject * _obj1 = 0; + char *_kwnames[] = { "stream","mimetype","index", NULL }; + char _ptemp[128]; + + self = self; + if(!PyArg_ParseTupleAndKeywords(args,kwargs,"OO|i:wxImageFromStreamMime",_kwnames,&_obj0,&_obj1,&_arg2)) + return NULL; +{ + if (SWIG_GetPtrObj(_obj0, (void **) &temp, "_wxPyInputStream_p") == 0) { + _arg0 = temp->m_wxis; + created = FALSE; + } else { + _arg0 = wxPyCBInputStream::create(_obj0, FALSE); + if (_arg0 == NULL) { + PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p or Python file-like object."); + return NULL; + } + created = TRUE; + } +} +{ +#if PYTHON_API_VERSION >= 1009 + char* tmpPtr; int tmpSize; + if (!PyString_Check(_obj1) && !PyUnicode_Check(_obj1)) { + PyErr_SetString(PyExc_TypeError, wxStringErrorMsg); + return NULL; + } + if (PyString_AsStringAndSize(_obj1, &tmpPtr, &tmpSize) == -1) + return NULL; + _arg1 = new wxString(tmpPtr, tmpSize); +#else + if (!PyString_Check(_obj1)) { + PyErr_SetString(PyExc_TypeError, wxStringErrorMsg); + return NULL; + } + _arg1 = new wxString(PyString_AS_STRING(_obj1), PyString_GET_SIZE(_obj1)); +#endif +} +{ + PyThreadState* __tstate = wxPyBeginAllowThreads(); + _result = (wxImage *)wxImageFromStreamMime(*_arg0,*_arg1,_arg2); + + wxPyEndAllowThreads(__tstate); + if (PyErr_Occurred()) return NULL; +} if (_result) { + SWIG_MakePtr(_ptemp, (char *) _result,"_wxImage_p"); + _resultobj = Py_BuildValue("s",_ptemp); + } else { + Py_INCREF(Py_None); + _resultobj = Py_None; + } +{ + if (created) + delete _arg0; +} +{ + if (_obj1) + delete _arg1; +} + return _resultobj; +} + static PyObject *_wrap_wxInitAllImageHandlers(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject * _resultobj; char *_kwnames[] = { NULL }; @@ -1922,6 +2056,170 @@ static PyObject *_wrap_wxImage_SaveMimeFile(PyObject *self, PyObject *args, PyOb return _resultobj; } +static PyObject *_wrap_wxImage_CanReadStream(PyObject *self, PyObject *args, PyObject *kwargs) { + PyObject * _resultobj; + bool _result; + wxInputStream * _arg0; + wxPyInputStream * temp; + bool created; + PyObject * _obj0 = 0; + char *_kwnames[] = { "stream", NULL }; + + self = self; + if(!PyArg_ParseTupleAndKeywords(args,kwargs,"O:wxImage_CanReadStream",_kwnames,&_obj0)) + return NULL; +{ + if (SWIG_GetPtrObj(_obj0, (void **) &temp, "_wxPyInputStream_p") == 0) { + _arg0 = temp->m_wxis; + created = FALSE; + } else { + _arg0 = wxPyCBInputStream::create(_obj0, FALSE); + if (_arg0 == NULL) { + PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p or Python file-like object."); + return NULL; + } + created = TRUE; + } +} +{ + PyThreadState* __tstate = wxPyBeginAllowThreads(); + _result = (bool )wxImage::CanRead(*_arg0); + + wxPyEndAllowThreads(__tstate); + if (PyErr_Occurred()) return NULL; +} _resultobj = Py_BuildValue("i",_result); +{ + if (created) + delete _arg0; +} + return _resultobj; +} + +#define wxImage_LoadStream(_swigobj,_swigarg0,_swigarg1,_swigarg2) (_swigobj->LoadFile(_swigarg0,_swigarg1,_swigarg2)) +static PyObject *_wrap_wxImage_LoadStream(PyObject *self, PyObject *args, PyObject *kwargs) { + PyObject * _resultobj; + bool _result; + wxImage * _arg0; + wxInputStream * _arg1; + long _arg2 = (long ) wxBITMAP_TYPE_ANY; + int _arg3 = (int ) -1; + PyObject * _argo0 = 0; + wxPyInputStream * temp; + bool created; + PyObject * _obj1 = 0; + char *_kwnames[] = { "self","stream","type","index", NULL }; + + self = self; + if(!PyArg_ParseTupleAndKeywords(args,kwargs,"OO|li:wxImage_LoadStream",_kwnames,&_argo0,&_obj1,&_arg2,&_arg3)) + return NULL; + if (_argo0) { + if (_argo0 == Py_None) { _arg0 = NULL; } + else if (SWIG_GetPtrObj(_argo0,(void **) &_arg0,"_wxImage_p")) { + PyErr_SetString(PyExc_TypeError,"Type error in argument 1 of wxImage_LoadStream. Expected _wxImage_p."); + return NULL; + } + } +{ + if (SWIG_GetPtrObj(_obj1, (void **) &temp, "_wxPyInputStream_p") == 0) { + _arg1 = temp->m_wxis; + created = FALSE; + } else { + _arg1 = wxPyCBInputStream::create(_obj1, FALSE); + if (_arg1 == NULL) { + PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p or Python file-like object."); + return NULL; + } + created = TRUE; + } +} +{ + PyThreadState* __tstate = wxPyBeginAllowThreads(); + _result = (bool )wxImage_LoadStream(_arg0,*_arg1,_arg2,_arg3); + + wxPyEndAllowThreads(__tstate); + if (PyErr_Occurred()) return NULL; +} _resultobj = Py_BuildValue("i",_result); +{ + if (created) + delete _arg1; +} + return _resultobj; +} + +#define wxImage_LoadMimeStream(_swigobj,_swigarg0,_swigarg1,_swigarg2) (_swigobj->LoadFile(_swigarg0,_swigarg1,_swigarg2)) +static PyObject *_wrap_wxImage_LoadMimeStream(PyObject *self, PyObject *args, PyObject *kwargs) { + PyObject * _resultobj; + bool _result; + wxImage * _arg0; + wxInputStream * _arg1; + wxString * _arg2; + int _arg3 = (int ) -1; + PyObject * _argo0 = 0; + wxPyInputStream * temp; + bool created; + PyObject * _obj1 = 0; + PyObject * _obj2 = 0; + char *_kwnames[] = { "self","stream","mimetype","index", NULL }; + + self = self; + if(!PyArg_ParseTupleAndKeywords(args,kwargs,"OOO|i:wxImage_LoadMimeStream",_kwnames,&_argo0,&_obj1,&_obj2,&_arg3)) + return NULL; + if (_argo0) { + if (_argo0 == Py_None) { _arg0 = NULL; } + else if (SWIG_GetPtrObj(_argo0,(void **) &_arg0,"_wxImage_p")) { + PyErr_SetString(PyExc_TypeError,"Type error in argument 1 of wxImage_LoadMimeStream. Expected _wxImage_p."); + return NULL; + } + } +{ + if (SWIG_GetPtrObj(_obj1, (void **) &temp, "_wxPyInputStream_p") == 0) { + _arg1 = temp->m_wxis; + created = FALSE; + } else { + _arg1 = wxPyCBInputStream::create(_obj1, FALSE); + if (_arg1 == NULL) { + PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p or Python file-like object."); + return NULL; + } + created = TRUE; + } +} +{ +#if PYTHON_API_VERSION >= 1009 + char* tmpPtr; int tmpSize; + if (!PyString_Check(_obj2) && !PyUnicode_Check(_obj2)) { + PyErr_SetString(PyExc_TypeError, wxStringErrorMsg); + return NULL; + } + if (PyString_AsStringAndSize(_obj2, &tmpPtr, &tmpSize) == -1) + return NULL; + _arg2 = new wxString(tmpPtr, tmpSize); +#else + if (!PyString_Check(_obj2)) { + PyErr_SetString(PyExc_TypeError, wxStringErrorMsg); + return NULL; + } + _arg2 = new wxString(PyString_AS_STRING(_obj2), PyString_GET_SIZE(_obj2)); +#endif +} +{ + PyThreadState* __tstate = wxPyBeginAllowThreads(); + _result = (bool )wxImage_LoadMimeStream(_arg0,*_arg1,*_arg2,_arg3); + + wxPyEndAllowThreads(__tstate); + if (PyErr_Occurred()) return NULL; +} _resultobj = Py_BuildValue("i",_result); +{ + if (created) + delete _arg1; +} +{ + if (_obj2) + delete _arg2; +} + return _resultobj; +} + #define wxImage_Ok(_swigobj) (_swigobj->Ok()) static PyObject *_wrap_wxImage_Ok(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject * _resultobj; @@ -2994,6 +3292,9 @@ static PyMethodDef imagecMethods[] = { { "wxImage_GetHeight", (PyCFunction) _wrap_wxImage_GetHeight, METH_VARARGS | METH_KEYWORDS }, { "wxImage_GetWidth", (PyCFunction) _wrap_wxImage_GetWidth, METH_VARARGS | METH_KEYWORDS }, { "wxImage_Ok", (PyCFunction) _wrap_wxImage_Ok, METH_VARARGS | METH_KEYWORDS }, + { "wxImage_LoadMimeStream", (PyCFunction) _wrap_wxImage_LoadMimeStream, METH_VARARGS | METH_KEYWORDS }, + { "wxImage_LoadStream", (PyCFunction) _wrap_wxImage_LoadStream, METH_VARARGS | METH_KEYWORDS }, + { "wxImage_CanReadStream", (PyCFunction) _wrap_wxImage_CanReadStream, METH_VARARGS | METH_KEYWORDS }, { "wxImage_SaveMimeFile", (PyCFunction) _wrap_wxImage_SaveMimeFile, METH_VARARGS | METH_KEYWORDS }, { "wxImage_SaveFile", (PyCFunction) _wrap_wxImage_SaveFile, METH_VARARGS | METH_KEYWORDS }, { "wxImage_LoadMimeFile", (PyCFunction) _wrap_wxImage_LoadMimeFile, METH_VARARGS | METH_KEYWORDS }, @@ -3032,6 +3333,8 @@ static PyMethodDef imagecMethods[] = { { "wxImageHandler_GetName", (PyCFunction) _wrap_wxImageHandler_GetName, METH_VARARGS | METH_KEYWORDS }, { "wxBitmapFromImage", (PyCFunction) _wrap_wxBitmapFromImage, METH_VARARGS | METH_KEYWORDS }, { "wxInitAllImageHandlers", (PyCFunction) _wrap_wxInitAllImageHandlers, METH_VARARGS | METH_KEYWORDS }, + { "wxImageFromStreamMime", (PyCFunction) _wrap_wxImageFromStreamMime, METH_VARARGS | METH_KEYWORDS }, + { "wxImageFromStream", (PyCFunction) _wrap_wxImageFromStream, METH_VARARGS | METH_KEYWORDS }, { "wxImageFromData", (PyCFunction) _wrap_wxImageFromData, METH_VARARGS | METH_KEYWORDS }, { "wxImageFromBitmap", (PyCFunction) _wrap_wxImageFromBitmap, METH_VARARGS | METH_KEYWORDS }, { "wxImageFromMime", (PyCFunction) _wrap_wxImageFromMime, METH_VARARGS | METH_KEYWORDS }, diff --git a/wxPython/src/msw/image.py b/wxPython/src/msw/image.py index 227aa78dcd..f725822176 100644 --- a/wxPython/src/msw/image.py +++ b/wxPython/src/msw/image.py @@ -4,6 +4,8 @@ import imagec from misc import * from gdi import * + +from streams import * class wxImageHandlerPtr(wxObjectPtr): def __init__(self,this): self.this = this @@ -233,6 +235,12 @@ class wxImagePtr(wxObjectPtr): def SaveMimeFile(self, *_args, **_kwargs): val = apply(imagec.wxImage_SaveMimeFile,(self,) + _args, _kwargs) return val + def LoadStream(self, *_args, **_kwargs): + val = apply(imagec.wxImage_LoadStream,(self,) + _args, _kwargs) + return val + def LoadMimeStream(self, *_args, **_kwargs): + val = apply(imagec.wxImage_LoadMimeStream,(self,) + _args, _kwargs) + return val def Ok(self, *_args, **_kwargs): val = apply(imagec.wxImage_Ok,(self,) + _args, _kwargs) return val @@ -348,6 +356,16 @@ def wxImageFromData(*_args, **_kwargs): if val: val = wxImagePtr(val); val.thisown = 1 return val +def wxImageFromStream(*_args, **_kwargs): + val = apply(imagec.wxImageFromStream,_args,_kwargs) + if val: val = wxImagePtr(val); val.thisown = 1 + return val + +def wxImageFromStreamMime(*_args, **_kwargs): + val = apply(imagec.wxImageFromStreamMime,_args,_kwargs) + if val: val = wxImagePtr(val); val.thisown = 1 + return val + wxInitAllImageHandlers = imagec.wxInitAllImageHandlers def wxBitmapFromImage(*_args, **_kwargs): @@ -359,6 +377,8 @@ wxImage_CanRead = imagec.wxImage_CanRead wxImage_GetImageCount = imagec.wxImage_GetImageCount +wxImage_CanReadStream = imagec.wxImage_CanReadStream + wxImage_AddHandler = imagec.wxImage_AddHandler wxImage_InsertHandler = imagec.wxImage_InsertHandler diff --git a/wxPython/src/msw/streams.cpp b/wxPython/src/msw/streams.cpp index fbd7f64371..e06a968f1f 100644 --- a/wxPython/src/msw/streams.cpp +++ b/wxPython/src/msw/streams.cpp @@ -91,310 +91,13 @@ static PyObject* t_output_helper(PyObject* target, PyObject* o) { #else static char* wxStringErrorMsg = "String type required"; #endif - // C++ -// definitions of wxStringPtrList and wxPyInputStream -#include -WX_DEFINE_LIST(wxStringPtrList); - - -void wxPyInputStream::close() { - /* do nothing */ -} - -void wxPyInputStream::flush() { - /*do nothing*/ -} - -bool wxPyInputStream::eof() { - if (wxi) - return wxi->Eof(); - else - return TRUE; -} - -wxPyInputStream::~wxPyInputStream() { - /*do nothing*/ -} - -wxString* wxPyInputStream::read(int size) { - wxString* s = NULL; - const int BUFSIZE = 1024; - - // check if we have a real wxInputStream to work with - if (!wxi) { - PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); - return NULL; - } - - if (size < 0) { - // init buffers - char * buf = new char[BUFSIZE]; - if (!buf) { - PyErr_NoMemory(); - return NULL; - } - - s = new wxString(); - if (!s) { - delete buf; - PyErr_NoMemory(); - return NULL; - } - - // read until EOF - while (! wxi->Eof()) { - wxi->Read(buf, BUFSIZE); - //*s += wxString(buf, wxi->LastRead()); - s->Append(buf, wxi->LastRead()); - } - delete buf; - - // error check - if (wxi->LastError() == wxSTREAM_READ_ERROR) { - delete s; - PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); - return NULL; - } - - } else { // Read only size number of characters - s = new wxString; - if (!s) { - PyErr_NoMemory(); - return NULL; - } - - // read size bytes - wxi->Read(s->GetWriteBuf(size+1), size); - s->UngetWriteBuf(wxi->LastRead()); - - // error check - if (wxi->LastError() == wxSTREAM_READ_ERROR) { - delete s; - PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); - return NULL; - } - } - return s; -} - - -wxString* wxPyInputStream::readline (int size) { - // check if we have a real wxInputStream to work with - if (!wxi) { - PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); - return NULL; - } - - // init buffer - int i; - char ch; - wxString* s = new wxString; - if (!s) { - PyErr_NoMemory(); - return NULL; - } - - // read until \n or byte limit reached - for (i=ch=0; (ch != '\n') && (!wxi->Eof()) && ((size < 0) || (i < size)); i++) { - *s += ch = wxi->GetC(); - } - - // errorcheck - if (wxi->LastError() == wxSTREAM_READ_ERROR) { - delete s; - PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); - return NULL; - } - return s; -} - - -wxStringPtrList* wxPyInputStream::readlines (int sizehint) { - // check if we have a real wxInputStream to work with - if (!wxi) { - PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); - return NULL; - } - - // init list - wxStringPtrList* l = new wxStringPtrList(); - if (!l) { - PyErr_NoMemory(); - return NULL; - } - - // read sizehint bytes or until EOF - int i; - for (i=0; (!wxi->Eof()) && ((sizehint < 0) || (i < sizehint));) { - wxString* s = readline(); - if (s == NULL) { - l->DeleteContents(TRUE); - l->Clear(); - return NULL; - } - l->Append(s); - i = i + s->Length(); - } - - // error check - if (wxi->LastError() == wxSTREAM_READ_ERROR) { - l->DeleteContents(TRUE); - l->Clear(); - PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); - return NULL; - } - return l; -} - - -void wxPyInputStream::seek(int offset, int whence) { - if (wxi) - wxi->SeekI(offset, wxSeekMode(whence)); -} - -int wxPyInputStream::tell(){ - if (wxi) - return wxi->TellI(); -} - - - -// wxInputStream which operates on a Python file-like object -class wxPyCBInputStream : public wxInputStream { -protected: - PyObject* read; - PyObject* seek; - PyObject* tell; - PyObject* py; - - virtual size_t OnSysRead(void *buffer, size_t bufsize) { - if (bufsize == 0) - return 0; - - wxPyBeginBlockThreads(); - PyObject* arglist = Py_BuildValue("(i)", bufsize); - PyObject* result = PyEval_CallObject(read, arglist); - Py_DECREF(arglist); - - size_t o = 0; - if ((result != NULL) && PyString_Check(result)) { - o = PyString_Size(result); - if (o == 0) - m_lasterror = wxSTREAM_EOF; - if (o > bufsize) - o = bufsize; - strncpy((char*)buffer, PyString_AsString(result), o); - Py_DECREF(result); - - } - else - m_lasterror = wxSTREAM_READ_ERROR; - wxPyEndBlockThreads(); - m_lastcount = o; - return o; - } - - virtual size_t OnSysWrite(const void *buffer, size_t bufsize){ - m_lasterror = wxSTREAM_WRITE_ERROR; - return 0; - } - - virtual off_t OnSysSeek(off_t off, wxSeekMode mode){ - wxPyBeginBlockThreads(); - PyObject*arglist = Py_BuildValue("(ii)", off, mode); - PyObject*result = PyEval_CallObject(seek, arglist); - Py_DECREF(arglist); - Py_XDECREF(result); - wxPyEndBlockThreads(); - return OnSysTell(); - } - - virtual off_t OnSysTell() const{ - wxPyBeginBlockThreads(); - PyObject* arglist = Py_BuildValue("()"); - PyObject* result = PyEval_CallObject(tell, arglist); - Py_DECREF(arglist); - off_t o = 0; - if (result != NULL) { - o = PyInt_AsLong(result); - Py_DECREF(result); - }; - wxPyEndBlockThreads(); - return o; - } - - wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t) - : py(p), read(r), seek(s), tell(t) - {} - -public: - ~wxPyCBInputStream() { - wxPyBeginBlockThreads(); - Py_XDECREF(py); - Py_XDECREF(read); - Py_XDECREF(seek); - Py_XDECREF(tell); - wxPyEndBlockThreads(); - } - - virtual size_t GetSize() { - if (seek && tell) { - off_t temp = OnSysTell(); - off_t ret = OnSysSeek(0, wxFromEnd); - OnSysSeek(temp, wxFromStart); - return ret; - } - else - return 0; - } - - static wxPyCBInputStream* create(PyObject *py) { - PyObject* read; - PyObject* seek; - PyObject* tell; - - if (!PyInstance_Check(py) && !PyFile_Check(py)) { - PyErr_SetString(PyExc_TypeError, "Not a file-like object"); - Py_XDECREF(py); - return NULL; - } - read = getMethod(py, "read"); - seek = getMethod(py, "seek"); - tell = getMethod(py, "tell"); - - if (!read) { - PyErr_SetString(PyExc_TypeError, "Not a file-like object"); - Py_XDECREF(py); - Py_XDECREF(read); - Py_XDECREF(seek); - Py_XDECREF(tell); - return NULL; - } - return new wxPyCBInputStream(py, read, seek, tell); - } - - static PyObject* getMethod(PyObject* py, char* name) { - if (!PyObject_HasAttrString(py, name)) - return NULL; - PyObject* o = PyObject_GetAttrString(py, name); - if (!PyMethod_Check(o) && !PyCFunction_Check(o)) { - Py_DECREF(o); - return NULL; - } - return o; - } - -protected: - -}; - #ifdef __cplusplus extern "C" { #endif static wxPyInputStream *new_wxPyInputStream(PyObject *p) { - wxInputStream* wxi = wxPyCBInputStream::create(p); - if (wxi) - return new wxPyInputStream(wxi); + wxInputStream* wxis = wxPyCBInputStream::create(p); + if (wxis) + return new wxPyInputStream(wxis); else return NULL; } diff --git a/wxPython/src/streams.i b/wxPython/src/streams.i index e87a10cb2a..4122cf2fce 100644 --- a/wxPython/src/streams.i +++ b/wxPython/src/streams.i @@ -31,20 +31,27 @@ //---------------------------------------------------------------------- // typemaps for wxInputStream -%typemap(python,in) wxInputStream *stream { - if (PyInstance_Check($source)) { - wxPyInputStream* ptr; - if (SWIG_GetPtrObj($source, (void **) &ptr,"_wxPyInputStream_p")) { - PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p."); + + +%typemap(python,in) wxInputStream * (wxPyInputStream* temp, bool created) { + if (SWIG_GetPtrObj($source, (void **) &temp, "_wxPyInputStream_p") == 0) { + $target = temp->m_wxis; + created = FALSE; + } else { + $target = wxPyCBInputStream::create($source, FALSE); + if ($target == NULL) { + PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p or Python file-like object."); return NULL; } - $target = ptr->wxi; - } else { - PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p."); - return NULL; + created = TRUE; } } +%typemap(python, freearg) wxInputStream * { + if (created) + delete $source; +} + // typemaps for wxInputStream %typemap(python,out) wxInputStream* { @@ -58,306 +65,6 @@ //---------------------------------------------------------------------- -%{ // C++ -// definitions of wxStringPtrList and wxPyInputStream -#include -WX_DEFINE_LIST(wxStringPtrList); - - -void wxPyInputStream::close() { - /* do nothing */ -} - -void wxPyInputStream::flush() { - /*do nothing*/ -} - -bool wxPyInputStream::eof() { - if (wxi) - return wxi->Eof(); - else - return TRUE; -} - -wxPyInputStream::~wxPyInputStream() { - /*do nothing*/ -} - -wxString* wxPyInputStream::read(int size) { - wxString* s = NULL; - const int BUFSIZE = 1024; - - // check if we have a real wxInputStream to work with - if (!wxi) { - PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); - return NULL; - } - - if (size < 0) { - // init buffers - char * buf = new char[BUFSIZE]; - if (!buf) { - PyErr_NoMemory(); - return NULL; - } - - s = new wxString(); - if (!s) { - delete buf; - PyErr_NoMemory(); - return NULL; - } - - // read until EOF - while (! wxi->Eof()) { - wxi->Read(buf, BUFSIZE); - //*s += wxString(buf, wxi->LastRead()); - s->Append(buf, wxi->LastRead()); - } - delete buf; - - // error check - if (wxi->LastError() == wxSTREAM_READ_ERROR) { - delete s; - PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); - return NULL; - } - - } else { // Read only size number of characters - s = new wxString; - if (!s) { - PyErr_NoMemory(); - return NULL; - } - - // read size bytes - wxi->Read(s->GetWriteBuf(size+1), size); - s->UngetWriteBuf(wxi->LastRead()); - - // error check - if (wxi->LastError() == wxSTREAM_READ_ERROR) { - delete s; - PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); - return NULL; - } - } - return s; -} - - -wxString* wxPyInputStream::readline (int size) { - // check if we have a real wxInputStream to work with - if (!wxi) { - PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); - return NULL; - } - - // init buffer - int i; - char ch; - wxString* s = new wxString; - if (!s) { - PyErr_NoMemory(); - return NULL; - } - - // read until \n or byte limit reached - for (i=ch=0; (ch != '\n') && (!wxi->Eof()) && ((size < 0) || (i < size)); i++) { - *s += ch = wxi->GetC(); - } - - // errorcheck - if (wxi->LastError() == wxSTREAM_READ_ERROR) { - delete s; - PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); - return NULL; - } - return s; -} - - -wxStringPtrList* wxPyInputStream::readlines (int sizehint) { - // check if we have a real wxInputStream to work with - if (!wxi) { - PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); - return NULL; - } - - // init list - wxStringPtrList* l = new wxStringPtrList(); - if (!l) { - PyErr_NoMemory(); - return NULL; - } - - // read sizehint bytes or until EOF - int i; - for (i=0; (!wxi->Eof()) && ((sizehint < 0) || (i < sizehint));) { - wxString* s = readline(); - if (s == NULL) { - l->DeleteContents(TRUE); - l->Clear(); - return NULL; - } - l->Append(s); - i = i + s->Length(); - } - - // error check - if (wxi->LastError() == wxSTREAM_READ_ERROR) { - l->DeleteContents(TRUE); - l->Clear(); - PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); - return NULL; - } - return l; -} - - -void wxPyInputStream::seek(int offset, int whence) { - if (wxi) - wxi->SeekI(offset, wxSeekMode(whence)); -} - -int wxPyInputStream::tell(){ - if (wxi) - return wxi->TellI(); -} - - - -// wxInputStream which operates on a Python file-like object -class wxPyCBInputStream : public wxInputStream { -protected: - PyObject* read; - PyObject* seek; - PyObject* tell; - PyObject* py; - - virtual size_t OnSysRead(void *buffer, size_t bufsize) { - if (bufsize == 0) - return 0; - - wxPyBeginBlockThreads(); - PyObject* arglist = Py_BuildValue("(i)", bufsize); - PyObject* result = PyEval_CallObject(read, arglist); - Py_DECREF(arglist); - - size_t o = 0; - if ((result != NULL) && PyString_Check(result)) { - o = PyString_Size(result); - if (o == 0) - m_lasterror = wxSTREAM_EOF; - if (o > bufsize) - o = bufsize; - strncpy((char*)buffer, PyString_AsString(result), o); - Py_DECREF(result); - - } - else - m_lasterror = wxSTREAM_READ_ERROR; - wxPyEndBlockThreads(); - m_lastcount = o; - return o; - } - - virtual size_t OnSysWrite(const void *buffer, size_t bufsize){ - m_lasterror = wxSTREAM_WRITE_ERROR; - return 0; - } - - virtual off_t OnSysSeek(off_t off, wxSeekMode mode){ - wxPyBeginBlockThreads(); - PyObject*arglist = Py_BuildValue("(ii)", off, mode); - PyObject*result = PyEval_CallObject(seek, arglist); - Py_DECREF(arglist); - Py_XDECREF(result); - wxPyEndBlockThreads(); - return OnSysTell(); - } - - virtual off_t OnSysTell() const{ - wxPyBeginBlockThreads(); - PyObject* arglist = Py_BuildValue("()"); - PyObject* result = PyEval_CallObject(tell, arglist); - Py_DECREF(arglist); - off_t o = 0; - if (result != NULL) { - o = PyInt_AsLong(result); - Py_DECREF(result); - }; - wxPyEndBlockThreads(); - return o; - } - - wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t) - : py(p), read(r), seek(s), tell(t) - {} - -public: - ~wxPyCBInputStream() { - wxPyBeginBlockThreads(); - Py_XDECREF(py); - Py_XDECREF(read); - Py_XDECREF(seek); - Py_XDECREF(tell); - wxPyEndBlockThreads(); - } - - virtual size_t GetSize() { - if (seek && tell) { - off_t temp = OnSysTell(); - off_t ret = OnSysSeek(0, wxFromEnd); - OnSysSeek(temp, wxFromStart); - return ret; - } - else - return 0; - } - - static wxPyCBInputStream* create(PyObject *py) { - PyObject* read; - PyObject* seek; - PyObject* tell; - - if (!PyInstance_Check(py) && !PyFile_Check(py)) { - PyErr_SetString(PyExc_TypeError, "Not a file-like object"); - Py_XDECREF(py); - return NULL; - } - read = getMethod(py, "read"); - seek = getMethod(py, "seek"); - tell = getMethod(py, "tell"); - - if (!read) { - PyErr_SetString(PyExc_TypeError, "Not a file-like object"); - Py_XDECREF(py); - Py_XDECREF(read); - Py_XDECREF(seek); - Py_XDECREF(tell); - return NULL; - } - return new wxPyCBInputStream(py, read, seek, tell); - } - - static PyObject* getMethod(PyObject* py, char* name) { - if (!PyObject_HasAttrString(py, name)) - return NULL; - PyObject* o = PyObject_GetAttrString(py, name); - if (!PyMethod_Check(o) && !PyCFunction_Check(o)) { - Py_DECREF(o); - return NULL; - } - return o; - } - -protected: - -}; - -%} // End of the C++ -//---------------------------------------------------------------------- - // wxStringPtrList* to python list of strings typemap %typemap(python, out) wxStringPtrList* { @@ -377,26 +84,15 @@ protected: } -%typemap(python, out) wxPyInputStream* { - char _ptemp[128]; - if ($source) { - SWIG_MakePtr(_ptemp, (char *) $source,"_wxPyInputStream_p"); - $target = Py_BuildValue("s",_ptemp); - } - else - $target=0; -} - - %name(wxInputStream) class wxPyInputStream { public: %addmethods { wxPyInputStream(PyObject* p) { - wxInputStream* wxi = wxPyCBInputStream::create(p); - if (wxi) - return new wxPyInputStream(wxi); + wxInputStream* wxis = wxPyCBInputStream::create(p); + if (wxis) + return new wxPyInputStream(wxis); else return NULL; } @@ -409,6 +105,7 @@ public: wxStringPtrList* readlines(int sizehint=-1); void seek(int offset, int whence=0); int tell(); + /* bool isatty(); int fileno(); -- 2.45.2