top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Calling Python macro from ctypes

0 votes
567 views

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 "", line 1, in 
 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
posted Aug 12, 2013 by Amit Parthsarthi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

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 workedn")
 ENDIF
}

And here's what the compiler sees:

$ gcc -E -P macro.c

main()
{
 if (1>0) {
 printf("It workedn")
 ;}
}
answer Aug 12, 2013 by Majula Joshi
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.
http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
Similar Questions
+1 vote

What all things need to be taken care while writing macro function which has multiple lines ?

+1 vote

On what factors, cell coverage is decided and how does an operator chose ?
Is it always good to chose broader coverage ?

+1 vote

Suppose a macro eNodeB supports multiple cells. Does each cell has its own instance for each layer or one instance of each protocol layer can handle more than one cell ?

+2 votes

I have a pile of C code that I wrote that I want to interface to via the ctypes module ( https://docs.python.org/3/library/ctypes.html ).

The C code uses the Boehm-Demers-Weiser garbage collector ( http://www.hboehm.info/gc/ ) for all of its memory management. What I want to know is, who owns allocated memory? That is, if my C code allocates memory via GC_MALLOC() (the standard call for allocating memory in the garbage collector), and I access some object via ctypes in python, will the python garbage collector assume that it owns it and attempt to dispose of it when it goes out of scope?

Ideally, the memory is owned by the side that created it, with the other side simply referencing it, but I want to be sure before I invest a lot of time interfacing the two sides together.

...