~oddbloke/cloud-init/lp-1436350

« back to all changes in this revision

Viewing changes to tests/unittests/helpers.py

  • Committer: Scott Moser
  • Date: 2015-03-04 22:10:52 UTC
  • mfrom: (1072.2.1 fix_py34_test_hang)
  • Revision ID: smoser@ubuntu.com-20150304221052-unhv6sen6i7vg7s5
Fix hang caused by HTTPretty on Python 3.4.2.

HTTPretty can causes hangs on Python 3.4.2 (and maybe Python 3.4.1), due
to a Python bug (fixed in Python 3.4.3). This works around the problem
in the appropriate Python versions.

See https://github.com/gabrielfalcao/HTTPretty/pull/193 and
https://github.com/gabrielfalcao/HTTPretty/issues/221 for details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from __future__ import print_function
2
2
 
 
3
import functools
3
4
import os
4
5
import sys
5
6
import shutil
25
26
PY26 = False
26
27
PY27 = False
27
28
PY3 = False
 
29
FIX_HTTPRETTY = False
28
30
 
29
31
_PY_VER = sys.version_info
30
 
_PY_MAJOR, _PY_MINOR = _PY_VER[0:2]
 
32
_PY_MAJOR, _PY_MINOR, _PY_MICRO = _PY_VER[0:3]
31
33
if (_PY_MAJOR, _PY_MINOR) <= (2, 6):
32
34
    if (_PY_MAJOR, _PY_MINOR) == (2, 6):
33
35
        PY26 = True
39
41
        PY2 = True
40
42
    if (_PY_MAJOR, _PY_MINOR) >= (3, 0):
41
43
        PY3 = True
 
44
        if _PY_MINOR == 4 and _PY_MICRO < 3:
 
45
            FIX_HTTPRETTY = True
42
46
 
43
47
if PY26:
44
48
    # For now add these on, taken from python 2.7 + slightly adjusted.  Drop
268
272
                mock.patch.object(sys, 'stderr', stderr))
269
273
 
270
274
 
 
275
def import_httpretty():
 
276
    """Import HTTPretty and monkey patch Python 3.4 issue.
 
277
    See https://github.com/gabrielfalcao/HTTPretty/pull/193 and
 
278
    as well as https://github.com/gabrielfalcao/HTTPretty/issues/221.
 
279
 
 
280
    Lifted from
 
281
    https://github.com/inveniosoftware/datacite/blob/master/tests/helpers.py
 
282
    """
 
283
    if not FIX_HTTPRETTY:
 
284
        import httpretty
 
285
    else:
 
286
        import socket
 
287
        old_SocketType = socket.SocketType
 
288
 
 
289
        import httpretty
 
290
        from httpretty import core
 
291
 
 
292
        def sockettype_patch(f):
 
293
            @functools.wraps(f)
 
294
            def inner(*args, **kwargs):
 
295
                f(*args, **kwargs)
 
296
                socket.SocketType = old_SocketType
 
297
                socket.__dict__['SocketType'] = old_SocketType
 
298
            return inner
 
299
 
 
300
        core.httpretty.disable = sockettype_patch(
 
301
            httpretty.httpretty.disable
 
302
        )
 
303
    return httpretty
 
304
 
 
305
 
271
306
class HttprettyTestCase(TestCase):
272
307
    # necessary as http_proxy gets in the way of httpretty
273
308
    # https://github.com/gabrielfalcao/HTTPretty/issues/122