Debugging in Python, part 14: Resolving recursion issues

MP 158: Fixing a recursion bug that only shows up after many consecutive games.

Note: This post is part of an ongoing series about debugging in Python. The posts in this series will only be available to paid subscribers for the first 6 weeks. After that they will be available to everyone. Thank you to everyone who supports my ongoing work on Mostly Python.

In the last post, we finished implementing the basic logic to run through an entire Go Fish game. At the end of a game, we let the user choose whether to play again. There's one last significant bug that I've become aware of, which only shows up after a number of consecutive games. In this post, we'll dig into this cross-game bug. We'll add an option to automate game play in order to help replicate, diagnose, and address the bug.

An automated player

When playing a bunch of partial games during development work, I noticed longer tracebacks than I'd expect to see from quitting mid-game, or quitting a debugging session. I wanted a way to automatically play through an entire game, and see the state of the game after a full game or several full games. I was curious to see if these tracebacks grow as the number of games played increases. I was also curious to see if the program would crash after a certain number of consecutive games.

One approach that helps with debugging is to automate your program's inputs in a way that brings you to the point of execution that you want to focus on. This kind of work also tends to make writing tests easier. There are only two kinds of inputs during an active Go Fish game. The player makes guesses, and they press Enter after pauses. We can automate an entire game by guessing for the player, and skipping all pauses during a game.

A more formal CLI

I used some simple sys.argv values to implement a minimal CLI in earlier posts, but now I want some formal named CLI arguments. I want to be able to automatically play through one game, or an arbitrary number of games. So I want to be able to run the game in these two ways:

$ python go_fish.py --auto-play
$ python go_fish.py --auto-play --num-games 10

The first will automatically play through a single game, and then ask whether you want to play again. The second will automatically play through 10 games, and then ask if you want to play again.

In a small project like this, it's pretty straightforward to add a CLI by adding a cli.py file that looks like this:

This post is only available to paid subscribers at the moment. It will be available to everyone 6 weeks after posting. If you'd like to continue reading now, please support my work by signing up for a paid subscription.

Paid subscription options