本文整理汇总了Python中pypy.rpython.lltypesystem.rffi.charpsize2str函数的典型用法代码示例。如果您正苦于以下问题:Python charpsize2str函数的具体用法?Python charpsize2str怎么用?Python charpsize2str使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了charpsize2str函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: detect_floatformat
def detect_floatformat():
from pypy.rpython.lltypesystem import rffi, lltype
buf = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw')
rffi.cast(rffi.DOUBLEP, buf)[0] = 9006104071832581.0
packed = rffi.charpsize2str(buf, 8)
if packed == "\x43\x3f\xff\x01\x02\x03\x04\x05":
double_format = 'IEEE, big-endian'
elif packed == "\x05\x04\x03\x02\x01\xff\x3f\x43":
double_format = 'IEEE, little-endian'
else:
double_format = 'unknown'
lltype.free(buf, flavor='raw')
#
buf = lltype.malloc(rffi.CCHARP.TO, 4, flavor='raw')
rffi.cast(rffi.FLOATP, buf)[0] = rarithmetic.r_singlefloat(16711938.0)
packed = rffi.charpsize2str(buf, 4)
if packed == "\x4b\x7f\x01\x02":
float_format = 'IEEE, big-endian'
elif packed == "\x02\x01\x7f\x4b":
float_format = 'IEEE, little-endian'
else:
float_format = 'unknown'
lltype.free(buf, flavor='raw')
return double_format, float_format
示例2: _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
示例3: recv_bytes
def recv_bytes(self, space, maxlength=PY_SSIZE_T_MAX):
self._check_readable(space)
if maxlength < 0:
raise OperationError(space.w_ValueError, space.wrap("maxlength < 0"))
res, newbuf = self.do_recv_string(space, self.BUFFER_SIZE, maxlength)
res = intmask(res) # XXX why?
try:
if newbuf:
return space.wrap(rffi.charpsize2str(newbuf, res))
else:
return space.wrap(rffi.charpsize2str(self.buffer, res))
finally:
if newbuf:
rffi.free_charp(newbuf)
示例4: recv_bytes_into
def recv_bytes_into(self, space, w_buffer, offset=0):
rwbuffer = space.rwbuffer_w(w_buffer)
length = rwbuffer.getlength()
res, newbuf = self.do_recv_string(space, length - offset, PY_SSIZE_T_MAX)
res = intmask(res) # XXX why?
try:
if newbuf:
raise BufferTooShort(space, space.wrap(rffi.charpsize2str(newbuf, res)))
rwbuffer.setslice(offset, rffi.charpsize2str(self.buffer, res))
finally:
if newbuf:
rffi.free_charp(newbuf)
return space.wrap(res)
示例5: _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])
示例6: 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.charpsize2str(buf_ptr[0],
length))
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)
示例7: encodeex
def encodeex(encodebuf, unicodedata, errors="strict", errorcb=None, namecb=None, ignore_error=0):
inleft = len(unicodedata)
inbuf = rffi.get_nonmoving_unicodebuffer(unicodedata)
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, unicodedata)
while flags & MBENC_RESET:
r = pypy_cjk_enc_reset(encodebuf)
if r == 0:
break
multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb, unicodedata)
src = pypy_cjk_enc_outbuf(encodebuf)
length = pypy_cjk_enc_outlen(encodebuf)
return rffi.charpsize2str(src, length)
#
finally:
rffi.free_nonmoving_unicodebuffer(unicodedata, inbuf)
示例8: 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)
示例9: ioctl
def ioctl(space, w_fd, op, w_arg=0, mutate_flag=True):
"""ioctl(fd, opt[, arg[, mutate_flag]])
Perform the requested operation on file descriptor fd. The operation is
defined by opt and is operating system dependent. Typically these codes
are retrieved from the fcntl or termios library modules.
The argument arg is optional, and defaults to 0; it may be an int or a
buffer containing character data (most likely a string or an array).
If the argument is a mutable buffer (such as an array) and if the
mutate_flag argument (which is only allowed in this case) is true then the
buffer is (in effect) passed to the operating system and changes made by
the OS will be reflected in the contents of the buffer after the call has
returned. The return value is the integer returned by the ioctl system
call.
If the argument is a mutable buffer and the mutable_flag argument is not
passed or is false, the behavior is as if a string had been passed. This
behavior will change in future releases of Python.
If the argument is an immutable buffer (most likely a string) then a copy
of the buffer is passed to the operating system and the return value is a
string of the same length containing whatever the operating system put in
the buffer. The length of the arg buffer in this case is not allowed to
exceed 1024 bytes.
If the arg given is an integer or if none is specified, the result value
is an integer corresponding to the return value of the ioctl call in the
C code."""
fd = _conv_descriptor(space, w_fd)
# Python turns number > sys.maxint into long, we need the signed C value
op = rffi.cast(rffi.INT, op)
IOCTL_BUFSZ = 1024
if space.is_w(space.type(w_arg), space.w_int):
arg = space.int_w(w_arg)
rv = ioctl_int(fd, op, arg)
if rv < 0:
raise OperationError(space.w_IOError,
space.wrap(_get_error_msg()))
return space.wrap(rv)
elif space.is_w(space.type(w_arg), space.w_str): # immutable
arg = space.str_w(w_arg)
if len(arg) > IOCTL_BUFSZ:
raise OperationError(space.w_ValueError,
space.wrap("ioctl string arg too long"))
ll_arg = rffi.str2charp(arg)
rv = ioctl_str(fd, op, ll_arg)
arg = rffi.charpsize2str(ll_arg, len(arg))
lltype.free(ll_arg, flavor='raw')
if rv < 0:
raise OperationError(space.w_IOError,
space.wrap(_get_error_msg()))
return space.wrap(arg)
else:
raise OperationError(space.w_TypeError,
space.wrap("an integer or a buffer required"))
示例10: ioctl
def ioctl(space, w_fd, op, w_arg=0, mutate_flag=-1):
"""ioctl(fd, opt[, arg[, mutate_flag]])
Perform the requested operation on file descriptor fd. The operation is
defined by opt and is operating system dependent. Typically these codes
are retrieved from the fcntl or termios library modules.
"""
# removed the largish docstring because it is not in sync with the
# documentation any more (even in CPython's docstring is out of date)
# XXX this function's interface is a mess.
# We try to emulate the behavior of Python >= 2.5 w.r.t. mutate_flag
fd = space.c_filedescriptor_w(w_fd)
op = rffi.cast(rffi.INT, op) # C long => C int
if mutate_flag != 0:
try:
rwbuffer = space.rwbuffer_w(w_arg)
except OperationError, e:
if not e.match(space, space.w_TypeError):
raise
if mutate_flag > 0:
raise
else:
arg = rwbuffer.as_str()
ll_arg = rffi.str2charp(arg)
rv = ioctl_str(fd, op, ll_arg)
arg = rffi.charpsize2str(ll_arg, len(arg))
lltype.free(ll_arg, flavor='raw')
if rv < 0:
raise _get_error(space, "ioctl")
rwbuffer.setslice(0, arg)
return space.wrap(rv)
示例11: llimpl_FormatError
def llimpl_FormatError(code):
"Return a message corresponding to the given Windows error code."
buf = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
try:
msglen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
None,
rffi.cast(DWORD, code),
DEFAULT_LANGUAGE,
rffi.cast(rffi.CCHARP, buf),
0, None)
if msglen <= 2: # includes the case msglen < 0
return fake_FormatError(code)
# FormatMessage always appends \r\n.
buflen = intmask(msglen - 2)
assert buflen > 0
result = rffi.charpsize2str(buf[0], buflen)
LocalFree(rffi.cast(rffi.VOIDP, buf[0]))
finally:
lltype.free(buf, flavor='raw')
return result
示例12: Array_to_str
def Array_to_str(vm):
(self,),_ = vm.decode_args("!", self_of=Array)
assert isinstance(self, Array)
data = rffi.charpsize2str(self.data, self.num_entries * self.type_size)
objectmodel.keepalive_until_here(self)
return Con_String(vm, data)
示例13: load_resource
def load_resource(filename,resname,resid,reslang):
"""Load the named resource from the given file.
The filename and resource name must be ascii strings, and the resid and
reslang must be integers.
"""
l_handle = k32_LoadLibraryExA(filename,0,LOAD_LIBRARY_AS_DATAFILE)
if not l_handle:
raise WindowsError(rwin32.GetLastError(),"LoadLibraryExW failed")
try:
r_handle = k32_FindResourceExA(l_handle,resname,resid,reslang)
if not r_handle:
raise WindowsError(rwin32.GetLastError(),"FindResourceExA failed")
r_size = k32_SizeofResource(l_handle,r_handle)
if not r_size:
raise WindowsError(rwin32.GetLastError(),"SizeofResource failed")
r_info = k32_LoadResource(l_handle,r_handle)
if not r_info:
raise WindowsError(rwin32.GetLastError(),"LoadResource failed")
r_ptr = k32_LockResource(r_info)
if not r_ptr:
raise WindowsError(rwin32.GetLastError(),"LockResource failed")
return rffi.charpsize2str(r_ptr,r_size)
finally:
if not k32_FreeLibrary(l_handle):
raise WindowsError(rwin32.GetLastError(),"FreeLibrary failed")
示例14: convert_from_regdata
def convert_from_regdata(space, buf, buflen, typ):
if typ == rwinreg.REG_DWORD:
if not buflen:
return space.wrap(0)
d = rffi.cast(rwin32.LPDWORD, buf)[0]
return space.wrap(d)
elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ:
if not buflen:
return space.wrap("")
s = rffi.charp2strn(rffi.cast(rffi.CCHARP, buf), buflen)
return space.wrap(s)
elif typ == rwinreg.REG_MULTI_SZ:
if not buflen:
return space.newlist([])
i = 0
l = []
while i < buflen and buf[i]:
s = []
while i < buflen and buf[i] != '\0':
s.append(buf[i])
i += 1
if len(s) == 0:
break
s = ''.join(s)
l.append(space.wrap(s))
i += 1
return space.newlist(l)
else: # REG_BINARY and all other types
return space.wrap(rffi.charpsize2str(buf, buflen))
示例15: encode
def encode(codec, unicodedata, errors="strict", errorcb=None, namecb=None):
inleft = len(unicodedata)
inbuf = rffi.get_nonmoving_unicodebuffer(unicodedata)
try:
encodebuf = pypy_cjk_enc_init(codec, inbuf, inleft)
if not encodebuf:
raise MemoryError
try:
while True:
r = pypy_cjk_enc_chunk(encodebuf)
if r == 0:
break
multibytecodec_encerror(encodebuf, r, errors,
codec, errorcb, namecb, unicodedata)
while True:
r = pypy_cjk_enc_reset(encodebuf)
if r == 0:
break
multibytecodec_encerror(encodebuf, r, errors,
codec, errorcb, namecb, unicodedata)
src = pypy_cjk_enc_outbuf(encodebuf)
length = pypy_cjk_enc_outlen(encodebuf)
return rffi.charpsize2str(src, length)
#
finally:
pypy_cjk_enc_free(encodebuf)
#
finally:
rffi.free_nonmoving_unicodebuffer(unicodedata, inbuf)