wxImage(width,height) throws exception

I'm trying to create a wxImage by calling the constructor that takes only integer width and height arguments but even the simplest code throws exceptions. See below. Am I missing something obvious or is this a bug?

Thanks for any advice,
Barry Tolnas

Python 2.3.2 (#1, Oct 27 2003, 22:40:39)
[GCC 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r2, propolice)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from wxPython.wx import *
>>> wxVERSION_STRING
'2.4.1.2'
>>> image = wxImage(100,100)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/site-packages/wxPython/image.py", line 351, in __init__
    self.this = imagec.new_wxImage(*_args,**_kwargs)
TypeError: String or Unicode type required

···

Barry Tolnas wrote:

I'm trying to create a wxImage by calling the constructor that takes only integer width and height arguments but even the simplest code throws exceptions. See below. Am I missing something obvious or is this a bug?

Use wxEmptyImage(width, height)

···

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

Robin Dunn wrote:

Barry Tolnas wrote:

I'm trying to create a wxImage by calling the constructor that takes only integer width and height arguments but even the simplest code throws exceptions. See below. Am I missing something obvious or is this a bug?

Use wxEmptyImage(width, height)

Thanks for that. I'm still having problems though.

1. wxEmptyBitmap(width,height) segfaults
2. I can't convert an empty wxImage to a wxBitmap. I want to fill the image with raster data computed by my program and then draw it in a window with wxDC.Blit or DrawBitmap. wxImage.ConvertToBitmap() segfaults and is the wxdocs say its deprecated in favor of the wxImage constructor. When I use the wxImage constructor, bitmap=wxBitmap(image), I get this:

  File "/home/barry/CAAD/src/gui/images.py", line 12, in __init__
    self.bitmap = wxBitmap(self.image,3)
  File "/usr/lib/python2.3/site-packages/wxPython/gdi.py", line 102, in __init__
    self.this = gdic.new_wxBitmap(*_args,**_kwargs)
TypeError: String or Unicode type required
Exception exceptions.AttributeError: "wxBitmap instance has no attribute 'thisown'" in ignored

It seems every permutation I try results in either a segfault if it has anything to do with wxBitmap or throws an exception as shown above.

Thanks for you help,
Barry

Thanks, I've since found that (and Robin Dunn posted the same pointer). I'm curious why it is necessary to have a separate Method for making empty Images or Bitmaps. Wouldn't it make more sense to make wxImage(width,height) just behave the same as wxEmptyImage(width, height)?

I did some more experiments on Mac OS X and got a program working using wxEmptyImage() and the wxImage.ConvertToBitmap() (which the docs say is deprecated in favor of the wxBitmap Constructor which takes a wxImage but seems to be the only way to get it to work). However, the same code causes a segfault on my Linux machine, I don't think my installation is totally hosed because everything else including wxGLCanvas code seems to work fine.

As another niggling detail, and I mean this as constructive criticism not a complaint, it seems like something a little more helpful than "TypeError: String or Unicode type required" would make more sense to alert users than wxImage(width,height) constructor doesn't work in wxPython as it appears in the wxWindows documentation. The existing error message seems nonsensical in this instance. What do strings and Unicode have to do with calling a constructor that takes only integer arguments? How can I get a type error if it calls for two ints and I passed in two ints? I'm sure it has something to do with *args or **kwargs, but nevertheless knowing why doesn't make the error message any more meaningful.

Thanks again to everyone who read this far. Lets make wxPython the best tool we can!

Barry

Jim West wrote:

···

You need to use wxEmptyImage ( width, height )

On Tue, 2003-11-11 at 15:29, Barry Tolnas wrote:

I'm trying to create a wxImage by calling the constructor that takes only integer width and height arguments but even the simplest code throws exceptions. See below. Am I missing something obvious or is this a bug?

Thanks for any advice,
Barry Tolnas

Python 2.3.2 (#1, Oct 27 2003, 22:40:39)
[GCC 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r2, propolice)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from wxPython.wx import *
>>> wxVERSION_STRING
'2.4.1.2'
>>> image = wxImage(100,100)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/site-packages/wxPython/image.py", line 351, in __init__
   self.this = imagec.new_wxImage(*_args,**_kwargs)
TypeError: String or Unicode type required
>>>

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
   

Jim,

Thanks so much for your long reply. I just learned yesterday while figuring out images and bitmaps that I should look first for any wxPython additions to the wxWindows docs.

I'll run gdb and try to report more information on my segfaults in case the info is helpful to the developers.

I understand how semantic differences between C++ and Python result in some difficulties wrapping libraries for one language for use with another. If anyone knows the reasons behind this particular case (need for wxEmptyBitmap() ) I'm interested in hearing it just for my own understanding of this type of problem. I'll hunt back for some of Robin's prior posts on the topic.

Thanks again,
Barry

Jim West wrote:

···

Barry,

Not sure why it would segfault on Linux. I do this as well and it
works here. We are not at the latest version of wxPython(wxWindows) yet
either. We are a version behind. Code works on both Linux and Windoze.

Also, something I never really noticed when I began with the docs. Since us wxPython users have to use the wxWindows documentation, a good
number of method sections have a wxPython Users and PERL users section
at the end. This is where I began to discover some of the subtle
differences of the available method calls, such as wxEmptyImage().

So I've trained myself to basically go there first now. :slight_smile:

The crux as to "why" there are these differences has to do with
creating Python extensions to C/C++ code (which I've done in the
past...and is not as smooth as one would like). C++ allows for method
overloading which is based on method signature sans return type. Python's "method" of method overloading takes on a different approach
which really boils down to strict/strong-typing (C++) vs. weak-typing
(Python).

Robin has ranted a few times on this so you might be able to find some
details in the archives.

On your Linux issue, try running your app in gdb. Actually you run
the python interpreter in gdb and pass your app as a command line arg. You might be able to get a stack trace to help narrow the field where
the segfault is happening.

Good luck! I've only been doing wxPython for about six months so I'm
far from being an expert but I do have extensive background in X11,
Motif, GKS, Phigs, SGL, etc. which tends to hurt me more than help me.
:slight_smile:

On Wed, 2003-11-12 at 13:13, Barry Tolnas wrote:

Thanks, I've since found that (and Robin Dunn posted the same pointer). I'm curious why it is necessary to have a separate Method for making empty Images or Bitmaps. Wouldn't it make more sense to make wxImage(width,height) just behave the same as wxEmptyImage(width, height)?

I did some more experiments on Mac OS X and got a program working using wxEmptyImage() and the wxImage.ConvertToBitmap() (which the docs say is deprecated in favor of the wxBitmap Constructor which takes a wxImage but seems to be the only way to get it to work). However, the same code causes a segfault on my Linux machine, I don't think my installation is totally hosed because everything else including wxGLCanvas code seems to work fine.

As another niggling detail, and I mean this as constructive criticism not a complaint, it seems like something a little more helpful than "TypeError: String or Unicode type required" would make more sense to alert users than wxImage(width,height) constructor doesn't work in wxPython as it appears in the wxWindows documentation. The existing error message seems nonsensical in this instance. What do strings and Unicode have to do with calling a constructor that takes only integer arguments? How can I get a type error if it calls for two ints and I passed in two ints? I'm sure it has something to do with *args or **kwargs, but nevertheless knowing why doesn't make the error message any more meaningful.

Thanks again to everyone who read this far. Lets make wxPython the best tool we can!

Barry

Jim West wrote:

You need to use wxEmptyImage ( width, height )

On Tue, 2003-11-11 at 15:29, Barry Tolnas wrote:

I'm trying to create a wxImage by calling the constructor that takes only integer width and height arguments but even the simplest code throws exceptions. See below. Am I missing something obvious or is this a bug?

Thanks for any advice,
Barry Tolnas

Python 2.3.2 (#1, Oct 27 2003, 22:40:39)
[GCC 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r2, propolice)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
       

from wxPython.wx import *
wxVERSION_STRING
             

'2.4.1.2'
       

image = wxImage(100,100)
             

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/site-packages/wxPython/image.py", line 351, in __init__
  self.this = imagec.new_wxImage(*_args,**_kwargs)
TypeError: String or Unicode type required
       
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
  

Barry Tolnas wrote:

If anyone knows the reasons behind this particular case (need
for wxEmptyBitmap() ) I'm interested in hearing it just for my own
understanding of this type of problem. I'll hunt back for some of
Robin's prior posts on the topic.

The reason is the same as for a lot of wxPython functions: Python does
not support function overloading the way C++ does. I dopn't know much
C++, but I'm pretty sure the way wxImage, for instance, works is that it
has a set of constuctors, each taking different parameters. The compiler
knows which one you want by the type of the parameters. Python, on teh
other hand, has dynamic typing, so it's not possible to automatically
call the correct constructor byt he types of the arguments it's called
with. You can simulate that by having one function that first checks the
types of the inputs, and then calls the correct constuctor, but that
requires a bunch of handwork and type checking in the wrappers, and
Robin has only done that in a few cases. It's easier to just make
constructors with different names for each of the different C++
constructors: thus here is the wxPython note for wxImage:

wxPython note: Constructors supported by wxPython are:

wxImage(name, flag)
Loads an image from a file

wxNullImage()
Create a null image (has no size or image data)

wxEmptyImage(width, height)
Creates an empty image of the given size

wxImageFromMime(name, mimetype
Creates an image from the given file of the given mimetype

wxImageFromBitmap(bitmap)
Creates an image from a platform-dependent bitmap

You'll now know to look for those wxPython notes...particularly for
classes with multiple constructors. I'd also look in teh name space for
names that might work if you are having a problem like that:

from wxPython import wx
for n in dir(wx):

... if "Image" in n: print n
...
wxBitmapFromImage
wxCursorFromImage
wxDragImage
wxDragImagePtr
wxEmptyImage
wxImage
wxImageFromBitmap
wxImageFromData
wxImageFromMime
wxImageFromStream
wxImageFromStreamMime
wxImageHandler
wxImageHandlerPtr
wxImageList
wxImageListPtr
wxImagePtr
wxImage_AddHandler
wxImage_CanRead
wxImage_CanReadStream
wxImage_GetImageCount
wxImage_InsertHandler
wxImage_RemoveHandler
wxInitAllImageHandlers
wxNullImage

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Barry Tolnas wrote:

Robin Dunn wrote:

Barry Tolnas wrote:

I'm trying to create a wxImage by calling the constructor that
takes only integer width and height arguments but even the
simplest code throws exceptions. See below. Am I missing
something obvious or is this a bug?

Use wxEmptyImage(width, height)

Thanks for that. I'm still having problems though. 1.
wxEmptyBitmap(width,height) segfaults 2. I can't convert an empty
wxImage to a wxBitmap. I want to fill the image with raster data
computed by my program and then draw it in a window with wxDC.Blit or
DrawBitmap. wxImage.ConvertToBitmap() segfaults and is the wxdocs
say its deprecated in favor of the wxImage constructor. When I use
the wxImage constructor, bitmap=wxBitmap(image), I get this:

Use wxImageFromBitmap(bitmap)

Barry Tolnas wrote in another message:

I'm curious why it is necessary to have a separate Method for making empty Images or Bitmaps. Wouldn't it make more sense to make wxImage(width,height) just behave the same as wxEmptyImage(width,
height)?

Because Python does not support method overloading, so you can not have multiple methods (or constructors) of the same name taking different parameters like you can in C++. The way I have worked around this is to choose one of them to be the normally named method and renaming the others.

SWIG 1.3.x (which I am now using for wxPython 2.5) does support making a single Python wrapper for multiple overloaded methods, but in order to use that feature I lose the ability to use keyword args when calling the function, so I am using it sparingly and still relying on renaming in most cases.

I did some more experiments on Mac OS X and got a program working
using wxEmptyImage() and the wxImage.ConvertToBitmap() (which the
docs say is deprecated in favor of the wxBitmap Constructor which
takes a wxImage but seems to be the only way to get it to work).
However, the same code causes a segfault on my Linux machine, I don't
think my installation is totally hosed because everything else
including wxGLCanvas code seems to work fine.

If you make a small standalone app that shows the problem I'll take a look at it.

As another niggling detail, and I mean this as constructive criticism
not a complaint, it seems like something a little more helpful than "TypeError: String or Unicode type required" would make more sense
to alert users than wxImage(width,height) constructor doesn't work in
wxPython as it appears in the wxWindows documentation. The existing error message seems nonsensical in this instance. What do strings and
Unicode have to do with calling a constructor that takes only
integer arguments?

Look for a constructor that does take a string.

···

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

Chris Barker wrote:

Barry Tolnas wrote:

If anyone knows the reasons behind this particular case (need
for wxEmptyBitmap() ) I'm interested in hearing it just for my own
understanding of this type of problem. I'll hunt back for some of
Robin's prior posts on the topic.

The reason is the same as for a lot of wxPython functions: Python does
not support function overloading the way C++ does. I dopn't know much
C++, but I'm pretty sure the way wxImage, for instance, works is that it
has a set of constuctors, each taking different parameters. The compiler
knows which one you want by the type of the parameters. Python, on teh
other hand, has dynamic typing, so it's not possible to automatically
call the correct constructor byt he types of the arguments it's called
with. You can simulate that by having one function that first checks the
types of the inputs, and then calls the correct constuctor, but that
requires a bunch of handwork and type checking in the wrappers, and
Robin has only done that in a few cases. It's easier to just make
constructors with different names for each of the different C++
constructors:

Yeah, what Chris said... :wink:

···

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

Now I see the problem with wrapping C++ overloaded methods with Python. It sounds like the new SWIG has a way to handle this although having it generate code that checks argument types for every overloaded function is likely to be far less efficient for frequently called functions than simply giving each overloaded method a different name as in the current wxPython.

Below is a sample program that demonstrates the segfault on my linux system along with some GDB output which may prove helpful to anyone interested in the cause of the segfault in Linux.

Thanks,
Barry

--------Python Session-------------------

>>> from wxPython.wx import *
>>> wxVERSION_STRING
'2.4.1.2'
>>> image = wxEmptyImage(10,10)
>>> bitmap = image.ConvertToBitmap()
Segmentation fault

--------GDB Session---------------------

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 16384 (LWP 19053)]
wxApp::GetGdkVisual() (this=0x0) at src/gtk/app.cpp:544
544 if (m_glVisualInfo)
Current language: auto; currently c++
(gdb) l
539
540 GdkVisual *wxApp::GetGdkVisual()
541 {
542 GdkVisual *visual = NULL;
543
544 if (m_glVisualInfo)
545 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
546 else
547 visual = gdk_window_get_visual( wxGetRootWindow()->window );
548
(gdb) backtrace
#0 wxApp::GetGdkVisual() (this=0x0) at src/gtk/app.cpp:544
#1 0x40cdccc8 in __JCR_LIST__ () from /usr/lib/libwx_gtk-2.4.so
#2 0x407351b6 in _wrap_wxImage_ConvertToBitmap ()
   from /usr/lib/python2.3/site-packages/wxPython/wxc.so
#3 0x4006f0e2 in PyCFunction_Call () from /usr/lib/libpython2.3.so.1.0

P.S. libwx_gtk-2.4.so is soft linked to libwx_gtk-2.4.so.0.1.1

Robin Dunn wrote:

···

Chris Barker wrote:

Barry Tolnas wrote:

If anyone knows the reasons behind this particular case (need
for wxEmptyBitmap() ) I'm interested in hearing it just for my own
understanding of this type of problem. I'll hunt back for some of
Robin's prior posts on the topic.

The reason is the same as for a lot of wxPython functions: Python does
not support function overloading the way C++ does. I dopn't know much
C++, but I'm pretty sure the way wxImage, for instance, works is that it
has a set of constuctors, each taking different parameters. The compiler
knows which one you want by the type of the parameters. Python, on teh
other hand, has dynamic typing, so it's not possible to automatically
call the correct constructor byt he types of the arguments it's called
with. You can simulate that by having one function that first checks the
types of the inputs, and then calls the correct constuctor, but that
requires a bunch of handwork and type checking in the wrappers, and
Robin has only done that in a few cases. It's easier to just make
constructors with different names for each of the different C++
constructors:

Yeah, what Chris said... :wink:

Barry Tolnas wrote:

Now I see the problem with wrapping C++ overloaded methods with Python. It sounds like the new SWIG has a way to handle this although having it generate code that checks argument types for every overloaded function is likely to be far less efficient for frequently called functions than simply giving each overloaded method a different name as in the current wxPython.

Below is a sample program that demonstrates the segfault on my linux system along with some GDB output which may prove helpful to anyone interested in the cause of the segfault in Linux.

Thanks,
Barry

--------Python Session-------------------

>>> from wxPython.wx import *
>>> wxVERSION_STRING
'2.4.1.2'
>>> image = wxEmptyImage(10,10)
>>> bitmap = image.ConvertToBitmap()
Segmentation fault

You can't do anything with bitmaps in wxGTK until an app object has been created.

···

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

Robin Dunn wrote:

Barry Tolnas wrote:

Now I see the problem with wrapping C++ overloaded methods with Python. It sounds like the new SWIG has a way to handle this although having it generate code that checks argument types for every overloaded function is likely to be far less efficient for frequently called functions than simply giving each overloaded method a different name as in the current wxPython.

Below is a sample program that demonstrates the segfault on my linux system along with some GDB output which may prove helpful to anyone interested in the cause of the segfault in Linux.

Thanks,
Barry

--------Python Session-------------------

>>> from wxPython.wx import *
>>> wxVERSION_STRING
'2.4.1.2'
>>> image = wxEmptyImage(10,10)
>>> bitmap = image.ConvertToBitmap()
Segmentation fault

You can't do anything with bitmaps in wxGTK until an app object has been created.

BINGO! Well that was an easy fix to implement. I wish I had known that earlier. Did I miss something in the docs? It seems to me that if wxImages are somehow dependent on a wxApp, then either there should be a check to make sure whatever needs to have been done in wxApp has been done or wxApp should provide a factory method for wxImages so Its not possible to do something that makes sense but doesn't work. My mental model for a wxImage was just that it was a datastructure in memory with some methods for creating/manipulating that data. The abstraction does not make it clear there is some other dependency. I suppose this is a wxWindows design flaw and not a wxPython one.

Thanks again for solving my problem. And thank for all your hard work that makes it possible for me to even use this library!

Barry