Esempio n. 1
0
def find_module(space, w_name, w_path=None):
    name = space.text0_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 hay módulo llamado %s", name)

    w_filename = space.newtext(find_info.filename)
    stream = find_info.stream

    if stream is not None:
        fileobj = W_File(space)
        fileobj.fdopenstream(stream, stream.try_to_find_file_descriptor(),
                             find_info.filemode, w_filename)
        w_fileobj = fileobj
    else:
        w_fileobj = space.w_None
    w_import_info = space.newtuple([
        space.newtext(find_info.suffix),
        space.newtext(find_info.filemode),
        space.newint(find_info.modtype)
    ])
    return space.newtuple([w_fileobj, w_filename, w_import_info])
Esempio n. 2
0
def find_module(space, w_name, w_path=None):
    name = space.str0_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.wrap(find_info.filename)
    stream = find_info.stream

    if stream is not None:
        fileobj = W_File(space)
        fileobj.fdopenstream(
            stream, stream.try_to_find_file_descriptor(),
            find_info.filemode, w_filename)
        w_fileobj = space.wrap(fileobj)
    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])
Esempio n. 3
0
def find_module(space, w_name, w_path=None):
    name = space.str0_w(w_name)
    if space.is_w(w_path, space.w_None):
        w_path = None

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

    w_filename = space.wrap(find_info.filename)
    stream = find_info.stream

    if stream is not None:
        fileobj = W_File(space)
        fileobj.fdopenstream(stream, stream.try_to_find_file_descriptor(),
                             find_info.filemode, w_filename)
        w_fileobj = space.wrap(fileobj)
    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])
Esempio n. 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])
Esempio n. 5
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])