def strcoll(space, w_s1, w_s2): "string,string -> int. Compares two strings according to the locale." if (space.isinstance_w(w_s1, space.w_bytes) and space.isinstance_w(w_s2, space.w_bytes)): s1, s2 = space.bytes_w(w_s1), space.bytes_w(w_s2) s1_c = rffi.str2charp(s1) s2_c = rffi.str2charp(s2) try: return space.newint(_strcoll(s1_c, s2_c)) finally: rffi.free_charp(s1_c) rffi.free_charp(s2_c) s1, l1 = space.utf8_len_w(w_s1) s2, l2 = space.utf8_len_w(w_s2) s1_c = rffi.utf82wcharp(s1, l1) s2_c = rffi.utf82wcharp(s2, l2) try: result = _wcscoll(s1_c, s2_c) finally: rffi.free_wcharp(s1_c) rffi.free_wcharp(s2_c) return space.newint(result)
def encodeex(encodebuf, utf8data, length, errors="strict", errorcb=None, namecb=None, ignore_error=0): inleft = length inbuf = rffi.utf82wcharp(utf8data, length) try: if pypy_cjk_enc_init(encodebuf, inbuf, inleft) < 0: raise MemoryError if ignore_error == 0: flags = MBENC_FLUSH | MBENC_RESET else: flags = 0 while True: r = pypy_cjk_enc_chunk(encodebuf, flags) if r == 0 or r == ignore_error: break multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb, utf8data) while flags & MBENC_RESET: r = pypy_cjk_enc_reset(encodebuf) if r == 0: break multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb, utf8data) src = pypy_cjk_enc_outbuf(encodebuf) length = pypy_cjk_enc_outlen(encodebuf) return rffi.charpsize2str(src, length) finally: lltype.free(inbuf, flavor='raw')
def multibytecodec_decerror(decodebuf, e, errors, errorcb, namecb, stringdata): if e > 0: reason = "illegal multibyte sequence" esize = e elif e == MBERR_TOOFEW: reason = "incomplete multibyte sequence" esize = pypy_cjk_dec_inbuf_remaining(decodebuf) elif e == MBERR_NOMEMORY: raise MemoryError else: raise RuntimeError # # compute the unicode to use as a replacement -> 'replace', and # the current position in the input 'unicodedata' -> 'end' start = pypy_cjk_dec_inbuf_consumed(decodebuf) end = start + esize if errors == "strict": raise EncodeDecodeError(start, end, reason) elif errors == "ignore": replace = "" elif errors == "replace": replace = UNICODE_REPLACEMENT_CHARACTER else: assert errorcb replace, end, rettype, obj = errorcb(errors, namecb, reason, stringdata, start, end) # 'replace' is UTF8 encoded unicode, rettype is 'u' lgt = rutf8.codepoints_in_utf8(replace) inbuf = rffi.utf82wcharp(replace, lgt) try: r = pypy_cjk_dec_replace_on_error(decodebuf, inbuf, lgt, end) finally: lltype.free(inbuf, flavor='raw') if r == MBERR_NOMEMORY: raise MemoryError
def strcoll(space, w_s1, w_s2): "string,string -> int. Compares two strings according to the locale." s1, l1 = space.utf8_len_w(w_s1) s2, l2 = space.utf8_len_w(w_s2) if '\x00' in s1 or '\x00' in s2: raise oefmt(space.w_ValueError, "embedded null character") s1_c = rffi.utf82wcharp(s1, l1) s2_c = rffi.utf82wcharp(s2, l2) try: result = _wcscoll(s1_c, s2_c) finally: rffi.free_wcharp(s1_c) rffi.free_wcharp(s2_c) return space.newint(result)
def PyUnicode_AS_UNICODE(space, ref): """Return a pointer to the internal Py_UNICODE buffer of the object. ref has to be a PyUnicodeObject (not checked).""" ref_unicode = rffi.cast(PyUnicodeObject, ref) if not ref_unicode.c_str: # Copy unicode buffer w_unicode = from_ref(space, rffi.cast(PyObject, ref)) u, length = space.utf8_len_w(w_unicode) ref_unicode.c_str = rffi.utf82wcharp(u, length) return ref_unicode.c_str
def stop_error_capture(self, w_done): if w_done is None: return w_text = self.space.call_function(w_done) p = rffi.utf82wcharp(self.space.utf8_w(w_text), self.space.len_w(w_text), track_allocation=False) if self.text_p: rffi.free_wcharp(self.text_p, track_allocation=False) self.text_p = p # keepalive cffi_errorbox(p)
def get_programname(self): if not self.programname: space = self.space argv = space.sys.get('argv') if space.len_w(argv): argv0 = space.getitem(argv, space.newint(0)) progname = space.utf8_w(argv0) lgt = space.len_w(argv0) else: progname = "pypy3" lgt = len(progname) self.programname = rffi.utf82wcharp(progname, lgt) lltype.render_immortal(self.programname) return self.programname
def descr_init(self, space, w_nameobj, mode='r', closefd=True, w_opener=None): name = rffi.cast(rffi.CWCHARP, 0) self.fd = -1 self.handle = rwin32.INVALID_HANDLE_VALUE self.readable = False self.writable = False self.blksize = 0 rwa = False console_type = '\0' self.buf = '' if space.isinstance_w(w_nameobj, space.w_int): self.fd = space.int_w(w_nameobj) if self.fd < 0: raise oefmt(space.w_ValueError, "negative file descriptor") # make the flow analysis happy,otherwise it thinks w_path # is undefined later w_path = w_nameobj if self.fd < 0: from pypy.module.posix.interp_posix import fspath w_path = fspath(space, w_nameobj) console_type = _pyio_get_console_type(space, w_path) if not console_type: raise oefmt(space.w_ValueError, "Invalid console type") if console_type == '\0': raise oefmt(space.w_ValueError, "Cannot open non-console file") self.mode = 'u' for char in mode: if char in "+ax": # OK do nothing pass elif char == "b": self.mode = 'b' elif char == "r": if rwa: raise oefmt(space.w_ValueError, "invalid mode: %s", mode) rwa = True self.readable = True if console_type == "x": console_type = "r" elif char == "w": if rwa: raise oefmt(space.w_ValueError, "invalid mode: %s", mode) rwa = True self.writable = True if console_type == 'x': console_type = 'w' else: raise oefmt(space.w_ValueError, "invalid mode: %s", mode) if not rwa: raise oefmt(space.w_ValueError, "Must have exactly one of read or write mode") if self.fd >= 0: self.handle = rwin32.get_osfhandle(self.fd) self.closehandle = False else: access = rwin32.GENERIC_READ self.closehandle = True if not closefd: raise oefmt(space.w_ValueError, "Cannot use closefd=False with a file name") if self.writable: access = rwin32.GENERIC_WRITE traits = _preferred_traits(space.realunicode_w(w_path)) if not (traits.str is unicode): raise oefmt(space.w_ValueError, "Non-unicode string name %s", traits.str) win32traits = make_win32_traits(traits) pathlen = space.len_w(w_path) name = rffi.utf82wcharp(space.utf8_w(w_path), pathlen) self.handle = win32traits.CreateFile( name, rwin32.GENERIC_READ | rwin32.GENERIC_WRITE, rwin32.FILE_SHARE_READ | rwin32.FILE_SHARE_WRITE, rffi.NULL, win32traits.OPEN_EXISTING, 0, rffi.cast(rwin32.HANDLE, 0)) if self.handle == rwin32.INVALID_HANDLE_VALUE: self.handle = win32traits.CreateFile( name, access, rwin32.FILE_SHARE_READ | rwin32.FILE_SHARE_WRITE, rffi.NULL, win32traits.OPEN_EXISTING, 0, rffi.cast(rwin32.HANDLE, 0)) lltype.free(name, flavor='raw') if self.handle == rwin32.INVALID_HANDLE_VALUE: raise WindowsError(rwin32.GetLastError_saved(), "Failed to open handle") if console_type == '\0': console_type = _get_console_type(self.handle) if console_type == '\0': raise oefmt(space.w_ValueError, "Cannot open non-console file") if self.writable and console_type != 'w': raise oefmt(space.w_ValueError, "Cannot open input buffer for writing") if self.readable and console_type != 'r': raise oefmt(space.w_ValueError, "Cannot open output buffer for reading") self.blksize = DEFAULT_BUFFER_SIZE
def handle_unichar_p(self, w_ffitype, w_obj, utf8val, utf8len): buf = rffi.utf82wcharp(utf8val, utf8len) self.w_func.to_free.append(rffi.cast(rffi.VOIDP, buf)) addr = rffi.cast(rffi.ULONG, buf) self.argchain.arg(addr)