[Sublime + Python Setup] The Ctrl+s "Heisenbug"

"What the **** is going on?!" I heard Keith yell.

Returning from my lunch break and in a helpful mood I grabbed my coffee mug and shuffled over to my coworker's desk.

"What's wrong?"

Keith was working on a ticket to fix an issue with our Python-based web portal. The result of some numerical calculation was way off. It seemed like a straightforward algorithmic fix…

"I think this Python code is haunted or something," said Keith as I sat down next to him. "Opening and saving this source file fixes one bug, but then 5 others show up!"

Two hours of swearing and pair-programming later, our investigation of the "haunted code" revealed the following steps to reproduce the issue:

First, we check out the original version of the affected source file from Git. Bug A shows up. So far, so expected.

Next, we open the file in Sublime Text and, without making any edits, immediately hit Ctrl+S to save it again. Now bug A is gone—only to have brand new bugs B, C, and D show up!

Umm…

We were stumped.

"Maybe it's some odd whitespace issue…"

You see, whitespace has a meaning in Python. It uses whitespace indentation levels to determine how code blocks are nested.

Personally, I really like the idea of "semantic whitespace". But occasionally it can lead to pure hell. Take a look at the screenshot below:

Image

Running this script you'd expect to get 10 × 10 = 100 as the answer.

However, by using a mixture of space and tab (\t) characters, you can get this program to print "10"—a completely bogus answer:

For Python indenting, a tab is counted as equivalent to 8 spaces…

And code that looked correct on our screens was actually way off for the Python interpreter:

def square(x):
    result = 0
    for i in range(x):
        result += x
<\t>return result  # ← 1 tab character
^^^^               #   instead of 4 spaces

Remember, one tab is equal to 8 spaces. So this mixture of tab and spaces gets parsed as the following:

def square(x):
    result = 0
    for i in range(x):
        result += x
        return result  # ← 1 tab == 8 spaces

Now the return-statement is indented one level too far. It breaks out of the loop after the first iteration —

D'Oh!

Now by merely re-saving the file in Sublime these tab characters were converted to 4 space characters each. Thus fixing the original indentation problem, but also introducing several new ones elsewhere in the code…

Double D'Oh!

In the end Keith and I easily spent 20 or more developer hours on tracking down various bugs caused by inconsistent whitespace throughout the code base.

It was a nightmare of a bug to fix, and what frustrated me the most about it was how easily it could've been avoided in the first place:

Had we used static code analysis tools back then we would've caught these problems much more easily. Code linting tools would've simply highlighted this whitespace issue right in our editors (and on our build server):

Image

For this reason I'm a big proponent of static code analysis tools now. They can help you detect and void certain bugs and classes of errors completely.

A code linter can catch functional bugs like misspelled identifiers, or reveal code quality issues like unused variables or imports.

I won't say automated code analysis is a miracle cure (sometimes it feels like it) —

But usually the Return on Investment for these tools is simply through the roof. They help reduce debugging and code review time with just a tiny initial time investment.

Now, as awesome as these tools sound, there are some common gotchas to integrating them with Sublime Text:

If you're not careful, integrated code linting can get overly verbose and distracting—and it can slow your editing experience down to a crawl…

To see how to set up silky smooth code linting for Python in less than 10 minutes, click here for more.

— Dan Bader

Older messages

[Python Dependency Pitfalls] How to set the world on fire

Sunday, March 6, 2022

Hey there, #1 on my list of dependency management pitfalls is there for a good reason: It lead to a single developer causing mayhem and breaking thousands of open-source projects around the world in

[PythonistaCafe] Q&A

Saturday, March 5, 2022

Hey there, At this point you should have a pretty good idea of what PythonistaCafe is about and what makes it special. In this email I want to answer some common questions that I get asked about the

[Python Dependency Pitfalls] A total mess?

Friday, March 4, 2022

Hey there, Recently I watched a Pythonista ask for advice on setting up a Python project on his work machine. This new developer had some prior experience with NodeJS and had just started to get his

[Sublime + Python Setup] How to become a happier & more productive Python dev

Friday, March 4, 2022

Hey there, I really struggled with setting up an effective development environment as a new Python developer. It was difficult to build the right habits and to find a set of tools I enjoyed to use.

[PythonistaCafe] What's in PythonistaCafe for you?

Friday, March 4, 2022

Hey there, A couple of years ago I'd become quite interested in martial arts. Hours upon hours of watching "The Karate Kid" growing up must've taken their toll on me... And so, I

You Might Also Like

📧 EF Core Migrations: A Detailed Guide

Saturday, May 18, 2024

​ EF Core Migrations: A Detailed Guide Read on: m​y website / Read time: 10 minutes BROUGHT TO YOU BY ​ Low-code Framework for .NET Devs ​ Introducing Shesha, a brand new, open-source, low-code

Slack is under attack … and you don’t want that

Friday, May 17, 2024

Plus: OpenAI is not aligned with its Superalignment team View this email online in your browser By Christine Hall Friday, May 17, 2024 Good afternoon, and welcome back to TechCrunch PM. We made it to

Ilya Sutskever leaves OpenAI - Weekly News Roundup - Issue #467

Friday, May 17, 2024

Plus: Apple is close to using ChatGPT; Microsoft builds its own LLM; China is sending a humanoid robot to space; lab-grown meat is on shelves but there is a catch; hybrid mouse/rat brains; and more! ͏

SWLW #599: Surfing through trade-offs, How to do hard things, and more.

Friday, May 17, 2024

Weekly articles & videos about people, culture and leadership: everything you need to design the org that makes the product. A weekly newsletter by Oren Ellenbogen with the best content I found

💾 There Will Never Be Another Windows XP — Why Ray Tracing is a Big Deal in Gaming

Friday, May 17, 2024

Also: What to Know About Google's Project Astra, and More! How-To Geek Logo May 17, 2024 Did You Know The very first mass-manufactured drinking straw was made of paper coated in wax; the straw was

It's the dawning of the age of AI

Friday, May 17, 2024

Plus: Musk is raging against the machine View this email online in your browser By Haje Jan Kamps Friday, May 17, 2024 Image Credits: Google Welcome to Startups Weekly — Haje's weekly recap of

Daily Coding Problem: Problem #1444 [Medium]

Friday, May 17, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Yahoo. Recall that a full binary tree is one in which each node is either a leaf node,

(Not) Sent From My iPad

Friday, May 17, 2024

The future of computing remains frustrating (Not) Sent From My iPad By MG Siegler • 17 May 2024 View in browser View in browser I tried. I really did. I tried to put together and send this newsletter

iOS Dev Weekly - Issue 661

Friday, May 17, 2024

What's the word on everyone's lips? 🅰️👁️ View on the Web Archives ISSUE 661 May 17th 2024 Comment Did you catch Google I/O this week? It's Always Interesting to see what the Android

Your Google Play recap from I/O 2024

Friday, May 17, 2024

Check out all of our latest updates and announcements Email not displaying correctly? View it online May 2024 Google Play at I/O 2024 Check out the Google Play keynote to discover the latest products