示例#1
0
def PyUnicode_GetDefaultEncoding(space):
    """Returns the currently active default encoding."""
    if default_encoding[0] == '\x00':
        encoding = unicodeobject.getdefaultencoding(space)
        i = 0
        while i < len(encoding) and i < DEFAULT_ENCODING_SIZE:
            default_encoding[i] = encoding[i]
            i += 1
    return default_encoding
示例#2
0
def PyUnicode_GetDefaultEncoding(space):
    """Returns the currently active default encoding."""
    if default_encoding[0] == '\x00':
        encoding = unicodeobject.getdefaultencoding(space)
        i = 0
        while i < len(encoding) and i < DEFAULT_ENCODING_SIZE:
            default_encoding[i] = encoding[i]
            i += 1
    return default_encoding
示例#3
0
def find_module(space, w_name, w_path=None):
    name = space.fsencode_w(w_name)
    if space.is_none(w_path):
        w_path = None

    find_info = importing.find_module(space,
                                      name,
                                      w_name,
                                      name,
                                      w_path,
                                      use_loader=False)
    if not find_info:
        raise oefmt(space.w_ImportError, "No module named %s", name)

    w_filename = space.fsdecode(space.wrapbytes(find_info.filename))
    stream = find_info.stream

    if stream is not None:
        encoding = None
        if find_info.modtype == importing.PY_SOURCE:
            # try to find the declared encoding
            top = stream.readline()
            top += stream.readline()
            stream.seek(0, 0)  # reset position
            stream.flush()
            encoding = pyparse._check_for_encoding(top)
            if encoding is None:
                encoding = unicodeobject.getdefaultencoding(space)
        #
        # in python2, both CPython and PyPy pass the filename to
        # open(). However, CPython 3 just passes the fd, so the returned file
        # object doesn't have a name attached. We do the same in PyPy, because
        # there is no easy way to attach the filename -- too bad
        fd = stream.try_to_find_file_descriptor()
        try:
            w_fileobj = interp_io.open(space,
                                       space.wrap(fd),
                                       find_info.filemode,
                                       encoding=encoding)
        except OperationError as e:
            if e.match(space, space.w_LookupError):
                raise OperationError(space.w_SyntaxError,
                                     space.str(e.get_w_value(space)))
            raise
    else:
        w_fileobj = space.w_None
    w_import_info = space.newtuple([
        space.wrap(find_info.suffix),
        space.wrap(find_info.filemode),
        space.wrap(find_info.modtype)
    ])
    return space.newtuple([w_fileobj, w_filename, w_import_info])
示例#4
0
def find_module(space, w_name, w_path=None):
    name = space.fsencode_w(w_name)
    if space.is_none(w_path):
        w_path = None

    find_info = importing.find_module(
        space, name, w_name, name, w_path, use_loader=False)
    if not find_info:
        raise oefmt(space.w_ImportError, "No module named %s", name)

    w_filename = space.fsdecode(space.wrapbytes(find_info.filename))
    stream = find_info.stream

    if stream is not None:
        encoding = None
        if find_info.modtype == importing.PY_SOURCE:
            # try to find the declared encoding
            top = stream.readline()
            top += stream.readline()
            stream.seek(0, 0) # reset position
            stream.flush()
            encoding = pyparse._check_for_encoding(top)
            if encoding is None:
                encoding = unicodeobject.getdefaultencoding(space)
        #
        # in python2, both CPython and PyPy pass the filename to
        # open(). However, CPython 3 just passes the fd, so the returned file
        # object doesn't have a name attached. We do the same in PyPy, because
        # there is no easy way to attach the filename -- too bad
        fd = stream.try_to_find_file_descriptor()
        try:
            w_fileobj = interp_io.open(space, space.wrap(fd),
                                       find_info.filemode, encoding=encoding)
        except OperationError as e:
            if e.match(space, space.w_LookupError):
                raise OperationError(space.w_SyntaxError,
                                     space.str(e.get_w_value(space)))
            raise
    else:
        w_fileobj = space.w_None
    w_import_info = space.newtuple(
        [space.wrap(find_info.suffix),
         space.wrap(find_info.filemode),
         space.wrap(find_info.modtype)])
    return space.newtuple([w_fileobj, w_filename, w_import_info])
示例#5
0
 def unicode_w(self, space):
     # Use the default encoding.
     encoding = getdefaultencoding(space)
     return space.unicode_w(decode_object(space, self, encoding, None))
示例#6
0
 def unicode_w(self, space):
     # Use the default encoding.
     encoding = getdefaultencoding(space)
     return space.unicode_w(decode_object(space, self, encoding, None))