def test_nonascii(self): start_string = test_support.u(r'P\N{LATIN SMALL LETTER Y WITH CIRCUMFLEX}t') end_string = test_support.u(r'h\N{LATIN SMALL LETTER O WITH HORN}n') try: p = xmlrpclib.ServerProxy(URL) self.assertEqual(p.add(start_string, end_string), start_string + end_string) except (xmlrpclib.ProtocolError, socket.error) as e: # ignore failures due to non-blocking socket unavailable errors. if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_dump_encoding(self): value = {test_support.u(r'key\u20ac\xa4'): test_support.u(r'value\u20ac\xa4')} strg = xmlrpclib.dumps((value,), encoding='iso-8859-15') strg = "<?xml version='1.0' encoding='iso-8859-15'?>" + strg self.assertEqual(xmlrpclib.loads(strg)[0][0], value) strg = xmlrpclib.dumps((value,), encoding='iso-8859-15', methodresponse=True) self.assertEqual(xmlrpclib.loads(strg)[0][0], value) methodname = test_support.u(r'method\u20ac\xa4') strg = xmlrpclib.dumps((value,), encoding='iso-8859-15', methodname=methodname) self.assertEqual(xmlrpclib.loads(strg)[0][0], value) self.assertEqual(xmlrpclib.loads(strg)[1], methodname)
def http_server(evt, numrequests, requestHandler=None, encoding=None): class TestInstanceClass: def div(self, x, y): return x // y def _methodHelp(self, name): if name == 'div': return 'This is the div function' def my_function(): '''This is my function''' return True class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer): def get_request(self): # Ensure the socket is always non-blocking. On Linux, socket # attributes are not inherited like they are on *BSD and Windows. s, port = self.socket.accept() s.setblocking(True) return s, port if not requestHandler: requestHandler = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler serv = MyXMLRPCServer(("localhost", 0), requestHandler, encoding=encoding, logRequests=False, bind_and_activate=False) try: serv.socket.settimeout(3) serv.server_bind() global ADDR, PORT, URL ADDR, PORT = serv.socket.getsockname() #connect to IP address directly. This avoids socket.create_connection() #trying to connect to "localhost" using all address families, which #causes slowdown e.g. on vista which supports AF_INET6. The server listens #on AF_INET only. URL = "http://%s:%d" % (ADDR, PORT) serv.server_activate() serv.register_introspection_functions() serv.register_multicall_functions() serv.register_function(pow) serv.register_function(lambda x, y: x + y, 'add') serv.register_function(lambda x: x, test_support.u(r't\xea\u0161t')) serv.register_function(my_function) serv.register_instance(TestInstanceClass()) evt.set() # handle up to 'numrequests' requests while numrequests > 0: serv.handle_request() numrequests -= 1 except socket.timeout: pass finally: serv.socket.close() PORT = None evt.set()
def http_server(evt, numrequests, requestHandler=None, encoding=None): class TestInstanceClass: def div(self, x, y): return x // y def _methodHelp(self, name): if name == 'div': return 'This is the div function' def my_function(): '''This is my function''' return True class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer): def get_request(self): # Ensure the socket is always non-blocking. On Linux, socket # attributes are not inherited like they are on *BSD and Windows. s, port = self.socket.accept() s.setblocking(True) return s, port if not requestHandler: requestHandler = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler serv = MyXMLRPCServer(("localhost", 0), requestHandler, encoding=encoding, logRequests=False, bind_and_activate=False) try: serv.socket.settimeout(3) serv.server_bind() global ADDR, PORT, URL ADDR, PORT = serv.socket.getsockname() #connect to IP address directly. This avoids socket.create_connection() #trying to connect to "localhost" using all address families, which #causes slowdown e.g. on vista which supports AF_INET6. The server listens #on AF_INET only. URL = "http://%s:%d"%(ADDR, PORT) serv.server_activate() serv.register_introspection_functions() serv.register_multicall_functions() serv.register_function(pow) serv.register_function(lambda x,y: x+y, 'add') serv.register_function(lambda x: x, test_support.u(r't\xea\u0161t')) serv.register_function(my_function) serv.register_instance(TestInstanceClass()) evt.set() # handle up to 'numrequests' requests while numrequests > 0: serv.handle_request() numrequests -= 1 except socket.timeout: pass finally: serv.socket.close() PORT = None evt.set()
def test_expandvars_nonascii_word(self): encoding = sys.getfilesystemencoding() # Non-ASCII word characters letters = test_support.u(r'\xe6\u0130\u0141\u03c6\u041a\u05d0\u062a\u0e01') uwnonascii = letters.encode(encoding, 'ignore').decode(encoding)[:3] swnonascii = uwnonascii.encode(encoding) if not swnonascii: self.skipTest('Needs non-ASCII word characters') with test_support.EnvironmentVarGuard() as env: env.clear() env[swnonascii] = 'baz' + swnonascii self.assertEqual(posixpath.expandvars(u'$%s bar' % uwnonascii), u'baz%s bar' % uwnonascii)
def test_introspection1(self): try: p = xmlrpclib.ServerProxy(URL) meth = p.system.listMethods() expected_methods = set(['pow', 'div', 'my_function', 'add', test_support.u(r't\xea\u0161t'), 'system.listMethods', 'system.methodHelp', 'system.methodSignature', 'system.multicall']) self.assertEqual(set(meth), expected_methods) except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_encoding(self): code = b"# -*- coding: badencoding -*-\npass\n" self.assertRaises(SyntaxError, compile, code, "tmp", "exec") code = u"# -*- coding: utf-8 -*-\npass\n" self.assertRaises(SyntaxError, compile, code, "tmp", "exec") code = 'u"\xc2\xa4"\n' self.assertEqual(eval(code), u"\xc2\xa4") code = u'u"\xc2\xa4"\n' self.assertEqual(eval(code), u"\xc2\xa4") code = '# -*- coding: latin1 -*-\nu"\xc2\xa4"\n' self.assertEqual(eval(code), u"\xc2\xa4") code = '# -*- coding: utf-8 -*-\nu"\xc2\xa4"\n' self.assertEqual(eval(code), u"\xa4") code = '# -*- coding: iso8859-15 -*-\nu"\xc2\xa4"\n' self.assertEqual(eval(code), test_support.u(r"\xc2\u20ac")) code = 'u"""\\\n# -*- coding: utf-8 -*-\n\xc2\xa4"""\n' self.assertEqual(eval(code), u"# -*- coding: utf-8 -*-\n\xc2\xa4")
def test_encoding(self): code = b'# -*- coding: badencoding -*-\npass\n' self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec') code = u"# -*- coding: utf-8 -*-\npass\n" self.assertRaises(SyntaxError, compile, code, "tmp", "exec") code = 'u"\xc2\xa4"\n' self.assertEqual(eval(code), u'\xc2\xa4') code = u'u"\xc2\xa4"\n' self.assertEqual(eval(code), u'\xc2\xa4') code = '# -*- coding: latin1 -*-\nu"\xc2\xa4"\n' self.assertEqual(eval(code), u'\xc2\xa4') code = '# -*- coding: utf-8 -*-\nu"\xc2\xa4"\n' self.assertEqual(eval(code), u'\xa4') code = '# -*- coding: iso8859-15 -*-\nu"\xc2\xa4"\n' self.assertEqual(eval(code), test_support.u(r'\xc2\u20ac')) code = 'u"""\\\n# -*- coding: utf-8 -*-\n\xc2\xa4"""\n' self.assertEqual(eval(code), u'# -*- coding: utf-8 -*-\n\xc2\xa4')
def test_toBytes(self): result = urllib.toBytes(u'http://www.python.org') self.assertEqual(result, 'http://www.python.org') self.assertRaises( UnicodeError, urllib.toBytes, test_support.u(r'http://www.python.org/medi\u00e6val'))
def test_toBytes(self): result = urllib.toBytes(u'http://www.python.org') self.assertEqual(result, 'http://www.python.org') self.assertRaises(UnicodeError, urllib.toBytes, test_support.u(r'http://www.python.org/medi\u00e6val'))
'afloat': 7283.43, 'anint': 2**20, 'ashortlong': 2L, 'anotherlist': ['.zyx.41'], 'abase64': xmlrpclib.Binary("my dog has fleas"), 'boolean': xmlrpclib.False, 'datetime1': xmlrpclib.DateTime('20050210T11:41:23'), 'datetime2': xmlrpclib.DateTime( (2005, 02, 10, 11, 41, 23, 0, 1, -1)), 'datetime3': xmlrpclib.DateTime( datetime.datetime(2005, 02, 10, 11, 41, 23)), }] if test_support.have_unicode: alist[0].update({ 'unicode': test_support.u(r'\u4000\u6000\u8000'), test_support.u(r'ukey\u4000'): 'regular value', }) class XMLRPCTestCase(unittest.TestCase): def test_dump_load(self): self.assertEqual(alist, xmlrpclib.loads(xmlrpclib.dumps((alist,)))[0][0]) def test_dump_bare_datetime(self): # This checks that an unwrapped datetime.date object can be handled # by the marshalling code. This can't be done via test_dump_load() # since with use_datetime set to 1 the unmarshaller would create # datetime objects for the 'datetime[123]' keys as well dt = datetime.datetime(2005, 02, 10, 11, 41, 23)
class ParseTest(unittest.TestCase): data = support.u(r'<money value="$\xa3\u20ac\U0001017b">' r'$\xa3\u20ac\U0001017b</money>') def tearDown(self): support.unlink(TESTFN) def check_parse(self, f): from xml.sax import parse result = StringIO() parse(f, XMLGenerator(result, 'utf-8')) self.assertEqual(result.getvalue(), xml_bytes(self.data, 'utf-8')) def test_parse_bytes(self): # UTF-8 is default encoding, US-ASCII is compatible with UTF-8, # UTF-16 is autodetected encodings = ('us-ascii', 'utf-8', 'utf-16', 'utf-16le', 'utf-16be') for encoding in encodings: self.check_parse(io.BytesIO(xml_bytes(self.data, encoding))) make_xml_file(self.data, encoding) self.check_parse(TESTFN) with io.open(TESTFN, 'rb') as f: self.check_parse(f) self.check_parse(io.BytesIO(xml_bytes(self.data, encoding, None))) make_xml_file(self.data, encoding, None) self.check_parse(TESTFN) with io.open(TESTFN, 'rb') as f: self.check_parse(f) # accept UTF-8 with BOM self.check_parse(io.BytesIO(xml_bytes(self.data, 'utf-8-sig', 'utf-8'))) make_xml_file(self.data, 'utf-8-sig', 'utf-8') self.check_parse(TESTFN) with io.open(TESTFN, 'rb') as f: self.check_parse(f) self.check_parse(io.BytesIO(xml_bytes(self.data, 'utf-8-sig', None))) make_xml_file(self.data, 'utf-8-sig', None) self.check_parse(TESTFN) with io.open(TESTFN, 'rb') as f: self.check_parse(f) # accept data with declared encoding self.check_parse(io.BytesIO(xml_bytes(self.data, 'iso-8859-1'))) make_xml_file(self.data, 'iso-8859-1') self.check_parse(TESTFN) with io.open(TESTFN, 'rb') as f: self.check_parse(f) # fail on non-UTF-8 incompatible data without declared encoding with self.assertRaises(SAXException): self.check_parse( io.BytesIO(xml_bytes(self.data, 'iso-8859-1', None))) make_xml_file(self.data, 'iso-8859-1', None) with self.assertRaises(SAXException): self.check_parse(TESTFN) with io.open(TESTFN, 'rb') as f: with self.assertRaises(SAXException): self.check_parse(f) def test_parse_InputSource(self): # accept data without declared but with explicitly specified encoding make_xml_file(self.data, 'iso-8859-1', None) with io.open(TESTFN, 'rb') as f: input = InputSource() input.setByteStream(f) input.setEncoding('iso-8859-1') self.check_parse(input) def test_parse_close_source(self): builtin_open = open non_local = {'fileobj': None} def mock_open(*args): fileobj = builtin_open(*args) non_local['fileobj'] = fileobj return fileobj with support.swap_attr(saxutils, 'open', mock_open): make_xml_file(self.data, 'iso-8859-1', None) with self.assertRaises(SAXException): self.check_parse(TESTFN) self.assertTrue(non_local['fileobj'].closed) def check_parseString(self, s): from xml.sax import parseString result = StringIO() parseString(s, XMLGenerator(result, 'utf-8')) self.assertEqual(result.getvalue(), xml_bytes(self.data, 'utf-8')) def test_parseString_bytes(self): # UTF-8 is default encoding, US-ASCII is compatible with UTF-8, # UTF-16 is autodetected encodings = ('us-ascii', 'utf-8', 'utf-16', 'utf-16le', 'utf-16be') for encoding in encodings: self.check_parseString(xml_bytes(self.data, encoding)) self.check_parseString(xml_bytes(self.data, encoding, None)) # accept UTF-8 with BOM self.check_parseString(xml_bytes(self.data, 'utf-8-sig', 'utf-8')) self.check_parseString(xml_bytes(self.data, 'utf-8-sig', None)) # accept data with declared encoding self.check_parseString(xml_bytes(self.data, 'iso-8859-1')) # fail on non-UTF-8 incompatible data without declared encoding with self.assertRaises(SAXException): self.check_parseString(xml_bytes(self.data, 'iso-8859-1', None))