News for hackers. Stuff that matters.
Re:PySnooper: Never use print for debugging again by badatshipping on Monday April 22, 2019 @01:20PM (#19720704)
This is a thoughtful hack but the real solution here is a) make debuggers easier to set up and b) make your project easily debuggable with a debugger from the beginning.

Like, debugging should be considered part of programming, and a local dev environment that can’t be debugged should be viewed to be as broken as a codebase without e.g. a way to run the server in watch mode.

Also someone should make a debugger that supports the equivalent of print statements, e.g. set print breakpoint on a variable to print its value every time it’s run, instead of typing print everywhere.

Re:PySnooper: Never use print for debugging again by int_19h on Monday April 22, 2019 @10:32PM (#19725114)
I'm not sure what you mean by "debugger equivalent of print statements".

If what you want is a breakpoint that just prints something out when it's hit, then these already exist, e.g.:

https://code.visualstudio.com/docs/editor/debugging#_logpoin...

But "set breakpoint on a variable" kinda sounds more like a data breakpoint (i.e. break/log when value changes)? These also exist in some places:

https://docs.microsoft.com/en-us/visualstudio/debugger/using...

but usually with many limitations. The problem with these is that it's hard to implement it in such a way that there's zero overhead when not debugging. If I remember correctly, native data breakpoints have some hardware support on Intel, but for high-level languages it can be difficult to map their data model to something like that.

Re:PySnooper: Never use print for debugging again by badatshipping on Tuesday April 23, 2019 @12:43AM (#19725671)
Oh wow, I never knew about that. Thanks.
Re:PySnooper: Never use print for debugging again by nitrogen on Monday April 22, 2019 @03:55PM (#19722245)
Also someone should make a debugger that supports the equivalent of print statements, e.g. set print breakpoint on a variable to print its value every time it’s run, instead of typing print everywhere.

Traditonal debuggers like gdb for compiled languages support this (breakpoint actions, memory write breakpoints, and variable displays). If something similar isn't already in your language's debugger, that might be a source of ideas for adding it.

Re:PySnooper: Never use print for debugging again by spc476 on Monday April 22, 2019 @06:38PM (#19723753)
Until such a day comes, `printf()` debugging will still be useful. Heck, even after that day comes, it may still find uses.

The other day I was writing LPEG [1]. I had a rule that wasn't firing for some reason and I wanted to know why LPEG was skipping that part of the input. It was not a simple matter to fire up a debugger and put a breakpoint on the rule:

    -- look for 1 or more ASCII characters minus the colon
    local hdr_name = (R"\0\127" - P":")^1 -- [2]
I mean, I could put a breakpoint there, but that would trigger when the expression is being compiled, not run. LPEG has its own VM geared for parsing (it really is its own language) so yes, it is sometimes difficult to debug (especially when you end up with a large parsing expression to parse a document---I only found the bug when I was creating another document similar to my initial testing document that was just different enough).

Fortunately, there is a way to hook into the parsing done by LPEG and I was able to insert literal print statements during parsing that showed what exactly was going on (my rule was a bit too promiscuous).

[1] Lua Parsing Expression Grammar http://www.inf.puc-rio.br/~roberto/lpeg/

[2] Yes, a regular expression for that is smaller than what I typed, but with LPEG, I can reuse that rule in other expressions. Also, that wasn't the actual rule used, but the effective rule I was using.

Re:PySnooper: Never use print for debugging again by cknd on Monday April 22, 2019 @03:41PM (#19722131)
One situation where I would have wanted a real debugger and couldn't use one was in crash reporting on remote systems (like, machine learning code running in some container somewhere, or on a headless box far from the network).

Since Python's built-in tracebacks are pretty minimal, the default crash logs don't offer much help other than a line number. I ended up writing a tool that prints tracebacks along with code context and local variables [i], sort of a souped up version of the built-in crash message. It's surprising how much that already helped in a few situations, makes me wonder why it isn't the default.

So, yes, more debuggers, but also abundant logging everywhere!

[i] https://github.com/cknd/stackprinter

Re:PySnooper: Never use print for debugging again by Gibbon1 on Monday April 22, 2019 @08:40PM (#19724566)
Old school neckbeard way

a) Crashes dump the program state to a file and then you load that in the debugger.

b) If the process is still running but broken, attach the debugger.

c) In my firmware I log bus fault addresses and restart. That allows me to see what code or memory access caused the error. 75% of the time it's fairly obvious what happened.

Re:PySnooper: Never use print for debugging again by int_19h on Monday April 22, 2019 @10:44PM (#19725160)
Crash dump debugging is tricky to implement Python, because its normal debugging APIs - sys.settrace, frame objects etc - require being inside the process with a running Python interpreter.

You can still do it, but you basically have to redo the whole thing from scratch. For example, instead of asking for a repr() of an object to display its value, you have to access the internal representation of it directly - and then accommodate all the differences in that between various Python versions. Something like this (note several different dicts): https://github.com/Microsoft/PTVS/tree/bcdfec4f211488e373fa2...

Re:PySnooper: Never use print for debugging again by yoz on Tuesday April 23, 2019 @12:40AM (#19725660)
Python's standard lib has had something like this for years, but it's been criminally under-promoted (probably because of the terrible name): https://docs.python.org/3/library/cgitb.html

However, yours provides much nicer output. Thank you!

Re:PySnooper: Never use print for debugging again by josteink on Monday April 22, 2019 @01:26PM (#19720767)
Yeah. Python is dead easy to debug via pdb.

If you don’t want to go CLI-debugging both VSCode and Emacs have more integrated, in-editor options.

Many bugs can be isolated and reproduced in unit-tests. That’s just a click away from being debuggable inside a real debugger. Why use anything but that?

To me, not using a debugger to debug seems kinda crazy.

Re:PySnooper: Never use print for debugging again by JHer on Monday April 22, 2019 @05:20PM (#19723107)
Debugging python processes running in containers unfortunately requires a bit of setup in VS Code (no idea about Emacs).
Re:PySnooper: Never use print for debugging again by AtHeartEngineer on Thursday May 02, 2019 @05:17PM (#19813057)
True...but inheriting code, not having time to refactor, and just getting a basic version out the door...pysnooper is pretty useful.
Re:PySnooper: Never use print for debugging again by strunz on Monday April 22, 2019 @01:51PM (#19721008)
I love using PDB, but even still this has a different use case. Say you have a bug in a function and it depends on timing or it's a race condition, or it has a lot of statements. Being able to see the state throughout the function in your output may be able to catch things or speed up debugging.
Re:PySnooper: Never use print for debugging again by BenFrantzDale on Monday April 22, 2019 @04:52PM (#19722863)
Agreed. My go-to for debugging is `import epdb;epdb.st()` which is a quick and dirty breakpoint. There may be more elegant solutions but that has served me very well.
Re:PySnooper: Never use print for debugging again by askvictor on Monday April 22, 2019 @06:00PM (#19723438)
True, though pysnooper (and print statements for that matter) will change the execution speed, so won't necessarily catch timing bugs (or introduce new ones).
Re:PySnooper: Never use print for debugging again by nijave on Monday April 22, 2019 @07:38PM (#19724186)
Or just use a logger with proper log levels
Re:PySnooper: Never use print for debugging again by OrgNet on Monday April 22, 2019 @02:22PM (#19721321)
With python being a language that tries to only have one way of doing each thing, maybe they should have figured that out at the beginning stages of the language's design phase.