Beispiel #1
0
def _realize_c_struct_or_union(ffi, sindex):
    s = ffi.ctxobj.ctx.c_struct_unions[sindex]
    type_index = rffi.getintfield(s, 'c_type_index')
    if ffi.cached_types[type_index] is not None:
        return ffi.cached_types[type_index] #found already in the "primary" slot

    space = ffi.space
    w_ctype = None
    c_flags = rffi.getintfield(s, 'c_flags')
    c_first_field_index = rffi.getintfield(s, 'c_first_field_index')
    if (c_flags & cffi_opcode.F_EXTERNAL) == 0:
        if (c_flags & cffi_opcode.F_UNION) != 0:
            name = _realize_name("union ", s.c_name)
            x = ctypestruct.W_CTypeUnion(space, name)
        else:
            name = _realize_name("struct ", s.c_name)
            x = ctypestruct.W_CTypeStruct(space, name)
        if (c_flags & cffi_opcode.F_OPAQUE) == 0:
            assert c_first_field_index >= 0
            w_ctype = x
            w_ctype.size = rffi.getintfield(s, 'c_size')
            w_ctype.alignment = rffi.getintfield(s, 'c_alignment')
            # w_ctype._field_list and other underscore fields are still
            # None, making it a "lazy" (i.e. "non-forced") kind of struct
            w_ctype._lazy_ffi = ffi
            w_ctype._lazy_s = s
        else:
            assert c_first_field_index < 0
    else:
        assert c_first_field_index < 0
        x = _fetch_external_struct_or_union(s, ffi.included_ffis_libs)
        if x is None:
            raise oefmt(ffi.w_FFIError,
                    "'%s %s' should come from ffi.include() but was not found",
                    "union" if c_flags & cffi_opcode.F_UNION else "struct",
                    rffi.charp2str(s.c_name))
        assert isinstance(x, ctypestruct.W_CTypeStructOrUnion)
        if (c_flags & cffi_opcode.F_OPAQUE) == 0 and x.size < 0:
            prefix = "union" if c_flags & cffi_opcode.F_UNION else "struct"
            name = rffi.charp2str(s.c_name)
            raise oefmt(space.w_NotImplementedError,
                    "'%s %s' is opaque in the ffi.include(), but no "
                    "longer in the ffi doing the include (workaround: don't "
                    "use ffi.include() but duplicate the declarations of "
                    "everything using %s %s)",
                    prefix, name, prefix, name)

    # Update the "primary" OP_STRUCT_UNION slot
    ffi.cached_types[type_index] = x

    if w_ctype is not None and rffi.getintfield(s, 'c_size') == -2:
        # oops, this struct is unnamed and we couldn't generate
        # a C expression to get its size.  We have to rely on
        # complete_struct_or_union() to compute it now.
        try:
            do_realize_lazy_struct(w_ctype)
        except:
            ffi.cached_types[type_index] = None
            raise
    return x
Beispiel #2
0
def PyModule_Create2(space, module, api_version):
    """Create a new module object, given the definition in module, assuming the
    API version module_api_version.  If that version does not match the version
    of the running interpreter, a RuntimeWarning is emitted.
    
    Most uses of this function should be using PyModule_Create()
    instead; only use this if you are sure you need it."""

    modname = rffi.charp2str(module.c_m_name)
    if module.c_m_doc:
        doc = rffi.charp2str(module.c_m_doc)
    else:
        doc = None
    methods = module.c_m_methods

    state = space.fromcache(State)
    f_name, f_path = state.package_context
    if f_name is not None:
        modname = f_name
    w_mod = space.wrap(Module(space, space.wrap(modname)))
    state.package_context = None, None

    if f_path is not None:
        dict_w = {'__file__': space.wrap(f_path)}
    else:
        dict_w = {}
    convert_method_defs(space, dict_w, methods, None, w_mod, modname)
    for key, w_value in dict_w.items():
        space.setattr(w_mod, space.wrap(key), w_value)
    if doc:
        space.setattr(w_mod, space.wrap("__doc__"),
                      space.wrap(doc))
    return w_mod
Beispiel #3
0
def _Py_InitPyPyModule(space, name, methods, doc, w_self, apiver):
    """
    Create a new module object based on a name and table of functions, returning
    the new module object. If doc is non-NULL, it will be used to define the
    docstring for the module. If self is non-NULL, it will passed to the
    functions of the module as their (otherwise NULL) first parameter. (This was
    added as an experimental feature, and there are no known uses in the current
    version of Python.) For apiver, the only value which should be passed is
    defined by the constant PYTHON_API_VERSION.

    Note that the name parameter is actually ignored, and the module name is
    taken from the package_context attribute of the cpyext.State in the space
    cache.  CPython includes some extra checking here to make sure the module
    being initialized lines up with what's expected, but we don't.
    """
    from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
    modname = rffi.charp2str(name)
    state = space.fromcache(State)
    f_name, f_path = state.package_context
    if f_name is not None:
        modname = f_name
    w_mod = PyImport_AddModule(space, modname)
    state.package_context = None, None

    if f_path is not None:
        dict_w = {'__file__': space.wrap(f_path)}
    else:
        dict_w = {}
    convert_method_defs(space, dict_w, methods, None, w_self, modname)
    for key, w_value in dict_w.items():
        space.setattr(w_mod, space.wrap(key), w_value)
    if doc:
        space.setattr(w_mod, space.wrap("__doc__"),
                      space.wrap(rffi.charp2str(doc)))
    return w_mod   # borrowed result kept alive in PyImport_AddModule()
Beispiel #4
0
def create_module(filename):
    ''' Returns the W_Module representing an LLVM module created with the
        contents of the given file. '''

    module = llwrap.LLVMModuleCreateWithName("module")
    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as out_message:
        with lltype.scoped_alloc(rffi.VOIDP.TO, 1) as mem_buff:
            with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as mem_buff_ptr:
                mem_buff_ptr[0] = mem_buff
                rc = llwrap.LLVMCreateMemoryBufferWithContentsOfFile(filename,
                                                                     mem_buff_ptr,
                                                                     out_message)
                if rc != 0:
                    print "[ERROR]: Cannot create memory buffer with contents of"\
                          " %s: %s.\n" % (filename, rffi.charp2str(out_message[0]))
                    raise InvalidFileException(filename)
                mem_buff = mem_buff_ptr[0]
            with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as module_ptr:
                module_ptr[0] = module
                rc = llwrap.LLVMParseBitcode(mem_buff, module_ptr, out_message)
                if rc != 0:
                    print "[ERROR]: Cannot parse %s: %s.\n" % (filename,
                                                               rffi.charp2str(out_message[0]))
                    raise UnparsableBitcodeException(filename)
                module = module_ptr[0]
    return W_Module(module)
Beispiel #5
0
def PyCodec_IncrementalEncoder(space, encoding, errors):
    w_codec = interp_codecs.lookup_codec(space, rffi.charp2str(encoding))
    if errors:
        w_errors = space.wrap(rffi.charp2str(errors))
        return space.call_method(w_codec, "incrementalencoder", w_errors)
    else:
        return space.call_method(w_codec, "incrementalencoder")
Beispiel #6
0
    def test_encode_decimal(self, space, api):
        with rffi.scoped_unicode2wcharp(u' (12, 35 ABC)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                res = api.PyUnicode_EncodeDecimal(u, 13, buf.raw, None)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == ' (12, 35 ABC)'

        with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                res = api.PyUnicode_EncodeDecimal(u, 9, buf.raw, None)
        assert res == -1
        api.PyErr_Clear()

        with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with rffi.scoped_str2charp("replace") as errors:
                    res = api.PyUnicode_EncodeDecimal(u, 9, buf.raw,
                                                      errors)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == " (12, ??)"

        with rffi.scoped_unicode2wcharp(u'12\u1234') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with rffi.scoped_str2charp("xmlcharrefreplace") as errors:
                    res = api.PyUnicode_EncodeDecimal(u, 3, buf.raw,
                                                      errors)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == "12&#4660;"
Beispiel #7
0
    def __init__(self, space, pto):
        bases_w = space.fixedview(from_ref(space, pto.c_tp_bases))
        dict_w = {}

        add_operators(space, dict_w, pto)
        convert_method_defs(space, dict_w, pto.c_tp_methods, self)
        convert_getset_defs(space, dict_w, pto.c_tp_getset, self)
        convert_member_defs(space, dict_w, pto.c_tp_members, self)

        name = rffi.charp2str(pto.c_tp_name)
        flag_heaptype = pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE
        if flag_heaptype:
            minsize = rffi.sizeof(PyHeapTypeObject.TO)
        else:
            minsize = rffi.sizeof(PyObject.TO)
        new_layout = (pto.c_tp_basicsize > minsize or pto.c_tp_itemsize > 0)

        W_TypeObject.__init__(self, space, name,
            bases_w or [space.w_object], dict_w, force_new_layout=new_layout,
            is_heaptype=flag_heaptype)
        self.flag_cpytype = True
        # if a sequence or a mapping, then set the flag to force it
        if pto.c_tp_as_sequence and pto.c_tp_as_sequence.c_sq_item:
            self.flag_map_or_seq = 'S'
        elif (pto.c_tp_as_mapping and pto.c_tp_as_mapping.c_mp_subscript and
              not (pto.c_tp_as_sequence and pto.c_tp_as_sequence.c_sq_slice)):
            self.flag_map_or_seq = 'M'
        if pto.c_tp_doc:
            self.w_doc = space.wrap(rffi.charp2str(pto.c_tp_doc))
Beispiel #8
0
    def getdefaultlocale():
        encoding = "cp%d" % GetACP()

        BUFSIZE = 50
        buf_lang = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor='raw')
        buf_country = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor='raw')

        try:
            if (GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT,
                              cConfig.LOCALE_SISO639LANGNAME,
                              buf_lang, BUFSIZE) and
                GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT,
                              cConfig.LOCALE_SISO3166CTRYNAME,
                              buf_country, BUFSIZE)):
                lang = rffi.charp2str(buf_lang)
                country = rffi.charp2str(buf_country)
                language = "%s_%s" % (lang, country)

            # If we end up here, this windows version didn't know about
            # ISO639/ISO3166 names (it's probably Windows 95).  Return the
            # Windows language identifier instead (a hexadecimal number)
            elif GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT,
                               cConfig.LOCALE_IDEFAULTLANGUAGE,
                               buf_lang, BUFSIZE):
                lang = rffi.charp2str(buf_lang)
                language = "0x%s" % (lang,)

            else:
                language = None
        finally:
            lltype.free(buf_lang, flavor='raw')
            lltype.free(buf_country, flavor='raw')

        return language, encoding
Beispiel #9
0
    def descr_list_types(self):
        """\
Returns the user type names known to this FFI instance.
This returns a tuple containing three lists of names:
(typedef_names, names_of_structs, names_of_unions)"""
        #
        space = self.space
        ctx = self.ctxobj.ctx

        lst1_w = []
        for i in range(rffi.getintfield(ctx, "c_num_typenames")):
            s = rffi.charp2str(ctx.c_typenames[i].c_name)
            lst1_w.append(space.wrap(s))

        lst2_w = []
        lst3_w = []
        for i in range(rffi.getintfield(ctx, "c_num_struct_unions")):
            su = ctx.c_struct_unions[i]
            if su.c_name[0] == "$":
                continue
            s = rffi.charp2str(su.c_name)
            if rffi.getintfield(su, "c_flags") & cffi_opcode.F_UNION:
                lst_w = lst3_w
            else:
                lst_w = lst2_w
            lst_w.append(space.wrap(s))

        return space.newtuple([space.newlist(lst1_w), space.newlist(lst2_w), space.newlist(lst3_w)])
Beispiel #10
0
def PyUnicode_FromEncodedObject(space, w_obj, encoding, errors):
    """Coerce an encoded object obj to an Unicode object and return a reference with
    incremented refcount.

    String and other char buffer compatible objects are decoded according to the
    given encoding and using the error handling defined by errors.  Both can be
    NULL to have the interface use the default values (see the next section for
    details).

    All other objects, including Unicode objects, cause a TypeError to be
    set."""
    if not encoding:
        raise oefmt(space.w_TypeError, "decoding Unicode is not supported")
    w_encoding = space.wrap(rffi.charp2str(encoding))
    if errors:
        w_errors = space.wrap(rffi.charp2str(errors))
    else:
        w_errors = None

    # - unicode is disallowed
    # - raise TypeError for non-string types
    if space.isinstance_w(w_obj, space.w_unicode):
        w_meth = None
    else:
        try:
            w_meth = space.getattr(w_obj, space.wrap('decode'))
        except OperationError as e:
            if not e.match(space, space.w_AttributeError):
                raise
            w_meth = None
    if w_meth is None:
        raise oefmt(space.w_TypeError, "decoding Unicode is not supported")
    return space.call_function(w_meth, w_encoding, w_errors)
Beispiel #11
0
def Py_FindMethod(space, table, w_obj, name_ptr):
    """Return a bound method object for an extension type implemented in
    C.  This can be useful in the implementation of a tp_getattro or
    tp_getattr handler that does not use the PyObject_GenericGetAttr()
    function.
    """
    # XXX handle __doc__

    name = rffi.charp2str(name_ptr)
    methods = rffi.cast(rffi.CArrayPtr(PyMethodDef), table)
    method_list_w = []

    if methods:
        i = -1
        while True:
            i = i + 1
            method = methods[i]
            if not method.c_ml_name:
                break
            if name == "__methods__":
                method_list_w.append(
                    space.wrap(rffi.charp2str(method.c_ml_name)))
            elif rffi.charp2str(method.c_ml_name) == name: # XXX expensive copy
                return space.wrap(W_PyCFunctionObject(space, method, w_obj))
    if name == "__methods__":
        return space.newlist(method_list_w)
    raise OperationError(space.w_AttributeError, space.wrap(name))
Beispiel #12
0
    def dcgettext(space, w_domain, msg, category):
        """dcgettext(domain, msg, category) -> string
        Return translation of msg in domain and category."""

        if space.is_w(w_domain, space.w_None):
            domain = None
            msg_c = rffi.str2charp(msg)
            try:
                result = _dcgettext(domain, msg_c, rffi.cast(rffi.INT, category))
                # note that 'result' may be the same pointer as 'msg_c',
                # so it must be converted to an RPython string *before*
                # we free msg_c.
                result = rffi.charp2str(result)
            finally:
                rffi.free_charp(msg_c)
        else:
            domain = space.str_w(w_domain)
            domain_c = rffi.str2charp(domain)
            msg_c = rffi.str2charp(msg)
            try:
                result = _dcgettext(domain_c, msg_c,
                                    rffi.cast(rffi.INT, category))
                # note that 'result' may be the same pointer as 'msg_c',
                # so it must be converted to an RPython string *before*
                # we free msg_c.
                result = rffi.charp2str(result)
            finally:
                rffi.free_charp(domain_c)
                rffi.free_charp(msg_c)

        return space.wrap(result)
Beispiel #13
0
def PyFile_FromString(space, filename, mode):
    """
    On success, return a new file object that is opened on the file given by
    filename, with a file mode given by mode, where mode has the same
    semantics as the standard C routine fopen().  On failure, return NULL."""
    w_filename = space.wrap(rffi.charp2str(filename))
    w_mode = space.wrap(rffi.charp2str(mode))
    return space.call_method(space.builtin, 'file', w_filename, w_mode)
Beispiel #14
0
def _realize_name(prefix, charp_src_name):
    # "xyz" => "struct xyz"
    # "$xyz" => "xyz"
    # "$1" => "struct $1"
    if charp_src_name[0] == "$" and charp_src_name[1] != "$" and not ("0" <= charp_src_name[1] <= "9"):
        return rffi.charp2str(rffi.ptradd(charp_src_name, 1))
    else:
        return prefix + rffi.charp2str(charp_src_name)
Beispiel #15
0
def _realize_name(prefix, charp_src_name):
    # "xyz" => "struct xyz"
    # "$xyz" => "xyz"
    # "$1" => "struct $1"
    if (charp_src_name[0] == '$' and charp_src_name[1] != '$'
            and not ('0' <= charp_src_name[1] <= '9')):
        return rffi.charp2str(rffi.ptradd(charp_src_name, 1))
    else:
        return prefix + rffi.charp2str(charp_src_name)
Beispiel #16
0
def _ssl_seterror(space, ss, ret):
    assert ret <= 0

    if ss is None:
        errval = libssl_ERR_peek_last_error()
        errstr = rffi.charp2str(libssl_ERR_error_string(errval, None))
        return ssl_error(space, errstr, errval)
    elif ss.ssl:
        err = libssl_SSL_get_error(ss.ssl, ret)
    else:
        err = SSL_ERROR_SSL
    errstr = ""
    errval = 0

    if err == SSL_ERROR_ZERO_RETURN:
        errstr = "TLS/SSL connection has been closed"
        errval = PY_SSL_ERROR_ZERO_RETURN
    elif err == SSL_ERROR_WANT_READ:
        errstr = "The operation did not complete (read)"
        errval = PY_SSL_ERROR_WANT_READ
    elif err == SSL_ERROR_WANT_WRITE:
        errstr = "The operation did not complete (write)"
        errval = PY_SSL_ERROR_WANT_WRITE
    elif err == SSL_ERROR_WANT_X509_LOOKUP:
        errstr = "The operation did not complete (X509 lookup)"
        errval = PY_SSL_ERROR_WANT_X509_LOOKUP
    elif err == SSL_ERROR_WANT_CONNECT:
        errstr = "The operation did not complete (connect)"
        errval = PY_SSL_ERROR_WANT_CONNECT
    elif err == SSL_ERROR_SYSCALL:
        e = libssl_ERR_get_error()
        if e == 0:
            if ret == 0 or space.is_w(ss.w_socket, space.w_None):
                errstr = "EOF occurred in violation of protocol"
                errval = PY_SSL_ERROR_EOF
            elif ret == -1:
                # the underlying BIO reported an I/0 error
                error = rsocket.last_error()
                return interp_socket.converted_error(space, error)
            else:
                errstr = "Some I/O error occurred"
                errval = PY_SSL_ERROR_SYSCALL
        else:
            errstr = rffi.charp2str(libssl_ERR_error_string(e, None))
            errval = PY_SSL_ERROR_SYSCALL
    elif err == SSL_ERROR_SSL:
        e = libssl_ERR_get_error()
        errval = PY_SSL_ERROR_SSL
        if e != 0:
            errstr = rffi.charp2str(libssl_ERR_error_string(e, None))
        else:
            errstr = "A failure in the SSL library occurred"
    else:
        errstr = "Invalid error code"
        errval = PY_SSL_ERROR_INVALID_ERROR_CODE

    return ssl_error(space, errstr, errval)
Beispiel #17
0
def _unpack_fields(entry):
    if not entry.c_name:
        return None
    name = rffi.charp2str(entry.c_name)
    _type = int(entry.c_type)
    offset = float(entry.c_gmtoffset)
    fullname = ""
    if entry.c_full_tz_name:
        fullname = rffi.charp2str(entry.c_full_tz_name)
    return TzAbbrEntry(name, _type, offset, fullname)
Beispiel #18
0
def _build_user_info(space, res):
    return space.new_array_from_pairs([
        (space.newstr("name"), space.newstr(rffi.charp2str(res.c_pw_name))),
        (space.newstr("passwd"), space.newstr(rffi.charp2str(res.c_pw_passwd))),
        (space.newstr("uid"), space.newint(intmask(res.c_pw_uid))),
        (space.newstr("gid"), space.newint(intmask(res.c_pw_gid))),
        (space.newstr("gecos"), space.newstr(rffi.charp2str(res.c_pw_gecos))),
        (space.newstr("dir"), space.newstr(rffi.charp2str(res.c_pw_dir))),
        (space.newstr("shell"), space.newstr(rffi.charp2str(res.c_pw_shell))),
    ])
Beispiel #19
0
def _build_group_info(space, res):
    members = []
    for member in rffi.charpp2liststr(res.c_gr_mem):
        members.append(space.newstr(member))

    return space.new_array_from_pairs([
        (space.newstr("name"), space.newstr(rffi.charp2str(res.c_gr_name))),
        (space.newstr("passwd"), space.newstr(rffi.charp2str(res.c_gr_passwd))),
        (space.newstr("members"), space.new_array_from_list(members)),
        (space.newstr("gid"), space.newint(intmask(res.c_gr_gid))),
    ])
Beispiel #20
0
def _get_common_types(space, w_dict):
    from pypy.module._cffi_backend.parse_c_type import ll_enum_common_types
    index = 0
    while True:
        p = ll_enum_common_types(rffi.cast(rffi.INT, index))
        if not p:
            break
        key = rffi.charp2str(p)
        value = rffi.charp2str(rffi.ptradd(p, len(key) + 1))
        space.setitem_str(w_dict, key, space.wrap(value))
        index += 1
Beispiel #21
0
def PyImport_ExecCodeModuleEx(space, name, w_code, pathname):
    """Like PyImport_ExecCodeModule(), but the __file__ attribute of
    the module object is set to pathname if it is non-NULL."""
    code = space.interp_w(PyCode, w_code)
    w_name = space.wrap(rffi.charp2str(name))
    if pathname:
        pathname = rffi.charp2str(pathname)
    else:
        pathname = code.co_filename
    w_mod = importing.add_module(space, w_name)
    space.setattr(w_mod, space.wrap('__file__'), space.wrap(pathname))
    return importing.exec_code_module(space, w_mod, code, w_name)
Beispiel #22
0
 def __init__(self, getset, w_type):
     self.getset = getset
     self.name = rffi.charp2str(getset.c_name)
     self.w_type = w_type
     doc = set = get = None
     if doc:
         doc = rffi.charp2str(getset.c_doc)
     if getset.c_get:
         get = GettersAndSetters.getter.im_func
     if getset.c_set:
         set = GettersAndSetters.setter.im_func
     GetSetProperty.__init__(self, get, set, None, doc, cls=None, use_closure=True, tag="cpyext_1")
Beispiel #23
0
def PyFile_FromFile(space, fp, name, mode, close):
    """Create a new PyFileObject from the already-open standard C file
    pointer, fp.  The function close will be called when the file should be
    closed.  Return NULL on failure."""
    if close:
        raise oefmt(space.w_NotImplementedError, 
            'PyFromFile(..., close) with close function not implemented')
    w_ret = space.allocate_instance(W_File, space.gettypefor(W_File))
    w_ret.w_name = space.wrap(rffi.charp2str(name))
    w_ret.check_mode_ok(rffi.charp2str(mode))
    w_ret.fp = fp
    return w_ret
Beispiel #24
0
def _get_timezone(interp, this):
    zone_type = rffi.cast(lltype.Signed, this.timelib_time.c_zone_type)

    if zone_type == timelib.ZONETYPE_ID:
        return interp.space.wrap(
            rffi.charp2str(this.timezone.timelib_timezone.c_name)
        )
    elif zone_type == timelib.ZONETYPE_ABBR:
        return interp.space.wrap(
            rffi.charp2str(this.timelib_time.c_tz_abbr)
        )
    elif zone_type == timelib.ZONETYPE_OFFSET:
        pass
Beispiel #25
0
def PyUnicode_Decode(space, s, size, encoding, errors):
    """Create a Unicode object by decoding size bytes of the encoded string s.
    encoding and errors have the same meaning as the parameters of the same name
    in the unicode() built-in function.  The codec to be used is looked up
    using the Python codec registry.  Return NULL if an exception was raised by
    the codec."""
    w_str = space.wrap(rffi.charpsize2str(s, size))
    w_encoding = space.wrap(rffi.charp2str(encoding))
    if errors:
        w_errors = space.wrap(rffi.charp2str(errors))
    else:
        w_errors = space.w_None
    return space.call_method(w_str, 'decode', w_encoding, w_errors)
Beispiel #26
0
 def __init__(self, member, w_type):
     self.member = member
     self.name = rffi.charp2str(member.c_name)
     self.w_type = w_type
     flags = rffi.cast(lltype.Signed, member.c_flags)
     doc = set = None
     if member.c_doc:
         doc = rffi.charp2str(member.c_doc)
     get = GettersAndSetters.member_getter.im_func
     del_ = GettersAndSetters.member_delete.im_func
     if not (flags & structmemberdefs.READONLY):
         set = GettersAndSetters.member_setter.im_func
     GetSetProperty.__init__(self, get, set, del_, doc, cls=None, use_closure=True, tag="cpyext_2")
Beispiel #27
0
def make_struct_passwd(space, pw):
    w_passwd_struct = space.getattr(space.getbuiltinmodule('pwd'),
                                    space.wrap('struct_passwd'))
    w_tuple = space.newtuple([
        space.wrap(rffi.charp2str(pw.c_pw_name)),
        space.wrap(rffi.charp2str(pw.c_pw_passwd)),
        space.int(space.wrap(pw.c_pw_uid)),
        space.int(space.wrap(pw.c_pw_gid)),
        space.wrap(rffi.charp2str(pw.c_pw_gecos)),
        space.wrap(rffi.charp2str(pw.c_pw_dir)),
        space.wrap(rffi.charp2str(pw.c_pw_shell)),
        ])
    return space.call_function(w_passwd_struct, w_tuple)
Beispiel #28
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)
Beispiel #29
0
def Py_CompileStringFlags(space, source, filename, start, flagsptr):
    """Parse and compile the Python source code in str, returning the
    resulting code object.  The start token is given by start; this
    can be used to constrain the code which can be compiled and should
    be Py_eval_input, Py_file_input, or Py_single_input.  The filename
    specified by filename is used to construct the code object and may
    appear in tracebacks or SyntaxError exception messages.  This
    returns NULL if the code cannot be parsed or compiled."""
    source = rffi.charp2str(source)
    filename = rffi.charp2str(filename)
    if flagsptr:
        flags = rffi.cast(lltype.Signed, flagsptr.c_cf_flags)
    else:
        flags = 0
    return compile_string(space, source, filename, start, flags)
Beispiel #30
0
def getnameinfo(address, flags):
    host = lltype.malloc(rffi.CCHARP.TO, NI_MAXHOST, flavor="raw")
    try:
        serv = lltype.malloc(rffi.CCHARP.TO, NI_MAXSERV, flavor="raw")
        try:
            addr = address.lock()
            error = intmask(_c.getnameinfo(addr, address.addrlen, host, NI_MAXHOST, serv, NI_MAXSERV, flags))
            address.unlock()
            if error:
                raise GAIError(error)
            return rffi.charp2str(host), rffi.charp2str(serv)
        finally:
            lltype.free(serv, flavor="raw")
    finally:
        lltype.free(host, flavor="raw")
 def test_frombuffer(self, space, api):
     w_view = SimpleView(StringBuffer("hello")).wrap(space)
     w_memoryview = api.PyMemoryView_FromObject(w_view)
     c_memoryview = rffi.cast(PyMemoryViewObject,
                              make_ref(space, w_memoryview))
     view = c_memoryview.c_view
     assert view.c_ndim == 1
     f = rffi.charp2str(view.c_format)
     assert f == 'B'
     assert view.c_shape[0] == 5
     assert view.c_strides[0] == 1
     assert view.c_len == 5
     o = rffi.charp2str(view.c_buf)
     assert o == 'hello'
     ref = api.PyMemoryView_FromBuffer(view)
     w_mv = from_ref(space, ref)
     for f in ('format', 'itemsize', 'ndim', 'readonly', 'shape', 'strides',
               'suboffsets'):
         w_f = space.wrap(f)
         assert space.eq_w(space.getattr(w_mv, w_f),
                           space.getattr(w_memoryview, w_f))
     decref(space, ref)
     decref(space, c_memoryview)
Beispiel #32
0
def load_embedded_cffi_module(space, version, init_struct):
    from pypy.module._cffi_backend.cffi1_module import load_cffi1_module
    declare_c_function()  # translation-time hint only:
    # declare _cffi_carefully_make_gil()
    #
    version = rffi.cast(lltype.Signed, version)
    if not (EMBED_VERSION_MIN <= version <= EMBED_VERSION_MAX):
        raise oefmt(space.w_ImportError,
                    "cffi embedded module has got unknown version tag %s",
                    hex(version))
    #
    if space.config.objspace.usemodules.thread:
        from pypy.module.thread import os_thread
        os_thread.setup_threads(space)
    #
    name = rffi.charp2str(init_struct.name)
    load_cffi1_module(space, name, None, init_struct.func)
    code = rffi.charp2str(init_struct.code)
    compiler = space.createcompiler()
    pycode = compiler.compile(code, "<init code for '%s'>" % name, 'exec', 0)
    w_globals = space.newdict(module=True)
    space.setitem_str(w_globals, "__builtins__", space.builtin)
    pycode.exec_code(space, w_globals, w_globals)
Beispiel #33
0
def PyErr_SetFromErrnoWithFilename(space, w_type, llfilename):
    """Similar to PyErr_SetFromErrno(), with the additional behavior that if
    filename is not NULL, it is passed to the constructor of type as a third
    parameter.  In the case of exceptions such as IOError and OSError,
    this is used to define the filename attribute of the exception instance.
    Return value: always NULL."""
    # XXX Doesn't actually do anything with PyErr_CheckSignals.
    if llfilename:
        w_filename = rffi.charp2str(llfilename)
        filename = space.wrap(w_filename)
    else:
        filename = space.w_None

    PyErr_SetFromErrnoWithFilenameObject(space, w_type, filename)
Beispiel #34
0
def realpath(path):
    path = pathobj.os_stringify(path).encode('utf-8')
    req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True)
    try:
        response = uv_callback.fs(req)
        response.wait(
            uv.fs_realpath(response.ec.uv_loop, req, path, uv_callback.fs.cb))
        if req.c_result < 0:
            raise uv_callback.to_error(req.c_result)
        # hmm?
        return from_cstring(rffi.charp2str(rffi.cast(rffi.CCHARP, req.c_ptr)))
    finally:
        uv.fs_req_cleanup(req)
        lltype.free(req, flavor='raw')
Beispiel #35
0
    def test_asbuffer(self, space):
        bufp = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
        lenp = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')

        w_text = space.newbytes("text")
        ref = make_ref(space, w_text)
        prev_refcnt = ref.c_ob_refcnt
        assert PyObject_AsCharBuffer(space, ref, bufp, lenp) == 0
        assert ref.c_ob_refcnt == prev_refcnt
        assert lenp[0] == 4
        assert rffi.charp2str(bufp[0]) == 'text'
        lltype.free(bufp, flavor='raw')
        lltype.free(lenp, flavor='raw')
        decref(space, ref)
Beispiel #36
0
def mkdtemp(path):
    path = pathobj.os_stringify(path).encode('utf-8')
    # TODO: XXXXXX  the last six characters must be these.
    req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True)
    try:
        response = uv_callback.fs(req)
        response.wait(
            uv.fs_mkdtemp(response.ec.uv_loop, req, path, uv_callback.fs.cb))
        if req.c_result < 0:
            raise uv_callback.to_error(req.c_result)
        return from_cstring(rffi.charp2str(rffi.cast(rffi.CCHARP, req.c_path)))
    finally:
        uv.fs_req_cleanup(req)
        lltype.free(req, flavor='raw')
Beispiel #37
0
def localeconv(space):
    "() -> dict. Returns numeric and monetary locale-specific parameters."
    lp = rlocale.localeconv()

    # Numeric information
    w_result = space.newdict()
    w = space.wrap
    space.setitem(w_result, w("decimal_point"),
                  charp2uni(space, lp.c_decimal_point))
    space.setitem(w_result, w("thousands_sep"),
                  charp2uni(space, lp.c_thousands_sep))
    space.setitem(w_result, w("grouping"),
                  _w_copy_grouping(space, rffi.charp2str(lp.c_grouping)))
    space.setitem(w_result, w("int_curr_symbol"),
                  charp2uni(space, lp.c_int_curr_symbol))
    space.setitem(w_result, w("currency_symbol"),
                  charp2uni(space, lp.c_currency_symbol))
    space.setitem(w_result, w("mon_decimal_point"),
                  charp2uni(space, lp.c_mon_decimal_point))
    space.setitem(w_result, w("mon_thousands_sep"),
                  charp2uni(space, lp.c_mon_thousands_sep))
    space.setitem(w_result, w("mon_grouping"),
                  _w_copy_grouping(space, rffi.charp2str(lp.c_mon_grouping)))
    space.setitem(w_result, w("positive_sign"),
                  charp2uni(space, lp.c_positive_sign))
    space.setitem(w_result, w("negative_sign"),
                  charp2uni(space, lp.c_negative_sign))
    space.setitem(w_result, w("int_frac_digits"), w(lp.c_int_frac_digits))
    space.setitem(w_result, w("frac_digits"), w(lp.c_frac_digits))
    space.setitem(w_result, w("p_cs_precedes"), w(lp.c_p_cs_precedes))
    space.setitem(w_result, w("p_sep_by_space"), w(lp.c_p_sep_by_space))
    space.setitem(w_result, w("n_cs_precedes"), w(lp.c_n_cs_precedes))
    space.setitem(w_result, w("n_sep_by_space"), w(lp.c_n_sep_by_space))
    space.setitem(w_result, w("p_sign_posn"), w(lp.c_p_sign_posn))
    space.setitem(w_result, w("n_sign_posn"), w(lp.c_n_sign_posn))

    return w_result
Beispiel #38
0
    def __init__(self, space, pto):
        bases_w = space.fixedview(from_ref(space, pto.c_tp_bases))
        dict_w = {}

        add_operators(space, dict_w, pto)
        convert_method_defs(space, dict_w, pto.c_tp_methods, self)
        convert_getset_defs(space, dict_w, pto.c_tp_getset, self)
        convert_member_defs(space, dict_w, pto.c_tp_members, self)

        w_dict = from_ref(space, pto.c_tp_dict)
        if w_dict is not None:
            dictkeys_w = space.listview(w_dict)
            for w_key in dictkeys_w:
                key = space.text_w(w_key)
                dict_w[key] = space.getitem(w_dict, w_key)

        name = rffi.charp2str(cts.cast('char*', pto.c_tp_name))
        flag_heaptype = pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE
        if flag_heaptype:
            minsize = rffi.sizeof(PyHeapTypeObject.TO)
        else:
            minsize = rffi.sizeof(PyObject.TO)
        new_layout = (pto.c_tp_basicsize > minsize or pto.c_tp_itemsize > 0)

        W_TypeObject.__init__(self, space, name,
            bases_w or [space.w_object], dict_w, force_new_layout=new_layout,
            is_heaptype=flag_heaptype)
        self.flag_cpytype = True
        # if a sequence or a mapping, then set the flag to force it
        if pto.c_tp_as_sequence and pto.c_tp_as_sequence.c_sq_item:
            self.flag_map_or_seq = 'S'
        elif (pto.c_tp_as_mapping and pto.c_tp_as_mapping.c_mp_subscript and
              not (pto.c_tp_as_sequence and pto.c_tp_as_sequence.c_sq_slice)):
            self.flag_map_or_seq = 'M'
        if pto.c_tp_doc:
            self.w_doc = space.newtext(
                rffi.charp2str(cts.cast('char*', pto.c_tp_doc)))
Beispiel #39
0
def getaddrinfo(node, service):
    req = lltype.malloc(uv.getaddrinfo_ptr.TO, flavor='raw', zero=True)
    if node is None:
        node_string = lltype.nullptr(rffi.CCHARP.TO)
    else:
        node_string = rffi.str2charp(node.string.encode('utf-8'))
    if service is None:
        service_string = lltype.nullptr(rffi.CCHARP.TO)
    else:
        service_string = rffi.str2charp(service.string.encode('utf-8'))
    try:
        response = uv_callback.getaddrinfo(req)
        status, res = response.wait(
            uv.getaddrinfo(response.ec.uv_loop, req,
                           uv_callback.getaddrinfo.cb, node_string,
                           service_string, lltype.nullptr(uv.addrinfo_ptr.TO)))
        if rffi.r_long(status) < 0:
            raise uv_callback.to_error(status)
        this = res
        result = []
        while this != lltype.nullptr(uv.addrinfo_ptr.TO):
            entry = Exnihilo()
            entry.setattr(u"flags", Integer(rffi.r_long(this.c_ai_flags)))
            entry.setattr(u"family", Integer(rffi.r_long(this.c_ai_family)))
            entry.setattr(u"socktype",
                          Integer(rffi.r_long(this.c_ai_socktype)))
            entry.setattr(u"protocol",
                          Integer(rffi.r_long(this.c_ai_protocol)))
            entry.setattr(
                u"addr",
                copy_to_uint8array(rffi.cast(rffi.VOIDP, this.c_ai_addr),
                                   this.c_ai_addrlen,
                                   rffi.sizeof(uv.sockaddr_storage)))
            if this.c_ai_canonname:
                entry.setattr(
                    u"canonname",
                    from_cstring(rffi.charp2str(this.c_ai_canonname)))
            else:
                entry.setattr(u"canonname", null)
            result.append(entry)
            this = rffi.cast(uv.addrinfo_ptr, this.c_ai_next)
        uv.freeaddrinfo(res)
        return List(result)
    finally:
        if node_string:
            lltype.free(node_string, flavor='raw')
        if service_string:
            lltype.free(service_string, flavor='raw')
        lltype.free(req, flavor='raw')
Beispiel #40
0
def PyMember_GetOne(space, obj, w_member):
    addr = rffi.cast(ADDR, obj)
    addr += w_member.c_offset

    member_type = rffi.cast(lltype.Signed, w_member.c_type)
    for converter in integer_converters:
        typ, lltyp, _ = converter
        if typ == member_type:
            result = rffi.cast(rffi.CArrayPtr(lltyp), addr)
            if lltyp is rffi.FLOAT:
                w_result = space.newfloat(
                    lltype.cast_primitive(lltype.Float, result[0]))
            elif typ == T_BOOL:
                x = rffi.cast(lltype.Signed, result[0])
                w_result = space.newbool(x != 0)
            elif typ == T_DOUBLE:
                w_result = space.newfloat(result[0])
            else:
                w_result = space.newint(result[0])
            return w_result

    if member_type == T_STRING:
        result = rffi.cast(rffi.CCHARPP, addr)
        if result[0]:
            w_result = PyString_FromString(space, result[0])
        else:
            w_result = space.w_None
    elif member_type == T_STRING_INPLACE:
        result = rffi.cast(rffi.CCHARP, addr)
        w_result = PyString_FromString(space, result)
    elif member_type == T_CHAR:
        result = rffi.cast(rffi.CCHARP, addr)
        w_result = space.newtext(result[0])
    elif member_type == T_OBJECT:
        obj_ptr = rffi.cast(PyObjectP, addr)
        if obj_ptr[0]:
            w_result = from_ref(space, obj_ptr[0])
        else:
            w_result = space.w_None
    elif member_type == T_OBJECT_EX:
        obj_ptr = rffi.cast(PyObjectP, addr)
        if obj_ptr[0]:
            w_result = from_ref(space, obj_ptr[0])
        else:
            w_name = space.newtext(rffi.charp2str(w_member.c_name))
            raise OperationError(space.w_AttributeError, w_name)
    else:
        raise oefmt(space.w_SystemError, "bad memberdescr type")
    return w_result
Beispiel #41
0
def _cffi_call_python(ll_externpy, ll_args):
    """Invoked by the helpers generated from extern "Python" in the cdef.

       'externpy' is a static structure that describes which of the
       extern "Python" functions is called.  It has got fields 'name' and
       'type_index' describing the function, and more reserved fields
       that are initially zero.  These reserved fields are set up by
       ffi.def_extern(), which invokes externpy_deco() below.

       'args' is a pointer to an array of 8-byte entries.  Each entry
       contains an argument.  If an argument is less than 8 bytes, only
       the part at the beginning of the entry is initialized.  If an
       argument is 'long double' or a struct/union, then it is passed
       by reference.

       'args' is also used as the place to write the result to
       (directly, even if more than 8 bytes).  In all cases, 'args' is
       at least 8 bytes in size.
    """
    from pypy.module._cffi_backend.ccallback import reveal_callback
    from rpython.rlib import rgil

    rgil.acquire_maybe_in_new_thread()
    llop.gc_stack_bottom(lltype.Void)  # marker to enter RPython from C

    cerrno._errno_after(rffi.RFFI_ERR_ALL | rffi.RFFI_ALT_ERRNO)

    if not ll_externpy.c_reserved1:
        # Not initialized!  We don't have a space at all.
        # Write the error to the file descriptor stderr.
        try:
            funcname = rffi.charp2str(ll_externpy.c_name)
            msg = ("extern \"Python\": function %s() called, but no code was "
                   "attached to it yet with @ffi.def_extern().  "
                   "Returning 0.\n" % (funcname, ))
            os.write(STDERR, msg)
        except:
            pass
        for i in range(intmask(ll_externpy.c_size_of_result)):
            ll_args[i] = '\x00'
    else:
        externpython = reveal_callback(ll_externpy.c_reserved1)
        # the same buffer is used both for passing arguments and
        # the result value
        externpython.invoke(ll_args, ll_args)

    cerrno._errno_before(rffi.RFFI_ERR_ALL | rffi.RFFI_ALT_ERRNO)

    rgil.release()
Beispiel #42
0
def do_ufunc(space, funcs, data, types, ntypes, nin, nout, identity, name, doc,
             check_return, w_signature):
    funcs_w = [None] * ntypes
    dtypes_w = [None] * ntypes * (nin + nout)
    for i in range(ntypes):
        funcs_w[i] = ufuncs.W_GenericUFuncCaller(funcs[i], data)
    for i in range(ntypes * (nin + nout)):
        dtypes_w[i] = get_dtype_cache(space).dtypes_by_num(ord(types[i]))
    w_funcs = space.newlist(funcs_w)
    w_dtypes = space.newlist(dtypes_w)
    w_doc = rffi.charp2str(doc)
    w_name = rffi.charp2str(name)
    w_identity = space.newint(identity)
    ufunc_generic = ufuncs.frompyfunc(space,
                                      w_funcs,
                                      nin,
                                      nout,
                                      w_dtypes,
                                      w_signature,
                                      w_identity,
                                      w_name,
                                      w_doc,
                                      stack_inputs=True)
    return ufunc_generic
Beispiel #43
0
def PyRun_StringFlags(space, source, start, w_globals, w_locals, flagsptr):
    """Execute Python source code from str in the context specified by the
    dictionaries globals and locals with the compiler flags specified by
    flags.  The parameter start specifies the start token that should be used to
    parse the source code.

    Returns the result of executing the code as a Python object, or NULL if an
    exception was raised."""
    source = rffi.charp2str(source)
    if flagsptr:
        flags = rffi.cast(lltype.Signed, flagsptr.c_cf_flags)
    else:
        flags = 0
    w_code = compile_string(space, source, "<string>", start, flags)
    return compiling.eval(space, w_code, w_globals, w_locals)
Beispiel #44
0
    def test_unicodeobject(self, space, api):
        assert api.PyUnicode_GET_SIZE(space.wrap(u'späm')) == 4
        assert api.PyUnicode_GetSize(space.wrap(u'späm')) == 4
        unichar = rffi.sizeof(Py_UNICODE)
        assert api.PyUnicode_GET_DATA_SIZE(space.wrap(u'späm')) == 4 * unichar

        encoding = rffi.charp2str(api.PyUnicode_GetDefaultEncoding())
        w_default_encoding = space.call_function(
            space.sys.get('getdefaultencoding'))
        assert encoding == space.unwrap(w_default_encoding)
        invalid = rffi.str2charp('invalid')
        utf_8 = rffi.str2charp('utf-8')
        prev_encoding = rffi.str2charp(space.unwrap(w_default_encoding))
        self.raises(space, api, TypeError, api.PyUnicode_SetDefaultEncoding,
                    lltype.nullptr(rffi.CCHARP.TO))
        assert api.PyUnicode_SetDefaultEncoding(invalid) == -1
        assert api.PyErr_Occurred() is space.w_LookupError
        api.PyErr_Clear()
        assert api.PyUnicode_SetDefaultEncoding(utf_8) == 0
        assert rffi.charp2str(api.PyUnicode_GetDefaultEncoding()) == 'utf-8'
        assert api.PyUnicode_SetDefaultEncoding(prev_encoding) == 0
        rffi.free_charp(invalid)
        rffi.free_charp(utf_8)
        rffi.free_charp(prev_encoding)
Beispiel #45
0
def hash_name_mapper_callback(obj_name, userdata):
    if not obj_name:
        return
    # Ignore aliased names, they pollute the list and OpenSSL appears
    # to have a its own definition of alias as the resulting list
    # still contains duplicate and alternate names for several
    # algorithms.
    if rffi.cast(lltype.Signed, obj_name[0].c_alias):
        return
    try:
        space = global_name_fetcher.space
        w_name = space.wrap(rffi.charp2str(obj_name[0].c_name))
        global_name_fetcher.meth_names.append(w_name)
    except OperationError, e:
        global_name_fetcher.w_error = e
Beispiel #46
0
def UnknownEncodingHandlerData_callback(ll_userdata, name, info):
    id = rffi.cast(lltype.Signed, ll_userdata)
    userdata = global_storage.get_object(id)
    space = userdata.space
    parser = userdata.parser()

    name = rffi.charp2str(name)

    try:
        parser.UnknownEncodingHandler(space, name, info)
    except OperationError, e:
        if not parser._exc_info:
            parser._exc_info = e
        XML_StopParser(parser.itself, XML_FALSE)
        result = 0
Beispiel #47
0
def PyUnicode_DecodeUTF16(space, s, size, llerrors, pbyteorder):
    """Decode length bytes from a UTF-16 encoded buffer string and return the
    corresponding Unicode object.  errors (if non-NULL) defines the error
    handling. It defaults to "strict".

    If byteorder is non-NULL, the decoder starts decoding using the given byte
    order:

    *byteorder == -1: little endian
    *byteorder == 0:  native order
    *byteorder == 1:  big endian

    If *byteorder is zero, and the first two bytes of the input data are a
    byte order mark (BOM), the decoder switches to this byte order and the BOM is
    not copied into the resulting Unicode string.  If *byteorder is -1 or
    1, any byte order mark is copied to the output (where it will result in
    either a \ufeff or a \ufffe character).

    After completion, *byteorder is set to the current byte order at the end
    of input data.

    If byteorder is NULL, the codec starts in native order mode.

    Return NULL if an exception was raised by the codec."""

    string = rffi.charpsize2str(s, size)

    if pbyteorder is not None:
        llbyteorder = rffi.cast(lltype.Signed, pbyteorder[0])
        if llbyteorder < 0:
            byteorder = "little"
        elif llbyteorder > 0:
            byteorder = "big"
        else:
            byteorder = "native"
    else:
        byteorder = "native"

    if llerrors:
        errors = rffi.charp2str(llerrors)
    else:
        errors = None

    result, _, length, byteorder = str_decode_utf_16_helper(
        string, errors, final=True, errorhandler=None, byteorder=byteorder)
    if pbyteorder is not None:
        pbyteorder[0] = rffi.cast(rffi.INT_real, byteorder)
    return space.newutf8(result, length)
Beispiel #48
0
def c_get_all_cpp_names(space, scope):
    sz = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw', zero=True)
    try:
        args = [_ArgH(scope.handle), _ArgP(rffi.cast(rffi.VOIDP, sz))]
        rawnames = rffi.cast(rffi.CCHARPP,
            _cdata_to_ptr(space, call_capi(space, 'get_all_cpp_names', args)))
        count = int(intmask(sz[0]))
    finally:
        lltype.free(sz, flavor='raw')
    allnames = []
    for i in range(count):
        pystr = rffi.charp2str(rawnames[i])
        c_free(space, rffi.cast(rffi.VOIDP, rawnames[i])) # c_free defined below
        allnames.append(pystr)
    c_free(space, rffi.cast(rffi.VOIDP, rawnames))        # id.
    return allnames
Beispiel #49
0
def PyLong_FromString(space, str, pend, base):
    """Return a new PyLongObject based on the string value in str, which is
    interpreted according to the radix in base.  If pend is non-NULL,
    *pend will point to the first character in str which follows the
    representation of the number.  If base is 0, the radix will be determined
    based on the leading characters of str: if str starts with '0x' or
    '0X', radix 16 will be used; if str starts with '0', radix 8 will be
    used; otherwise radix 10 will be used.  If base is not 0, it must be
    between 2 and 36, inclusive.  Leading spaces are ignored.  If there are
    no digits, ValueError will be raised."""
    s = rffi.charp2str(str)
    w_str = space.wrap(s)
    w_base = space.wrap(rffi.cast(lltype.Signed, base))
    if pend:
        pend[0] = rffi.ptradd(str, len(s))
    return space.call_function(space.w_long, w_str, w_base)
Beispiel #50
0
def PySequence_Fast(space, w_obj, m):
    """Returns the sequence o as a tuple, unless it is already a tuple or list, in
    which case o is returned.  Use PySequence_Fast_GET_ITEM() to access the
    members of the result.  Returns NULL on failure.  If the object is not a
    sequence, raises TypeError with m as the message text."""
    if isinstance(w_obj, listobject.W_ListObject):
        # make sure we can return a borrowed obj from PySequence_Fast_GET_ITEM
        # XXX how does this interact with CPyListStrategy?
        w_obj.ensure_object_strategy()
        return w_obj
    if isinstance(w_obj, tupleobject.W_TupleObject):
        return w_obj
    try:
        return tupleobject.W_TupleObject(space.fixedview(w_obj))
    except OperationError:
        raise OperationError(space.w_TypeError, space.wrap(rffi.charp2str(m)))
Beispiel #51
0
    def _getCharacterSetName(self, space, attribute):
        # get character set id
        charsetIdPtr = lltype.malloc(rffi.CArrayPtr(roci.ub2).TO,
                                     1,
                                     flavor='raw')
        try:
            status = roci.OCIAttrGet(self.environment.handle,
                                     roci.OCI_HTYPE_ENV,
                                     rffi.cast(roci.dvoidp, charsetIdPtr),
                                     lltype.nullptr(roci.Ptr(roci.ub4).TO),
                                     attribute, self.environment.errorHandle)
            self.environment.checkForError(
                status, "Connection_GetCharacterSetName(): get charset id")
            charsetId = charsetIdPtr[0]
        finally:
            lltype.free(charsetIdPtr, flavor='raw')

        # get character set name
        charsetname_buf, charsetname = rffi.alloc_buffer(roci.OCI_NLS_MAXBUFSZ)
        try:
            status = roci.OCINlsCharSetIdToName(self.environment.handle,
                                                charsetname_buf,
                                                roci.OCI_NLS_MAXBUFSZ,
                                                charsetId)
            self.environment.checkForError(
                status,
                "Connection_GetCharacterSetName(): get Oracle charset name")

            ianacharset_buf, ianacharset = rffi.alloc_buffer(
                roci.OCI_NLS_MAXBUFSZ)

            try:
                # get IANA character set name
                status = roci.OCINlsNameMap(self.environment.handle,
                                            ianacharset_buf,
                                            roci.OCI_NLS_MAXBUFSZ,
                                            charsetname_buf,
                                            roci.OCI_NLS_CS_ORA_TO_IANA)
                self.environment.checkForError(
                    status,
                    "Connection_GetCharacterSetName(): translate NLS charset")
                charset = rffi.charp2str(ianacharset_buf)
            finally:
                rffi.keep_buffer_alive_until_here(ianacharset_buf, ianacharset)
        finally:
            rffi.keep_buffer_alive_until_here(charsetname_buf, charsetname)
        return space.wrap(charset)
def PyMember_SetOne(space, obj, w_member, w_value):
    addr = rffi.cast(ADDR, obj)
    addr += w_member.c_offset
    member_type = rffi.cast(lltype.Signed, w_member.c_type)
    flags = rffi.cast(lltype.Signed, w_member.c_flags)

    if flags & READONLY:
        raise oefmt(space.w_AttributeError, "readonly attribute")
    elif member_type in [T_STRING, T_STRING_INPLACE]:
        raise oefmt(space.w_TypeError, "readonly attribute")
    elif w_value is None:
        if member_type == T_OBJECT_EX:
            if not rffi.cast(PyObjectP, addr)[0]:
                w_name = space.newtext(rffi.charp2str(w_member.c_name))
                raise OperationError(space.w_AttributeError, w_name)
        elif member_type != T_OBJECT:
            raise oefmt(space.w_TypeError,
                        "can't delete numeric/char attribute")

    for converter in integer_converters:
        typ, lltyp, getter, range_checking = converter
        if typ == member_type:
            value = getter(space, w_value)
            array = rffi.cast(rffi.CArrayPtr(lltyp), addr)
            casted = rffi.cast(lltyp, value)
            if range_checking:
                if rffi.cast(lltype.typeOf(value), casted) != value:
                    space.warn(
                        space.newtext("structmember: truncation of value"),
                        space.w_RuntimeWarning)
            array[0] = casted
            return 0

    if member_type == T_CHAR:
        str_value = space.text_w(w_value)
        if len(str_value) != 1:
            raise oefmt(space.w_TypeError, "string of length 1 expected")
        array = rffi.cast(rffi.CCHARP, addr)
        array[0] = str_value[0]
    elif member_type in [T_OBJECT, T_OBJECT_EX]:
        array = rffi.cast(PyObjectP, addr)
        if array[0]:
            decref(space, array[0])
        array[0] = make_ref(space, w_value)
    else:
        raise oefmt(space.w_SystemError, "bad memberdescr type")
    return 0
Beispiel #53
0
 def pypy_setup_home(ll_home, verbose):
     from pypy.module.sys.initpath import pypy_find_stdlib
     verbose = rffi.cast(lltype.Signed, verbose)
     if ll_home and ord(ll_home[0]):
         home1 = rffi.charp2str(ll_home)
         home = join(home1, 'x')  # <- so that 'll_home' can be
         # directly the root directory
     else:
         home1 = "pypy's shared library location"
         home = '*'
     w_path = pypy_find_stdlib(space, home)
     if space.is_none(w_path):
         if verbose:
             debug(
                 "pypy_setup_home: directories 'lib-python' and 'lib_pypy'"
                 " not found in %s or in any parent directory" % home1)
         return rffi.cast(rffi.INT, 1)
     space.startup()
     must_leave = space.threadlocals.try_enter_thread(space)
     try:
         # initialize sys.{path,executable,stdin,stdout,stderr}
         # (in unbuffered mode, to avoid troubles) and import site
         space.appexec([w_path, space.newtext(home), w_initstdio],
                       r"""(path, home, initstdio):
             import sys
             # don't import anything more above this: sys.path is not set
             sys.path[:] = path
             sys.executable = home
             initstdio(unbuffered=True)
             try:
                 import site
             except Exception as e:
                 sys.stderr.write("'import site' failed:\n")
                 import traceback
                 traceback.print_exc()
         """)
         return rffi.cast(rffi.INT, 0)
     except OperationError as e:
         if verbose:
             debug("OperationError:")
             debug(" operror-type: " + e.w_type.getname(space))
             debug(" operror-value: " +
                   space.text_w(space.str(e.get_w_value(space))))
         return rffi.cast(rffi.INT, -1)
     finally:
         if must_leave:
             space.threadlocals.leave_thread(space)
Beispiel #54
0
def wrap_getbuffer(space, w_self, w_args, func):
    func_target = rffi.cast(getbufferproc, func)
    py_obj = make_ref(space, w_self)
    py_type = py_obj.c_ob_type
    rbp = rffi.cast(rffi.VOIDP, 0)
    if py_type.c_tp_as_buffer:
        rbp = rffi.cast(rffi.VOIDP, py_type.c_tp_as_buffer.c_bf_releasebuffer)
    decref(space, py_obj)
    with lltype.scoped_alloc(Py_buffer) as pybuf:
        _flags = 0
        if space.len_w(w_args) > 0:
            _flags = space.int_w(space.listview(w_args)[0])
        flags = rffi.cast(rffi.INT_real, _flags)
        size = generic_cpy_call(space, func_target, w_self, pybuf, flags)
        if widen(size) < 0:
            space.fromcache(State).check_and_raise_exception(always=True)
        ptr = pybuf.c_buf
        size = pybuf.c_len
        ndim = widen(pybuf.c_ndim)
        shape = None
        if pybuf.c_shape:
            shape = [pybuf.c_shape[i] for i in range(ndim)]
        strides = None
        if pybuf.c_strides:
            strides = [pybuf.c_strides[i] for i in range(ndim)]
        if pybuf.c_format:
            format = rffi.charp2str(pybuf.c_format)
        else:
            format = 'B'
        # the CPython docs mandates that you do an incref whenever you call
        # bf_getbuffer; so, we pass needs_decref=True to ensure that we don't
        # leak we release the buffer:
        # https://docs.python.org/3.5/c-api/typeobj.html#c.PyBufferProcs.bf_getbuffer
        buf = CPyBuffer(space,
                        ptr,
                        size,
                        w_self,
                        format=format,
                        ndim=ndim,
                        shape=shape,
                        strides=strides,
                        itemsize=pybuf.c_itemsize,
                        readonly=widen(pybuf.c_readonly),
                        needs_decref=True,
                        releasebufferproc=rbp)
        fq.register_finalizer(buf)
        return space.newbuffer(buf)
Beispiel #55
0
def timezone_location_get(interp, w_datetimezone):
    space = interp.space

    c_location = w_datetimezone.timelib_timezone.c_location

    country_code = "%s%s" % (c_location.c_country_code[0],
                             c_location.c_country_code[1])
    latitude = c_location.c_latitude
    longitude = c_location.c_longitude
    comments = rffi.charp2str(c_location.c_comments)

    return space.new_array_from_pairs([
        (space.wrap("country_code"), space.wrap(country_code)),
        (space.wrap("latitude"), space.wrap(latitude)),
        (space.wrap("longitude"), space.wrap(longitude)),
        (space.wrap("comments"), space.wrap(comments))
    ])
Beispiel #56
0
def pypy_find_stdlib(space, executable):
    path, prefix = None, None
    if executable != '*':
        path, prefix = find_stdlib(get_state(space), executable)
    if path is None:
        if space.config.translation.shared:
            dynamic_location = pypy_init_home()
            if dynamic_location:
                dyn_path = rffi.charp2str(dynamic_location)
                pypy_init_free(dynamic_location)
                path, prefix = find_stdlib(get_state(space), dyn_path)
        if path is None:
            return space.w_None
    w_prefix = space.newtext(prefix)
    space.setitem(space.sys.w_dict, space.newtext('prefix'), w_prefix)
    space.setitem(space.sys.w_dict, space.newtext('exec_prefix'), w_prefix)
    return space.newlist([space.newtext(p) for p in path])
Beispiel #57
0
def add_operators(space, dict_w, pto):
    from pypy.module.cpyext.object import PyObject_HashNotImplemented
    hash_not_impl = llslot(space, PyObject_HashNotImplemented)
    for method_name, slot_names, wrapper_class, doc in slotdefs_for_wrappers:
        if method_name in dict_w:
            continue
        offset = [rffi.offsetof(lltype.typeOf(pto).TO, slot_names[0])]
        if len(slot_names) == 1:
            func = getattr(pto, slot_names[0])
            if slot_names[0] == 'c_tp_hash':
                # two special cases where __hash__ is explicitly set to None
                # (which leads to an unhashable type):
                # 1) tp_hash == PyObject_HashNotImplemented
                # 2) tp_hash == NULL and either of tp_compare or tp_richcompare are not NULL
                if hash_not_impl == func or (not func and
                                             (pto.c_tp_compare
                                              or pto.c_tp_richcompare)):
                    dict_w[method_name] = space.w_None
                    continue
        else:
            assert len(slot_names) == 2
            struct = getattr(pto, slot_names[0])
            if not struct:
                continue
            offset.append(
                rffi.offsetof(lltype.typeOf(struct).TO, slot_names[1]))
            func = getattr(struct, slot_names[1])
        func_voidp = rffi.cast(rffi.VOIDP, func)
        if not func:
            continue
        if wrapper_class is None:
            continue

        assert issubclass(wrapper_class, W_PyCWrapperObject)
        w_obj = wrapper_class(space,
                              pto,
                              method_name,
                              doc,
                              func_voidp,
                              offset=offset[:])
        dict_w[method_name] = w_obj
    if pto.c_tp_doc:
        dict_w['__doc__'] = space.newtext(
            rffi.charp2str(cts.cast('char*', pto.c_tp_doc)))
    if pto.c_tp_new:
        add_tp_new_wrapper(space, dict_w, pto)
Beispiel #58
0
def ctime(space, w_seconds=None):
    """ctime([seconds]) -> string

    Convert a time in seconds since the Epoch to a string in local time.
    This is equivalent to asctime(localtime(seconds)). When the time tuple is
    not present, current time as returned by localtime() is used."""

    seconds = _get_inttime(space, w_seconds)

    t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
    t_ref[0] = seconds
    p = c_ctime(t_ref)
    lltype.free(t_ref, flavor='raw')
    if not p:
        raise oefmt(space.w_ValueError, "unconvertible time")

    return space.newtext(rffi.charp2str(p)[:-1])  # get rid of new line
Beispiel #59
0
def finish_type_1(space, pto):
    """
    Sets up tp_bases, necessary before creating the interpreter type.
    """
    base = pto.c_tp_base
    base_pyo = rffi.cast(PyObject, pto.c_tp_base)
    if base and not base.c_tp_flags & Py_TPFLAGS_READY:
        name = rffi.charp2str(cts.cast('char*', base.c_tp_name))
        type_realize(space, base_pyo)
    if base and not pto.c_ob_type:  # will be filled later
        pto.c_ob_type = base.c_ob_type
    if not pto.c_tp_bases:
        if not base:
            bases = space.newtuple([])
        else:
            bases = space.newtuple([from_ref(space, base_pyo)])
        pto.c_tp_bases = make_ref(space, bases)
Beispiel #60
0
def PySequence_Fast(space, w_obj, m):
    """Returns the sequence o as a tuple, unless it is already a tuple or list, in
    which case o is returned.  Use PySequence_Fast_GET_ITEM() to access the
    members of the result.  Returns NULL on failure.  If the object cannot be
    converted to a sequence, and raises a TypeError, raise a new TypeError with
    m as the message text. If the conversion otherwise, fails, reraise the
    original exception"""
    if isinstance(w_obj, W_ListObject):
        # make sure we can return a borrowed obj from PySequence_Fast_GET_ITEM
        w_obj.convert_to_cpy_strategy(space)
        return w_obj
    try:
        return W_ListObject.newlist_cpyext(space, space.listview(w_obj))
    except OperationError as e:
        if e.match(space, space.w_TypeError):
            raise OperationError(space.w_TypeError, space.newtext(rffi.charp2str(m)))
        raise e