From 141754c6212c218c538dcadd2d79da4b5bb0fc5e Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 12 May 2017 21:30:06 +0300 Subject: [PATCH] Replace email.message._formatparam with _formatparam from Python 2.7 Avoid re-encoding non-ascii params. --- formatparam_27.py | 26 ++++++++++++++++++++++++++ mimedecode.py | 4 ++++ mimedecode_version.py | 2 +- setup.py | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 formatparam_27.py 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 ) -- 2.39.5