コード例 #1
0
ファイル: bytearrayobject.py プロジェクト: mozillazg/pypy
    def descr_init(self, space, w_source=None, encoding=None, errors=None):
        if w_source is None:
            w_source = space.wrap('')
        if encoding is not None:
            from pypy.objspace.std.unicodeobject import encode_object
            # if w_source is an integer this correctly raises a
            # TypeError the CPython error message is: "encoding or
            # errors without a string argument" ours is: "expected
            # unicode, got int object"
            w_source = encode_object(space, w_source, encoding, errors)

        # Is it an int?
        try:
            count = space.int_w(w_source)
        except OperationError as e:
            if not e.match(space, space.w_TypeError):
                raise
        else:
            if count < 0:
                raise oefmt(space.w_ValueError, "bytearray negative count")
            self.data = resizable_list_supporting_raw_ptr(['\0'] * count)
            return

        data = makebytearraydata_w(space, w_source)
        self.data = resizable_list_supporting_raw_ptr(data)
コード例 #2
0
ファイル: unicodeobject.py プロジェクト: mozillazg/pypy
 def PyUnicode_AsXXXString(space, w_unicode):
     """Encode a Unicode object and return the result as Python
     string object.  Error handling is "strict".  Return NULL if an
     exception was raised by the codec."""
     if not PyUnicode_Check(space, w_unicode):
         PyErr_BadArgument(space)
     return unicodeobject.encode_object(space, w_unicode, encoding, "strict")
コード例 #3
0
ファイル: unicodeobject.py プロジェクト: mozillazg/pypy
def PyUnicode_AsUnicodeEscapeString(space, w_unicode):
    """Encode a Unicode object using Unicode-Escape and return the result as Python
    string object.  Error handling is "strict". Return NULL if an exception was
    raised by the codec."""
    if not PyUnicode_Check(space, w_unicode):
        PyErr_BadArgument(space)

    return unicodeobject.encode_object(space, w_unicode, 'unicode-escape', 'strict')
コード例 #4
0
ファイル: unicodeobject.py プロジェクト: mozillazg/pypy
def PyUnicode_AsEncodedObject(space, w_unicode, llencoding, llerrors):
    """Encode a Unicode object and return the result as Python object.
    encoding and errors have the same meaning as the parameters of the same name
    in the Unicode encode() method. The codec to be used is looked up using
    the Python codec registry. Return NULL if an exception was raised by the
    codec."""
    if not PyUnicode_Check(space, w_unicode):
        PyErr_BadArgument(space)

    encoding = errors = None
    if llencoding:
        encoding = rffi.charp2str(llencoding)
    if llerrors:
        errors = rffi.charp2str(llerrors)
    return unicodeobject.encode_object(space, w_unicode, encoding, errors)
コード例 #5
0
def PyUnicode_AsEncodedObject(space, w_unicode, llencoding, llerrors):
    """Encode a Unicode object and return the result as Python object.
    encoding and errors have the same meaning as the parameters of the same name
    in the Unicode encode() method. The codec to be used is looked up using
    the Python codec registry. Return NULL if an exception was raised by the
    codec."""
    if not PyUnicode_Check(space, w_unicode):
        PyErr_BadArgument(space)

    encoding = errors = None
    if llencoding:
        encoding = rffi.charp2str(llencoding)
    if llerrors:
        errors = rffi.charp2str(llerrors)
    return unicodeobject.encode_object(space, w_unicode, encoding, errors)
コード例 #6
0
def PyUnicode_AsUTF8AndSize(space, ref, psize):
    if not PyUnicode_Check(space, ref):
        PyErr_BadArgument(space)
    if not get_ready(ref):
        res = _PyUnicode_Ready(space, ref)

    if not get_utf8(ref):
        # Copy unicode buffer
        w_unicode = from_ref(space, ref)
        w_encoded = unicodeobject.encode_object(space, w_unicode, "utf-8",
                                                "strict")
        s = space.bytes_w(w_encoded)
        set_utf8(ref, rffi.str2charp(s))
        set_utf8_len(ref, len(s))
    if psize:
        psize[0] = get_utf8_len(ref)
    return get_utf8(ref)
コード例 #7
0
ファイル: bytearrayobject.py プロジェクト: Darriall/pypy
    def descr_init(self, space, __args__):
        # this is on the silly side
        w_source, w_encoding, w_errors = __args__.parse_obj(
                None, 'bytearray', init_signature, init_defaults)

        if w_source is None:
            w_source = space.wrap('')
        if w_encoding is None:
            w_encoding = space.w_None
        if w_errors is None:
            w_errors = space.w_None

        # Unicode argument
        if not space.is_w(w_encoding, space.w_None):
            from pypy.objspace.std.unicodeobject import (
                _get_encoding_and_errors, encode_object
            )
            encoding, errors = _get_encoding_and_errors(space, w_encoding,
                                                        w_errors)

            # if w_source is an integer this correctly raises a
            # TypeError the CPython error message is: "encoding or
            # errors without a string argument" ours is: "expected
            # unicode, got int object"
            w_source = encode_object(space, w_source, encoding, errors)

        # Is it an int?
        try:
            count = space.int_w(w_source)
        except OperationError as e:
            if not e.match(space, space.w_TypeError):
                raise
        else:
            if count < 0:
                raise oefmt(space.w_ValueError, "bytearray negative count")
            self.data = ['\0'] * count
            return

        data = makebytearraydata_w(space, w_source)
        self.data = data
コード例 #8
0
ファイル: bytearrayobject.py プロジェクト: zielmicha/pypy
    def descr_init(self, space, __args__):
        # this is on the silly side
        w_source, w_encoding, w_errors = __args__.parse_obj(
            None, 'bytearray', init_signature, init_defaults)

        if w_source is None:
            w_source = space.wrap('')
        if w_encoding is None:
            w_encoding = space.w_None
        if w_errors is None:
            w_errors = space.w_None

        # Unicode argument
        if not space.is_w(w_encoding, space.w_None):
            from pypy.objspace.std.unicodeobject import (
                _get_encoding_and_errors, encode_object)
            encoding, errors = _get_encoding_and_errors(
                space, w_encoding, w_errors)

            # if w_source is an integer this correctly raises a
            # TypeError the CPython error message is: "encoding or
            # errors without a string argument" ours is: "expected
            # unicode, got int object"
            w_source = encode_object(space, w_source, encoding, errors)

        # Is it an int?
        try:
            count = space.int_w(w_source)
        except OperationError as e:
            if not e.match(space, space.w_TypeError):
                raise
        else:
            if count < 0:
                raise oefmt(space.w_ValueError, "bytearray negative count")
            self.data = ['\0'] * count
            return

        data = makebytearraydata_w(space, w_source)
        self.data = data
コード例 #9
0
ファイル: unicodehelper.py プロジェクト: Qointum/pypy
def encode(space, w_data, encoding=None, errors='strict'):
    from pypy.objspace.std.unicodeobject import encode_object
    return encode_object(space, w_data, encoding, errors)
コード例 #10
0
ファイル: stringmethods.py プロジェクト: sota/pypy-old
 def descr_encode(self, space, w_encoding=None, w_errors=None):
     from pypy.objspace.std.unicodeobject import (_get_encoding_and_errors,
                                                  encode_object)
     encoding, errors = _get_encoding_and_errors(space, w_encoding,
                                                 w_errors)
     return encode_object(space, self, encoding, errors)
コード例 #11
0
 def encode_unicode_object(self, w_unicode, encoding, errors):
     from pypy.objspace.std.unicodeobject import encode_object
     return encode_object(self, w_unicode, encoding, errors)
コード例 #12
0
def HPyUnicode_AsUTF8String(space, ctx, h):
    w_unicode = handles.deref(space, h)
    # XXX: what should we do if w_unicode is not a str?
    w_bytes = unicodeobject.encode_object(space, w_unicode, 'utf-8', 'strict')
    return handles.new(space, w_bytes)
コード例 #13
0
ファイル: stringmethods.py プロジェクト: Darriall/pypy
 def descr_encode(self, space, w_encoding=None, w_errors=None):
     from pypy.objspace.std.unicodeobject import (
         _get_encoding_and_errors, encode_object)
     encoding, errors = _get_encoding_and_errors(space, w_encoding,
                                                 w_errors)
     return encode_object(space, self, encoding, errors)
コード例 #14
0
        if count < 0:
            raise OperationError(space.w_ValueError,
                                 space.wrap("negative count"))
        if encoding is not None or errors is not None:
            raise OperationError(
                space.w_TypeError,
                space.wrap("encoding or errors without string argument"))
        return ['\0'] * count
    # Unicode with encoding
    if space.isinstance_w(w_source, space.w_unicode):
        if encoding is None:
            raise OperationError(
                space.w_TypeError,
                space.wrap("string argument without an encoding"))
        from pypy.objspace.std.unicodeobject import encode_object
        w_source = encode_object(space, w_source, encoding, errors)
        # and continue with the encoded string

    return makebytesdata_w(space, w_source)


def makebytesdata_w(space, w_source):
    w_bytes_method = space.lookup(w_source, "__bytes__")
    if w_bytes_method is not None:
        w_bytes = space.get_and_call_function(w_bytes_method, w_source)
        if not space.isinstance_w(w_bytes, space.w_bytes):
            raise oefmt(space.w_TypeError,
                        "__bytes__ returned non-bytes (type '%T')", w_bytes)
        return [c for c in space.bytes_w(w_bytes)]

    # String-like argument
コード例 #15
0
ファイル: objspace.py プロジェクト: mozillazg/pypy
    def encode_unicode_object(self, w_unicode, encoding, errors):
        from pypy.objspace.std.unicodeobject import encode_object

        return encode_object(self, w_unicode, encoding, errors)
コード例 #16
0
ファイル: bytesobject.py プロジェクト: Qointum/pypy
            raise
    else:
        if count < 0:
            raise OperationError(space.w_ValueError,
                                 space.wrap("negative count"))
        if encoding is not None or errors is not None:
            raise OperationError(space.w_TypeError, space.wrap(
                    "encoding or errors without string argument"))
        return ['\0'] * count
    # Unicode with encoding
    if space.isinstance_w(w_source, space.w_unicode):
        if encoding is None:
            raise OperationError(space.w_TypeError, space.wrap(
                    "string argument without an encoding"))
        from pypy.objspace.std.unicodeobject import encode_object
        w_source = encode_object(space, w_source, encoding, errors)
        # and continue with the encoded string

    return makebytesdata_w(space, w_source)

def makebytesdata_w(space, w_source):
    w_bytes_method = space.lookup(w_source, "__bytes__")
    if w_bytes_method is not None:
        w_bytes = space.get_and_call_function(w_bytes_method, w_source)
        if not space.isinstance_w(w_bytes, space.w_bytes):
            raise oefmt(space.w_TypeError,
                        "__bytes__ returned non-bytes (type '%T')", w_bytes)
        return [c for c in space.bytes_w(w_bytes)]

    # String-like argument
    try: