[issue22138] patch.object doesn't restore function defaults

2016-01-08 Thread Senthil Kumaran

Senthil Kumaran added the comment:

This was an interesting issue. Thanks for the patch, Sean to fix this bug. I 
have committed it in 3.5 and 3.6.

--
assignee:  -> orsenthil
nosy: +orsenthil
resolution:  -> fixed
stage: commit review -> resolved
status: open -> closed
versions: +Python 3.5, Python 3.6 -Python 3.4

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2016-01-08 Thread Roundup Robot

Roundup Robot added the comment:

New changeset b67ed559a7d3 by Senthil Kumaran in branch '3.5':
Issue #22138: Fix mock.patch behavior when patching descriptors. Restore
https://hg.python.org/cpython/rev/b67ed559a7d3

New changeset 9b21dfd71561 by Senthil Kumaran in branch 'default':
merge from 3.5
https://hg.python.org/cpython/rev/9b21dfd71561

--
nosy: +python-dev

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2015-12-01 Thread R. David Murray

Changes by R. David Murray :


--
stage: needs patch -> commit review

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2015-12-01 Thread Michael Foord

Michael Foord added the comment:

Sean's patch looks good to me.

--

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2015-12-01 Thread Xiadong Zhu

Xiadong Zhu added the comment:

How's the issue going on?

The situation to mock function's ``__defaults__`` attribute is general, as 
default argument is determinate after function definition, when we need to test 
a function such as:

def access_db(statement, backend=default_db_backend):
return default_db_backend.execute(statement)

that we must mock ``__defaults__`` attribute if we want to invoke it with 
default backend.

It has one year past, though I could patch the ``_patch`` class but it's dirty, 
is the issue a defect can be fixed or unsolvable?

--
nosy: +mailto1587

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2014-08-16 Thread STINNER Victor

Changes by STINNER Victor :


--
nosy: +haypo

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2014-08-10 Thread Sean McCully

Sean McCully added the comment:

So the changes submitted, take into the attributes that are part of the 
standard Python Data Model/Descriptors and defined as editable per 
documentation. 
https://docs.python.org/3/reference/datamodel.html

The thought is if a user is needing to support outside of Proxy Object 
(currently supported) and default descriptor attributes then mock should be 
special cased by user/developer. 

Please advise on current solution.

--

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2014-08-06 Thread Michael Foord

Michael Foord added the comment:

And yes, there's deliberate proxy object support in mock.patch (django settings 
being one specific use-case).

--

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2014-08-06 Thread Michael Foord

Michael Foord added the comment:

It might have to be. There's no general purpose solution that will fit every 
possible behaviour for a Python descriptor I'm afraid.

--

___
Python tracker 

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




[issue22138] patch.object doesn't restore function defaults

2014-08-06 Thread Ezio Melotti

Ezio Melotti added the comment:

Thanks for the patch, however I don't think this is a robust solution.
Other objects might have undeletable attributes too.

The current code can delete an attribute without restoring it so an easy 
solution would be removing the hasattr() check, but that seems to be there to 
deal with proxy objects, so doing that will probably break them 
(Lib/unittest/test/testmock/testpatch.py:821 seems to test proxy object).

--

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2014-08-06 Thread Sean McCully

Sean McCully added the comment:

Is special casing the special attrs a permament enough solution?

--
keywords: +patch
nosy: +seanmccully
Added file: http://bugs.python.org/file36286/issue22138.patch

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2014-08-04 Thread Ezio Melotti

Ezio Melotti added the comment:

The issue seems to affect special attributes that can't be deleted.
In Lib/unittest/mock.py:1329, patch() tried to delete the attribute, and then, 
if it doesn't exist, it restores the previous value.  However some special 
attributes exist even after they are deleted, but their initial value is lost:
>>> def foo(x:int=5, y:int=3): return x + y
... 
>>> foo.__defaults__
(5, 3)
>>> del foo.__defaults__
>>> foo.__defaults__
>>> foo.__annotations__
{'y': , 'x': }
>>> del foo.__annotations__
>>> foo.__annotations__
{}
>>> foo.__qualname__
'foo'
>>> del foo.__qualname__
TypeError: __qualname__ must be set to a string object

--
keywords: +easy
nosy: +ezio.melotti, michael.foord
stage:  -> needs patch

___
Python tracker 

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



[issue22138] patch.object doesn't restore function defaults

2014-08-04 Thread Clint Hepner

New submission from Clint Hepner:

Following a patch, a function's __defaults__ attribute is reset to None.

def foo(x=5):
return x

assert foo() == 5  # As expected
with unittest.mock.patch.object(foo, '__defaults__', (10,)):
assert foo() == 10  # As expected

assert foo() == 5  # Fails
assert foo.__defaults__ is None  # Succeeds

--
components: Library (Lib)
messages: 224801
nosy: chepner
priority: normal
severity: normal
status: open
title: patch.object doesn't restore function defaults
type: behavior
versions: Python 3.4

___
Python tracker 

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