Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

You need tender loving care once a week - so that I can slap you into shape. -- Ellyn Mustard


devel / comp.lang.python / Re: Defining a Python enum in a C extension - am I doing this right?

SubjectAuthor
o Re: Defining a Python enum in a C extension - am I doing this right?Bartosz Golaszewski

1
Re: Defining a Python enum in a C extension - am I doing this right?

<mailman.260.1627284497.4164.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=14299&group=comp.lang.python#14299

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: brg...@bgdev.pl (Bartosz Golaszewski)
Newsgroups: comp.lang.python
Subject: Re: Defining a Python enum in a C extension - am I doing this right?
Date: Mon, 26 Jul 2021 09:28:04 +0200
Lines: 38
Message-ID: <mailman.260.1627284497.4164.python-list@python.org>
References: <CAMRc=MehhhAGOt=u3qpYzHRhgqpeStew30qiLRNoA5mScgf=PA@mail.gmail.com>
<CAGGBd_okHs_6Norrd2D6zctFm0XYfBL1q5ohr4JvkigHWXX5Ew@mail.gmail.com>
<CAMRc=MdUZ0kkgffLfzJue7BtRKNDFhaNYzPUM1F6dynjGrvP2g@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de s5wvTFpOFMAxX5JCc7SkIANBvWxi5yqrKYsRYhaUzYxw==
Return-Path: <brgl@bgdev.pl>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=bgdev-pl.20150623.gappssmtp.com
header.i=@bgdev-pl.20150623.gappssmtp.com
header.b=NfFOAYyW; dkim-adsp=unknown (unprotected policy);
dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'jul': 0.04;
'kernel': 0.05; 'linux': 0.07; 'python.': 0.07; 'aimed': 0.09;
'anyway,': 0.09; 'cc:addr:python-list': 0.09; 'dan': 0.09;
'flags': 0.09; 'subject:Python': 0.12; '"the"': 0.16; '6:55':
0.16; 'bart': 0.16; 'bindings': 0.16; 'c++': 0.16;
'cc:name:python': 0.16; 'elegant,': 0.16; 'expose': 0.16; 'fast,':
0.16; 'subject:extension': 0.16; 'url:tree': 0.16; 'wrote:': 0.16;
'python': 0.16; 'instead': 0.17; 'api': 0.18;
'cc:addr:python.org': 0.19; 'language': 0.22; 'version': 0.22;
'classes': 0.23; 'fri,': 0.23; 'interface': 0.23; 'sat,': 0.23;
"i'd": 0.23; 'extension': 0.24; 'code': 0.24; 'probably': 0.24;
'written': 0.24; 'cc:2**0': 0.27; '(as': 0.27; 'library': 0.27;
'creating': 0.27; 'module': 0.28; 'but': 0.31; '(with': 0.31;
'question.': 0.31; "i'm": 0.32; '23,': 0.32; 'message-
id:@mail.gmail.com': 0.33; 'using': 0.33; 'class': 0.33; 'header
:In-Reply-To:1': 0.33; 'received:google.com': 0.34; "we're": 0.35;
'missing': 0.35; "we'll": 0.37; 'received:209.85': 0.38; 'author':
0.38; 'something': 0.38; 'received:209': 0.38; 'use': 0.38;
'could': 0.40; 'among': 0.61; 'lot': 0.62; 'subject:this': 0.63;
'look': 0.66; 'similar': 0.66; 'url:c': 0.67; 'order': 0.68;
'speed': 0.69; 'tools': 0.75; 'features': 0.76; '2021': 0.84;
'bartosz': 0.84; 'converted': 0.84; 'easier,': 0.84; 'exposing':
0.84; 'url-ip:136/8': 0.84; 'device.': 0.91; 'url:news': 0.93;
'url-ip:209/8': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=bgdev-pl.20150623.gappssmtp.com; s=20150623;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=3qk1/AOAHWFZ+Dga9abZAVVPzq0ZkAxt8aqiFTA6GtQ=;
b=NfFOAYyW665DN8Cz4WeTzjikiXzVTnlLYNGTSBdKZ5uKLvwcodAMAhq68cNhW4Su4k
ZGLKCyO90rrOQfYFhU2qWQ6DWkFCgfu1qSyRVqGNU7YjdF47Wz5iXAU+0kZxcflsQk0Z
jIAMeSzxdkDG+INUhXwBCNcoBa9Vf9WepcaJQtq/QB6khcdTsWk8+A1QjO/PL/xUANqp
2/IVMpSkiW16HmyNT7FahmlsQlozjyzLvF8UR7x5S7wBrtnPqg6BVM6/41M2C4UoD4Rd
huOh9FoNOU+hNV1YAcORwZwiiVN2HG2xMwBxxF6H8lqhntfDm26pjBBgs9twh1+0d/Nl
8zbg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:cc;
bh=3qk1/AOAHWFZ+Dga9abZAVVPzq0ZkAxt8aqiFTA6GtQ=;
b=thA6y/QJXU86H3RtcLojts29f50bUU1f6Of6ddp8Wb+CG2t5ACWhyUXMnihqtQxUIO
eEAwP9cDzGn+XU8w2O/EcZc+PN/2Tz3zSZ14JOBbXp2MLm+FxcwyIR3mHbLkSScDsY1i
kS1afNC+/FDUeBffEotmTenb3EDtqfD5YFTv3pq7JL0NJW5owbaWngMdrWMeYR/vLBMC
Bo7+D7pceYV0g32DXcokP3Srmem5cgxjsU9tFEGT7fasL82tf0+i3a9Bojjre9WdiVh0
4Xj1H4bgL4ugoXEsPcW3ZawxHJKLJBM2UaYT6bGJM9D0E4rpp/pvqBOR+lrdzgQjN2fs
GARw==
X-Gm-Message-State: AOAM530q+O+zzhnMcCV726/Imt4JfZy5UzgLyL74zFzQSxxajXn21a6p
J/WlUh9xDiLd/0xPWOzBHm98XKGi3aBMeRrpepUbkA==
X-Google-Smtp-Source: ABdhPJx9OeEn04IVtRAOlFhpg1kEHbnY8DFPGUBOQCibTizbl4I0a4vRDPkjjiukC5Q0IqPTHBGBAs8v+vX2vQpqArE=
X-Received: by 2002:a17:906:5f99:: with SMTP id
a25mr15964910eju.101.1627284495092;
Mon, 26 Jul 2021 00:28:15 -0700 (PDT)
In-Reply-To: <CAGGBd_okHs_6Norrd2D6zctFm0XYfBL1q5ohr4JvkigHWXX5Ew@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAMRc=MdUZ0kkgffLfzJue7BtRKNDFhaNYzPUM1F6dynjGrvP2g@mail.gmail.com>
X-Mailman-Original-References: <CAMRc=MehhhAGOt=u3qpYzHRhgqpeStew30qiLRNoA5mScgf=PA@mail.gmail.com>
<CAGGBd_okHs_6Norrd2D6zctFm0XYfBL1q5ohr4JvkigHWXX5Ew@mail.gmail.com>
 by: Bartosz Golaszewski - Mon, 26 Jul 2021 07:28 UTC

