Latest / Talk Python To Me / #240: A guided tour of the CPython source code
Transcript
- 0:00You might use Python every day, but how much do you know about what happens under the covers, down at the C-level?
- 0:06When you type something like variable equals open bracket square bracket to create an empty list,
- 0:11what are the bytecodes that accomplish this?
- 0:14How about the class backing the list itself?
- 0:17All of these details live at the C-layer of CPython.
- 0:21On this episode, you'll meet Anthony Shaw.
- 0:23He and I take a guided tour of the CPython source code.
- 0:26After listening to this episode, you won't have to guess what's happening.
- 0:30You can git clone the CPython source code and see for yourself.
- 0:33This is Talk Python To Me, episode 240, recorded Wednesday, October 30th, 2019.
- 0:53Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 0:59This is your host, Michael Kennedy.
- 1:01Follow me on Twitter, where I'm @mkennedy.
- 1:03Keep up with the show and listen to past episodes at talkpython.fm.
- 1:07And follow the show on Twitter via at Talk Python.
- 1:09This episode is sponsored by Linode and the University of San Francisco.
- 1:14Please check out what they're offering during their segments.
- 1:16It really helps support the show.
- 1:18Anthony, welcome back to Talk Python To Me.
- 1:20Hey, Michael. It's great to be back.
- 1:22Yeah, it's great to have you back.
- 1:23To say you've been on the show before is a bit of an understatement.
- 1:25You were on episode 131, contributed to open source.
- 1:29155, practical steps from moving to Python 3.
- 1:32168, 10 Python security holes and how to plug them.
- 1:35180, what's new in Python 3.
- 1:377, and then 214, diving into 3.
- 1:408.
- 1:40So I think you might be one of the most prolific guests here on the show, which is awesome.
- 1:46I love having you on here.
- 1:47Yeah, thanks for having me back.
- 1:48It's good to be on the show.
- 1:49Yeah, I can't believe that this is the sixth time I've been on here.
- 1:53Yeah, I had to actually use the search engine on the website to figure out how many times you've been here.
- 1:58So yeah, this is going to be a good one because we're going to dive into something that everybody uses.
- 2:04But there's so many dark corners for most folks who are not core developers, right?
- 2:10Like, okay, we all know there's CPython.
- 2:12There's probably other Pythons.
- 2:14You maybe have heard of those like PyPy and whatnot.
- 2:16But can you open up the code?
- 2:19Where do you get it?
- 2:19Where are the important parts, right?
- 2:21It's a huge project.
- 2:22But there's certain parts you really should pay attention to.
- 2:25And the others are like details, right?
- 2:27Yeah, absolutely.
- 2:27I mean, I think if it's kind of like my car, like if I open up the bonnet of my car, I know where the engine is.
- 2:33I know where to put the oil.
- 2:34And that's pretty much it.
- 2:35Like, I don't know what half the other components do.
- 2:37So yeah, I feel like it's like that with CPython sometimes.
- 2:40You know how to use it.
- 2:42But in terms of how it actually works, it's a bit of a mystery.
- 2:45That's a good analogy for sure.
- 2:47Now, before we dig into this, maybe just tell people what it is that you did day to day.
- 2:53So they get a sense of where you're coming from, like in the open source space and in your day to day job.
- 2:57If they want your whole story, they go back to episode 132 and the whole getting started and whatnot.
- 3:02But give us the quick summary there.
- 3:04Yeah, sure.
- 3:04I work for a company called NTT.
- 3:06And I run talent transformation for them.
- 3:09So looking at skills and development of employees for NTT globally.
- 3:14That's my day job.
- 3:15And then I'm also a sort of Python enthusiast and get involved with various open source projects as well.
- 3:21Some Apache projects as well as some of my own personal projects.
- 3:25Yeah, awesome.
- 3:26Like Wiley.
- 3:27Like Wiley, yeah.
- 3:28And been playing a lot with pytest and Azure Pipelines recently as well.
- 3:32Yeah, right on.
- 3:32Cool.
- 3:33So we're going to talk about this whole CPython source code story.
- 3:39And you've been touching on this in several ways.
- 3:42You've been writing some articles.
- 3:43But then you decided to write a book and disguise it as an article.
- 3:49And you called it your guide to the CPython source code over at RealPython.
- 3:53It's excellent.
- 3:54We'll link over to it.
- 3:55But we're going to cover a bunch of the ideas that you touched on in there because this is a really good exploration.
- 4:00But, you know, what got you started in like digging into the source code in the first place?
- 4:04Some of it was curiosity.
- 4:05A few years ago, I wrote an article on how to add an operator to the Python syntax.
- 4:10So how to add a plus plus.
- 4:12So like an in-place increment operator, which Guido is famously against for good reasons.
- 4:18But it was more of an exploration, like how would you actually add that to the syntax and recompile Python, which was really, really interesting to dig into.
- 4:25And also I found that if you want to contribute to CPython, the documentation that there's a site called the dev guide, which is great telling you like the process for raising pull requests, like what the branch strategy is.
- 4:39But if you, you know, if you were to join a new software team, you would expect that in the first few weeks, one of the senior developers would sit you down and walk you through the code and explain how everything works.
- 4:49But that documentation is kind of missing.
- 4:53So I wanted to write something that kind of filled that gap so that if people wanted to get into working on CPython, contributing to it or making tweaks, enhancements or customizations, then there's something that really kind of takes them through in depth, the whole source code and how it works and what each component does.
- 5:10It definitely does that.
- 5:11I feel like these large projects that often have a bunch of special steps to get started, to get your machine configured and whatnot, you know, they can be intimidating.
- 5:20But, you know, using the article, I was able to get the code, get it up and running and be playing with Python 3.9 super quick.
- 5:26It was just, I don't know, most of the time was waiting on the compiler, actually.
- 5:30Yeah, there's ways of making it faster, but it's a big piece of code to compile.
- 5:34So it takes a while.
- 5:35Yeah, I definitely ramped up the number of cores getting used there, but it still takes a while.
- 5:39It's quite cool.
- 5:40All right.
- 5:41So before I guess we maybe dive into the source code itself, let's maybe talk a little bit higher level.
- 5:49It was like some of Python is Python, which is cool and meta.
- 5:52And some of Python is C code, which is maybe surprising to some folks who are kind of new to Python and how it's like executes internally.
- 6:01And maybe there's even some other code in there as well.
- 6:04Like I haven't seen any inline assembly, but you never know, right?
- 6:07What's the breakdown there?
- 6:09Or like, how would you categorize that?
- 6:10It's about 70% Python and then the rest is C code.
- 6:15So there's about 350,000 lines of C code, which is a lot of C code, but over 800,000 lines of Python, which includes, I guess, the test suites as well.
- 6:25On top of that, actually, there's documentation is over 220,000 lines of documentation.
- 6:31So the documentation itself is a huge amount of work.
- 6:35So the restructured text is actually one of the main languages.
- 6:39Restructured text is one of the main languages.
- 6:41Yeah, absolutely.
- 6:42230,000 lines of restructured text.
- 6:44Would it be safe to say that most of the standard library is written in Python, but not all of it, but almost all of the core interpreter and compiler is written in C.
- 6:53Is that a good representation?
- 6:55The core types are written in C.
- 6:57The compiler is written in C.
- 6:59Most of the sort of core engine and the runtime is written in C.
- 7:03In terms of the standard library, anything which doesn't need to patch into any of the operating system,
- 7:11APIs, like the networking or any hardware or anything, is written in Python.
- 7:16Otherwise, it's written in C.
- 7:17Some languages that all of it are written in that language, right?
- 7:22Like Go, for example.
- 7:23Then there's other ones like Python words, some Python, some C.
- 7:27But we also have things like PyPy, which is more Python.
- 7:30Is it 100% Python?
- 7:32I'm not sure there might be some little tiny shim to get it started.
- 7:35But why is it in C?
- 7:37If you're making a new programming language, to write the compiler, you need a programming language to write the compiler in.
- 7:43So it's difficult if you're starting a new language from scratch.
- 7:47The Go is actually a good example because the Go compiler is now written in Go, but it wasn't originally.
- 7:53Once they got Go a bit more mature, then they basically rewrote the compiler in Go.
- 7:59But you still need an actual interpreter and a compiler to be able to do that.
- 8:04So CPython is written in C largely because they need something to start off with.
- 8:08This was written a while ago.
- 8:11C is still a very popular language.
- 8:12And also Python has a lot of integrations into the operating system components.
- 8:19And most operating system APIs are in C.
- 8:23So for Windows, Linux, and macOS, if you want to talk to the sound card, if you want to talk to the screen,
- 8:29and if you want to open a socket on the network, then you're going to be talking about C APIs.
- 8:34So, you know, the ability to do all that stuff seamlessly in Python means that at some point,
- 8:39it needs a C layer to integrate into the kernel.
- 8:43Right, to call the Win32 API or down into Linux or macOS, their native APIs, right?
- 8:49Yeah, exactly.
- 8:50Yeah, cool.
- 8:51So this is a huge project, as the size, my joke about your book, you know, I sort of hinted at.
- 8:59So when you look at it, like, how did you get started?
- 9:01There's got to be a bunch of stuff you decided not to cover.
- 9:04Some stuff you did.
- 9:05You do have your sort of mission of like, here's the missing dev guide, sit down with a senior developer.
- 9:10But how do you decide to get started on this?
- 9:12Or like, what goes in and out?
- 9:14Yeah, so the approach I took was not to go file by file,
- 9:18but instead kind of follow a trace from typing Python at the command line with some code,
- 9:25all the way to it being executed and then back up again.
- 9:29So it kind of takes the article takes you through, you know, what happens when you run Python,
- 9:34how and then basically steps through each layer deeper and deeper and deeper into the code,
- 9:39and then explains each point what's happening.
- 9:42And then kind of I've added diagrams and stuff like that to show you.
- 9:46So it's almost like a, you know, like a traceback.
- 9:49If you were to kind of add like a custom traceback, but actually doing tracebacks in Python is really hard.
- 9:55So not in Python, but in CPython.
- 9:57Yeah, most of the code that you'd be trying to look at would actually be in C, not in Python, right?
- 10:01Yeah, exactly.
- 10:02And I've ended up writing some tools to help me put the article together and also do some debugging to kind of pick this apart.
- 10:09Sure.
- 10:09Well, what's your background in C?
- 10:11Like how prepared were you for this journey?
- 10:14And how easy was it?
- 10:15I guess is what I'm getting at.
- 10:16I thought I understood C, but then diving deeper and deeper into this code,
- 10:20I really kind of had my head scratching a few times.
- 10:24There's a lot of macros in the CPython source as well.
- 10:27So anyone who's worked quite a bit with C code might be surprised at the shiv volume of macros.
- 10:33So a macro is basically a way of before the code gets compiled,
- 10:38the preprocessor will replace a macro with another piece of text basically before it gets compiled.
- 10:44And there's a lot of these in CPython.
- 10:46So it makes it basically they're micro optimizations to the code,
- 10:50but it does make it quite tricky to read and understand.
- 10:53Yeah, I can imagine.
- 10:54You know, when I was looking through it, I used to do for a handful of years, professional C++ development.
- 11:00And I could read it, but I was thinking, you know, I'm really glad I'm writing Python these days because,
- 11:05wow, I know what this means.
- 11:07It's a lot of work.
- 11:09A lot of work to write C.
- 11:10Yeah.
- 11:10And also making changes to the code.
- 11:13So in the article, it kind of encourages you to not just understand how it works,
- 11:19but also to make little tweaks and changes and add your own customer statements and maybe,
- 11:24you know, interfere or look at the tracing and stuff like that.
- 11:27And as part of this, I ended up kind of writing a few pull requests into CPython and doing a couple of bug fixes and things like that.
- 11:34That's awesome.
- 11:34What were they for?
- 11:35They were really minor ones, just stuff that I discovered when I was kind of digging in.
- 11:39There's a couple that still need to be merged as well.
- 11:41I'm still working on one for Windows support for changing the parser generator.
- 11:46So if you want to add custom syntax to Python from Windows, then getting that support in.
- 11:52And also I worked on one which was rejected, but it was an interesting experiment to do with list comprehensions.
- 12:01So if you basically do a list comprehension over a list, so typically you'd use list comprehensions for things like filtering a list into another list.
- 12:11But when you run a list comprehension, it first of all initializes an empty list.
- 12:16And what I realized is that if you initialized that list to a larger size or you predicted the size of the list, then it's a lot more efficient.
- 12:25So it ends up being about 10%.
- 12:27If there was no if block, there's no if part in the list, and you know you were doing a comprehension over the list, the size should be exactly the same as before, right?
- 12:36So you should just pre-allocate that.
- 12:37Yeah.
- 12:37So it was an experiment to see if that was possible, which it was, but it was a bit hacky.
- 12:42And it did make a difference in terms of performance.
- 12:44I think it worked out being about 8% or 10% faster on list comprehensions, but it added too much complexity.
- 12:51So it was rejected.
- 12:53But I think it's an ongoing experiment that we need to look into.
- 12:56That's a non-trivial difference you made by doing that.
- 12:59I mean, I understand the complexity thing, but 8% is a lot these days on a 30-year-old polished piece of software.
- 13:06Yeah, it's if you're doing a list comprehension over a list of a fixed size, but all of the benchmarking tools in CPython use the range function, which doesn't have a fixed size.
- 13:16So basically, the benchmark suite didn't think there was much difference because the benchmark suite heavily uses range.
- 13:24But in practical applications, you wouldn't use range a great deal.
- 13:28How interesting.
- 13:28Okay.
- 13:28That's a super cool one.
- 13:29I love it.
- 13:30Nice.
- 13:31All right.
- 13:31Well, let's start at the beginning.
- 13:32I'm interested in the CPython source code, and I want to play around with it.
- 13:36How do I get it?
- 13:38What is it, in Subversion or something these days?
- 13:39Well, so it's really easy.
- 13:41Yeah.
- 13:42So it's all moved to GitHub.
- 13:43It's easy to find.
- 13:44GitHub.com slash Python slash CPython.
- 13:47And you can download that as a zip file.
- 13:50You can download that using a Git client, or you can use your IDE to pull it for you.
- 13:54Yeah, it's so cool that it's over on GitHub these days.
- 13:57It's really nice to have it kind of modernized.
- 14:00I think it encourages people to participate more in the discussion.
- 14:04They were talking about moving the issues there as well, but I don't know if they were moved yet.
- 14:09That'll be cool when that happens.
- 14:11There's a PEP that Marietta has put together, and it's proposing moving GitHub to GitHub issues
- 14:16from a bug tracker that they have at the moment.
- 14:19I don't think that's been decided on yet.
- 14:21It wouldn't surprise me to see it happen, but I guess maybe one of the questions is just
- 14:25all the historical issues get somehow migrated over, and I can see challenges there.
- 14:32This portion of Talk Python To Me is brought to you by Linode.
- 14:38Are you looking for hosting that's fast, simple, and incredibly affordable?
- 14:41Well, look past that bookstore and check out Linode at talkpython.fm/Linode.
- 14:46That's L-I-N-O-D-E.
- 14:48Plans start at just $5 a month for a dedicated server with a gig of RAM.
- 14:53They have 10 data centers across the globe, so no matter where you are or where your users
- 14:57are, there's a data center for you.
- 14:59Whether you want to run a Python web app, host a private Git server, or just a file server,
- 15:03you'll get native SSDs on all the machines, a newly upgraded 200 gigabit network, 24-7 friendly
- 15:10support even on holidays, and a seven-day money-back guarantee.
- 15:13Need a little help with your infrastructure?
- 15:15They even offer professional services to help you with architecture, migrations, and more.
- 15:19Do you want a dedicated server for free for the next four months?
- 15:23Just visit talkpython.fm/Linode.
- 15:25All right, so we've got the code.
- 15:28We git cloned it, or however we're going to get it off of GitHub.
- 15:31And then you get this project structure with like 13 or 14 top-level folders.
- 15:36So maybe we could talk through just some of the major sections, because I think the file
- 15:41structure is a pretty good partitioning.
- 15:43The folder structure, there's pretty good partitioning to start to understand, like, where do I go explore?
- 15:48Yeah, once you've downloaded the source code, the first folder is called doc, which is where
- 15:52the documentation is.
- 15:53So that's the 230,000 lines of restructured text.
- 15:57Wow.
- 15:58Yeah, and if you want to start off by understanding some of the APIs as well, the documentation is
- 16:02a good place to go.
- 16:03There's also a folder called grammar, which is for the sort of computer-readable language
- 16:10definition.
- 16:10So what is in the Python syntax?
- 16:13What makes an if statement an if statement?
- 16:15Are you, you know, could you type if else colon?
- 16:19Like, that wouldn't make sense.
- 16:21And why?
- 16:21So the computer understands, I guess, how the language is structured.
- 16:25There's a folder for include for C header files, which is also good to understand the
- 16:31API in a bit more detail.
- 16:32There's the lib directory for basically the standard library models, all the ones that are
- 16:38written in Python.
- 16:39Yeah, I think the include one is pretty good because you can just see the function definitions.
- 16:43You don't have to like jump through all the implementation and the macros and the hash if def stuff.
- 16:48You can say like, these are the things I can call over here.
- 16:51Right.
- 16:51You can get a little higher level view.
- 16:53This is why if you're going to jump into this, you want to pick a pretty decent IDE because there's a lot of code in here and using a plain text editor is going to be extremely difficult to navigate things.
- 17:05I agree.
- 17:06So yeah, I'd recommend picking a decent IDE to start off with.
- 17:09For me, when I was playing with this, I used VS Code on the whole top level project.
- 17:13And it just said, hey, you should have the C, C++ extension installed.
- 17:17Sure.
- 17:18Do that.
- 17:18Yeah.
- 17:19It was pretty good from there.
- 17:20Also installed the restructured text extension.
- 17:22It was kind of adapting.
- 17:23Maybe over if I was on Windows, I might use Visual Studio proper because it's got a Visual Studio solution in there, which is kind of cool for Windows developers.
- 17:31Yeah.
- 17:31And the article actually I'll take through.
- 17:33So Visual Studio 2019 came out whilst I was writing this.
- 17:38So it's been updated to explain how to use the community edition, which is the free version.
- 17:43So it's different to VS Code.
- 17:45Visual Studio is like a fully blown IDE.
- 17:49It's designed for sort of C, C++ and C# development.
- 17:52And there's some explanation in here about how to use that to both compile CPython from source as well as do debugging and stuff like that.
- 18:02Yeah.
- 18:02You've got some really cool stuff how you have like the REPL running in a debugging instance embedded in Visual Studio or something like that, right?
- 18:09Yeah.
- 18:09It's pretty cool.
- 18:10Actually, I was really impressed.
- 18:11Definitely for Windows users, I'd say if you want to make changes and not just explore,
- 18:18then I'd pick on Visual Studio rather than VS Code because you're going to get much richer debugging.
- 18:24And Visual Studio 2019 as well is going to be able to compile for you.
- 18:28So if you just use VS Code, then you're going to need to run the MSBuild script files,
- 18:34which are located in the PCBuild directory.
- 18:37But it's actually a lot easier to use Visual Studio rather than running it all in the command line.
- 18:42Yeah, that makes sense.
- 18:43And I totally derailed your summary of these things over here.
- 18:46So we were talking about the lib folder is full of all the part of the standard library that is written in Python,
- 18:52like the CSV module or whatever.
- 18:55But then there's a bunch more, yeah?
- 18:56Yeah.
- 18:57So there's a folder for macOS support files.
- 18:59There's a couple of other miscellaneous directories that you shouldn't need to worry about.
- 19:03There's a folder called modules, which is the standard library modules that are written in C.
- 19:08So the standard library modules are split between the lib and the modules folders.
- 19:13So whether they're written in Python or C, they're in two different places.
- 19:16Right.
- 19:16So object, where you have a class and it drives from object, there's an object.c file in modules.
- 19:22Right.
- 19:23That part's pure C.
- 19:24Actually, they're in the objects folder.
- 19:26Oh, yeah.
- 19:27So there's a folder called objects, which has got the core types and the object model.
- 19:34So what is a number type?
- 19:36What is a string?
- 19:38What is an array?
- 19:40That sort of thing.
- 19:41Yeah, I had it totally wrong.
- 19:42So maybe like GC or something like that.
- 19:44Yeah.
- 19:44Then there's the parser, which is basically the thing that actually parses the source code into something that can be interpreted.
- 19:51Then there's some directories for Windows users.
- 19:54So there's PC and PC build.
- 19:55PC is the new version.
- 19:58PC build has got some sort of legacy scripts for building for older versions of Windows.
- 20:03There's a programs directory, which is the source code for the sort of either the Python.exe or the Python binary that you end up with on your machine.
- 20:14And there's a Python folder, which is confusing, but it has the interpreter source code.
- 20:19So I think it interprets the code through to execution.
- 20:23And then there's a folder called tools, which has got some tools and scripts and stuff like that for either building or extending Python.
- 20:30Super cool.
- 20:31And yeah, there's some of these that you want to really dive into and others are just support files.
- 20:36When I was looking around in the lib folder, I was kind of blown away at some of the stuff that's in there.
- 20:41I'm like, all right, well, what in here is actually implemented in Python?
- 20:44What's the code look like?
- 20:47You know, those are all interesting things.
- 20:48And then I came across some stuff that surprised me.
- 20:52Obviously, you would expect that these files would have comments, documentation that describes what they do, right?
- 20:59Absolutely.
- 20:59Yeah.
- 21:00So, but then I saw that a lot of them, not a lot, some of them, non-trivial number of them, actually have like ASCII diagrams that describe, say, like workflows or like relationships in the comments.
- 21:13It's pretty wild.
- 21:14Like the lib concurrent features process.py has like a great long like in process, out of process, like workflow diagram in the help doc.
- 21:23Oh, wow.
- 21:24Okay.
- 21:24That's pretty funny, right?
- 21:25And then also the JSON module, you know, has like cool ASCII art documentation.
- 21:31So anyway, I thought those were, those were surprising to me.
- 21:34Like really, there's, there's like diagrams in here.
- 21:36How cool.
- 21:36Yeah.
- 21:37I think if anyone wants to have a go at contributing to CPython, a really easy place to start is in the lib folder is all the standard library modules that are written in Python.
- 21:48They're easy to read.
- 21:49Most of them are not too complicated.
- 21:51Have a look through some of those because there's stuff in there, which is legacy syntax, which needs updating.
- 21:57There are bugs in there, which have been reported in the bug tracker, but never fixed.
- 22:01As well as if you compare what's in the code to what's in the documentation, you'll pretty quickly find gaps.
- 22:09The functions, which either don't have any documentation or the documentation is wrong, or the argument list is up to, is not up to date.
- 22:17So if you want to have a go and contribute and you're looking for something simple to get started with, then I'd say pick some of the, probably some of the more obscure standard library modules and see what needs fixing in those.
- 22:29Yeah, that's a good idea.
- 22:29And I honestly don't know how much of those have these, these issues, but it seems like a pretty straightforward way.
- 22:35Certainly contributing to the lib folder or the docs folder seems much more approachable to me than to like the objects or the modules.
- 22:44Cause down there, that's where the C code lives, right?
- 22:47Yeah.
- 22:47Before you get stuck into the core runtime, then I think it's a good idea to have a look at some of the high level Python code first.
- 22:53Probably a good idea as well.
- 22:55So I thought I'd just pull out a couple of files from each of these major sections, these folders that you talked about that were just kind of interesting.
- 23:04So over in the lib module, we have things like CSV, whole CSV implementation is over there.
- 23:12And it's, you know, it's not that much.
- 23:14I don't remember exactly how long it was, but it's, you know, a couple hundred lines, maybe 500 lines and you can just go read it and play with it.
- 23:20Right.
- 23:20Yeah.
- 23:20And you can make changes.
- 23:21You can put your own debug statements in, you can see how things are working, but it's pretty straightforward.
- 23:26There's a, you used to use the dict reader before, which is a really handy way of doing CSV parsing.
- 23:32Yeah.
- 23:33It's easy to understand how that works.
- 23:35It's written in clear, clear Python.
- 23:37Super cool.
- 23:37Yeah.
- 23:37So people can poke around there over in, let's say objects.
- 23:40That's the one I got wrong, actually.
- 23:42Over in objects.
- 23:43This is where object.c is defined, right?
- 23:47And this thing is way more complicated than I expected it.
- 23:50Obviously it's the base class for everything.
- 23:53So it's going to be doing a lot.
- 23:54It doesn't do as much as I thought it would, but it, there's a lot of code involved in there, isn't there?
- 23:59The core object types are actually quite complicated.
- 24:02That was something that surprised me when I was going through this deep dive is that I thought that there wasn't really a big difference between objects that I defined and objects that were built in like the, you know, the int type and the string type.
- 24:15I thought that they were more or less the same, but actually everything kind of sits on top of the core types.
- 24:20So the core types are all declared in C effectively, and they have C functions and everything that you put on top of that goes into a dictionary and is, is pure Python.
- 24:30Right. And it seems like there's a lot of memory management stuff that's happening down in there as well, like allocation and finalization.
- 24:37And it seems like that's the main purpose of what the plain object is about.
- 24:42Yeah. I think the two ones that are really interesting to look into is the list object and the dictionary object.
- 24:48So the beautiful thing about the list object is that you never have to worry about writing linked lists or doing list allocation like you would in many other languages.
- 24:57You can just add items to a list and it just magically makes it the right size.
- 25:02In the article, we actually talk about the growth pattern and how it reallocates the size automatically and how that works.
- 25:09But yeah, it's pretty cool to see how that is behind behind the scenes.
- 25:12Yeah. Do you really appreciate what it's doing for you down there?
- 25:15That's awesome.
- 25:15I love that.
- 25:17It's not just here's an array.
- 25:19Now you get to figure out all the complicated details of using it dynamically.
- 25:23Right.
- 25:24It's no, it's beautiful.
- 25:25It's a list.
- 25:26You know, there's one, you know, I told you I opened this whole project up in Visual Studio Code and Visual Studio Code has this cool extension, built in extension or like something I installed that has like a little gray highlight.
- 25:37When you're on a line of, you know, who's done what I think it's called get lens, maybe it's the thing I installed.
- 25:44But it had, you know, I was just poking around and it shows, you know, who's contributed or checked in this file.
- 25:49Like, so Pablo Galindo had just worked on object.c 22 days ago.
- 25:55But then as I arrowed down, like different parts would light up with people doing different things.
- 25:58Like line three, it says, get it on Rossum 29 years ago, initial revision.
- 26:03Some of the code in the CPython source is quite old and hasn't needed to change.
- 26:08Like it just worked the first time and it hasn't needed to be updated or there's nothing that's been changed in it.
- 26:13It's really interesting because when you dive through the code, I guess you can kind of see there.
- 26:19If you're looking at the Python 3.8 source code, you're actually looking at a almost like a canvas of 2.anything all the way up to the latest version.
- 26:30Even actually, even before 2.something, like some of this stuff is all the way back from version one.
- 26:35Right, right.
- 26:35Like object.c.
- 26:36Yeah.
- 26:37Yeah, exactly.
- 26:37So some things haven't really changed since the first versions, but the vast majority of the code has been drastically rewritten.
- 26:43over the last 10 years.
- 26:44It makes me think of like a canvas that like a painter would paint on and it's been painted over and painted over, right?
- 26:51Yeah, exactly.
- 26:52Cool.
- 26:52All right.
- 26:53So the next major area was the modules folder and this is where the standard library C implementation goes, right?
- 27:00Yeah.
- 27:01So there we have the main.c, which is the Python interpreter main program, which is pretty cool.
- 27:06And also GC module and stuff like that, right?
- 27:09Like memory allocation and whatnot.
- 27:11Yeah.
- 27:11So one of the ones we dig into quite a lot is main.c.
- 27:14So this is some of the really high level APIs for initializing the Python application.
- 27:20So the binary that you would run.
- 27:21So there's different ways that you can run Python.
- 27:25One that you typically use is just by typing Python on the command line or Python.xe.
- 27:30And that basically goes through a sort of a high level binary.
- 27:34And then that either takes an argument, which is the name of the file you want to run or the library you want to run or the module.
- 27:41Or you can even do, you know, Python dash C, for example, and actually give it a string with some Python code.
- 27:47So all the wrappers around that are in both this main.c and also another file called pymain.
- 27:54And then also there's a Python API, which you can call at the C level.
- 28:01So the Python binary is actually just a wrapper for the Python C API.
- 28:05And the Python C API, you can also import and use from your own application.
- 28:10So you can actually write an application in C that has embedded Python.
- 28:16And it uses exactly the same code, compilation, parsing, everything that you get when you type Python at the command line.
- 28:23So there are some practical uses for that.
- 28:26So there's some big applications out there that have Python kind of like built into them.
- 28:31One would be like a 3D designer called Houdini, which is like a 3D graphics tool.
- 28:37This kind of uses Python like deeply integrated into it, you know, using the C APIs.
- 28:43Yeah, that's really cool.
- 28:44I don't think people do that maybe as much as they should, right?
- 28:47Because saying the way that you extend our application is not to go write tons of C code that you could forget to allocate something and crash the whole program.
- 28:55But here, just write a little simple Python code and it makes our main app go.
- 28:59I think the folks in the movie and 3D game space use that a lot in a lot of their tools and actually use Python to like kind of drive those pipelines, the automation of a lot of the tooling like you're talking about Houdini.
- 29:10Yeah, that's pretty cool.
- 29:11So you spend a whole lot of time in the article diving into those different things and how that works.
- 29:15That's pretty cool.
- 29:16And then I guess the last interesting, there's lots of interesting stuff, but there's the last one that I want to call out on the different sections of different directories is the whole Python one.
- 29:26And this is where the Python runtime lives as opposed to the standard library or maybe the stuff that starts up the processes or the builds.
- 29:36Like this is where the execution happens, right?
- 29:39So the Python directory in the source code is really the brain of the whole application.
- 29:47So it's basically how it does the evaluation of the top codes, which is the sort of low level assembly code that Python ends up building.
- 29:57The Python run.c, ceval.c.
- 30:00So these are highly optimized C files, which have been written in and changed over time that basically executes the Python code.
- 30:10For sure.
- 30:10And so ceval.c, this is the big one when it comes to execution.
- 30:17This portion of Talk Python To Me is brought to you by the University of San Francisco.
- 30:21Learn how to use Python to analyze the digital economy in the new master's in applied economics at the University of San Francisco.
- 30:28Located at the epicenter of digital disruption, USF is the ideal launching pad for the next phase of your career.
- 30:34Their new STEM designated economics program doesn't just provide technical training in high demand skills like machine learning, causal inference, experimental design, and econometrics.
- 30:45It takes the next step, teaching you how to apply these techniques to understand the economics of platforms, auctions, pricing, and competitive business strategy in the world of big data.
- 30:55The program is open to beginner and to advanced coders looking to apply their skills in a new area.
- 31:01Applications are now open for the fall 2020 classes.
- 31:05To learn more and get an application fee waiver, go to talkpython.fm/USF.
- 31:11That's talkpython.fm/USF.
- 31:13Maybe talk about the process of going from Python source code, what we would think of as what we wrote, to getting to Python bytecode before we get to CofLC.
- 31:27Like, what's the high-level flow there?
- 31:29Okay, cool, cool.
- 31:30So, first things first.
- 31:32So, you've written some Python code in a file, I'm assuming.
- 31:36So, first of all, it has to read the file, which is actually non-trivial because you've got to think about encodings and all sorts of other fun things.
- 31:44Then, basically, the parser will go through and take the file apart and put it into something called an abstract syntax tree.
- 31:52So, there's basically, actually, before that, there's a step called tokenization, which is to split the application into components.
- 32:00Then it goes into an abstract syntax tree.
- 32:02You can use the AST module.
- 32:04And in the article, I talk about how to use the AST module.
- 32:08And I wrote a small web application called Instaviz, which you can download on GitHub.
- 32:13Nice.
- 32:14What does it do?
- 32:14It basically represents the Python syntax in a massive tree that you can explore, like an interactive tree.
- 32:21And you can write Python code and it will give you the abstract syntax tree in, like, web application, like an interactive D3 graph.
- 32:29So, you can kind of play around and see.
- 32:31So, it's a bit easier to see how that tree works with the syntax and understanding the difference between a new line and an indent and what a name is and things like that.
- 32:42So, yeah, the tokenizer will kind of look at the file and wrap it up into tokens.
- 32:48And then the next step will be to put that into an abstract syntax tree.
- 32:53Once it's got the abstract syntax tree, then it will essentially compile that by doing a depth-first search.
- 33:01And then it puts it into something called a concrete syntax tree.
- 33:05That is more of a sort of literal interpretation.
- 33:08So, an abstract syntax tree says that you've got an if statement and inside the if statement you're comparing two variables and you're checking that they equal each other.
- 33:16And then if that is successful, then you're going and doing these three extra lines of code, which you've nested with a tab, for example.
- 33:24So, that's what an abstract syntax tree would look like.
- 33:27The concrete syntax tree, the CST, is basically a bit more low level than that.
- 33:32So, it's actually saying at the lower level, like, here's the statements we need to execute.
- 33:36And then the compiler is another step, which basically takes the concrete syntax tree and converts it into a list of opcodes.
- 33:46And this is the Python bytecode, basically, which is no longer a byte.
- 33:53It's actually a word.
- 33:54But, yeah, there's a separate issue.
- 33:56They're long words.
- 33:57Yeah, they're long words.
- 33:58Yeah, yeah.
- 33:59So, I think that's interesting because a lot of folks think of Python as a scripting language.
- 34:04They think of it as, like, this thing that is not, like, one of the things that defines what Python is that it's not compiled.
- 34:11But you just said there's a compiler.
- 34:12Yes.
- 34:13So, it absolutely is compiled.
- 34:15It's compiled into an intermediate language.
- 34:18So, similar to .NET and Java.
- 34:21So, .NET and Java both have JITs.
- 34:24So, just-in-time compiles and execution.
- 34:27But Python is a bit different.
- 34:29CPython, that is.
- 34:30PyPy does have a JIT.
- 34:31But CPython compiles down to an intermediate language as well.
- 34:36It's kind of like Csharpen.net or like Java.
- 34:39But where it differs is what happens when you try to execute that, right?
- 34:43In, like, Java, it would JIT that to machine instructions.
- 34:46In Python, it takes this big bunch of these opcodes and feeds them off to ceval.c.
- 34:53Yeah.
- 34:53There's a switch statement.
- 34:54The opcodes end up getting cached in a PYC file or in newer versions in the __pycache__ folder.
- 35:04So, if you've ever noticed that when you've run a Python application, it creates this cache folder in your application directory.
- 35:13So, basically, it goes from the source code all the way through to almost like the compiled code.
- 35:19And then it puts that in a cache folder.
- 35:21So, it does actually do the compilation.
- 35:23And then the next step is to execute that.
- 35:25It's a literal list of statements that it works through.
- 35:29And it has a frame stack and a value stack, which I talked through in the article because it takes a bit of time to get your head around those.
- 35:36But in a nutshell, the frame stack is if you called a function inside your Python code, then you'd expect the local variables to be different, for example.
- 35:45And you couldn't just reference the stuff that you were using beforehand.
- 35:49So, there's essentially these frames.
- 35:51And then also there's a value stack, which is used by the opcode so that it doesn't really understand the different variables you have.
- 35:58They're just all on the pile.
- 35:59And you can add things on top of the pile and you can remove things from the pile.
- 36:02So, that's essentially how it works.
- 36:05Right.
- 36:05It might load three things onto the value stack and then call the function with that, right?
- 36:11Something to that effect.
- 36:12So, the opcodes are really low-level statements, essentially.
- 36:15So, they're sort of push and pop values from the stack, for example, or to initialize a new list or to initialize a new variable or to call a C function.
- 36:27That's cool.
- 36:27If you want to explore those, there's the disk module, right?
- 36:31And people can import, they can say, from disk, import disk, and then they can start taking a function and asking what bytecodes or opcodes make up this thing, right?
- 36:39Yeah.
- 36:39I've got a small snippet of code which will work in Python 3.7 and above.
- 36:44So, in 3.7 and above, you can actually do, in the sys module, there's a tracing flag, which you can enable.
- 36:51And you can run a piece of code and it'll actually print out where you are in the frame stack, what opcodes are being executed,
- 36:59and you can inspect the value stack as well.
- 37:01So, if you want to almost, like, play around with the runtime and see what's happening live when I run this piece of code,
- 37:08there's a snippet in the article, which we'll link to in the show notes as well, where you can basically see the frame stack live.
- 37:15That's pretty awesome.
- 37:15So, you can just get it to just dump, like, every bit of what it's up to, huh?
- 37:19Yeah, and I got it to nest as well, so that, you know, as you go deeper down in terms of the frame stack,
- 37:23it will pad things out further and further to the right, so you can see where you are in the tree as well.
- 37:28Yeah, that sounds like you'd almost have to have it or you'd just go crazy.
- 37:31That's cool, though.
- 37:33It sounds super useful if you're just trying to understand.
- 37:35Yeah, so then eventually it goes down to ceval.c, which is a very complicated piece of C code.
- 37:44It has a lot of macros as well, which makes it quite hard to follow.
- 37:48But essentially, it's a big loop.
- 37:50So, it's a big for loop, essentially, and it goes through each opcode in that frame and executes it.
- 37:57So, it's a big for loop, and inside the for loop, there's a massive switch statement, which says, you know,
- 38:03if it's this opcode, do this, if it's that opcode, do this.
- 38:06And then for each one, it typically calls a C function.
- 38:10So, you know, if you're going to load a variable onto the value stack, then it would fetch the variable and push it onto the stack.
- 38:18Like, there would be, you know, two or three lines of code for each opcode.
- 38:22Some of them are a bit more complicated.
- 38:24So, you can do, let's say, set add, for example, is a basic opcode.
- 38:30So, if you had set add, you're adding two sets together, then basically it would call py set add, which is a C API.
- 38:37So, most of the opcodes actually just call C functions.
- 38:40Right, they take the variables that are on this value stack and they just go call the C function with that.
- 38:45Yeah.
- 38:46Yeah, this is like a serious switch statement.
- 38:49It's, you know, people haven't looked at it yet.
- 38:51It's like three, four thousand lines long.
- 38:54I don't have it pulled up just right a second.
- 38:56Another thing that surprised me is there's some interesting flow control mechanisms in there.
- 39:03Like, go-to.
- 39:04There's a lot of go-tos.
- 39:05How did that, did you feel like when you saw that?
- 39:08Were you like, what is this?
- 39:09The go-tos make sense because basically there's some optimizations in here.
- 39:14So, sometimes you'll get opcodes that typically come in pairs.
- 39:18So, what they've done is over time they've said, okay, you know, if you're going to create a new list, then, you know, you're going to create a new name and initialize a new list.
- 39:31And those two opcodes are going to end up being next to each other.
- 39:34So, in the switch statement, it actually kind of has like shortcuts in the code so that it knows that if it's running this opcode, the chances are it's probably going to run that opcode next.
- 39:45So, it basically shortcuts a lot of the other inspections and a lot of the other checks.
- 39:50Also, there's a lot of go-tos in terms of yields.
- 39:54So, if you're yielding values back as well as doing errors.
- 39:57So, if you call a function and the function crashes or if you try and store an attribute but there's some sort of error at the low level, then it will go to like a generic error section which basically starts off the whole exception process.
- 40:11It makes sense because it's so highly optimized.
- 40:14Like, forget doing it the right way.
- 40:17If the go-to makes it a little bit faster, get in a go-to, right?
- 40:20Because this is the hot loop that runs every single thing that happens in the language.
- 40:24Yeah, this thing is going to run thousands and thousands and thousands of times.
- 40:28So, you want it to be as fast as possible.
- 40:31I'd say that in terms of micro-optimizations, I think they've pretty much done most of what they can.
- 40:38There was some stuff introduced in 3.7 to do with the fast method calls.
- 40:42Right, for methods without keywords, right?
- 40:44Yeah, so the fast method calls were to do with if you're calling a method in a class that doesn't have keyword arguments, then it's about 20% faster.
- 40:52And you can see in this loop, you can actually see that opcode and how it works as well.
- 40:57So, it's pretty cool.
- 40:58I would say if you are trying to understand how CPython executes code, the C eval C and the switch statement, this is the place to start, right?
- 41:05It's a good place to understand.
- 41:07I wouldn't say start here.
- 41:09I'd say get here.
- 41:10I'd say work your way towards it.
- 41:13If you just jump in, it's not going to make a whole lot of sense.
- 41:16Right, okay, fair enough.
- 41:17So, another thing that you spent a fair amount of time on and you tied it back to the underlying C API was memory management.
- 41:23Yeah, this is definitely one of my weaknesses in understanding.
- 41:27I thought I understood how memory management worked in CPython.
- 41:30But the more I looked into it, the more complicated it actually is.
- 41:33There's basically different types.
- 41:35There's different ways it allocates blocks and also arenas.
- 41:39And there's basically a Python version of, or a CPython version of Memalik.
- 41:44So, instead of calling Memalik directly from C, you're supposed to call the sort of CPython version, which has got more governance and also more cleanup.
- 41:52Yeah, and it also does a bunch of work to try to avoid fragmentation and stuff like that.
- 41:56So, I think it's interesting.
- 41:57I don't think people talk about memory management in Python very much.
- 42:00On one hand, like, who cares?
- 42:01Whatever.
- 42:02We don't have to worry about it.
- 42:03Hooray, right?
- 42:04That's, like, one of the reasons we like the language.
- 42:06Like, I'm so done with calling Malik and free.
- 42:09I just don't want to do that again.
- 42:10But on the other hand, just having a conceptual understanding of what is happening at a pretty good level helps you think through, this algorithm might be better than that.
- 42:20Or if we're having these memory problems, we might be able to, you know, do something slightly different in terms of how we're using, like, how we're defining our code.
- 42:27Like, you know, maybe to take advantage of not work against the way it works, but to work with it, right?
- 42:31Yeah, one of the biggest benefits to the CPython is that everything basically comes from PyObject.
- 42:38So, the core type of an object, which is used by integers and strings and lists and everything, including the objects that you define, everything kind of comes from the same type.
- 42:50So, the memory management is very optimized because basically everything inherits from something.
- 42:56So, you know that the structure at least has this fixed size.
- 42:59So, what they've done is they've built in these utilities for allocating sections of memory on your machine so that you can store objects easily and fetch them and reference them.
- 43:12So, that's something called the PyArena, which is referenced quite a lot in the code.
- 43:16And you'll see, you know, when you add objects to the arena, where that goes to.
- 43:20So, basically, it's a way of putting Python objects into memory.
- 43:24And also, that's where the PyArena malloc kind of comes from, which is to do with object memory allocation.
- 43:30So, that's the really sort of low-level memory allocation techniques that are used in size-seat Python, which are optimized around the size of the PyObject type and typically the types of memory that are requested and the way that they're used.
- 43:46But if you're using Python at the Python level, you'd never care about that sort of thing.
- 43:51You just expect that when you declare an object, it has its memory.
- 43:56Like, you know, it figures that out itself.
- 43:59But at the Python layer, you definitely do need to know about the reference counter and the garbage collector if you're writing applications which run for any long period of time.
- 44:10Yeah.
- 44:10Well, you talked about Java and .NET before.
- 44:13Those are both market sweep garbage collecting type of systems.
- 44:17If you go back to something like C or C++, it's manual.
- 44:20You know, maybe you could create a smart pointer in C++ and then that's kind of reference counting.
- 44:25But Python's interesting, I think, because it has this blend, right?
- 44:28It's like, well, we're going to do reference counting, which is pretty awesome and predictable and deterministic and fast.
- 44:32Except for when you have cycles.
- 44:35Like, that's the main weakness of reference counting, right?
- 44:38You cannot break a cycle because the reference count is never going to go below one.
- 44:43Or if you have two things that refer to each other, like, how do they become garbage, right?
- 44:46So we have this GC that also runs.
- 44:49But yeah, it's pretty interesting.
- 44:50And you could see some of that happening that you're exploring there.
- 44:53I'll go through the garbage collection module.
- 44:56So there's GC module.
- 44:57And you can actually put debugging in the GC module from the Python layer.
- 45:02So if you import GC and then run GC set debug, you can basically turn on debug statistics so that when you're even at the REPL, like if you want to assign variables and stuff like that, you can see like what's happening at the garbage collector and what the threshold is and when it runs.
- 45:19And you can also customize how many cycles the garbage collector runs at.
- 45:23Because basically the garbage collector, I've taken the easy analogy that it's like the trash trucks that come and pick up your garbage.
- 45:31Like it doesn't make sense for them to come every time you put something in the bin.
- 45:36They come once a week or once a fortnight.
- 45:38So you can basically customize that.
- 45:40So you can say how many cycles until it goes and checks which objects are no longer needed and which ones don't have any references anymore.
- 45:47And it'll go and clean those up.
- 45:49Another thing that's interesting, the two things that are interesting that you can write in Python code that are fun to play with to give you a better understanding is you can write some code.
- 45:59I think you've got to do some C imports, but you can basically ask how many references are there to this object ID.
- 46:05Right.
- 46:06And it'll tell you there's five or whatever and so on.
- 46:09The other one that you can do that's interesting is to play with the weak reference type to create a weak reference to a thing.
- 46:15Then see if it's still alive or not.
- 46:18Right.
- 46:19Because that doesn't still let you address that thing, but not actually keep it alive by holding a pointer to it.
- 46:26Right.
- 46:26The garbage collector's debug stats are a great way of doing that because it kind of dumps that information to the to the repo as well.
- 46:32So, yeah.
- 46:33Another thing you can do is implement the Dunderdell, I think.
- 46:36You can actually get it to print out when an object is finalized or deleted.
- 46:41Yeah.
- 46:41Dunderdell is really useful for doing any of the custom cleanup code.
- 46:45So it's almost like you have a constructor and Dunder in it and then you have a destructor as well.
- 46:51Yeah, exactly.
- 46:52So while we're on this memory stuff, let me just throw out this really quick, this article by Instagram.
- 46:57It's a little bit old.
- 46:58It's a couple of years old now, but it's called Dismissing Python Garbage Collection at Instagram.
- 47:03I think maybe I covered this on Python Bytes long, long ago, but it says because you can import GC and say GC.disable or GC.collect now.
- 47:13Right.
- 47:14You can take a little bit of control.
- 47:15Not necessarily that you should, but you can.
- 47:18And over at Instagram, they said they can run 10% more efficiently by disabling GC and reduce the memory footprint and improve the CPU LLC cache hit ratio on their Django servers.
- 47:31That's a very focused use case.
- 47:33But they happen to do that.
- 47:34I'll put a link to that in the show notes.
- 47:36So you can also see about playing around the GC, some over there and whatnot.
- 47:39Awesome.
- 47:40Yeah, pretty wild.
- 47:41All right.
- 47:41Oh, let's see.
- 47:42So another thing that I think will be fun to talk about, we've got just a little bit of time left, not much, is just objects in the Python data model.
- 47:50Right.
- 47:51So the objects folder is where all that stuff lives.
- 47:54And we talked a little bit about object C, but there's more stuff that defines the object, the data model, right?
- 48:00Like dunder iter, dunder enter, dunder exit, repper, all that kind of stuff, right?
- 48:05There's basically a list of these core in the Python data model.
- 48:09And actually, I referenced Luciano Romano's book, which if you want to understand.
- 48:14Yeah, it's a fluent Python.
- 48:16It's a great book.
- 48:16Yeah.
- 48:17Fantastic book.
- 48:17And if you want to understand the Python data model and how to really leverage it to write fluent Python, then I'd recommend checking out that book.
- 48:25It's available in pretty much every language now.
- 48:27So that's awesome.
- 48:28I think it's a must read for any Python programmer.
- 48:31But basically, if you are writing a custom type that was a sequence, so it was a sequence of items, then you'd have dunder len, for example.
- 48:41You can customize what the length is.
- 48:43You can say dunder contains or you can do slicing.
- 48:47You can do repeats, concatenation.
- 48:49So you can kind of override the behavior of these kind of core operators.
- 48:54But that stuff is actually built in to the object.
- 48:58So there's a list object.
- 49:00If I pick on a list as an example.
- 49:01And in the list object type, there is something called sequence methods, which is basically in the data model, the dunder methods that are in place for anything which is a sequence.
- 49:13So a byte array, for example, or a list.
- 49:16So you can go in there and just look at all the different parts or aspects of the core data model.
- 49:20It's spread across all these different classes, right?
- 49:23It's not just all jammed into object.c.
- 49:25Like you said, there's a list object.c and an iter object.c and so on.
- 49:31Yeah, so there's basically these core types.
- 49:33I think there's about 20 of them, the core types.
- 49:38So you'd recognize them, things like dictionaries and modules and methods, memory and long objects.
- 49:44So yeah, it's interesting to dig through them.
- 49:47If you want to look at them, I'd say don't jump into the Unicode object first because it's probably one of the most complicated.
- 49:55So the Unicode object is the string type.
- 49:57The old string type basically doesn't really exist anymore.
- 50:01You can do byte arrays.
- 50:02So there's a byte array object type now.
- 50:05But if you want to look at strings, the Unicode object is in there.
- 50:08But it's hugely complicated because it has to deal with all the encodings and all that magic.
- 50:13Yeah, wide character pointers and all that.
- 50:16Yeah, no thanks.
- 50:17I'll start somewhere else.
- 50:18So, you know, that about covers it for our guided tour through the actual source code.
- 50:24But maybe we could talk just really quickly about a couple of things before we're out of time.
- 50:28You talked about doing a lot of stuff with pytest and doing testing.
- 50:31What's the story around testing in Python, Python source code?
- 50:34In CPython, there's a huge test suite, which takes a lot of time to run.
- 50:39It's both.
- 50:41There's a all the tests are written in Python, which is great.
- 50:44They are written using unit tests.
- 50:46They're using the unit test module.
- 50:48And they run using concurrent processes as well.
- 50:52Because there are so many tests to run in the test suite.
- 50:54There's basically a it doesn't call unit test directly.
- 50:57It actually runs like this test runner, this custom test runner that they've built.
- 51:02So inside the test module for CPython, it'll test both the standard library module behaviors,
- 51:09as well as the parser, as well as the core runtime, as well as the APIs.
- 51:14So, yeah, like I said, there's a huge test suite.
- 51:19The simple ones to understand, I guess, are the tests which are focused on the standard
- 51:23library modules because it's Python code testing Python code, which is fairly simple.
- 51:28And then if you look at the C layer, then basically the way that you wrap C code and call it from
- 51:34Python, essentially using that from the test code to test different functionality.
- 51:39There's also documented somewhere is the sort of coverage for the different parts of Python.
- 51:45Some standard modules, standard library modules have a pretty low test coverage.
- 51:50So if you do want to get started somewhere, adding tests is always a great place to have a look.
- 51:54And you'll find some of the more obscure modules as well have little to no tests.
- 51:59Yeah. So you could write some potentially, right?
- 52:01Yeah, definitely. You could write some.
- 52:02And, you know, when you're writing tests, you might come across a few bugs that you want to fix as well,
- 52:07which is awesome. But the core runtime itself is heavily tested.
- 52:10I'm sure it's super, super heavily tested.
- 52:13The other thing that might be fun to talk about is, you know, now that CPython is over on GitHub,
- 52:19it's really easy to go look at the branches and how they're working at those.
- 52:22Maybe give people an overview of that.
- 52:24It looks like it's pretty much focused around releases and not like, say, feature branches or
- 52:29something like that.
- 52:30Yeah. So they have released branches.
- 52:32So if you were to go and look at CPython now, it would say Python 3.9,
- 52:38which might take some of you by surprise because 3.8 only came out a week ago.
- 52:43So actually what they do is they declare a sort of a feature freeze in the Python release cycle.
- 52:50So the feature freeze for 3.8 actually happened a few months ago.
- 52:54Basically when they go to beta, right?
- 52:55Yeah, basically. So any new enhancements or stuff like that would go into the next version,
- 53:01which is 3.9 and bug fixes would get merged back in.
- 53:05There's a series of bots in the GitHub repository that do merging back of bug fixes and stuff like that.
- 53:12So when you tag the issues or the PRs that you've raised with certain tags,
- 53:18there's some really cool bots that Marietta wrote that will actually go and merge that back into the appropriate releases,
- 53:24which is really cool.
- 53:24Yeah, that actually sounds super awesome.
- 53:26That's awesome. That's great.
- 53:27I suspect people who are core developers or making contributions,
- 53:31they might do feature branches, but not here in the main repo.
- 53:33Yeah. So basically most of the core developers have their own fork
- 53:38and they run feature branches on their own forks.
- 53:41So if you want to look at some of the proposed peps,
- 53:44then typically at the bottom of the pep, there'll be a link to example implementation.
- 53:49And that typically sits on a fork of the CPython repo on that core developer's copy.
- 53:55So you can actually see different experimental versions and different experimental features,
- 54:00like some of the work that Eric Snow was doing on subinterpreters,
- 54:03lived inside his fork, which is really interesting to explore.
- 54:07But it doesn't live in the main Python, CPython repository.
- 54:11Right. Not until it's officially accepted, part of it.
- 54:13Yeah. And then when it gets officially accepted, they typically rewrite it anyway and clean up the code.
- 54:18Yeah, I can imagine.
- 54:19Oh, very cool.
- 54:20And then I, you know, just it's worth throwing out that we're over on GitHub.com slash Python slash CPython.
- 54:26But if you step it up one level just to the Python organization,
- 54:29there's actually some other interesting projects there as well.
- 54:33Right. We've got the peps are over there.
- 54:35TypeShed, which are the stubs that define the static types for various Python things.
- 54:40Python.org is there.
- 54:42The dev guide that you referenced, a bunch of stuff to go play with, right?
- 54:45Yeah, there's heaps of stuff on there.
- 54:46Super cool.
- 54:47All right, Anthony.
- 54:48Well, this is really interesting.
- 54:49Thanks for doing all the research, writing this great article, and walking us all through it.
- 54:54It's going to be a good resource for years to come, I think.
- 54:56Yeah, I'm hoping that people read this and get some value out of it, get some knowledge,
- 55:00and hopefully fix some bugs, write some documentation, do some tests,
- 55:04or maybe even add a new feature and get it merged into CPython.
- 55:08Yeah, absolutely. You know, one really quick thing, you did mention that as you were writing this,
- 55:12the source code that you were referring back to was changing, and you didn't want it to get out
- 55:16of date. So you also did some cool extensions to keep the article up to date, didn't you?
- 55:21In the article, I reference some of the functions actually a lot of times. So like thousands of times,
- 55:27I reference a particular function or a particular file. And as I was writing the article,
- 55:32obviously the code is not static, it's changing all the time. So what I ended up having to do was
- 55:37actually write a markdown preprocessor in Python.
- 55:41Were you inspired by the C macros you were seeing all over the place?
- 55:44Yeah, I just saw so many macros. So I thought it'd be a good idea. So in the article,
- 55:48like if I reference a function, you can click on it, and it takes you to the GitHub source code,
- 55:55and it takes you straight to the line where that function is defined. But actually, that work was
- 56:00done using Python using a preprocessor, so that I can basically refresh the article with newer versions
- 56:07of newer versions of CPython as they come out, and it rewrites it for me.
- 56:11That's awesome. I love it. That's really, really a cool way to approach it.
- 56:14All right, so I think we have to leave it there for the guided tour. But I do have the two questions
- 56:18to ask you, of course, before I let you out of here. I'm going to write some Python code. Maybe let's
- 56:23change it. If we're going to work on this project, right, where it's got Python and C,
- 56:28what editor are you going to use on it?
- 56:29I use, thanks to the nice people, JetBrains. I've got access to PyCharm and CLion. CLion is the
- 56:38C version of PyCharm. So it's made by JetBrains. It's very similar. So I use CLion for really deep
- 56:45debugging and PyCharm for exploring some of the Python code. And then when I was writing on Windows,
- 56:52I would use Visual Studio 2019. So I'd recommend either of those two stacks. You can use VS Code
- 56:59in both environments as well. But for the deep debugging and stuff like that, then I found that
- 57:04CLion was pretty good.
- 57:05Yeah, that's cool. I was definitely considering CLion to explore this as well. But I'm not going
- 57:09to compile it. I just want to walk through it and kind of pull some stuff out. So VS Code.
- 57:12Super cool. All right. And then notable PyPI package. What have you run across lately that you're
- 57:17like, oh, this is sweet?
- 57:18That's a hard one, actually. I'm kind of working on a few at the moment.
- 57:22Yeah, at the moment, I'm working on pytest support for Azure Pipelines. So if you search
- 57:30for pytest as Azure Pipelines, if you're using Azure's new CI/CD service, and you're using pytest,
- 57:36then please check out the module. It basically automates how you run pytest and upload test results.
- 57:42And it gives you coverage automatically and stuff like that. So yeah, it's a really,
- 57:46really simple package that I put together. And it's ended up being referenced in the documentation.
- 57:51So it's become quite popular pretty quickly.
- 57:53That's super cool. So it's becoming officially part of the way to work over there, right?
- 57:58Yeah. And then there's also now a plugin for your GUI. So in the Azure GUI, if you install this plugin,
- 58:05you can actually get all the pytest information in the UI as well.
- 58:09Okay, that's awesome. People definitely have to check that out. All right, final call to action.
- 58:13People are excited about CPython. You gave them some ideas about going back,
- 58:18adding some documentation, adding some tests, looking for unsolved bugs, things like that.
- 58:22What are you telling them?
- 58:23Oh, yeah. Have fun as well. Like you can add silly features. You can do experiments.
- 58:28You know, don't feel like you have to do something that's, you know, might seem like a chore at first.
- 58:34Just experiment, see what you can break, see what you can change, and run your own custom fork.
- 58:40And just I think you'll learn a lot by just experimenting.
- 58:43Yeah, it's super easy to get started with the article you put together. So it's fun to experiment for sure.
- 58:46All right. Well, thanks. Bye.
- 58:48Thanks, Michael.
- 58:50This has been another episode of Talk Python To Me.
- 58:52Our guest on this episode was Anthony Shaw, and it's been brought to you by Linode and the University of San Francisco.
- 58:58Linode is your go-to hosting for whatever you're building with Python.
- 59:03Get four months free at talkpython.fm/linode. That's L-I-N-O-D-E.
- 59:08Learn how to use Python to analyze the digital economy in the Masters of Applied Economics at the University of San Francisco.
- 59:16Just go to talkpython.fm/USF to find out more.
- 59:20Want to level up your Python?
- 59:23If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 59:27Or if you're looking for something more advanced, check out our new async course that digs into all the different types of async programming you can do in Python.
- 59:36And of course, if you're interested in more than one of these, be sure to check out our Everything Bundle.
- 59:40It's like a subscription that never expires.
- 59:43Be sure to subscribe to the show.
- 59:44Open your favorite podcatcher and search for Python.
- 59:47We should be right at the top.
- 59:48You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 59:57This is your host, Michael Kennedy.
- 59:59Thanks so much for listening.
- 1:00:00I really appreciate it.
- 1:00:02Now get out there and write some Python code.
- 1:00:03I really appreciate it.