[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

[Sublime + Python Setup] why Atom can’t replace Sublime

Thursday, January 13, 2022

Hey there, "In a nutshell, Atom is a Sublime Text editor that just got better." When I read this quote I got so worked up I nearly fell off my chair. Because every time I've tried Atom or

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

Monday, January 10, 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] Q&A

Monday, January 10, 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

[PythonistaCafe] What's in PythonistaCafe for you?

Sunday, January 9, 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

[Sublime + Python Setup] Sublime Text is just a blank canvas…

Saturday, January 8, 2022

Hey there, When I became serious about optimizing Sublime Text with plugins, it was hard for me to separate the wheat from the chaff. Without a real guideline or roadmap I resorted to installing *any*

You Might Also Like

Data Science Weekly - Issue 540

Friday, March 29, 2024

Curated news, articles and jobs related to Data Science, AI, & Machine Learning ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

This Week in Rust #540

Friday, March 29, 2024

Email isn't displaying correctly? Read this e-mail on the Web This Week in Rust issue 540 — 27 MAR 2024 Hello and welcome to another issue of This Week in Rust! Rust is a programming language

The Value Of A Promise 🤞

Friday, March 29, 2024

How much is a promise from a tech company really worth, anyway? Here's a version for your browser. Hunting for the end of the long tail • March 28, 2024 The Value Of A Promise When you hear a

New Elastic Security for SIEM Training Course

Friday, March 29, 2024

Detect and respond to evolving threats ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ elastic | Search. Observe. Protect Detect anomalies and malicious behavior March

SBF gets 25 years 

Thursday, March 28, 2024

Sam Bankman-Fried is sentenced View this email online in your browser By Christine Hall Thursday, March 28, 2024 Welcome back to TechCrunch PM! The editorial team spent a chunk of the day discussing

💎 Issue 410 - Being laid off in 2023-2024 as an early-career developer

Thursday, March 28, 2024

This week's Awesome Ruby Newsletter Read this email on the Web The Awesome Ruby Newsletter Issue » 410 Release Date Mar 28, 2024 Your weekly report of the most popular Ruby news, articles and

💻 Issue 403 - Microsoft defends .NET 9 features competing with open source ecosystem

Thursday, March 28, 2024

This week's Awesome .NET Weekly Read this email on the Web The Awesome .NET Weekly Issue » 403 Release Date Mar 28, 2024 Your weekly report of the most popular .NET news, articles and projects

💻 Issue 410 - Node.js TSC Confirms: No Intention to Remove npm from Distribution

Thursday, March 28, 2024

This week's Awesome Node.js Weekly Read this email on the Web The Awesome Node.js Weekly Issue » 410 Release Date Mar 28, 2024 Your weekly report of the most popular Node.js news, articles and

💻 Issue 410 - JSDoc as an alternative TypeScript syntax

Thursday, March 28, 2024

This week's Awesome JavaScript Weekly Read this email on the Web The Awesome JavaScript Weekly Issue » 410 Release Date Mar 28, 2024 Your weekly report of the most popular JavaScript news, articles

📱 Issue 404 - Dependency Injection for Modern Swift Applications Part II

Thursday, March 28, 2024

This week's Awesome iOS Weekly Read this email on the Web The Awesome iOS Weekly Issue » 404 Release Date Mar 28, 2024 Your weekly report of the most popular iOS news, articles and projects Popular