From 9650cd6276ae83264291419f9d74f8c73d514358 Mon Sep 17 00:00:00 2001 From: Marcio Faustino Date: Thu, 27 Feb 2014 12:41:02 +0100 Subject: [PATCH] Support `.encode()` keyword arguments for Python 2.6. --- mwparserfromhell/string_mixin.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mwparserfromhell/string_mixin.py b/mwparserfromhell/string_mixin.py index 3599fe4..10c88e3 100644 --- a/mwparserfromhell/string_mixin.py +++ b/mwparserfromhell/string_mixin.py @@ -32,6 +32,15 @@ from .compat import bytes, py3k, str __all__ = ["StringMixIn"] +def inheritdoc(method): + """Set __doc__ of *method* to __doc__ of *method* in its parent class. + + Since this is used on :py:class:`~.StringMixIn`, the "parent class" used is + ``str``. This function can be used as a decorator. + """ + method.__doc__ = getattr(str, method.__name__).__doc__ + return method + class StringMixIn(object): """Implement the interface for ``unicode``/``str`` in a dynamic manner. @@ -99,8 +108,20 @@ class StringMixIn(object): def __contains__(self, item): return str(item) in self.__unicode__() + @inheritdoc + def encode(self, encoding=None, errors=None): + if encoding is None: + encoding = getdefaultencoding() + args = [encoding] + if errors is not None: + args.append(errors) + return self.__unicode__().encode(*args) + def __getattr__(self, attr): return getattr(self.__unicode__(), attr) if py3k: maketrans = str.maketrans # Static method can't rely on __getattr__ + + +del inheritdoc