Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#7919 closed (fixed)

Import hashlib instead of md5/sha when possible

Reported by: Karen Tracey <kmtracey@…> Owned by: kmtracey
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@…> 7 years ago.
hashcompat2.diff (17.6 KB) - added by kmtracey 7 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 follow-up: Changed 7 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 7 years ago by kmtracey

  • Owner changed from nobody to kmtracey
  • Status changed from new to assigned

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 7 years ago by Karen Tracey <kmtracey@…>

comment:3 Changed 7 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 7 years ago by kmtracey

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 7 years ago by kmtracey

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

comment:5 Changed 7 years ago by gwilson

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 4 years ago by jacob

  • milestone 1.0 beta deleted

Milestone 1.0 beta deleted

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