Can I turn off implicit Python unicode conversions to find my mixed-strings bugs?

前端 未结 1 1272
既然无缘
既然无缘 2021-02-06 08:16

When profiling our code I was surprised to find millions of calls to
C:\\Python26\\lib\\encodings\\utf_8.py:15(decode)

I started debugging and found that across

1条回答
  •  一整个雨季
    2021-02-06 08:38

    The following should work:

    >>> import sys
    >>> reload(sys)
    
    >>> sys.setdefaultencoding('undefined')
    >>> u"abc" + u"xyz"
    u'abcxyz'
    >>> u"abc" + "xyz"
    Traceback (most recent call last):
      File "", line 1, in 
      File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/undefined.py", line 22, in decode
        raise UnicodeError("undefined encoding")
    UnicodeError: undefined encoding
    

    reload(sys) in the snippet above is only necessary here since normally sys.setdefaultencoding is supposed to go in a sitecustomize.py file in your Python site-packages directory (it's advisable to do that).

    0 讨论(0)
提交回复
热议问题