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


Groups > comp.lang.python > #52423

Re: Calling Python macro from ctypes

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.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 <python-python-list@m.gmane.org>
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; 'yet.': 0.04; 'subject:Python': 0.06; 'compiler': 0.07; 'tool,': 0.07; '#define': 0.09; 'function,': 0.09; 'main()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'undefined': 0.09; 'windows,': 0.09; 'python': 0.11; 'assume': 0.14; 'endif': 0.16; 'file).': 0.16; 'func': 0.16; 'introduces': 0.16; 'macros': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'rules.': 0.16; 'subject:ctypes': 0.16; 'symbols': 0.16; 'url:peps': 0.16; 'wrote:': 0.18; 'library': 0.18; 'bit': 0.19; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'dll': 0.24; 'url:dev': 0.24; "haven't": 0.24; 'looks': 0.24; "i've": 0.25; 'source': 0.25; 'skip:" 40': 0.26; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; '"",': 0.31; 'ctypes': 0.31; "d'aprano": 0.31; 'gcc': 0.31; 'steven': 0.31; 'file': 0.32; 'know.': 0.32; 'linux': 0.33; 'url:python': 0.33; '(most': 0.33; 'subject:from': 0.34; 'possible.': 0.35; 'skip:s 30': 0.35; 'tool': 0.35; 'more,': 0.35; 'but': 0.35; 'really': 0.36; 'c++': 0.36; 'library.': 0.36; 'processed': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'similar': 0.36; 'url:org': 0.36; 'example,': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'even': 0.60; 'read': 0.60; 'new': 0.61; 'real': 0.63; 'such': 0.63; '366,': 0.84; 'otten': 0.84; 'together,': 0.84; 'examine': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: Calling Python macro from ctypes
Date Mon, 12 Aug 2013 17:11:30 +0000 (UTC)
References <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> <kuahlo$rtj$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.30
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.505.1376327513.1251.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376327513 news.xs4all.nl 15953 [2001:888:2000:d::a6]:56319
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52423

Show key headers only | View raw


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
>> 
>> 
>> So I try this in 3.3:
>> 
>> py> import ctypes
>> py> ctypes.pythonapi.PyUnicode_MAX_CHAR_VALUE
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>>   File "/usr/local/lib/python3.3/ctypes/__init__.py", line 366, in
>> __getattr__
>>     func = self.__getitem__(name)
>>   File "/usr/local/lib/python3.3/ctypes/__init__.py", line 371, in
>> __getitem__
>>     func = self._FuncPtr((name_or_ordinal, self))
>> AttributeError: python3.3: undefined symbol: PyUnicode_MAX_CHAR_VALUE
>
> That's not possible. It may look like a function, but a preprocessor 
> replaces the C macro in the C source before compilation. An example of very 
> bad usage of macros, just to drive the point home:
>
> $ cat macro.c 
> #define IF(expr) if (expr) {
> #define ENDIF ;}
>
> main()
> {
>   IF(1>0)
>     printf("It worked\n")
>   ENDIF
> }
>
> And here's what the compiler sees:
>
> $ gcc -E -P macro.c
>
>
>
> main()
> {
>   if (1>0) {
>     printf("It worked\n")
>   ;}
> }
>

To elaborate a bit more, Python can only see those symbols that are put
into the shared library  They can be functions, and they can be
"values," but they don't include macros, which are processed by the
preprocessor, before the real C compiler even starts.  C Macros are
actually text-substitution rules.  They can look like functions, but
those functions do not end up in the shared library.

In Windows, you can use dumpbin to examine a DLL and see what symbols it
exports.  I don't remember the syntax; it's been years.

I assume there's a similar tool for Linux to examine a shared library
(typically an .so file).  Perhaps "readelf" and/or "nm" is such a tool,
but I don't really know. Although I've been using Python and C++ in
Linux in recent years, I haven't used them together, and neither have
I had to examine a shared library.

The following link looks interesting, but I haven't read it yet.

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

-- 
DaveA

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


Thread

Calling Python macro from ctypes Steven D'Aprano <steve@pearwood.info> - 2013-08-12 10:01 +0000
  Re: Calling Python macro from ctypes Peter Otten <__peter__@web.de> - 2013-08-12 13:42 +0200
    Re: Calling Python macro from ctypes Steven D'Aprano <steve@pearwood.info> - 2013-08-13 06:25 +0000
      Re: Calling Python macro from ctypes Stefan Behnel <stefan_ml@behnel.de> - 2013-08-13 10:11 +0200
        Re: Calling Python macro from ctypes Steven D'Aprano <steve@pearwood.info> - 2013-08-13 09:13 +0000
      Re: Calling Python macro from ctypes Peter Otten <__peter__@web.de> - 2013-08-13 12:25 +0200
  Re: Calling Python macro from ctypes Dave Angel <davea@davea.name> - 2013-08-12 17:11 +0000

csiph-web