|
16 | 16 | import contextlib |
17 | 17 | from test import support |
18 | 18 | from test.support import os_helper |
| 19 | +from test.support import script_helper |
19 | 20 | from test.support import socket_helper |
20 | 21 | from test.support import threading_helper |
21 | 22 | from test.support import ALWAYS_EQ, LARGEST, SMALLEST |
@@ -129,6 +130,56 @@ def test_dump_big_long(self): |
129 | 130 | def test_dump_bad_dict(self): |
130 | 131 | self.assertRaises(TypeError, xmlrpclib.dumps, ({(1,2,3): 1},)) |
131 | 132 |
|
| 133 | + def test_dump_invalid_params(self): |
| 134 | + for params in ([], ["x"], {"x": 1}, "abc", 1): |
| 135 | + with self.subTest(params=params): |
| 136 | + with self.assertRaisesRegex( |
| 137 | + TypeError, |
| 138 | + "^argument must be tuple or Fault instance$", |
| 139 | + ): |
| 140 | + xmlrpclib.dumps(params) |
| 141 | + |
| 142 | + def test_dump_invalid_methodresponse(self): |
| 143 | + for params in ((), (1, 2)): |
| 144 | + with self.subTest(params=params): |
| 145 | + with self.assertRaisesRegex( |
| 146 | + ValueError, |
| 147 | + "^response tuple must be a singleton$", |
| 148 | + ): |
| 149 | + xmlrpclib.dumps(params, methodresponse=True) |
| 150 | + |
| 151 | + def test_dump_invalid_params_optimized(self): |
| 152 | + code = r""" |
| 153 | +import xmlrpc.client as xmlrpclib |
| 154 | +
|
| 155 | +if __debug__: |
| 156 | + raise AssertionError("expected optimized mode") |
| 157 | +
|
| 158 | +def check(exc_type, message, func): |
| 159 | + try: |
| 160 | + func() |
| 161 | + except exc_type as exc: |
| 162 | + if str(exc) != message: |
| 163 | + raise AssertionError(str(exc)) |
| 164 | + else: |
| 165 | + raise AssertionError(f"{exc_type.__name__} not raised") |
| 166 | +
|
| 167 | +for params in [], ["x"], {"x": 1}, "abc", 1: |
| 168 | + check( |
| 169 | + TypeError, |
| 170 | + "argument must be tuple or Fault instance", |
| 171 | + lambda params=params: xmlrpclib.dumps(params), |
| 172 | + ) |
| 173 | +
|
| 174 | +for params in (), (1, 2): |
| 175 | + check( |
| 176 | + ValueError, |
| 177 | + "response tuple must be a singleton", |
| 178 | + lambda params=params: xmlrpclib.dumps(params, methodresponse=True), |
| 179 | + ) |
| 180 | +""" |
| 181 | + script_helper.assert_python_ok("-O", "-c", code) |
| 182 | + |
132 | 183 | def test_dump_recursive_seq(self): |
133 | 184 | l = [1,2,3] |
134 | 185 | t = [3,4,5,l] |
|
0 commit comments