def getattr(self, space, attr): try: attribute = self.objectType.attributesByName[attr] except KeyError: msg = "ExternalObject has no attribute '%s'" %(attr,) raise OperationError(space.w_AttributeError, space.wrap(msg)) environment = self.objectType.environment scalarvalueindicatorptr = lltype.malloc(rffi.CArrayPtr(roci.OCIInd).TO, 1, flavor='raw') valueindicatorptr = lltype.malloc(rffi.CArrayPtr(roci.dvoidp).TO, 1, flavor='raw') valueptr = lltype.malloc(rffi.CArrayPtr(roci.dvoidp).TO, 1, flavor='raw') tdoptr = lltype.malloc(rffi.CArrayPtr(roci.OCIType).TO, 1, flavor='raw') nameptr = lltype.malloc(rffi.CArrayPtr(roci.oratext).TO, 1, flavor='raw') nameptr[0] = rffi.str2charp(attr) namelenptr = lltype.malloc(rffi.CArrayPtr(roci.ub4).TO, 1, flavor='raw') namelenptr[0] = rffi.cast(roci.ub4, len(attr)) try: status = roci.OCIObjectGetAttr( environment.handle, environment.errorHandle, self.instance, self.indicator, self.objectType.tdo, nameptr, namelenptr, 1, lltype.nullptr(roci.Ptr(roci.ub4).TO), 0, scalarvalueindicatorptr, valueindicatorptr, valueptr, tdoptr) environment.checkForError( status, "ExternalObject_GetAttributeValue(): getting value") # determine the proper null indicator valueIndicator = valueindicatorptr[0] if not valueIndicator: valueIndicator = rffi.cast(roci.dvoidp, scalarvalueindicatorptr) value = valueptr[0] return convertObject( space, environment, attribute.typeCode, value, valueIndicator, self, attribute.subType) finally: lltype.free(scalarvalueindicatorptr, flavor='raw') lltype.free(valueindicatorptr, flavor='raw') lltype.free(valueptr, flavor='raw') lltype.free(tdoptr, flavor='raw') rffi.free_charp(nameptr[0]) lltype.free(nameptr, flavor='raw') lltype.free(namelenptr, flavor='raw')
def strxfrm(space, s): "string -> string. Returns a string that behaves for cmp locale-aware." n1 = len(s) + 1 buf = lltype.malloc(rffi.CCHARP.TO, n1, flavor="raw", zero=True) s_c = rffi.str2charp(s) try: n2 = _strxfrm(buf, s_c, n1) + 1 finally: rffi.free_charp(s_c) if n2 > n1: # more space needed lltype.free(buf, flavor="raw") buf = lltype.malloc(rffi.CCHARP.TO, intmask(n2), flavor="raw", zero=True) s_c = rffi.str2charp(s) try: _strxfrm(buf, s_c, n2) finally: rffi.free_charp(s_c) val = rffi.charp2str(buf) lltype.free(buf, flavor="raw") return space.wrap(val)
def test_ascii_codec(self, space, api): s = 'abcdefg' data = rffi.str2charp(s) w_u = api.PyUnicode_DecodeASCII(data, len(s), lltype.nullptr(rffi.CCHARP.TO)) assert space.eq_w(w_u, space.wrap(u"abcdefg")) rffi.free_charp(data) s = 'abcd\xFF' data = rffi.str2charp(s) self.raises(space, api, UnicodeDecodeError, api.PyUnicode_DecodeASCII, data, len(s), lltype.nullptr(rffi.CCHARP.TO)) rffi.free_charp(data) uni = u'abcdefg' data = rffi.unicode2wcharp(uni) w_s = api.PyUnicode_EncodeASCII(data, len(uni), lltype.nullptr(rffi.CCHARP.TO)) assert space.eq_w(space.wrap("abcdefg"), w_s) rffi.free_wcharp(data) u = u'äbcdéfg' data = rffi.unicode2wcharp(u) w_s = api.PyUnicode_EncodeASCII(data, len(u), lltype.nullptr(rffi.CCHARP.TO)) self.raises(space, api, UnicodeEncodeError, api.PyUnicode_EncodeASCII, data, len(u), lltype.nullptr(rffi.CCHARP.TO)) rffi.free_wcharp(data)
def test_setitemstring(self, space, api): w_d = space.newdict() key = rffi.str2charp("key") api.PyMapping_SetItemString(w_d, key, space.wrap(42)) assert 42 == space.unwrap( api.PyMapping_GetItemString(w_d, key)) rffi.free_charp(key)
def SetValue(space, w_hkey, w_subkey, typ, value): """SetValue(key, sub_key, type, value) - Associates a value with a specified key. key is an already open key, or any one of the predefined HKEY_* constants. sub_key is a string that names the subkey with which the value is associated. type is an integer that specifies the type of the data. Currently this must be REG_SZ, meaning only strings are supported. value is a string that specifies the new value. If the key specified by the sub_key parameter does not exist, the SetValue function creates it. Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently. The key identified by the key parameter must have been opened with KEY_SET_VALUE access.""" if typ != rwinreg.REG_SZ: errstring = space.wrap("Type must be _winreg.REG_SZ") raise OperationError(space.w_ValueError, errstring) hkey = hkey_w(w_hkey, space) if space.is_w(w_subkey, space.w_None): subkey = None else: subkey = space.str_w(w_subkey) dataptr = rffi.str2charp(value) try: ret = rwinreg.RegSetValue(hkey, subkey, rwinreg.REG_SZ, dataptr, len(value)) finally: rffi.free_charp(dataptr) if ret != 0: raiseWindowsError(space, ret, "RegSetValue")
def demo(filename): width = 800 height = 600 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,width,height) cr = cairo_create(surface) cairo_scale(cr, width, height) cairo_set_line_width(cr, 0.04) cairo_arc(cr, 0.5, 0.5, 0.3, 0, 2 * pi) cairo_clip(cr) cairo_new_path(cr) # current path is not # consumed by cairo_clip() cairo_rectangle(cr, 0, 0, 1, 1) cairo_fill(cr) cairo_set_source_rgb(cr, 0, 1, 0) cairo_move_to(cr, 0, 0) cairo_line_to(cr, 1, 1) cairo_move_to(cr, 1, 0) cairo_line_to(cr, 0, 1) cairo_stroke(cr) #_cairo_surface_write_to_png(surface, 'foo.png') # XXX why does this fail to compile ?? path = rffi.str2charp(filename) cairo_surface_write_to_png(surface, path) rffi.free_charp(path) cairo_destroy(cr) cairo_surface_destroy(surface) return 0
def SetPythonHome(self,path): return impl = self.lib.getpointer("Py_SetPythonHome",[clibffi.ffi_type_pointer],clibffi.ffi_type_void) buf = rffi.str2charp(path) impl.push_arg(buf) impl.call(lltype.Void) rffi.free_charp(buf)
def test(encoded, endian, realendian=None): encoded_charp = rffi.str2charp(encoded) strict_charp = rffi.str2charp("strict") if endian is not None: if endian < 0: value = -1 elif endian > 0: value = 1 else: value = 0 pendian = lltype.malloc(rffi.INTP.TO, 1, flavor='raw') pendian[0] = rffi.cast(rffi.INT, value) else: pendian = None w_ustr = api.PyUnicode_DecodeUTF32(encoded_charp, len(encoded), strict_charp, pendian) assert space.eq_w( space.call_method(w_ustr, 'encode', space.wrap('ascii')), space.wrap("ab")) rffi.free_charp(encoded_charp) rffi.free_charp(strict_charp) if pendian: if realendian is not None: assert rffi.cast(rffi.INT, realendian) == pendian[0] lltype.free(pendian, flavor='raw')
def strcoll(space, w_s1, w_s2): "string,string -> int. Compares two strings according to the locale." if space.is_true(space.isinstance(w_s1, space.w_str)) and \ space.is_true(space.isinstance(w_s2, space.w_str)): s1, s2 = space.str_w(w_s1), space.str_w(w_s2) s1_c = rffi.str2charp(s1) s2_c = rffi.str2charp(s2) try: return space.wrap(_strcoll(s1_c, s2_c)) finally: rffi.free_charp(s1_c) rffi.free_charp(s2_c) #if not space.is_true(space.isinstance(w_s1, space.w_unicode)) and \ # not space.is_true(space.isinstance(w_s2, space.w_unicode)): # raise OperationError(space.w_ValueError, # space.wrap("strcoll arguments must be strings")) s1, s2 = space.unicode_w(w_s1), space.unicode_w(w_s2) s1_c = rffi.unicode2wcharp(s1) s2_c = rffi.unicode2wcharp(s2) try: result = _wcscoll(s1_c, s2_c) finally: rffi.free_wcharp(s1_c) rffi.free_wcharp(s2_c) return space.wrap(result)
def test_overflow_neg(self, api): s = rffi.str2charp('-1e500') null = lltype.nullptr(rffi.CCHARPP.TO) r = api.PyOS_string_to_double(s, null, None) assert math.isinf(r) assert r < 0 rffi.free_charp(s)
def execv_lltypeimpl(path, args): l_path = rffi.str2charp(path) l_args = rffi.liststr2charpp(args) os_execv(l_path, l_args) rffi.free_charpp(l_args) rffi.free_charp(l_path) raise OSError(rffi.c_errno, "execv failed")
def test(encoded, endian, realendian=None): encoded_charp = rffi.str2charp(encoded) strict_charp = rffi.str2charp("strict") if endian is not None: if endian < 0: value = -1 elif endian > 0: value = 1 else: value = 0 pendian = lltype.malloc(rffi.INTP.TO, 1, flavor='raw') pendian[0] = rffi.cast(rffi.INT, value) else: pendian = None w_ustr = api.PyUnicode_DecodeUTF16(encoded_charp, len(encoded), strict_charp, pendian) assert space.eq_w(space.call_method(w_ustr, 'encode', space.wrap('ascii')), space.wrap("abcd")) rffi.free_charp(encoded_charp) rffi.free_charp(strict_charp) if pendian: if realendian is not None: assert rffi.cast(rffi.INT, realendian) == pendian[0] lltype.free(pendian, flavor='raw')
def SetValue(space, w_hkey, w_subkey, typ, value): """SetValue(key, sub_key, type, value) - Associates a value with a specified key. key is an already open key, or any one of the predefined HKEY_* constants. sub_key is a string that names the subkey with which the value is associated. type is an integer that specifies the type of the data. Currently this must be REG_SZ, meaning only strings are supported. value is a string that specifies the new value. If the key specified by the sub_key parameter does not exist, the SetValue function creates it. Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently. The key identified by the key parameter must have been opened with KEY_SET_VALUE access.""" if typ != rwinreg.REG_SZ: errstring = space.wrap("Type must be _winreg.REG_SZ") raise OperationError(space.w_ValueError, errstring) hkey = hkey_w(w_hkey, space) if space.is_w(w_subkey, space.w_None): subkey = None else: subkey = space.str_w(w_subkey) dataptr = rffi.str2charp(value) try: ret = rwinreg.RegSetValue(hkey, subkey, rwinreg.REG_SZ, dataptr, len(value)) finally: rffi.free_charp(dataptr) if ret != 0: raiseWindowsError(space, ret, 'RegSetValue')
def demo(filename): width = 800 height = 600 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height) cr = cairo_create(surface) cairo_scale(cr, width, height) cairo_set_line_width(cr, 0.04) cairo_arc(cr, 0.5, 0.5, 0.3, 0, 2 * pi) cairo_clip(cr) cairo_new_path(cr) # current path is not # consumed by cairo_clip() cairo_rectangle(cr, 0, 0, 1, 1) cairo_fill(cr) cairo_set_source_rgb(cr, 0, 1, 0) cairo_move_to(cr, 0, 0) cairo_line_to(cr, 1, 1) cairo_move_to(cr, 1, 0) cairo_line_to(cr, 0, 1) cairo_stroke(cr) #_cairo_surface_write_to_png(surface, 'foo.png') # XXX why does this fail to compile ?? path = rffi.str2charp(filename) cairo_surface_write_to_png(surface, path) rffi.free_charp(path) cairo_destroy(cr) cairo_surface_destroy(surface) return 0
def Sys_SetPath(self, path): impl = self.lib.getpointer("PySys_SetPath", [libffi.ffi_type_pointer], libffi.ffi_type_void) buf = rffi.str2charp(path) impl.push_arg(buf) impl.call(lltype.Void) rffi.free_charp(buf)
def Run_SimpleString(self,string): impl = self.lib.getpointer("PyRun_SimpleString",[libffi.ffi_type_pointer],libffi.ffi_type_sint) buf = rffi.str2charp(string) impl.push_arg(buf) res = impl.call(rffi.INT) rffi.free_charp(buf) if res < 0: self._error()
def test_overflow_exc(self, space, api): s = rffi.str2charp('1e500') null = lltype.nullptr(rffi.CCHARPP.TO) r = api.PyOS_string_to_double(s, null, space.w_ValueError) assert r == -1.0 raises(ValueError) api.PyErr_Clear() rffi.free_charp(s)
def test_incremental(self, space, api): utf8 = rffi.str2charp('utf-8') w_encoder = api.PyCodec_IncrementalEncoder(utf8, None) w_encoded = space.call_method(w_encoder, 'encode', space.wrap(u'späm')) w_decoder = api.PyCodec_IncrementalDecoder(utf8, None) w_decoded = space.call_method(w_decoder, 'decode', w_encoded) assert space.unwrap(w_decoded) == u'späm' rffi.free_charp(utf8)
def test_Occurred(self, space, api): assert not api.PyErr_Occurred() string = rffi.str2charp("spam and eggs") api.PyErr_SetString(space.w_ValueError, string) rffi.free_charp(string) assert api.PyErr_Occurred() is space.w_ValueError api.PyErr_Clear()
def gettext(space, msg): """gettext(msg) -> string Return translation of msg.""" msg_c = rffi.str2charp(msg) try: return space.wrap(rffi.charp2str(_gettext(msg_c))) finally: rffi.free_charp(msg_c)
def test_load_wav(): if RMix.OpenAudio(22050, RSDL.AUDIO_S16LSB, 2, 1024) != 0: error = rffi.charp2str(RSDL.GetError()) raise Exception(error) filename = rffi.str2charp('applause.wav') RMix.LoadWAV(filename) rffi.free_charp(filename) RMix.CloseAudio()
def test_newcode(self, space, api): filename = rffi.str2charp('filename') funcname = rffi.str2charp('funcname') w_code = api.PyCode_NewEmpty(filename, funcname, 3) assert w_code.co_filename == 'filename' assert w_code.co_firstlineno == 3 rffi.free_charp(filename) rffi.free_charp(funcname)
def test_fromstring(self, space, api): s = rffi.str2charp(u'späm'.encode("utf-8")) w_res = api.PyUnicode_FromString(s) assert space.unwrap(w_res) == u'späm' w_res = api.PyUnicode_FromStringAndSize(s, 4) assert space.unwrap(w_res) == u'spä' rffi.free_charp(s)
def test_bad_string(self, api): s = rffi.str2charp(' 0.4') null = lltype.nullptr(rffi.CCHARPP.TO) r = api.PyOS_string_to_double(s, null, None) assert r == -1.0 raises(ValueError) api.PyErr_Clear() rffi.free_charp(s)
def buffer_dealloc(space, py_obj): py_buf = rffi.cast(PyBufferObject, py_obj) if py_buf.c_b_base: Py_DecRef(space, py_buf.c_b_base) else: rffi.free_charp(rffi.cast(rffi.CCHARP, py_buf.c_b_ptr)) from pypy.module.cpyext.object import PyObject_dealloc PyObject_dealloc(space, py_obj)
def Run_SimpleString(self,string): impl = self.lib.getpointer("PyRun_SimpleString",[clibffi.ffi_type_pointer],clibffi.ffi_type_sint) buf = rffi.str2charp(string) impl.push_arg(rffi.cast(rffi.VOIDP,buf)) res = impl.call(rffi.INT) rffi.free_charp(buf) if res < 0: self._error()
def test_dlsym(self): s = rffi.str2charp(get_libc_name()) lib = dlopen(s) rffi.free_charp(s) handle = rffi.cast(lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed)), dlsym(lib, 'abs')) assert 1 == handle(1) assert 1 == handle(-1)
def entry_point(argv): s = rffi.str2charp(argv[1]) n = foobar(s) rffi.free_charp(s) s = rffi.str2charp(argv[n]) n = foobar(s) rffi.free_charp(s) return n
def test_dlopen(self): s = rffi.str2charp('xxxxxxxxxxxx') py.test.raises(DLOpenError, "dlopen(s)") rffi.free_charp(s) # s = rffi.str2charp(get_libc_name()) assert dlopen(s) rffi.free_charp(s)
def os_open_lltypeimpl(path, flags, mode): l_path = rffi.str2charp(path) mode = lltype.cast_primitive(mode_t, mode) result = os_open(l_path, flags, mode) rffi.free_charp(l_path) if result == -1: raise OSError(rffi.c_errno, "os_open failed") return result
def getenv_llimpl(name): l_name = rffi.str2charp(name) l_result = os_getenv(l_name) if l_result: result = rffi.charp2str(l_result) else: result = None rffi.free_charp(l_name) return result
def wrap_getattr(space, w_self, w_args, func): func_target = rffi.cast(getattrfunc, func) check_num_args(space, w_args, 1) args_w = space.fixedview(w_args) name_ptr = rffi.str2charp(space.str_w(args_w[0])) try: return generic_cpy_call(space, func_target, w_self, name_ptr) finally: rffi.free_charp(name_ptr)
def send(self, data, flags=0): """Send a data string to the socket. For the optional flags argument, see the Unix manual. Return the number of bytes sent; this may be less than len(data) if the network is busy.""" dataptr = rffi.str2charp(data) try: return self.send_raw(dataptr, len(data), flags) finally: rffi.free_charp(dataptr)
def adler32(string, start=ADLER32_DEFAULT_START): """ Compute the Adler-32 checksum of the string, possibly with the given start value, and return it as a unsigned 32 bit integer. """ bytes = rffi.str2charp(string) checksum = _adler32(start, rffi.cast(Bytefp, bytes), len(string)) rffi.free_charp(bytes) return checksum
def Import_ImportModule(self,name): impl = self.lib.getpointer("PyImport_ImportModule",[libffi.ffi_type_pointer],libffi.ffi_type_pointer) buf = rffi.str2charp(name) impl.push_arg(buf) mod = impl.call(rffi.VOIDP) rffi.free_charp(buf) if not mod: self._error() return mod
def String_FromString(self,s): impl = self.lib.getpointer("PyString_FromString",[libffi.ffi_type_pointer],libffi.ffi_type_pointer) buf = rffi.str2charp(s) impl.push_arg(buf) ps = impl.call(rffi.VOIDP) rffi.free_charp(buf) if not ps: self._error() return ps
def test_dlsym(self): s = rffi.str2charp(get_libc_name()) lib = dlopen(s) rffi.free_charp(s) handle = rffi.cast( lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed)), dlsym(lib, 'abs')) assert 1 == handle(1) assert 1 == handle(-1)
def Import_ImportModule(self,name): impl = self.lib.getpointer("PyImport_ImportModule",[clibffi.ffi_type_pointer],clibffi.ffi_type_pointer) buf = rffi.str2charp(name) impl.push_arg(rffi.cast(rffi.VOIDP,buf)) mod = impl.call(rffi.VOIDP) rffi.free_charp(buf) if not mod: self._error() return mod
def String_FromString(self,s): impl = self.lib.getpointer("PyString_FromString",[clibffi.ffi_type_pointer],clibffi.ffi_type_pointer) buf = rffi.str2charp(s) impl.push_arg(rffi.cast(rffi.VOIDP,buf)) ps = impl.call(rffi.VOIDP) rffi.free_charp(buf) if not ps: self._error() return ps
def f42(n): c_strchr = glob.c_strchr raw = rffi.str2charp("foobar" + chr((n & 63) + 32)) argchain = ArgChain() argchain = argchain.arg(rffi.cast(lltype.Signed, raw)) argchain = argchain.arg(rffi.cast(rffi.INT, ord('b'))) res = c_strchr.call(argchain, rffi.CCHARP) check(rffi.charp2str(res) == "bar" + chr((n & 63) + 32)) rffi.free_charp(raw)
def test_file_writestring(self, space, api, capfd): s = rffi.str2charp("test\n") try: api.PyFile_WriteString(s, space.sys.get("stdout")) finally: rffi.free_charp(s) out, err = capfd.readouterr() out = out.replace('\r\n', '\n') assert out == "test\n"