Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Name Mangling Library for Python [closed]

I'd like to mangle and demangle C++ function names in a Python program. Is there anything like that available? I searched for hours now, perhaps I'm lucky here...

like image 861
manuels Avatar asked Jun 29 '11 19:06

manuels


People also ask

How to disable name mangling in C++?

dllexport of a C++ function will expose the function with C++ name mangling. To disable name mangling either use a . def file (EXPORTS keyword) or declare the function as extern “C”.

What is name mangling in Python?

Python. In Python, mangling is used for class attributes that one does not want subclasses to use which are designated as such by giving them a name with two or more leading underscores and no more than one trailing underscore.

Does C have name mangling?

Since C is a programming language that does not support name function overloading, it does no name mangling.

How do you stop the name mangling?

To prevent the C++ compiler from mangling the name of a function, you can apply the extern "C" linkage specifier to the declaration or declarations, as shown in the following example: extern "C" { int f1(int); int f2(int); int f3(int); };


1 Answers

If you want to demangle names, eg. for display, then you can create a pipe that runs c++filt.

def demangle(names):
    args = ['c++filt']
    args.extend(names)
    pipe = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout, _ = pipe.communicate()
    demangled = stdout.split("\n")

    # Each line ends with a newline, so the final entry of the split output
    # will always be ''.
    assert len(demangled) == len(names)+1
    return demangled[:-1]

print demangle(['_ZNSt15basic_stringbufIcSt11char_traitsIcESaIcEE17_M_stringbuf_initESt13_Ios_Openmode',
    '_ZNSt15basic_stringbufIcSt11char_traitsIcESaIcEE6setbufEPci'])

You can specify arguments to c++filt if you need to use a specific demangling method.

Mangling a name is much harder, and probably can't be done without knowing the definitions of the types involved.

like image 54
Glenn Maynard Avatar answered Sep 20 '22 20:09

Glenn Maynard