Commit 9bef8a3b authored by Jason Madden's avatar Jason Madden Committed by GitHub

Merge pull request #1109 from gevent/issue1108

Be more careful about issuing the SSL warning on Py2.
parents 811837cb eadd6ba0
......@@ -77,6 +77,9 @@
- Update c-ares to 1.14.0. See :issue:`1105`.
- Be more careful about issuing a warning about patching SSL on
Python 2. See :issue:`1108`.
1.3a1 (2018-01-27)
==================
......
This diff is collapsed.
from subprocess import Popen
from gevent import monkey
monkey.patch_all()
import sys
import unittest
class TestMonkey(unittest.TestCase):
maxDiff = None
import time
assert 'built-in' not in repr(time.sleep), repr(time.sleep)
def test_time(self):
import time
from gevent import time as gtime
self.assertIs(time.sleep, gtime.sleep)
try:
def test_thread(self):
try:
import thread
except ImportError:
except ImportError:
import _thread as thread
import threading
assert 'built-in' not in repr(thread.start_new_thread), repr(thread.start_new_thread)
assert 'built-in' not in repr(threading._start_new_thread), repr(threading._start_new_thread)
if sys.version_info[0] == 2:
assert 'built-in' not in repr(threading._sleep), repr(threading._sleep)
import socket
from gevent import socket as gevent_socket
assert socket.create_connection is gevent_socket.create_connection
import os
import types
for name in ('fork', 'forkpty'):
import threading
from gevent import thread as gthread
self.assertIs(thread.start_new_thread, gthread.start_new_thread)
self.assertIs(threading._start_new_thread, gthread.start_new_thread)
if sys.version_info[0] == 2:
from gevent import threading as gthreading
self.assertIs(threading._sleep, gthreading._sleep)
self.assertFalse(monkey.is_object_patched('threading', 'Event'))
monkey.patch_thread(Event=True)
self.assertTrue(monkey.is_object_patched('threading', 'Event'))
def test_socket(self):
import socket
from gevent import socket as gevent_socket
self.assertIs(socket.create_connection, gevent_socket.create_connection)
def test_os(self):
import os
import types
from gevent import os as gos
for name in ('fork', 'forkpty'):
if hasattr(os, name):
attr = getattr(os, name)
assert 'built-in' not in repr(attr), repr(attr)
assert not isinstance(attr, types.BuiltinFunctionType), repr(attr)
assert isinstance(attr, types.FunctionType), repr(attr)
self.assertIs(attr, getattr(gos, name))
assert monkey.saved
def test_saved(self):
self.assertTrue(monkey.saved)
for modname in monkey.saved:
self.assertTrue(monkey.is_module_patched(modname))
assert not monkey.is_object_patched('threading', 'Event')
monkey.patch_thread(Event=True)
assert monkey.is_object_patched('threading', 'Event')
for objname in monkey.saved[modname]:
self.assertTrue(monkey.is_object_patched(modname, objname))
for modname in monkey.saved:
assert monkey.is_module_patched(modname)
def test_patch_subprocess_twice(self):
self.assertNotIn('gevent', repr(Popen))
self.assertIs(Popen, monkey.get_original('subprocess', 'Popen'))
monkey.patch_subprocess()
self.assertIs(Popen, monkey.get_original('subprocess', 'Popen'))
for objname in monkey.saved[modname]:
assert monkey.is_object_patched(modname, objname)
def test_patch_twice(self):
import warnings
orig_saved = {}
for k, v in monkey.saved.items():
orig_saved = {}
for k, v in monkey.saved.items():
orig_saved[k] = v.copy()
import warnings
with warnings.catch_warnings(record=True) as issued_warnings:
with warnings.catch_warnings(record=True) as issued_warnings:
# Patch again, triggering three warnings, one for os=False/signal=True,
# one for repeated monkey-patching, one for patching after ssl (on python >= 2.7.9)
monkey.patch_all(os=False)
assert len(issued_warnings) >= 2, [str(x) for x in issued_warnings]
assert 'SIGCHLD' in str(issued_warnings[-1].message), issued_warnings[-1]
assert 'more than once' in str(issued_warnings[0].message), issued_warnings[0]
self.assertGreaterEqual(len(issued_warnings), 2)
self.assertIn('SIGCHLD', str(issued_warnings[-1].message))
self.assertIn('more than once', str(issued_warnings[0].message))
# Patching with the exact same argument doesn't issue a second warning.
# in fact, it doesn't do anything
......@@ -59,13 +88,20 @@ with warnings.catch_warnings(record=True) as issued_warnings:
monkey.patch_all(os=False)
orig_saved['_gevent_saved_patch_all'] = monkey.saved['_gevent_saved_patch_all']
assert not issued_warnings, [str(x) for x in issued_warnings]
self.assertFalse(issued_warnings)
# Make sure that re-patching did not change the monkey.saved
# attribute, overwriting the original functions.
if 'logging' in monkey.saved and 'logging' not in orig_saved:
# some part of the warning or unittest machinery imports logging
orig_saved['logging'] = monkey.saved['logging']
self.assertEqual(orig_saved, monkey.saved)
# Make sure some problematic attributes stayed correct.
# NOTE: This was only a problem if threading was not previously imported.
for k, v in monkey.saved['threading'].items():
self.assertNotIn('gevent', str(v))
# Make sure that re-patching did not change the monkey.saved
# attribute, overwriting the original functions.
assert orig_saved == monkey.saved, (orig_saved, monkey.saved)
# Make sure some problematic attributes stayed correct.
# NOTE: This was only a problem if threading was not previously imported.
for k, v in monkey.saved['threading'].items():
assert 'gevent' not in str(v), (k, v)
if __name__ == '__main__':
unittest.main()
import unittest
import warnings
# This file should only have this one test in it
# because we have to be careful about our imports
# and because we need to be careful about our patching.
class Test(unittest.TestCase):
def test_with_pkg_resources(self):
# Issue 1108: Python 2, importing pkg_resources,
# as is done for namespace packages, imports ssl,
# leading to an unwanted SSL warning.
__import__('pkg_resources')
from gevent import monkey
self.assertFalse(monkey.saved)
with warnings.catch_warnings(record=True) as issued_warnings:
warnings.simplefilter('always')
monkey.patch_all()
monkey.patch_all()
issued_warnings = [x for x in issued_warnings
if isinstance(x.message, monkey.MonkeyPatchWarning)]
self.assertFalse(issued_warnings, [str(i) for i in issued_warnings])
self.assertEqual(0, len(issued_warnings))
if __name__ == '__main__':
unittest.main()
test___monkey_patching.py
test__monkey_ssl_warning.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