Author: Manuel Jacob
Branch: refactor-str-types
Changeset: r66128:7b0bb92b8531
Date: 2013-08-13 20:29 +0200
http://bitbucket.org/pypy/pypy/changeset/7b0bb92b8531/
Log: Move some app-level methods of unicode and bytearray from top-level
to under the scope of the app-level classes.
diff --git a/pypy/objspace/std/bytearrayobject.py
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -108,6 +108,57 @@
raise operationerrfmt(space.w_TypeError, msg, len(self.data))
return space.wrap(ord(self.data[0]))
+ @staticmethod
+ def descr_new(space, w_bytearraytype, __args__):
+ return new_bytearray(space, w_bytearraytype, [])
+
+ def descr_reduce(self, space):
+ assert isinstance(self, W_BytearrayObject)
+ w_dict = self.getdict(space)
+ if w_dict is None:
+ w_dict = space.w_None
+ return space.newtuple([
+ space.type(self), space.newtuple([
+ space.wrap(''.join(self.data).decode('latin-1')),
+ space.wrap('latin-1')]),
+ w_dict])
+
+ @staticmethod
+ def descr_fromhex(space, w_bytearraytype, w_hexstring):
+ "bytearray.fromhex(string) -> bytearray\n"
+ "\n"
+ "Create a bytearray object from a string of hexadecimal numbers.\n"
+ "Spaces between two numbers are accepted.\n"
+ "Example: bytearray.fromhex('B9 01EF') ->
bytearray(b'\\xb9\\x01\\xef')."
+ hexstring = space.str_w(w_hexstring)
+ hexstring = hexstring.lower()
+ data = []
+ length = len(hexstring)
+ i = -2
+ while True:
+ i += 2
+ while i < length and hexstring[i] == ' ':
+ i += 1
+ if i >= length:
+ break
+ if i+1 == length:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "non-hexadecimal number found in fromhex() arg at position
%d" % i))
+
+ top = _hex_digit_to_int(hexstring[i])
+ if top == -1:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "non-hexadecimal number found in fromhex() arg at position
%d" % i))
+ bot = _hex_digit_to_int(hexstring[i+1])
+ if bot == -1:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "non-hexadecimal number found in fromhex() arg at position
%d" % (i+1,)))
+ data.append(chr(top*16 + bot))
+
+ # in CPython bytearray.fromhex is a staticmethod, so
+ # we ignore w_type and always return a bytearray
+ return new_bytearray(space, space.w_bytearray, data)
+
def descr_init(self, space, __args__):
# this is on the silly side
w_source, w_encoding, w_errors = __args__.parse_obj(
@@ -294,10 +345,6 @@
return w_obj
-def descr__new__(space, w_bytearraytype, __args__):
- return new_bytearray(space,w_bytearraytype, [])
-
-
def makebytearraydata_w(space, w_source):
# String-like argument
try:
@@ -327,17 +374,6 @@
resizelist_hint(data, extended)
return data
-def descr_bytearray__reduce__(space, w_self):
- assert isinstance(w_self, W_BytearrayObject)
- w_dict = w_self.getdict(space)
- if w_dict is None:
- w_dict = space.w_None
- return space.newtuple([
- space.type(w_self), space.newtuple([
- space.wrap(''.join(w_self.data).decode('latin-1')),
- space.wrap('latin-1')]),
- w_dict])
-
def _hex_digit_to_int(d):
val = ord(d)
if 47 < val < 58:
@@ -346,42 +382,6 @@
return val - 87
return -1
-def descr_fromhex(space, w_type, w_hexstring):
- "bytearray.fromhex(string) -> bytearray\n"
- "\n"
- "Create a bytearray object from a string of hexadecimal numbers.\n"
- "Spaces between two numbers are accepted.\n"
- "Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."
- hexstring = space.str_w(w_hexstring)
- hexstring = hexstring.lower()
- data = []
- length = len(hexstring)
- i = -2
- while True:
- i += 2
- while i < length and hexstring[i] == ' ':
- i += 1
- if i >= length:
- break
- if i+1 == length:
- raise OperationError(space.w_ValueError, space.wrap(
- "non-hexadecimal number found in fromhex() arg at position %d"
% i))
-
- top = _hex_digit_to_int(hexstring[i])
- if top == -1:
- raise OperationError(space.w_ValueError, space.wrap(
- "non-hexadecimal number found in fromhex() arg at position %d"
% i))
- bot = _hex_digit_to_int(hexstring[i+1])
- if bot == -1:
- raise OperationError(space.w_ValueError, space.wrap(
- "non-hexadecimal number found in fromhex() arg at position %d"
% (i+1,)))
- data.append(chr(top*16 + bot))
-
- # in CPython bytearray.fromhex is a staticmethod, so
- # we ignore w_type and always return a bytearray
- return new_bytearray(space, space.w_bytearray, data)
-
-# ____________________________________________________________
W_BytearrayObject.typedef = StdTypeDef(
"bytearray",
@@ -389,10 +389,10 @@
bytearray(sequence) -> bytearray initialized from sequence\'s items
If the argument is a bytearray, the return value is the same object.''',
- __new__ = interp2app(descr__new__),
+ __new__ = interp2app(W_BytearrayObject.descr_new),
__hash__ = None,
- __reduce__ = interp2app(descr_bytearray__reduce__),
- fromhex = interp2app(descr_fromhex, as_classmethod=True),
+ __reduce__ = interp2app(W_BytearrayObject.descr_reduce),
+ fromhex = interp2app(W_BytearrayObject.descr_fromhex, as_classmethod=True),
__repr__ = interp2app(W_BytearrayObject.descr_repr),
__str__ = interp2app(W_BytearrayObject.descr_str),
diff --git a/pypy/objspace/std/unicodeobject.py
b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -140,6 +140,42 @@
def _newlist_unwrapped(self, space, lst):
return space.newlist_unicode(lst)
+ @staticmethod
+ @unwrap_spec(w_string = WrappedDefault(""))
+ def descr_new(space, w_unicodetype, w_string, w_encoding=None,
+ w_errors=None):
+ # NB. the default value of w_obj is really a *wrapped* empty string:
+ # there is gateway magic at work
+ w_obj = w_string
+
+ encoding, errors = _get_encoding_and_errors(space, w_encoding,
w_errors)
+ # convoluted logic for the case when unicode subclass has a __unicode__
+ # method, we need to call this method
+ is_precisely_unicode = space.is_w(space.type(w_obj), space.w_unicode)
+ if (is_precisely_unicode or
+ (space.isinstance_w(w_obj, space.w_unicode) and
+ space.findattr(w_obj, space.wrap('__unicode__')) is None)):
+ if encoding is not None or errors is not None:
+ raise OperationError(space.w_TypeError, space.wrap(
+ 'decoding Unicode is not supported'))
+ if (is_precisely_unicode and
+ space.is_w(w_unicodetype, space.w_unicode)):
+ return w_obj
+ w_value = w_obj
+ else:
+ if encoding is None and errors is None:
+ w_value = unicode_from_object(space, w_obj)
+ else:
+ w_value = unicode_from_encoded_object(space, w_obj,
+ encoding, errors)
+ if space.is_w(w_unicodetype, space.w_unicode):
+ return w_value
+
+ assert isinstance(w_value, W_UnicodeObject)
+ w_newobj = space.allocate_instance(W_UnicodeObject, w_unicodetype)
+ W_UnicodeObject.__init__(w_newobj, w_value._value)
+ return w_newobj
+
def descr_repr(self, space):
chars = self._value
size = len(chars)
@@ -375,44 +411,11 @@
return unicode_from_encoded_object(space, w_str, "ascii", "strict")
-@unwrap_spec(w_string = WrappedDefault(""))
-def descr_new_(space, w_unicodetype, w_string, w_encoding=None, w_errors=None):
- # NB. the default value of w_obj is really a *wrapped* empty string:
- # there is gateway magic at work
- w_obj = w_string
-
- encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors)
- # convoluted logic for the case when unicode subclass has a __unicode__
- # method, we need to call this method
- is_precisely_unicode = space.is_w(space.type(w_obj), space.w_unicode)
- if (is_precisely_unicode or
- (space.isinstance_w(w_obj, space.w_unicode) and
- space.findattr(w_obj, space.wrap('__unicode__')) is None)):
- if encoding is not None or errors is not None:
- raise OperationError(space.w_TypeError,
- space.wrap('decoding Unicode is not
supported'))
- if is_precisely_unicode and space.is_w(w_unicodetype, space.w_unicode):
- return w_obj
- w_value = w_obj
- else:
- if encoding is None and errors is None:
- w_value = unicode_from_object(space, w_obj)
- else:
- w_value = unicode_from_encoded_object(space, w_obj,
- encoding, errors)
- if space.is_w(w_unicodetype, space.w_unicode):
- return w_value
-
- assert isinstance(w_value, W_UnicodeObject)
- w_newobj = space.allocate_instance(W_UnicodeObject, w_unicodetype)
- W_UnicodeObject.__init__(w_newobj, w_value._value)
- return w_newobj
-
# ____________________________________________________________
W_UnicodeObject.typedef = StdTypeDef(
"unicode", basestring_typedef,
- __new__ = interp2app(descr_new_),
+ __new__ = interp2app(W_UnicodeObject.descr_new),
__doc__ = '''unicode(string [, encoding[, errors]]) -> object
Create a new Unicode object from the given encoded string.
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit