|
7 | 7 | from __future__ import print_function |
8 | 8 | import unittest, os, shutil, errno, pytest, sys, difflib, re |
9 | 9 |
|
| 10 | +from contextlib import contextmanager |
| 11 | + |
10 | 12 | from roundup.anypy import xmlrpc_ |
11 | 13 | MultiCall = xmlrpc_.client.MultiCall |
12 | 14 | from roundup.cgi.exceptions import * |
|
26 | 28 |
|
27 | 29 | if client.defusedxml: |
28 | 30 | skip_defusedxml = lambda func, *args, **kwargs: func |
29 | | - |
30 | | - skip_defusedxml_used = mark_class(pytest.mark.skip( |
31 | | - reason='Skipping non-defusedxml tests: defusedxml library in use')) |
32 | 31 | else: |
33 | 32 | skip_defusedxml = mark_class(pytest.mark.skip( |
34 | 33 | reason='Skipping defusedxml tests: defusedxml library not available')) |
35 | | - |
36 | | - skip_defusedxml_used = lambda func, *args, **kwargs: func |
37 | 34 |
|
38 | 35 | if sys.version_info[0] > 2: |
39 | 36 | skip_python2 = lambda func, *args, **kwargs: func |
40 | 37 | else: |
41 | 38 | skip_python2 = mark_class(pytest.mark.skip( |
42 | 39 | reason='Skipping test under python 2')) |
43 | 40 |
|
| 41 | +@contextmanager |
| 42 | +def disable_defusedxml(): |
| 43 | + # if defusedxml not loaded, do nothing |
| 44 | + if 'defusedxml' not in sys.modules: |
| 45 | + yield |
| 46 | + return |
| 47 | + |
| 48 | + sys.modules['defusedxml'].xmlrpc.unmonkey_patch() |
| 49 | + try: |
| 50 | + yield |
| 51 | + finally: |
| 52 | + # restore normal defused xmlrpc functions |
| 53 | + sys.modules['defusedxml'].xmlrpc.monkey_patch() |
44 | 54 |
|
45 | 55 | class XmlrpcTest(object): |
46 | 56 |
|
@@ -340,9 +350,9 @@ def testDefusedXmlBomb(self): |
340 | 350 | self.XmlBomb(expectIn=b"defusedxml.common.EntitiesForbidden") |
341 | 351 |
|
342 | 352 | @skip_python2 |
343 | | - @skip_defusedxml_used |
344 | 353 | def testNonDefusedXmlBomb(self): |
345 | | - self.XmlBomb(expectIn=b"1234567890"*511) |
| 354 | + with disable_defusedxml(): |
| 355 | + self.XmlBomb(expectIn=b"1234567890"*511) |
346 | 356 |
|
347 | 357 | def XmlBomb(self, expectIn=None): |
348 | 358 |
|
|
0 commit comments