From 5244431a4289c86771ec0216421854eac3df1988 Mon Sep 17 00:00:00 2001 From: calvin Date: Tue, 5 Jul 2005 22:06:38 +0000 Subject: [PATCH] add ascii_safe method git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2719 e7d03fd6-7b0d-0410-9947-9c21f3af8025 --- linkcheck/strformat.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/linkcheck/strformat.py b/linkcheck/strformat.py index a346e312..26927ead 100644 --- a/linkcheck/strformat.py +++ b/linkcheck/strformat.py @@ -45,6 +45,21 @@ def unicode_safe (s, encoding="iso-8859-1"): return unicode(str(s), encoding, "ignore") +def ascii_safe (s): + """ + Get ASCII string without raising encoding errors. Unknown + characters of the given encoding will be ignored. + + @param s: the Unicode string to be encoded + @type s: unicode or None + @return: encoded ASCII version of s, or s itself if s evaluated to False + @rtype: string + """ + if s: + s = s.encode('ascii', 'ignore') + return s + + def url_unicode_split (url): """ Like urlparse.urlsplit(), but always returning unicode parts.