Re: Unable to use enums that start with numbers



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.

[1] http://lazka.github.io/pgi-docs/index.html#Gdk-3.0/enums.html#Gdk.EventType._2BUTTON_PRESS

Regards,
Philip C


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