瀏覽代碼

Support `.encode()` keyword arguments for Python 2.6.

tags/v0.3.3
Marcio Faustino 10 年之前
父節點
當前提交
9650cd6276
共有 1 個文件被更改,包括 21 次插入0 次删除
  1. +21
    -0
      mwparserfromhell/string_mixin.py

+ 21
- 0
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

Loading…
取消
儲存