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


Groups > comp.lang.python > #56584

Passing C pionters to Python for use with cffi

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <eric.frederich@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.06; 'correct,': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; 'language.': 0.14; "api's": 0.16; 'fine.': 0.16; 'thanks,': 0.17; 'code.': 0.18; 'module': 0.19; 'python?': 0.22; 'instead.': 0.24; 'pointer': 0.24; 'pass': 0.26; 'certain': 0.27; 'function': 0.29; 'correct': 0.29; 'skip:p 30': 0.29; '(like': 0.30; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'extending': 0.31; 'struct': 0.31; 'allows': 0.31; 'this.': 0.32; 'run': 0.32; 'url:python': 0.33; "i'd": 0.34; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:org': 0.36; 'application': 0.37; 'skip:& 10': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'skip:& 20': 0.39; 'structure': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'first': 0.61; 'within': 0.65; 'here': 0.66; 'good,': 0.91; 'step.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=HQfqa1AS5Hh+tG9T4DvgbRduku+r8Z1zcE50TedNCP8=; b=sVGnkFpyVhgyejkuyXWRdGfDKU1ZfrGqh5IAVD/r++d2Zo/KxrapwoYRZaFn+31Enc ive8+AETLJrguQJ7BjCWk36alnOXdYHwHwPq4bsjlk72A/1Ze4Sk8WUk1J9wTlGSYurJ MEYFnwCeyodI3nU2fWrYDTV4r2BqHCaeoh6uLGtuCPsvSfReKH9vpYkwQxkl+REkRuO3 j7+MBypBxZwQZCp6yLZyGHxx7PuOAN1zziaUntw/M5t5j3cmZweY24YvRgp/ZIz+2d6t 6/SB0mCE1M8Upn7GfSCLosCPnN+KrOsCQHPFN+2O0BKY1XbWfjjmc6m8mffdEezf3y1o JTzA==
MIME-Version 1.0
X-Received by 10.52.119.228 with SMTP id kx4mr12466088vdb.12.1381420069413; Thu, 10 Oct 2013 08:47:49 -0700 (PDT)
Date Thu, 10 Oct 2013 11:47:49 -0400
Subject Passing C pionters to Python for use with cffi
From Eric Frederich <eric.frederich@gmail.com>
To python-list@python.org
Content-Type multipart/alternative; boundary=047d7bdc0a9c85eacd04e864eca4
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.950.1381420076.18130.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1381420077 news.xs4all.nl 15909 [2001:888:2000:d::a6]:39740
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:56584

Show key headers only | View raw


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

Hello,

I'm extending an application that supports customization using the C
language.
I am able to write standalone python applications that use the C API's
using cffi.
This is good, but only a first step.

This application allows me to register code that will run on various events
but it has to be C code.
I'd like to write Python code instead.
So basically, my C code will use the Python C API to get a handle to the
module and function (like how they do it here
http://docs.python.org/2/extending/embedding.html#pure-embedding)
What would be the proper way to pass a pointer to a C structure from C to
Python so that I can use ffi.cast and be able to use it from within Python?

I have got this to work but I'm not certain that it is correct, fool-proof,
or portable.

This is how I got it to work from the C side....

PyObject* pArgs = Py_BuildValue("(k)", &some_structure);
PyObject_CallObject(pFunc, pArgs)

... and from the Python side...

def my_function(struct_ptr):
    struct = ffi.cast("mystruct_t *", struct_ptr)


Like I said, this works fine.  I am able to manipulate the structure from
within Python.
I just want to know the correct way to to this.

Thanks,
~Eric

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


Thread

Passing C pionters to Python for use with cffi Eric Frederich <eric.frederich@gmail.com> - 2013-10-10 11:47 -0400

csiph-web