def HPyModule_Create(space, ctx, hpydef): modname = rffi.constcharp2str(hpydef.c_m_name) w_mod = Module(space, space.newtext(modname)) # # add the functions defined in hpydef.c_legacy_methods if hpydef.c_legacy_methods: if space.config.objspace.hpy_cpyext_API: pymethods = rffi.cast(rffi.VOIDP, hpydef.c_legacy_methods) attach_legacy_methods(space, pymethods, w_mod, modname) else: raise oefmt( space.w_RuntimeError, "Module %s contains legacy methods, but _hpy_universal " "was compiled without cpyext support", modname) # # add the native HPy defines if hpydef.c_defines: p = hpydef.c_defines i = 0 while p[i]: # hpy native methods hpymeth = p[i].c_meth name = rffi.constcharp2str(hpymeth.c_name) sig = rffi.cast(lltype.Signed, hpymeth.c_signature) doc = get_doc(hpymeth.c_doc) w_extfunc = interp_extfunc.W_ExtensionFunction( space, name, sig, doc, hpymeth.c_impl, w_mod) space.setattr(w_mod, space.newtext(w_extfunc.name), w_extfunc) i += 1 return handles.new(space, w_mod)
def add_slot_defs(space, ctx, w_result, c_defines): p = c_defines i = 0 HPyDef_Kind = llapi.cts.gettype('HPyDef_Kind') while p[i]: kind = rffi.cast(lltype.Signed, p[i].c_kind) if kind == HPyDef_Kind.HPyDef_Kind_Slot: hpyslot = llapi.cts.cast('_pypy_HPyDef_as_slot*', p[i]).c_slot fill_slot(space, w_result, hpyslot) elif kind == HPyDef_Kind.HPyDef_Kind_Meth: hpymeth = p[i].c_meth name = rffi.constcharp2str(hpymeth.c_name) sig = rffi.cast(lltype.Signed, hpymeth.c_signature) doc = get_doc(hpymeth.c_doc) w_extfunc = W_ExtensionMethod(space, name, sig, doc, hpymeth.c_impl, w_result) w_result.setdictvalue(space, rffi.constcharp2str(hpymeth.c_name), w_extfunc) elif kind == HPyDef_Kind.HPyDef_Kind_Member: hpymember = llapi.cts.cast('_pypy_HPyDef_as_member*', p[i]).c_member add_member(space, w_result, hpymember) elif kind == HPyDef_Kind.HPyDef_Kind_GetSet: hpygetset = llapi.cts.cast('_pypy_HPyDef_as_getset*', p[i]).c_getset add_getset(space, w_result, hpygetset) else: raise oefmt(space.w_ValueError, "Unspported HPyDef.kind: %d", kind) i += 1
def add_member(space, w_type, hpymember): name = rffi.constcharp2str(hpymember.c_name) kind = rffi.cast(lltype.Signed, hpymember.c_type) offset = rffi.cast(lltype.Signed, hpymember.c_offset) readonly = rffi.cast(lltype.Signed, hpymember.c_readonly) doc = rffi.constcharp2str(hpymember.c_doc) if hpymember.c_doc else None w_descr = W_HPyMemberDescriptor(w_type, kind, name, doc, offset, readonly) w_type.setdictvalue(space, name, w_descr)
def _maybe_utf8_to_w(space, utf8): # should this be a method of space? s = rffi.constcharp2str(utf8) try: length = rutf8.check_utf8(s, allow_surrogates=False) except rutf8.CheckError: raise # XXX do something return space.newtext(s, length)
def set_error(self, space, code): err = rffi.constcharp2str(XML_ErrorString(code)) lineno = XML_GetCurrentLineNumber(self.itself) colno = XML_GetCurrentColumnNumber(self.itself) msg = "%s: line %d, column %d" % (err, lineno, colno) w_errorcls = space.fromcache(Cache).w_error w_error = space.call_function(w_errorcls, space.newtext(msg)) space.setattr(w_error, space.newtext("code"), space.newint(code)) space.setattr(w_error, space.newtext("offset"), space.newint(colno)) space.setattr(w_error, space.newtext("lineno"), space.newint(lineno)) self.w_error = w_error return OperationError(w_errorcls, w_error)
def w_convert_interned(self, space, data): if not data: return space.w_None w_data = self.w_convert(space, rffi.constcharp2str(data)) if not self.w_intern: return w_data try: return space.getitem(self.w_intern, w_data) except OperationError as e: if not e.match(space, space.w_KeyError): raise space.setitem(self.w_intern, w_data, w_data) return w_data
def __init__(self, w_type, hpygetset): self.hpygetset = hpygetset self.w_type = w_type # name = rffi.constcharp2str(hpygetset.c_name) doc = fset = fget = fdel = None if hpygetset.c_doc: doc = rffi.constcharp2str(hpygetset.c_doc) if hpygetset.c_getter_impl: fget = getset_get if hpygetset.c_setter_impl: fset = getset_set # XXX: write a test to check that 'del' works #fdel = ... GetSetProperty.__init__(self, fget, fset, fdel, doc, cls=None, use_closure=True, tag="hpy_getset", name=name)
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.constcharp2str(name) try: parser.UnknownEncodingHandler(space, name, info) except OperationError as e: if not parser._exc_info: parser._exc_info = e XML_StopParser(parser.itself, XML_FALSE) result = 0 else: result = 1 return rffi.cast(rffi.INT, result)
def HPyType_FromSpec(space, ctx, spec, params): dict_w = {} specname = rffi.constcharp2str(spec.c_name) dotpos = specname.rfind('.') if dotpos < 0: name = specname modname = None else: name = specname[dotpos + 1:] modname = specname[:dotpos] if modname is not None: dict_w['__module__'] = space.newtext(modname) bases_w = get_bases_from_params(space, ctx, params) basicsize = rffi.cast(lltype.Signed, spec.c_basicsize) w_result = _create_new_type(space, space.w_type, name, bases_w, dict_w, basicsize) if spec.c_legacy_slots: attach_legacy_slots_to_type(space, w_result, spec.c_legacy_slots) if spec.c_defines: add_slot_defs(space, ctx, w_result, spec.c_defines) return handles.new(space, w_result)
def get_doc(c_doc): if not c_doc: return None return rffi.constcharp2str(c_doc)
def ccharp2text(space, ptr): """ Convert a C const char* into a W_UnicodeObject """ s = rffi.constcharp2str(ptr) return space.newtext(s)
def ErrorString(space, code): """ErrorString(errno) -> string Returns string error for given number.""" return space.newtext(rffi.constcharp2str(XML_ErrorString(code)))
def get_expat_version(space): return space.newtext(rffi.constcharp2str(XML_ExpatVersion()))
def PyOS_string_to_double(space, s, endptr, w_overflow_exception): """Convert a string s to a double, raising a Python exception on failure. The set of accepted strings corresponds to the set of strings accepted by Python's float() constructor, except that s must not have leading or trailing whitespace. The conversion is independent of the current locale. If endptr is NULL, convert the whole string. Raise ValueError and return -1.0 if the string is not a valid representation of a floating-point number. If endptr is not NULL, convert as much of the string as possible and set *endptr to point to the first unconverted character. If no initial segment of the string is the valid representation of a floating-point number, set *endptr to point to the beginning of the string, raise ValueError, and return -1.0. If s represents a value that is too large to store in a float (for example, "1e500" is such a string on many platforms) then if overflow_exception is NULL return Py_HUGE_VAL (with an appropriate sign) and don't set any exception. Otherwise, overflow_exception must point to a Python exception object; raise that exception and return -1.0. In both cases, set *endptr to point to the first character after the converted value. If any other error occurs during the conversion (for example an out-of-memory error), set the appropriate Python exception and return -1.0. """ user_endptr = True try: if not endptr: endptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw') user_endptr = False result = rdtoa.dg_strtod(s, endptr) endpos = (rffi.cast(rffi.LONG, endptr[0]) - rffi.cast(rffi.LONG, s)) if endpos == 0 or (not user_endptr and not endptr[0][0] == '\0'): low = rffi.constcharp2str(s).lower() sz = 0 if len(low) < 3: pass elif low[0] == '-': if low.startswith('-infinity'): result = -rfloat.INFINITY sz = len("-infinity") elif low.startswith("-inf"): result = -rfloat.INFINITY sz = 4 elif low.startswith("-nan"): result = -rfloat.NAN sz = 4 elif low[0] == '+': if low.startswith("+infinity"): result = rfloat.INFINITY sz = len("+infinity") elif low.startswith("+inf"): result = rfloat.INFINITY sz = 4 elif low.startswith("+nan"): result = rfloat.NAN sz = 4 elif low.startswith("infinity"): result = rfloat.INFINITY sz = len("infinity") elif low.startswith("inf"): result = rfloat.INFINITY sz = 3 elif low.startswith("nan"): result = rfloat.NAN sz = 3 # result is set to 0.0 for a parse_error in dtoa.c # if it changed, we must have sucessfully converted if result != 0.0: if endptr: endptr[0] = rffi.cast(rffi.CCHARP, rffi.ptradd(s, sz)) return result raise oefmt(space.w_ValueError, "invalid input at position %d", endpos) err = rffi.cast(lltype.Signed, rposix._get_errno()) if err == errno.ERANGE: rposix._set_errno(rffi.cast(rffi.INT, 0)) if w_overflow_exception is None: if result > 0: return rfloat.INFINITY else: return -rfloat.INFINITY else: raise oefmt(w_overflow_exception, "value too large") return result finally: if not user_endptr: lltype.free(endptr, flavor='raw')
def entry_point(argv): parser = interp_pyexpat.XML_ParserCreate("test") interp_pyexpat.XML_ParserFree(parser) res = interp_pyexpat.XML_ErrorString(3) os.write(1, rffi.constcharp2str(res)) return 0