Test PEP 383 surrogateescape handling for AF_UNIX addresses. diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1395,6 +1395,38 @@ class TestUnixDomain(unittest.TestCase): self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) + def testBytearrayAddr(self): + # Test binding to a bytearray pathname. + path = os.path.abspath(support.TESTFN) + b = self.encoded(path) + self.sock.bind(bytearray(b)) + self.addCleanup(support.unlink, path) + self.assertEqual(self.sock.getsockname(), path) + + def testSurrogateescapeBind(self): + # Test binding to a valid non-ASCII pathname, with the + # non-ASCII bytes supplied using surrogateescape encoding. + path = os.path.abspath(support.TESTFN_UNICODE) + b = self.encoded(path) + self.sock.bind(b.decode("ascii", "surrogateescape")) + self.addCleanup(support.unlink, path) + self.assertEqual(self.sock.getsockname(), path) + + def testUndecodableAddr(self): + # Test binding to a non-ASCII pathname, then reading the + # address back with the file system encoding set to ASCII. + path = os.path.abspath(support.TESTFN_UNICODE) + b = self.encoded(path) + self.sock.bind(b) + self.addCleanup(support.unlink, path) + saved_encoding = sys.getfilesystemencoding() + try: + sys.setfilesystemencoding("ascii") + name = self.sock.getsockname() + finally: + sys.setfilesystemencoding(saved_encoding) + self.assertEqual(name, b.decode("ascii", "surrogateescape")) + class BufferIOTest(SocketConnectedTest): """ Test the buffer versions of socket.recv() and socket.send().