On Sat, Jul 24, 2021 at 6:55 AM Dan Stromberg <drsalists@gmail.com> wrote:
>
>
> On Fri, Jul 23, 2021 at 1:20 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>>
>> Hi!
>>
>> I'm working on a Python C extension and I would like to expose a
>> custom enum (as in: a class inheriting from enum.Enum) that would be
>> entirely defined in C.
>
>
> I'm probably missing something obvious, but why would you write new code in C when you can just use Cython? Cython is a lot easier, and quite fast, and should (eventually?) allow compiling to HPY instead of just "the" C extension module interface.
>
> https://news.ycombinator.com/item?id=26627683
>

I'm the author and maintainer of libgpiod - the user-space library and
tools for using the linux GPIO character device.

The core library is written in C but we're also exposing C++ and
Python bindings (with more language bindings planned). The python
bindings are written as a C extension module and look like this:
https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python/gpiodmodule.c.

We're in the process of writing the (backward incompatible) version 2
of the library in order to support the new kernel features and the C
API has changed a lot so we're also rewriting the bindings. Among
others: all bitwise flags have now been converted to enums, hence my
question. In C++ we'll use scoped enum classes and I'd like to do a
similar thing in python.

What I'm doing is not aimed at using C for speed but for calling the C
APIs. I know I could use SWIG but in order to make the interface
elegant, it would have to be packaged in proper Python classes anyway,
creating another layer of code so I prefer to just use C.

Bart

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor