Calculate Value tool fails, unicode error from paths with "\U" (rawunicodeescape)

5685
7
02-16-2011 01:01 PM
curtvprice
MVP Esteemed Contributor
- ArcGIS ArcInfo Desktop 10.0 SP 1
- Windows XP64 and Win732

I'm getting a strange error from the Calculate Value tool when I feed it a path variable:

Executing: PathTest
Executing (Calculate Value): CalculateValue r"C:\Users" # Variant
Start Time: Tue Feb 08 12:46:01 2011
ERROR 000539: Error running expression: r"C:\Users" <type 'exceptions.SyntaxError'>: (unicode error) 'rawunicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX (<string>, line 1)
Failed to execute (Calculate Value).


I have logged this as incident #896927 - the suggestion was to zap my HKCU registry and profile. Zapping the registry seemed to help, but the problem returns like a bad 70s song.

I'm posting it here in case someone else runs into it and finds a solution.
0 Kudos
7 Replies
ChrisFox3
Occasional Contributor III
Hi Curtis,

Thank you for posting this issue. We logged a defect for this issue, NIM066920 "Unicode escape is still being interpreted in Calculate Value (Model Builder) tool even when passing a raw string to the expression."

Unfortunately the only workaround I have been able to discover is to hard code the path, either in the expression or in a Model Variable that you pass as an inline variable, to have double slashes instead of single slashes.

For example "C:\\Users".
0 Kudos
NiklasNorrthon
Occasional Contributor III
It would help if you posted not just the traceback, but also the actual code that gave you that traceback...

I can think of two possible workarounds for this bug:

Use forward slashes instead of backslashes in your path string literals. That way you don't have to bother with raw strings, and that would work with unicode strings too, even if they contained the \U sequence (which would then be /U).

Use os.path.join do the concatenation of path strings for you.
0 Kudos
GerryGabrisch
Occasional Contributor III
This code worked to remove a \n escape sequence from a string, maybe you can alter the code to work for a \u

x = somestringwithanescapesequence"
x = repr(x)
x = x.replace(r'\n', "")
0 Kudos
NiklasNorrthon
Occasional Contributor III
This code worked to remove a \n escape sequence from a string, maybe you can alter the code to work for a \u

x = somestringwithanescapesequence"
x = repr(x)
x = x.replace(r'\n', "")


That seems like a terribliy complicated way to do something simple!
Strings don't containt escape sequences, they contain characters. Escape sequences occur in string literals, representing unprintable characters (e.g. line breaks). String literals are hardcoded strings in the source code.

The repr function converts an object to a printable expression. For strings that means putting quotes around it and escaping unprintable characters. That function can be handy for debugging, logging et cetera, but not for managing escape sequences (unless aiming for some obfuscated python coding contest).

The given example above will remove newlines from a string, but it will also do other probably unwanted things, like enclosing the string in quotes, and escaping tabs. To just remove newlines just do:

some_string.replace('\n', '')
0 Kudos
curtvprice
MVP Esteemed Contributor
The intermittent problem I was having with unicode was occurring only in modelbuilder. The problem path strings there are created by the system and the error would occur while the Calculate Value tool was reading the string -- so there was no place to work around it in code.

It was a very intermittent problem, fixed by resetting my HKCU registry hive, so I'm very happy that the ESRI support folks were able to reproduce it.
0 Kudos
CaseyBoettcher
New Contributor
The intermittent problem I was having with unicode was occurring only in modelbuilder. The problem path strings there are created by the system and the error would occur while the Calculate Value tool was reading the string -- so there was no place to work around it in code.

It was a very intermittent problem, fixed by resetting my HKCU registry hive, so I'm very happy that the ESRI support folks were able to reproduce it.


Could you please elaborate on what you mean by "resetting my HKCU registry hive"? I'm having this same issue and am wondering what I have to do to change the codec used by ModelBuilder to something other than raw_unicode_escape.

Also, this isn't an intermittent problem, in the sense that it's difficult to reproduce, given a static environment. It happens every time I run the model on a Windows 7 machine.
0 Kudos
curtvprice
MVP Esteemed Contributor

I have posted the details on resetting your user profile, including the HKCU registry stuff, here:

Resetting your ArcGIS application profile

If you continue to have trouble, I highly recommend contacting Esri support; they can walk you through this and help you solve your problem if the basic reset I describe above does not help.

0 Kudos