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

mock.patch with py2 and p3 #1598

Closed
2 tasks done
Hellowlol opened this issue Jun 8, 2016 · 4 comments
Closed
2 tasks done

mock.patch with py2 and p3 #1598

Hellowlol opened this issue Jun 8, 2016 · 4 comments

Comments

@Hellowlol
Copy link

Hellowlol commented Jun 8, 2016

Thanks for submitting an issue!

Here's a quick checklist in what to include:

  • Include a detailed description of the bug or suggestion
    Im wondering if there is a bug in pytest. I get a exception when i try to rename input or raw input depending on the python version.
  • pip list of the virtual environment you are using
    py3
aiohttp (0.21.5)
apipkg (1.4)
chardet (2.3.0)
colorama (0.3.7)
coverage (4.1)
curio (0.1)
enum-compat (0.0.2)
execnet (1.4.1)
mock (2.0.0)
pbr (1.10.0)
pip (8.1.2)
py (1.4.31)
pytest (2.9.2)
pytest-cov (2.2.1)
pytest-mock (1.1)
pytest-xdist (1.14)
requests (2.9.1)
setuptools (18.2)
six (1.10.0)
tqdm (3.8.0)
workerpool (0.9.4)

py2 list

addict (0.4.0)
addicted (0.0.5)
alabaster (0.7.7)
apipkg (1.4)
arrow (0.7.0)
Babel (2.2.0)
backports-abc (0.4)
backports.ssl-match-hostname (3.5.0.1)
beautifulsoup4 (4.4.1)
blitzdb (0.2.12)
bottle (0.12.9)
CacheControl (0.11.6)
certifi (2016.2.28)
cffi (1.6.0)
Cheetah (2.4.4)
colorama (0.3.7)
colorconsole (0.7.2)
CProfileV (1.0.7)
cryptography (1.3.2)
decorator (4.0.6)
doc2md (0.1.0)
docopt (0.6.2)
docutils (0.12)
dpath (1.4.0)
ecdsa (0.13)
enum-compat (0.0.2)
enum34 (1.1.6)
execnet (1.4.1)
FormEncode (1.3.0)
funcsigs (1.0.2)
functools32 (3.2.3.post2)
future (0.15.2)
futures (3.0.3)
Glances (2.5.1)
html5lib (0.9999999)
idna (2.1)
imdbpie (4.0.2)
IMDbPY (5.0)
ipaddress (1.0.16)
ipykernel (4.3.1)
ipython (4.1.2)
ipython-genutils (0.1.0)
ipywidgets (4.1.1)
iso8601 (0.1.11)
Jinja2 (2.8)
jsonpath-rw (1.4.0)
jsonrpclib-pelix (0.2.6)
jsonschema (2.5.1)
jupyter (1.0.0)
jupyter-client (4.2.1)
jupyter-console (4.1.1)
jupyter-core (4.1.0)
lifter (0.2.1)
line-profiler (1.0)
lockfile (0.12.2)
lxml (3.5.0)
m3u8 (0.2.8)
Markdown (2.6.5)
MarkupSafe (0.23)
mistune (0.7.2)
mock (2.0.0)
nbconvert (4.1.0)
nbformat (4.0.1)
netdisco (0.6.4)
netifaces (0.10.4)
noattr (0.0.6)
notebook (4.1.0)
oauthlib (1.0.3)
paramiko (2.0.0)
path.py (8.1.2)
pbr (1.8.1)
pickleshare (0.6)
Pillow (3.1.0)
pip (8.1.2)
pipreqs (0.4.2)
PlexAPI (2.0.0a-..................................-.version.of.this.api)
ply (3.8)
pockets (0.3)
profilehooks (1.8.1)
psutil (3.4.1)
py (1.4.31)
pyasn1 (0.1.9)
pycparser (2.14)
pycrypto (2.6.1)
Pygments (2.1)
pyment (0.3.2.dev0)
Pympler (0.4.2)
pyOpenSSL (0.15.1)
pypiwin32 (219)
pyreadline (2.1)
pytest (2.9.2)
pytest-mock (1.1)
pytest-xdist (1.14)
python-dateutil (2.4.2)
python-qbittorrent (0.1.5)
pytz (2015.7)
PyYAML (3.11)
pyzmq (15.2.0)
qtconsole (4.2.0)
records (0.4.0)
requests (2.9.1)
requests-futures (0.9.5)
requirements-txt (0.0.0)
selenium (2.53.0)
setuptools (22.0.0)
simplegeneric (0.8.1)
simplejson (3.8.1)
singledispatch (3.4.0.3)
six (1.10.0)
snowballstemmer (1.2.1)
Sphinx (1.3.5)
sphinx-rtd-theme (0.1.9)
SQLAlchemy (1.0.11)
sqlalchemy-migrate (0.10.0)
SQLObject (2.1.2)
sqlparse (0.1.18)
tablib (0.11.1)
tellcore-py (1.1.2)
tellive-py (0.5.2)
Tempita (0.5.2)
tornado (4.3)
tqdm (4.4.0)
traitlets (4.1.0)
tvdb-api (1.10)
ujson (1.35)
wheel (0.29.0)
workerpool (0.9.4)
xmltodict (0.10.1)
yappi (0.94)
yarg (0.1.9)
youtube-dl (2016.3.6)
zeroconf (0.17.5)
  • [x ] Minimal example if possible

