Commit fc8db79f authored by Jason Madden's avatar Jason Madden

Refactor '-m gevent.monkey' tests for clarity.

parent 9a198e54
......@@ -86,12 +86,23 @@ def import_c_accel(globs, cname):
# or we're running from the C extension
return
import importlib
from gevent._compat import PURE_PYTHON
if PURE_PYTHON:
return
mod = importlib.import_module(cname)
import importlib
import warnings
with warnings.catch_warnings():
# Python 3.7 likes to produce
# "ImportWarning: can't resolve
# package from __spec__ or __package__, falling back on
# __name__ and __path__"
# when we load cython compiled files. This is probably a bug in
# Cython, but it doesn't seem to have any consequences, it's
# just annoying to see and can mess up our unittests.
warnings.simplefilter('ignore', ImportWarning)
mod = importlib.import_module(cname)
# By adopting the entire __dict__, we get a more accurate
# __file__ and module repr, plus we don't leak any imported
......
......@@ -153,8 +153,6 @@ affects what we see:
"""
from __future__ import print_function
import sys
_PYPY = hasattr(sys, 'pypy_version_info')
from copy import copy
from weakref import ref
......@@ -574,14 +572,20 @@ try:
# in different ways. In CPython and PyPy3, it must be wrapped with classmethod;
# in PyPy2, it must not. In either case, the args that get passed to
# it are stil wrong.
if _PYPY and sys.version_info[:2] < (3, 0):
local.__new__ = 'None'
except TypeError: # pragma: no cover
# Must be compiled
pass
else:
from gevent._compat import PYPY
from gevent._compat import PY2
if PYPY and PY2:
local.__new__ = __new__
else:
local.__new__ = classmethod(__new__)
except TypeError: # pragma: no cover
pass
finally:
del sys
del PYPY
del PY2
_init()
......
......@@ -772,7 +772,8 @@ def main():
# passing .pyc/.pyo files and packages with a __main__ and
# potentially even zip files. Previously we used exec, which only
# worked if we directly read a python source file.
runpy.run_path(sys.argv[0], run_name='__main__')
runpy.run_path(sys.argv[0],
run_name='__main__')
else:
print(script_help)
......
from __future__ import print_function
import socket
import sys
if sys.argv[1] == 'patched':
print('gevent' in repr(socket.socket))
else:
assert sys.argv[1] == 'stdlib'
print('gevent' not in repr(socket.socket))
print(__file__)
if sys.version_info[:2] == (2, 7):
print(__package__ == None)
else:
if sys.argv[1] == 'patched':
# __package__ is handled differently, for some reason,
# and runpy doesn't let us override it. When we call it,
# it becomes ''
print(__package__ == '')
else:
# but the interpreter sets it to None
print(__package__ == None)
# -*- coding: utf-8 -*-
"""
Test script file, to be used directly as a file.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# We need some global imports
from textwrap import dedent
def use_import():
return dedent(" text")
if __name__ == '__main__':
print(__file__)
print(__name__)
print(use_import())
import sys
if 'gevent' not in sys.modules:
from subprocess import Popen
args = [sys.executable, '-m', 'gevent.monkey', __file__]
p = Popen(args)
code = p.wait()
assert code == 0, code
else:
import socket
assert 'gevent' in repr(socket.socket), repr(socket.socket)
assert __file__ == 'test__issue302monkey.py', repr(__file__)
assert __package__ is None, __package__
import sys
import unittest
if 'gevent' not in sys.modules:
from subprocess import Popen
from subprocess import PIPE
# Run a simple script
args = [sys.executable, '-m', 'gevent.monkey', __file__]
p = Popen(args)
code = p.wait()
assert code == 0, code
# Run a __main__ inside a package.
args = [sys.executable, '-m', 'gevent.monkey', 'monkey_package']
p = Popen(args, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
lines = out.splitlines()
assert lines[0].endswith(b'__main__.py'), (out, err)
assert lines[1] == b'__main__', (lines, out, err)
# Python 3.7 tends to produce some inscrutable
# warning from importlib._bootstrap.py on stderr
# "ImportWarning: can't resolve package from __spec__ or __package__".
# So we don't check that err is empty.
else:
from textwrap import dedent
def use_import():
dedent(" text")
use_import()
from subprocess import Popen
from subprocess import PIPE
class TestRun(unittest.TestCase):
def _run(self, script):
args = [sys.executable, '-m', 'gevent.monkey', script, 'patched']
p = Popen(args, stdout=PIPE, stderr=PIPE)
gout, gerr = p.communicate()
self.assertEqual(0, p.returncode, (gout, gerr))
args = [sys.executable, script, 'stdlib']
p = Popen(args, stdout=PIPE, stderr=PIPE)
pout, perr = p.communicate()
self.assertEqual(0, p.returncode, (pout, perr))
glines = gout.decode("utf-8").splitlines()
plines = pout.decode('utf-8').splitlines()
self.assertEqual(glines, plines)
self.assertEqual(gerr, perr)
return glines, gerr
def test_run_simple(self):
import os.path
self._run(os.path.join('monkey_package', 'script.py'))
def test_run_package(self):
# Run a __main__ inside a package.
lines, _ = self._run('monkey_package')
self.assertTrue(lines[0].endswith('__main__.py'), lines[0])
self.assertEqual(lines[1], '__main__')
def test_issue_302(self):
import os
lines, _ = self._run(os.path.join('monkey_package', 'issue302monkey.py'))
self.assertEqual(lines[0], 'True')
lines[1] = lines[1].replace('\\', '/') # windows path
self.assertEqual(lines[1], 'monkey_package/issue302monkey.py')
self.assertEqual(lines[2], 'True', lines)
if __name__ == '__main__':
unittest.main()
test___monkey_patching.py
test__monkey_ssl_warning.py
test___monitor.py
test__monkey_scope.py
......@@ -18,3 +18,4 @@ test___ident.py
test___config.py
test___monitor.py
test__events.py
test__monkey_scope.py
......@@ -79,7 +79,6 @@ test__hub.py
test__import_blocking_in_greenlet.py
test__import_wait.py
test__issue230.py
test__issue302monkey.py
test__issue330.py
test__issue467.py
test__issue6.py
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment