Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

test_hashlib

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


kristjan at ccpgames

Jul 21, 2012, 2:29 PM

Post #1 of 7 (479 views)
Permalink
test_hashlib

I was hit by this today.

in test_hashlib.py there is this:



def test_unknown_hash(self):

self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')

self.assertRaises(TypeError, hashlib.new, 1)



but in hashlib.py, there is this code:



except ImportError:

pass # no extension module, this hash is unsupported.

raise ValueError('unsupported hash type %s' % name)





The code will raise ValueError when int(1) is passed in, but the unittests expect a TypeError.

So, which is correct?



K


solipsis at pitrou

Jul 21, 2012, 2:58 PM

Post #2 of 7 (478 views)
Permalink
Re: test_hashlib [In reply to]

On Sat, 21 Jul 2012 21:29:50 +0000
Kristján Valur Jónsson <kristjan [at] ccpgames> wrote:
>
> The code will raise ValueError when int(1) is passed in, but the
> unittests expect a TypeError.

Well, if test_hashlib passes, surely your analysis is wrong, no?


--
Software development and contracting: http://pro.pitrou.net


_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


amauryfa at gmail

Jul 21, 2012, 3:56 PM

Post #3 of 7 (477 views)
Permalink
Re: test_hashlib [In reply to]

2012/7/21 Antoine Pitrou <solipsis [at] pitrou>:
> Kristján Valur Jónsson <kristjan [at] ccpgames> wrote:
>>
>> The code will raise ValueError when int(1) is passed in, but the
>> unittests expect a TypeError.
>
> Well, if test_hashlib passes, surely your analysis is wrong, no?

In the normal case, yes:

>>> import hashlib
>>> hashlib.new(1)
TypeError: name must be a string

But if the _hashlib extension module is not available, the python
version is used and ValueError is raised:

>>> import sys
>>> sys.modules['_hashlib'] = None
>>> import hashlib
>>> hashlib.new(1)
ValueError: unsupported hash type 1

--
Amaury Forgeot d'Arc
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


guido at python

Jul 21, 2012, 5:08 PM

Post #4 of 7 (475 views)
Permalink
Re: test_hashlib [In reply to]

I think I see Kristján's point: the pure Python implementation handles
errors differently than the C implementation, so the unittest fails if
the pure Python version is enabled. I imagine this is a general
problem that often occurs when a pure Python version is normally
shadowed by a C extension, unless the unittest is rigged so that it
tests the pure Python version as well as the C version. But it still
remains a matter of judgment whether in a particular case the unittest
is overspecified or the Python version is incorrect. I think that in
this case Kristján's hunch is correct, and the pure Python version
needs to be adjusted to pass the test. I also think this is a low
priority issue since it only affects behavior of error cases.

--Guido

On Sat, Jul 21, 2012 at 3:56 PM, Amaury Forgeot d'Arc
<amauryfa [at] gmail> wrote:
> 2012/7/21 Antoine Pitrou <solipsis [at] pitrou>:
>> Kristján Valur Jónsson <kristjan [at] ccpgames> wrote:
>>>
>>> The code will raise ValueError when int(1) is passed in, but the
>>> unittests expect a TypeError.
>>
>> Well, if test_hashlib passes, surely your analysis is wrong, no?
>
> In the normal case, yes:
>
>>>> import hashlib
>>>> hashlib.new(1)
> TypeError: name must be a string
>
> But if the _hashlib extension module is not available, the python
> version is used and ValueError is raised:
>
>>>> import sys
>>>> sys.modules['_hashlib'] = None
>>>> import hashlib
>>>> hashlib.new(1)
> ValueError: unsupported hash type 1
>
> --
> Amaury Forgeot d'Arc
> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org



--
--Guido van Rossum (python.org/~guido)
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


kristjan at ccpgames

Jul 21, 2012, 6:05 PM

Post #5 of 7 (471 views)
Permalink
Re: test_hashlib [In reply to]

Indeed, shame on me for not mentioning this.
I rarely have the full complement of externals available when I'm doing python work, and it struck me that this unitest was failing.
I suppose it should be possible to write unittests that test more than one particular implementation.

K

________________________________________
Frá: python-dev-bounces+kristjan=ccpgames.com [at] python [python-dev-bounces+kristjan=ccpgames.com [at] python] fyrir h&#246;nd Amaury Forgeot d'Arc [amauryfa [at] gmail]
Sent: 21. júlí 2012 22:56
To: Antoine Pitrou
Cc: python-dev [at] python
Efni: Re: [Python-Dev] test_hashlib

2012/7/21 Antoine Pitrou <solipsis [at] pitrou>:
> Kristján Valur Jónsson <kristjan [at] ccpgames> wrote:
>>
>> The code will raise ValueError when int(1) is passed in, but the
>> unittests expect a TypeError.
>
> Well, if test_hashlib passes, surely your analysis is wrong, no?

In the normal case, yes:

>>> import hashlib
>>> hashlib.new(1)
TypeError: name must be a string

But if the _hashlib extension module is not available, the python
version is used and ValueError is raised:

>>> import sys
>>> sys.modules['_hashlib'] = None
>>> import hashlib
>>> hashlib.new(1)
ValueError: unsupported hash type 1

--
Amaury Forgeot d'Arc
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/kristjan%40ccpgames.com
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


rdmurray at bitdance

Jul 21, 2012, 7:10 PM

Post #6 of 7 (471 views)
Permalink
Re: test_hashlib [In reply to]

On Sun, 22 Jul 2012 01:05:35 -0000, =?iso-8859-1?Q?Kristj=E1n_Valur_J=F3nsson?= <kristjan [at] ccpgames> wrote:
> Indeed, shame on me for not mentioning this.
> I rarely have the full complement of externals available when I'm doing python work, and it struck me that this unitest was failing.
> I suppose it should be possible to write unittests that test more than one particular implementation.

It is indeed, and we have been moving the unit tests to do exactly
that for the cases where there is a Python implementation as well as
a C implementation. It seems like it would be appropriate to open an
issue for doing that for hashlib, in addition to one for fixing this
particular issue with the Python version.

--David
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


greg at krypto

Jul 21, 2012, 9:25 PM

Post #7 of 7 (473 views)
Permalink
Re: test_hashlib [In reply to]

Fixed. The TypeError in this nonsense never gonna work use case is now
consistent in 2.7, 3.2 and 3.3.

On Sat, Jul 21, 2012 at 7:10 PM, R. David Murray <rdmurray [at] bitdance>wrote:

> On Sun, 22 Jul 2012 01:05:35 -0000,
> =?iso-8859-1?Q?Kristj=E1n_Valur_J=F3nsson?= <kristjan [at] ccpgames> wrote:
> > Indeed, shame on me for not mentioning this.
> > I rarely have the full complement of externals available when I'm doing
> python work, and it struck me that this unitest was failing.
> > I suppose it should be possible to write unittests that test more than
> one particular implementation.
>
> It is indeed, and we have been moving the unit tests to do exactly
> that for the cases where there is a Python implementation as well as
> a C implementation. It seems like it would be appropriate to open an
> issue for doing that for hashlib, in addition to one for fixing this
> particular issue with the Python version.
>
> --David
> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/greg%40krypto.org
>

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.