Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,10 @@ def _finish_closing(self, _):
log.debug("Closed child socket %s not yet accepted", child, extra={"sock": self})
child.close()
else:
if self.incoming_head is not None:
if self.incoming_head is not _PEER_CLOSED:
self.incoming_head.release()
self.incoming_head = None
msgs = []
self.incoming.drainTo(msgs)
for msg in msgs:
Expand Down
10 changes: 8 additions & 2 deletions Lib/test/test_float_jy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ def test_float_repr(self):

def test_float_repr2(self):
# Quite possibly these divergences result from JDK bug JDK-4511638:
if jython and test_support.get_java_version() >= (21,):
expected1 = '9.87654321e+18'
expected2 = '1.23523523523524e+18'
else:
expected1 = jython and '9.876543209999999e+18' or '9.87654321e+18'
expected2 = jython and '1.2352352352352399e+18' or '1.23523523523524e+18'
self.assertEqual(repr(9876.543210e+15),
jython and '9.876543209999999e+18' or '9.87654321e+18')
expected1)
self.assertEqual(repr(1235235235235240000.0),
jython and '1.2352352352352399e+18' or '1.23523523523524e+18')
expected2)

def test_float_str(self):
self.assertEqual(str(12345678.000005), '12345678.0')
Expand Down
40 changes: 34 additions & 6 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import errno
import os
import tempfile
import urlparse

import unittest
TestCase = unittest.TestCase
Expand All @@ -22,6 +23,26 @@

HOST = test_support.HOST

def _external_https_connection(host, port=443, **kwargs):
proxy = _https_proxy()
if proxy is None:
return httplib.HTTPSConnection(host, port, **kwargs)
proxy_host, proxy_port = proxy
conn = httplib.HTTPSConnection(proxy_host, proxy_port, **kwargs)
conn.set_tunnel(host, port)
return conn

def _https_proxy():
for name in ('https_proxy', 'HTTPS_PROXY', 'http_proxy', 'HTTP_PROXY'):
proxy = os.environ.get(name)
if proxy:
if '://' not in proxy:
proxy = '//' + proxy
parsed = urlparse.urlparse(proxy)
if parsed.hostname:
return parsed.hostname, parsed.port or 80
return None

class FakeSocket:
def __init__(self, text, fileclass=StringIO.StringIO, host=None, port=None):
self.text = text
Expand Down Expand Up @@ -827,7 +848,7 @@ def test_networked(self):
import ssl
test_support.requires('network')
with test_support.transient_internet('self-signed.pythontest.net'):
h = httplib.HTTPSConnection('self-signed.pythontest.net', 443)
h = _external_https_connection('self-signed.pythontest.net', 443)
with self.assertRaises(ssl.SSLError) as exc_info:
h.request('GET', '/')
if test_support.is_jython:
Expand All @@ -840,8 +861,8 @@ def test_networked_noverification(self):
test_support.requires('network')
with test_support.transient_internet('self-signed.pythontest.net'):
context = ssl._create_stdlib_context()
h = httplib.HTTPSConnection('self-signed.pythontest.net', 443,
context=context)
h = _external_https_connection('self-signed.pythontest.net', 443,
context=context)
h.request('GET', '/')
resp = h.getresponse()
self.assertIn('nginx', resp.getheader('server'))
Expand All @@ -851,7 +872,7 @@ def test_networked_trusted_by_default_cert(self):
# Default settings: requires a valid cert from a trusted CA
test_support.requires('network')
with test_support.transient_internet('www.python.org'):
h = httplib.HTTPSConnection('www.python.org', 443)
h = _external_https_connection('www.python.org', 443)
h.request('GET', '/')
resp = h.getresponse()
content_type = resp.getheader('content-type')
Expand All @@ -865,7 +886,7 @@ def test_networked_good_cert(self):
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations(CERT_selfsigned_pythontestdotnet)
h = httplib.HTTPSConnection('self-signed.pythontest.net', 443, context=context)
h = _external_https_connection('self-signed.pythontest.net', 443, context=context)
h.request('GET', '/')
resp = h.getresponse()
server_string = resp.getheader('server')
Expand All @@ -879,7 +900,7 @@ def test_networked_bad_cert(self):
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations(CERT_localhost)
h = httplib.HTTPSConnection('self-signed.pythontest.net', 443, context=context)
h = _external_https_connection('self-signed.pythontest.net', 443, context=context)
with self.assertRaises(ssl.SSLError) as exc_info:
h.request('GET', '/')
if test_support.is_jython:
Expand Down Expand Up @@ -996,6 +1017,13 @@ def create_connection(address, timeout=None, source_address=None):

