[issue38757] mocking an exception, arguments do not seem to be passed to the mock

2019-11-11 Thread Chris Withers


Chris Withers  added the comment:

Agreed.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38757] mocking an exception, arguments do not seem to be passed to the mock

2019-11-10 Thread Mario Corchero


Mario Corchero  added the comment:

The reason why it seems that "no arguments are beeing passed" is because the 
exception is not being raised by you, but by mock itself when the exception is 
trying to be created. When an exception type is passed as side_effect, the mock 
modules raises such exception on the callable (the creation of the initial 
exception) To confirm this, just try removing the "raise" and leave the 
creation of the exception only.

I'd suggest that you use `wraps` rather than `side_effect`. That will make your 
example work.

Alternatively, if you need to use `side_effect`, you can use a wrapper so mock 
won't raise an exception when it sees that one:

```

def wrapper(*args, **kwargs):
return MockError(*args, **kwargs)

patcher = patch('__main__.ProductionError', side_effect=wrapper)

```


I think this can be closed as a non-issue.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38757] mocking an exception, arguments do not seem to be passed to the mock

2019-11-09 Thread Chris Withers

Chris Withers  added the comment:

Not sure this is correct, if an effect is an exception and requires args, then 
it should be passed as an instance, not a class:

Mock(side_effect=MyException(‘foo’))

> On 10 Nov 2019, at 04:49, Karthikeyan Singaravelan  
> wrote:
> 
> 
> Karthikeyan Singaravelan  added the comment:
> 
> Currently, the exception is not instantiated. Maybe we can check if it's 
> callable and pass args, kwargs to the exception constructor to be raised.
> 
> diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
> index a48132c5b1..f5bcb911f5 100644
> --- a/Lib/unittest/mock.py
> +++ b/Lib/unittest/mock.py
> @@ -1145,7 +1145,10 @@ class CallableMixin(Base):
> effect = self.side_effect
> if effect is not None:
> if _is_exception(effect):
> -raise effect
> +if _callable(effect):
> +raise effect(*args, **kwargs)
> +else:
> +raise effect
> elif not _callable(effect):
> result = next(effect)
> if _is_exception(result):
> 
> --
> nosy: +cjw296, lisroach, mariocj89, michael.foord
> 
> ___
> Python tracker 
> 
> ___

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38757] mocking an exception, arguments do not seem to be passed to the mock

2019-11-09 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Currently, the exception is not instantiated. Maybe we can check if it's 
callable and pass args, kwargs to the exception constructor to be raised.

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index a48132c5b1..f5bcb911f5 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1145,7 +1145,10 @@ class CallableMixin(Base):
 effect = self.side_effect
 if effect is not None:
 if _is_exception(effect):
-raise effect
+if _callable(effect):
+raise effect(*args, **kwargs)
+else:
+raise effect
 elif not _callable(effect):
 result = next(effect)
 if _is_exception(result):

--
nosy: +cjw296, lisroach, mariocj89, michael.foord

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38757] mocking an exception, arguments do not seem to be passed to the mock

2019-11-09 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38757] mocking an exception, arguments do not seem to be passed to the mock

2019-11-09 Thread Troulet-lambert Odile


New submission from Troulet-lambert Odile :

When patching an Exception with a side_effect to another exception, it seems 
like the exceiption arguments are not passed to the mock.

--
components: Tests
files: essai mock_exception.py
messages: 356305
nosy: piscvau
priority: normal
severity: normal
status: open
title: mocking an exception, arguments do not seem to be passed to the mock
type: behavior
versions: Python 3.8
Added file: https://bugs.python.org/file48704/essai mock_exception.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com