本文整理汇总了Python中pypy.rpython.lltypesystem.lltype.scoped_alloc函数的典型用法代码示例。如果您正苦于以下问题:Python scoped_alloc函数的具体用法?Python scoped_alloc怎么用?Python scoped_alloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了scoped_alloc函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:60,代码来源:rzlib.py
示例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))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:27,代码来源:interp_winreg.py
示例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)])
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:27,代码来源:interp_winreg.py
示例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
开发者ID:gorakhargosh,项目名称:pypy,代码行数:59,代码来源:interp_ssl.py
示例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)
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:9,代码来源:interp_hashlib.py
示例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])
开发者ID:gorakhargosh,项目名称:pypy,代码行数:14,代码来源:interp_signal.py
示例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])
开发者ID:gorakhargosh,项目名称:pypy,代码行数:15,代码来源:interp_ssl.py
示例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)
开发者ID:gorakhargosh,项目名称:pypy,代码行数:33,代码来源:interp_ssl.py
示例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)
开发者ID:cfbolz,项目名称:converge,代码行数:34,代码来源:Con_Curses.py
示例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)
开发者ID:cfbolz,项目名称:converge,代码行数:31,代码来源:Con_POSIX_File.py
示例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))
开发者ID:gorakhargosh,项目名称:pypy,代码行数:8,代码来源:slotdefs.py
示例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)])
开发者ID:lucciano,项目名称:converge,代码行数:10,代码来源:Con_C_Time.py
示例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)])
开发者ID:lucciano,项目名称:converge,代码行数:10,代码来源:Con_C_Time.py
示例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()
开发者ID:gorakhargosh,项目名称:pypy,代码行数:10,代码来源:test_object.py
示例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)
开发者ID:cfbolz,项目名称:converge,代码行数:11,代码来源:Con_POSIX_File.py
示例16: QueryValueEx
def QueryValueEx(space, w_hkey, w_subkey):
"""value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.
key is an already open key, or any one of the predefined HKEY_* constants.
value_name is a string indicating the value to query"""
hkey = hkey_w(w_hkey, space)
if space.is_w(w_subkey, space.w_None):
subkey = None
else:
subkey = space.str_w(w_subkey)
null_dword = lltype.nullptr(rwin32.LPDWORD.TO)
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as retDataSize:
ret = rwinreg.RegQueryValueEx(hkey, subkey, null_dword, null_dword,
None, retDataSize)
bufSize = intmask(retDataSize[0])
if ret == rwinreg.ERROR_MORE_DATA:
bufSize = 256
elif ret != 0:
raiseWindowsError(space, ret, 'RegQueryValueEx')
while True:
with lltype.scoped_alloc(rffi.CCHARP.TO, bufSize) as databuf:
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as retType:
ret = rwinreg.RegQueryValueEx(hkey, subkey, null_dword,
retType, databuf, retDataSize)
if ret == rwinreg.ERROR_MORE_DATA:
# Resize and retry
bufSize *= 2
retDataSize[0] = rffi.cast(rwin32.DWORD, bufSize)
continue
if ret != 0:
raiseWindowsError(space, ret, 'RegQueryValueEx')
length = intmask(retDataSize[0])
return space.newtuple([
convert_from_regdata(space, databuf,
length, retType[0]),
space.wrap(retType[0]),
])
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:39,代码来源:interp_winreg.py
示例17: File_readln
def File_readln(vm):
(self,),_ = vm.decode_args(mand="!", self_of=File)
assert isinstance(self, File)
_check_open(vm, self)
while 1:
with lltype.scoped_alloc(rffi.SIZE_TP.TO, 1) as lenp:
l = fgetln(self.filep, lenp)
if not l:
if feof(self.filep) != 0:
break
_errno_raise(vm, self.get_slot(vm, "path"))
l_o = Con_String(vm, rffi.charpsize2str(l, rarithmetic.intmask(lenp[0])))
yield l_o
开发者ID:cfbolz,项目名称:converge,代码行数:14,代码来源:Con_POSIX_File.py
示例18: QueryValue
def QueryValue(space, w_hkey, w_subkey):
"""string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.
key is an already open key, or any one of the predefined HKEY_* constants.
sub_key is a string that holds the name of the subkey with which the value
is associated. If this parameter is None or empty, the function retrieves
the value set by the SetValue() method for the key identified by key.
Values in the registry have name, type, and data components. This method
retrieves the data for a key's first value that has a NULL name.
But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!"""
hkey = hkey_w(w_hkey, space)
if space.is_w(w_subkey, space.w_None):
subkey = None
else:
subkey = space.str_w(w_subkey)
with lltype.scoped_alloc(rwin32.PLONG.TO, 1) as bufsize_p:
ret = rwinreg.RegQueryValue(hkey, subkey, None, bufsize_p)
bufSize = intmask(bufsize_p[0])
if ret == rwinreg.ERROR_MORE_DATA:
bufSize = 256
elif ret != 0:
raiseWindowsError(space, ret, 'RegQueryValue')
while True:
with lltype.scoped_alloc(rffi.CCHARP.TO, bufSize) as buf:
ret = rwinreg.RegQueryValue(hkey, subkey, buf, bufsize_p)
if ret == rwinreg.ERROR_MORE_DATA:
# Resize and retry
bufSize *= 2
bufsize_p[0] = bufSize
continue
if ret != 0:
raiseWindowsError(space, ret, 'RegQueryValue')
length = intmask(bufsize_p[0] - 1)
return space.wrap(rffi.charp2strn(buf, length))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:37,代码来源:interp_winreg.py
示例19: do_recv_string
def do_recv_string(self, space, buflength, maxlength):
with lltype.scoped_alloc(rffi.CArrayPtr(rffi.UINT).TO, 1) as length_ptr:
self._recvall(space, rffi.cast(rffi.CCHARP, length_ptr), 4)
length = intmask(length_ptr[0])
if length > maxlength: # bad message, close connection
self.flags &= ~READABLE
if self.flags == 0:
self.close()
raise OperationError(space.w_IOError, space.wrap("bad message length"))
if length <= buflength:
self._recvall(space, self.buffer, length)
return length, lltype.nullptr(rffi.CCHARP.TO)
else:
newbuf = lltype.malloc(rffi.CCHARP.TO, length, flavor="raw")
self._recvall(space, newbuf, length)
return length, newbuf
开发者ID:gorakhargosh,项目名称:pypy,代码行数:17,代码来源:interp_connection.py
示例20: OpenKey
def OpenKey(space, w_hkey, subkey, res=0, sam=rwinreg.KEY_READ):
"""key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.
key is an already open key, or any one of the predefined HKEY_* constants.
sub_key is a string that identifies the sub_key to open
res is a reserved integer, and must be zero. Default is zero.
sam is an integer that specifies an access mask that describes the desired
security access for the key. Default is KEY_READ
The result is a new handle to the specified key
If the function fails, an EnvironmentError exception is raised."""
hkey = hkey_w(w_hkey, space)
with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey:
ret = rwinreg.RegOpenKeyEx(hkey, subkey, res, sam, rethkey)
if ret != 0:
raiseWindowsError(space, ret, 'RegOpenKeyEx')
return space.wrap(W_HKEY(rethkey[0]))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:17,代码来源:interp_winreg.py
注:本文中的pypy.rpython.lltypesystem.lltype.scoped_alloc函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论