@test_support.reap_threads
def test_main(verbose=None):
if test_support.is_jython:
from java.util.logging import Logger, Level
# Netty logs noisy channel-initializer warnings when tests deliberately
# close localhost sockets in error paths.
Logger.getLogger("io.netty.channel.ChannelInitializer").setLevel(Level.SEVERE)
Logger.getLogger("io.netty.util.concurrent.DefaultPromise").setLevel(Level.OFF)

test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
HTTPTest, HTTPSTest, SourceAddressTest,
TunnelTests)
Expand Down
26 changes: 18 additions & 8 deletions Lib/test/test_java_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ def test_equals(self):
self.assertNotEquals(x, z)
self.assertTrue(not (x == z))

@unittest.skipIf(test_support.get_java_version() >= (25,),
"Security Manager cannot be enabled on Java 25+")
class SecurityManagerTest(unittest.TestCase):

def test_nonexistent_import_with_security(self):
Expand Down Expand Up @@ -649,6 +651,14 @@ def find_jython_jars():
return jars


def java_command():
cmd = [os.path.join(System.getProperty("java.home"), "bin", "java")]
if test_support.get_java_version() >= (25,):
cmd.extend(["--enable-native-access=ALL-UNNAMED",
"--sun-misc-unsafe-memory-access=allow"])
return cmd


class JavaSource(SimpleJavaFileObject):

def __init__(self, name, source):
Expand Down Expand Up @@ -754,11 +764,11 @@ def test_proxy_serialization(self):
jars = find_jython_jars()
jars.append(proxies_jar_path)
classpath = os.pathsep.join(jars)
cmd = [os.path.join(System.getProperty("java.home"), "bin", "java"),
"-Dpython.path=" + os.path.dirname(__file__),
"-classpath", classpath,
"javatests.ProxyDeserialization",
cat_path]
cmd = java_command()
cmd.extend(["-Dpython.path=" + os.path.dirname(__file__),
"-classpath", classpath,
"javatests.ProxyDeserialization",
cat_path])
self.assertEqual(subprocess.check_output(cmd, universal_newlines=True), "meow\n")
finally:
org.python.core.Options.proxyDebugDirectory = old_proxy_debug_dir
Expand Down Expand Up @@ -815,9 +825,9 @@ def test_custom_proxymaker(self):
# PySystemState (and Jython runtime) is initialized for
# the proxy
classpath += os.pathsep + tempdir
cmd = [os.path.join(System.getProperty("java.home"), "bin", "java"),
"-Dpython.path=" + os.path.dirname(__file__),
"-classpath", classpath, "BarkTheDog"]
cmd = java_command()
cmd.extend(["-Dpython.path=" + os.path.dirname(__file__),
"-classpath", classpath, "BarkTheDog"])
self.assertRegexpMatches(
subprocess.check_output(cmd, universal_newlines=True,
stderr=subprocess.STDOUT),
Expand Down
11 changes: 7 additions & 4 deletions Lib/test/test_java_visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,17 @@ def run_accessibility_script(self, script, error=AttributeError):
# Prepare to break the rules
self.command.append("-J-Dpython.cachedir.skip=true")
self.command.append("-J-Dpython.security.respectJavaAccessibility=false")
if test_support.get_java_version() >= (9,):
# See all the cases for which we have forgotten --add-opens
self.command.append("-J--illegal-access=warn")
java_version = test_support.get_java_version()
if java_version >= (9,):
# See all the cases for which we have forgotten --add-opens where
# the diagnostic flag still exists.
if java_version < (17,):
self.command.append("-J--illegal-access=warn")
# Open the packages used in the scripts
self.add_opens("java.desktop", "java.awt.geom")
for package in ("lang", "util", "nio", "nio.charset"):
self.add_opens("java.base", "java." + package)
if test_support.get_java_version() >= (12,):
if java_version >= (12,):
self.add_opens("java.base", "java.lang.constant")

self.command.append(fn)
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_jy_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def survivors(weak_list):
return s


@unittest.skipIf(test_support.get_java_version() >= (25,),
"Java 25+ does not reliably unload these test classes after explicit GC")
class MemoryLeakTests(unittest.TestCase):

def test_class_to_test_weakness(self):
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_robotparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,14 @@ class NetworkTestCase(unittest.TestCase):

def testPythonOrg(self):
support.requires('network')
with support.transient_internet('www.python.org'):
with support.transient_internet('www.pythontest.net'):
parser = robotparser.RobotFileParser(
"http://www.python.org/robots.txt")
"http://www.pythontest.net/robots.txt")
parser.read()
self.assertTrue(
parser.can_fetch("*", "http://www.python.org/robots.txt"))
parser.can_fetch("*", "http://www.pythontest.net/"))
self.assertFalse(
parser.can_fetch("*", "http://www.pythontest.net/no-robots-here/"))

