Opened 10 years ago

Closed 10 years ago

Last modified 6 years ago

#7919 closed (fixed)

Import hashlib instead of md5/sha when possible

Reported by: Karen Tracey <kmtracey@…> Owned by: Karen Tracey
Component: Uncategorized Version: master
Severity: Keywords: python26
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Running under Python 2.6 (beta2) we get DeprecationWarnings wherever we import md5 or sha, with the recommendation to use hashlib instead. I can work on a patch to get rid of these if I can get some input on the correct way to fix it. Looking at the uses I see so far (haven't quite checked them all yet), it seems that something like:

try:
    import hashlib
    md5_constructor = hashlib.md5
except ImportError:
    import md5
    md5_constructor = md5.new

to replace "import md5", plus of course updating the calls to md5.new() or md5.md5() to use md5_constructor() would work. (Plus of course the same technique for where we import sha.) But I don't know if that's the most elegant/practical/desirable way to handle stuff like this, so guidance would be appreciated.

Attachments (2)

hashcompat.diff (18.7 KB) - added by Karen Tracey <kmtracey@…> 10 years ago.
hashcompat2.diff (17.6 KB) - added by Karen Tracey 10 years ago.
Let's try that patch again without the unrelated mysql base changes...that was for testing out a different fix

Download all attachments as: .zip

Change History (8)

comment:1 Changed 10 years ago by Malcolm Tredinnick

Triage Stage: UnreviewedAccepted

If you have to use aliases in a bunch of places to handle something that has one name in Python 2.3 and another in Python 2.6, particularly where it's more than just a simple import alias, it's probably better to put a compatibility module in utils that does the version-dependent importing and aliasing for us. That way we only need to import one thing. We currently import sets via a four line statement everywhere and that's a borderline case (which I wouldn't change), but anything more complex than that, if we're using it in multiple models, could use a helper module I think.

Extreme cases are django.utils.itercompat, but that's the sort of thing I'm thinking about: hide the implementation messiness in one file that we can import everywhere. That being said, I have no idea how many files are affected here, but if it's more than one or two, I'd encourage the helper module.

comment:2 in reply to:  1 Changed 10 years ago by Karen Tracey

Owner: changed from nobody to Karen Tracey
Status: newassigned

Replying to mtredinnick:

Extreme cases are django.utils.itercompat, but that's the sort of thing I'm thinking about: hide the implementation messiness in one file that we can import everywhere. That being said, I have no idea how many files are affected here, but if it's more than one or two, I'd encourage the helper module.

Oh it's about a dozen for md5 imports, four for sha. I'll try for the helper module approach, then. Thanks for the guidance.

Changed 10 years ago by Karen Tracey <kmtracey@…>

Attachment: hashcompat.diff added

comment:3 Changed 10 years ago by Karen Tracey <kmtracey@…>

Has patch: set

Attached patch using the helper module approach. Tested on Python 2.6b2 and 2.5.1. There was one bare import of sha in tests/regressiontests/test_client_regress/models.py that I simply removed because I couldn't find where it was being used.

comment:4 Changed 10 years ago by Karen Tracey

Oh yeah, tested on 2.3.5 also, since that's where the 'compatible' part actually gets exercised. My but the tests run slow on 2.3.5.

Changed 10 years ago by Karen Tracey

Attachment: hashcompat2.diff added

Let's try that patch again without the unrelated mysql base changes...that was for testing out a different fix

comment:5 Changed 10 years ago by Gary Wilson

Resolution: fixed
Status: assignedclosed

(In [8193]) Fixed #7919 -- md5 and sha modules are deprecated since Python 2.5, use hashlib module when available. Patch from Karen Tracey.

comment:6 Changed 6 years ago by Jacob

milestone: 1.0 beta

Milestone 1.0 beta deleted

Note: See TracTickets for help on using tickets.
Back to Top