Error in goto module [Python]

Question:

Ok, let me start by saying I know that it is bad that I am using the goto module and I shouldn’t be and blah blah blah. However, for this specific purpose I need it. Let me also say that I am new to Python so try to avoid complicated answers, thanks!

So with that out of the way let me now explain my issue (I am on Linux). When I run my little program, it runs fine until I hit my first string input. After I type in the string and press enter, it gives me this error:

Traceback (most recent call last):
   File "main.py", line 16, in <module>
      empid = input("Example Input: ")
   File "<string>", line 1, in <module>
   File "/usr/local/lib/python2.7/dist-packages/goto.py", line 255, in _trace
     _addToCaches(filename)
   File "/usr/local/lib/python2.7/dist-packages/goto.py", line 230, in _addToCaches
      in tokenize.generate_tokens(open(moduleFilename, 'r').readline):
IOError: [Errno 2] No such file or directory: '<string>'

I have tried reinstalling the module, reinstalling python, and I’m not too sure that string would really be incorporated into goto.py anyway.

Thanks,
Cether

EDIT:
As requested, here is the code that leads up to the problem:

from goto import *
import time
import sys
import os

label .start
os.system('clear')
print "Example Printout"



exampin = input("Example Input: ")

The error happens when I press enter after I enter the string in that input.

Asked By: Cether

||

Answers:

There is no use case where you would absolutely need goto in Python. In the worst case, use a continuation and keep track of your state more manually.

Answered By: a p

goto.py is an April Fool’s Day joke. Do not ever use it. If you are using it seriously, you are not using Python seriously. However, the source is quite simple, so one can find out why this happens, from a purely academic viewpoint.

goto.py:284 contains the line sys.settrace(_trace). The sys.settrace function is designed to be used by debuggers, and basically "catches" each line of Python before it actually executes, to create these pseudo-syntaxes. The function _trace defined on line 251 assumes that each line of code that is about to be executed has an associated filename, and that filename can be opened.

However, when the input function is run, it executes arbitrary Python code to evaluate, the string. This is a terrible default, but that’s what it does, and it was changed in Python 3 to only return a string. When the code is being evaluated, it considers its filename to be <string>. This filename does not exist, so when the _trace function sees the code, it can’t open the filename and crashes while "debugging".

The solution: ensure that you are never executing code that doesn’t have an existing file associated with it. Avoid eval, input, exec, and anything that evaluates code outside of a file. Use raw_input instead.

Answered By: Alyssa Haroldsen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.