Oddball issue arising with use of TextWrangler to edit Python 2.7 script which tests multithreading and Queue modules

StackOverflow https://stackoverflow.com/questions/12255433

Question

I'm writing a script in Python 2.7 exploring the use of the multithreading and Queue modules. the script just defines two functions, one of which will be started in a thread instance to fill a Queue, and a second one which will be started in a second thread instance to pull something off the Queue. However, I cannot even get this script to execute, as it chokes on one of the function definitions even before it executes. The code snippet with the problem is:

def listenThread(counter):
    while queue.empty() != True:
        try:
            outcome = queue.get()
            print outcome
            counter -=1
            print counter 
        except: 
            return 'queue.get() command loop failing.'

The error message I am getting is:

$ python threading-and-queue-test.py
  File "threading-and-queue-test.py", line 34
    except: 
           ^
IndentationError: unindent does not match any outer indentation level
$

I've searched SO and checked the docs for correct formation of the try-except construct, and it seems OK. I had earlier versions of the code (without the try-except ) working, so I am getting suspicious about the text editor, TextWrangler v4.0.1.

Can anyone suggest a debug approach or code correction? Thanks! Red

Was it helpful?

Solution

As you have already suspected, it is most likely an indentation error. Your text editor is probably mixing the use of tabs and spaces. Replace all tabs with spaces (or vice versa though the standard in python is 4 spaces) and you should see the error disappear.

OTHER TIPS

For those who find this question later like I did it's easy to fix TextWrangler's settings and stop this issue altogether; Go to "Preferences" -> "Editor Defaults" -> "Auto-expand tabs" then set tabs to 4 spaces. Restart TextWrangler for changes to take affect. For existing documents that continue giving you a problem read this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top