def __init__(self, space, environment, context, retrieveError): self.context = context if retrieveError: if environment.errorHandle: handle = environment.errorHandle handleType = roci.OCI_HTYPE_ERROR else: handle = environment.handle handleType = roci.OCI_HTYPE_ENV codeptr = lltype.malloc(rffi.CArray(roci.sb4), 1, flavor='raw') BUFSIZE = 1024 textbuf, text = rffi.alloc_buffer(BUFSIZE) try: status = roci.OCIErrorGet( handle, 1, lltype.nullptr(roci.oratext.TO), codeptr, textbuf, BUFSIZE, handleType) if status != roci.OCI_SUCCESS: raise OperationError( get(space).w_InternalError, space.wrap("No Oracle error?")) self.code = codeptr[0] self.w_message = config.w_string(space, textbuf) finally: lltype.free(codeptr, flavor='raw') rffi.keep_buffer_alive_until_here(textbuf, text) if config.WITH_UNICODE: # XXX remove double zeros at the end pass
def unicode_encode_mbcs(p, size, errors, errorhandler=None): dataptr = rffi.get_nonmoving_unicodebuffer(p) try: # first get the size of the result if size > 0: mbcssize = WideCharToMultiByte(CP_ACP, 0, dataptr, size, None, 0, None, None) if mbcssize == 0: raise rwin32.lastWindowsError() else: mbcssize = 0 raw_buf, gc_buf = rffi.alloc_buffer(mbcssize) try: # do the conversion if WideCharToMultiByte(CP_ACP, 0, dataptr, size, raw_buf, mbcssize, None, None) == 0: raise rwin32.lastWindowsError() return rffi.str_from_buffer(raw_buf, gc_buf, mbcssize, mbcssize) finally: rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) finally: rffi.free_nonmoving_unicodebuffer(p, dataptr)
def test_leak(self): size = 50 raw_buf, gc_buf = rffi.alloc_buffer(size) for i in range(size): raw_buf[i] = "a" str = rffi.str_from_buffer(raw_buf, gc_buf, size, size) rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
def recvfrom(self, buffersize, flags=0): """Like recv(buffersize, flags) but also return the sender's address.""" read_bytes = -1 timeout = self._select(False) if timeout == 1: raise SocketTimeout elif timeout == 0: raw_buf, gc_buf = rffi.alloc_buffer(buffersize) try: address, addr_p, addrlen_p = self._addrbuf() try: read_bytes = _c.recvfrom(self.fd, raw_buf, buffersize, flags, addr_p, addrlen_p) addrlen = rffi.cast(lltype.Signed, addrlen_p[0]) finally: lltype.free(addrlen_p, flavor='raw') address.unlock() if read_bytes >= 0: if addrlen: address.addrlen = addrlen else: address = None data = rffi.str_from_buffer(raw_buf, gc_buf, buffersize, read_bytes) return (data, address) finally: rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) raise self.error_handler()
def test_leak(self): size = 50 raw_buf, gc_buf = rffi.alloc_buffer(size) for i in range(size): raw_buf[i] = 'a' str = rffi.str_from_buffer(raw_buf, gc_buf, size, size) rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
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 test_str_from_buffer(self): """gc-managed memory does not need to be freed""" size = 50 raw_buf, gc_buf = rffi.alloc_buffer(size) for i in range(size): raw_buf[i] = 'a' rstr = rffi.str_from_buffer(raw_buf, gc_buf, size, size) rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) assert not leakfinder.ALLOCATED
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 read(self, num_bytes=1024): """read([len]) -> string Read up to len bytes from the SSL socket.""" count = libssl_SSL_pending(self.ssl) if not count: sockstate = check_socket_and_wait_for_timeout( self.space, self.w_socket, False) if sockstate == SOCKET_HAS_TIMED_OUT: raise ssl_error(self.space, "The read operation timed out") elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT: raise ssl_error(self.space, "Underlying socket too large for select().") elif sockstate == SOCKET_HAS_BEEN_CLOSED: if libssl_SSL_get_shutdown(self.ssl) == SSL_RECEIVED_SHUTDOWN: return self.space.wrap('') raise ssl_error( self.space, "Socket closed without SSL shutdown handshake") raw_buf, gc_buf = rffi.alloc_buffer(num_bytes) while True: err = 0 count = libssl_SSL_read(self.ssl, raw_buf, num_bytes) err = libssl_SSL_get_error(self.ssl, count) if err == SSL_ERROR_WANT_READ: sockstate = check_socket_and_wait_for_timeout( self.space, self.w_socket, False) elif err == SSL_ERROR_WANT_WRITE: sockstate = check_socket_and_wait_for_timeout( self.space, self.w_socket, True) elif (err == SSL_ERROR_ZERO_RETURN and libssl_SSL_get_shutdown( self.ssl) == SSL_RECEIVED_SHUTDOWN): return self.space.wrap("") else: sockstate = SOCKET_OPERATION_OK if sockstate == SOCKET_HAS_TIMED_OUT: raise ssl_error(self.space, "The read operation timed out") elif sockstate == SOCKET_IS_NONBLOCKING: break if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE: continue else: break if count <= 0: raise _ssl_seterror(self.space, self, count) result = rffi.str_from_buffer(raw_buf, gc_buf, num_bytes, count) rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) return self.space.wrap(result)
def read(self, num_bytes=1024): """read([len]) -> string Read up to len bytes from the SSL socket.""" count = libssl_SSL_pending(self.ssl) if not count: sockstate = check_socket_and_wait_for_timeout(self.space, self.w_socket, False) if sockstate == SOCKET_HAS_TIMED_OUT: raise ssl_error(self.space, "The read operation timed out") elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT: raise ssl_error(self.space, "Underlying socket too large for select().") elif sockstate == SOCKET_HAS_BEEN_CLOSED: if libssl_SSL_get_shutdown(self.ssl) == SSL_RECEIVED_SHUTDOWN: return self.space.wrap('') raise ssl_error(self.space, "Socket closed without SSL shutdown handshake") raw_buf, gc_buf = rffi.alloc_buffer(num_bytes) while True: err = 0 count = libssl_SSL_read(self.ssl, raw_buf, num_bytes) err = libssl_SSL_get_error(self.ssl, count) if err == SSL_ERROR_WANT_READ: sockstate = check_socket_and_wait_for_timeout(self.space, self.w_socket, False) elif err == SSL_ERROR_WANT_WRITE: sockstate = check_socket_and_wait_for_timeout(self.space, self.w_socket, True) elif (err == SSL_ERROR_ZERO_RETURN and libssl_SSL_get_shutdown(self.ssl) == SSL_RECEIVED_SHUTDOWN): return self.space.wrap("") else: sockstate = SOCKET_OPERATION_OK if sockstate == SOCKET_HAS_TIMED_OUT: raise ssl_error(self.space, "The read operation timed out") elif sockstate == SOCKET_IS_NONBLOCKING: break if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE: continue else: break if count <= 0: raise _ssl_seterror(self.space, self, count) result = rffi.str_from_buffer(raw_buf, gc_buf, num_bytes, count) rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) return self.space.wrap(result)
def read(self, num_bytes=1024): """read([len]) -> string Read up to len bytes from the SSL socket.""" count = libssl_SSL_pending(self.ssl) if not count: sockstate = check_socket_and_wait_for_timeout( self.space, self.w_socket, False) if sockstate == SOCKET_HAS_TIMED_OUT: raise ssl_error(self.space, "The read operation timed out") elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT: raise ssl_error(self.space, "Underlying socket too large for select().") raw_buf, gc_buf = rffi.alloc_buffer(num_bytes) while True: err = 0 count = libssl_SSL_read(self.ssl, raw_buf, num_bytes) err = libssl_SSL_get_error(self.ssl, count) if err == SSL_ERROR_WANT_READ: sockstate = check_socket_and_wait_for_timeout( self.space, self.w_socket, False) elif err == SSL_ERROR_WANT_WRITE: sockstate = check_socket_and_wait_for_timeout( self.space, self.w_socket, True) else: sockstate = SOCKET_OPERATION_OK if sockstate == SOCKET_HAS_TIMED_OUT: raise ssl_error(self.space, "The read operation timed out") elif sockstate == SOCKET_IS_NONBLOCKING: break if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE: continue else: break if count <= 0: errstr, errval = _ssl_seterror(self.space, self, count) raise ssl_error(self.space, "%s: %d" % (errstr, errval)) result = rffi.str_from_buffer(raw_buf, gc_buf, num_bytes, count) rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) return self.space.wrap(result)
def read(self, num_bytes=1024): """read([len]) -> string Read up to len bytes from the SSL socket.""" count = libssl_SSL_pending(self.ssl) if not count: sockstate = check_socket_and_wait_for_timeout(self.space, self.w_socket, False) if sockstate == SOCKET_HAS_TIMED_OUT: raise ssl_error(self.space, "The read operation timed out") elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT: raise ssl_error(self.space, "Underlying socket too large for select().") raw_buf, gc_buf = rffi.alloc_buffer(num_bytes) while True: err = 0 count = libssl_SSL_read(self.ssl, raw_buf, num_bytes) err = libssl_SSL_get_error(self.ssl, count) if err == SSL_ERROR_WANT_READ: sockstate = check_socket_and_wait_for_timeout(self.space, self.w_socket, False) elif err == SSL_ERROR_WANT_WRITE: sockstate = check_socket_and_wait_for_timeout(self.space, self.w_socket, True) else: sockstate = SOCKET_OPERATION_OK if sockstate == SOCKET_HAS_TIMED_OUT: raise ssl_error(self.space, "The read operation timed out") elif sockstate == SOCKET_IS_NONBLOCKING: break if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE: continue else: break if count <= 0: errstr, errval = _ssl_seterror(self.space, self, count) raise ssl_error(self.space, "%s: %d" % (errstr, errval)) result = rffi.str_from_buffer(raw_buf, gc_buf, num_bytes, count) rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) return self.space.wrap(result)
def recv(self, buffersize, flags=0): """Receive up to buffersize bytes from the socket. For the optional flags argument, see the Unix manual. When no data is available, block until at least one byte is available or until the remote end is closed. When the remote end is closed and all data is read, return the empty string.""" timeout = self._select(False) if timeout == 1: raise SocketTimeout elif timeout == 0: raw_buf, gc_buf = rffi.alloc_buffer(buffersize) try: read_bytes = _c.socketrecv(self.fd, raw_buf, buffersize, flags) if read_bytes >= 0: return rffi.str_from_buffer(raw_buf, gc_buf, buffersize, read_bytes) finally: rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) raise self.error_handler()
def _allocate_chunk(self, size): self.raw_buf, self.gc_buf = rffi.alloc_buffer(size) self.current_size = size self.bzs.c_next_out = self.raw_buf rffi.setintfield(self.bzs, 'c_avail_out', size)