Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

The only thing cheaper than hardware is talk.


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?Serhiy Storchaka

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: storchaka@gmail.com (Serhiy Storchaka)
Newsgroups: comp.lang.python
Subject: Re: Defining a Python enum in a C extension - am I doing this right?
Date: Fri, 6 Aug 2021 10:45:38 +0300
Lines: 28
Message-ID: <mailman.323.1628276395.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>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de UmOxHr/pdpMSOPCia3OxMA1gqZPca7Y+igCafuAX7vtw==
Return-Path: <python-python-list@m.gmane-mx.org>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(e.g.': 0.04; 'api,':
0.09; 'code?': 0.09; 'message-id:@ciao.gmane.io': 0.09; 'module.':
0.09; 'received:ciao.gmane.io': 0.09; 'received:gmane.io': 0.09;
'received:list': 0.09; 'subject:Python': 0.12; 'import': 0.14;
'attributes': 0.16; 'dummy': 0.16; 'follow-up:': 0.16;
'from:addr:storchaka': 0.16; 'from:name:serhiy storchaka': 0.16;
'received:116.202': 0.16; 'received:116.202.254': 0.16;
'received:116.202.254.214': 0.16; 'subject:extension': 0.16;
'\xd0\xbf\xd0\xb8\xd1\x88\xd0\xb5:': 0.16; 'python': 0.16;
'instead': 0.17; 'code.': 0.19; 'to:addr:python-list': 0.23;
'code': 0.24; 'idea': 0.25; 'module': 0.28; 'header:User-Agent:1':
0.31; 'think': 0.31; 'code,': 0.31; 'class': 0.33; 'header:In-
Reply-To:1': 0.33; 'from:addr:gmail.com': 0.35; 'inside': 0.37;
'skip:h 10': 0.61; 'subject:this': 0.63; 'your': 0.64; 'required':
0.66; 'received:116': 0.71; 'easy': 0.75; 'bartosz': 0.84;
'stable': 0.91
X-Injected-Via-Gmane: http://gmane.org/
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
In-Reply-To: <CAMRc=MfUQswzi2Xpwrp69T8B7yDYiNOnT8iwft9C+Dn7hi_4qw@mail.gmail.com>
Content-Language: en-US
X-Mailman-Approved-At: Fri, 06 Aug 2021 14:59:54 -0400
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: <seipb5$4dp$1@ciao.gmane.io>
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>
 by: Serhiy Storchaka - Fri, 6 Aug 2021 07:45 UTC

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.

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:

__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().


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