Zvika
Zvika

Reputation: 1692

PyCharm: break on built in function (such as `print`)?

How can I make PyCharm break on built in function, such as print? I've jumped to print's "Declaration" with Ctrl-B, and got to a PyCharm stub file: C:\Users\Zvika\AppData\Local\JetBrains\PyCharm2022.1\python_stubs\-185531349\builtins.py

Which has:

def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
    # I've omitted the docstring
    pass

But it's not very useful, as PyCharm cannot put breakpoints on pass.

Any idea how can I break on print?

Upvotes: 1

Views: 196

Answers (1)

STerliakov
STerliakov

Reputation: 7877

If you need this for debugging only, then the following will work:

import builtins

def my_breakpoint(*args, **kwargs):  # Ingore arguments
    breakpoint()

# Redefine `print` builtin
builtins.print = my_breakpoint

print('foo')
# Drops into pdb

And python_stubs are only stubs, they provide information about argument and return types of functions. They have nothing to do with the real implementation (which is in C, of course).

Upvotes: 3

Related Questions