[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

Saturday, May 29, 2021

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

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

Thursday, May 27, 2021

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

Thursday, May 27, 2021

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…

Tuesday, May 25, 2021

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*

[Sublime + Python Setup] Grumpy old greybeard with a whitespace problem

Monday, May 24, 2021

One fateful day, the Agile Gods that be decided to “add some firepower” to my little team… And so, developer Paul joined (name changed to protect the guilty). Before I dive into this story, let me ask

You Might Also Like

Hacker Newsletter #699

Friday, May 10, 2024

If you're thinking without writing, you only think you're thinking. //Leslie Lamport hackernewsletter Issue #699 // 2024-05-10 // View in your browser #Favorites Turbocharging Low-Code

OpenAI publisher pitch leaks 🤖, Xbox mobile game store 🎮 , dangerous protocols 🌎

Friday, May 10, 2024

OpenAI is pitching partnership opportunities to news publishers through its Preferred Publishers Program. It has been courting publishers since July Sign Up |Advertise|View Online TLDR Together With

Master iOS 17.5

Friday, May 10, 2024

Did you know that Apple has released 12 updates to iOS 17 since its release last fall? Apple has added a ton of major new features, including updates to Messages, Photos, and a brand new built-in app!

Press, Pause 🗜️

Friday, May 10, 2024

Apple doesn't understand why people like hydraulic presses. Here's a version for your browser. Hunting for the end of the long tail • May 09, 2024 Press, Pause Beyond misunderstanding its iPad

Data Science Weekly - Issue 546

Friday, May 10, 2024

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

Dell’s data breach

Thursday, May 9, 2024

Plus: Mistral AI is raising funds and Bumble's new strategy View this email online in your browser By Christine Hall Thursday, May 9, 2024 Welcome back to TechCrunch PM. Today I have for you a

💻 Issue 416 - The new disposable APIs in Javascript

Thursday, May 9, 2024

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

💻 Issue 409 - Making a 3D modeler in C in a week

Thursday, May 9, 2024

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

📱 Issue 410 - FDA recalls defective iOS app that injured over 200 insulin pump users

Thursday, May 9, 2024

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

💎 Issue 416 - Ruby typing 2024: RBS, Steep, RBS Collections, subjective feelings

Thursday, May 9, 2024

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