How to use C enum in Python CTypes?

Discussion in 'Python' started by rozniy, Aug 7, 2007.

  1. rozniy

    rozniy Guest

    I am using Python's CTypes module to import functions from a DLL into
    Python. Some of the DLL's functions take enum type arguments, eg:

    olDaGetDASS (HDEV hDev, OLSS OlSs, UINT uiElement, PHDASS phDass);

    //(this function is in C form)

    >From the header file, I found that type OLSS is an enum, like this:



    typedef enum olss_tag
    {
    OLSS_AD,
    OLSS_DA,
    OLSS_DIN,
    OLSS_DOUT,
    OLSS_SRL,
    OLSS_CT
    }
    OLSS;

    I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
    but I am not sure how translate a C enum into Python...

    This site
    http://python.net/crew/theller/ctypes/tutorial.html#bugs-todo-and-non-implemented-things

    says that enumeration types is not implemented,
    "Enumeration types are not implemented. You can do it easily yourself,
    using c_int as the base class."

    How do I do this? I am fairly new to Python, coming in from C++ (of
    which I am utterly sick!)
    rozniy, Aug 7, 2007
    #1
    1. Advertising

  2. On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:

    > typedef enum olss_tag
    > {
    > OLSS_AD,
    > OLSS_DA,
    > OLSS_DIN,
    > OLSS_DOUT,
    > OLSS_SRL,
    > OLSS_CT
    > }
    > OLSS;
    >
    > I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
    > but I am not sure how translate a C enum into Python...
    >
    > This site
    > http://python.net/crew/theller/ctypes/tutorial.html#bugs-todo-and-non-implemented-things
    >
    > says that enumeration types is not implemented,
    > "Enumeration types are not implemented. You can do it easily yourself,
    > using c_int as the base class."


    I would just define constants:

    (OLSS_AD,
    OLSS_DA,
    OLSS_DIN,
    OLSS_DOUT,
    OLSS_SRL,
    OLSS_CT) = map(ctypes.c_int, xrange(6))

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Aug 7, 2007
    #2
    1. Advertising

  3. rozniy

    rozniy Guest

    On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <> wrote:
    > On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:
    > > typedef enum olss_tag
    > > {
    > > OLSS_AD,
    > > OLSS_DA,
    > > OLSS_DIN,
    > > OLSS_DOUT,
    > > OLSS_SRL,
    > > OLSS_CT
    > > }
    > > OLSS;

    >
    > > I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
    > > but I am not sure how translate a C enum into Python...

    >
    > > This site
    > >http://python.net/crew/theller/ctypes/tutorial.html#bugs-todo-and-non...

    >
    > > says that enumeration types is not implemented,
    > > "Enumeration types are not implemented. You can do it easily yourself,
    > > using c_int as the base class."

    >
    > I would just define constants:
    >
    > (OLSS_AD,
    > OLSS_DA,
    > OLSS_DIN,
    > OLSS_DOUT,
    > OLSS_SRL,
    > OLSS_CT) = map(ctypes.c_int, xrange(6))
    >
    > Ciao,
    > Marc 'BlackJack' Rintsch- Hide quoted text -
    >
    > - Show quoted text -



    Wouldn't that assign integer values 0 to 5 to the things? I don't know
    if it'll give me the correct results.
    rozniy, Aug 7, 2007
    #3
  4. On Tue, 07 Aug 2007 02:13:38 -0700, rozniy wrote:

    > On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <> wrote:
    >> On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:
    >> > This site
    >> >http://python.net/crew/theller/ctypes/tutorial.html#bugs-todo-and-non...

    >>
    >> > says that enumeration types is not implemented,
    >> > "Enumeration types are not implemented. You can do it easily yourself,
    >> > using c_int as the base class."

    >>
    >> I would just define constants:
    >>
    >> (OLSS_AD,
    >> OLSS_DA,
    >> OLSS_DIN,
    >> OLSS_DOUT,
    >> OLSS_SRL,
    >> OLSS_CT) = map(ctypes.c_int, xrange(6))

    >
    > Wouldn't that assign integer values 0 to 5 to the things?


    Yes.

    > I don't know if it'll give me the correct results.


    It should. Enumerations in C are more or less just integer constants.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Aug 7, 2007
    #4
  5. rozniy

    Jim Langston Guest

    "rozniy" <> wrote in message
    news:...
    > On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <> wrote:
    >> On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:
    >> > typedef enum olss_tag
    >> > {
    >> > OLSS_AD,
    >> > OLSS_DA,
    >> > OLSS_DIN,
    >> > OLSS_DOUT,
    >> > OLSS_SRL,
    >> > OLSS_CT
    >> > }
    >> > OLSS;

    >>
    >> > I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
    >> > but I am not sure how translate a C enum into Python...

    >>
    >> > This site
    >> >http://python.net/crew/theller/ctypes/tutorial.html#bugs-todo-and-non...

    >>
    >> > says that enumeration types is not implemented,
    >> > "Enumeration types are not implemented. You can do it easily yourself,
    >> > using c_int as the base class."

    >>
    >> I would just define constants:
    >>
    >> (OLSS_AD,
    >> OLSS_DA,
    >> OLSS_DIN,
    >> OLSS_DOUT,
    >> OLSS_SRL,
    >> OLSS_CT) = map(ctypes.c_int, xrange(6))
    >>
    >> Ciao,
    >> Marc 'BlackJack' Rintsch- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    >
    > Wouldn't that assign integer values 0 to 5 to the things? I don't know
    > if it'll give me the correct results.


    Yes, that's how C's and C++'s enums work, unless an override is given (which
    I know you can do in C++ for sure). Otherwise, they are just numbers
    starting at 0. The size of the intergers (byte, 2 bytes, 4 bytes, etc..) is
    implemenation defined I believe, but an int normally works.
    Jim Langston, Aug 7, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    526
  2. Jerminia
    Replies:
    3
    Views:
    615
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    446
    Rolf Magnus
    Nov 13, 2003
  4. Henk Punt
    Replies:
    0
    Views:
    391
    Henk Punt
    Jul 23, 2004
  5. Replies:
    0
    Views:
    495
Loading...

Share This Page