classification
Title: mock's autospec's behavior on method-bound builtin functions is broken
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Aaron Gallagher, berker.peksag, csabella, michael.foord
Priority: normal Keywords: patch

Created on 2017-01-31 23:01 by Aaron Gallagher, last changed 2017-07-21 23:27 by berker.peksag. This issue is now closed.

Files
File name Uploaded Description Edit
mock.patch Aaron Gallagher, 2017-01-31 23:08 review
Pull Requests
URL Status Linked Edit
PR 3 merged Aaron Gallagher, 2017-02-22 04:58
PR 2807 merged berker.peksag, 2017-07-21 22:12
PR 2809 merged berker.peksag, 2017-07-21 23:11
Messages (7)
msg286565 - (view) Author: Aaron Gallagher (Aaron Gallagher) * Date: 2017-01-31 23:01
Cython will, in the right circumstances, offer a MethodType instance where im_func is a builtin function. Any instance of MethodType is automatically assumed to be a python-defined function (more specifically, a function that has an inspectable signature), but _set_signature was still conservative in its assumptions. As a result _set_signature would return early with None instead of a mock since the im_func had no inspectable signature. This causes problems deeper inside mock, as _set_signature is assumed to always return a mock, and nothing checked its return value.

In similar corner cases, autospec will simply not check the spec of the function, so _set_signature is amended to now return early with the original, not-wrapped mock object.

There is another None early return in _set_signature, but it seems.. harder to trigger? It seems more appropriate to raise an exception there if this is being fixed.
msg286566 - (view) Author: Aaron Gallagher (Aaron Gallagher) * Date: 2017-01-31 23:08
Patch adapted from https://github.com/testing-cabal/mock/pull/389
msg295249 - (view) Author: Cheryl Sabella (csabella) * Date: 2017-06-06 11:16
This PR appears to have been approved, perhaps pending input from michael.foord?
msg298696 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-07-20 00:01
New changeset 856cbcc12f2e4cca93af5dc7ed6bcea4dd942f10 by Berker Peksag (Aaron Gallagher) in branch 'master':
bpo-29403: Fix mock's broken autospec behavior on method-bound builtin functions (GH-3)
https://github.com/python/cpython/commit/856cbcc12f2e4cca93af5dc7ed6bcea4dd942f10
msg298827 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-07-21 22:31
New changeset 64b9a15886840df422c5203fad25c9801b4cf61e by Berker Peksag in branch '3.6':
[3.6] bpo-29403: Fix mock's broken autospec behavior on method-bound builtin functions (GH-3)
https://github.com/python/cpython/commit/64b9a15886840df422c5203fad25c9801b4cf61e
msg298828 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-07-21 23:27
New changeset e78dc0aaf03cd98373910150c2d35418cf938254 by Berker Peksag in branch '3.5':
[3.5] bpo-29403: Fix mock's broken autospec behavior on method-bound builtin functions (GH-3)
https://github.com/python/cpython/commit/e78dc0aaf03cd98373910150c2d35418cf938254
msg298829 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-07-21 23:27
Thank you for the PR, Aaron!
History
Date User Action Args
2017-07-21 23:27:59berker.peksagsetstatus: open -> closed
resolution: fixed
messages: + msg298829

stage: backport needed -> resolved
2017-07-21 23:27:19berker.peksagsetmessages: + msg298828
2017-07-21 23:11:26berker.peksagsetpull_requests: + pull_request2860
2017-07-21 22:31:06berker.peksagsetmessages: + msg298827
2017-07-21 22:12:01berker.peksagsetpull_requests: + pull_request2858
2017-07-20 00:23:06berker.peksagsetstage: patch review -> backport needed
2017-07-20 00:01:16berker.peksagsetmessages: + msg298696
2017-06-06 11:16:56csabellasetnosy: + csabella
messages: + msg295249
2017-02-22 04:58:30Aaron Gallaghersetpull_requests: + pull_request193
2017-02-13 20:54:25berker.peksagsetnosy: + michael.foord, berker.peksag
stage: patch review

versions: - Python 2.7, Python 3.3, Python 3.4
2017-01-31 23:08:03Aaron Gallaghersetfiles: + mock.patch
keywords: + patch
messages: + msg286566
2017-01-31 23:01:55Aaron Gallaghercreate