def load_tests(loader, suite, pattern):
suite = unittest.makeSuite(NetworkTestCase)
Expand Down
29 changes: 27 additions & 2 deletions Lib/test/test_ssl_jy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# directly in the original, but this reduces the diff and ongoing merge effort

import errno
import os
import select
import socket
import ssl
Expand All @@ -15,6 +16,20 @@
from test.test_ssl import REMOTE_HOST, REMOTE_ROOT_CERT


def _errno_values(*names):
return tuple(getattr(errno, name) for name in names if hasattr(errno, name))


TRANSIENT_CONNECT_EX = _errno_values(
'ECONNREFUSED', 'EHOSTUNREACH', 'ENETUNREACH', 'ETIMEDOUT',
'EHOSTDOWN', 'ENETDOWN')


def _external_http_proxy_configured():
return any(os.environ.get(name) for name in (
'http_proxy', 'https_proxy', 'all_proxy',
'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY'))


class BasicSocketTests(test.test_ssl.BasicSocketTests):
@unittest.skip("Jython does not have _ssl, therefore this test needs to be rewritten")
Expand Down Expand Up @@ -142,7 +157,14 @@ def test_subclass(self):
None


@unittest.skipIf(_external_http_proxy_configured(),
"external raw SSL socket tests require direct network access")
class NetworkedTests(test.test_ssl.NetworkedTests):
def _skip_if_remote_unreachable(self, rc):
if rc in TRANSIENT_CONNECT_EX:
self.skipTest("%s is not reachable from this environment" %
(REMOTE_HOST,))

def test_connect_ex(self):
# Issue #11326: check connect_ex() implementation
with support.transient_internet(REMOTE_HOST):
Expand All @@ -151,7 +173,9 @@ def test_connect_ex(self):
ca_certs=REMOTE_ROOT_CERT)
try:
# Jython, errno.EISCONN expected per earlier 2.x versions, not 0
self.assertEqual(errno.EISCONN, s.connect_ex((REMOTE_HOST, 443)))
rc = s.connect_ex((REMOTE_HOST, 443))
self._skip_if_remote_unreachable(rc)
self.assertEqual(errno.EISCONN, rc)
self.assertTrue(s.getpeercert())
finally:
s.close()
Expand All @@ -168,6 +192,7 @@ def test_non_blocking_connect_ex(self):
try:
s.setblocking(False)
rc = s.connect_ex((REMOTE_HOST, 443))
self._skip_if_remote_unreachable(rc)
# EWOULDBLOCK under Windows, EINPROGRESS elsewhere
# Jython added EALREADY, as in Jython connect may have already happened
self.assertIn(rc, (0, errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK))
Expand Down Expand Up @@ -200,6 +225,7 @@ def test_timeout_connect_ex(self):
try:
s.settimeout(0.0000001)
rc = s.connect_ex((REMOTE_HOST, 443))
self._skip_if_remote_unreachable(rc)
if rc == errno.EISCONN:
self.skipTest("REMOTE_HOST responded too quickly")
self.assertIn(rc, (errno.ETIMEDOUT, errno.EAGAIN, errno.EWOULDBLOCK))
Expand Down Expand Up @@ -229,4 +255,3 @@ def test_main(verbose=False):

