Python Error help on exit() call

Posted on
Sat Jan 19, 2013 9:25 pm
jd10884 offline
Posts: 39
Joined: Feb 18, 2012

Python Error help on exit() call

I have this code in a Python script which should close the script when there is an error. If I run this in Python from the Terminal it works OK
Code: Select all
import urllib2
from urllib2 import URLError
from sys import exit

#location of temp alert file
url = "http://10.0.1.99/xmlfeed.rb"

try:
   response = urllib2.urlopen(url)
except URLError, e:
      print 'error: script had a URL error'
      #if there is an error exit script
      exit(0)


When run in Indigo and there is no error it runs fine but when there is an error I get this error in the log;

2013-01-19 1:13:06 PM
Script Error Error in plugin execution executeFile:

Traceback (most recent call last):
File "plugin.py", line 216, in executeFile
File "plugin.py", line 184, in _compileExecuteSource
File "<string>", line 23, in _dynamicFunc
<type 'exceptions.SystemExit'>: 0

Posted on
Sat Jan 19, 2013 9:31 pm
matt (support) offline
Site Admin
User avatar
Posts: 19134
Joined: Jan 27, 2003
Location: Texas

Re: Python Error help???

Remove the "exit(0)" line. I think that the Indigo python host isn't like that exit() command being called.

Image

Posted on
Sat Jan 19, 2013 11:06 pm
jd10884 offline
Posts: 39
Joined: Feb 18, 2012

Re: Python Error help???

The exit(0) is there to exit the script in case the script can't connect to the url. If the Indigo Host doesn't like this exit method, can you suggest another?

Posted on
Sat Jan 19, 2013 11:08 pm
matt (support) offline
Site Admin
User avatar
Posts: 19134
Joined: Jan 27, 2003
Location: Texas

Re: Python Error help???

So there is more to the script than what you posted above?

What about moving the additional logic/lines inside the existing try/except?

Oh, and you can try adding a "return" instead of the exit. Returns are not normally allowed outside the scope of a python function, but Indigo actually wraps the script up into a function when it calls it.

Image

Posted on
Mon Jan 21, 2013 7:34 am
jd10884 offline
Posts: 39
Joined: Feb 18, 2012

Re: Python Error help???

So there is more to the script than what you posted above?

Yes I just included the block that required the exit to simplify my post.


What about moving the additional logic/lines inside the existing try/except?

The way the script works is it copies the file from a network location, then it parses the file. If there is an error in getting the file then it will exit the script. If I put the entire code in a try block it will still execute the parse on the previously copied file which I don't want it to do. I'll see if I can find another way and try your "return" suggestion.

Oh, and you can try adding a "return" instead of the exit. Returns are not normally allowed outside the scope of a python function, but Indigo actually wraps the script up into a function when it calls it.

I am only beginning to learn Python and I am a bit confused. If Indigo supports Python then why does it not allow correct Python code in a script such as the "exit(0)" I am trying to use? If I run the script in Python in Terminal without the Indigo code, the exit(0) works fine. Can you explain why so I can perhaps avoid this problem in the future?

Posted on
Mon Jan 21, 2013 11:44 am
matt (support) offline
Site Admin
User avatar
Posts: 19134
Joined: Jan 27, 2003
Location: Texas

Re: Python Error help???

Indigo executes python in its own special process (called the IndigoPluginHost), which enables the python code to have access to Indigo's object model, APIs, (and for plugins, callback hooks). So yes, there are a few differences one of which apparently includes how exit() behaves. We'll look at making exit() behave correctly in a future build, but it might be a while before we get to it.

Here are a few more details and here is the other known caveat.

Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest