Select to view content in your preferred language

ArcSDESQLExecute error message contains unicode character

5456
7
01-26-2015 04:07 PM
BlakeTerhune
MVP Regular Contributor

If you execute an invalid SQL statement with arcpy.ArcSDESQLExecute(), it will return an error message with a unicode special character (see ArcSDESQLExecute_Error.gif). That unicode character in the error message actually causes an error with Python if you try to print the message (see PythonUnicode_Error.gif). Apparently this is a known Python issue.

I found you have to wrap the arcpy error message up in encoding so Python doesn't choke (haha).

except Exception as err:
        raise Exception(unicode(err.message).encode("utf-8"))

Why is this silly unicode character in an error message anyway? Has anyone else found a better way to deal with this?

0 Kudos
7 Replies
JoshuaBixby
MVP Esteemed Contributor

What version of ArcGIS and Python are you using?  The known Python issue you reference affected Python 2.5 and 2.6, which the last version to ship with one of those versions was ArcGIS 10.0.  I am able to print the error message without encoding changes and no crashes in ArcGIS 10.3.

That said, it is odd that the error message returns an invalid Unicode character.

0 Kudos
BlakeTerhune
MVP Regular Contributor

I'm using PyScripter for Python 2.7 with ArcGIS 10.2.2 (which I believe ships with Python 2.7.5).

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am not sure why you are getting an error when you print the Unicode with the invalid character.  Understanding there is an invalid character, printing the string from Python 2.7 shouldn't cause an error since the bug was fixed.  That said, it would be nice if Esri's error handling was cleaner.

0 Kudos
BlakeTerhune
MVP Regular Contributor

This Python document mentions the issue of printing Exceptions with unicode characters but it's just generically directed at Python 2.x

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The issue you are experiencing is likely due to your console the error messages are being printed to and not Python itself.

If I run similar code in the interactive Python windows in ArcMap, it prints fine.  If I run the code in PyCharm, it prints fine.  If I run the code from a Windows command prompt, it generates a similar error you are seeing.  My guess is that the PyScripter console is not coping with the Unicode and hence the extra error is being generated.

0 Kudos
BlakeTerhune
MVP Regular Contributor

You may be right, possibly something with my version of Python and the place I'm running it from. I'm not sure how to run it from Windows Command Prompt so I just tried it in IDLE and still get the same error.

Here's how I'm running it

import arcpy
try:
    sdeconn = r"C:\GISConnections\SDE@GTEST.sde"
    sql = "SELECT * FROM INVALID_TABLE"
    cnxn = arcpy.ArcSDESQLExecute(sdeconn)
    sqlresult = cnxn.execute(sql)
    for i in sqlresult:
        print i
except Exception as err:
    print unicode(err.message).encode("utf-8")  ## Successfully prints Esri error message
    print err  ## Throws Python error
finally:
    # Cleanup
    if 'cnxn' in locals():
        del cnxn

Thanks for your persistence, Joshua. At least I know how to handle the error for now. I'll just hope it will go away after the next ArcGIS upgrade we do.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Interesting, or oddly, IDLE on my 10.3 machine handles it fine.

arcpy_arcsdesqlexecute_print_error.PNG

I am still convinced the extra error message is tied to the console where the original error message is trying to be printed.  That said, I can't imagine a bit change in IDLE or Python between 10.2.2 and 10.3.

As you point out, you already know how to work around the issue, which is really the most important thing.  In terms of Unicode characters, it would be interesting to know why the ArcSDE errors are getting returned with a problematic character on the end.

0 Kudos