Search code examples
pythonpycharmbreakpointsbuilt-in

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?


Solution

  • 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).