Django

Code

Ticket #7919 (closed: fixed)

Opened 4 months ago

Last modified 4 months ago

Import hashlib instead of md5/sha when possible

Reported by: Karen Tracey <kmtracey@gmail.com> Assigned to: kmtracey
Milestone: 1.0 beta Component: Uncategorized
Version: SVN Keywords: python26
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

hashcompat.diff (18.7 kB) - added by Karen Tracey <kmtracey@gmail.com> on 08/01/08 10:32:55.
hashcompat2.diff (17.6 kB) - added by kmtracey on 08/01/08 15:41:05.
Let's try that patch again without the unrelated mysql base changes...that was for testing out a different fix

Change History

(follow-up: ↓ 2 ) 07/23/08 12:34:11 changed by mtredinnick

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

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.

(in reply to: ↑ 1 ) 07/23/08 12:55:40 changed 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.

08/01/08 10:32:55 changed by Karen Tracey <kmtracey@gmail.com>

  • attachment hashcompat.diff added.

08/01/08 10:35:43 changed by Karen Tracey <kmtracey@gmail.com>

  • has_patch set to 1.

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.

08/01/08 14:11:18 changed 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.

08/01/08 15:41:05 changed by kmtracey

  • attachment hashcompat2.diff added.

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

08/02/08 00:56:58 changed by gwilson

  • status changed from assigned to closed.
  • resolution set to fixed.

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


Add/Change #7919 (Import hashlib instead of md5/sha when possible)




Change Properties
Action