Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python extension debugging

I'm trying to debug an extension module for python that I wrote in C. I compiled it using the following:

python setup.py build -g install --user

I then debug with:

gdb python
...
b py_node_make
run test.py

It breaks at py_node_make (one of the functions I defined), but then I try:

(gdb) print node
No symbol "node" in current context.

The function I'm trying to debug is:

static Python_node_t* py_node_make(
        node_t* node)
{
    Python_node_t* pyNode;

    pyNode = PyObject_New(Python_node_t, &t_node);
    pyNode->node = node;
    pyNode->borrowed = true;

    return pyNode;
}
like image 225
CrazyCasta Avatar asked Mar 06 '13 17:03

CrazyCasta


People also ask

Does VS Code have a Python debugger?

Python debugging in VS Code. The Python extension supports debugging of several types of Python applications. For a short walkthrough of basic debugging, see Tutorial - Configure and run the debugger. Also see the Flask tutorial.

How do I debug a Python file in Visual Studio?

With a stand-alone Python file open, right-click in the editor, select Start with Debugging, and Visual Studio launches the script with the global default environment (see Python environments) and no arguments. But from then on, you have full debugging support.

What's debugging in Python?

A debugger is a program that can help you find out what is going on in a computer program. You can stop the execution at any prescribed line number, print out variables, continue execution, stop again, execute statements one by one, and repeat such actions until you have tracked down abnormal behavior and found bugs.


2 Answers

For source debugging to work, your C extensions must be built with debug info (gcc -g). Since you're driving the compilation process with distutils, you can specify the compiler flags used through the CFLAGS environment variable (Installing Python Modules: Tweaking compiler/linker flags):

CFLAGS='-Wall -O0 -g' python setup.py build

Note that even if distutils defaults to a higher optimization level than -O0, you really shouldn't get that No symbol "node" in current context error as long as -g is passed and most Python builds do pass -g by default.

like image 135
scottt Avatar answered Oct 29 '22 00:10

scottt


The problem is the optimization. I'm not sure how to do it from the command line, but in the setup.py script I just added extra_compile_args=['-O0'], to the Extension constructor and everything worked.

Would still like (and would accept) an answer that involved a command line arg (something after python setup.py build) that would accomplish the same thing so I don't have to have a compiler specific line in my setup.py file.

like image 33
CrazyCasta Avatar answered Oct 28 '22 22:10

CrazyCasta