[wxPython] wxFileSystemHandler segfaults

The following code should handle requests for URLs of 'myprotocol://' and
return the requested open file from the real filesystem.

from wxPython.wx import *

class myfs(wxFileSystemHandler):
  def __init__(self):
    wxFileSystemHandler.__init__(self)
  def CanOpen(self,location):
    print 'canopen:',location
    p=self.GetProtocol(location)
    print 'canopen: requested protocol is',p
    return (p=='myprotocol')
  def OpenFile(self,parentfs,location):
    print 'openfile',location
    fname=self.GetRightLocation(location)
    print 'i will return a stream on',fname
    i=wxInputStream(open(fname))
    f=wxFSFile(i,location,'text/plain','',wxDateTime_Now())
    return f

handler=myfs()
wxFileSystem_AddHandler(handler)
wxfs=wxFileSystem()
#f=wxfs.OpenFile('/etc/passwd') # this works
f=wxfs.OpenFile('myprotocol://etc/passwd') # this doesnt, causes segfault
if f:
  print f,dir(f)
  print 'OPENED',f.GetStream().read()

but when I run it I get:

canopen: myprotocol://etc/passwd
canopen: requested protocol is myprotocol
Segmentation fault

The gdb stacktrace is:

#0 0x40078289 in Py_InitModule4 () from /usr/lib/libpython2.1.so.0.0
#1 0x40077df0 in Py_InitModule4 () from /usr/lib/libpython2.1.so.0.0
#2 0x400780a6 in Py_InitModule4 () from /usr/lib/libpython2.1.so.0.0
#3 0x40078370 in Py_VaBuildValue () from /usr/lib/libpython2.1.so.0.0
#4 0x400782fa in Py_BuildValue () from /usr/lib/libpython2.1.so.0.0
#5 0x4032edf6 in wxPyFileSystemHandler::OpenFile (this=0x8164070,
a=@0x81721e8,
    b=@0xbffff508) at src/gtk/filesys.cpp:128
#6 0x405e4f3e in wxFileSystem::OpenFile () from
/usr/local/lib/libwx_gtk-2.3.so
#7 0x4033129e in _wrap_wxFileSystem_OpenFile (self=0x0, args=0x816416c,
    kwargs=0x81641fc) at src/gtk/filesys.cpp:1179
#8 0x40062ade in PyEval_CallObjectWithKeywords ()
   from /usr/lib/libpython2.1.so.0.0
#9 0x40062a03 in PyEval_CallObjectWithKeywords ()
   from /usr/lib/libpython2.1.so.0.0
#10 0x40062883 in PyEval_CallObjectWithKeywords ()
   from /usr/lib/libpython2.1.so.0.0
#11 0x40059644 in _PyUnicode_IsAlpha () from /usr/lib/libpython2.1.so.0.0
#12 0x40062b28 in PyEval_CallObjectWithKeywords ()
   from /usr/lib/libpython2.1.so.0.0
#13 0x4006137a in PyEval_EvalCode () from /usr/lib/libpython2.1.so.0.0
#14 0x40062f6a in PyEval_CallObjectWithKeywords ()
   from /usr/lib/libpython2.1.so.0.0
#15 0x40061436 in PyEval_EvalCode () from /usr/lib/libpython2.1.so.0.0
#16 0x4005e49e in PyEval_EvalCode () from /usr/lib/libpython2.1.so.0.0
#17 0x4007a8c4 in PyRun_FileExFlags () from /usr/lib/libpython2.1.so.0.0
#18 0x4007a867 in PyRun_FileExFlags () from /usr/lib/libpython2.1.so.0.0
#19 0x4007a82d in PyRun_FileExFlags () from /usr/lib/libpython2.1.so.0.0
#20 0x40079aef in PyRun_SimpleFileExFlags () from /usr/lib/libpython2.1.so.0.0
#21 0x4007959f in PyRun_AnyFileExFlags () from /usr/lib/libpython2.1.so.0.0
#22 0x4007f0e7 in Py_Main () from /usr/lib/libpython2.1.so.0.0
#23 0x08048624 in main ()
#24 0x4010f6cf in __libc_start_main () from /lib/libc.so.6

Any help is appreciated.

but when I run it I get:

canopen: myprotocol://etc/passwd
canopen: requested protocol is myprotocol
Segmentation fault

I just checked in a fix to helpers.h for this.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!