Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'subject:Python': 0.06; 'compiler': 0.07; 'debug': 0.07; 'level,': 0.07; 'versions,': 0.07; 'string': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'api': 0.11; 'python': 0.11; 'abi': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'introduces': 0.16; 'macros': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:ctypes': 0.16; 'underlying': 0.16; 'url:peps': 0.16; 'wrote:': 0.18; 'properly': 0.19; 'stefan': 0.19; '>>>': 0.22; 'aug': 0.22; 'header:User- Agent:1': 0.23; 'platform,': 0.24; 'url:dev': 0.24; 'mon,': 0.24; 'source': 0.25; 'define': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'code': 0.31; 'ctypes': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'struct': 0.31; 'stuff': 0.32; 'url:python': 0.33; 'actual': 0.34; 'maybe': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'could': 0.34; 'possible.': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; '+0200,': 0.36; 'possible': 0.36; 'url:org': 0.36; 'example,': 0.37; 'level': 0.37; 'configured': 0.38; 'to:addr :python-list': 0.38; 'that,': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'manually': 0.60; 'new': 0.61; "you're": 0.61; 'different': 0.65; 'surprise': 0.74; 'otten': 0.84; 'received:87.139': 0.84; 'directly.': 0.95; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: Calling Python macro from ctypes Date: Tue, 13 Aug 2013 10:11:29 +0200 References: <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> <5209d161$0$29885$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: p578ba676.dip0.t-ipconnect.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 In-Reply-To: <5209d161$0$29885$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376381506 news.xs4all.nl 15920 [2001:888:2000:d::a6]:39735 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52442 Steven D'Aprano, 13.08.2013 08:25: > On Mon, 12 Aug 2013 13:42:14 +0200, Peter Otten wrote: >> Steven D'Aprano wrote: >> >>> Is it possible to call a Python macro from ctypes? For example, Python >>> 3.3 introduces some new macros for querying the internal representation >>> of strings: >>> >>> http://www.python.org/dev/peps/pep-0393/#new-api > [...] > >> That's not possible. It may look like a function, but a preprocessor >> replaces the C macro in the C source before compilation. > > That's what I feared. > > In that case, how would I use ctypes to access the underlying fields in > the new string implementation? I'd personally use Cython (no surprise here), but in order to use something like ctypes, which works at the ABI level, not the API level, and doesn't use a C compiler to get things properly configured for the local platform, you'd have to manually define the actual PyObject struct in order to access its fields directly. The macros are there to give you source code level portability for that, but if you start defining the struct layout statically, you're pretty much on your own when it comes to stuff like different CPython versions, debug builds, etc. Could you describe your use case a little deeper? Maybe there's a better way all together to do what you want. Stefan