Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Error Handling Improvements and except*
Lecture: No Cost Exceptions, Seen Through Byte Code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here's another one of those features that is either for error handling or is for performance.
0:07 You know what leans a little bit more towards the error handling.
0:10 So we're going to talk about it now, but it just as well could fit into the chapter where we
0:14 talk about performance and that is zero cost exception handling. Did you know that if you were to write proper python code with try except blocks,
0:23 you paid a price for that? Not a huge price, but there was a performance cost even when it succeeded. Well, as part of the faster c python team,
0:33 Mark Shannon and a bunch of other folks as you can see here worked on making exception handling basically free not
0:43 have any overhead unless there's an error and in which case it becomes slightly more expensive but that's the exceptional.
0:49 Not the standard case. So I think that's the right choice, let's play with that. So let's call this zero cost exceptions first.
0:58 I just want to start by having a little bit of code to play with and just seeing how it works So let's drop this in here. We've got a troubling method.
1:08 Was it troubling. Well, it may take a crash, it may take a header, so we pass in some kind of string and its job is to convert
1:16 it to an integer and it just tries goes into of X. That better work. We might get a value error and says,
1:23 Oh no, well that didn't work. In which case it returns -1.
1:27 It might get some weird or like maybe the thing we passed in had this ability to be converted to an
1:33 end but it through an exception, say it wasn't implemented or we just don't know what's wrong.
1:38 Right? Something went wrong. It has all these print statements. Obviously that was probably not what you would do but no matter what case,
1:46 either returns this value or this. So we call it with a good number like this,
1:51 Let's do our name convention here and try again. We get 77 So 77 is great,
1:59 we do it 901. We can even call it with I know 2 as an integer and it should work
2:09 But you know the typing says don't do that but we're gonna do it anyway. Right? What if we pass this number 9 900 will that work?
2:21 Let's try. Oh no, it couldn't be converted because of values invalid, literal intent with a base 10 900 we got negative one.
2:32 Alright, well that didn't work. Standard python code, I mean it's a little contrived but this is kind of how you're supposed to do it right?
2:41 You try to do the thing, you catch the exceptions, you handle them, deal with it. So it turns out that to write this part,
2:50 even if everything succeeds in previous versions of python, there was a price to be paid. So let's see try from dis import this Now this is a thing,
3:02 we can call on a function that will or class or different things that will show us the python byte code
3:11 commands. So basically the assembly language, the lowest level building blocks of what python actually does because a lot
3:19 of times you'll see one line like this, like oh, that's one or two lines, that's a bunch of things happening.
3:25 The python has to actually do to make that work. For example, pointing out that it's entering an exception block.
3:32 Okay, so in addition to this, let's just do dis of troubling method, take away that run it and there we have Oh,
3:47 a bunch of stuff. All right, now, this is only somewhat interesting on its own. Let's actually take this, let's go out and run this two ways here,
4:02 Way on the left will say Python 3 11, this and we'll say Python 3.10 this what I want
4:10 to do is compare the byte code that comes out from each of them and show you the actual byte code
4:17 that python uses, what actually happens in the python run time is different now than it used to be.
4:24 So there's this one, there's that one notice, first of all, there's an exception table. That's kind of interesting. Well,
4:32 let's go to the top where we right there to resume. The first, the first line there. This one starts at eight,
4:39 but that's fine. We get the top resume kind of nothing. No up is kind of nothing. Then we load fast the X,
4:46 the value of X and then we do a pre call and I call this is I believe an optimization from
4:52 a specialized interpreter. We'll get to that later in the course and then we return the value.
4:57 So notice those five those five statements there, there's nothing about exception handling. But if you get past that you don't do the return value,
5:08 then you push the exception information, you load up the value error or you load up the not implemented.
5:14 So you do this extra work right here and then you go into the exception handling. That's 3.11. That's the new, the new goodness over here.
5:22 What's the very first thing set up? The finally the final thing that you're doing and then we're gonna run down here, do the stuff that we already did,
5:31 that's this. And then we're gonna pop things worked. It looks like we're gonna pop the exception block and return the value.
5:39 Otherwise we get into the exception handling. So notice 3.11,
5:44 If things go right, we don't see anything about exceptions or finally or working with the exception blocks.
5:51 It's only when there is an error that we pay the price. So pretty cool. Other than that though, it should have run exactly the same.
6:00 We should have 77 901 1 null, it couldn't convert and we do, behaves the same its just a matter of performance


Talk Python's Mastodon Michael Kennedy's Mastodon