[pypy-svn] r48665 - pypy/branch/more-unicode-improvements/pypy/objspace/std
cfbolz at codespeak.net
cfbolz at codespeak.net
Tue Nov 13 21:22:19 CET 2007
Author: cfbolz
Date: Tue Nov 13 21:22:19 2007
New Revision: 48665
Modified:
pypy/branch/more-unicode-improvements/pypy/objspace/std/unicodeobject.py
pypy/branch/more-unicode-improvements/pypy/objspace/std/unicodetype.py
Log:
go through a bit less machinery when mixing strings and unicode strings in the
same operation.
Modified: pypy/branch/more-unicode-improvements/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/branch/more-unicode-improvements/pypy/objspace/std/unicodeobject.py (original)
+++ pypy/branch/more-unicode-improvements/pypy/objspace/std/unicodeobject.py Tue Nov 13 21:22:19 2007
@@ -26,6 +26,12 @@
def unwrap(w_self, space):
# for testing
return w_self._value
+
+ def create_if_subclassed(w_self):
+ if type(w_self) is W_UnicodeObject:
+ return w_self
+ return W_UnicodeObject(w_self._value)
+
W_UnicodeObject.EMPTY = W_UnicodeObject(u'')
registerimplementation(W_UnicodeObject)
@@ -59,7 +65,8 @@
# string-to-unicode delegation
def delegate_String2Unicode(space, w_str):
- w_uni = space.call_function(space.w_unicode, w_str)
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ w_uni = unicode_from_string(space, w_str)
assert isinstance(w_uni, W_UnicodeObject) # help the annotator!
return w_uni
@@ -92,17 +99,20 @@
return W_UnicodeObject(w_left._value + w_right._value)
def add__String_Unicode(space, w_left, w_right):
- return space.add(space.call_function(space.w_unicode, w_left) , w_right)
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.add(unicode_from_string(space, w_left) , w_right)
add__Rope_Unicode = add__String_Unicode
def add__Unicode_String(space, w_left, w_right):
- return space.add(w_left, space.call_function(space.w_unicode, w_right))
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.add(w_left, unicode_from_string(space, w_right))
add__Unicode_Rope = add__Unicode_String
def contains__String_Unicode(space, w_container, w_item):
- return space.contains(space.call_function(space.w_unicode, w_container), w_item )
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.contains(unicode_from_string(space, w_container), w_item )
contains__Rope_Unicode = contains__String_Unicode
@@ -311,8 +321,9 @@
def unicode_strip__Unicode_Unicode(space, w_self, w_chars):
return _strip(space, w_self, w_chars, 1, 1)
def unicode_strip__Unicode_String(space, w_self, w_chars):
+ from pypy.objspace.std.unicodetype import unicode_from_string
return space.call_method(w_self, 'strip',
- space.call_function(space.w_unicode, w_chars))
+ unicode_from_string(space, w_chars))
unicode_strip__Unicode_Rope = unicode_strip__Unicode_String
def unicode_lstrip__Unicode_None(space, w_self, w_chars):
@@ -320,8 +331,9 @@
def unicode_lstrip__Unicode_Unicode(space, w_self, w_chars):
return _strip(space, w_self, w_chars, 1, 0)
def unicode_lstrip__Unicode_String(space, w_self, w_chars):
+ from pypy.objspace.std.unicodetype import unicode_from_string
return space.call_method(w_self, 'lstrip',
- space.call_function(space.w_unicode, w_chars))
+ unicode_from_string(space, w_chars))
unicode_lstrip__Unicode_Rope = unicode_lstrip__Unicode_String
@@ -330,8 +342,9 @@
def unicode_rstrip__Unicode_Unicode(space, w_self, w_chars):
return _strip(space, w_self, w_chars, 0, 1)
def unicode_rstrip__Unicode_String(space, w_self, w_chars):
+ from pypy.objspace.std.unicodetype import unicode_from_string
return space.call_method(w_self, 'rstrip',
- space.call_function(space.w_unicode, w_chars))
+ unicode_from_string(space, w_chars))
unicode_rstrip__Unicode_Rope = unicode_rstrip__Unicode_String
@@ -481,7 +494,7 @@
fillchar = _to_unichar_w(space, w_fillchar)
padding = width - len(self)
if padding < 0:
- return space.call_function(space.w_unicode, w_self)
+ return w_self.create_if_subclassed()
leftpad = padding // 2 + (padding & width & 1)
result = [fillchar] * width
for i in range(len(self)):
@@ -494,7 +507,7 @@
fillchar = _to_unichar_w(space, w_fillchar)
padding = width - len(self)
if padding < 0:
- return space.call_function(space.w_unicode, w_self)
+ return w_self.create_if_subclassed()
result = [fillchar] * width
for i in range(len(self)):
result[i] = self[i]
@@ -506,7 +519,7 @@
fillchar = _to_unichar_w(space, w_fillchar)
padding = width - len(self)
if padding < 0:
- return space.call_function(space.w_unicode, w_self)
+ return w_self.create_if_subclassed()
result = [fillchar] * width
for i in range(len(self)):
result[padding + i] = self[i]
@@ -519,7 +532,7 @@
return W_UnicodeObject(u'0' * width)
padding = width - len(self)
if padding <= 0:
- return space.call_function(space.w_unicode, w_self)
+ return w_self.create_if_subclassed()
result = [u'0'] * width
for i in range(len(self)):
result[padding + i] = self[i]
@@ -948,47 +961,58 @@
from pypy.objspace.std.stringobject import W_StringObject
from pypy.objspace.std.ropeobject import W_RopeObject
def str_strip__String_Unicode(space, w_self, w_chars):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'strip', w_chars)
str_strip__Rope_Unicode = str_strip__String_Unicode
def str_lstrip__String_Unicode(space, w_self, w_chars):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'lstrip', w_chars)
str_lstrip__Rope_Unicode = str_lstrip__String_Unicode
def str_rstrip__String_Unicode(space, w_self, w_chars):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'rstrip', w_chars)
str_rstrip__Rope_Unicode = str_rstrip__String_Unicode
def str_count__String_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'count', w_substr, w_start, w_end)
str_count__Rope_Unicode_ANY_ANY = str_count__String_Unicode_ANY_ANY
def str_find__String_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'find', w_substr, w_start, w_end)
str_find__Rope_Unicode_ANY_ANY = str_find__String_Unicode_ANY_ANY
def str_rfind__String_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'rfind', w_substr, w_start, w_end)
str_rfind__Rope_Unicode_ANY_ANY = str_rfind__String_Unicode_ANY_ANY
def str_index__String_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'index', w_substr, w_start, w_end)
str_index__Rope_Unicode_ANY_ANY = str_index__String_Unicode_ANY_ANY
def str_rindex__String_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'rindex', w_substr, w_start, w_end)
str_rindex__Rope_Unicode_ANY_ANY = str_rindex__String_Unicode_ANY_ANY
def str_replace__String_Unicode_Unicode_ANY(space, w_self, w_old, w_new, w_maxsplit):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'replace', w_old, w_new, w_maxsplit)
str_replace__Rope_Unicode_Unicode_ANY = str_replace__String_Unicode_Unicode_ANY
def str_split__String_Unicode_ANY(space, w_self, w_delim, w_maxsplit):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'split', w_delim, w_maxsplit)
str_split__Rope_Unicode_ANY = str_split__String_Unicode_ANY
def str_rsplit__String_Unicode_ANY(space, w_self, w_delim, w_maxsplit):
- return space.call_method(space.call_function(space.w_unicode, w_self),
+ from pypy.objspace.std.unicodetype import unicode_from_string
+ return space.call_method(unicode_from_string(space, w_self),
'rsplit', w_delim, w_maxsplit)
str_rsplit__Rope_Unicode_ANY = str_rsplit__String_Unicode_ANY
register_all(vars(), stringtype)
Modified: pypy/branch/more-unicode-improvements/pypy/objspace/std/unicodetype.py
==============================================================================
--- pypy/branch/more-unicode-improvements/pypy/objspace/std/unicodetype.py (original)
+++ pypy/branch/more-unicode-improvements/pypy/objspace/std/unicodetype.py Tue Nov 13 21:22:19 2007
@@ -219,7 +219,7 @@
from pypy.objspace.std.unicodeobject import W_UnicodeObject
encoding = getdefaultencoding(space)
if encoding != 'ascii':
- return unicode_from_object(space, w_str)
+ return unicode_from_encoded_object(space, w_str, encoding, "strict")
s = space.str_w(w_str)
try:
return W_UnicodeObject(s.decode("ascii"))
More information about the Pypy-commit
mailing list