|
1 | 1 | import base64 |
2 | 2 | import datetime |
| 3 | +import decimal |
3 | 4 | import sys |
4 | 5 | import time |
5 | 6 | import unittest |
@@ -237,6 +238,54 @@ def test_loads_unsupported(self): |
237 | 238 | '</struct></value></param></params>') |
238 | 239 | self.assertRaises(ResponseError, xmlrpclib.loads, data) |
239 | 240 |
|
| 241 | + def check_loads(self, s, value, **kwargs): |
| 242 | + dump = '<params><param><value>%s</value></param></params>' % s |
| 243 | + result, m = xmlrpclib.loads(dump, **kwargs) |
| 244 | + (newvalue,) = result |
| 245 | + self.assertEqual(newvalue, value) |
| 246 | + self.assertIs(type(newvalue), type(value)) |
| 247 | + self.assertIsNone(m) |
| 248 | + |
| 249 | + def test_load_standard_types(self): |
| 250 | + check = self.check_loads |
| 251 | + check('string', 'string') |
| 252 | + check('<string>string</string>', 'string') |
| 253 | + check('<string>𝔘𝔫𝔦𝔠𝔬𝔡𝔢 string</string>', '𝔘𝔫𝔦𝔠𝔬𝔡𝔢 string') |
| 254 | + check('<int>2056183947</int>', 2056183947) |
| 255 | + check('<int>-2056183947</int>', -2056183947) |
| 256 | + check('<i4>2056183947</i4>', 2056183947) |
| 257 | + check('<double>46093.78125</double>', 46093.78125) |
| 258 | + check('<boolean>0</boolean>', False) |
| 259 | + check('<base64>AGJ5dGUgc3RyaW5n/w==</base64>', |
| 260 | + xmlrpclib.Binary(b'\x00byte string\xff')) |
| 261 | + check('<base64>AGJ5dGUgc3RyaW5n/w==</base64>', |
| 262 | + b'\x00byte string\xff', use_builtin_types=True) |
| 263 | + check('<dateTime.iso8601>20050210T11:41:23</dateTime.iso8601>', |
| 264 | + xmlrpclib.DateTime('20050210T11:41:23')) |
| 265 | + check('<dateTime.iso8601>20050210T11:41:23</dateTime.iso8601>', |
| 266 | + datetime.datetime(2005, 2, 10, 11, 41, 23), |
| 267 | + use_builtin_types=True) |
| 268 | + check('<array><data>' |
| 269 | + '<value><int>1</int></value><value><int>2</int></value>' |
| 270 | + '</data></array>', [1, 2]) |
| 271 | + check('<struct>' |
| 272 | + '<member><name>b</name><value><int>2</int></value></member>' |
| 273 | + '<member><name>a</name><value><int>1</int></value></member>' |
| 274 | + '</struct>', {'a': 1, 'b': 2}) |
| 275 | + |
| 276 | + def test_load_extension_types(self): |
| 277 | + check = self.check_loads |
| 278 | + check('<nil/>', None) |
| 279 | + check('<ex:nil/>', None) |
| 280 | + check('<i1>205</i1>', 205) |
| 281 | + check('<i2>20561</i2>', 20561) |
| 282 | + check('<i8>9876543210</i8>', 9876543210) |
| 283 | + check('<biginteger>98765432100123456789</biginteger>', |
| 284 | + 98765432100123456789) |
| 285 | + check('<float>93.78125</float>', 93.78125) |
| 286 | + check('<bigdecimal>9876543210.0123456789</bigdecimal>', |
| 287 | + decimal.Decimal('9876543210.0123456789')) |
| 288 | + |
240 | 289 | def test_get_host_info(self): |
241 | 290 | # see bug #3613, this raised a TypeError |
242 | 291 | transp = xmlrpc.client.Transport() |
|
0 commit comments