本文整理汇总了Python中pypy.interpreter.error.oefmt函数的典型用法代码示例。如果您正苦于以下问题:Python oefmt函数的具体用法?Python oefmt怎么用?Python oefmt使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了oefmt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _new_array_type
def _new_array_type(space, w_ctptr, length):
_setup_wref(rweakref.has_weakref_support())
if not isinstance(w_ctptr, ctypeptr.W_CTypePointer):
raise oefmt(space.w_TypeError, "first arg must be a pointer ctype")
arrays = w_ctptr._array_types
if arrays is None:
arrays = rweakref.RWeakValueDictionary(int, ctypearray.W_CTypeArray)
w_ctptr._array_types = arrays
else:
ctype = arrays.get(length)
if ctype is not None:
return ctype
#
ctitem = w_ctptr.ctitem
if ctitem.size < 0:
raise oefmt(space.w_ValueError, "array item of unknown size: '%s'",
ctitem.name)
if length < 0:
assert length == -1
arraysize = -1
extra = '[]'
else:
try:
arraysize = ovfcheck(length * ctitem.size)
except OverflowError:
raise oefmt(space.w_OverflowError,
"array size would overflow a ssize_t")
extra = '[%d]' % length
#
ctype = ctypearray.W_CTypeArray(space, w_ctptr, length, arraysize, extra)
arrays.set(length, ctype)
return ctype
示例2: descr_translate
def descr_translate(self, space, w_table):
selfvalue = self._value
w_sys = space.getbuiltinmodule('sys')
maxunicode = space.int_w(space.getattr(w_sys,
space.wrap("maxunicode")))
result = []
for unichar in selfvalue:
try:
w_newval = space.getitem(w_table, space.wrap(ord(unichar)))
except OperationError as e:
if e.match(space, space.w_LookupError):
result.append(unichar)
else:
raise
else:
if space.is_w(w_newval, space.w_None):
continue
elif space.isinstance_w(w_newval, space.w_int):
newval = space.int_w(w_newval)
if newval < 0 or newval > maxunicode:
raise oefmt(space.w_TypeError,
"character mapping must be in range(%s)",
hex(maxunicode + 1))
result.append(unichr(newval))
elif space.isinstance_w(w_newval, space.w_unicode):
result.append(space.unicode_w(w_newval))
else:
raise oefmt(space.w_TypeError,
"character mapping must return integer, None "
"or str")
return W_UnicodeObject(u''.join(result))
示例3: _getfunc
def _getfunc(space, CDLL, w_name, w_argtypes, w_restype):
argtypes_w, argtypes, w_restype, restype = unpack_argtypes(
space, w_argtypes, w_restype)
if space.isinstance_w(w_name, space.w_str):
name = space.str_w(w_name)
try:
func = CDLL.cdll.getpointer(name, argtypes, restype,
flags = CDLL.flags)
except KeyError:
raise oefmt(space.w_AttributeError,
"No symbol %s found in library %s",
name, CDLL.name)
except LibFFIError:
raise got_libffi_error(space)
return W_FuncPtr(func, argtypes_w, w_restype)
elif space.isinstance_w(w_name, space.w_int):
ordinal = space.int_w(w_name)
try:
func = CDLL.cdll.getpointer_by_ordinal(
ordinal, argtypes, restype,
flags = CDLL.flags)
except KeyError:
raise oefmt(space.w_AttributeError,
"No ordinal %d found in library %s",
ordinal, CDLL.name)
except LibFFIError:
raise got_libffi_error(space)
return W_FuncPtr(func, argtypes_w, w_restype)
else:
raise OperationError(space.w_TypeError, space.wrap(
'function name must be a string or integer'))
示例4: _realize_c_struct_or_union
def _realize_c_struct_or_union(ffi, sindex):
s = ffi.ctxobj.ctx.c_struct_unions[sindex]
type_index = rffi.getintfield(s, 'c_type_index')
if ffi.cached_types[type_index] is not None:
return ffi.cached_types[type_index] #found already in the "primary" slot
space = ffi.space
w_ctype = None
c_flags = rffi.getintfield(s, 'c_flags')
c_first_field_index = rffi.getintfield(s, 'c_first_field_index')
if (c_flags & cffi_opcode.F_EXTERNAL) == 0:
if (c_flags & cffi_opcode.F_UNION) != 0:
name = _realize_name("union ", s.c_name)
x = ctypestruct.W_CTypeUnion(space, name)
else:
name = _realize_name("struct ", s.c_name)
x = ctypestruct.W_CTypeStruct(space, name)
if (c_flags & cffi_opcode.F_OPAQUE) == 0:
assert c_first_field_index >= 0
w_ctype = x
w_ctype.size = rffi.getintfield(s, 'c_size')
w_ctype.alignment = rffi.getintfield(s, 'c_alignment')
# w_ctype._field_list and other underscore fields are still
# None, making it a "lazy" (i.e. "non-forced") kind of struct
w_ctype._lazy_ffi = ffi
w_ctype._lazy_s = s
else:
assert c_first_field_index < 0
else:
assert c_first_field_index < 0
x = _fetch_external_struct_or_union(s, ffi.included_ffis_libs)
if x is None:
raise oefmt(ffi.w_FFIError,
"'%s %s' should come from ffi.include() but was not found",
"union" if c_flags & cffi_opcode.F_UNION else "struct",
rffi.charp2str(s.c_name))
assert isinstance(x, ctypestruct.W_CTypeStructOrUnion)
if (c_flags & cffi_opcode.F_OPAQUE) == 0 and x.size < 0:
prefix = "union" if c_flags & cffi_opcode.F_UNION else "struct"
name = rffi.charp2str(s.c_name)
raise oefmt(space.w_NotImplementedError,
"'%s %s' is opaque in the ffi.include(), but no "
"longer in the ffi doing the include (workaround: don't "
"use ffi.include() but duplicate the declarations of "
"everything using %s %s)",
prefix, name, prefix, name)
# Update the "primary" OP_STRUCT_UNION slot
ffi.cached_types[type_index] = x
if w_ctype is not None and rffi.getintfield(s, 'c_size') == -2:
# oops, this struct is unnamed and we couldn't generate
# a C expression to get its size. We have to rely on
# complete_struct_or_union() to compute it now.
try:
do_realize_lazy_struct(w_ctype)
except:
ffi.cached_types[type_index] = None
raise
return x
示例5: descr_long
def descr_long(self, space):
try:
return W_LongObject.fromfloat(space, self.floatval)
except OverflowError:
raise oefmt(space.w_OverflowError, "cannot convert float infinity to integer")
except ValueError:
raise oefmt(space.w_ValueError, "cannot convert float NaN to integer")
示例6: __init__
def __init__(self, space, ctype, w_callable, w_error, w_onerror):
raw_closure = rffi.cast(rffi.CCHARP, clibffi.closureHeap.alloc())
self._closure = Closure(raw_closure)
W_ExternPython.__init__(self, space, raw_closure, ctype,
w_callable, w_error, w_onerror)
self.key_pycode = space._try_fetch_pycode(w_callable)
#
cif_descr = self.getfunctype().cif_descr
if not cif_descr:
raise oefmt(space.w_NotImplementedError,
"%s: callback with unsupported argument or "
"return type or with '...'", self.getfunctype().name)
with self as ptr:
closure_ptr = rffi.cast(clibffi.FFI_CLOSUREP, ptr)
unique_id = self.hide_object()
res = clibffi.c_ffi_prep_closure(closure_ptr, cif_descr.cif,
invoke_callback,
unique_id)
if rffi.cast(lltype.Signed, res) != clibffi.FFI_OK:
raise oefmt(space.w_SystemError,
"libffi failed to build this callback")
if closure_ptr.c_user_data != unique_id:
raise oefmt(space.w_SystemError,
"ffi_prep_closure(): bad user_data (it seems that the "
"version of the libffi library seen at runtime is "
"different from the 'ffi.h' file seen at compile-time)")
示例7: execve
def execve(space, w_command, w_args, w_env):
""" execve(path, args, env)
Execute a path with arguments and environment, replacing current process.
path: path of executable file
args: iterable of arguments
env: dictionary of strings mapping to strings
"""
command = fsencode_w(space, w_command)
try:
args_w = space.unpackiterable(w_args)
if len(args_w) < 1:
raise oefmt(space.w_ValueError,
"execv() must have at least one argument")
args = [fsencode_w(space, w_arg) for w_arg in args_w]
except OperationError as e:
if not e.match(space, space.w_TypeError):
raise
raise oefmt(space.w_TypeError,
"execv() arg 2 must be an iterable of strings")
#
if w_env is None: # when called via execv() above
try:
os.execv(command, args)
except OSError as e:
raise wrap_oserror(space, e)
else:
env = _env2interp(space, w_env)
try:
os.execve(command, args, env)
except OSError as e:
raise wrap_oserror(space, e)
示例8: utime
def utime(space, w_path, w_tuple):
""" utime(path, (atime, mtime))
utime(path, None)
Set the access and modified time of the file to the given values. If the
second form is used, set the access and modified times to the current time.
"""
if space.is_w(w_tuple, space.w_None):
try:
dispatch_filename(rposix.utime, 1)(space, w_path, None)
return
except OSError as e:
raise wrap_oserror2(space, e, w_path)
try:
msg = "utime() arg 2 must be a tuple (atime, mtime) or None"
args_w = space.fixedview(w_tuple)
if len(args_w) != 2:
raise oefmt(space.w_TypeError, msg)
actime = space.float_w(args_w[0], allow_conversion=False)
modtime = space.float_w(args_w[1], allow_conversion=False)
dispatch_filename(rposix.utime, 2)(space, w_path, (actime, modtime))
except OSError as e:
raise wrap_oserror2(space, e, w_path)
except OperationError as e:
if not e.match(space, space.w_TypeError):
raise
raise oefmt(space.w_TypeError, msg)
示例9: poll
def poll(self, space, w_timeout):
if space.is_w(w_timeout, space.w_None):
timeout = -1
else:
# we want to be compatible with cpython and also accept things
# that can be casted to integer (I think)
try:
# compute the integer
w_timeout = space.int(w_timeout)
except OperationError:
raise oefmt(space.w_TypeError, "timeout must be an integer or None")
timeout = space.c_int_w(w_timeout)
if self.running:
raise oefmt(space.w_RuntimeError, "concurrent poll() invocation")
self.running = True
try:
retval = rpoll.poll(self.fddict, timeout)
except rpoll.PollError as e:
w_errortype = space.fromcache(Cache).w_error
message = e.get_msg()
raise OperationError(w_errortype, space.newtuple([space.wrap(e.errno), space.wrap(message)]))
finally:
self.running = False
retval_w = []
for fd, revents in retval:
retval_w.append(space.newtuple([space.wrap(fd), space.wrap(revents)]))
return space.newlist(retval_w)
示例10: convert_array_from_object
def convert_array_from_object(self, cdata, w_ob):
space = self.space
if (space.isinstance_w(w_ob, space.w_list) or
space.isinstance_w(w_ob, space.w_tuple)):
self._convert_array_from_listview(cdata, w_ob)
elif (self.can_cast_anything or
(self.ctitem.is_primitive_integer and
self.ctitem.size == rffi.sizeof(lltype.Char))):
if not space.isinstance_w(w_ob, space.w_str):
raise self._convert_error("str or list or tuple", w_ob)
s = space.str_w(w_ob)
n = len(s)
if self.length >= 0 and n > self.length:
raise oefmt(space.w_IndexError,
"initializer string is too long for '%s' (got %d "
"characters)", self.name, n)
copy_string_to_raw(llstr(s), cdata, 0, n)
if n != self.length:
cdata[n] = '\x00'
elif isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveUniChar):
if not space.isinstance_w(w_ob, space.w_unicode):
raise self._convert_error("unicode or list or tuple", w_ob)
s = space.unicode_w(w_ob)
n = len(s)
if self.length >= 0 and n > self.length:
raise oefmt(space.w_IndexError,
"initializer unicode string is too long for '%s' "
"(got %d characters)", self.name, n)
unichardata = rffi.cast(rffi.CWCHARP, cdata)
copy_unicode_to_raw(llunicode(s), unichardata, 0, n)
if n != self.length:
unichardata[n] = u'\x00'
else:
raise self._convert_error("list or tuple", w_ob)
示例11: _build_function_type
def _build_function_type(space, fargs, fresult, ellipsis, abi):
from pypy.module._cffi_backend import ctypefunc
#
if ((fresult.size < 0 and
not isinstance(fresult, ctypevoid.W_CTypeVoid))
or isinstance(fresult, ctypearray.W_CTypeArray)):
if (isinstance(fresult, ctypestruct.W_CTypeStructOrUnion) and
fresult.size < 0):
raise oefmt(space.w_TypeError,
"result type '%s' is opaque", fresult.name)
else:
raise oefmt(space.w_TypeError,
"invalid result type: '%s'", fresult.name)
#
fct = ctypefunc.W_CTypeFunc(space, fargs, fresult, ellipsis, abi)
unique_cache = space.fromcache(UniqueCache)
func_hash = _func_key_hash(unique_cache, fargs, fresult, ellipsis, abi)
for weakdict in unique_cache.functions:
if weakdict.get(func_hash) is None:
weakdict.set(func_hash, fct)
break
else:
weakdict = rweakref.RWeakValueDictionary(int, ctypefunc.W_CTypeFunc)
unique_cache.functions.append(weakdict)
weakdict.set(func_hash, fct)
return fct
示例12: new_enum_type
def new_enum_type(space, name, w_enumerators, w_enumvalues, w_basectype):
enumerators_w = space.fixedview(w_enumerators)
enumvalues_w = space.fixedview(w_enumvalues)
if len(enumerators_w) != len(enumvalues_w):
raise oefmt(space.w_ValueError, "tuple args must have the same size")
enumerators = [space.str_w(w) for w in enumerators_w]
#
if (not isinstance(w_basectype, ctypeprim.W_CTypePrimitiveSigned) and
not isinstance(w_basectype, ctypeprim.W_CTypePrimitiveUnsigned)):
raise oefmt(space.w_TypeError,
"expected a primitive signed or unsigned base type")
#
lvalue = lltype.malloc(rffi.CCHARP.TO, w_basectype.size, flavor='raw')
try:
for w in enumvalues_w:
# detects out-of-range or badly typed values
w_basectype.convert_from_object(lvalue, w)
finally:
lltype.free(lvalue, flavor='raw')
#
size = w_basectype.size
align = w_basectype.align
if isinstance(w_basectype, ctypeprim.W_CTypePrimitiveSigned):
enumvalues = [space.int_w(w) for w in enumvalues_w]
ctype = ctypeenum.W_CTypeEnumSigned(space, name, size, align,
enumerators, enumvalues)
else:
enumvalues = [space.uint_w(w) for w in enumvalues_w]
ctype = ctypeenum.W_CTypeEnumUnsigned(space, name, size, align,
enumerators, enumvalues)
return ctype
示例13: create_all_slots
def create_all_slots(w_self, hasoldstylebase):
space = w_self.space
dict_w = w_self.dict_w
if "__slots__" not in dict_w:
wantdict = True
wantweakref = True
else:
wantdict = False
wantweakref = False
w_slots = dict_w["__slots__"]
if space.isinstance_w(w_slots, space.w_str) or space.isinstance_w(w_slots, space.w_unicode):
slot_names_w = [w_slots]
else:
slot_names_w = space.unpackiterable(w_slots)
for w_slot_name in slot_names_w:
slot_name = space.str_w(w_slot_name)
if slot_name == "__dict__":
if wantdict or w_self.hasdict:
raise oefmt(space.w_TypeError, "__dict__ slot disallowed: we already got one")
wantdict = True
elif slot_name == "__weakref__":
if wantweakref or w_self.weakrefable:
raise oefmt(space.w_TypeError, "__weakref__ slot disallowed: we already got one")
wantweakref = True
else:
create_slot(w_self, slot_name)
wantdict = wantdict or hasoldstylebase
if wantdict:
create_dict_slot(w_self)
if wantweakref:
create_weakref_slot(w_self)
if "__del__" in dict_w:
w_self.needsdel = True
示例14: fmt_c
def fmt_c(self, w_value):
self.prec = -1 # just because
space = self.space
if space.isinstance_w(w_value, space.w_str):
s = space.str_w(w_value)
if len(s) != 1:
raise oefmt(space.w_TypeError, "%c requires int or char")
self.std_wp(s)
elif space.isinstance_w(w_value, space.w_unicode):
if not do_unicode:
raise NeedUnicodeFormattingError
ustr = space.unicode_w(w_value)
if len(ustr) != 1:
raise oefmt(space.w_TypeError, "%c requires int or unichar")
self.std_wp(ustr)
else:
n = space.int_w(w_value)
if do_unicode:
try:
c = unichr(n)
except ValueError:
raise oefmt(space.w_OverflowError,
"unicode character code out of range")
self.std_wp(c)
else:
try:
s = chr(n)
except ValueError:
raise oefmt(space.w_OverflowError,
"character code not in range(256)")
self.std_wp(s)
示例15: decodeslice
def decodeslice(self, space, w_slice):
if not space.isinstance_w(w_slice, space.w_slice):
raise oefmt(space.w_TypeError, "index must be int or slice")
letter = self.shape.itemcode
if letter != 'c':
raise oefmt(space.w_TypeError, "only 'c' arrays support slicing")
w_start = space.getattr(w_slice, space.wrap('start'))
w_stop = space.getattr(w_slice, space.wrap('stop'))
w_step = space.getattr(w_slice, space.wrap('step'))
if space.is_w(w_start, space.w_None):
start = 0
else:
start = space.int_w(w_start)
if space.is_w(w_stop, space.w_None):
stop = self.length
else:
stop = space.int_w(w_stop)
if not space.is_w(w_step, space.w_None):
step = space.int_w(w_step)
if step != 1:
raise oefmt(space.w_ValueError, "no step support")
if not (0 <= start <= stop <= self.length):
raise oefmt(space.w_ValueError, "slice out of bounds")
if not self.ll_buffer:
raise segfault_exception(space, "accessing a freed array")
return start, stop