On Fri, May 25, Malcolm Tredinnick wrote:
> Oh! I'd forgotten that guy was still there. :-(
Fine. I had absolutely no idea why this was there ;-)
I had another problem with force_unicode: It would turn
any SafeString into an unsafe Unicode. I was not
able to overwrite SafeString.__unicode__ (python seems
not to use __unicode__() when you call unicode() with an
encoding).
Thus, I modified force_unicode to use decode() instead,
and added proxy functions to SafeString. The code is
below.
Do you see any problems with this approach? I'm aware that I should
try to share the _proxy_method from SafeUnicode somehow, but
I'm still looking for the best way to do it.
Cheers,
Michael
class SafeString(str, SafeData):
# ...
def _proxy_method(self, *args, **kwargs):
"""
Wrap a call to a normal unicode method up so that we return safe
results. The method that is being wrapped is passed in the 'method'
argument.
"""
method = kwargs.pop('method')
data = method(self, *args, **kwargs)
if isinstance(data, str):
return SafeString(data)
else:
return SafeUnicode(data)
encode = curry(_proxy_method, method = str.encode)
decode = curry(_proxy_method, method = str.decode)
def force_unicode(s, encoding='utf-8', errors='strict'):
"""
Similar to smart_unicode, except that lazy instances are resolved to
strings, rather than kept as lazy objects.
"""
if not isinstance(s, basestring,):
if hasattr(s, '__unicode__'):
s = unicode(s)
else:
s = unicode(str(s), encoding, errors)
elif not isinstance(s, unicode):
s = s.decode(encoding, errors)
return s
--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100
http://www.noris.de - The IT-Outsourcing Company
Vorstand: Ingo Kraupa (Vorsitzender), Joachim Astel, Hansjochen Klenk -
Vorsitzender des Aufsichtsrats: Stefan Schnabel - AG Nürnberg HRB 17689
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---