Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87093

Re: Adding a 'struct' into new python type

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jason.swails@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; "subject:' ": 0.07; 'etc).': 0.09; 'subject:into': 0.09; 'translate': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; '"test"': 0.16; "'c'": 0.16; '*i*': 0.16; '8bit%:32': 0.16; 'cc:name:python list': 0.16; 'char,': 0.16; 'dependency,': 0.16; 'fine.': 0.16; 'luck,': 0.16; 'subject:Adding': 0.16; 'subject:type': 0.16; 'typedef': 0.16; 'types,': 0.16; 'subject:python': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'example': 0.22; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'char': 0.24; 'pointer': 0.24; 'test.': 0.24; 'cc:2**0': 0.24; 'class.': 0.26; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'int,': 0.31; 'object.': 0.31; 'struct': 0.31; 'this.': 0.32; 'another': 0.32; 'basic': 0.35; "can't": 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'entry': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'similar': 0.36; 'example,': 0.37; 'turn': 0.37; 'subject:new': 0.38; 'skip:& 10': 0.38; 'jason': 0.38; 'handle': 0.38; 'sure': 0.39; 'how': 0.40; 'skip:\xc2 10': 0.60; 'conversion': 0.61; 'new': 0.61; 'simple': 0.61; 'linked': 0.65; 'skip:\xe2 10': 0.65; 'to:addr:gmail.com': 0.65; 'believe': 0.68; 'mar': 0.68; 'to,': 0.72; 'potentially': 0.81; '2015': 0.84; 'double,': 0.84; 'float,': 0.84; 'struct.': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=1MVZ0ciHCpEVUD3Hh8cQmcjG9uj+b0R6Q2Pfoh5UhJU=; b=llv/9t74xtd179ay7Ams3Arc/X6h3EmWQiyyOxHqXADUCNNyxzFsQv0pYly3YnL+dK vsXon81L1sEx3wMlogpE0ZrJi1eKuRWlF6yrmrQDo2GYNug0Egc9zXbA/JAJow/TIDsB 4D+KoT8dzD6yzJeihnvmYaKdt2TzBlnRn/YxdbxRFbns5lMLELD4AaGP0TaB2TNGMWFj hnHOQHjUZVYPKy/+aEEkAmh3KXAVMVE7M6UVqoC+0pnFt+uG+kG9xHnCsrCC1A8QAG9O PPW9ZxuQqBsXG1Nw416/jP0ayksqz/TDkwW95FGVVF7BnIQpN04q7TA8NrTijBz/zpek QJhA==
MIME-Version 1.0
X-Received by 10.236.32.4 with SMTP id n4mr18645685yha.128.1425743585262; Sat, 07 Mar 2015 07:53:05 -0800 (PST)
In-Reply-To <CAKuJGC_NxRa8MPGihJ9y3DoOvkvkpp0tzeeXZkfqJJ+2vowtzg@mail.gmail.com>
References <CAKuJGC_NxRa8MPGihJ9y3DoOvkvkpp0tzeeXZkfqJJ+2vowtzg@mail.gmail.com>
Date Sat, 7 Mar 2015 10:53:05 -0500
Subject Re: Adding a 'struct' into new python type
From Jason Swails <jason.swails@gmail.com>
To "Lakshmipathi.G" <lakshmipathi.g@gmail.com>
Cc python list <python-list@python.org>
Content-Type multipart/alternative; boundary=001a11c1bae6f0c2a30510b4cb54
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
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 <http://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>
Newsgroups comp.lang.python
Message-ID <mailman.144.1425743593.21433.python-list@python.org> (permalink)
Lines 156
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425743593 news.xs4all.nl 2883 [2001:888:2000:d::a6]:35048
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87093

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Sat, Mar 7, 2015 at 4:15 AM, Lakshmipathi.G <lakshmipathi.g@gmail.com>
wrote:

> Hi,
>
> I'm following this example :
> http://nedbatchelder.com/text/whirlext.html#h_making_a_type and trying
> to add
> new data into 'CountDict' type
>
> Adding a simple 'char' works well.
>
> typedef struct {
>    PyObject_HEAD
>    PyObject * dict;
>    int count;
>    char c;  //add this and placed an entry into PyMemberDef as T_CHAR.
> } CountDict;
>
> I can access  'c' from python code,no issues so far.
>
> Now I want to added 'struct type' into this 'CountDict' type.
> struct test {
> int x;
> };
>
> typedef struct {
>    PyObject_HEAD
>    PyObject * dict;
>    int count;
>    char c;
>    struct test t1; //?? how to add this
> } CountDict;
> ​​
> ​
>

​​
>
> ​​
> How to do achieve this? (Due to legacy code dependency, I can't use
> ​​
> ctype/cpython etc).
> ​​
> thanks for any help/pointers.
>

​The way *I* would do this is turn struct test into another Python object.
Then instead of defining it in CountDict as type "struct test", define it
as the PyStructTest that you assigned it to when you turned it into a
Python class.

For example, in that website you linked to, CountDict was turned into a
CountDictType Python type.  So do something similar to "test" in which you
turn it into a PyStructTest type in the same way.  Then declare it as a
PyStructTest pointer instead of a struct test.

I'm not sure you can get around an approach like this.  I believe that in
order to access data from Python, it needs to be a Python type.  The
struct-to-Python-type conversion *knows* how to translate basic types, like
char, double, float, int, and long into their Python equivalents; but it
can't handle something as potentially complicated as an arbitrary struct.
Of course, if all you want to do is access t1 from C, then I think what you
have is fine.

​Good luck,
Jason

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Adding a 'struct' into new python type Jason Swails <jason.swails@gmail.com> - 2015-03-07 10:53 -0500

csiph-web