Skip to content

Instantly share code, notes, and snippets.

@zaitcev
Created July 27, 2016 16:31
Show Gist options
  • Save zaitcev/2215b7b085d974070321711843255190 to your computer and use it in GitHub Desktop.
Save zaitcev/2215b7b085d974070321711843255190 to your computer and use it in GitHub Desktop.
Alternative for c/347498
See https://review.openstack.org/347498
monkey_patch_mimetools() now does nothing on py3
diff --git a/swift/common/wsgi.py b/swift/common/wsgi.py
index 88e61f2..f018cd1 100644
--- a/swift/common/wsgi.py
+++ b/swift/common/wsgi.py
@@ -22,7 +22,6 @@ import inspect
import os
import signal
import time
-import mimetools
from swift import gettext_ as _
from textwrap import dedent
@@ -142,26 +141,32 @@ def wrap_conf_type(f):
appconfig = wrap_conf_type(loadwsgi.appconfig)
-def monkey_patch_mimetools():
- """
- mimetools.Message defaults content-type to "text/plain"
- This changes it to default to None, so we can detect missing headers.
- """
+monkey_patch_mimetools = lambda: None
+if six.PY2:
+ import mimetools
+
+ def _monkey_patch_mimetools_py2():
+ """
+ mimetools.Message defaults content-type to "text/plain"
+ This changes it to default to None, so we can detect missing headers.
+ """
- orig_parsetype = mimetools.Message.parsetype
+ orig_parsetype = mimetools.Message.parsetype
- def parsetype(self):
- if not self.typeheader:
- self.type = None
- self.maintype = None
- self.subtype = None
- self.plisttext = ''
- else:
- orig_parsetype(self)
- parsetype.patched = True
+ def parsetype(self):
+ if not self.typeheader:
+ self.type = None
+ self.maintype = None
+ self.subtype = None
+ self.plisttext = ''
+ else:
+ orig_parsetype(self)
+ parsetype.patched = True
+
+ if not getattr(mimetools.Message.parsetype, 'patched', None):
+ mimetools.Message.parsetype = parsetype
- if not getattr(mimetools.Message.parsetype, 'patched', None):
- mimetools.Message.parsetype = parsetype
+ monkey_patch_mimetools = _monkey_patch_mimetools_py2
def get_socket(conf):
diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py
index cc33833..eb13075 100644
--- a/test/unit/common/test_wsgi.py
+++ b/test/unit/common/test_wsgi.py
@@ -17,7 +17,6 @@
import errno
import logging
-import mimetools
import socket
import unittest
import os
@@ -25,11 +24,15 @@ from textwrap import dedent
from collections import defaultdict
from eventlet import listen
+import six
from six import BytesIO
from six import StringIO
from six.moves.urllib.parse import quote
+if six.PY2:
+ import mimetools
import mock
+import nose
import swift.common.middleware.catch_errors
import swift.common.middleware.gatekeeper
@@ -65,12 +68,17 @@ class TestWSGI(unittest.TestCase):
def setUp(self):
utils.HASH_PATH_PREFIX = 'startcap'
- self._orig_parsetype = mimetools.Message.parsetype
+ if six.PY2:
+ self._orig_parsetype = mimetools.Message.parsetype
def tearDown(self):
- mimetools.Message.parsetype = self._orig_parsetype
+ if six.PY2:
+ mimetools.Message.parsetype = self._orig_parsetype
def test_monkey_patch_mimetools(self):
+ if not six.PY2:
+ raise nose.SkipTest('test specific to Python 2')
+
sio = StringIO('blah')
self.assertEqual(mimetools.Message(sio).type, 'text/plain')
sio = StringIO('blah')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment