Decompile .cpp

310 views
Skip to first unread message

Yaidel Ferrales

unread,
Aug 1, 2015, 2:16:41 PM8/1/15
to cython-users
Hello, I want to convert a .cpp file generated using cython into the original .pyx. Do you know any tool I can use to do that?

Thanks in advance.

Robert Bradshaw

unread,
Aug 1, 2015, 2:36:17 PM8/1/15
to cython...@googlegroups.com
Actually converting the C/C++ code back into pyx would be difficult, and wouldn't likely yield very pythonic (let alone the original) source. However, you could probably extract a lot from the comments, e.g.

from collections import defaultdict
import re
import sys

all = defaultdict(dict)

for line in sys.stdin:
    m = re.match(r'/\* "(.*\.pyx)":(\d+)', line)
    if m:
        file = m.group(1)
        lineno = int(m.group(2)) - 2
    elif '*/' in line:
        lineno = None
    elif lineno is not None:
        all[file][lineno] = line[2:].rstrip()
        lineno += 1

for file, lines in all.items():
    print '=' * 20, file, '=' * 20
    for lineno, line in sorted(lines.items()):
        print lineno, ':', line



On Sat, Aug 1, 2015 at 2:34 AM, Yaidel Ferrales <yaidelf...@gmail.com> wrote:
Hello, I want to convert a .cpp file generated using cython into the original .pyx. Do you know any tool I can use to do that?

Thanks in advance.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Bradshaw

unread,
Aug 1, 2015, 2:46:44 PM8/1/15
to cython...@googlegroups.com
Fixed the regex, try

from collections import defaultdict
import re
import sys

all = defaultdict(dict)

lineno = None
for line in sys.stdin:
    m = re.match(r' */\* "(.*\.pyx)":(\d+)', line)
    if m:
        file = m.group(1)
        lineno = int(m.group(2)) - 2
        print "found", m.groups()
    elif '*/' in line:
        lineno = None
    elif lineno is not None:
        all[file][lineno] = line[2:].rstrip()
        lineno += 1

for file, lines in all.items():
    print '=' * 20, file, '=' * 20
    for lineno, line in sorted(lines.items()):
        print lineno, ':', line

Stefan Behnel

unread,
Aug 2, 2015, 1:55:40 AM8/2/15
to cython...@googlegroups.com
Robert Bradshaw schrieb am 01.08.2015 um 20:35:
> On Sat, Aug 1, 2015 at 2:34 AM, Yaidel Ferrales wrote:
>> Hello, I want to convert a .cpp file generated using cython into the
>> original .pyx. Do you know any tool I can use to do that?
>
> Actually converting the C/C++ code back into pyx would be difficult, and
> wouldn't likely yield very pythonic (let alone the original) source.
> However, you could probably extract a lot from the comments, e.g.
> [...]

Also see the coverage analysis plugin in Cython/Coverage.py which does this.

Stefan

Reply all
Reply to author
Forward
0 new messages