PyCon 2014

Usos avanzados del PDB, el depurador de Python

Nathan Yergler  · 


Extracto de la transcripción automática del vídeo realizada por YouTube.

alright hi everybody I'd like to introduce Nathan yer glure who's going to talk about in depth PDB thank you my name is Nathan I'm a principal engineer at eventbrite and today I'd like to talk to you about PDB this talk is all about how to

find and remove bugs from your software if you're interested in adding bugs there are other talks available so now is the time to perhaps check those out not that any of my fellow presenters are anyway um so I'd like to put a couple URLs up first just

to hold one second there we go ok so I'm going to be showing a bunch of examples of using PDB in a couple different contexts and I had got some feedback during run throughs that might be hard I hope they're not hard to read I think I've tried to

adjust the font appropriately but you can follow along % to Tron if you if you so desire so we're here to talk about PDB the Python debugger and I hope that by the end of this talk i will have convinced you that pdb is better than using prints for debugging

that pd well you know there are the tried and true staples and there's a reason you know it takes some energy to break out of those that you'll know how to use pdb to explore a running program and that you'll be convinced that pdb is a useful tool

to understand so that you can extend it to behave in the way that's most useful for you so what sort of things can you actually do with PDB you can look around running code this is an example of a program that we're going talk about later it's

a good little calculator of course and in this case I've been just looking at what a value of a variable is but more interestingly than this you can also look around pythons code so if i wanted to know what OS dot path join does when i actually call it

i can stop pythons execution inside of this function and I can do this without editing files inside of my system Python and I can in this case discover that it's actually calling a module called POSIX path which who knew I can also return to the scene

of a crime if I have a piece of code like this that creates a server and when I actually make a call to it raises exception because the address is already in use I can actually use PDB to jump back into the program act for the exception has already occurred

and inspect the state as it was when the program died so these are all things we're gonna talk about everybody prints everybody uses print as I tried and true debugging mechanism but PDB is better pdb lets you in Splore the state of a running program or

a dead one unless you repeatedly run your program so you can debug it and actually get into a flow of debugging as you work with a piece of software and it lets you build the tools you need to extend it so that it's appropriate for your particular use

cases let's start with a little bit of pdb one on one and just want to lay a little bit of groundwork so that we're all on the same page around how to use pdb the most common way to get into pdb is with set trace so set trace is a function that tells

Python stop immediately when you get here and you can see with the green arrow on the left hand part of the code is my little Fibonacci program there's a green arrow pointing to my set trace line and when I actually run that on the right hand side you'll

see that Python stops before it gets to the print statement and that it gives me the PDB prompt and lets me know it's about to do this next line so from here there's a few really basic commands that I just want to cover briefly so that we so that we

all know about them and kind of understand the difference I can say next if I wanted to execute the entire next statement so in this case you'll see that oh yeah people tweet at me it's going to buzz on my watch so that'll be fun you can see if

I did next here it's going to execute that entire print statement for me so it's going to call fib with sis arg v it's going to print it out you'll see the eight there lo next it's going to print the dash dash return which means I've

reached the end of a block and there's going to wait for the next command so next execute the current line and any calls it might have to make this is in contrast to step if I did this again and just run step step also execute the current line but stops

as soon as it possibly can so in this case it's inside of that fibbed function and at this point now I could actually continue to step or next and work through that when I figured out what I need to know where I'm ready to continue writing the program

there's the continued command cont very cunningly named but this leaves the debugger and lets you continues actually executing the program so those are sort of when i'm working with more junior engineers those are the three commands that we do first

and we like get some mastery up because they're going to form the basis of almost everything else we look at so there's next step continue and if you press enter you'll repeat the previous command except if it's list in which case we'll

talk about that so the PDB prompt can look a little foreign I just want to point out it it looks for him because it's meant for speed once you understand how to use it and you understand what the commands are you can operate very very quickly and you can

very easily understand what's going on in your program so you can always ask for help about any of these commands except the undocumented ones of course and and get in get some context so we've looked at how to enter PDB directly at a particular point

but more useful in my experience it's actually executing code under PDP's control and so this is what we're going to do for the rest of the presentation is look at how this works and then dive into some more ways you can extend PDB for your particular

uses pdb like a lot of python modules can be executed as a script so python am pdb and then what you're actually going to run this is going to stop your program before it does anything so in this case you can see it stopped at Fibonacci pie and line 1

it's at the module level and it's getting ready to or cysts so it's done nothing at this point you can use this even for running Django under PDB so that you can do your django run server and set a breakpoint which we'll talk about or do some

other sort of introspection of lower levels of your web app before your program starts you can also do this there's also these run and run call functions I don't use these a whole lot unless I may be working an interactive prompt you know trying to

understand a piece of code a little better but this lets you I think that's pretty clear the run with quotes basically execs a little bit of a code and run call takes a callable and some arguments executed under PDB and again it stops it immediately it's

sort of interesting that when you do run with the quoted code that it stops inside of string one module as opposed to the actual thing because it hasn't actually exact it yet so if we can run our code under control enter PDB immediately we now have the

necessary understanding to actually debug under PDB and help us fix some buggy software I should point out that this software is this little program is in the github repo with the slides so so I wrote a small program that of course does a postfix notation

[ ... ]

Nota: se han omitido las otras 3.712 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.