Ask Your Question
2

UnicodeDecodeError when doing nothing with strings

asked 2017-10-13 22:02:49 +0200

Weux082690 gravatar image

updated 2023-01-09 23:59:43 +0200

tmonteil gravatar image

I am attempting to create a function which increments a Gray code count, while returning which bit was flipped. I am implementing this in C with a Cython wrapper to be able to access it in Sage.

On the Sage command line, I am trying to load this Cython file:

cdef extern from "gray_2.c": 
    long next_gray_code(long* x, long* parity)

With this C file:

long next_gray_code(long* x, long* parity) {
            y = -(*x)&(*x);
            *x = (*x)^(y << 1);
            return log2(y) + 1;            }

And I am getting this error:

    Compiling ./gray.spyx...
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in run_code(self, code_obj, result)
   2896             if result is not None:
   2897                 result.error_in_exec = sys.exc_info()[1]
-> 2898             self.showtraceback()
   2899         else:
   2900             outflag = 0

/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in showtraceback(self, exc_tuple, filename, tb_offset, exception_only)
   1822                     except Exception:
   1823                         stb = self.InteractiveTB.structured_traceback(etype,
-> 1824                                             value, tb, tb_offset=tb_offset)
   1825 
   1826                     self._showtraceback(etype, value, stb)

/usr/lib/python2.7/dist-packages/IPython/core/ultratb.pyc in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1404         self.tb = tb
   1405         return FormattedTB.structured_traceback(
-> 1406             self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1407 
   1408 

/usr/lib/python2.7/dist-packages/IPython/core/ultratb.pyc in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1312             # Verbose modes need a full traceback
   1313             return VerboseTB.structured_traceback(
-> 1314                 self, etype, value, tb, tb_offset, number_of_lines_of_context
   1315             )
   1316         else:

/usr/lib/python2.7/dist-packages/IPython/core/ultratb.pyc in structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
   1162 
   1163         formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
-> 1164                                                                tb_offset)
   1165 
   1166         colors = self.Colors  # just a shorthand + quicker name lookup

/usr/lib/python2.7/dist-packages/IPython/core/ultratb.pyc in format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset)
   1111             return ""
   1112 
-> 1113         last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
   1114 
   1115         frames = self.format_records(records, last_unique, recursion_repeat)

/usr/lib/python2.7/dist-packages/IPython/core/ultratb.pyc in find_recursion(etype, value, records)
    453     # quarter of the traceback (250 frames by default) is repeats, and find the
    454     # first frame (from in to out) that looks different.
--> 455     if not is_recursion_error(etype, value, records):
    456         return len(records), 0
    457 

/usr/lib/python2.7/dist-packages/IPython/core/ultratb.pyc in is_recursion_error(etype, value, records)
    439     # a recursion error.
    440     return (etype is recursion_error_type) \
--> 441            and "recursion" in str(value).lower() \
    442            and len(records) > 500
    443 

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1542: ordinal not in range(128)

Which makes no sense to me since nothing I am doing has anything to do with strings, nor am I using anything but ascii characters in my code files (also, the trace is unhelpful since none of the referenced functions are in my code). What is going on here and how do I fix it?

edit retag flag offensive close merge delete

Comments

My guess is that it tried to print a different error message but couldn't process some relevant string, like maybe the path. Does the path to the file have any non-ascii characters?

John Palmieri gravatar imageJohn Palmieri ( 2017-10-16 19:35:42 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-10-22 22:56:44 +0200

Jeroen Demeyer gravatar image

This is a known IPython problem: https://github.com/ipython/ipython/pu...

edit flag offensive delete link more
0

answered 2017-10-16 14:23:28 +0200

Weux082690 gravatar image

When attempting the same thing in the SageMath cloud (CoCalc now), I received the much more helpful error message that in the file included from "gray.spyx" the variable "y" was used before it was declared. After adding in that declaration, it works fine now.

Considering I'm switching back and forth from Python to C, errors like this are bound to happen, and I'm surprised Sage's normal command line doesn't give more helpful error messages.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-10-13 22:02:49 +0200

Seen: 376 times

Last updated: Oct 22 '17