#7919 closed (fixed)
Import hashlib instead of md5/sha when possible
Reported by: | Owned by: | Karen Tracey | |
---|---|---|---|
Component: | Uncategorized | Version: | dev |
Severity: | Keywords: | python26 | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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)
Change History (8)
follow-up: 2 comment:1 by , 16 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → 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.
by , 16 years ago
Attachment: | hashcompat.diff added |
---|
comment:3 by , 16 years ago
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 by , 16 years ago
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.
by , 16 years ago
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 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
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.