From: Oleg Broytman Date: Fri, 12 May 2017 18:30:06 +0000 (+0300) Subject: Replace email.message._formatparam with _formatparam from Python 2.7 X-Git-Tag: 2.8.0~43 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=141754c6212c218c538dcadd2d79da4b5bb0fc5e;p=mimedecode.git Replace email.message._formatparam with _formatparam from Python 2.7 Avoid re-encoding non-ascii params. --- diff --git a/formatparam_27.py b/formatparam_27.py new file mode 100644 index 0000000..9abbebe --- /dev/null +++ b/formatparam_27.py @@ -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 diff --git a/mimedecode.py b/mimedecode.py index 5f3321c..5d58333 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -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]) diff --git a/mimedecode_version.py b/mimedecode_version.py index e550257..d9184dc 100644 --- a/mimedecode_version.py +++ b/mimedecode_version.py @@ -1,4 +1,4 @@ -__version__ = "2.7.0" +__version__ = "2.8.0a0" __author__ = "Oleg Broytman " __copyright__ = "Copyright (C) 2001-2017 PhiloSoft Design" __license__ = "GNU GPL" diff --git a/setup.py b/setup.py index 88892d1..e100825 100755 --- 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 )