From 4c54305bee3ed6df897be73c277eb1931d2f197d Mon Sep 17 00:00:00 2001 From: Pablo Hoffman Date: Fri, 9 Jan 2009 10:35:14 +0000 Subject: [PATCH] added docstrings to unicode_to_str and str_to_unicode --HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40693 --- scrapy/trunk/scrapy/utils/python.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/scrapy/trunk/scrapy/utils/python.py b/scrapy/trunk/scrapy/utils/python.py index 3ea5124c8..cd3be792e 100644 --- a/scrapy/trunk/scrapy/utils/python.py +++ b/scrapy/trunk/scrapy/utils/python.py @@ -55,20 +55,32 @@ def unique(list_, key=lambda x: x): def str_to_unicode(text, encoding='utf-8'): + """Return the unicode representation of text in the given encoding. Unlike + .encode(encoding) this function can be applied directly to a unicode + object without the risk of double-decoding problems (which can happen if + you don't use the default 'ascii' encoding) + """ + if isinstance(text, str): return text.decode(encoding) elif isinstance(text, unicode): return text else: - raise TypeError('str_to_unicode can only receive a string object') + raise TypeError('str_to_unicode must receive a str or unicode object, got %s' % type(text).__name__) def unicode_to_str(text, encoding='utf-8'): + """Return the str representation of text in the given encoding. Unlike + .encode(encoding) this function can be applied directly to a str + object without the risk of double-decoding problems (which can happen if + you don't use the default 'ascii' encoding) + """ + if isinstance(text, unicode): return text.encode(encoding) elif isinstance(text, str): return text else: - raise TypeError('unicode_to_str can only receive a unicode object') + raise TypeError('unicode_to_str must receive a unicode or str object, got %s' % type(text).__name__) def re_rsearch(pattern, text, chunk_size=1024): """