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

  • Home
  • SEARCH
  • 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 6227589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:15:03+00:00 2026-05-24T09:15:03+00:00

Python is supposed to be strongly typed. For instance: ‘abc'[‘1’] won’t work, because you’re

  • 0

Python is supposed to be strongly typed.

For instance: 'abc'['1'] won’t work, because you’re expected to provide an integer there, not a string. An error wil be raised and you can go on and correct it.

But that’s not the case with hashlib. Indeed, try the following:

import hashlib
hashlib.md5('abc') #Works OK        

hashlib.md5(1) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: md5() argument 1 must be string or read-only buffer, not int

hashlib.md5(u'abc') #Works, but shouldn't : this is unicode, not str.

haslib.md5(u'é')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)

Of course, it does not fail because of a TypeError, but because of UnicodeEncodeError. UnicodeEncodeError is supposed to be raised when you attempt to encode unicode to a string.

I think I’m not too far from the truth when my guess is that Hashlib silently attempted to convert unicode to a string.

Now. I agree, hashlib indicated that the argument to hashlib.md5() should be a string or a read-only buffer, which a unicode string is.
But this actually goes to show that it actually is not: hashlib.md5() will work properly with strings and that’s about it.

Of course, the main problem this causes is that you will get an exception with some unicode strings, and not with some others.

Which leads me to my questions. First, do you have an explanation as to why hashlib implements this behavior? Second, is it considered an issue? Third, is there a way to fix this without changing the module itself?

Hashlib is basically an example, there are several other modules that behave the same when provided unicode strings – which leads you to an uncomfortable situation where your program will work with ASCII input but completely fail with accents.

  • 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-05-24T09:15:04+00:00Added an answer on May 24, 2026 at 9:15 am

    It’s not just hashlib – Python 2 handles Unicode in a number of places by trying to encode it as ascii. This was one of the big changes made for Python 3.

    In Python 3, strings are unicode, and they behave as you expect: there’s no automatic conversion to bytes, and you have to encode them if you want to use bytes (e.g. for MD5 hashing). I believe there are hacks using sys.setdefaultencoding that enable this behaviour in Python 2, but I’d advise against using them in production, because they’ll affect any code running in that Python instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a module that is supposed to work in both Python 2
I have a python app which is supposed to be very long-lived, but sometimes
If a Python 3 class is pickled using protocol 2, it is supposed to
I'm trying to call io_submit using python ctypes. The code I'm writing is supposed
There are many escape functions in the Python module mysqldb whose documentation I don't
It seems that Python has some limitations regarding instance methods. Instance methods can't be
In the Python data model reference section on slots there is a list of
I have this python script which is supposed to wrap all that look like
I have a Python class called ClassA and another Python class which is supposed
I've been learning python here recently....mainly for scripting purposes....since apparently it's supposed to be

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.