6

When I run my Python debugger, I can step into functions that I write. But if I try to step into a library function like os.mkdir("folder"), for example, it "steps over" it instead. Is there a way to step into builtin library functions to see what Python is doing under the hood?

Ideally there'd be a way to do this in PyPy so that you could keep drilling down into Python code.

smci
  • 32,567
  • 20
  • 113
  • 146
twasbrillig
  • 17,084
  • 9
  • 43
  • 67

2 Answers2

8

pdb, the Python Debugger, cannot step into C functions like os.mkdir, but gdb can. Try this:

gdb --args python whatever.py ...

Then:

start
break posix_mkdir
continue

You should see it stop inside Python's implementation of os.mkdir, as detailed here: https://stackoverflow.com/a/16617835/4323

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
6

os.mkdir() is implemented in C code and pdb cannot step into that function.

You are limited to debugging pure Python code only; it doesn't matter if that code is part of the standard library or not. You can step into the shutil module, or os.path just fine, for example.

os.mkdir() has to call into native code because it interacts with the OS; even PyPy has to defer to the underlying (host-Python) os.mkdir() call to handle that part, so you cannot step into it with pdb even in PyPy. In fact, just like in CPython, that part of the standard library is part of the RPython runtime and not seen as 'native Python code' by PyPy either, just like the built-in types are part of the runtime environment.

You could run the PyPy interpreter untranslated (so not statically compile the RPython code but have Python run the PyPy interpreter directly), but that'll only give you access to the RPython code paths, not the os.mkdir() C code.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    @twasbrillig: functions that wrap OS functions still need to use natively compiled code, even in PyPy. – Martijn Pieters Oct 05 '14 at 01:50
  • Thanks. Just curious though, what about a builtin, non-OS function like "string".index for example? Can that be stepped into using PyPy? – twasbrillig Oct 05 '14 at 01:55
  • 1
    @twasbrillig: actually, you cannot step into code part of the rpython objects either; like the Python built-in types, in PyPy the built-in types are part of RPython and cannot be their code stepped into with the pdb debugger in PyPy. – Martijn Pieters Oct 05 '14 at 02:23
  • Oh well. Thanks again for investigating it! – twasbrillig Oct 05 '14 at 02:24