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: 21411
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: 21411
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: 21411
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

Posted on
Wed Aug 21, 2019 2:28 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Python Error help???

matt (support) wrote:
...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...

So, it is now 6 1/2 years later :wink: and I have run into the same problem. I am working on a script that can be run both inside and outside Indigo. I placed an import indigo command in a try block and then trap the exception to determine if I am not in Indigo. Then I can set a constant and use that to, more-or-less, IFDEF parts of the code as appropriate. Then, if I am in Indigo, I try to get some variables by name and need to trap the KeyError if the variable does not exist, and then log an error and quit.

return works, as long as the script is run by the IndigoPluginHost. But, if I try to run the script from the command line. However, it fails with SyntaxError: 'return' outside function when run from the command line. Not a big deal, I guess I can just give up the ability top run from the command line, though I kinda of liked the idea of the script being portable.

Posted on
Wed Aug 21, 2019 4:57 am
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Python Error help on exit() call

You could try

try:return
except : pass
try: exit()
except: pass

Then you have both and it should suppress the error messages!?



Sent from my iPhone using Tapatalk

Posted on
Wed Aug 21, 2019 5:27 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Python Error help on exit() call

kw123 wrote:
You could try
    try:return
    except : pass
    try: exit()
    except: pass
Then you have both and it should suppress the error messages!?...

You'd think. Unfortunately, it is not a run-time error. It blows up during the initial code interpretation with a Syntax Error:
Code: Select all
$ ./somecode.py
  File "./somecode.py", line 67
    return
SyntaxError: 'return' outside function

Posted on
Wed Aug 21, 2019 5:30 am
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Python Error help on exit() call

You could set a variable to “return” or “exit()”

Then exec that variable in the code.



Sent from my iPhone using Tapatalk

Posted on
Wed Aug 21, 2019 5:59 am
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Python Error help on exit() call

Code: Select all
var1 = "return"
var2 = "exit()"

....


try: exec(var1)
except: pass
try: exec(var2)
except: pass


should do the trick

Posted on
Wed Aug 21, 2019 1:03 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Python Error help on exit() call

kw123 wrote:
Code: Select all
var1 = "return"
var2 = "exit()"
... ...
try: exec(var1)
except: pass
try: exec(var2)
except: pass
should do the trick

Now that is an interesting idea. Unfortunately, for the moment I have given up and have the script written only for the indigoP{luginHost. But, sooner or later I will get back to try g to make it work in the shell and Indigo and I will give your suggestion a whirl.

Thanks!

Posted on
Wed Aug 21, 2019 2:08 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Python Error help on exit() call

probably you don't need the variables just exec("return") & exec("exit()") should be fine

Karl

Posted on
Sat Aug 24, 2019 5:10 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Python Error help on exit() call

Next version will handle the scripts with exit() calls better. Using exit() or exit(0) will silently stop the script execution. Calling exit() with a non-zero value or string will log an error to the Event Log and exit.

Image

Posted on
Sun Aug 25, 2019 8:27 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Python Error help on exit() call

matt (support) wrote:
Next version will handle the scripts with exit() calls better. Using exit() or exit(0) will silently stop the script execution. Calling exit() with a non-zero value or string will log an error to the Event Log and exit.

:!: One big attaboy!

Who is online

Users browsing this forum: No registered users and 5 guests