]> git.phdru.name Git - mimedecode.git/commitdiff
Replace email.message._formatparam with _formatparam from Python 2.7
authorOleg Broytman <phd@phdru.name>
Fri, 12 May 2017 18:30:06 +0000 (21:30 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 12 May 2017 18:30:06 +0000 (21:30 +0300)
Avoid re-encoding non-ascii params.

formatparam_27.py [new file with mode: 0644]
mimedecode.py
mimedecode_version.py
setup.py

diff --git a/formatparam_27.py b/formatparam_27.py
new file mode 100644 (file)
index 0000000..9abbebe
--- /dev/null
@@ -0,0 +1,26 @@
+from email import message
+from email import utils
+
+
+def _formatparam(param, value=None, quote=True):
+    """This is _formatparam from Python 2.7"""
+    if value is not None and len(value) > 0:
+        # A tuple is used for RFC 2231 encoded parameter values where items
+        # are (charset, language, value).  charset is a string, not a Charset
+        # instance.
+        if isinstance(value, tuple):
+            # Encode as per RFC 2231
+            param += '*'
+            value = utils.encode_rfc2231(value[2], value[0], value[1])
+        # BAW: Please check this.  I think that if quote is set it should
+        # force quoting even if not necessary.
+        if quote or message.tspecials.search(value):
+            return '%s="%s"' % (param, utils.quote(value))
+        else:
+            return '%s=%s' % (param, value)
+    else:
+        return param
+
+
+# Replace with this _formatparam to avoid re-encoding non-ascii params
+message._formatparam = _formatparam
index 5f3321c22fa73529108230a79516436cfa78e1cc..5d58333e51350c03077d90a053e29137a20e18d2 100755 (executable)
@@ -4,6 +4,10 @@
 import sys, os
 from mimedecode_version import __version__, \
     __author__, __copyright__, __license__
+if sys.version_info[0] >= 3:
+    # Replace email.message._formatparam with _formatparam from Python 2.7
+    # to avoid re-encoding non-ascii params.
+    import formatparam_27
 
 me = os.path.basename(sys.argv[0])
 
index e55025704b7c68c71f5ffd3e726492a575bf3773..d9184dc744b2c70cc14cc9d6dea43ad3c187b292 100644 (file)
@@ -1,4 +1,4 @@
-__version__ = "2.7.0"
+__version__ = "2.8.0a0"
 __author__ = "Oleg Broytman <phd@phdru.name>"
 __copyright__ = "Copyright (C) 2001-2017 PhiloSoft Design"
 __license__ = "GNU GPL"
index 88892d16265a46b4403b9133a5cc2e929a1b9461..e10082566e703ec9fc182c3f7fd1c237ee5f1985 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -34,7 +34,7 @@ setup(name = "mimedecode",
         'Programming Language :: Python :: 2.7',
         'Programming Language :: Python :: 2 :: Only',
     ],
-    py_modules = ['mimedecode_version'],
+    py_modules = ['formatparam_27', 'mimedecode_version'],
     scripts = ['mimedecode.py'],
     **kw
 )