Test script to recreate the error

from __future__ import print_function

import mock
import sys

if sys.version_info >= (3, 0):
    ips = 'builtins.input'
    #input = input
else:
    ips = '__builtin__.raw_input'
    input = raw_input


def something():
    print("test")
    x = input('\nEnter stuff\n')
    print("s")
    return x


def test_something():
    with mock.patch(ips, return_value='::'):
        assert something() == '::'

py2 with raw_input renamed to input:

============================= test session starts =============================
platform win32 -- Python 2.7.10, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: C:\Users\admin\Desktop, inifile: 
plugins: mock-1.1, xdist-1.14
collected 0 items / 1 errors

=================================== ERRORS ====================================
_________________________ ERROR collecting tt_zomg.py _________________________
tt_zomg.py:26: in <module>
    test_something()
tt_zomg.py:22: in test_something
    f = something()
tt_zomg.py:15: in something
    x = input('\nEnter stuff\n')
c:\python27\lib\site-packages\_pytest\capture.py:433: in read
    raise IOError("reading from stdin while output is captured")
E   IOError: reading from stdin while output is captured
------------------------------- Captured stdout -------------------------------
test

Enter stuff
=========================== 1 error in 0.02 seconds ===========================

py3 with input

============================= test session starts =============================
platform win32 -- Python 3.5.1, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: C:\Users\admin\Desktop, inifile: 
plugins: cov-2.2.1, mock-1.1, xdist-1.14
collected 1 items

test_zomg.py .

========================== 1 passed in 0.03 seconds ===========================
@nicoddemus
Copy link
Member

nicoddemus commented Jun 8, 2016

Thanks for reporting.

I assume if you pass -s (--capture=no) then the test works, is that right? Currently there's no way to disable capture for a test (there's been some discussion in #167 though).

Either way I think mocking builtins like open and input are a bad idea in general, as third-party libraries (like pytest) depend on them to work.

@Hellowlol
Copy link
Author

Hellowlol commented Jun 8, 2016

I wouldnt say it works :P I mean i dont get any errors but i have to manually write in the cli inputs so its kinda useless. I have simply disabled he cli tests for python3 as a work around. Ill find another way to solve this

I only rename do i can support both python 2 and 3 in my script.

Thanks for your work on py test. Its awesome

@nicoddemus
Copy link
Member

I wouldnt say it works :P I mean i dont get any errors but i have to manually write in the cli inputs so its kinda useless.

I see.

If you want a workaround: in situations where I want to mock a builtin and that's a problem for some reason, I usually write a thin wrapper and use that in my code:

def obtain_input(text):
    return input(text)

def delete_file():    
    response = obtain_input('continue?')
    ...

This way you can easily mock the thin wrapper instead on the tests themselves:

@pytest.yield_fixture
def fake_input():
    with mock.patch('module.obtain_input') as m:    
        yield m

def test_delete_file(fake_input):
    fake_input.return_value = 'y'
    module.delete_file()
    assert module.file_exists()

Just an idea. 😁

@Hellowlol
Copy link
Author

Hellowlol commented Jun 8, 2016

Thanks for the suggestion. I ended up with replacing the renamed input/raw_input with

def compat_input(s=''):
    try:
        return raw_input(s)
    except NameError:
        return input(s)

Ill close this because of the issue you opened.

redshiftzero added a commit to freedomofpress/securedrop that referenced this issue Apr 8, 2019
(bandit will flag the use of input() in python 2 as a high severity
security issue otherwise)

Also adds a wrapper as a workaround for
pytest-dev/pytest#1598
redshiftzero added a commit to freedomofpress/securedrop that referenced this issue Apr 8, 2019
(bandit will flag the use of input() in python 2 as a high severity
security issue otherwise)

Also adds a wrapper as a workaround for
pytest-dev/pytest#1598
redshiftzero added a commit to freedomofpress/securedrop that referenced this issue Apr 9, 2019
(bandit will flag the use of input() in python 2 as a high severity
security issue otherwise)

Also adds a wrapper as a workaround for
pytest-dev/pytest#1598
redshiftzero added a commit to freedomofpress/securedrop that referenced this issue Apr 15, 2019
(bandit will flag the use of input() in python 2 as a high severity
security issue otherwise)

Also adds a wrapper as a workaround for
pytest-dev/pytest#1598
kushaldas pushed a commit to freedomofpress/securedrop that referenced this issue Sep 25, 2019
(bandit will flag the use of input() in python 2 as a high severity
security issue otherwise)

Also adds a wrapper as a workaround for
pytest-dev/pytest#1598
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

No branches or pull requests

2 participants