Pārlūkot izejas kodu

Handle keyword arguments in some methods with py3k correctly.

tags/v0.2
Ben Kurtovic pirms 11 gadiem
vecāks
revīzija
49b9863b77
2 mainītis faili ar 54 papildinājumiem un 24 dzēšanām
  1. +46
    -24
      mwparserfromhell/string_mixin.py
  2. +8
    -0
      tests/test_string_mixin.py

+ 46
- 24
mwparserfromhell/string_mixin.py Parādīt failu

@@ -143,19 +143,21 @@ class StringMixIn(object):
if not py3k:
@inheritdoc
def decode(self, encoding=None, errors=None):
if errors is None:
if encoding is None:
return self.__unicode__().decode()
return self.__unicode__().decode(encoding)
return self.__unicode__().decode(encoding, errors)
kwargs = {}
if encoding is not None:
kwargs["encoding"] = encoding
if errors is not None:
kwargs["errors"] = errors
return self.__unicode__().decode(**kwargs)

@inheritdoc
def encode(self, encoding=None, errors=None):
if errors is None:
if encoding is None:
return self.__unicode__().encode()
return self.__unicode__().encode(encoding)
return self.__unicode__().encode(encoding, errors)
kwargs = {}
if encoding is not None:
kwargs["encoding"] = encoding
if errors is not None:
kwargs["errors"] = errors
return self.__unicode__().encode(**kwargs)

@inheritdoc
def endswith(self, prefix, start=None, end=None):
@@ -286,25 +288,45 @@ class StringMixIn(object):
def rpartition(self, sep):
return self.__unicode__().rpartition(sep)

@inheritdoc
def rsplit(self, sep=None, maxsplit=None):
if maxsplit is None:
if sep is None:
return self.__unicode__().rsplit()
return self.__unicode__().rsplit(sep)
return self.__unicode__().rsplit(sep, maxsplit)
if py3k:
@inheritdoc
def rsplit(self, sep=None, maxsplit=None):
kwargs = {}
if sep is not None:
kwargs["sep"] = sep
if maxsplit is not None:
kwargs["maxsplit"] = maxsplit
return self.__unicode__().rsplit(**kwargs)
else:
@inheritdoc
def rsplit(self, sep=None, maxsplit=None):
if maxsplit is None:
if sep is None:
return self.__unicode__().rsplit()
return self.__unicode__().rsplit(sep)
return self.__unicode__().rsplit(sep, maxsplit)

@inheritdoc
def rstrip(self, chars=None):
return self.__unicode__().rstrip(chars)

@inheritdoc
def split(self, sep=None, maxsplit=None):
if maxsplit is None:
if sep is None:
return self.__unicode__().split()
return self.__unicode__().split(sep)
return self.__unicode__().split(sep, maxsplit)
if py3k:
@inheritdoc
def split(self, sep=None, maxsplit=None):
kwargs = {}
if sep is not None:
kwargs["sep"] = sep
if maxsplit is not None:
kwargs["maxsplit"] = maxsplit
return self.__unicode__().split(**kwargs)
else:
@inheritdoc
def split(self, sep=None, maxsplit=None):
if maxsplit is None:
if sep is None:
return self.__unicode__().split()
return self.__unicode__().split(sep)
return self.__unicode__().split(sep, maxsplit)

@inheritdoc
def splitlines(self, keepends=None):


+ 8
- 0
tests/test_string_mixin.py Parādīt failu

@@ -189,10 +189,14 @@ class TestStringMixIn(unittest.TestCase):
self.assertEquals(b"fake string", str1.encode())
self.assertEquals(b"\xF0\x90\x8C\xB2\xF0\x90\x8C\xBF\xF0\x90\x8D\x84",
str3.encode("utf8"))
self.assertEquals(b"\xF0\x90\x8C\xB2\xF0\x90\x8C\xBF\xF0\x90\x8D\x84",
str3.encode(encoding="utf8"))
self.assertRaises(UnicodeEncodeError, str3.encode)
self.assertRaises(UnicodeEncodeError, str3.encode, "ascii")
self.assertRaises(UnicodeEncodeError, str3.encode, "ascii", "strict")
self.assertRaises(UnicodeEncodeError, str3.encode, errors="strict")
self.assertEquals("", str3.encode("ascii", "ignore"))
self.assertEquals("", str3.encode(errors="ignore"))

self.assertTrue(str1.endswith("ing"))
self.assertFalse(str1.endswith("ingh"))
@@ -358,6 +362,8 @@ class TestStringMixIn(unittest.TestCase):
self.assertEquals(actual, str25.rsplit(None, 3))
actual = [" this is a sentence with", "", "whitespace", ""]
self.assertEquals(actual, str25.rsplit(" ", 3))
if py3k:
self.assertEquals(actual, str25.rsplit(maxsplit=3))

self.assertEquals("fake string", str1.rstrip())
self.assertEquals(" fake string", str23.rstrip())
@@ -373,6 +379,8 @@ class TestStringMixIn(unittest.TestCase):
self.assertEquals(actual, str25.split(None, 3))
actual = ["", "", "", "this is a sentence with whitespace "]
self.assertEquals(actual, str25.split(" ", 3))
if py3k:
self.assertEquals(actual, str25.split(maxsplit=3))

str26 = _FakeString("lines\nof\ntext\r\nare\r\npresented\nhere")
self.assertEquals(["lines", "of", "text", "are", "presented", "here"],


Notiek ielāde…
Atcelt
Saglabāt