Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-29403: Fix mock's broken autospec behavior on method-bound builtin functions #3

Merged

Conversation

habnabit
Copy link
Contributor

http://bugs.python.org/issue29403

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.

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA. This is necessary for legal reasons before we can look at your contribution. Please follow these steps to help rectify the issue:

  1. If you don't have an account on b.p.o, please create one
  2. Make sure your GitHub username is listed in "Your Details" at b.p.o
  3. If you have not already done so, please sign the PSF contributor agreement
  4. If you just signed the CLA, please wait at least a day and then check "Your Details" on bugs.python.org to see if your account has been marked as having signed the CLA (the delay is due to a person having to manually check your signed CLA)
  5. Reply here saying you have completed the above steps

Thanks again to your contribution and we look forward to looking at it!

@habnabit
Copy link
Contributor Author

I signed the CLA when I opened the bug on bpo last week, but my account doesn't say received still: http://bugs.python.org/user25445

@ShalbafZadeh
Copy link

@habnabit
have you added you github username to your bpo account?
i can't see it
https://bugs.python.org/user6636

@habnabit
Copy link
Contributor Author

@ShalbafZadeh that was an old account; I made a new one last week from a newer openid provider. http://bugs.python.org/user25445 is the new one, which I'm logged into now.

@ShalbafZadeh
Copy link

@habnabit seems odd

@habnabit habnabit force-pushed the fix-mock-builtin-methods-for-cython branch from c8bc9c9 to 9148028 Compare February 11, 2017 01:47
@habnabit
Copy link
Contributor Author

Rebased and pushed, including a test fix. The third-party mock used six, and the stdlib does not include six.

@habnabit habnabit force-pushed the fix-mock-builtin-methods-for-cython branch 2 times, most recently from e12a088 to 97bb733 Compare February 11, 2017 06:25
@habnabit
Copy link
Contributor Author

Rebased again!

@codecov
Copy link

codecov bot commented Feb 11, 2017

Codecov Report

Merging #3 into master will increase coverage by <.01%.

@@            Coverage Diff             @@
##           master       #3      +/-   ##
==========================================
+ Coverage   82.37%   82.37%   +<.01%     
==========================================
  Files        1427     1427              
  Lines      350948   350959      +11     
==========================================
+ Hits       289088   289104      +16     
+ Misses      61860    61855       -5

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e7ffb99...97bb733. Read the comment docs.

Copy link
Member

@berkerpeksag berkerpeksag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't had a time to dive into the whole life cycle of _set_signature() yet, but from a quick glance it looks good to me.

@berkerpeksag
Copy link
Member

Please add an entry to Misc/NEWS. It should be in the "Library" section.

@habnabit habnabit force-pushed the fix-mock-builtin-methods-for-cython branch from 97bb733 to c5b3653 Compare February 23, 2017 22:40
@habnabit
Copy link
Contributor Author

Okay, rebased and added a NEWS entry.

@Yhg1s
Copy link
Member

Yhg1s commented May 22, 2017

This change looks good to me: returning None does not make sense in the one place where _set_signature is called, and I don't think there's a better solution here. If the code wants to try harder and figure out what signature to use, that would be a change to _get_signature, not _set_signature, and this change would still make sense.

@voidspace, do you want to take a look, or can we merge?

@buhtz
Copy link

buhtz commented Jul 19, 2017

Can we merge this?

habnabit and others added 2 commits July 20, 2017 02:33
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.
@berkerpeksag berkerpeksag force-pushed the fix-mock-builtin-methods-for-cython branch from 75ad0f4 to 989e99a Compare July 19, 2017 23:35
@berkerpeksag berkerpeksag merged commit 856cbcc into python:master Jul 20, 2017
thatbirdguythatuknownot referenced this pull request in thatbirdguythatuknownot/cpython Sep 14, 2021
thatbirdguythatuknownot referenced this pull request in thatbirdguythatuknownot/cpython Sep 14, 2021
nanjekyejoannah added a commit to nanjekyejoannah/cpython that referenced this pull request Mar 30, 2022
3: Add warnings for numbers r=vext01 a=nanjekyejoannah

This has several changes, after git playing games with me, I managed to split the commits instead of committing one long one, as I had done before.

*First commit*: Adds warnings for when the suffix "L" is used with numbers but also adds a custom Exception for our work suggested in the review of python#2 , called `Py3xWarning`. I should have committed the exception separately though.
*Second commit*: Add warnings for octal literals. I fixed a test too

Co-authored-by: Joannah Nanjekye <jnanjekye@python.org>
gpshead added a commit that referenced this pull request Apr 14, 2022
Fix an uninitialized bool in exception print context.
    
`struct exception_print_context.need_close` was uninitialized.
    
Found by oss-fuzz in a test case running under the undefined behavior sanitizer.
    
https://oss-fuzz.com/testcase-detail/6217746058182656
    
```
Python/pythonrun.c:1241:28: runtime error: load of value 253, which is not a valid value for type 'bool'
    #0 0xbf2203 in print_chained cpython3/Python/pythonrun.c:1241:28
    #1 0xbea4bb in print_exception_cause_and_context cpython3/Python/pythonrun.c:1320:19
    #2 0xbea4bb in print_exception_recursive cpython3/Python/pythonrun.c:1470:13
    #3 0xbe9e39 in _PyErr_Display cpython3/Python/pythonrun.c:1517:9
```
    
Pretty obvious what the ommission was upon code inspection.
hello-adam pushed a commit to hello-adam/cpython that referenced this pull request Jun 2, 2022
jaraco pushed a commit that referenced this pull request Dec 2, 2022
nanjekyejoannah added a commit to nanjekyejoannah/cpython that referenced this pull request Jan 11, 2023
3: Add Py2x flag r=ltratt a=nanjekyejoannah

This PR adds the Py2x flag:

```
Python 3.12.0a0 (heads/migration-dirty:5842fee697, May 23 2022, 00:25:04) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.flags
sys.flags(debug=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0, ignore_environment=0, verbose=0, bytes_warning=0, py2x_warning=1, quiet=0, hash_randomization=1, isolated=0, dev_mode=False, utf8_mode=0, warn_default_encoding=0)
>>> 
```

Co-authored-by: Joannah Nanjekye <jnanjekye@python.org>
nanjekyejoannah added a commit to nanjekyejoannah/cpython that referenced this pull request Jan 11, 2023
5: Add 2.x related warnings r=ltratt a=nanjekyejoannah

I have broken away the warning bit from the [flag](python#3 ) and the [port ](python#4 )PR. Well, the way function calls are done between C and Python is confusing, nothing scary anyway, review maybe a bit annoying.

Review this PR before python#4 

Co-authored-by: Joannah Nanjekye <jnanjekye@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants