Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Thank heaven for startups; without them we'd never have any advances." -- Seymour Cray


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?MRAB

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pyt...@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Defining a Python enum in a C extension - am I doing this right?
Date: Fri, 23 Jul 2021 16:03:03 +0100
Lines: 34
Message-ID: <mailman.245.1627052772.4164.python-list@python.org>
References: <CAMRc=MehhhAGOt=u3qpYzHRhgqpeStew30qiLRNoA5mScgf=PA@mail.gmail.com>
<d62e460e-3be6-5ae8-dd9b-c8e14caebf2e@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de HLHLRkCbMHLSir3oYvHlpw6BcRZc11NUbTWwRyf25Kgw==
Return-Path: <python@mrabarnett.plus.com>
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=plus.com header.i=@plus.com header.b=iCResKt6;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.009
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'this:': 0.03; 'mechanism':
0.07; 'from:addr:python': 0.09; 'pulled': 0.09;
'received:192.168.1.64': 0.09; 'trivial': 0.09; 'subject:Python':
0.12; '[snip]': 0.16; 'expose': 0.16;
'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16;
'message-id:@mrabarnett.plus.com': 0.16; 'received:plus.net':
0.16; 'static': 0.16; 'subject:extension': 0.16; 'wrote:': 0.16;
'python': 0.16; 'basically': 0.23; 'to:addr:python-list': 0.23;
'extension': 0.24; 'skip:p 30': 0.26; '(as': 0.27; 'task': 0.27;
'header:User-Agent:1': 0.31; 'received:192.168.1': 0.31;
"doesn't": 0.32; "i'm": 0.32; 'using': 0.33; 'class': 0.33;
'header:In-Reply-To:1': 0.33; 'trying': 0.36; 'received:192.168':
0.37; 'likely': 0.61; 'skip:* 20': 0.63; 'subject:this': 0.63;
'in.': 0.65; 'received:212': 0.68; 'reference.': 0.81; 'bartosz':
0.84; 'inheritance': 0.84; 'null;': 0.84; 'steal': 0.84; 'turned':
0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1627052584; bh=fYIZcmCiXiIp5RD4tfDD7LdAZ3EjEwgVF7s0iLQJ/yY=;
h=Subject:To:References:From:Date:In-Reply-To;
b=iCResKt6MQKCMYfi4fFRzGHCRGUHDqXJunD8GoZc4eFr7BR4YqRwYK85h289r4JtU
6qlx4B6PZwLcs1PBlbZt/0MEcGwq/Fb+bRgnwJoUYMMThKjURyZPJMN5tIbA50+FWz
c53c6K9Jp4bPBHLWcA3SUY+QnQDp+Osc8UmJE3PgxbF0mXZ4gbfiX/j+o9A1nwJR0Q
CXUQIMidTV/JfJZW1vvu5AkzAWL+rQ8PyhroKWpojxOeNAc9oEliyEf+10iqDYvPM/
B0PAemu5f6w8Io2vNYNqDtctGt6GGUChu3tf8MCk6JG2MebCaO27LkusffQzw7itSK
QrAg8UXAtVfrQ==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.3 cv=H+BAP9Qi c=1 sm=1 tr=0
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=YT3Wa3Zn3yGhESYU:21 a=IkcTkHD0fZMA:10 a=UkHb8ZEHnTeTH0yDep8A:9
a=QEXdDO2ut3YA:10 a=u2eN_BWj8lB7kqIn7vVk:22
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
In-Reply-To: <CAMRc=MehhhAGOt=u3qpYzHRhgqpeStew30qiLRNoA5mScgf=PA@mail.gmail.com>
Content-Language: en-GB
X-CMAE-Envelope: MS4wfCgV0oSnFg9d9HtO72qqTTgzcXJBiqRDPDQMBu/4SFsDpAYXH1dypd+wFg1GI28sda2xbtX3gdBJEVlx4g5VYvZN0Ap1DSJ50cJxotlCSC6JBI32/srx
yPudk9V0fTG/9dbwrdjwi1r8/MPkYoJGb567imhAbzjBh5WXr0JJ7W8/ubqtprToVjDcUGiuNkKk/g==
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: <d62e460e-3be6-5ae8-dd9b-c8e14caebf2e@mrabarnett.plus.com>
X-Mailman-Original-References: <CAMRc=MehhhAGOt=u3qpYzHRhgqpeStew30qiLRNoA5mScgf=PA@mail.gmail.com>
 by: MRAB - Fri, 23 Jul 2021 15:03 UTC

On 2021-07-23 09:20, Bartosz Golaszewski 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.
>
> It turned out to not be a trivial task and the regular mechanism for
> inheritance using .tp_base doesn't work - most likely due to the
> Enum's meta class not being pulled in.
>
> Basically I'm trying to do this:
>
[snip]
>
> static PyObject *make_bases(PyObject *enum_mod)
> {
> PyObject *enum_type, *bases;
>
> enum_type = PyObject_GetAttrString(enum_mod, "Enum");
> if (!enum_type)
> return NULL;
>
> bases = PyTuple_Pack(1, enum_type); /* Steals reference. */

PyTuple_Pack doesn't steal references, as far as I can tell.

> if (!bases)
> Py_DECREF(enum_type);
>
> return bases;
> }
>
[snip]

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor