[PySide] pickling enums (again)

Robin Dunn rdunn at enthought.com
Mon May 6 21:38:02 CEST 2013

John Ehresman wrote:
> On 4/26/13 12:50 PM, Hugo Parente Lima wrote:
>>> How would converting integers into enums after checking that they
>>> were valid cause a segfault?
>> After checking they are valid wont cause any problems, but checking
>> if they are valid on every function call seems like a unnecessary
>> overhead IMO, although I don't have numbers to prove this possible
>> overhead :-)
>> My point is about not doing the check plus converting every integer
>> with a reinterpret_cast.
> Yes, not checking would be a problem.  I tend to think the overhead
> from checking would be negligible.  Does anyone know if PyQt would
> accept an integer in these cases?

I'm not sure if PyQt is doing anything different, but I've been using 
SIP lately (for the new wxPython) and things like pickle or copy module 
operations work fine with enum values.  IIRC SIP implements each enum as 
a class that derives from int and the enum values are instances of that 
class.  This lets there be some type checking when passing enum values 
to a function, ensuring that the parameter is not an enum value from a 
different enum class, but it is still more-or-less as fast as using a 
plain int.  It does allow plain ints to be passed as parameters for enum 
args, so it isn't fool-proof, but it gets a long way down that path.  It 
seems like a good balance to me.

 >>> f = wx.Frame(None)
 >>> f.Show()
 >>> wx.WindowVariant
<class 'wx._core.WindowVariant'>
<class 'wx._core.WindowVariant'>
 >>> f.SetWindowVariant(wx.WINDOW_VARIANT_SMALL)
 >>> f.SetWindowVariant(1)
 >>> f.SetWindowVariant(wx.SHOW_EFFECT_ROLL_TO_BOTTOM)
  Traceback (most recent call last):
    File "<input>", line 1, in <module>
  TypeError: Window.SetWindowVariant(): argument 1 has unexpected type 

Robin Dunn
Software Engineer
Enthought, Inc.
rdunn at enthought.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130506/b1cd9d1e/attachment.html>

More information about the PySide mailing list