Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

This place just isn't big enough for all of us. We've got to find a way off this planet.


devel / comp.lang.python / Using my routines as functions AND methods

SubjectAuthor
o Using my routines as functions AND methodsGuenther Sohler

1
Using my routines as functions AND methods

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: guenther...@gmail.com (Guenther Sohler)
Newsgroups: comp.lang.python
Subject: Using my routines as functions AND methods
Date: Wed, 3 Jan 2024 23:47:20 +0100
Lines: 59
Message-ID: <mailman.118.1704322055.2892.python-list@python.org>
References: <CAASeUHrrsPPe2sqDFjZRLdYTuWoUg1uWNzKRz5QKAhA=T2E0ew@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de x3LuLwb7ouFKhR1TwAiAuQboJ7vhooCQ3jA2DQUDbUMQ==
Cancel-Lock: sha1:UV53P2pyQ69gNNXPRVsVAMD/cQU= sha256:g5clDPfco79BCUA7V36oxYTr3TKhU8bEB16yurNcj+I=
Return-Path: <guenther.sohler@gmail.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=gmail.com header.i=@gmail.com header.b=aEFcP1fm;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.020
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'int': 0.09; 'methods,': 0.09; 'syntax': 0.15; '&lt;': 0.16;
'-&gt;': 0.16; 'cpython': 0.16; 'lambda': 0.16; 'reference,':
0.16; 'reuse': 0.16; 'structure.': 0.16; 'tuple': 0.16; 'python':
0.16; 'uses': 0.19; 'bug': 0.19; 'to:addr:python-list': 0.20;
'written': 0.22; 'returns': 0.22; 'goal': 0.23; 'function': 0.27;
'approach': 0.31; 'modify': 0.31; '(this': 0.32; 'subject:Using':
0.32; 'message-id:@mail.gmail.com': 0.32; 'but': 0.32; 'there':
0.33; 'same': 0.34; 'fill': 0.34; 'received:google.com': 0.34;
'item': 0.35; 'from:addr:gmail.com': 0.35; 'functions': 0.36;
'using': 0.37; 'class': 0.37; '8bit%:14': 0.38; 'quite': 0.39;
'list': 0.39; 'use': 0.39; 'methods': 0.39; 'want': 0.40;
'method': 0.61; 'skip:o 10': 0.61; 'skip:\xc2 10': 0.62; 'skip:*
20': 0.62; 'skip:o 20': 0.63; 'great': 0.63; '100%': 0.66;
'reach': 0.69; '1);': 0.84; 'obj': 0.84; 'i++)': 0.91; 'that:':
0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1704322053; x=1704926853; darn=python.org;
h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
:date:message-id:reply-to;
bh=MtbFu2n+X+7Uq/lP4IgYJu8K0nqETTBPRGtxi/UgEEQ=;
b=aEFcP1fmYzhKSBBSgh/CHH4rucU4hiIl0gnPmjFU2H+KaZLWgK8/S01PghkzA5aBOE
k6HDVT8ubVFlnpWYq+nrY3g9oOXeFaVb7Ss6idUCf3AXJ7nNj46EjC844P7UYZDGukAi
NFeRpwf9t3v/hv+1Vth7c4w7gmQpQEDgbf/7GwYCg2xMzaTIA3ZDPb3K80nd8zh1UvDq
MKZr0LpF629CF7R8DCmJz8tS5CtNIcoQJFUHMhyZLEXSWOLXCNJ4ZIoKMrsTKSldZMB+
IDsr8/ehrbJdirLaSkWfX7zLWzdv37yzJqA19PpvGMi8EaZsnlYpKqZKwtK9QEK2Cfb7
UiQA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1704322053; x=1704926853;
h=to:subject:message-id:date:from:mime-version:x-gm-message-state
:from:to:cc:subject:date:message-id:reply-to;
bh=MtbFu2n+X+7Uq/lP4IgYJu8K0nqETTBPRGtxi/UgEEQ=;
b=jYyF2VOE1k9QS9LaD06moD8KQ6hQcUTMJOQkGXGx+tuhYySDWvZZ27pyDa5O8WsI6p
C1uVZklb9ZgnNzNhORLoeTOYvRSBLau8dZxfdxdJ/GSK10+UIbwM7wQWLPKNB3qJ94er
sKKCyzsJyq8uWfhkGO3P9yr4HLlJGm696CuAsE+VbFHmW8kziyRBfxGYxf0YReEMrgpa
y16euO5HuFd2Eu7DnJ3drOoNg/sROcEEsugnuy8xoRRpjgjr8cCdeasM5+sieohZWFHm
Xw3vDH/pWqoxDlWR8V88NE2NieHWic2ylykja+0eKQQkLxOGefgbQT8ciQ1fgWbgMvJx
r+Lw==
X-Gm-Message-State: AOJu0YwuyOZbIA7yivpUKKVsqyTbX4b9nncjEgeiNYJzf9DwkUCuDlSI
61DUtmRCIopP+hmjsWEMqPacjuF67SFk1FmVHv/tRud1MLd+Gw==
X-Google-Smtp-Source: AGHT+IFlZqI6slIoe+8GsJXOv+vsaqvxpAY2zN7Raif+ZZXrsVA0A+c2dW9XVVLFX6OHnRNicoRqevT49I1Z+QU3E9U=
X-Received: by 2002:a2e:b8d2:0:b0:2cc:97af:fdf4 with SMTP id
s18-20020a2eb8d2000000b002cc97affdf4mr8926290ljp.103.1704322052595; Wed, 03
Jan 2024 14:47:32 -0800 (PST)
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <CAASeUHrrsPPe2sqDFjZRLdYTuWoUg1uWNzKRz5QKAhA=T2E0ew@mail.gmail.com>
 by: Guenther Sohler - Wed, 3 Jan 2024 22:47 UTC

Hi,

In my cpython i have written quite some functions to modify "objects".
and their python syntax is e.g.\

translate(obj, vec). e.g whereas obj is ALWAYS first argument.

on c side this functions looks like:
PyObject *python_translate(PyObject *self, PyObject *args, PyObject *kwargs)

this works great when specifying a list of functions in PyModuleDef
structure.

However, I also want to use these functions as class methods without having
to
write the function , twice. When using the SAME function as a methos, the
args tuple must insert/contain "self" in the first location, so i have
written a function to do that:

PyObject *python_oo_args(PyObject *self, PyObject *args) // returns new
reference,
{ int i;
PyObject *item;
int n = PyTuple_Size(args);
PyObject *new_args = PyTuple_New(n + 1);
PyTuple_SetItem(new_args, 0, self);

for (i = 0; i < PyTuple_Size(args); i++) {
item = PyTuple_GetItem(args, i);
PyTuple_SetItem(new_args, i + 1, item);
}
return new_args;
}

To fill in method array, i have created a #define like this:

#define OO_METHOD_ENTRY(name,desc) \
{ #name, (PyCFunction) ( [ ] (PyObject *self, PyObject *args) -> PyObject
* { \
PyObject *new_args = python_oo_args(self, args); \
PyObject *result = python_##name(self, new_args, NULL); \
return result; } ), METH_VARARGS | METH_KEYWORDS, (desc)},

(this uses a lambda function)

and use this in the array as:

PyMethodDef PyOpenSCADMethods[] = {
OO_METHOD_ENTRY(translate,"Move Object")
OO_METHOD_ENTRY(right,"Right Object")

Using this i can reuse all the functions as methods,
but its not 100% stable/bulletproof and crashes sometimes.
So there is the bug ?
Is there a better approach to reach my goal ?

thank you

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor