1# pylint:disable=missing-docstring,invalid-name,too-many-lines
2from __future__ import print_function, absolute_import, division
3
4import collections
5import contextlib
6import functools
7import sys
8import os
9# At least on 3.6+, importing platform
10# imports subprocess, which imports selectors. That
11# can expose issues with monkey patching. We don't need it
12# though.
13# import platform
14import re
15
16from .sysinfo import RUNNING_ON_APPVEYOR as APPVEYOR
17from .sysinfo import RUNNING_ON_TRAVIS as TRAVIS
18from .sysinfo import RESOLVER_NOT_SYSTEM as ARES
19from .sysinfo import RESOLVER_ARES
20from .sysinfo import RESOLVER_DNSPYTHON
21from .sysinfo import RUNNING_ON_CI
22from .sysinfo import RUN_COVERAGE
23
24
25from .sysinfo import PYPY
26from .sysinfo import PYPY3
27from .sysinfo import PY3
28from .sysinfo import PY2
29from .sysinfo import PY35
30from .sysinfo import PY36
31from .sysinfo import PY37
32from .sysinfo import PY38
33from .sysinfo import PY39
34from .sysinfo import PY310
35
36from .sysinfo import WIN
37from .sysinfo import OSX
38
39from .sysinfo import LIBUV
40from .sysinfo import CFFI_BACKEND
41
42from . import flaky
43
44CPYTHON = not PYPY
45
46# By default, test cases are expected to switch and emit warnings if there was none
47# If a test is found in this list, it's expected not to switch.
48no_switch_tests = '''test_patched_select.SelectTestCase.test_error_conditions
49test_patched_ftplib.*.test_all_errors
50test_patched_ftplib.*.test_getwelcome
51test_patched_ftplib.*.test_sanitize
52test_patched_ftplib.*.test_set_pasv
53#test_patched_ftplib.TestIPv6Environment.test_af
54test_patched_socket.TestExceptions.testExceptionTree
55test_patched_socket.Urllib2FileobjectTest.testClose
56test_patched_socket.TestLinuxAbstractNamespace.testLinuxAbstractNamespace
57test_patched_socket.TestLinuxAbstractNamespace.testMaxName
58test_patched_socket.TestLinuxAbstractNamespace.testNameOverflow
59test_patched_socket.FileObjectInterruptedTestCase.*
60test_patched_urllib.*
61test_patched_asyncore.HelperFunctionTests.*
62test_patched_httplib.BasicTest.*
63test_patched_httplib.HTTPSTimeoutTest.test_attributes
64test_patched_httplib.HeaderTests.*
65test_patched_httplib.OfflineTest.*
66test_patched_httplib.HTTPSTimeoutTest.test_host_port
67test_patched_httplib.SourceAddressTest.testHTTPSConnectionSourceAddress
68test_patched_select.SelectTestCase.test_error_conditions
69test_patched_smtplib.NonConnectingTests.*
70test_patched_urllib2net.OtherNetworkTests.*
71test_patched_wsgiref.*
72test_patched_subprocess.HelperFunctionTests.*
73'''
74
75ignore_switch_tests = '''
76test_patched_socket.GeneralModuleTests.*
77test_patched_httpservers.BaseHTTPRequestHandlerTestCase.*
78test_patched_queue.*
79test_patched_signal.SiginterruptTest.*
80test_patched_urllib2.*
81test_patched_ssl.*
82test_patched_signal.BasicSignalTests.*
83test_patched_threading_local.*
84test_patched_threading.*
85'''
86
87
88def make_re(tests):
89    tests = [x.strip().replace(r'\.', r'\\.').replace('*', '.*?')
90             for x in tests.split('\n') if x.strip()]
91    return re.compile('^%s$' % '|'.join(tests))
92
93
94no_switch_tests = make_re(no_switch_tests)
95ignore_switch_tests = make_re(ignore_switch_tests)
96
97
98def get_switch_expected(fullname):
99    """
100    >>> get_switch_expected('test_patched_select.SelectTestCase.test_error_conditions')
101    False
102    >>> get_switch_expected('test_patched_socket.GeneralModuleTests.testCrucialConstants')
103    False
104    >>> get_switch_expected('test_patched_socket.SomeOtherTest.testHello')
105    True
106    >>> get_switch_expected("test_patched_httplib.BasicTest.test_bad_status_repr")
107    False
108    """
109    # certain pylint versions mistype the globals as
110    # str, not re.
111    # pylint:disable=no-member
112    if ignore_switch_tests.match(fullname) is not None:
113        return None
114    if no_switch_tests.match(fullname) is not None:
115        return False
116    return True
117
118
119disabled_tests = [
120    # The server side takes awhile to shut down
121    'test_httplib.HTTPSTest.test_local_bad_hostname',
122    # These were previously 3.5+ issues (same as above)
123    # but have been backported.
124    'test_httplib.HTTPSTest.test_local_good_hostname',
125    'test_httplib.HTTPSTest.test_local_unknown_cert',
126
127
128    'test_threading.ThreadTests.test_PyThreadState_SetAsyncExc',
129    # uses some internal C API of threads not available when threads are emulated with greenlets
130
131    'test_threading.ThreadTests.test_join_nondaemon_on_shutdown',
132    # asserts that repr(sleep) is '<built-in function sleep>'
133
134    'test_urllib2net.TimeoutTest.test_ftp_no_timeout',
135    'test_urllib2net.TimeoutTest.test_ftp_timeout',
136    'test_urllib2net.TimeoutTest.test_http_no_timeout',
137    'test_urllib2net.TimeoutTest.test_http_timeout',
138    # accesses _sock.gettimeout() which is always in non-blocking mode
139
140    'test_urllib2net.OtherNetworkTests.test_ftp',
141    # too slow
142
143    'test_urllib2net.OtherNetworkTests.test_urlwithfrag',
144    # fails dues to some changes on python.org
145
146    'test_urllib2net.OtherNetworkTests.test_sites_no_connection_close',
147    # flaky
148
149    'test_socket.UDPTimeoutTest.testUDPTimeout',
150    # has a bug which makes it fail with error: (107, 'Transport endpoint is not connected')
151    # (it creates a TCP socket, not UDP)
152
153    'test_socket.GeneralModuleTests.testRefCountGetNameInfo',
154    # fails with "socket.getnameinfo loses a reference" while the reference is only "lost"
155    # because it is referenced by the traceback - any Python function would lose a reference like that.
156    # the original getnameinfo does not "lose" it because it's in C.
157
158    'test_socket.NetworkConnectionNoServer.test_create_connection_timeout',
159    # replaces socket.socket with MockSocket and then calls create_connection.
160    # this unfortunately does not work with monkey patching, because gevent.socket.create_connection
161    # is bound to gevent.socket.socket and updating socket.socket does not affect it.
162    # this issues also manifests itself when not monkey patching DNS: http://code.google.com/p/gevent/issues/detail?id=54
163    # create_connection still uses gevent.socket.getaddrinfo while it should be using socket.getaddrinfo
164
165    'test_asyncore.BaseTestAPI.test_handle_expt',
166    # sends some OOB data and expect it to be detected as such; gevent.select.select does not support that
167
168    # This one likes to check its own filename, but we rewrite
169    # the file to a temp location during patching.
170    'test_asyncore.HelperFunctionTests.test_compact_traceback',
171
172    # expects time.sleep() to return prematurely in case of a signal;
173    # gevent.sleep() is better than that and does not get interrupted
174    # (unless signal handler raises an error)
175    'test_signal.WakeupSignalTests.test_wakeup_fd_early',
176
177    # expects select.select() to raise select.error(EINTR'interrupted
178    # system call') gevent.select.select() does not get interrupted
179    # (unless signal handler raises an error) maybe it should?
180    'test_signal.WakeupSignalTests.test_wakeup_fd_during',
181
182    'test_signal.SiginterruptTest.test_without_siginterrupt',
183    'test_signal.SiginterruptTest.test_siginterrupt_on',
184    # these rely on os.read raising EINTR which never happens with gevent.os.read
185
186    'test_subprocess.ProcessTestCase.test_leak_fast_process_del_killed',
187    'test_subprocess.ProcessTestCase.test_zombie_fast_process_del',
188    # relies on subprocess._active which we don't use
189
190    # Very slow, tries to open lots and lots of subprocess and files,
191    # tends to timeout on CI.
192    'test_subprocess.ProcessTestCase.test_no_leaking',
193
194    # This test is also very slow, and has been timing out on Travis
195    # since November of 2016 on Python 3, but now also seen on Python 2/Pypy.
196    'test_subprocess.ProcessTestCase.test_leaking_fds_on_error',
197
198    # Added between 3.6.0 and 3.6.3, uses _testcapi and internals
199    # of the subprocess module. Backported to Python 2.7.16.
200    'test_subprocess.POSIXProcessTestCase.test_stopped',
201
202    'test_ssl.ThreadedTests.test_default_ciphers',
203    'test_ssl.ThreadedTests.test_empty_cert',
204    'test_ssl.ThreadedTests.test_malformed_cert',
205    'test_ssl.ThreadedTests.test_malformed_key',
206    'test_ssl.NetworkedTests.test_non_blocking_connect_ex',
207    # XXX needs investigating
208
209    'test_ssl.NetworkedTests.test_algorithms',
210    # The host this wants to use, sha256.tbs-internet.com, is not resolvable
211    # right now (2015-10-10), and we need to get Windows wheels
212
213    # This started timing out randomly on Travis in oct/nov 2018. It appears
214    # to be something with random number generation taking too long.
215    'test_ssl.BasicSocketTests.test_random_fork',
216
217    # Relies on the repr of objects (Py3)
218    'test_ssl.BasicSocketTests.test_dealloc_warn',
219
220    'test_urllib2.HandlerTests.test_cookie_redirect',
221    # this uses cookielib which we don't care about
222
223    'test_thread.ThreadRunningTests.test__count',
224    'test_thread.TestForkInThread.test_forkinthread',
225    # XXX needs investigating
226
227    'test_subprocess.POSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipes',
228    # Does not exist in the test suite until 2.7.4+. Subclasses Popen, and overrides
229    # _execute_child. But our version has a different parameter list than the
230    # version that comes with PyPy/CPython, so fails with a TypeError.
231
232    # This one crashes the interpreter if it has a bug parsing the
233    # invalid data.
234    'test_ssl.BasicSocketTests.test_parse_cert_CVE_2019_5010',
235    # We had to copy in a newer version of the test file for SSL fixes
236    # and this doesn't work reliably on all versions.
237    'test_httplib.HeaderTests.test_headers_debuglevel',
238
239    # On Appveyor with Python 3.8.0 and 3.7.5, this test
240    # for __class_getitem__ fails. Presumably this was added
241    # in a patch release (it's not in the PEP.) Sigh.
242    # https://bugs.python.org/issue38979
243    'test_context.ContextTest.test_contextvar_getitem',
244    # The same patch that fixed that removed this test,
245    # because it would now fail.
246    'test_context.ContextTest.test_context_var_new_2',
247]
248
249
250if sys.version_info[:3] < (2, 7, 18):
251    # The final release was 2.7.18. It added some new tests for new
252    # fixes. At this writing, AppVeyor is still on 2.7.17.
253    disabled_tests += [
254        'test_urllib2.MiscTests.test_url_host_with_control_char_rejected',
255    ]
256
257if OSX:
258    disabled_tests += [
259        # These are timing dependent, and sometimes run into the OS X
260        # kernel bug leading to 'Protocol wrong type for socket'.
261        # See discussion at https://github.com/benoitc/gunicorn/issues/1487
262        'test_ssl.SimpleBackgroundTests.test_connect_capath',
263        'test_ssl.SimpleBackgroundTests.test_connect_with_context',
264    ]
265
266
267if 'thread' in os.getenv('GEVENT_FILE', ''):
268    disabled_tests += [
269        'test_subprocess.ProcessTestCase.test_double_close_on_error'
270        # Fails with "OSError: 9 invalid file descriptor"; expect GC/lifetime issues
271    ]
272
273if PY2 and PYPY:
274    disabled_tests += [
275        # These appear to hang or take a long time for some reason?
276        # Likely a hostname/binding issue or failure to properly close/gc sockets.
277        'test_httpservers.BaseHTTPServerTestCase.test_head_via_send_error',
278        'test_httpservers.BaseHTTPServerTestCase.test_head_keep_alive',
279        'test_httpservers.BaseHTTPServerTestCase.test_send_blank',
280        'test_httpservers.BaseHTTPServerTestCase.test_send_error',
281        'test_httpservers.BaseHTTPServerTestCase.test_command',
282        'test_httpservers.BaseHTTPServerTestCase.test_handler',
283        'test_httpservers.CGIHTTPServerTestcase.test_post',
284        'test_httpservers.CGIHTTPServerTestCase.test_query_with_continuous_slashes',
285        'test_httpservers.CGIHTTPServerTestCase.test_query_with_multiple_question_mark',
286        'test_httpservers.CGIHTTPServerTestCase.test_os_environ_is_not_altered',
287
288        # This one sometimes results on connection refused
289        'test_urllib2_localnet.TestUrlopen.test_info',
290        # Sometimes hangs
291        'test_ssl.ThreadedTests.test_socketserver',
292        # We had to update 'CERTFILE' to continue working, but
293        # this test hasn't been updated yet (the CPython tests
294        # are also too new to run on PyPy).
295        'test_ssl.BasicSocketTests.test_parse_cert',
296
297    ]
298
299if PY2 and WIN:
300    disabled_tests += [
301        # This test randomly produces a 'LoopExit: Would block forever'
302        # on 'self.serv.accept()', but only on Windows with Python 2. Possibly
303        # due to the weird refcounting involving socket.makefile (just a guess)?
304        # Seen in both PyPy 7.3 and CPython 2.7.x
305        # https://ci.appveyor.com/project/denik/gevent/builds/36874106/job/guyq6h9k56n81uf6#L563
306        'test_socket.BasicTCPTest2.testDup',
307    ]
308
309if LIBUV:
310    # epoll appears to work with these just fine in some cases;
311    # kqueue (at least on OS X, the only tested kqueue system)
312    # never does (failing with abort())
313    # (epoll on Raspbian 8.0/Debian Jessie/Linux 4.1.20 works;
314    # on a VirtualBox image of Ubuntu 15.10/Linux 4.2.0 both tests fail;
315    # Travis CI Ubuntu 12.04 precise/Linux 3.13 causes one of these tests to hang forever)
316    # XXX: Retry this with libuv 1.12+
317    disabled_tests += [
318        # A 2.7 test. Tries to fork, and libuv cannot fork
319        'test_signal.InterProcessSignalTests.test_main',
320        # Likewise, a forking problem
321        'test_signal.SiginterruptTest.test_siginterrupt_off',
322    ]
323
324    if PY2:
325
326        if TRAVIS:
327
328            if CPYTHON:
329
330                disabled_tests += [
331                    # This appears to crash the process, for some reason,
332                    # but only on CPython 2.7.14 on Travis. Cannot reproduce in
333                    # 2.7.14 on macOS or 2.7.12 in local Ubuntu 16.04
334                    'test_subprocess.POSIXProcessTestCase.test_close_fd_0',
335                    'test_subprocess.POSIXProcessTestCase.test_close_fds_0_1',
336                    'test_subprocess.POSIXProcessTestCase.test_close_fds_0_2',
337                ]
338
339            if PYPY:
340                disabled_tests += [
341                    # This seems to crash the interpreter. I cannot reproduce
342                    # on macOS or local Linux VM.
343                    # See https://travis-ci.org/gevent/gevent/jobs/348661604#L709
344                    'test_smtplib.TooLongLineTests.testLineTooLong',
345                ]
346                if ARES:
347
348                    disabled_tests += [
349                        # This can timeout with a socket timeout in ssl.wrap_socket(c)
350                        # on Travis. I can't reproduce locally.
351                        'test_ssl.ThreadedTests.test_handshake_timeout',
352                    ]
353
354    if PY3:
355
356        disabled_tests += [
357            # This test wants to pass an arbitrary fileno
358            # to a socket and do things with it. libuv doesn't like this,
359            # it raises EPERM. It is disabled on windows already.
360            # It depends on whether we had a fd already open and multiplexed with
361            'test_socket.GeneralModuleTests.test_unknown_socket_family_repr',
362            # And yes, there's a typo in some versions.
363            'test_socket.GeneralModuleTests.test_uknown_socket_family_repr',
364        ]
365
366        if PY37:
367
368            disabled_tests += [
369                # This test sometimes fails at line 358. It's apparently
370                # extremely sensitive to timing.
371                'test_selectors.PollSelectorTestCase.test_timeout',
372            ]
373
374        if OSX:
375            disabled_tests += [
376                # XXX: Starting when we upgraded from libuv 1.18.0
377                # to 1.19.2, this sometimes (usually) started having
378                # a series of calls ('select.poll(0)', 'select.poll(-1)')
379                # take longer than the allowed 0.5 seconds. Debugging showed that
380                # it was the second call that took longer, for no apparent reason.
381                # There doesn't seem to be a change in the source code to libuv that
382                # would affect this.
383                # XXX-XXX: This actually disables too many tests :(
384                'test_selectors.PollSelectorTestCase.test_timeout',
385            ]
386
387        if RUN_COVERAGE:
388
389            disabled_tests += [
390                # Starting with #1145 this test (actually
391                # TestTLS_FTPClassMixin) becomes sensitive to timings
392                # under coverage.
393                'test_ftplib.TestFTPClass.test_storlines',
394            ]
395
396
397    if sys.platform.startswith('linux'):
398        disabled_tests += [
399            # crashes with EPERM, which aborts the epoll loop, even
400            # though it was allowed in in the first place.
401            'test_asyncore.FileWrapperTest.test_dispatcher',
402        ]
403
404
405
406    if WIN and PY2:
407        # From PyPy2-v5.9.0 and CPython 2.7.14, using its version of tests,
408        # which do work on darwin (and possibly linux?)
409        # I can't produce them in a local VM running Windows 10
410        # and the same pypy version.
411        disabled_tests += [
412            # These, which use asyncore, fail with
413            # 'NoneType is not iterable' on 'conn, addr = self.accept()'
414            # That returns None when the underlying socket raises
415            # EWOULDBLOCK, which it will do because it's set to non-blocking
416            # both by gevent and by libuv (at the level below python's knowledge)
417            # I can *usually* reproduce these locally; it seems to be some sort
418            # of race condition.
419            'test_ftplib.TestFTPClass.test_acct',
420            'test_ftplib.TestFTPClass.test_all_errors',
421            'test_ftplib.TestFTPClass.test_cwd',
422            'test_ftplib.TestFTPClass.test_delete',
423            'test_ftplib.TestFTPClass.test_dir',
424            'test_ftplib.TestFTPClass.test_exceptions',
425            'test_ftplib.TestFTPClass.test_getwelcome',
426            'test_ftplib.TestFTPClass.test_line_too_long',
427            'test_ftplib.TestFTPClass.test_login',
428            'test_ftplib.TestFTPClass.test_makepasv',
429            'test_ftplib.TestFTPClass.test_mkd',
430            'test_ftplib.TestFTPClass.test_nlst',
431            'test_ftplib.TestFTPClass.test_pwd',
432            'test_ftplib.TestFTPClass.test_quit',
433            'test_ftplib.TestFTPClass.test_makepasv',
434            'test_ftplib.TestFTPClass.test_rename',
435            'test_ftplib.TestFTPClass.test_retrbinary',
436            'test_ftplib.TestFTPClass.test_retrbinary_rest',
437            'test_ftplib.TestFTPClass.test_retrlines',
438            'test_ftplib.TestFTPClass.test_retrlines_too_long',
439            'test_ftplib.TestFTPClass.test_rmd',
440            'test_ftplib.TestFTPClass.test_sanitize',
441            'test_ftplib.TestFTPClass.test_set_pasv',
442            'test_ftplib.TestFTPClass.test_size',
443            'test_ftplib.TestFTPClass.test_storbinary',
444            'test_ftplib.TestFTPClass.test_storbinary_rest',
445            'test_ftplib.TestFTPClass.test_storlines',
446            'test_ftplib.TestFTPClass.test_storlines_too_long',
447            'test_ftplib.TestFTPClass.test_voidcmd',
448            'test_ftplib.TestTLS_FTPClass.test_data_connection',
449            'test_ftplib.TestTLS_FTPClass.test_control_connection',
450            'test_ftplib.TestTLS_FTPClass.test_context',
451            'test_ftplib.TestTLS_FTPClass.test_check_hostname',
452            'test_ftplib.TestTLS_FTPClass.test_auth_ssl',
453            'test_ftplib.TestTLS_FTPClass.test_auth_issued_twice',
454
455            # This one times out, but it's still a non-blocking socket
456            'test_ftplib.TestFTPClass.test_makeport',
457
458            # A timeout, possibly because of the way we handle interrupts?
459            'test_socketserver.SocketServerTest.test_InterruptedServerSelectCall',
460            'test_socketserver.SocketServerTest.test_InterruptServerSelectCall',
461
462            # times out with something about threading?
463            # The apparent hang is just after the print of "waiting for server"
464            'test_socketserver.SocketServerTest.test_ThreadingTCPServer',
465            'test_socketserver.SocketServerTest.test_ThreadingUDPServer',
466            'test_socketserver.SocketServerTest.test_TCPServer',
467            'test_socketserver.SocketServerTest.test_UDPServer',
468
469            # This one might be like  'test_urllib2_localnet.TestUrlopen.test_https_with_cafile'?
470            # XXX: Look at newer pypy and verify our usage of drop/reuse matches
471            # theirs.
472            'test_httpservers.BaseHTTPServerTestCase.test_command',
473            'test_httpservers.BaseHTTPServerTestCase.test_handler',
474            'test_httpservers.BaseHTTPServerTestCase.test_head_keep_alive',
475            'test_httpservers.BaseHTTPServerTestCase.test_head_via_send_error',
476            'test_httpservers.BaseHTTPServerTestCase.test_header_close',
477            'test_httpservers.BaseHTTPServerTestCase.test_internal_key_error',
478            'test_httpservers.BaseHTTPServerTestCase.test_request_line_trimming',
479            'test_httpservers.BaseHTTPServerTestCase.test_return_custom_status',
480            'test_httpservers.BaseHTTPServerTestCase.test_send_blank',
481            'test_httpservers.BaseHTTPServerTestCase.test_send_error',
482            'test_httpservers.BaseHTTPServerTestCase.test_version_bogus',
483            'test_httpservers.BaseHTTPServerTestCase.test_version_digits',
484            'test_httpservers.BaseHTTPServerTestCase.test_version_invalid',
485            'test_httpservers.BaseHTTPServerTestCase.test_version_none',
486            'test_httpservers.SimpleHTTPServerTestCase.test_get',
487            'test_httpservers.SimpleHTTPServerTestCase.test_head',
488            'test_httpservers.SimpleHTTPServerTestCase.test_invalid_requests',
489            'test_httpservers.SimpleHTTPServerTestCase.test_path_without_leading_slash',
490            'test_httpservers.CGIHTTPServerTestCase.test_invaliduri',
491            'test_httpservers.CGIHTTPServerTestCase.test_issue19435',
492
493            # Unexpected timeouts sometimes
494            'test_smtplib.TooLongLineTests.testLineTooLong',
495            'test_smtplib.GeneralTests.testTimeoutValue',
496
497            # This sometimes crashes, which can't be our fault?
498            'test_ssl.BasicSocketTests.test_parse_cert_CVE_2019_5010',
499
500        ]
501
502        if PYPY:
503            disabled_tests += [
504                # appears to timeout?
505                'test_threading.ThreadTests.test_finalize_with_trace',
506                'test_asyncore.DispatcherWithSendTests_UsePoll.test_send',
507                'test_asyncore.DispatcherWithSendTests.test_send',
508
509                # More unexpected timeouts
510                'test_ssl.ContextTests.test__https_verify_envvar',
511                'test_subprocess.ProcessTestCase.test_check_output',
512                'test_telnetlib.ReadTests.test_read_eager_A',
513
514                # But on Windows, our gc fix for that doesn't work anyway
515                # so we have to disable it.
516                'test_urllib2_localnet.TestUrlopen.test_https_with_cafile',
517
518                # These tests hang. see above.
519                'test_threading.ThreadJoinOnShutdown.test_1_join_on_shutdown',
520                'test_threading.ThreadingExceptionTests.test_print_exception',
521
522                # Our copy of these in test__subprocess.py also hangs.
523                # Anything that uses Popen.communicate or directly uses
524                # Popen.stdXXX.read hangs. It's not clear why.
525                'test_subprocess.ProcessTestCase.test_communicate',
526                'test_subprocess.ProcessTestCase.test_cwd',
527                'test_subprocess.ProcessTestCase.test_env',
528                'test_subprocess.ProcessTestCase.test_stderr_pipe',
529                'test_subprocess.ProcessTestCase.test_stdout_pipe',
530                'test_subprocess.ProcessTestCase.test_stdout_stderr_pipe',
531                'test_subprocess.ProcessTestCase.test_stderr_redirect_with_no_stdout_redirect',
532                'test_subprocess.ProcessTestCase.test_stdout_filedes_of_stdout',
533                'test_subprocess.ProcessTestcase.test_stdout_none',
534                'test_subprocess.ProcessTestcase.test_universal_newlines',
535                'test_subprocess.ProcessTestcase.test_writes_before_communicate',
536                'test_subprocess.Win32ProcessTestCase._kill_process',
537                'test_subprocess.Win32ProcessTestCase._kill_dead_process',
538                'test_subprocess.Win32ProcessTestCase.test_shell_sequence',
539                'test_subprocess.Win32ProcessTestCase.test_shell_string',
540                'test_subprocess.CommandsWithSpaces.with_spaces',
541            ]
542
543
544    if WIN:
545
546        disabled_tests += [
547            # This test winds up hanging a long time.
548            # Inserting GCs doesn't fix it.
549            'test_ssl.ThreadedTests.test_handshake_timeout',
550
551            # These sometimes raise LoopExit, for no apparent reason,
552            # mostly but not exclusively on Python 2. Sometimes (often?)
553            # this happens in the setUp() method when we attempt to get a client
554            # connection
555            'test_socket.BufferIOTest.testRecvFromIntoBytearray',
556            'test_socket.BufferIOTest.testRecvFromIntoArray',
557            'test_socket.BufferIOTest.testRecvIntoArray',
558            'test_socket.BufferIOTest.testRecvIntoMemoryview',
559            'test_socket.BufferIOTest.testRecvFromIntoEmptyBuffer',
560            'test_socket.BufferIOTest.testRecvFromIntoMemoryview',
561            'test_socket.BufferIOTest.testRecvFromIntoSmallBuffer',
562            'test_socket.BufferIOTest.testRecvIntoBytearray',
563        ]
564
565        if PY3:
566
567            disabled_tests += [
568            ]
569
570            if APPVEYOR:
571
572                disabled_tests += [
573                ]
574
575    if PYPY:
576
577        if TRAVIS:
578
579            disabled_tests += [
580                # This sometimes causes a segfault for no apparent reason.
581                # See https://travis-ci.org/gevent/gevent/jobs/327328704
582                # Can't reproduce locally.
583                'test_subprocess.ProcessTestCase.test_universal_newlines_communicate',
584            ]
585
586if RUN_COVERAGE and CFFI_BACKEND:
587    disabled_tests += [
588        # This test hangs in this combo for some reason
589        'test_socket.GeneralModuleTests.test_sendall_interrupted',
590        # This can get a timeout exception instead of the Alarm
591        'test_socket.TCPTimeoutTest.testInterruptedTimeout',
592
593        # This test sometimes gets the wrong answer (due to changed timing?)
594        'test_socketserver.SocketServerTest.test_ForkingUDPServer',
595
596        # Timing and signals are off, so a handler exception doesn't get raised.
597        # Seen under libev
598        'test_signal.InterProcessSignalTests.test_main',
599    ]
600
601if PY2:
602    if TRAVIS:
603        disabled_tests += [
604            # When we moved to group:travis_latest and dist:xenial,
605            # this started returning a value (33554432L) != 0; presumably
606            # because of updated SSL library? Only on CPython.
607            'test_ssl.ContextTests.test_options',
608            # When we moved to group:travis_latest and dist:xenial,
609            # one of the values used started *working* when it was expected to fail.
610            # The list of values and systems is long and complex, so
611            # presumably something needs to be updated. Only on PyPy.
612            'test_ssl.ThreadedTests.test_alpn_protocols',
613        ]
614
615    disabled_tests += [
616        # At least on OSX, this results in connection refused
617        'test_urllib2_localnet.TestUrlopen.test_https_sni',
618    ]
619
620    if sys.version_info[:3] < (2, 7, 16):
621        # We have 2.7.16 tests; older versions can fail
622        # to validate some SSL things or are missing important support functions
623        disabled_tests += [
624            # Support functions
625            'test_thread.ThreadRunningTests.test_nt_and_posix_stack_size',
626            'test_thread.ThreadRunningTests.test_save_exception_state_on_error',
627            'test_thread.ThreadRunningTests.test_starting_threads',
628            'test_thread.BarrierTest.test_barrier',
629            # Broken SSL
630            'test_urllib2_localnet.TestUrlopen.test_https',
631            'test_ssl.ContextTests.test__create_stdlib_context',
632            'test_ssl.ContextTests.test_create_default_context',
633            'test_ssl.ContextTests.test_options',
634        ]
635
636if PYPY and sys.pypy_version_info[:2] == (7, 3): # pylint:disable=no-member
637
638    if OSX:
639        disabled_tests += [
640            # This is expected to produce an SSLError, but instead it appears to
641            # actually work. See above for when it started failing the same on
642            # Travis.
643            'test_ssl.ThreadedTests.test_alpn_protocols',
644            # This fails, presumably due to the OpenSSL it's compiled with.
645            'test_ssl.ThreadedTests.test_default_ecdh_curve',
646        ]
647
648if PYPY3 and TRAVIS:
649    disabled_tests += [
650        # If socket.SOCK_CLOEXEC is defined, this creates a socket
651        # and tests its type with ``sock.type & socket.SOCK_CLOEXEC``
652        # We have a ``@property`` for ``type`` that takes care of
653        # ``SOCK_NONBLOCK`` on Linux, but otherwise it's just a pass-through.
654        # This started failing with PyPy 7.3.1 and it's not clear why.
655        'test_socket.InheritanceTest.test_SOCK_CLOEXEC',
656    ]
657
658def _make_run_with_original(mod_name, func_name):
659    @contextlib.contextmanager
660    def with_orig():
661        mod = __import__(mod_name)
662        now = getattr(mod, func_name)
663        from gevent.monkey import get_original
664        orig = get_original(mod_name, func_name)
665        try:
666            setattr(mod, func_name, orig)
667            yield
668        finally:
669            setattr(mod, func_name, now)
670    return with_orig
671
672@contextlib.contextmanager
673def _gc_at_end():
674    try:
675        yield
676    finally:
677        import gc
678        gc.collect()
679        gc.collect()
680
681@contextlib.contextmanager
682def _flaky_socket_timeout():
683    import socket
684    try:
685        yield
686    except socket.timeout:
687        flaky.reraiseFlakyTestTimeout()
688
689# Map from FQN to a context manager that will be wrapped around
690# that test.
691wrapped_tests = {
692}
693
694
695
696class _PatchedTest(object):
697    def __init__(self, test_fqn):
698        self._patcher = wrapped_tests[test_fqn]
699
700    def __call__(self, orig_test_fn):
701
702        @functools.wraps(orig_test_fn)
703        def test(*args, **kwargs):
704            with self._patcher():
705                return orig_test_fn(*args, **kwargs)
706        return test
707
708
709
710if sys.version_info[:3] <= (2, 7, 11):
711
712    disabled_tests += [
713        # These were added/fixed in 2.7.12+
714        'test_ssl.ThreadedTests.test__https_verify_certificates',
715        'test_ssl.ThreadedTests.test__https_verify_envvar',
716    ]
717
718if OSX:
719    disabled_tests += [
720        'test_subprocess.POSIXProcessTestCase.test_run_abort',
721        # causes Mac OS X to show "Python crashes" dialog box which is annoying
722    ]
723
724if WIN:
725    disabled_tests += [
726        # Issue with Unix vs DOS newlines in the file vs from the server
727        'test_ssl.ThreadedTests.test_socketserver',
728        # This sometimes hangs (only on appveyor)
729        'test_ssl.ThreadedTests.test_asyncore_server',
730        # On appveyor, this sometimes produces 'A non-blocking socket
731        # operation could not be completed immediately', followed by
732        # 'No connection could be made because the target machine
733        # actively refused it'
734        'test_socket.NonBlockingTCPTests.testAccept',
735    ]
736
737    # These are a problem on 3.5; on 3.6+ they wind up getting (accidentally) disabled.
738    wrapped_tests.update({
739        'test_socket.SendfileUsingSendTest.testWithTimeout': _flaky_socket_timeout,
740        'test_socket.SendfileUsingSendTest.testOffset': _flaky_socket_timeout,
741        'test_socket.SendfileUsingSendTest.testRegularFile': _flaky_socket_timeout,
742        'test_socket.SendfileUsingSendTest.testCount': _flaky_socket_timeout,
743    })
744
745if PYPY:
746    disabled_tests += [
747        # Does not exist in the CPython test suite, tests for a specific bug
748        # in PyPy's forking. Only runs on linux and is specific to the PyPy
749        # implementation of subprocess (possibly explains the extra parameter to
750        # _execut_child)
751        'test_subprocess.ProcessTestCase.test_failed_child_execute_fd_leak',
752        # On some platforms, this returns "zlib_compression", but the test is looking for
753        # "ZLIB"
754        'test_ssl.ThreadedTests.test_compression',
755
756        # These are flaxy, apparently a race condition? Began with PyPy 2.7-7 and 3.6-7
757        'test_asyncore.TestAPI_UsePoll.test_handle_error',
758        'test_asyncore.TestAPI_UsePoll.test_handle_read',
759    ]
760
761    if WIN:
762        disabled_tests += [
763            # Starting in 7.3.1 on Windows, this stopped raising ValueError; it appears to
764            # be a bug in PyPy.
765            'test_signal.WakeupFDTests.test_invalid_fd',
766            # Likewise for 7.3.1. See the comments for PY35
767            'test_socket.GeneralModuleTests.test_sock_ioctl',
768        ]
769
770    if PY36:
771        disabled_tests += [
772            # These are flaky, beginning in 3.6-alpha 7.0, not finding some flag
773            # set, apparently a race condition
774            'test_asyncore.TestAPI_UveIPv6Poll.test_handle_accept',
775            'test_asyncore.TestAPI_UveIPv6Poll.test_handle_accepted',
776            'test_asyncore.TestAPI_UveIPv6Poll.test_handle_close',
777            'test_asyncore.TestAPI_UveIPv6Poll.test_handle_write',
778
779            'test_asyncore.TestAPI_UseIPV6Select.test_handle_read',
780
781            # These are reporting 'ssl has no attribute ...'
782            # This could just be an OSX thing
783            'test_ssl.ContextTests.test__create_stdlib_context',
784            'test_ssl.ContextTests.test_create_default_context',
785            'test_ssl.ContextTests.test_get_ciphers',
786            'test_ssl.ContextTests.test_options',
787            'test_ssl.ContextTests.test_constants',
788
789            # These tend to hang for some reason, probably not properly
790            # closed sockets.
791            'test_socketserver.SocketServerTest.test_write',
792
793            # This uses ctypes to do funky things including using ptrace,
794            # it hangs
795            'test_subprocess.ProcessTestcase.test_child_terminated_in_stopped_state',
796
797            # Certificate errors; need updated test
798            'test_urllib2_localnet.TestUrlopen.test_https',
799        ]
800
801# Generic Python 3
802
803if PY3:
804
805    disabled_tests += [
806        # Triggers the crash reporter
807        'test_threading.SubinterpThreadingTests.test_daemon_threads_fatal_error',
808
809        # Relies on an implementation detail, Thread._tstate_lock
810        'test_threading.ThreadTests.test_tstate_lock',
811        # Relies on an implementation detail (reprs); we have our own version
812        'test_threading.ThreadTests.test_various_ops',
813        'test_threading.ThreadTests.test_various_ops_large_stack',
814        'test_threading.ThreadTests.test_various_ops_small_stack',
815
816        # Relies on Event having a _cond and an _reset_internal_locks()
817        # XXX: These are commented out in the source code of test_threading because
818        # this doesn't work.
819        # 'lock_tests.EventTests.test_reset_internal_locks',
820
821        # Python bug 13502. We may or may not suffer from this as its
822        # basically a timing race condition.
823        # XXX Same as above
824        # 'lock_tests.EventTests.test_set_and_clear',
825
826        # These tests want to assert on the type of the class that implements
827        # `Popen.stdin`; we use a FileObject, but they expect different subclasses
828        # from the `io` module
829        'test_subprocess.ProcessTestCase.test_io_buffered_by_default',
830        'test_subprocess.ProcessTestCase.test_io_unbuffered_works',
831
832        # 3.3 exposed the `endtime` argument to wait accidentally.
833        # It is documented as deprecated and not to be used since 3.4
834        # This test in 3.6.3 wants to use it though, and we don't have it.
835        'test_subprocess.ProcessTestCase.test_wait_endtime',
836
837        # These all want to inspect the string value of an exception raised
838        # by the exec() call in the child. The _posixsubprocess module arranges
839        # for better exception handling and printing than we do.
840        'test_subprocess.POSIXProcessTestCase.test_exception_bad_args_0',
841        'test_subprocess.POSIXProcessTestCase.test_exception_bad_executable',
842        'test_subprocess.POSIXProcessTestCase.test_exception_cwd',
843        # Relies on a 'fork_exec' attribute that we don't provide
844        'test_subprocess.POSIXProcessTestCase.test_exception_errpipe_bad_data',
845        'test_subprocess.POSIXProcessTestCase.test_exception_errpipe_normal',
846
847        # Python 3 fixed a bug if the stdio file descriptors were closed;
848        # we still have that bug
849        'test_subprocess.POSIXProcessTestCase.test_small_errpipe_write_fd',
850
851        # Relies on implementation details (some of these tests were added in 3.4,
852        # but PyPy3 is also shipping them.)
853        'test_socket.GeneralModuleTests.test_SocketType_is_socketobject',
854        'test_socket.GeneralModuleTests.test_dealloc_warn',
855        'test_socket.GeneralModuleTests.test_repr',
856        'test_socket.GeneralModuleTests.test_str_for_enums',
857        'test_socket.GeneralModuleTests.testGetaddrinfo',
858
859    ]
860    if TRAVIS:
861        disabled_tests += [
862            # test_cwd_with_relative_executable tends to fail
863            # on Travis...it looks like the test processes are stepping
864            # on each other and messing up their temp directories. We tend to get things like
865            #    saved_dir = os.getcwd()
866            #   FileNotFoundError: [Errno 2] No such file or directory
867            'test_subprocess.ProcessTestCase.test_cwd_with_relative_arg',
868            'test_subprocess.ProcessTestCaseNoPoll.test_cwd_with_relative_arg',
869            'test_subprocess.ProcessTestCase.test_cwd_with_relative_executable',
870
871            # In 3.7 and 3.8 on Travis CI, this appears to take the full 3 seconds.
872            # Can't reproduce it locally. We have our own copy of this that takes
873            # timing on CI into account.
874            'test_subprocess.RunFuncTestCase.test_run_with_shell_timeout_and_capture_output',
875        ]
876
877    disabled_tests += [
878        # XXX: BUG: We simply don't handle this correctly. On CPython,
879        # we wind up raising a BlockingIOError and then
880        # BrokenPipeError and then some random TypeErrors, all on the
881        # server. CPython 3.5 goes directly to socket.send() (via
882        # socket.makefile), whereas CPython 3.6 uses socket.sendall().
883        # On PyPy, the behaviour is much worse: we hang indefinitely, perhaps exposing a problem
884        # with our signal handling.
885
886        # In actuality, though, this test doesn't fully test the EINTR it expects
887        # to under gevent (because if its EWOULDBLOCK retry behaviour.)
888        # Instead, the failures were all due to `pthread_kill` trying to send a signal
889        # to a greenlet instead of a real thread. The solution is to deliver the signal
890        # to the real thread by letting it get the correct ID, and we previously
891        # used make_run_with_original to make it do that.
892        #
893        # But now that we have disabled our wrappers around Thread.join() in favor
894        # of the original implementation, that causes problems:
895        # background.join() thinks that it is the current thread, and won't let it
896        # be joined.
897        'test_wsgiref.IntegrationTests.test_interrupted_write',
898    ]
899
900# PyPy3 3.5.5 v5.8-beta
901
902if PYPY3:
903
904
905    disabled_tests += [
906        # This raises 'RuntimeError: reentrant call' when exiting the
907        # process tries to close the stdout stream; no other platform does this.
908        # Seen in both 3.3 and 3.5 (5.7 and 5.8)
909        'test_signal.SiginterruptTest.test_siginterrupt_off',
910    ]
911
912
913if PYPY and PY3:
914    disabled_tests += [
915        # This fails to close all the FDs, at least on CI. On OS X, many of the
916        # POSIXProcessTestCase fd tests have issues.
917        'test_subprocess.POSIXProcessTestCase.test_close_fds_when_max_fd_is_lowered',
918
919        # This has the wrong constants in 5.8 (but worked in 5.7), at least on
920        # OS X. It finds "zlib compression" but expects "ZLIB".
921        'test_ssl.ThreadedTests.test_compression',
922
923        # The below are new with 5.10.1
924        # This gets an EOF in violation of protocol; again, even without gevent
925        # (at least on OS X; it's less consistent about that on travis)
926        'test_ssl.NetworkedBIOTests.test_handshake',
927
928        # This passes various "invalid" strings and expects a ValueError. not sure why
929        # we don't see errors on CPython.
930        'test_subprocess.ProcessTestCase.test_invalid_env',
931    ]
932
933    if OSX:
934        disabled_tests += [
935            # These all fail with "invalid_literal for int() with base 10: b''"
936            'test_subprocess.POSIXProcessTestCase.test_close_fds',
937            'test_subprocess.POSIXProcessTestCase.test_close_fds_after_preexec',
938            'test_subprocess.POSIXProcessTestCase.test_pass_fds',
939            'test_subprocess.POSIXProcessTestCase.test_pass_fds_inheritable',
940            'test_subprocess.POSIXProcessTestCase.test_pipe_cloexec',
941
942
943            # The below are new with 5.10.1
944            # These fail with 'OSError: received malformed or improperly truncated ancillary data'
945            'test_socket.RecvmsgSCMRightsStreamTest.testCmsgTruncLen0',
946            'test_socket.RecvmsgSCMRightsStreamTest.testCmsgTruncLen0Plus1',
947            'test_socket.RecvmsgSCMRightsStreamTest.testCmsgTruncLen1',
948            'test_socket.RecvmsgSCMRightsStreamTest.testCmsgTruncLen2Minus1',
949
950            # Using the provided High Sierra binary, these fail with
951            # 'ValueError: invalid protocol version _SSLMethod.PROTOCOL_SSLv3'.
952            # gevent code isn't involved and running them unpatched has the same issue.
953            'test_ssl.ContextTests.test_constructor',
954            'test_ssl.ContextTests.test_protocol',
955            'test_ssl.ContextTests.test_session_stats',
956            'test_ssl.ThreadedTests.test_echo',
957            'test_ssl.ThreadedTests.test_protocol_sslv23',
958            'test_ssl.ThreadedTests.test_protocol_sslv3',
959            'test_ssl.ThreadedTests.test_protocol_tlsv1',
960            'test_ssl.ThreadedTests.test_protocol_tlsv1_1',
961            # Similar, they fail without monkey-patching.
962            'test_ssl.TestPostHandshakeAuth.test_pha_no_pha_client',
963            'test_ssl.TestPostHandshakeAuth.test_pha_optional',
964            'test_ssl.TestPostHandshakeAuth.test_pha_required',
965
966            # This gets None instead of http1.1, even without gevent
967            'test_ssl.ThreadedTests.test_npn_protocols',
968
969            # This fails to decode a filename even without gevent,
970            # at least on High Sierra. Newer versions of the tests actually skip this.
971            'test_httpservers.SimpleHTTPServerTestCase.test_undecodable_filename',
972        ]
973
974    disabled_tests += [
975        # This seems to be a buffering issue? Something isn't
976        # getting flushed. (The output is wrong). Under PyPy3 5.7,
977        # I couldn't reproduce locally in Ubuntu 16 in a VM
978        # or a laptop with OS X. Under 5.8.0, I can reproduce it, but only
979        # when run by the testrunner, not when run manually on the command line,
980        # so something is changing in stdout buffering in those situations.
981        'test_threading.ThreadJoinOnShutdown.test_2_join_in_forked_process',
982        'test_threading.ThreadJoinOnShutdown.test_1_join_in_forked_process',
983    ]
984
985    if TRAVIS:
986        disabled_tests += [
987            # Likewise, but I haven't produced it locally.
988            'test_threading.ThreadJoinOnShutdown.test_1_join_on_shutdown',
989        ]
990
991if PYPY:
992
993    wrapped_tests.update({
994        # XXX: gevent: The error that was raised by that last call
995        # left a socket open on the server or client. The server gets
996        # to http/server.py(390)handle_one_request and blocks on
997        # self.rfile.readline which apparently is where the SSL
998        # handshake is done. That results in the exception being
999        # raised on the client above, but apparently *not* on the
1000        # server. Consequently it sits trying to read from that
1001        # socket. On CPython, when the client socket goes out of scope
1002        # it is closed and the server raises an exception, closing the
1003        # socket. On PyPy, we need a GC cycle for that to happen.
1004        # Without the socket being closed and exception being raised,
1005        # the server cannot be stopped (it runs each request in the
1006        # same thread that would notice it had been stopped), and so
1007        # the cleanup method added by start_https_server to stop the
1008        # server blocks "forever".
1009
1010        # This is an important test, so rather than skip it in patched_tests_setup,
1011        # we do the gc before we return.
1012        'test_urllib2_localnet.TestUrlopen.test_https_with_cafile': _gc_at_end,
1013
1014        'test_httpservers.BaseHTTPServerTestCase.test_command': _gc_at_end,
1015        'test_httpservers.BaseHTTPServerTestCase.test_handler': _gc_at_end,
1016        'test_httpservers.BaseHTTPServerTestCase.test_head_keep_alive': _gc_at_end,
1017        'test_httpservers.BaseHTTPServerTestCase.test_head_via_send_error': _gc_at_end,
1018        'test_httpservers.BaseHTTPServerTestCase.test_header_close': _gc_at_end,
1019        'test_httpservers.BaseHTTPServerTestCase.test_internal_key_error': _gc_at_end,
1020        'test_httpservers.BaseHTTPServerTestCase.test_request_line_trimming': _gc_at_end,
1021        'test_httpservers.BaseHTTPServerTestCase.test_return_custom_status': _gc_at_end,
1022        'test_httpservers.BaseHTTPServerTestCase.test_return_header_keep_alive': _gc_at_end,
1023        'test_httpservers.BaseHTTPServerTestCase.test_send_blank': _gc_at_end,
1024        'test_httpservers.BaseHTTPServerTestCase.test_send_error': _gc_at_end,
1025        'test_httpservers.BaseHTTPServerTestCase.test_version_bogus': _gc_at_end,
1026        'test_httpservers.BaseHTTPServerTestCase.test_version_digits': _gc_at_end,
1027        'test_httpservers.BaseHTTPServerTestCase.test_version_invalid': _gc_at_end,
1028        'test_httpservers.BaseHTTPServerTestCase.test_version_none': _gc_at_end,
1029        'test_httpservers.BaseHTTPServerTestCase.test_version_none_get': _gc_at_end,
1030        'test_httpservers.BaseHTTPServerTestCase.test_get': _gc_at_end,
1031        'test_httpservers.SimpleHTTPServerTestCase.test_get': _gc_at_end,
1032        'test_httpservers.SimpleHTTPServerTestCase.test_head': _gc_at_end,
1033        'test_httpservers.SimpleHTTPServerTestCase.test_invalid_requests': _gc_at_end,
1034        'test_httpservers.SimpleHTTPServerTestCase.test_path_without_leading_slash': _gc_at_end,
1035        'test_httpservers.CGIHTTPServerTestCase.test_invaliduri': _gc_at_end,
1036        'test_httpservers.CGIHTTPServerTestCase.test_issue19435': _gc_at_end,
1037
1038        'test_httplib.TunnelTests.test_connect': _gc_at_end,
1039        'test_httplib.SourceAddressTest.testHTTPConnectionSourceAddress': _gc_at_end,
1040
1041        # Unclear
1042        'test_urllib2_localnet.ProxyAuthTests.test_proxy_with_bad_password_raises_httperror': _gc_at_end,
1043        'test_urllib2_localnet.ProxyAuthTests.test_proxy_with_no_password_raises_httperror': _gc_at_end,
1044    })
1045
1046
1047if PY35:
1048    disabled_tests += [
1049        'test_subprocess.ProcessTestCase.test_threadsafe_wait',
1050        # XXX: It seems that threading.Timer is not being greened properly, possibly
1051        # due to a similar issue to what gevent.threading documents for normal threads.
1052        # In any event, this test hangs forever
1053
1054
1055        'test_subprocess.POSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipes',
1056        # Subclasses Popen, and overrides _execute_child. Expects things to be done
1057        # in a particular order in an exception case, but we don't follow that
1058        # exact order
1059
1060
1061        'test_selectors.PollSelectorTestCase.test_above_fd_setsize',
1062        # This test attempts to open many many file descriptors and
1063        # poll on them, expecting them all to be ready at once. But
1064        # libev limits the number of events it will return at once. Specifically,
1065        # on linux with epoll, it returns a max of 64 (ev_epoll.c).
1066
1067        # XXX: Hangs (Linux only)
1068        'test_socket.NonBlockingTCPTests.testInitNonBlocking',
1069        # We don't handle the Linux-only SOCK_NONBLOCK option
1070        'test_socket.NonblockConstantTest.test_SOCK_NONBLOCK',
1071
1072        # Tries to use multiprocessing which doesn't quite work in
1073        # monkey_test module (Windows only)
1074        'test_socket.TestSocketSharing.testShare',
1075
1076        # Windows-only: Sockets have a 'ioctl' method in Python 3
1077        # implemented in the C code. This test tries to check
1078        # for the presence of the method in the class, which we don't
1079        # have because we don't inherit the C implementation. But
1080        # it should be found at runtime.
1081        'test_socket.GeneralModuleTests.test_sock_ioctl',
1082
1083        # XXX This fails for an unknown reason
1084        'test_httplib.HeaderTests.test_parse_all_octets',
1085    ]
1086
1087    if OSX:
1088        disabled_tests += [
1089            # These raise "OSError: 12 Cannot allocate memory" on both
1090            # patched and unpatched runs
1091            'test_socket.RecvmsgSCMRightsStreamTest.testFDPassEmpty',
1092        ]
1093
1094        if TRAVIS:
1095            # This has been seen to produce "Inconsistency detected by
1096            # ld.so: dl-open.c: 231: dl_open_worker: Assertion
1097            # `_dl_debug_initialize (0, args->nsid)->r_state ==
1098            # RT_CONSISTENT' failed!" and fail.
1099            disabled_tests += [
1100                'test_threading.ThreadTests.test_is_alive_after_fork',
1101                # This has timing constraints that are strict and do not always
1102                # hold.
1103                'test_selectors.PollSelectorTestCase.test_timeout',
1104            ]
1105
1106    if TRAVIS:
1107        disabled_tests += [
1108            'test_subprocess.ProcessTestCase.test_double_close_on_error',
1109            # This test is racy or OS-dependent. It passes locally (sufficiently fast machine)
1110            # but fails under Travis
1111        ]
1112
1113if PY35:
1114    disabled_tests += [
1115        # XXX: Hangs
1116        'test_ssl.ThreadedTests.test_nonblocking_send',
1117        'test_ssl.ThreadedTests.test_socketserver',
1118        # Uses direct sendfile, doesn't properly check for it being enabled
1119        'test_socket.GeneralModuleTests.test__sendfile_use_sendfile',
1120
1121
1122        # Relies on the regex of the repr having the locked state (TODO: it'd be nice if
1123        # we did that).
1124        # XXX: These are commented out in the source code of test_threading because
1125        # this doesn't work.
1126        # 'lock_tests.LockTests.lest_locked_repr',
1127        # 'lock_tests.LockTests.lest_repr',
1128
1129
1130        # This test opens a socket, creates a new socket with the same fileno,
1131        # closes the original socket (and hence fileno) and then
1132        # expects that the calling setblocking() on the duplicate socket
1133        # will raise an error. Our implementation doesn't work that way because
1134        # setblocking() doesn't actually touch the file descriptor.
1135        # That's probably OK because this was a GIL state error in CPython
1136        # see https://github.com/python/cpython/commit/fa22b29960b4e683f4e5d7e308f674df2620473c
1137        'test_socket.TestExceptions.test_setblocking_invalidfd',
1138    ]
1139
1140    if sys.version_info[:2] == (3, 5):
1141        # These tests are broken now that certificates are
1142        # expired and Python 3.5 is out of maintenance.
1143        disabled_tests += [
1144            'test_ssl.ThreadedTests.test_crl_check',
1145            'test_ssl.BasicSocketTests.test_parse_cert',
1146        ]
1147
1148    if ARES:
1149        disabled_tests += [
1150            # These raise different errors or can't resolve
1151            # the IP address correctly
1152            'test_socket.GeneralModuleTests.test_host_resolution',
1153            'test_socket.GeneralModuleTests.test_getnameinfo',
1154        ]
1155
1156        if sys.version_info[1] == 5:
1157            disabled_tests += [
1158                # This test tends to time out, but only under 3.5, not under
1159                # 3.6 or 3.7. Seen with both libev and libuv
1160                'test_socket.SendfileUsingSendTest.testWithTimeoutTriggeredSend',
1161            ]
1162
1163if sys.version_info[:3] <= (3, 5, 1):
1164    # Python issue 26499 was fixed in 3.5.2 and these tests were added.
1165    disabled_tests += [
1166        'test_httplib.BasicTest.test_mixed_reads',
1167        'test_httplib.BasicTest.test_read1_bound_content_length',
1168        'test_httplib.BasicTest.test_read1_content_length',
1169        'test_httplib.BasicTest.test_readline_bound_content_length',
1170        'test_httplib.BasicTest.test_readlines_content_length',
1171    ]
1172
1173if PY36:
1174    disabled_tests += [
1175        'test_threading.MiscTestCase.test__all__',
1176    ]
1177
1178    # We don't actually implement socket._sendfile_use_sendfile,
1179    # so these tests, which think they're using that and os.sendfile,
1180    # fail.
1181    disabled_tests += [
1182        'test_socket.SendfileUsingSendfileTest.testCount',
1183        'test_socket.SendfileUsingSendfileTest.testCountSmall',
1184        'test_socket.SendfileUsingSendfileTest.testCountWithOffset',
1185        'test_socket.SendfileUsingSendfileTest.testOffset',
1186        'test_socket.SendfileUsingSendfileTest.testRegularFile',
1187        'test_socket.SendfileUsingSendfileTest.testWithTimeout',
1188        'test_socket.SendfileUsingSendfileTest.testEmptyFileSend',
1189        'test_socket.SendfileUsingSendfileTest.testNonBlocking',
1190        'test_socket.SendfileUsingSendfileTest.test_errors',
1191    ]
1192
1193    # Ditto
1194    disabled_tests += [
1195        'test_socket.GeneralModuleTests.test__sendfile_use_sendfile',
1196    ]
1197
1198    disabled_tests += [
1199        # This test requires Linux >= 4.3. When we were running 'dist:
1200        # trusty' on the 4.4 kernel, it passed (~July 2017). But when
1201        # trusty became the default dist in September 2017 and updated
1202        # the kernel to 4.11.6, it begain failing. It fails on `res =
1203        # op.recv(assoclen + len(plain) + taglen)` (where 'op' is the
1204        # client socket) with 'OSError: [Errno 22] Invalid argument'
1205        # for unknown reasons. This is *after* having successfully
1206        # called `op.sendmsg_afalg`. Post 3.6.0, what we test with,
1207        # the test was changed to require Linux 4.9 and the data was changed,
1208        # so this is not our fault. We should eventually update this when we
1209        # update our 3.6 version.
1210        # See https://bugs.python.org/issue29324
1211        'test_socket.LinuxKernelCryptoAPI.test_aead_aes_gcm',
1212    ]
1213
1214if PY37:
1215    disabled_tests += [
1216        # These want to use the private '_communicate' method, which
1217        # our Popen doesn't have.
1218        'test_subprocess.MiscTests.test_call_keyboardinterrupt_no_kill',
1219        'test_subprocess.MiscTests.test_context_manager_keyboardinterrupt_no_kill',
1220        'test_subprocess.MiscTests.test_run_keyboardinterrupt_no_kill',
1221
1222        # This wants to check that the underlying fileno is blocking,
1223        # but it isn't.
1224        'test_socket.NonBlockingTCPTests.testSetBlocking',
1225
1226        # 3.7b2 made it impossible to instantiate SSLSocket objects
1227        # directly, and this tests for that, but we don't follow that change.
1228        'test_ssl.BasicSocketTests.test_private_init',
1229
1230        # 3.7b2 made a change to this test that on the surface looks incorrect,
1231        # but it passes when they run it and fails when we do. It's not
1232        # clear why.
1233        'test_ssl.ThreadedTests.test_check_hostname_idn',
1234
1235        # These appear to hang, haven't investigated why
1236        'test_ssl.SimpleBackgroundTests.test_get_server_certificate',
1237        # Probably the same as NetworkConnectionNoServer.test_create_connection_timeout
1238        'test_socket.NetworkConnectionNoServer.test_create_connection',
1239
1240        # Internals of the threading module that change.
1241        'test_threading.ThreadTests.test_finalization_shutdown',
1242        'test_threading.ThreadTests.test_shutdown_locks',
1243        # Expects a deprecation warning we don't raise
1244        'test_threading.ThreadTests.test_old_threading_api',
1245        # This tries to use threading.interrupt_main() from a new Thread;
1246        # but of course that's actually the same thread and things don't
1247        # work as expected.
1248        'test_threading.InterruptMainTests.test_interrupt_main_subthread',
1249        'test_threading.InterruptMainTests.test_interrupt_main_noerror',
1250
1251        # TLS1.3 seems flaky
1252        'test_ssl.ThreadedTests.test_wrong_cert_tls13',
1253    ]
1254
1255    if sys.version_info < (3, 7, 6):
1256        disabled_tests += [
1257            # Earlier versions parse differently so the newer test breaks
1258            'test_ssl.BasicSocketTests.test_parse_all_sans',
1259            'test_ssl.BasicSocketTests.test_parse_cert_CVE_2013_4238',
1260        ]
1261
1262    if APPVEYOR:
1263        disabled_tests += [
1264            # This sometimes produces ``self.assertEqual(1, len(s.select(0))): 1 != 0``.
1265            # Probably needs to spin the loop once.
1266            'test_selectors.BaseSelectorTestCase.test_timeout',
1267        ]
1268
1269if PY38:
1270    disabled_tests += [
1271        # This one seems very strict: doesn't want a pathlike
1272        # first argument when shell is true.
1273        'test_subprocess.RunFuncTestCase.test_run_with_pathlike_path',
1274        # This tests for a warning we don't raise.
1275        'test_subprocess.RunFuncTestCase.test_bufsize_equal_one_binary_mode',
1276
1277        # This compares the output of threading.excepthook with
1278        # data constructed in Python. But excepthook is implemented in C
1279        # and can't see the patched threading.get_ident() we use, so the
1280        # output doesn't match.
1281        'test_threading.ExceptHookTests.test_excepthook_thread_None',
1282    ]
1283
1284    if sys.version_info[:3] < (3, 8, 1):
1285        disabled_tests += [
1286            # Earlier versions parse differently so the newer test breaks
1287            'test_ssl.BasicSocketTests.test_parse_all_sans',
1288            'test_ssl.BasicSocketTests.test_parse_cert_CVE_2013_4238',
1289        ]
1290
1291    if sys.version_info[:3] < (3, 8, 10):
1292        disabled_tests += [
1293            # These were added for fixes sometime between 3.8.1 and 3.8.10
1294            'test_ftplib.TestFTPClass.test_makepasv_issue43285_security_disabled',
1295            'test_ftplib.TestFTPClass.test_makepasv_issue43285_security_enabled_default',
1296            'test_httplib.BasicTest.test_dir_with_added_behavior_on_status',
1297            'test_httplib.TunnelTests.test_tunnel_connect_single_send_connection_setup',
1298            'test_ssl.TestSSLDebug.test_msg_callback_deadlock_bpo43577',
1299            # This one fails with the updated certs
1300            'test_ssl.ContextTests.test_load_verify_cadata',
1301            # This one times out on 3.7.1 on Appveyor
1302            'test_ftplib.TestTLS_FTPClassMixin.test_retrbinary_rest',
1303        ]
1304
1305if RESOLVER_DNSPYTHON:
1306    disabled_tests += [
1307        # This does two things DNS python doesn't. First, it sends it
1308        # capital letters and expects them to be returned lowercase.
1309        # Second, it expects the symbolic scopeid to be stripped from the end.
1310        'test_socket.GeneralModuleTests.test_getaddrinfo_ipv6_scopeid_symbolic',
1311    ]
1312
1313# if 'signalfd' in os.environ.get('GEVENT_BACKEND', ''):
1314#     # tests that don't interact well with signalfd
1315#     disabled_tests.extend([
1316#         'test_signal.SiginterruptTest.test_siginterrupt_off',
1317#         'test_socketserver.SocketServerTest.test_ForkingTCPServer',
1318#         'test_socketserver.SocketServerTest.test_ForkingUDPServer',
1319#         'test_socketserver.SocketServerTest.test_ForkingUnixStreamServer'])
1320
1321# LibreSSL reports OPENSSL_VERSION_INFO (2, 0, 0, 0, 0) regardless of its version,
1322# so this is known to fail on some distros. We don't want to detect this because we
1323# don't want to trigger the side-effects of importing ssl prematurely if we will
1324# be monkey-patching, so we skip this test everywhere. It doesn't do much for us
1325# anyway.
1326disabled_tests += [
1327    'test_ssl.BasicSocketTests.test_openssl_version'
1328]
1329
1330if OSX:
1331
1332    disabled_tests += [
1333        # This sometimes produces OSError: Errno 40: Message too long
1334        'test_socket.RecvmsgIntoTCPTest.testRecvmsgIntoGenerator',
1335
1336        # These sometime timeout. Cannot reproduce locally.
1337        'test_ftp.TestTLS_FTPClassMixin.test_mlsd',
1338        'test_ftp.TestTLS_FTPClassMixin.test_retrlines_too_long',
1339        'test_ftp.TestTLS_FTPClassMixin.test_storlines',
1340        'test_ftp.TestTLS_FTPClassMixin.test_retrbinary_rest',
1341    ]
1342
1343    if RESOLVER_ARES and PY38 and not RUNNING_ON_CI:
1344        disabled_tests += [
1345            # When updating to 1.16.0 this was seen locally, but not on CI.
1346            # Tuples differ: ('ff02::1de:c0:face:8d', 1234, 0, 0)
1347            #             != ('ff02::1de:c0:face:8d', 1234, 0, 1)
1348            'test_socket.GeneralModuleTests.test_getaddrinfo_ipv6_scopeid_symbolic',
1349        ]
1350
1351if PY39:
1352
1353    disabled_tests += [
1354        # Depends on exact details of the repr. Eww.
1355        'test_subprocess.ProcessTestCase.test_repr',
1356        # Tries to wait for the process without using Popen APIs, and expects the
1357        # ``returncode`` attribute to stay None. But we have already hooked SIGCHLD, so
1358        # we see and set the ``returncode``; there is no way to wait that doesn't do that.
1359        'test_subprocess.POSIXProcessTestTest.test_send_signal_race',
1360    ]
1361
1362    if sys.version_info[:3] < (3, 9, 5):
1363        disabled_tests += [
1364            # These were added for fixes sometime between 3.9.1 and 3.9.5
1365            'test_ftplib.TestFTPClass.test_makepasv_issue43285_security_disabled',
1366            'test_ftplib.TestFTPClass.test_makepasv_issue43285_security_enabled_default',
1367            'test_httplib.BasicTest.test_dir_with_added_behavior_on_status',
1368            'test_httplib.TunnelTests.test_tunnel_connect_single_send_connection_setup',
1369            'test_ssl.TestSSLDebug.test_msg_callback_deadlock_bpo43577',
1370            # This one fails with the updated certs
1371            'test_ssl.ContextTests.test_load_verify_cadata',
1372            # These time out on 3.9.1 on Appveyor
1373            'test_ftplib.TestTLS_FTPClassMixin.test_retrbinary_rest',
1374            'test_ftplib.TestTLS_FTPClassMixin.test_retrlines_too_long',
1375        ]
1376
1377if PY310:
1378    disabled_tests += [
1379        # They arbitrarily made some types so that they can't be created;
1380        # that's an implementation detail we're not going to follow (
1381        # it would require them to be factory functions).
1382        'test_select.SelectTestCase.test_disallow_instantiation',
1383        'test_threading.ThreadTests.test_disallow_instantiation',
1384        # This wants two true threads to work, but a CPU bound loop
1385        # in a greenlet can't be interrupted.
1386        'test_threading.InterruptMainTests.test_can_interrupt_tight_loops',
1387    ]
1388
1389    if TRAVIS:
1390        disabled_tests += [
1391            # The mixing of subinterpreters (with threads) and gevent apparently
1392            # leads to a segfault on Ubuntu/GitHubActions/3.10rc1. Not clear why.
1393            # But that's not a great use case for gevent.
1394            'test_threading.SubinterpThreadingTests.test_threads_join',
1395            'test_threading.SubinterpThreadingTests.test_threads_join_2',
1396        ]
1397
1398if TRAVIS:
1399    disabled_tests += [
1400        # These tests frequently break when we try to use newer Travis CI images,
1401        # due to different versions of OpenSSL being available. See above for some
1402        # specific examples. Usually the tests catch up, eventually (e.g., at this writing,
1403        # the 3.9b1 tests are fine on Ubuntu Bionic, but all other versions fail).
1404        'test_ssl.ContextTests.test_options',
1405        'test_ssl.ThreadedTests.test_alpn_protocols',
1406        'test_ssl.ThreadedTests.test_default_ecdh_curve',
1407        'test_ssl.ThreadedTests.test_shared_ciphers',
1408
1409    ]
1410
1411
1412# Now build up the data structure we'll use to actually find disabled tests
1413# to avoid a linear scan for every file (it seems the list could get quite large)
1414# (First, freeze the source list to make sure it isn't modified anywhere)
1415
1416def _build_test_structure(sequence_of_tests):
1417
1418    _disabled_tests = frozenset(sequence_of_tests)
1419
1420    disabled_tests_by_file = collections.defaultdict(set)
1421    for file_case_meth in _disabled_tests:
1422        file_name, _case, _meth = file_case_meth.split('.')
1423
1424        by_file = disabled_tests_by_file[file_name]
1425
1426        by_file.add(file_case_meth)
1427
1428    return disabled_tests_by_file
1429
1430_disabled_tests_by_file = _build_test_structure(disabled_tests)
1431
1432_wrapped_tests_by_file = _build_test_structure(wrapped_tests)
1433
1434
1435def disable_tests_in_source(source, filename):
1436    # Source and filename are both native strings.
1437
1438    if filename.startswith('./'):
1439        # turn "./test_socket.py" (used for auto-complete) into "test_socket.py"
1440        filename = filename[2:]
1441
1442    if filename.endswith('.py'):
1443        filename = filename[:-3]
1444
1445
1446    # XXX ignoring TestCase class name (just using function name).
1447    # Maybe we should do this with the AST, or even after the test is
1448    # imported.
1449    my_disabled_tests = _disabled_tests_by_file.get(filename, ())
1450    my_wrapped_tests = _wrapped_tests_by_file.get(filename, {})
1451
1452
1453    if my_disabled_tests or my_wrapped_tests:
1454        # Insert our imports early in the file.
1455        # If we do it on a def-by-def basis, we can break syntax
1456        # if the function is already decorated
1457        pattern = r'^import .*'
1458        replacement = r'from gevent.testing import patched_tests_setup as _GEVENT_PTS;'
1459        replacement += r'import unittest as _GEVENT_UTS;'
1460        replacement += r'\g<0>'
1461        source, n = re.subn(pattern, replacement, source, 1, re.MULTILINE)
1462
1463        print("Added imports", n)
1464
1465    # Test cases will always be indented some,
1466    # so use [ \t]+. Without indentation, test_main, commonly used as the
1467    # __main__ function at the top level, could get matched. \s matches
1468    # newlines even in MULTILINE mode so it would still match that.
1469    my_disabled_testcases = set()
1470    for test in my_disabled_tests:
1471        testcase = test.split('.')[-1]
1472        my_disabled_testcases.add(testcase)
1473        # def foo_bar(self)
1474        # ->
1475        # @_GEVENT_UTS.skip('Removed by patched_tests_setup')
1476        # def foo_bar(self)
1477        pattern = r"^([ \t]+)def " + testcase
1478        replacement = r"\1@_GEVENT_UTS.skip('Removed by patched_tests_setup: %s')\n" % (test,)
1479        replacement += r"\g<0>"
1480        source, n = re.subn(pattern, replacement, source, 0, re.MULTILINE)
1481        print('Skipped %s (%d)' % (testcase, n), file=sys.stderr)
1482
1483
1484    for test in my_wrapped_tests:
1485        testcase = test.split('.')[-1]
1486        if testcase in my_disabled_testcases:
1487            print("Not wrapping %s because it is skipped" % (test,))
1488            continue
1489
1490        # def foo_bar(self)
1491        # ->
1492        # @_GEVENT_PTS._PatchedTest('file.Case.name')
1493        # def foo_bar(self)
1494        pattern = r"^([ \t]+)def " + testcase
1495        replacement = r"\1@_GEVENT_PTS._PatchedTest('%s')\n" % (test,)
1496        replacement += r"\g<0>"
1497
1498        source, n = re.subn(pattern, replacement, source, 0, re.MULTILINE)
1499        print('Wrapped %s (%d)' % (testcase, n), file=sys.stderr)
1500
1501    return source
1502