Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

FORTRAN is the language of Powerful Computers. -- Steven Feiner


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.328.1628450673.4164.python-list@python.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: brgl@bgdev.pl (Bartosz Golaszewski)
Newsgroups: comp.lang.python
Subject: Re: Defining a Python enum in a C extension - am I doing this right?
Date: Sun, 8 Aug 2021 21:24:21 +0200
Lines: 43
Message-ID: <mailman.328.1628450673.4164.python-list@python.org>
References: <CAMRc=MehhhAGOt=u3qpYzHRhgqpeStew30qiLRNoA5mScgf=PA@mail.gmail.com>
<se0cqh$13ol$1@ciao.gmane.io>
<CAMRc=MeZtzZuotXL8SUZ=G68HYV4ogbXg9bvFayY3RpHZeM71Q@mail.gmail.com>
<CAMRc=MfUQswzi2Xpwrp69T8B7yDYiNOnT8iwft9C+Dn7hi_4qw@mail.gmail.com>
<seipb5$4dp$1@ciao.gmane.io>
<CAMRc=Md6K3rWr5dsY0+mdXe3AOZuUnYFEENLr2aNHndO9zfdNg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de ZppzuLZF5s5fyiosTni40A6L0ijiiAqntJuUt0fLfhxA==
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=u1oDo6v5; dkim-adsp=unknown (unprotected policy);
dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(e.g.': 0.04; 'that?':
0.07; 'api,': 0.09; 'aug': 0.09; 'cc:addr:python-list': 0.09;
'code?': 0.09; 'module.': 0.09; 'subject:Python': 0.12; 'import':
0.14; 'attributes': 0.16; 'bart': 0.16; 'cc:name:python': 0.16;
'dummy': 0.16; 'follow-up:': 0.16; 'subject:extension': 0.16;
'\xd0\xbf\xd0\xb8\xd1\x88\xd0\xb5:': 0.16; 'wrote:': 0.16;
'python': 0.16; 'instead': 0.17; 'code.': 0.19;
'cc:addr:python.org': 0.19; 'fri,': 0.23; 'code': 0.24; 'idea':
0.25; 'cc:2**0': 0.27; "i'll": 0.28; 'module': 0.28; 'think':
0.31; 'code,': 0.31; 'thanks,': 0.31; 'message-
id:@mail.gmail.com': 0.33; 'class': 0.33; 'header:In-Reply-To:1':
0.33; 'received:google.com': 0.34; 'received:209.85.208': 0.36;
'inside': 0.37; 'received:209.85': 0.38; "it's": 0.38;
'received:209': 0.38; 'use': 0.38; 'skip:h 10': 0.61;
'subject:this': 0.63; 'your': 0.64; 'required': 0.66; 'easy':
0.75; '2021': 0.84; 'attribute': 0.84; 'bartosz': 0.84; 'stable':
0.91
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:content-transfer-encoding;
bh=PUfH+UWkLoeaJ3FbOlVzHJo910RxRxXfu9dpVSEnnqE=;
b=u1oDo6v5hzcA+eBfVT2UvejZojDMnQHuDn1fRZkjnvIyFsqBEyNI6KaKpyWcAfHY8D
xFDkexBwM78VbQegOfFCrThkSXNPDUhit223tluvi8MZogzsaBqeFeolfsFy5RRZ88mM
UNHowsYlNNt2oomdFhik9dURSaNN0RNcG6sjG9NUFeu0mI1iglgARsGkX1Ck3/CrwOae
x4achrYuvck/ugqFpstfSuKd2iKgfOVCN9V+0kLosSawP4vPxurO/teIV/GAkYsoEZlw
f4G7xXO6lwxHOKw1BD+NzO+BwB2ayPnf4oL233CzVO2+8SHA0FsmOVPxY2hq1/lARY62
6s8w==
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:content-transfer-encoding;
bh=PUfH+UWkLoeaJ3FbOlVzHJo910RxRxXfu9dpVSEnnqE=;
b=KXC0Zk0DVB/IQEsFlanZKtWEeQEJW2sNAzuTUqba+kehnk6cbuU1pZcrukVX4I0u5u
GAg8fivhLmeqJnOTbERikGcjeK6drDvS3xLHG/skq0u3yY6a3jJA/m9/CTQGR+x5MpDm
8lDXsrD4L+Xus8Z4ZvEb/bR/JXBx9FCh6XajrKxhauzNLeXkT6TBEwoNMa761LOunjBm
BqzWdj6dbK2laqNht5bU2hP5kZOgzLFg+16PZdVWxorUfh6+iTjA5Vqwx5ZC+MRYUp8j
YL9+UO31qdxRraOkbzUBHC3rZdhq/39u9on+JOnLLsSzIA+3EH1FRcmM0IDMtlq3AZIe
E5qQ==
X-Gm-Message-State: AOAM530iLwah8izClPMM3kMfQj91hzCz4BSXiR/LqWG8RG4lk/D+Asrd
dPsvsrYVpCtqd/Y79Dw6ibBlVdWmoAGazJg0k6S3Hg==
X-Google-Smtp-Source: ABdhPJy4m8UIal1MnA0r72TvU/cO/0oX+PRg5amqxDz5a56s376RFQLcPFCH4PCEs/AZ6sjdpKCHo6hY4oyXV3T1/TU=
X-Received: by 2002:a05:6402:1395:: with SMTP id
b21mr24768315edv.119.1628450672268;
Sun, 08 Aug 2021 12:24:32 -0700 (PDT)
In-Reply-To: <seipb5$4dp$1@ciao.gmane.io>
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=Md6K3rWr5dsY0+mdXe3AOZuUnYFEENLr2aNHndO9zfdNg@mail.gmail.com>
X-Mailman-Original-References: <CAMRc=MehhhAGOt=u3qpYzHRhgqpeStew30qiLRNoA5mScgf=PA@mail.gmail.com>
<se0cqh$13ol$1@ciao.gmane.io>
<CAMRc=MeZtzZuotXL8SUZ=G68HYV4ogbXg9bvFayY3RpHZeM71Q@mail.gmail.com>
<CAMRc=MfUQswzi2Xpwrp69T8B7yDYiNOnT8iwft9C+Dn7hi_4qw@mail.gmail.com>
<seipb5$4dp$1@ciao.gmane.io>
 by: Bartosz Golaszewski - Sun, 8 Aug 2021 19:24 UTC

On Fri, Aug 6, 2021 at 9:02 PM Serhiy Storchaka <storchaka@gmail.com> wrote:
>
> 03.08.21 13:03, Bartosz Golaszewski пише:
> > Just a follow-up: this is how I did it eventually:
>
> I think it can be simpler.
>
> 1. No need to create the __main__ module. You can just create a dict. If
> some attributes are required (e.g. __name__) it is easy to set them in
> the Python code (__name__ = 'pycenum').
>
> 2. No need to import the enum module in the C code. It is easier to do
> it in the Python code.
>

Don't you need the '__import__' attribute for that? It's linked to point #1..

> 3. Can you define your DummyType in the Python code instead of the C
> code? Even if you need to create in the C code, it may be better to
> define your enum class inside a dummy DummyType. It will set correct
> __qualname__ of the enum class:
>

This code is a PoC for the real code I want to use it in where the
type embedding the enum is defined in C.

> __name__ = 'pycenum'
> import enum
> class DummyType:
> class FooBar(enum.Enum):
> ...
>
> 4. Did you consider idea of making DummyType a heap-allocated type? It
> may be easy to create new type with PyType_FromModuleAndSpec(), and it
> is more stable API, and the type is not immutable, so it is easy to add
> new attributes with PyObject_SetAttrString().
>

Makes sense, thanks, I'll try it.

Bart


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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor