unittest assertRaisesRegex bug?

Here's the code in question: class PsuedoFloat: def __init__(self, value): self.value = float(value) def __int__(self): return int(self.value) pi = PsuedoFloat(3.1415) self.assertRaisesRegex(TypeError, '%x format: an integer is required, not PsuedoFloat', '%x'.__mod__, pi), Here's the exception: ====================================================================== ERROR: test_formatting (test.test_unicode.UnicodeTest) ---------------------------------------------------------------------- TypeError: 'PsuedoFloat' object is not callable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/ethan/source/python/issue19995/Lib/test/test_unicode.py", line 1156, in test_formatting self.assertRaisesRegex(TypeError, '%c'.__mod__, pi), File "/home/ethan/source/python/issue19995/Lib/unittest/case.py", line 1235, in assertRaisesRegex return context.handle('assertRaisesRegex', callable_obj, args, kwargs) File "/home/ethan/source/python/issue19995/Lib/unittest/case.py", line 161, in handle callable_obj(*args, **kwargs) File "/home/ethan/source/python/issue19995/Lib/unittest/case.py", line 190, in __exit__ if not expected_regex.search(str(exc_value)): AttributeError: 'method-wrapper' object has no attribute 'search' ---------------------------------------------------------------------- At worst, I was expecting a difference in the TypeError exception message; I have no idea why `pi` is being called. From the docs: ---- http://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaise... ---- assertRaisesRegex(exception, regex, callable, *args, **kwds) Like assertRaises() but also tests that regex matches on the string representation of the raised exception. regex may be a regular expression object or a string containing a regular expression suitable for use by re.search(). Examples: self.assertRaisesRegex(ValueError, "invalid literal for.*XYZ'$", int, 'XYZ') -------------------------------------------------------------------------------------------- Am I correct in thinking this is a bug? -- ~Ethan~

On 03/19/2014 03:57 PM, Antoine Pitrou wrote:
A regex pattern can be a literal, yes? In which case exception -> TypeError regex -> '%x format: an integer is required, not PsuedoFloat' callable -> '%x'.__mod__ *args -> pi **kwargs -> None So, unless you can point to where I've gone wrong with the above (which is why I posted in the first place), I think we have a bug in unittest. Also: self.assertRaisesRegex(TypeError, '%x format: an integer is required, not float','%x'.__mod__, 3.14), self.assertRaisesRegex(TypeError, '%X format: an integer is required, not float','%X'.__mod__, 2.11), self.assertRaisesRegex(TypeError, '%o format: an integer is required, not float','%o'.__mod__, 1.79), these lines all work just fine. -- ~Ethan~

On Wed, Mar 19, 2014 at 4:13 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
What Antoine is trying to tell you is that the traceback you pasted shows this: File "/home/ethan/source/python/issue19995/Lib/test/test_unicode.py", line 1156, in test_formatting self.assertRaisesRegex(TypeError, '%c'.__mod__, pi), ... which is passing '%c'.__mod__ as the 'regex' argument. '%c'.__mod__ is a method of a builtin type, a 'method-wrapper' object, which is why you get the error you're getting: AttributeError: 'method-wrapper' object has no attribute 'search'. -- Thomas Wouters <thomas@python.org> Hi! I'm an email virus! Think twice before sending your email to help me spread!

On Wed, 19 Mar 2014 16:41:10 -0700, Thomas Wouters <thomas@python.org> wrote:
I think http://bugs.python.org/issue20145 is relevant here. --David

On 03/19/2014 03:57 PM, Antoine Pitrou wrote:
A regex pattern can be a literal, yes? In which case exception -> TypeError regex -> '%x format: an integer is required, not PsuedoFloat' callable -> '%x'.__mod__ *args -> pi **kwargs -> None So, unless you can point to where I've gone wrong with the above (which is why I posted in the first place), I think we have a bug in unittest. Also: self.assertRaisesRegex(TypeError, '%x format: an integer is required, not float','%x'.__mod__, 3.14), self.assertRaisesRegex(TypeError, '%X format: an integer is required, not float','%X'.__mod__, 2.11), self.assertRaisesRegex(TypeError, '%o format: an integer is required, not float','%o'.__mod__, 1.79), these lines all work just fine. -- ~Ethan~

On Wed, Mar 19, 2014 at 4:13 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
What Antoine is trying to tell you is that the traceback you pasted shows this: File "/home/ethan/source/python/issue19995/Lib/test/test_unicode.py", line 1156, in test_formatting self.assertRaisesRegex(TypeError, '%c'.__mod__, pi), ... which is passing '%c'.__mod__ as the 'regex' argument. '%c'.__mod__ is a method of a builtin type, a 'method-wrapper' object, which is why you get the error you're getting: AttributeError: 'method-wrapper' object has no attribute 'search'. -- Thomas Wouters <thomas@python.org> Hi! I'm an email virus! Think twice before sending your email to help me spread!

On Wed, 19 Mar 2014 16:41:10 -0700, Thomas Wouters <thomas@python.org> wrote:
I think http://bugs.python.org/issue20145 is relevant here. --David
participants (4)
-
Antoine Pitrou
-
Ethan Furman
-
R. David Murray
-
Thomas Wouters