Skip to content

Python 2.7 and "from __future__ import unicode_literals" causes UnicodeEncodeError #2276

@derterz

Description

@derterz

I have some very strange UnicodeEncodeError in my application. I could reduce the problem to the following example:

from __future__ import unicode_literals
deg = u'\u00B0'
'%s%s' % ('15', deg)

When I compile this example with cython>=0.28.0 and run is with Python 2.7.14 I get the following error:

C:\tmp\python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import crash
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "crash.pyx", line 3, in init crash
    '%s%s' % ('15', deg)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 0: ordinal not in range(128)

Using cython==0.27.3 there is no such error and the example works fine.

By looking at the code it seems the "PyObject_Str" statement causes the problem. However I have no idea how to fix the issue except for using cython 0.27.3 for python 2.7.

  /* "crash.pyx":3
 * from __future__ import unicode_literals
 * deg = u'\u00B0'
 * '%s%s' % ('15', deg)             # <<<<<<<<<<<<<<
 */
  __Pyx_GetModuleGlobalName(__pyx_t_1, __pyx_n_s_deg); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_FormatSimpleAndDecref(PyObject_Str(__pyx_t_1), __pyx_empty_unicode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 3, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_PyUnicode_Concat(__pyx_kp_u_15, __pyx_t_2); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions