Esempio n. 1
0
def fsdecode(space, w_string):
    from pypy.module._codecs import interp_codecs
    state = space.fromcache(interp_codecs.CodecState)
    if _WIN32:
        bytes = space.bytes_w(w_string)
        uni = str_decode_mbcs(bytes,
                              len(bytes),
                              'strict',
                              errorhandler=decode_error_handler(space),
                              force_ignore=False)[0]
    elif _MACOSX:
        bytes = space.bytes_w(w_string)
        uni = runicode.str_decode_utf_8_impl(
            bytes,
            len(bytes),
            'surrogateescape',
            final=True,
            errorhandler=state.decode_error_handler,
            allow_surrogates=False)[0]
    elif space.sys.filesystemencoding is None or state.codec_need_encodings:
        # bootstrap check: if the filesystemencoding isn't initialized
        # or the filesystem codec is implemented in Python we cannot
        # use it before the codecs are ready. use the locale codec
        # instead
        from pypy.module._codecs.locale import (
            str_decode_locale_surrogateescape)
        bytes = space.bytes_w(w_string)
        uni = str_decode_locale_surrogateescape(
            bytes, errorhandler=decode_error_handler(space))
    else:
        from pypy.module.sys.interp_encoding import getfilesystemencoding
        return space.call_method(w_string, 'decode',
                                 getfilesystemencoding(space),
                                 space.newtext('surrogateescape'))
    return space.newunicode(uni)
Esempio n. 2
0
def _getimporter(space, w_pathitem):
    # 'imp._getimporter' is somewhat like CPython's get_path_importer
    w_path_importer_cache = space.sys.get("path_importer_cache")
    w_importer = space.finditem(w_path_importer_cache, w_pathitem)
    if w_importer is None:
        space.setitem(w_path_importer_cache, w_pathitem, space.w_None)
        for w_hook in space.unpackiterable(space.sys.get("path_hooks")):
            w_pathbytes = w_pathitem
            if space.isinstance_w(w_pathitem, space.w_unicode):
                from pypy.module.sys.interp_encoding import getfilesystemencoding
                w_pathbytes = space.call_method(space.w_unicode, 'encode',
                                     w_pathitem, getfilesystemencoding(space))
            try:
                w_importer = space.call_function(w_hook, w_pathbytes)
            except OperationError as e:
                if not e.match(space, space.w_ImportError):
                    raise
            else:
                break
        if w_importer is None:
            try:
                w_importer = space.call_function(
                    space.gettypefor(W_NullImporter), w_pathitem
                )
            except OperationError as e:
                if e.match(space, space.w_ImportError):
                    return None
                raise
        if space.is_true(w_importer):
            space.setitem(w_path_importer_cache, w_pathitem, w_importer)
    return w_importer
Esempio n. 3
0
def listdir(space, w_dirname):
    """Return a list containing the names of the entries in the directory.

\tpath: path of directory to list

The list is in arbitrary order.  It does not include the special
entries '.' and '..' even if they are present in the directory."""
    try:
        if space.isinstance_w(w_dirname, space.w_unicode):
            dirname = FileEncoder(space, w_dirname)
            result = rposix.listdir(dirname)
            # NOTE: 'result' can be either a list of str or a list of
            # unicodes, depending on the platform
            w_fs_encoding = getfilesystemencoding(space)
            len_result = len(result)
            result_w = [None] * len_result
            for i in range(len_result):
                res = result[i]
                if isinstance(res, str):
                    w_bytes = space.newtext(res)
                    try:
                        w_res = space.call_method(w_bytes, "decode",
                                                  w_fs_encoding)
                    except OperationError as e:
                        # fall back to the original byte string
                        if e. async (space):
                            raise
                        w_res = w_bytes
                elif isinstance(res, unicode):
                    w_res = u2utf8(space, res)
                else:
                    assert False
                result_w[i] = w_res
            return space.newlist(result_w)
Esempio n. 4
0
def fsdecode(space, w_string):
    state = space.fromcache(interp_codecs.CodecState)
    if _WIN32:
        bytes = space.bytes_w(w_string)
        uni = str_decode_mbcs(bytes, len(bytes), 'strict',
                              errorhandler=decode_error_handler(space),
                              force_ignore=False)[0]
    elif _MACOSX:
        bytes = space.bytes_w(w_string)
        uni = runicode.str_decode_utf_8(
            bytes, len(bytes), 'surrogateescape',
            errorhandler=state.decode_error_handler)[0]
    elif state.codec_need_encodings:
        # bootstrap check: if the filesystem codec is implemented in
        # Python we cannot use it before the codecs are ready. use the
        # locale codec instead
        from pypy.module._codecs.locale import (
            str_decode_locale_surrogateescape)
        bytes = space.bytes_w(w_string)
        uni = str_decode_locale_surrogateescape(
            bytes, errorhandler=decode_error_handler(space))
    else:
        from pypy.module.sys.interp_encoding import getfilesystemencoding
        return space.call_method(w_string, 'decode',
                                 getfilesystemencoding(space),
                                 space.wrap('surrogateescape'))
    return space.wrap(uni)
Esempio n. 5
0
def fsencode(space, w_uni):
    from pypy.module._codecs import interp_codecs
    state = space.fromcache(interp_codecs.CodecState)
    if _WIN32:
        uni = space.unicode_w(w_uni)
        bytes = unicode_encode_mbcs(uni,
                                    len(uni),
                                    'strict',
                                    errorhandler=encode_error_handler(space),
                                    force_replace=False)
    elif _MACOSX:
        uni = space.unicode_w(w_uni)
        bytes = runicode.unicode_encode_utf_8_impl(
            uni,
            len(uni),
            'surrogateescape',
            errorhandler=state.encode_error_handler,
            allow_surrogates=False)
    elif space.sys.filesystemencoding is None or state.codec_need_encodings:
        # bootstrap check: if the filesystemencoding isn't initialized
        # or the filesystem codec is implemented in Python we cannot
        # use it before the codecs are ready. use the locale codec
        # instead
        from pypy.module._codecs.locale import (
            unicode_encode_locale_surrogateescape)
        uni = space.unicode_w(w_uni)
        if u'\x00' in uni:
            raise oefmt(space.w_ValueError, "embedded null character")
        bytes = unicode_encode_locale_surrogateescape(
            uni, errorhandler=encode_error_handler(space))
    else:
        from pypy.module.sys.interp_encoding import getfilesystemencoding
        return space.call_method(w_uni, 'encode', getfilesystemencoding(space),
                                 space.newtext('surrogateescape'))
    return space.newbytes(bytes)
Esempio n. 6
0
def listdir(space, w_dirname):
    """Return a list containing the names of the entries in the directory.

\tpath: path of directory to list

The list is in arbitrary order.  It does not include the special
entries '.' and '..' even if they are present in the directory."""
    try:
        if space.isinstance_w(w_dirname, space.w_unicode):
            dirname = FileEncoder(space, w_dirname)
            result = rposix.listdir(dirname)
            w_fs_encoding = getfilesystemencoding(space)
            len_result = len(result)
            result_w = [None] * len_result
            for i in range(len_result):
                w_bytes = space.wrap(result[i])
                try:
                    result_w[i] = space.call_method(w_bytes,
                                                    "decode", w_fs_encoding)
                except OperationError as e:
                    # fall back to the original byte string
                    if e.async(space):
                        raise
                    result_w[i] = w_bytes
            return space.newlist(result_w)
        else:
Esempio n. 7
0
def fsencode(space, w_uni):
    state = space.fromcache(interp_codecs.CodecState)
    if _WIN32:
        uni = space.unicode_w(w_uni)
        bytes = unicode_encode_mbcs(uni, len(uni), 'strict',
                                    errorhandler=encode_error_handler(space),
                                    force_replace=False)
    elif _MACOSX:
        uni = space.unicode_w(w_uni)
        bytes = runicode.unicode_encode_utf_8(
            uni, len(uni), 'surrogateescape',
            errorhandler=state.encode_error_handler)
    elif state.codec_need_encodings:
        # bootstrap check: if the filesystem codec is implemented in
        # Python we cannot use it before the codecs are ready. use the
        # locale codec instead
        from pypy.module._codecs.locale import (
            unicode_encode_locale_surrogateescape)
        uni = space.unicode_w(w_uni)
        bytes = unicode_encode_locale_surrogateescape(
            uni, errorhandler=encode_error_handler(space))
    else:
        from pypy.module.sys.interp_encoding import getfilesystemencoding
        return space.call_method(w_uni, 'encode',
                                 getfilesystemencoding(space),
                                 space.wrap('surrogateescape'))
    return space.wrapbytes(bytes)
Esempio n. 8
0
def listdir(space, w_dirname):
    """Return a list containing the names of the entries in the directory.

\tpath: path of directory to list

The list is in arbitrary order.  It does not include the special
entries '.' and '..' even if they are present in the directory."""
    try:
        if space.isinstance_w(w_dirname, space.w_unicode):
            dirname = FileEncoder(space, w_dirname)
            result = rposix.listdir(dirname)
            w_fs_encoding = getfilesystemencoding(space)
            result_w = [space.call_method(space.wrap(s), "decode", w_fs_encoding) for s in result]
        else:
            dirname = space.str_w(w_dirname)
            result = rposix.listdir(dirname)
            result_w = [space.wrap(s) for s in result]
    except OSError, e:
        raise wrap_oserror2(space, e, w_dirname)
Esempio n. 9
0
def listdir(space, w_dirname):
    """Return a list containing the names of the entries in the directory.

\tpath: path of directory to list

The list is in arbitrary order.  It does not include the special
entries '.' and '..' even if they are present in the directory."""
    try:
        if space.isinstance_w(w_dirname, space.w_unicode):
            dirname = FileEncoder(space, w_dirname)
            result = rposix.listdir(dirname)
            w_fs_encoding = getfilesystemencoding(space)
            result_w = [
                space.call_method(space.wrap(s), "decode", w_fs_encoding)
                for s in result
            ]
        else:
            dirname = space.str_w(w_dirname)
            result = rposix.listdir(dirname)
            result_w = [space.wrap(s) for s in result]
    except OSError, e:
        raise wrap_oserror2(space, e, w_dirname)
Esempio n. 10
0
def listdir(space, w_dirname):
    """Return a list containing the names of the entries in the directory.

\tpath: path of directory to list

The list is in arbitrary order.  It does not include the special
entries '.' and '..' even if they are present in the directory."""
    try:
        if space.isinstance_w(w_dirname, space.w_unicode):
            dirname = FileEncoder(space, w_dirname)
            result = rposix.listdir(dirname)
            w_fs_encoding = getfilesystemencoding(space)
            len_result = len(result)
            result_w = [None] * len_result
            for i in range(len_result):
                w_bytes = space.wrap(result[i])
                try:
                    result_w[i] = space.call_method(w_bytes, "decode",
                                                    w_fs_encoding)
                except OperationError, e:
                    # fall back to the original byte string
                    result_w[i] = w_bytes
        else:
Esempio n. 11
0
 def getcwdu(space):
     """Return the current working directory as a unicode string."""
     w_filesystemencoding = getfilesystemencoding(space)
     return space.call_method(getcwd(space), 'decode', w_filesystemencoding)
Esempio n. 12
0
 def as_unicode(self):
     space = self.space
     w_unicode = space.call_method(self.w_obj, 'decode',
                                   getfilesystemencoding(space))
     return space.unicode_w(w_unicode)
Esempio n. 13
0
def fsencode_w(space, w_obj):
    if space.isinstance_w(w_obj, space.w_unicode):
        w_obj = space.call_method(w_obj, 'encode',
                                  getfilesystemencoding(space))
    return space.str_w(w_obj)
Esempio n. 14
0
 def as_unicode(self):
     space = self.space
     w_unicode = space.call_method(self.w_obj, 'decode',
                                   getfilesystemencoding(space))
     return space.unicode_w(w_unicode)
Esempio n. 15
0
def fsencode_w(space, w_obj):
    if space.isinstance_w(w_obj, space.w_unicode):
        w_obj = space.call_method(w_obj, 'encode',
                                  getfilesystemencoding(space))
    return space.str_w(w_obj)
Esempio n. 16
0
 def as_bytes(self):
     from pypy.module.sys.interp_encoding import getfilesystemencoding
     space = self.space
     w_bytes = space.call_method(self.w_obj, 'encode',
                                 getfilesystemencoding(space))
     return space.str_w(w_bytes)
Esempio n. 17
0
 def as_unicode(self):
     from pypy.module.sys.interp_encoding import getfilesystemencoding
     space = self.space
     w_unicode = space.call_method(self.w_obj, 'decode',
                                   getfilesystemencoding(space))
     return space.unicode_w(w_unicode)
Esempio n. 18
0
 def getcwdu(space):
     """Return the current working directory as a unicode string."""
     w_filesystemencoding = getfilesystemencoding(space)
     return space.call_method(getcwd(space), 'decode',
                              w_filesystemencoding)