Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:45:18+00:00 2026-06-11T22:45:18+00:00

A very common source of encoding errors is that python 2 will silently coerce

  • 0

A very common source of encoding errors is that python 2 will silently coerce strings to unicode when you add them together with unicode. This can cause mixed encoding problems and can be very hard to debug.

For example:

import urllib
import webbrowser
name = raw_input("What's your name?\nName: ")
greeting = "Hello, %s" % name
if name == "John":
    greeting += u' (Feliz cumplea\xf1os!)'
webbrowser.open('http://lmgtf\x79.com?q=' + urllib.quote_plus(greeting))

will fail with a cryptic error if you enter “John”:

/usr/lib/python2.7/urllib.py:1268: UnicodeWarning: Unicode equal comparison faile
d to convert both arguments to Unicode - interpreting them as being unequal
  return ''.join(map(quoter, s))
Traceback (most recent call last):
  File "feliz.py", line 7, in <module>
    webbrowser.open('http://lmgtf\x79.com?q=' + urllib.quote_plus(greeting))
  File "/usr/lib/python2.7/urllib.py", line 1273, in quote_plus
    s = quote(s, safe + ' ')
  File "/usr/lib/python2.7/urllib.py", line 1268, in quote
    return ''.join(map(quoter, s))
KeyError: u'\xf1'

It’s particularly hard to track down when the actual errors come far down the road from where the actual coercion happened.

How can you configure python to give a warning or exception immediately when strings are coerced to unicode?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:45:20+00:00Added an answer on June 11, 2026 at 10:45 pm

    I did a little more research after asking this question and hit on the perfect answer. Armin Ronacher created a wonderful little tool called unicode-nazi. Just install it and run your program like this:

    python -Werror -municodenazi myprog.py
    

    and you get a traceback right where the coercion happened:

    Traceback (most recent call last):
      File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
        "__main__", fname, loader, pkg_name)
      File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
        exec code in run_globals
      File "SITE-PACKAGES/unicodenazi.py", line 128, in <module>
        main()
      File "SITE-PACKAGES/unicodenazi.py", line 119, in main
        execfile(sys.argv[0], main_mod.__dict__)
      File "myprog.py", line 4, in <module>
        print foo()
      File "myprog.py", line 2, in foo
        return 'bar' + u'baz'
      File "SITE-PACKAGES/unicodenazi.py", line 34, in warning_decode
        stacklevel=2)
    UnicodeWarning: Implicit conversion of str to unicode
    

    If you’re dealing with python libraries that trigger implicit coercions themselves and you can’t catch the exceptions or otherwise work around them, you can leave out the -Werror:

    python -municodenazi myprog.py
    

    and at least see a warning printed out on stderr when it happens:

    /SITE-PACKAGES/unicodenazi.py:119: UnicodeWarning: Implicit conversion of str to unicode
      execfile(sys.argv[0], main_mod.__dict__)
    barbaz
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I believe I have a very common problem that affects developers with multiple versions
This is a very common but rarely solved topic. I seriously need some help
It is very common we link CSS and JS files in out HTML, PHP
OK this is a very common problem but mine is a bit different and
This is a very common scenario: displaying images in a ListView which have to
It is very common but important question for me as in my project I
This is a very common thing in web applications. If I have a user
Use of gradient images is very common among developers for styling a page. Gradient
The following simple code pattern is very common in graphics programming. It creates an
I'm looking for a very common element - I want to pop a modal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.