[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

Key phrases

Older messages

[PythonistaCafe] Why PythonistaCafe exists

Tuesday, July 26, 2022

Hey there, In one of my last emails I talked about how some online communities in the tech space devolve over time and turn into cesspools of negativity. This relates directly to how and why I started

[Python Dependency Pitfalls] A total mess?

Tuesday, July 26, 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

Monday, July 25, 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 makes PythonistaCafe different

Monday, July 25, 2022

Hey there, Mastering Python is *not* just about getting the books and courses to study—to be successful you also need a way to stay motivated and to grow your abilities in the long run. Many

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

Monday, July 25, 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*

Linux replacements for antiquated tools, reimagine math with Python and Raspberry Pi, and more

Tuesday, August 16, 2022

Create beautiful PDFs in LaTeX Create beautiful PDFs in LaTeX Opensource.com THE LATEST Our favorite Linux replacements for antiquated open source tools We asked our community of contributors what open

Issue #11: Focused Work by Michael Tigas

Tuesday, August 16, 2022

Today, we're taking a look at Focused Work by Michael Tigas. Focused Work is a powerful timer that helps you have highly productive days. Inspired by the Pomodoro Technique, it's purpose-built

New Blogs on ThomasMaurer.ch for 08/16/2022

Tuesday, August 16, 2022

View this email in your browser Thomas Maurer Cloud & Datacenter Update This is the update for blog posts on ThomasMaurer.ch. Schedule and Run PowerShell Scripts for Azure VMs using Azure

[Python Mastery] What does it mean to "master" Python?

Tuesday, August 16, 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

Using HPC clusters to run simulations & solve complex equations

Tuesday, August 16, 2022

How to select the best processor & HPC system View this email in your browser engineering.com White Paper - How to Select the Best Processor and HPC System for Your Workloads How to Select the Best

Don't miss out: Ready, Set, Actionable search results with Elastic Enterprise Search

Tuesday, August 16, 2022

Tailor-made for participants in tech and non-tech roles elastic | Search. Observe. Protect Search & Stream with Elastic Enterprise Search You've looked for a good movie to watch, but ever

Declining quality of consumer-grade products – 2009 fridge compressor autopsy — and Breaking all macOS security layers with a single vulnerability

Monday, August 15, 2022

Issue #860 — Top 20 stories of August 16, 2022 Issue #860 — August 16, 2022 You receive this email because you are subscribed to Hacker News Digest. You can open it in the browser if you prefer. 1

Meta backs SMB e-commerce app launched by former Facebook engineering manager

Monday, August 15, 2022

TechCrunch Newsletter TechCrunch logo The Daily Crunch logo By Christine Hall and Haje Jan Kamps Monday, August 15, 2022 Hello, Crunchers! Wait, that's kind of a weird nickname, as if you're

JSK Daily for Aug 15, 2022

Monday, August 15, 2022

JSK Daily for Aug 15, 2022 View this email in your browser A community curated daily e-mail of JavaScript news Handling Optimistic Concurrency in Web Frontends Building scalable web applications

Live Long, Vote and Nominate #Noonies2022

Monday, August 15, 2022

for the brightest stars in tech ✨ The Noonies 2022 Greetings, Hackers 👋🏽 As you well know, HackerNoon is recognizing the incredible work of those who have boldly gone where no one has gone before, for