if __name__ == "__main__":
test_main()

24 changes: 16 additions & 8 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,14 +1490,22 @@ def test_HTTPError_interface_call(self):

def test_main(verbose=None):
from test import test_urllib2
test_support.run_doctest(test_urllib2, verbose)
test_support.run_doctest(urllib2, verbose)
tests = (TrivialTests,
OpenerDirectorTests,
HandlerTests,
MiscTests,
RequestTests)
test_support.run_unittest(*tests)
old_opener = urllib2._opener
with test_support.EnvironmentVarGuard() as env:
try:
for name in ('http_proxy', 'https_proxy', 'ftp_proxy', 'no_proxy',
'HTTP_PROXY', 'HTTPS_PROXY', 'FTP_PROXY', 'NO_PROXY'):
env.unset(name)
test_support.run_doctest(test_urllib2, verbose)
test_support.run_doctest(urllib2, verbose)
tests = (TrivialTests,
OpenerDirectorTests,
HandlerTests,
MiscTests,
RequestTests)
test_support.run_unittest(*tests)
finally:
urllib2._opener = old_opener

if __name__ == "__main__":
test_main(verbose=True)
16 changes: 16 additions & 0 deletions Lib/test/test_urllib2net.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ def wrapped(*args, **kwargs):
'on Travis CI')


def _external_http_proxy_configured():
return any(os.environ.get(name) for name in (
'http_proxy', 'https_proxy', 'all_proxy',
'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY'))


skip_ftp_test_with_http_proxy = unittest.skipIf(
_external_http_proxy_configured(),
'external FTP tests require direct FTP access, not an HTTP proxy')


# Connecting to remote hosts is flaky. Make it more robust by retrying
# the connection several times.
_urlopen_with_retry = _wrap_with_retry_thrice(urllib2.urlopen, urllib2.URLError)
Expand Down Expand Up @@ -107,6 +118,7 @@ def setUp(self):
# XXX The rest of these tests aren't very good -- they don't check much.
# They do sometimes catch some major disasters, though.

@skip_ftp_test_with_http_proxy
@skip_ftp_test_on_travis
def test_ftp(self):
urls = [
Expand Down Expand Up @@ -293,13 +305,15 @@ def test_http_timeout(self):

FTP_HOST = 'ftp://www.pythontest.net/'

@skip_ftp_test_with_http_proxy
@skip_ftp_test_on_travis
def test_ftp_basic(self):
self.assertIsNone(socket.getdefaulttimeout())
with test_support.transient_internet(self.FTP_HOST, timeout=None):
u = _urlopen_with_retry(self.FTP_HOST)
self.assertIsNone(u.fp.fp._sock.gettimeout())

@skip_ftp_test_with_http_proxy
@skip_ftp_test_on_travis
def test_ftp_default_timeout(self):
self.assertIsNone(socket.getdefaulttimeout())
Expand All @@ -311,6 +325,7 @@ def test_ftp_default_timeout(self):
socket.setdefaulttimeout(None)
self.assertEqual(u.fp.fp._sock.gettimeout(), 60)

@skip_ftp_test_with_http_proxy
@skip_ftp_test_on_travis
def test_ftp_no_timeout(self):
self.assertIsNone(socket.getdefaulttimeout(),)
Expand All @@ -322,6 +337,7 @@ def test_ftp_no_timeout(self):
socket.setdefaulttimeout(None)
self.assertIsNone(u.fp.fp._sock.gettimeout())

@skip_ftp_test_with_http_proxy
@skip_ftp_test_on_travis
def test_ftp_timeout(self):
with test_support.transient_internet(self.FTP_HOST):
Expand Down
Loading
Loading