Re: Unable to use enums that start with numbers



Hello,

On Tue, Jul 5, 2016 at 11:50 PM, <philip chimento gmail com> wrote:
Hi,

On Thu, Jun 30, 2016 at 12:00 AM Element Green <element elementsofsound org> wrote:
I'm adding GObject introspection to an existing GObject based library (libInstPatch) written in C.  For this purpose I am willing to update the C API to make a good binding, even if it breaks backwards compatibility (though I'd like to avoid that if possible).

I've gone through and annotated the majority of the API and am able to load the binding in Python.  However, I noticed an issue with an enum, which looks like the following:

typedef enum
{
  IPATCH_SAMPLE_INVALID = 0,
  IPATCH_SAMPLE_8BIT    = 1,
  IPATCH_SAMPLE_16BIT   = 2,
  IPATCH_SAMPLE_24BIT   = 3,
  IPATCH_SAMPLE_32BIT   = 4,
  IPATCH_SAMPLE_FLOAT   = 5,
  IPATCH_SAMPLE_DOUBLE  = 6,
  IPATCH_SAMPLE_REAL24BIT = 7
} IpatchSampleWidth;


The enum ends up being Ipatch.SampleWidth, which is as expected.  However, the values turn in to 8BIT, 16BIT, 24BIT, and 32BIT which are invalid Python variable names.  It seems like g-ir-scanner should have warned about this.

Any tips on the best way to fix these?  For the moment I changed them to read IPATCH_SAMPLE_BIT8 (BIT8), etc. and added #defines for the old names for backwards compatibility with the C API, but I was wondering if there was a better way.  I tried rename-to, but that doesn't seem to work on a per enum value basis.

There's an example of this in the GDK API as well: GDK_2BUTTON_PRESS which maps to Gdk.EventType.2BUTTON_PRESS (invalid syntax) in Python. The alternate name DOUBLE_BUTTON_PRESS is provided, and you could do something similar directly in the enum to preserve backwards compatibility. This would be slightly more consistent than using macros, since then you'd have both names available in your GI API, but I don't think it would matter that much.

IPATCH_SAMPLE_8BIT = 1,
IPATCH_SAMPLE_BIT8 = IPATCH_SAMPLE_8BIT,
...etc.

However, it seems that PyGObject also automatically detects this situation and adds another alias, Gdk.EventType._2BUTTON_PRESS, as shown in the Python documentation for Gdk. [1] I assume it does something similar for your enum as well? But I don't believe this happens in other GI bindings such as _javascript_ or Vala.

You can also access the original name in Python with getattr(Ipatch.SampleWidth, '8BIT'), but this is not a very user-friendly API.


Regards,
Philip C


That is all very useful information.  At this point I've made the choice to prioritize a clean GI binding over trying not to break the C API, so I just changed the enum.  I did end up adding the C API defines though, but with the (skip) property to cut down on binding clutter.

I know it isn't really the same topic, but I have not been able to figure out how to call an initialization function with my library, when it gets loaded from the gi.repository.  Is this possible?  It seems like the Gtk examples I have seen don't require Gtk.init() to be called, so I'm assuming this is being done at load time.  If you happen to know anything about this, that would be quite helpful.

Thanks again and best regards,

Element Green


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]