本文整理汇总了Python中pypy.rpython.lltypesystem.lltype.scoped_alloc函数的典型用法代码示例。如果您正苦于以下问题:Python scoped_alloc函数的具体用法?Python scoped_alloc怎么用?Python scoped_alloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了scoped_alloc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _operate
def _operate(stream, data, flush, max_length, cfunc, while_doing):
"""Common code for compress() and decompress().
"""
# Prepare the input buffer for the stream
with lltype.scoped_alloc(rffi.CCHARP.TO, len(data)) as inbuf:
for i in xrange(len(data)):
inbuf[i] = data[i]
stream.c_next_in = rffi.cast(Bytefp, inbuf)
rffi.setintfield(stream, 'c_avail_in', len(data))
# Prepare the output buffer
with lltype.scoped_alloc(rffi.CCHARP.TO, OUTPUT_BUFFER_SIZE) as outbuf:
# Strategy: we call deflate() to get as much output data as fits in
# the buffer, then accumulate all output into a StringBuffer
# 'result'.
result = StringBuilder()
while True:
stream.c_next_out = rffi.cast(Bytefp, outbuf)
bufsize = OUTPUT_BUFFER_SIZE
if max_length < bufsize:
if max_length <= 0:
err = Z_OK
break
bufsize = max_length
max_length -= bufsize
rffi.setintfield(stream, 'c_avail_out', bufsize)
err = cfunc(stream, flush)
if err == Z_OK or err == Z_STREAM_END:
# accumulate data into 'result'
avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
result.append_charpsize(outbuf, bufsize - avail_out)
# if the output buffer is full, there might be more data
# so we need to try again. Otherwise, we're done.
if avail_out > 0:
break
# We're also done if we got a Z_STREAM_END (which should
# only occur when flush == Z_FINISH).
if err == Z_STREAM_END:
break
else:
continue
elif err == Z_BUF_ERROR:
avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
# When compressing, we will only get Z_BUF_ERROR if
# the output buffer was full but there wasn't more
# output when we tried again, so it is not an error
# condition.
if avail_out == bufsize:
break
# fallback case: report this error
raise RZlibError.fromstream(stream, err, while_doing)
# When decompressing, if the compressed stream of data was truncated,
# then the zlib simply returns Z_OK and waits for more. If it is
# complete it returns Z_STREAM_END.
return (result.build(),
err,
rffi.cast(lltype.Signed, stream.c_avail_in))
示例2: EnumKey
def EnumKey(space, w_hkey, index):
"""string = EnumKey(key, index) - Enumerates subkeys of an open registry key.
key is an already open key, or any one of the predefined HKEY_* constants.
index is an integer that identifies the index of the key to retrieve.
The function retrieves the name of one subkey each time it is called.
It is typically called repeatedly until an EnvironmentError exception is
raised, indicating no more values are available."""
hkey = hkey_w(w_hkey, space)
null_dword = lltype.nullptr(rwin32.LPDWORD.TO)
# The Windows docs claim that the max key name length is 255
# characters, plus a terminating nul character. However,
# empirical testing demonstrates that it is possible to
# create a 256 character key that is missing the terminating
# nul. RegEnumKeyEx requires a 257 character buffer to
# retrieve such a key name.
with lltype.scoped_alloc(rffi.CCHARP.TO, 257) as buf:
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as retValueSize:
retValueSize[0] = r_uint(257) # includes NULL terminator
ret = rwinreg.RegEnumKeyEx(hkey, index, buf, retValueSize,
null_dword, None, null_dword,
lltype.nullptr(rwin32.PFILETIME.TO))
if ret != 0:
raiseWindowsError(space, ret, 'RegEnumKeyEx')
return space.wrap(rffi.charp2str(buf))
示例3: QueryInfoKey
def QueryInfoKey(space, w_hkey):
"""tuple = QueryInfoKey(key) - Returns information about a key.
key is an already open key, or any one of the predefined HKEY_* constants.
The result is a tuple of 3 items:
An integer that identifies the number of sub keys this key has.
An integer that identifies the number of values this key has.
A long integer that identifies when the key was last modified (if available)
as 100's of nanoseconds since Jan 1, 1600."""
hkey = hkey_w(w_hkey, space)
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as nSubKeys:
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as nValues:
with lltype.scoped_alloc(rwin32.PFILETIME.TO, 1) as ft:
null_dword = lltype.nullptr(rwin32.LPDWORD.TO)
ret = rwinreg.RegQueryInfoKey(
hkey, None, null_dword, null_dword,
nSubKeys, null_dword, null_dword,
nValues, null_dword, null_dword,
null_dword, ft)
if ret != 0:
raiseWindowsError(space, ret, 'RegQueryInfoKey')
l = ((lltype.r_longlong(ft[0].c_dwHighDateTime) << 32) +
lltype.r_longlong(ft[0].c_dwLowDateTime))
return space.newtuple([space.wrap(nSubKeys[0]),
space.wrap(nValues[0]),
space.wrap(l)])
示例4: _decode_certificate
def _decode_certificate(space, certificate, verbose=False):
w_retval = space.newdict()
w_peer = _create_tuple_for_X509_NAME(
space, libssl_X509_get_subject_name(certificate))
space.setitem(w_retval, space.wrap("subject"), w_peer)
if verbose:
w_issuer = _create_tuple_for_X509_NAME(
space, libssl_X509_get_issuer_name(certificate))
space.setitem(w_retval, space.wrap("issuer"), w_issuer)
space.setitem(w_retval, space.wrap("version"),
space.wrap(libssl_X509_get_version(certificate)))
biobuf = libssl_BIO_new(libssl_BIO_s_mem())
try:
if verbose:
libssl_BIO_reset(biobuf)
serialNumber = libssl_X509_get_serialNumber(certificate)
libssl_i2a_ASN1_INTEGER(biobuf, serialNumber)
# should not exceed 20 octets, 160 bits, so buf is big enough
with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
length = libssl_BIO_gets(biobuf, buf, 99)
if length < 0:
raise _ssl_seterror(space, None, length)
w_serial = space.wrap(rffi.charpsize2str(buf, length))
space.setitem(w_retval, space.wrap("serialNumber"), w_serial)
libssl_BIO_reset(biobuf)
notBefore = libssl_X509_get_notBefore(certificate)
libssl_ASN1_TIME_print(biobuf, notBefore)
with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
length = libssl_BIO_gets(biobuf, buf, 99)
if length < 0:
raise _ssl_seterror(space, None, length)
w_date = space.wrap(rffi.charpsize2str(buf, length))
space.setitem(w_retval, space.wrap("notBefore"), w_date)
libssl_BIO_reset(biobuf)
notAfter = libssl_X509_get_notAfter(certificate)
libssl_ASN1_TIME_print(biobuf, notAfter)
with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
length = libssl_BIO_gets(biobuf, buf, 99)
if length < 0:
raise _ssl_seterror(space, None, length)
w_date = space.wrap(rffi.charpsize2str(buf, length))
space.setitem(w_retval, space.wrap("notAfter"), w_date)
finally:
libssl_BIO_free(biobuf)
# Now look for subjectAltName
w_alt_names = _get_peer_alt_names(space, certificate)
if w_alt_names is not space.w_None:
space.setitem(w_retval, space.wrap("subjectAltName"), w_alt_names)
return w_retval
示例5: _digest
def _digest(self, space):
with lltype.scoped_alloc(ropenssl.EVP_MD_CTX.TO) as ctx:
with self.lock:
ropenssl.EVP_MD_CTX_copy(ctx, self.ctx)
digest_size = self.digest_size
with lltype.scoped_alloc(rffi.CCHARP.TO, digest_size) as digest:
ropenssl.EVP_DigestFinal(ctx, digest, None)
ropenssl.EVP_MD_CTX_cleanup(ctx)
return rffi.charpsize2str(digest, digest_size)
示例6: setitimer
def setitimer(space, which, first, interval=0):
with lltype.scoped_alloc(itimervalP.TO, 1) as new:
timeval_from_double(first, new[0].c_it_value)
timeval_from_double(interval, new[0].c_it_interval)
with lltype.scoped_alloc(itimervalP.TO, 1) as old:
ret = c_setitimer(which, new, old)
if ret != 0:
raise exception_from_errno(space, get_itimer_error(space))
return itimer_retval(space, old[0])
示例7: _create_tuple_for_attribute
def _create_tuple_for_attribute(space, name, value):
with lltype.scoped_alloc(rffi.CCHARP.TO, X509_NAME_MAXLEN) as buf:
length = libssl_OBJ_obj2txt(buf, X509_NAME_MAXLEN, name, 0)
if length < 0:
raise _ssl_seterror(space, None, 0)
w_name = space.wrap(rffi.charpsize2str(buf, length))
with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
length = libssl_ASN1_STRING_to_UTF8(buf_ptr, value)
if length < 0:
raise _ssl_seterror(space, None, 0)
w_value = space.wrap(rffi.charpsize2str(buf_ptr[0], length))
w_value = space.call_method(w_value, "decode", space.wrap("utf-8"))
return space.newtuple([w_name, w_value])
示例8: peer_certificate
def peer_certificate(self, der=False):
"""peer_certificate([der=False]) -> certificate
Returns the certificate for the peer. If no certificate was provided,
returns None. If a certificate was provided, but not validated, returns
an empty dictionary. Otherwise returns a dict containing information
about the peer certificate.
If the optional argument is True, returns a DER-encoded copy of the
peer certificate, or None if no certificate was provided. This will
return the certificate even if it wasn't validated."""
if not self.peer_cert:
return self.space.w_None
if der:
# return cert in DER-encoded format
with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
buf_ptr[0] = lltype.nullptr(rffi.CCHARP.TO)
length = libssl_i2d_X509(self.peer_cert, buf_ptr)
if length < 0:
raise _ssl_seterror(self.space, self, length)
try:
# this is actually an immutable bytes sequence
return self.space.wrap(rffi.charp2str(buf_ptr[0]))
finally:
libssl_OPENSSL_free(buf_ptr[0])
else:
verification = libssl_SSL_CTX_get_verify_mode(
libssl_SSL_get_SSL_CTX(self.ssl))
if not verification & SSL_VERIFY_PEER:
return self.space.newdict()
else:
return _decode_certificate(self.space, self.peer_cert)
示例9: setupterm_func
def setupterm_func(vm):
mod = vm.get_funcs_mod()
(term_o, file_o), _ = vm.decode_args(opt="sO")
if HAVE_CURSES:
if term_o:
assert isinstance(term_o, Con_String)
raise Exception("XXX")
else:
term = None
if file_o:
fd = type_check_int(vm, vm.get_slot_apply(file_o, "fileno")).v
else:
fd = STDOUT_FILENO
with lltype.scoped_alloc(rffi.INTP.TO, 1) as erret:
if setupterm(term, fd, erret) != OK:
ec = int(erret[0])
if ec == -1:
msg = "Can't find terminfo database."
elif ec == 0:
msg = "Terminal not found or not enough information known about it."
elif ec == 1:
msg = "Terminal is hardcopy."
else:
raise Exception("XXX")
cex_class = mod.get_defn(vm, "Curses_Exception")
vm.raise_(vm.get_slot_apply(cex_class, "new", [Con_String(vm, msg)]))
else:
raise Exception("XXX")
return vm.get_builtin(BUILTIN_NULL_OBJ)
示例10: File_read
def File_read(vm):
(self, rsize_o),_ = vm.decode_args(mand="!", opt="I", self_of=File)
assert isinstance(self, File)
_check_open(vm, self)
flockfile(self.filep)
fsize = os.fstat(fileno(self.filep)).st_size
if rsize_o is None:
rsize = fsize
else:
assert isinstance(rsize_o, Con_Int)
rsize = rsize_o.v
if rsize < 0:
vm.raise_helper("File_Exception", \
[Con_String(vm, "Can not read less than 0 bytes from file.")])
elif rsize > fsize:
rsize = fsize
if objectmodel.we_are_translated():
with lltype.scoped_alloc(rffi.CCHARP.TO, rsize) as buf:
r = fread(buf, 1, rsize, self.filep)
if r < rffi.r_size_t(rsize) and ferror(self.filep) != 0:
vm.raise_helper("File_Exception", [Con_String(vm, "Read error.")])
s = rffi.charpsize2str(buf, rarithmetic.intmask(r))
else:
# rffi.charpsize2str is so slow (taking minutes for big strings) that it's worth bypassing
# it when things are run untranslated.
s = os.read(fileno(self.filep), rsize)
funlockfile(self.filep)
return Con_String(vm, s)
示例11: wrap_getreadbuffer
def wrap_getreadbuffer(space, w_self, w_args, func):
func_target = rffi.cast(readbufferproc, func)
with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as ptr:
index = rffi.cast(Py_ssize_t, 0)
size = generic_cpy_call(space, func_target, w_self, index, ptr)
if size < 0:
space.fromcache(State).check_and_raise_exception(always=True)
return space.wrap(CPyBuffer(ptr[0], size, w_self))
示例12: current
def current(vm):
_,_ = vm.decode_args()
with lltype.scoped_alloc(TIMEVAL) as tp:
if gettimeofday(tp, lltype.nullptr(TIMEZONEP.TO)) != 0:
raise Exception("XXX")
sec = rarithmetic.r_int(tp.c_tv_sec)
usec = rarithmetic.r_int(tp.c_tv_usec)
return Con_List(vm, [Con_Int(vm, sec), Con_Int(vm, usec * 1000)])
示例13: current_mono
def current_mono(vm):
_,_ = vm.decode_args()
with lltype.scoped_alloc(TIMESPEC) as ts:
if clock_gettime(CLOCK_MONOTONIC, ts) != 0:
raise Exception("XXX")
sec = rarithmetic.r_int(ts.c_tv_sec)
nsec = rarithmetic.r_int(ts.c_tv_nsec)
return Con_List(vm, [Con_Int(vm, sec), Con_Int(vm, nsec)])
示例14: test_cmp
def test_cmp(self, space, api):
w = space.wrap
with lltype.scoped_alloc(rffi.INTP.TO, 1) as ptr:
assert api.PyObject_Cmp(w(42), w(72), ptr) == 0
assert ptr[0] == -1
assert api.PyObject_Cmp(w("a"), w("a"), ptr) == 0
assert ptr[0] == 0
assert api.PyObject_Cmp(w(u"\xe9"), w("\xe9"), ptr) < 0
assert api.PyErr_Occurred()
api.PyErr_Clear()
示例15: canon_path
def canon_path(vm):
(p_o,),_ = vm.decode_args("S")
assert isinstance(p_o, Con_String)
with lltype.scoped_alloc(rffi.CCHARP.TO, PATH_MAX) as resolved:
r = realpath(p_o.v, resolved)
if not r:
_errno_raise(vm, p_o)
rp = rffi.charpsize2str(resolved, rarithmetic.intmask(strlen(resolved)))
return Con_String(vm, rp)