[pypy-commit] pypy llvm-translation-backend: hg merge default
Manuel Jacob
noreply at buildbot.pypy.org
Tue Jan 21 15:33:29 CET 2014
Author: Manuel Jacob
Branch: llvm-translation-backend
Changeset: r68812:67ac13633b48
Date: 2014-01-21 14:54 +0100
http://bitbucket.org/pypy/pypy/changeset/67ac13633b48/
Log: hg merge default
diff too long, truncating to 2000 out of 2137 lines
diff --git a/lib_pypy/_ctypes/array.py b/lib_pypy/_ctypes/array.py
--- a/lib_pypy/_ctypes/array.py
+++ b/lib_pypy/_ctypes/array.py
@@ -1,4 +1,4 @@
-import _ffi
+from _rawffi import alt as _ffi
import _rawffi
from _ctypes.basics import _CData, cdata_from_address, _CDataMeta, sizeof
diff --git a/lib_pypy/_ctypes/basics.py b/lib_pypy/_ctypes/basics.py
--- a/lib_pypy/_ctypes/basics.py
+++ b/lib_pypy/_ctypes/basics.py
@@ -1,6 +1,6 @@
import _rawffi
-import _ffi
+from _rawffi import alt as _ffi
import sys
try: from __pypy__ import builtinify
diff --git a/lib_pypy/_ctypes/function.py b/lib_pypy/_ctypes/function.py
--- a/lib_pypy/_ctypes/function.py
+++ b/lib_pypy/_ctypes/function.py
@@ -5,7 +5,7 @@
from _ctypes.basics import is_struct_shape
from _ctypes.builtin import get_errno, set_errno, get_last_error, set_last_error
import _rawffi
-import _ffi
+from _rawffi import alt as _ffi
import sys
import traceback
diff --git a/lib_pypy/_ctypes/pointer.py b/lib_pypy/_ctypes/pointer.py
--- a/lib_pypy/_ctypes/pointer.py
+++ b/lib_pypy/_ctypes/pointer.py
@@ -1,6 +1,6 @@
import _rawffi
-import _ffi
+from _rawffi import alt as _ffi
from _ctypes.basics import _CData, _CDataMeta, cdata_from_address, ArgumentError
from _ctypes.basics import keepalive_key, store_reference, ensure_objects
from _ctypes.basics import sizeof, byref, as_ffi_pointer
diff --git a/lib_pypy/_ctypes/primitive.py b/lib_pypy/_ctypes/primitive.py
--- a/lib_pypy/_ctypes/primitive.py
+++ b/lib_pypy/_ctypes/primitive.py
@@ -1,4 +1,4 @@
-import _ffi
+from _rawffi import alt as _ffi
import _rawffi
import weakref
import sys
diff --git a/lib_pypy/_ffi.py b/lib_pypy/_ffi.py
new file mode 100644
--- /dev/null
+++ b/lib_pypy/_ffi.py
@@ -0,0 +1,2 @@
+# Backward compatibility hack
+from _rawffi.alt import *
diff --git a/pypy/config/pypyoption.py b/pypy/config/pypyoption.py
--- a/pypy/config/pypyoption.py
+++ b/pypy/config/pypyoption.py
@@ -34,14 +34,14 @@
"struct", "_hashlib", "_md5", "_sha", "_minimal_curses", "cStringIO",
"thread", "itertools", "pyexpat", "_ssl", "cpyext", "array",
"binascii", "_multiprocessing", '_warnings',
- "_collections", "_multibytecodec", "micronumpy", "_ffi",
+ "_collections", "_multibytecodec", "micronumpy",
"_continuation", "_cffi_backend", "_csv", "cppyy", "_pypyjson"]
))
translation_modules = default_modules.copy()
translation_modules.update(dict.fromkeys(
["fcntl", "rctime", "select", "signal", "_rawffi", "zlib",
- "struct", "_md5", "cStringIO", "array", "_ffi",
+ "struct", "_md5", "cStringIO", "array",
"binascii",
# the following are needed for pyrepl (and hence for the
# interactive prompt/pdb)
@@ -96,7 +96,6 @@
# no _rawffi if importing rpython.rlib.clibffi raises ImportError
# or CompilationError or py.test.skip.Exception
"_rawffi" : ["rpython.rlib.clibffi"],
- "_ffi" : ["rpython.rlib.clibffi"],
"zlib" : ["rpython.rlib.rzlib"],
"bz2" : ["pypy.module.bz2.interp_bz2"],
diff --git a/pypy/doc/_ref.txt b/pypy/doc/_ref.txt
--- a/pypy/doc/_ref.txt
+++ b/pypy/doc/_ref.txt
@@ -109,6 +109,4 @@
.. _`rpython/translator/c/`: https://bitbucket.org/pypy/pypy/src/default/rpython/translator/c/
.. _`rpython/translator/c/src/stacklet/`: https://bitbucket.org/pypy/pypy/src/default/rpython/translator/c/src/stacklet/
.. _`rpython/translator/c/src/stacklet/stacklet.h`: https://bitbucket.org/pypy/pypy/src/default/rpython/translator/c/src/stacklet/stacklet.h
-.. _`rpython/translator/cli/`: https://bitbucket.org/pypy/pypy/src/default/rpython/translator/cli/
-.. _`rpython/translator/jvm/`: https://bitbucket.org/pypy/pypy/src/default/rpython/translator/jvm/
.. _`rpython/translator/tool/`: https://bitbucket.org/pypy/pypy/src/default/rpython/translator/tool/
diff --git a/pypy/doc/config/translation.lldebug0.txt b/pypy/doc/config/translation.lldebug0.txt
new file mode 100644
--- /dev/null
+++ b/pypy/doc/config/translation.lldebug0.txt
@@ -0,0 +1,1 @@
+Like lldebug, but in addition compile C files with -O0
diff --git a/pypy/doc/whatsnew-head.rst b/pypy/doc/whatsnew-head.rst
--- a/pypy/doc/whatsnew-head.rst
+++ b/pypy/doc/whatsnew-head.rst
@@ -44,3 +44,7 @@
.. branch: refactor-str-types
Remove multimethods on str/unicode/bytearray and make the implementations share code.
+
+.. branch: remove-del-from-generatoriterator
+Speed up generators that don't yield inside try or wait blocks by skipping
+unnecessary cleanup.
diff --git a/pypy/interpreter/pyframe.py b/pypy/interpreter/pyframe.py
--- a/pypy/interpreter/pyframe.py
+++ b/pypy/interpreter/pyframe.py
@@ -167,7 +167,7 @@
def run(self):
"""Start this frame's execution."""
if self.getcode().co_flags & pycode.CO_GENERATOR:
- if self.getcode().co_flags & pycode.CO_YIELD_INSIDE_TRY:
+ if 1:# self.getcode().co_flags & pycode.CO_YIELD_INSIDE_TRY:
from pypy.interpreter.generator import GeneratorIteratorWithDel
return self.space.wrap(GeneratorIteratorWithDel(self))
else:
diff --git a/pypy/module/_cffi_backend/ctypestruct.py b/pypy/module/_cffi_backend/ctypestruct.py
--- a/pypy/module/_cffi_backend/ctypestruct.py
+++ b/pypy/module/_cffi_backend/ctypestruct.py
@@ -33,7 +33,7 @@
if self.fields_dict is None:
space = self.space
raise operationerrfmt(w_errorcls or space.w_TypeError,
- "'%s' is not completed yet", self.name)
+ "'%s' is opaque or not completed yet", self.name)
def _alignof(self):
self.check_complete(w_errorcls=self.space.w_ValueError)
diff --git a/pypy/module/_cffi_backend/newtype.py b/pypy/module/_cffi_backend/newtype.py
--- a/pypy/module/_cffi_backend/newtype.py
+++ b/pypy/module/_cffi_backend/newtype.py
@@ -118,6 +118,7 @@
SF_MSVC_BITFIELDS = 1
SF_GCC_ARM_BITFIELDS = 2
SF_GCC_BIG_ENDIAN = 4
+SF_PACKED = 8
if sys.platform == 'win32':
DEFAULT_SFLAGS = SF_MSVC_BITFIELDS
@@ -190,8 +191,8 @@
boffset = 0 # reset each field at offset 0
#
# update the total alignment requirement, but skip it if the
- # field is an anonymous bitfield
- falign = ftype.alignof()
+ # field is an anonymous bitfield or if SF_PACKED
+ falign = 1 if sflags & SF_PACKED else ftype.alignof()
do_align = True
if (sflags & SF_GCC_ARM_BITFIELDS) == 0 and fbitsize >= 0:
if (sflags & SF_MSVC_BITFIELDS) == 0:
@@ -305,6 +306,12 @@
if bits_already_occupied + fbitsize > 8 * ftype.size:
# it would not fit, we need to start at the next
# allowed position
+ if ((sflags & SF_PACKED) != 0 and
+ (bits_already_occupied & 7) != 0):
+ raise operationerrfmt(space.w_NotImplementedError,
+ "with 'packed', gcc would compile field "
+ "'%s.%s' to reuse some bits in the previous "
+ "field", w_ctype.name, fname)
field_offset_bytes += falign
assert boffset < field_offset_bytes * 8
boffset = field_offset_bytes * 8
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -3137,6 +3137,44 @@
p = newp(BArray, None)
assert sizeof(p[2:9]) == 7 * sizeof(BInt)
+def test_packed():
+ BLong = new_primitive_type("long")
+ BChar = new_primitive_type("char")
+ BShort = new_primitive_type("short")
+ BStruct = new_struct_type("struct foo")
+ complete_struct_or_union(BStruct, [('a1', BLong, -1),
+ ('a2', BChar, -1),
+ ('a3', BShort, -1)],
+ None, -1, -1, 8) # SF_PACKED==8
+ d = BStruct.fields
+ assert len(d) == 3
+ assert d[0][0] == 'a1'
+ assert d[0][1].type is BLong
+ assert d[0][1].offset == 0
+ assert d[0][1].bitshift == -1
+ assert d[0][1].bitsize == -1
+ assert d[1][0] == 'a2'
+ assert d[1][1].type is BChar
+ assert d[1][1].offset == sizeof(BLong)
+ assert d[1][1].bitshift == -1
+ assert d[1][1].bitsize == -1
+ assert d[2][0] == 'a3'
+ assert d[2][1].type is BShort
+ assert d[2][1].offset == sizeof(BLong) + sizeof(BChar)
+ assert d[2][1].bitshift == -1
+ assert d[2][1].bitsize == -1
+ assert sizeof(BStruct) == sizeof(BLong) + sizeof(BChar) + sizeof(BShort)
+ assert alignof(BStruct) == 1
+
+def test_packed_with_bitfields():
+ BLong = new_primitive_type("long")
+ BChar = new_primitive_type("char")
+ BStruct = new_struct_type("struct foo")
+ py.test.raises(NotImplementedError,
+ complete_struct_or_union,
+ BStruct, [('a1', BLong, 30),
+ ('a2', BChar, 5)],
+ None, -1, -1, 8) # SF_PACKED==8
def test_version():
# this test is here mostly for PyPy
diff --git a/pypy/module/_multiprocessing/test/test_memory.py b/pypy/module/_multiprocessing/test/test_memory.py
--- a/pypy/module/_multiprocessing/test/test_memory.py
+++ b/pypy/module/_multiprocessing/test/test_memory.py
@@ -1,6 +1,6 @@
class AppTestMemory:
spaceconfig = dict(usemodules=('_multiprocessing', 'mmap',
- '_rawffi', '_ffi', 'itertools'))
+ '_rawffi', 'itertools'))
def test_address_of(self):
import _multiprocessing
diff --git a/pypy/module/_rawffi/__init__.py b/pypy/module/_rawffi/__init__.py
--- a/pypy/module/_rawffi/__init__.py
+++ b/pypy/module/_rawffi/__init__.py
@@ -2,6 +2,7 @@
"""
from pypy.interpreter.mixedmodule import MixedModule
+from pypy.module._rawffi import alt
class Module(MixedModule):
interpleveldefs = {
@@ -33,6 +34,10 @@
appleveldefs = {
}
+ submodules = {
+ 'alt': alt.Module,
+ }
+
def buildloaders(cls):
from pypy.module._rawffi import interp_rawffi
diff --git a/pypy/module/_ffi/__init__.py b/pypy/module/_rawffi/alt/__init__.py
rename from pypy/module/_ffi/__init__.py
rename to pypy/module/_rawffi/alt/__init__.py
diff --git a/pypy/module/_ffi/app_struct.py b/pypy/module/_rawffi/alt/app_struct.py
rename from pypy/module/_ffi/app_struct.py
rename to pypy/module/_rawffi/alt/app_struct.py
--- a/pypy/module/_ffi/app_struct.py
+++ b/pypy/module/_rawffi/alt/app_struct.py
@@ -1,4 +1,4 @@
-import _ffi
+from _rawffi import alt
class MetaStructure(type):
@@ -11,7 +11,7 @@
fields = dic.get('_fields_')
if fields is None:
return
- struct_descr = _ffi._StructDescr(name, fields)
+ struct_descr = alt._StructDescr(name, fields)
for field in fields:
dic[field.name] = field
dic['_struct_'] = struct_descr
diff --git a/pypy/module/_ffi/interp_ffitype.py b/pypy/module/_rawffi/alt/interp_ffitype.py
rename from pypy/module/_ffi/interp_ffitype.py
rename to pypy/module/_rawffi/alt/interp_ffitype.py
--- a/pypy/module/_ffi/interp_ffitype.py
+++ b/pypy/module/_rawffi/alt/interp_ffitype.py
@@ -116,7 +116,7 @@
types = [
# note: most of the type name directly come from the C equivalent,
# with the exception of bytes: in C, ubyte and char are equivalent,
- # but for _ffi the first expects a number while the second a 1-length
+ # but for here the first expects a number while the second a 1-length
# string
W_FFIType('slong', libffi.types.slong),
W_FFIType('sint', libffi.types.sint),
diff --git a/pypy/module/_ffi/interp_funcptr.py b/pypy/module/_rawffi/alt/interp_funcptr.py
rename from pypy/module/_ffi/interp_funcptr.py
rename to pypy/module/_rawffi/alt/interp_funcptr.py
--- a/pypy/module/_ffi/interp_funcptr.py
+++ b/pypy/module/_rawffi/alt/interp_funcptr.py
@@ -3,7 +3,7 @@
operationerrfmt
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef
-from pypy.module._ffi.interp_ffitype import W_FFIType
+from pypy.module._rawffi.alt.interp_ffitype import W_FFIType
#
from rpython.rtyper.lltypesystem import lltype, rffi
#
@@ -13,7 +13,7 @@
from rpython.rlib.rdynload import DLOpenError
from rpython.rlib.rarithmetic import r_uint
from rpython.rlib.objectmodel import we_are_translated
-from pypy.module._ffi.type_converter import FromAppLevelConverter, ToAppLevelConverter
+from pypy.module._rawffi.alt.type_converter import FromAppLevelConverter, ToAppLevelConverter
from pypy.module._rawffi.interp_rawffi import got_libffi_error, wrap_dlopenerror
import os
@@ -302,7 +302,7 @@
W_FuncPtr.typedef = TypeDef(
- '_ffi.FuncPtr',
+ '_rawffi.alt.FuncPtr',
__call__ = interp2app(W_FuncPtr.call),
getaddr = interp2app(W_FuncPtr.getaddr),
free_temp_buffers = interp2app(W_FuncPtr.free_temp_buffers),
@@ -346,7 +346,7 @@
W_CDLL.typedef = TypeDef(
- '_ffi.CDLL',
+ '_rawffi.alt.CDLL',
__new__ = interp2app(descr_new_cdll),
getfunc = interp2app(W_CDLL.getfunc),
getaddressindll = interp2app(W_CDLL.getaddressindll),
@@ -363,7 +363,7 @@
W_WinDLL.typedef = TypeDef(
- '_ffi.WinDLL',
+ '_rawffi.alt.WinDLL',
__new__ = interp2app(descr_new_windll),
getfunc = interp2app(W_WinDLL.getfunc),
getaddressindll = interp2app(W_WinDLL.getaddressindll),
diff --git a/pypy/module/_ffi/interp_struct.py b/pypy/module/_rawffi/alt/interp_struct.py
rename from pypy/module/_ffi/interp_struct.py
rename to pypy/module/_rawffi/alt/interp_struct.py
--- a/pypy/module/_ffi/interp_struct.py
+++ b/pypy/module/_rawffi/alt/interp_struct.py
@@ -8,8 +8,8 @@
from pypy.interpreter.typedef import TypeDef, interp_attrproperty
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.error import operationerrfmt
-from pypy.module._ffi.interp_ffitype import W_FFIType
-from pypy.module._ffi.type_converter import FromAppLevelConverter, ToAppLevelConverter
+from pypy.module._rawffi.alt.interp_ffitype import W_FFIType
+from pypy.module._rawffi.alt.type_converter import FromAppLevelConverter, ToAppLevelConverter
class W_Field(W_Root):
diff --git a/pypy/module/_ffi/test/__init__.py b/pypy/module/_rawffi/alt/test/__init__.py
rename from pypy/module/_ffi/test/__init__.py
rename to pypy/module/_rawffi/alt/test/__init__.py
diff --git a/pypy/module/_ffi/test/test_ffitype.py b/pypy/module/_rawffi/alt/test/test_ffitype.py
rename from pypy/module/_ffi/test/test_ffitype.py
rename to pypy/module/_rawffi/alt/test/test_ffitype.py
--- a/pypy/module/_ffi/test/test_ffitype.py
+++ b/pypy/module/_rawffi/alt/test/test_ffitype.py
@@ -1,21 +1,21 @@
-from pypy.module._ffi.test.test_funcptr import BaseAppTestFFI
+from pypy.module._rawffi.alt.test.test_funcptr import BaseAppTestFFI
class AppTestFFIType(BaseAppTestFFI):
def test_simple_types(self):
- from _ffi import types
+ from _rawffi.alt import types
assert str(types.sint) == "<ffi type sint>"
assert str(types.uint) == "<ffi type uint>"
assert types.sint.name == 'sint'
assert types.uint.name == 'uint'
def test_sizeof(self):
- from _ffi import types
+ from _rawffi.alt import types
assert types.sbyte.sizeof() == 1
assert types.sint.sizeof() == 4
def test_typed_pointer(self):
- from _ffi import types
+ from _rawffi.alt import types
intptr = types.Pointer(types.sint) # create a typed pointer to sint
assert intptr.deref_pointer() is types.sint
assert str(intptr) == '<ffi type (pointer to sint)>'
@@ -23,7 +23,7 @@
raises(TypeError, "types.Pointer(42)")
def test_pointer_identity(self):
- from _ffi import types
+ from _rawffi.alt import types
x = types.Pointer(types.slong)
y = types.Pointer(types.slong)
z = types.Pointer(types.char)
@@ -31,7 +31,7 @@
assert x is not z
def test_char_p_cached(self):
- from _ffi import types
+ from _rawffi.alt import types
x = types.Pointer(types.char)
assert x is types.char_p
x = types.Pointer(types.unichar)
diff --git a/pypy/module/_ffi/test/test_funcptr.py b/pypy/module/_rawffi/alt/test/test_funcptr.py
rename from pypy/module/_ffi/test/test_funcptr.py
rename to pypy/module/_rawffi/alt/test/test_funcptr.py
--- a/pypy/module/_ffi/test/test_funcptr.py
+++ b/pypy/module/_rawffi/alt/test/test_funcptr.py
@@ -7,7 +7,7 @@
import sys, py
class BaseAppTestFFI(object):
- spaceconfig = dict(usemodules=('_ffi', '_rawffi'))
+ spaceconfig = dict(usemodules=('_rawffi',))
@classmethod
def prepare_c_example(cls):
@@ -62,17 +62,17 @@
cls.w_f_12_34_plus_56_78 = space.wrap(f_result)
def test_libload(self):
- import _ffi
- _ffi.CDLL(self.libc_name)
+ import _rawffi.alt
+ _rawffi.alt.CDLL(self.libc_name)
def test_libload_fail(self):
- import _ffi
- raises(OSError, _ffi.CDLL, "xxxxx_this_name_does_not_exist_xxxxx")
+ import _rawffi.alt
+ raises(OSError, _rawffi.alt.CDLL, "xxxxx_this_name_does_not_exist_xxxxx")
def test_libload_None(self):
if self.iswin32:
skip("unix specific")
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
# this should return *all* loaded libs, dlopen(NULL)
dll = CDLL(None)
# libm should be loaded
@@ -80,20 +80,20 @@
assert res == 1.0
def test_callfunc(self):
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libm = CDLL(self.libm_name)
pow = libm.getfunc('pow', [types.double, types.double], types.double)
assert pow(2, 3) == 8
def test_getaddr(self):
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libm = CDLL(self.libm_name)
pow = libm.getfunc('pow', [types.double, types.double], types.double)
assert pow.getaddr() == self.pow_addr
def test_getaddressindll(self):
import sys
- from _ffi import CDLL
+ from _rawffi.alt import CDLL
libm = CDLL(self.libm_name)
pow_addr = libm.getaddressindll('pow')
fff = sys.maxint*2-1
@@ -102,7 +102,7 @@
assert pow_addr == self.pow_addr & fff
def test_func_fromaddr(self):
- from _ffi import CDLL, types, FuncPtr
+ from _rawffi.alt import CDLL, types, FuncPtr
libm = CDLL(self.libm_name)
pow_addr = libm.getaddressindll('pow')
pow = FuncPtr.fromaddr(pow_addr, 'pow', [types.double, types.double],
@@ -117,7 +117,7 @@
}
"""
import sys
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy', [types.sint, types.sint], types.sint)
assert sum_xy(30, 12) == 42
@@ -129,7 +129,7 @@
DLLEXPORT void set_dummy(int val) { dummy = val; }
DLLEXPORT int get_dummy() { return dummy; }
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
set_dummy = libfoo.getfunc('set_dummy', [types.sint], types.void)
get_dummy = libfoo.getfunc('get_dummy', [], types.sint)
@@ -144,7 +144,7 @@
DLLEXPORT int* get_dummy_ptr() { return &dummy; }
DLLEXPORT void set_val_to_ptr(int* ptr, int val) { *ptr = val; }
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
get_dummy = libfoo.getfunc('get_dummy', [], types.sint)
get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], types.void_p)
@@ -163,7 +163,7 @@
DLLEXPORT int* get_dummy_ptr(); // defined in test_pointer_args
DLLEXPORT void set_val_to_ptr(int* ptr, int val); // ditto
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
class MyPointerWrapper(object):
def __init__(self, value):
@@ -197,7 +197,7 @@
return len;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
import _rawffi
libfoo = CDLL(self.libfoo_name)
mystrlen = libfoo.getfunc('mystrlen', [types.char_p], types.slong)
@@ -223,7 +223,7 @@
return len;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
import _rawffi
libfoo = CDLL(self.libfoo_name)
mystrlen = libfoo.getfunc('mystrlen_u', [types.unichar_p], types.slong)
@@ -247,7 +247,7 @@
return s;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
import _rawffi
libfoo = CDLL(self.libfoo_name)
do_nothing = libfoo.getfunc('do_nothing', [types.char_p], types.char_p)
@@ -264,7 +264,7 @@
DLLEXPORT int* get_dummy_ptr(); // defined in test_pointer_args
DLLEXPORT void set_val_to_ptr(int* ptr, int val); // ditto
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
intptr = types.Pointer(types.sint)
@@ -283,7 +283,7 @@
DLLEXPORT long is_null_ptr(void* ptr) { return ptr == NULL; }
"""
import sys
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
is_null_ptr = libfoo.getfunc('is_null_ptr', [types.void_p], types.ulong)
assert not is_null_ptr(sys.maxint+1)
@@ -296,7 +296,7 @@
}
"""
import sys
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_ul', [types.ulong, types.ulong],
types.ulong)
@@ -313,7 +313,7 @@
return x+y;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_us', [types.ushort, types.ushort],
types.ushort)
@@ -327,7 +327,7 @@
return x+y;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_us', [types.ubyte, types.ubyte],
types.ubyte)
@@ -342,7 +342,7 @@
}
"""
import sys
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
maxint32 = 2147483647
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_ui', [types.uint, types.uint],
@@ -357,7 +357,7 @@
return x+y;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_sb', [types.sbyte, types.sbyte],
types.sbyte)
@@ -371,7 +371,7 @@
return x - ('a'-'A');
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
my_toupper = libfoo.getfunc('my_toupper', [types.char],
types.char)
@@ -385,7 +385,7 @@
return x + y;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_wc', [types.unichar, types.unichar],
types.unichar)
@@ -400,7 +400,7 @@
return x+y;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_float', [types.float, types.float],
types.float)
@@ -415,7 +415,7 @@
return x+y;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
maxint32 = 2147483647 # we cannot really go above maxint on 64 bits
# (and we would not test anything, as there long
# is the same as long long)
@@ -437,7 +437,7 @@
return x+y;
}
"""
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
maxint64 = 9223372036854775807 # maxint64+1 does not fit into a
# longlong, but it does into a
# ulonglong
@@ -464,7 +464,7 @@
return p.x + p.y;
}
"""
- from _ffi import CDLL, types, _StructDescr, Field
+ from _rawffi.alt import CDLL, types, _StructDescr, Field
Point = _StructDescr('Point', [
Field('x', types.slong),
Field('y', types.slong),
@@ -487,7 +487,7 @@
return p;
}
"""
- from _ffi import CDLL, types, _StructDescr, Field
+ from _rawffi.alt import CDLL, types, _StructDescr, Field
Point = _StructDescr('Point', [
Field('x', types.slong),
Field('y', types.slong),
@@ -500,9 +500,9 @@
assert p.getfield('x') == 12
assert p.getfield('y') == 34
- # XXX: support for _rawffi structures should be killed as soon as we
- # implement ctypes.Structure on top of _ffi. In the meantime, we support
- # both
+ # XXX: long ago the plan was to kill _rawffi structures in favor of
+ # _rawffi.alt structures. The plan never went anywhere, so we're
+ # stuck with both.
def test_byval_argument__rawffi(self):
"""
// defined above
@@ -510,7 +510,7 @@
DLLEXPORT long sum_point(struct Point p);
"""
import _rawffi
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
POINT = _rawffi.Structure([('x', 'l'), ('y', 'l')])
ffi_point = POINT.get_ffi_type()
libfoo = CDLL(self.libfoo_name)
@@ -529,7 +529,7 @@
DLLEXPORT struct Point make_point(long x, long y);
"""
import _rawffi
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
POINT = _rawffi.Structure([('x', 'l'), ('y', 'l')])
ffi_point = POINT.get_ffi_type()
libfoo = CDLL(self.libfoo_name)
@@ -542,23 +542,23 @@
def test_TypeError_numargs(self):
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy', [types.sint, types.sint], types.sint)
raises(TypeError, "sum_xy(1, 2, 3)")
raises(TypeError, "sum_xy(1)")
def test_TypeError_voidarg(self):
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
raises(TypeError, "libfoo.getfunc('sum_xy', [types.void], types.sint)")
def test_OSError_loading(self):
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
raises(OSError, "CDLL('I do not exist')")
def test_AttributeError_missing_function(self):
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
raises(AttributeError, "libfoo.getfunc('I_do_not_exist', [], types.void)")
if self.iswin32:
@@ -569,7 +569,7 @@
def test_calling_convention1(self):
if not self.iswin32:
skip("windows specific")
- from _ffi import WinDLL, types
+ from _rawffi.alt import WinDLL, types
libm = WinDLL(self.libm_name)
pow = libm.getfunc('pow', [types.double, types.double], types.double)
try:
@@ -582,7 +582,7 @@
def test_calling_convention2(self):
if not self.iswin32:
skip("windows specific")
- from _ffi import WinDLL, types
+ from _rawffi.alt import WinDLL, types
kernel = WinDLL('Kernel32.dll')
sleep = kernel.getfunc('Sleep', [types.uint], types.void)
sleep(10)
@@ -590,7 +590,7 @@
def test_calling_convention3(self):
if not self.iswin32:
skip("windows specific")
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
wrong_kernel = CDLL('Kernel32.dll')
wrong_sleep = wrong_kernel.getfunc('Sleep', [types.uint], types.void)
try:
@@ -603,7 +603,7 @@
def test_func_fromaddr2(self):
if not self.iswin32:
skip("windows specific")
- from _ffi import CDLL, types, FuncPtr
+ from _rawffi.alt import CDLL, types, FuncPtr
from _rawffi import FUNCFLAG_STDCALL
libm = CDLL(self.libm_name)
pow_addr = libm.getaddressindll('pow')
@@ -619,7 +619,7 @@
def test_func_fromaddr3(self):
if not self.iswin32:
skip("windows specific")
- from _ffi import WinDLL, types, FuncPtr
+ from _rawffi.alt import WinDLL, types, FuncPtr
from _rawffi import FUNCFLAG_STDCALL
kernel = WinDLL('Kernel32.dll')
sleep_addr = kernel.getaddressindll('Sleep')
@@ -636,7 +636,7 @@
"""
if not self.iswin32:
skip("windows specific")
- from _ffi import CDLL, types
+ from _rawffi.alt import CDLL, types
libfoo = CDLL(self.libfoo_name)
f_name = libfoo.getfunc('AAA_first_ordinal_function', [], types.sint)
f_ordinal = libfoo.getfunc(1, [], types.sint)
diff --git a/pypy/module/_ffi/test/test_struct.py b/pypy/module/_rawffi/alt/test/test_struct.py
rename from pypy/module/_ffi/test/test_struct.py
rename to pypy/module/_rawffi/alt/test/test_struct.py
--- a/pypy/module/_ffi/test/test_struct.py
+++ b/pypy/module/_rawffi/alt/test/test_struct.py
@@ -1,7 +1,7 @@
from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.module._ffi.interp_ffitype import app_types, W_FFIType
-from pypy.module._ffi.interp_struct import compute_size_and_alignement, W_Field
-from pypy.module._ffi.test.test_funcptr import BaseAppTestFFI
+from pypy.module._rawffi.alt.interp_ffitype import app_types, W_FFIType
+from pypy.module._rawffi.alt.interp_struct import compute_size_and_alignement, W_Field
+from pypy.module._rawffi.alt.test.test_funcptr import BaseAppTestFFI
class TestStruct(object):
@@ -69,7 +69,7 @@
cls.w_runappdirect = cls.space.wrap(cls.runappdirect)
def test__StructDescr(self):
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('x', types.slong),
@@ -80,7 +80,7 @@
assert descr.ffitype.name == 'struct foo'
def test_alignment(self):
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('x', types.sbyte),
@@ -92,7 +92,7 @@
assert fields[1].offset == longsize # aligned to WORD
def test_missing_field(self):
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('x', types.slong),
@@ -106,7 +106,7 @@
def test_unknown_type(self):
if self.runappdirect:
skip('cannot use self.dummy_type with -A')
- from _ffi import _StructDescr, Field
+ from _rawffi.alt import _StructDescr, Field
fields = [
Field('x', self.dummy_type),
]
@@ -116,7 +116,7 @@
raises(TypeError, "struct.setfield('x', 42)")
def test_getfield_setfield(self):
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('x', types.slong),
@@ -133,7 +133,7 @@
def test_getfield_setfield_signed_types(self):
import sys
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('sbyte', types.sbyte),
@@ -156,7 +156,7 @@
def test_getfield_setfield_unsigned_types(self):
import sys
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('ubyte', types.ubyte),
@@ -188,7 +188,7 @@
def test_getfield_setfield_longlong(self):
import sys
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('slonglong', types.slonglong),
@@ -205,7 +205,7 @@
def test_getfield_setfield_float(self):
import sys
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('x', types.double),
@@ -219,7 +219,7 @@
def test_getfield_setfield_singlefloat(self):
import sys
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('x', types.float),
@@ -237,7 +237,7 @@
assert mem == [123.5]
def test_define_fields(self):
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('x', types.slong),
@@ -255,7 +255,7 @@
raises(ValueError, "descr.define_fields(fields)")
def test_pointer_to_incomplete_struct(self):
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
fields = [
Field('x', types.slong),
@@ -271,7 +271,7 @@
assert types.Pointer(descr.ffitype) is foo_p
def test_nested_structure(self):
- from _ffi import _StructDescr, Field, types
+ from _rawffi.alt import _StructDescr, Field, types
longsize = types.slong.sizeof()
foo_fields = [
Field('x', types.slong),
@@ -310,7 +310,7 @@
def test_compute_shape(self):
- from _ffi import Structure, Field, types
+ from _rawffi.alt import Structure, Field, types
class Point(Structure):
_fields_ = [
Field('x', types.slong),
diff --git a/pypy/module/_ffi/test/test_type_converter.py b/pypy/module/_rawffi/alt/test/test_type_converter.py
rename from pypy/module/_ffi/test/test_type_converter.py
rename to pypy/module/_rawffi/alt/test/test_type_converter.py
--- a/pypy/module/_ffi/test/test_type_converter.py
+++ b/pypy/module/_rawffi/alt/test/test_type_converter.py
@@ -1,8 +1,8 @@
import sys
from rpython.rlib.rarithmetic import r_uint, r_singlefloat, r_longlong, r_ulonglong
from rpython.rlib.libffi import IS_32_BIT
-from pypy.module._ffi.interp_ffitype import app_types, descr_new_pointer
-from pypy.module._ffi.type_converter import FromAppLevelConverter, ToAppLevelConverter
+from pypy.module._rawffi.alt.interp_ffitype import app_types, descr_new_pointer
+from pypy.module._rawffi.alt.type_converter import FromAppLevelConverter, ToAppLevelConverter
class DummyFromAppLevelConverter(FromAppLevelConverter):
@@ -29,7 +29,7 @@
class TestFromAppLevel(object):
- spaceconfig = dict(usemodules=('_ffi',))
+ spaceconfig = dict(usemodules=('_rawffi',))
def setup_class(cls):
converter = DummyFromAppLevelConverter(cls.space)
@@ -104,12 +104,12 @@
def test__as_ffi_pointer_(self):
space = self.space
w_MyPointerWrapper = space.appexec([], """():
- import _ffi
+ from _rawffi.alt import types
class MyPointerWrapper(object):
def __init__(self, value):
self.value = value
def _as_ffi_pointer_(self, ffitype):
- assert ffitype is _ffi.types.void_p
+ assert ffitype is types.void_p
return self.value
return MyPointerWrapper
@@ -151,7 +151,7 @@
class TestToAppLevel(object):
- spaceconfig = dict(usemodules=('_ffi',))
+ spaceconfig = dict(usemodules=('_rawffi',))
def setup_class(cls):
converter = DummyToAppLevelConverter(cls.space)
diff --git a/pypy/module/_ffi/test/test_ztranslation.py b/pypy/module/_rawffi/alt/test/test_ztranslation.py
rename from pypy/module/_ffi/test/test_ztranslation.py
rename to pypy/module/_rawffi/alt/test/test_ztranslation.py
--- a/pypy/module/_ffi/test/test_ztranslation.py
+++ b/pypy/module/_rawffi/alt/test/test_ztranslation.py
@@ -1,4 +1,4 @@
-from pypy.objspace.fake.checkmodule import checkmodule
-
-def test__ffi_translates():
- checkmodule('_ffi', '_rawffi')
+from pypy.objspace.fake.checkmodule import checkmodule
+
+def test__ffi_translates():
+ checkmodule('_rawffi')
diff --git a/pypy/module/_ffi/type_converter.py b/pypy/module/_rawffi/alt/type_converter.py
rename from pypy/module/_ffi/type_converter.py
rename to pypy/module/_rawffi/alt/type_converter.py
--- a/pypy/module/_ffi/type_converter.py
+++ b/pypy/module/_rawffi/alt/type_converter.py
@@ -3,7 +3,7 @@
from rpython.rlib.rarithmetic import r_uint
from pypy.interpreter.error import operationerrfmt, OperationError
from pypy.module._rawffi.structure import W_StructureInstance, W_Structure
-from pypy.module._ffi.interp_ffitype import app_types
+from pypy.module._rawffi.alt.interp_ffitype import app_types
class FromAppLevelConverter(object):
"""
@@ -17,7 +17,7 @@
self.space = space
def unwrap_and_do(self, w_ffitype, w_obj):
- from pypy.module._ffi.interp_struct import W__StructInstance
+ from pypy.module._rawffi.alt.interp_struct import W__StructInstance
space = self.space
if w_ffitype.is_longlong():
# note that we must check for longlong first, because either
@@ -194,7 +194,7 @@
self.space = space
def do_and_wrap(self, w_ffitype):
- from pypy.module._ffi.interp_struct import W__StructDescr
+ from pypy.module._rawffi.alt.interp_struct import W__StructDescr
space = self.space
if w_ffitype.is_longlong():
# note that we must check for longlong first, because either
diff --git a/pypy/module/_rawffi/interp_rawffi.py b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -311,10 +311,7 @@
raise NotImplementedError
def descr_get_ffi_type(self, space):
- # XXX: this assumes that you have the _ffi module enabled. In the long
- # term, probably we will move the code for build structures and arrays
- # from _rawffi to _ffi
- from pypy.module._ffi.interp_ffitype import W_FFIType
+ from pypy.module._rawffi.alt.interp_ffitype import W_FFIType
return W_FFIType('<unknown>', self.get_basic_ffi_type(), self)
@unwrap_spec(n=int)
@@ -579,7 +576,7 @@
s = rffi.wcharpsize2unicode(rffi.cast(rffi.CWCHARP, address), maxlength)
return space.wrap(s)
- at unwrap_spec(address=r_uint, newcontent=str)
+ at unwrap_spec(address=r_uint, newcontent='bufferstr')
def rawstring2charp(space, address, newcontent):
from rpython.rtyper.annlowlevel import llstr
from rpython.rtyper.lltypesystem.rstr import copy_string_to_raw
diff --git a/pypy/module/_rawffi/test/test__rawffi.py b/pypy/module/_rawffi/test/test__rawffi.py
--- a/pypy/module/_rawffi/test/test__rawffi.py
+++ b/pypy/module/_rawffi/test/test__rawffi.py
@@ -329,6 +329,8 @@
a = A(10, 'x'*10)
_rawffi.rawstring2charp(a.buffer, "foobar")
assert ''.join([a[i] for i in range(10)]) == "foobarxxxx"
+ _rawffi.rawstring2charp(a.buffer, buffer("baz"))
+ assert ''.join([a[i] for i in range(10)]) == "bazbarxxxx"
a.free()
def test_raw_callable(self):
@@ -1137,24 +1139,32 @@
gc.collect()
gc.collect()
S = _rawffi.Structure([('x', 'i')])
- oldnum = _rawffi._num_of_allocated_objects()
+ try:
+ oldnum = _rawffi._num_of_allocated_objects()
+ except RuntimeError:
+ oldnum = '?'
s = S(autofree=True)
s.x = 3
s = None
gc.collect()
- assert oldnum == _rawffi._num_of_allocated_objects()
+ if oldnum != '?':
+ assert oldnum == _rawffi._num_of_allocated_objects()
def test_array_autofree(self):
import gc, _rawffi
gc.collect()
- oldnum = _rawffi._num_of_allocated_objects()
+ try:
+ oldnum = _rawffi._num_of_allocated_objects()
+ except RuntimeError:
+ oldnum = '?'
A = _rawffi.Array('c')
a = A(6, 'xxyxx\x00', autofree=True)
assert _rawffi.charp2string(a.buffer) == 'xxyxx'
a = None
gc.collect()
- assert oldnum == _rawffi._num_of_allocated_objects()
+ if oldnum != '?':
+ assert oldnum == _rawffi._num_of_allocated_objects()
def teardown_class(cls):
Tracker.DO_TRACING = False
diff --git a/pypy/module/cppyy/test/test_crossing.py b/pypy/module/cppyy/test/test_crossing.py
--- a/pypy/module/cppyy/test/test_crossing.py
+++ b/pypy/module/cppyy/test/test_crossing.py
@@ -65,7 +65,7 @@
return str(pydname)
class AppTestCrossing(AppTestCpythonExtensionBase):
- spaceconfig = dict(usemodules=['cpyext', 'cppyy', 'thread', '_rawffi', '_ffi',
+ spaceconfig = dict(usemodules=['cpyext', 'cppyy', 'thread', '_rawffi',
'array', 'itertools', 'rctime', 'binascii'])
def setup_class(cls):
diff --git a/pypy/module/cpyext/test/test_ztranslation.py b/pypy/module/cpyext/test/test_ztranslation.py
--- a/pypy/module/cpyext/test/test_ztranslation.py
+++ b/pypy/module/cpyext/test/test_ztranslation.py
@@ -1,4 +1,4 @@
from pypy.objspace.fake.checkmodule import checkmodule
def test_cpyext_translates():
- checkmodule('cpyext', '_ffi')
+ checkmodule('cpyext')
diff --git a/pypy/module/pypyjit/policy.py b/pypy/module/pypyjit/policy.py
--- a/pypy/module/pypyjit/policy.py
+++ b/pypy/module/pypyjit/policy.py
@@ -105,7 +105,7 @@
else:
rest = ''
if modname in ['pypyjit', 'signal', 'micronumpy', 'math', 'exceptions',
- 'imp', 'sys', 'array', '_ffi', 'itertools', 'operator',
+ 'imp', 'sys', 'array', 'itertools', 'operator',
'posix', '_socket', '_sre', '_lsprof', '_weakref',
'__pypy__', 'cStringIO', '_collections', 'struct',
'mmap', 'marshal', '_codecs', 'rctime', 'cppyy',
diff --git a/pypy/module/struct/__init__.py b/pypy/module/struct/__init__.py
--- a/pypy/module/struct/__init__.py
+++ b/pypy/module/struct/__init__.py
@@ -49,11 +49,12 @@
'calcsize': 'interp_struct.calcsize',
'pack': 'interp_struct.pack',
'unpack': 'interp_struct.unpack',
- }
+
+ 'Struct': 'interp_struct.W_Struct',
+ }
appleveldefs = {
'error': 'app_struct.error',
'pack_into': 'app_struct.pack_into',
'unpack_from': 'app_struct.unpack_from',
- 'Struct': 'app_struct.Struct',
- }
+ }
diff --git a/pypy/module/struct/app_struct.py b/pypy/module/struct/app_struct.py
--- a/pypy/module/struct/app_struct.py
+++ b/pypy/module/struct/app_struct.py
@@ -4,6 +4,7 @@
"""
import struct
+
class error(Exception):
"""Exception raised on various occasions; argument is a string
describing what is wrong."""
@@ -21,21 +22,3 @@
raise error("unpack_from requires a buffer of at least %d bytes"
% (size,))
return struct.unpack(fmt, data)
-
-# XXX inefficient
-class Struct(object):
- def __init__(self, format):
- self.format = format
- self.size = struct.calcsize(format)
-
- def pack(self, *args):
- return struct.pack(self.format, *args)
-
- def unpack(self, s):
- return struct.unpack(self.format, s)
-
- def pack_into(self, buffer, offset, *args):
- return pack_into(self.format, buffer, offset, *args)
-
- def unpack_from(self, buffer, offset=0):
- return unpack_from(self.format, buffer, offset)
diff --git a/pypy/module/struct/interp_struct.py b/pypy/module/struct/interp_struct.py
--- a/pypy/module/struct/interp_struct.py
+++ b/pypy/module/struct/interp_struct.py
@@ -1,15 +1,22 @@
-from pypy.interpreter.gateway import unwrap_spec
-from pypy.interpreter.error import OperationError
-from pypy.module.struct.formatiterator import PackFormatIterator, UnpackFormatIterator
from rpython.rlib import jit
from rpython.rlib.rstruct.error import StructError, StructOverflowError
from rpython.rlib.rstruct.formatiterator import CalcSizeFormatIterator
+from rpython.tool.sourcetools import func_with_new_name
+
+from pypy.interpreter.baseobjspace import W_Root
+from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.typedef import TypeDef, interp_attrproperty
+from pypy.module.struct.formatiterator import (
+ PackFormatIterator, UnpackFormatIterator
+)
@unwrap_spec(format=str)
def calcsize(space, format):
return space.wrap(_calcsize(space, format))
+
def _calcsize(space, format):
fmtiter = CalcSizeFormatIterator()
try:
@@ -52,3 +59,44 @@
w_error = space.getattr(w_module, space.wrap('error'))
raise OperationError(w_error, space.wrap(e.msg))
return space.newtuple(fmtiter.result_w[:])
+
+
+class W_Struct(W_Root):
+ _immutable_fields_ = ["format", "size"]
+
+ def __init__(self, space, format):
+ self.format = format
+ self.size = _calcsize(space, format)
+
+ @unwrap_spec(format=str)
+ def descr__new__(space, w_subtype, format):
+ self = space.allocate_instance(W_Struct, w_subtype)
+ W_Struct.__init__(self, space, format)
+ return self
+
+ def wrap_struct_method(name):
+ def impl(self, space, __args__):
+ w_module = space.getbuiltinmodule('struct')
+ w_method = space.getattr(w_module, space.wrap(name))
+ return space.call_obj_args(
+ w_method, space.wrap(self.format), __args__
+ )
+
+ return func_with_new_name(impl, 'descr_' + name)
+
+ descr_pack = wrap_struct_method("pack")
+ descr_unpack = wrap_struct_method("unpack")
+ descr_pack_into = wrap_struct_method("pack_into")
+ descr_unpack_from = wrap_struct_method("unpack_from")
+
+
+W_Struct.typedef = TypeDef("Struct",
+ __new__=interp2app(W_Struct.descr__new__.im_func),
+ format=interp_attrproperty("format", cls=W_Struct),
+ size=interp_attrproperty("size", cls=W_Struct),
+
+ pack=interp2app(W_Struct.descr_pack),
+ unpack=interp2app(W_Struct.descr_unpack),
+ pack_into=interp2app(W_Struct.descr_pack_into),
+ unpack_from=interp2app(W_Struct.descr_unpack_from),
+)
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/support.py b/pypy/module/test_lib_pypy/ctypes_tests/support.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/support.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/support.py
@@ -37,6 +37,8 @@
pass
def teardown_class(cls):
+ if not hasattr(sys, 'pypy_translation_info'):
+ return
if sys.pypy_translation_info['translation.gc'] == 'boehm':
return # it seems that boehm has problems with __del__, so not
# everything is freed
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_strings.py b/pypy/module/test_lib_pypy/ctypes_tests/test_strings.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_strings.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_strings.py
@@ -38,6 +38,16 @@
buf.raw = "Hello, World"
assert buf.value == "Hello, World"
+ def test_c_buffer_raw_from_buffer(self):
+ buf = c_buffer(32)
+ buf.raw = buffer("Hello, World")
+ assert buf.value == "Hello, World"
+
+ def test_c_buffer_raw_from_memoryview(self):
+ buf = c_buffer(32)
+ buf.raw = memoryview("Hello, World")
+ assert buf.value == "Hello, World"
+
def test_param_1(self):
BUF = c_char * 4
buf = BUF()
diff --git a/pypy/module/test_lib_pypy/test_grp_extra.py b/pypy/module/test_lib_pypy/test_grp_extra.py
--- a/pypy/module/test_lib_pypy/test_grp_extra.py
+++ b/pypy/module/test_lib_pypy/test_grp_extra.py
@@ -2,7 +2,7 @@
class AppTestGrp:
- spaceconfig = dict(usemodules=('binascii', '_ffi', '_rawffi', 'itertools'))
+ spaceconfig = dict(usemodules=('binascii', '_rawffi', 'itertools'))
def setup_class(cls):
cls.w_grp = import_lib_pypy(cls.space, 'grp',
diff --git a/pypy/module/test_lib_pypy/test_os_wait.py b/pypy/module/test_lib_pypy/test_os_wait.py
--- a/pypy/module/test_lib_pypy/test_os_wait.py
+++ b/pypy/module/test_lib_pypy/test_os_wait.py
@@ -10,7 +10,7 @@
class AppTestOsWait:
- spaceconfig = dict(usemodules=('_ffi', '_rawffi', 'itertools'))
+ spaceconfig = dict(usemodules=('_rawffi', 'itertools'))
def setup_class(cls):
if not hasattr(os, "fork"):
diff --git a/pypy/module/test_lib_pypy/test_pwd.py b/pypy/module/test_lib_pypy/test_pwd.py
--- a/pypy/module/test_lib_pypy/test_pwd.py
+++ b/pypy/module/test_lib_pypy/test_pwd.py
@@ -1,7 +1,7 @@
import py, sys
class AppTestPwd:
- spaceconfig = dict(usemodules=('_ffi', '_rawffi', 'itertools', 'binascii'))
+ spaceconfig = dict(usemodules=('_rawffi', 'itertools', 'binascii'))
def setup_class(cls):
if sys.platform == 'win32':
diff --git a/pypy/module/test_lib_pypy/test_resource.py b/pypy/module/test_lib_pypy/test_resource.py
--- a/pypy/module/test_lib_pypy/test_resource.py
+++ b/pypy/module/test_lib_pypy/test_resource.py
@@ -9,7 +9,7 @@
class AppTestResource:
- spaceconfig = dict(usemodules=('_ffi', '_rawffi', 'itertools'))
+ spaceconfig = dict(usemodules=('_rawffi', 'itertools'))
def setup_class(cls):
rebuild.rebuild_one('resource.ctc.py')
diff --git a/pypy/objspace/std/stringmethods.py b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -35,13 +35,7 @@
if (isinstance(self, W_BytearrayObject) and
space.isinstance_w(w_sub, space.w_int)):
char = space.int_w(w_sub)
- if not 0 <= char < 256:
- raise OperationError(space.w_ValueError,
- space.wrap("byte must be in range(0, 256)"))
- for c in self.data:
- if ord(c) == char:
- return space.w_True
- return space.w_False
+ return _descr_contains_bytearray(self.data, space, char)
return space.newbool(self._val(space).find(self._op_val(space, w_sub)) >= 0)
def descr_add(self, space, w_other):
@@ -79,7 +73,7 @@
assert start >= 0 and stop >= 0
return self._sliced(space, selfvalue, start, stop, self)
else:
- ret = [selfvalue[start + i*step] for i in range(sl)]
+ ret = _descr_getslice_slowpath(selfvalue, start, step, sl)
return self._new_from_list(ret)
index = space.getindex_w(w_index, space.w_IndexError, "string index")
@@ -253,17 +247,21 @@
return self._is_generic(space, '_isdigit')
# this is only for bytes and bytesarray: unicodeobject overrides it
+ def _descr_islower_slowpath(self, space, v):
+ cased = False
+ for idx in range(len(v)):
+ if self._isupper(v[idx]):
+ return False
+ elif not cased and self._islower(v[idx]):
+ cased = True
+ return cased
+
def descr_islower(self, space):
v = self._val(space)
if len(v) == 1:
c = v[0]
return space.newbool(self._islower(c))
- cased = False
- for idx in range(len(v)):
- if self._isupper(v[idx]):
- return space.w_False
- elif not cased and self._islower(v[idx]):
- cased = True
+ cased = self._descr_islower_slowpath(space, v)
return space.newbool(cased)
def descr_isspace(self, space):
@@ -291,17 +289,21 @@
return space.newbool(cased)
# this is only for bytes and bytesarray: unicodeobject overrides it
+ def _descr_isupper_slowpath(self, space, v):
+ cased = False
+ for idx in range(len(v)):
+ if self._islower(v[idx]):
+ return False
+ elif not cased and self._isupper(v[idx]):
+ cased = True
+ return cased
+
def descr_isupper(self, space):
v = self._val(space)
if len(v) == 1:
c = v[0]
return space.newbool(self._isupper(c))
- cased = False
- for idx in range(len(v)):
- if self._islower(v[idx]):
- return space.w_False
- elif not cased and self._isupper(v[idx]):
- cased = True
+ cased = self._descr_isupper_slowpath(space, v)
return space.newbool(cased)
def descr_join(self, space, w_list):
@@ -677,3 +679,19 @@
def descr_getnewargs(self, space):
return space.newtuple([self._new(self._val(space))])
+
+# ____________________________________________________________
+# helpers for slow paths, moved out because they contain loops
+
+def _descr_contains_bytearray(data, space, char):
+ if not 0 <= char < 256:
+ raise OperationError(space.w_ValueError,
+ space.wrap("byte must be in range(0, 256)"))
+ for c in data:
+ if ord(c) == char:
+ return space.w_True
+ return space.w_False
+
+ at specialize.argtype(0)
+def _descr_getslice_slowpath(selfvalue, start, step, sl):
+ return [selfvalue[start + i*step] for i in range(sl)]
diff --git a/pypy/tool/pypyjit.py b/pypy/tool/pypyjit.py
--- a/pypy/tool/pypyjit.py
+++ b/pypy/tool/pypyjit.py
@@ -37,7 +37,7 @@
config.objspace.usemodules._sre = False
config.objspace.usemodules._lsprof = False
#
-config.objspace.usemodules._ffi = False
+config.objspace.usemodules._rawffi = False
config.objspace.usemodules.micronumpy = False
#
set_pypy_opt_level(config, level='jit')
diff --git a/rpython/rlib/ropenssl.py b/rpython/rlib/ropenssl.py
--- a/rpython/rlib/ropenssl.py
+++ b/rpython/rlib/ropenssl.py
@@ -58,7 +58,6 @@
ASN1_ITEM = rffi.COpaquePtr('ASN1_ITEM')
X509_NAME = rffi.COpaquePtr('X509_NAME')
-# maybe there's a better way but this is correct and fixes LLVM translation
class CConfigBootstrap:
_compilation_info_ = eci
OPENSSL_EXPORT_VAR_AS_FUNCTION = rffi_platform.Defined(
diff --git a/rpython/rlib/test/test_runicode.py b/rpython/rlib/test/test_runicode.py
--- a/rpython/rlib/test/test_runicode.py
+++ b/rpython/rlib/test/test_runicode.py
@@ -99,7 +99,7 @@
called[0] += 1
if called[0] == 1:
assert errors == "foo!"
- assert enc == encoding
+ assert enc == encoding.replace('-', '')
assert t is s
assert start == startingpos
assert stop == endingpos
diff --git a/rpython/rtyper/lltypesystem/lloperation.py b/rpython/rtyper/lltypesystem/lloperation.py
--- a/rpython/rtyper/lltypesystem/lloperation.py
+++ b/rpython/rtyper/lltypesystem/lloperation.py
@@ -167,7 +167,7 @@
#
# This list corresponds to the operations implemented by the LLInterpreter.
# Note that many exception-raising operations can be replaced by calls
-# to helper functions in rpython.rtyper.raisingops.raisingops.
+# to helper functions in rpython.rtyper.raisingops.
# ***** Run test_lloperation after changes. *****
LL_OPERATIONS = {
diff --git a/rpython/rtyper/raisingops.py b/rpython/rtyper/raisingops.py
new file mode 100644
--- /dev/null
+++ b/rpython/rtyper/raisingops.py
@@ -0,0 +1,295 @@
+import sys
+from rpython.rlib.rarithmetic import r_longlong, r_uint, intmask
+from rpython.rtyper.lltypesystem.lloperation import llop
+from rpython.rtyper.lltypesystem.lltype import Signed, SignedLongLong, \
+ UnsignedLongLong
+
+#XXX original SIGNED_RIGHT_SHIFT_ZERO_FILLS not taken into account
+#XXX assuming HAVE_LONG_LONG (int_mul_ovf)
+#XXX should int_mod and int_floordiv return an intmask(...) instead?
+
+LONG_MAX = sys.maxint
+LONG_MIN = -sys.maxint-1
+
+LLONG_MAX = r_longlong(2 ** (r_longlong.BITS-1) - 1)
+LLONG_MIN = -LLONG_MAX-1
+
+def int_floordiv_zer(x, y):
+ '''#define OP_INT_FLOORDIV_ZER(x,y,r,err) \
+ if ((y)) { OP_INT_FLOORDIV(x,y,r,err); } \
+ else FAIL_ZER(err, "integer division")
+ '''
+ if y:
+ return llop.int_floordiv(Signed, x, y)
+ else:
+ raise ZeroDivisionError("integer division")
+
+def uint_floordiv_zer(x, y):
+ '''#define OP_UINT_FLOORDIV_ZER(x,y,r,err) \
+ if ((y)) { OP_UINT_FLOORDIV(x,y,r,err); } \
+ else FAIL_ZER(err, "unsigned integer division")
+ '''
+ if y:
+ return x / y
+ else:
+ raise ZeroDivisionError("unsigned integer division")
+
+def llong_floordiv_zer(x, y):
+ '''#define OP_LLONG_FLOORDIV_ZER(x,y,r) \
+ if ((y)) { OP_LLONG_FLOORDIV(x,y,r); } \
+ else FAIL_ZER("integer division")
+ '''
+ if y:
+ return llop.llong_floordiv(SignedLongLong, x, y)
+ else:
+ raise ZeroDivisionError("integer division")
+
+def ullong_floordiv_zer(x, y):
+ '''#define OP_ULLONG_FLOORDIV_ZER(x,y,r) \
+ if ((y)) { OP_ULLONG_FLOORDIV(x,y,r); } \
+ else FAIL_ZER("unsigned integer division")
+ '''
+ if y:
+ return llop.llong_floordiv(UnsignedLongLong, x, y)
+ else:
+ raise ZeroDivisionError("unsigned integer division")
+
+
+def int_neg_ovf(x):
+ if x == LONG_MIN:
+ raise OverflowError("integer negate")
+ return -x
+
+def llong_neg_ovf(x):
+ if x == LLONG_MIN:
+ raise OverflowError("integer negate")
+ return -x
+
+def int_abs_ovf(x):
+ if x == LONG_MIN:
+ raise OverflowError("integer absolute")
+ if x < 0:
+ return -x
+ else:
+ return x
+
+def llong_abs_ovf(x):
+ if x == LLONG_MIN:
+ raise OverflowError("integer absolute")
+ if x < 0:
+ return -x
+ else:
+ return x
+
+def _int_add_ovf(x, y):
+ '''#define OP_INT_ADD_OVF(x,y,r,err) \
+ OP_INT_ADD(x,y,r,err); \
+ if ((r^(x)) >= 0 || (r^(y)) >= 0); \
+ else FAIL_OVF(err, "integer addition")
+ '''
+ r = x + y
+ if r^x >= 0 or r^y >= 0:
+ return r
+ else:
+ raise OverflowError("integer addition")
+
+def _int_add_nonneg_ovf(x, y):
+ '''
+ OP_INT_ADD(x,y,r); \
+ if (r >= (x)); \
+ else FAIL_OVF("integer addition")
+ '''
+ r = x + y
+ if r >= x:
+ return r
+ else:
+ raise OverflowError("integer addition")
+
+def _int_sub_ovf(x, y):
+ '''#define OP_INT_SUB_OVF(x,y,r,err) \
+ OP_INT_SUB(x,y,r,err); \
+ if ((r^(x)) >= 0 || (r^~(y)) >= 0); \
+ else FAIL_OVF(err, "integer subtraction")
+ '''
+ r = x - y
+ if r^x >= 0 or r^~y >= 0:
+ return r
+ else:
+ raise OverflowError("integer subtraction")
+
+def int_lshift_ovf(x, y):
+ '''#define OP_INT_LSHIFT_OVF(x,y,r,err) \
+ OP_INT_LSHIFT(x,y,r,err); \
+ if ((x) != Py_ARITHMETIC_RIGHT_SHIFT(long, r, (y))) \
+ FAIL_OVF(err, "x<<y losing bits or changing sign")
+ '''
+ r = x << y
+ if x != _Py_ARITHMETIC_RIGHT_SHIFT(r, y):
+ raise OverflowError("x<<y losing bits or changing sign")
+ else:
+ return r
+
+def int_rshift_val(x, y):
+ '''#define OP_INT_RSHIFT_VAL(x,y,r,err) \
+ if ((y) >= 0) { OP_INT_RSHIFT(x,y,r,err); } \
+ else FAIL_VAL(err, "negative shift count")
+ '''
+ if y >= 0:
+ return _Py_ARITHMETIC_RIGHT_SHIFT(x, y)
+ else:
+ raise ValueError("negative shift count")
+
+def int_lshift_val(x, y):
+ '''#define OP_INT_LSHIFT_VAL(x,y,r,err) \
+ if ((y) >= 0) { OP_INT_LSHIFT(x,y,r,err); } \
+ else FAIL_VAL(err, "negative shift count")
+ '''
+ if y >= 0:
+ return x << y
+ else:
+ raise ValueError("negative shift count")
+
+def int_lshift_ovf_val(x, y):
+ '''#define OP_INT_LSHIFT_OVF_VAL(x,y,r,err) \
+ if ((y) >= 0) { OP_INT_LSHIFT_OVF(x,y,r,err); } \
+ else FAIL_VAL(err, "negative shift count")
+ '''
+ if y >= 0:
+ return int_lshift_ovf(x, y)
+ else:
+ raise ValueError("negative shift count")
+
+def int_floordiv_ovf(x, y):
+ '''#define OP_INT_FLOORDIV_OVF(x,y,r,err) \
+ if ((y) == -1 && (x) < 0 && ((unsigned long)(x) << 1) == 0) \
+ FAIL_OVF(err, "integer division"); \
+ OP_INT_FLOORDIV(x,y,r,err)
+ '''
+ if y == -1 and x < 0 and (r_uint(x) << 1) == 0:
+ raise OverflowError("integer division")
+ else:
+ return llop.int_floordiv(Signed, x, y)
+
+def int_floordiv_ovf_zer(x, y):
+ '''#define OP_INT_FLOORDIV_OVF_ZER(x,y,r,err) \
+ if ((y)) { OP_INT_FLOORDIV_OVF(x,y,r,err); } \
+ else FAIL_ZER(err, "integer division")
+ '''
+ if y:
+ return int_floordiv_ovf(x, y)
+ else:
+ raise ZeroDivisionError("integer division")
+
+def int_mod_ovf(x, y):
+ '''#define OP_INT_MOD_OVF(x,y,r,err) \
+ if ((y) == -1 && (x) < 0 && ((unsigned long)(x) << 1) == 0) \
+ FAIL_OVF(err, "integer modulo"); \
+ OP_INT_MOD(x,y,r,err)
+ '''
+ if y == -1 and x < 0 and (r_uint(x) << 1) == 0:
+ raise OverflowError("integer modulo")
+ else:
+ return llop.int_mod(Signed, x, y)
+
+def int_mod_zer(x, y):
+ '''#define OP_INT_MOD_ZER(x,y,r,err) \
+ if ((y)) { OP_INT_MOD(x,y,r,err); } \
+ else FAIL_ZER(err, "integer modulo")
+ '''
+ if y:
+ return llop.int_mod(Signed, x, y)
+ else:
+ raise ZeroDivisionError("integer modulo")
+
+def uint_mod_zer(x, y):
+ '''#define OP_UINT_MOD_ZER(x,y,r,err) \
+ if ((y)) { OP_UINT_MOD(x,y,r,err); } \
+ else FAIL_ZER(err, "unsigned integer modulo")
+ '''
+ if y:
+ return x % y
+ else:
+ raise ZeroDivisionError("unsigned integer modulo")
+
+def int_mod_ovf_zer(x, y):
+ '''#define OP_INT_MOD_OVF_ZER(x,y,r,err) \
+ if ((y)) { OP_INT_MOD_OVF(x,y,r,err); } \
+ else FAIL_ZER(err, "integer modulo")
+ '''
+ if y:
+ return int_mod_ovf(x, y)
+ else:
+ raise ZeroDivisionError("integer modulo")
+
+def llong_mod_zer(x, y):
+ '''#define OP_LLONG_MOD_ZER(x,y,r) \
+ if ((y)) { OP_LLONG_MOD(x,y,r); } \
+ else FAIL_ZER("integer modulo")
+ '''
+ if y:
+ return llop.int_mod(SignedLongLong, x, y)
+ else:
+ raise ZeroDivisionError("integer modulo")
+
+# Helpers...
+
+def _Py_ARITHMETIC_RIGHT_SHIFT(i, j):
+ '''
+// Py_ARITHMETIC_RIGHT_SHIFT
+// C doesn't define whether a right-shift of a signed integer sign-extends
+// or zero-fills. Here a macro to force sign extension:
+// Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
+// Return I >> J, forcing sign extension.
+// Requirements:
+// I is of basic signed type TYPE (char, short, int, long, or long long).
+// TYPE is one of char, short, int, long, or long long, although long long
+// must not be used except on platforms that support it.
+// J is an integer >= 0 and strictly less than the number of bits in TYPE
+// (because C doesn't define what happens for J outside that range either).
+// Caution:
+// I may be evaluated more than once.
+
+#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
+ #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
+ ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
+#else
+ #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
+#endif
+ '''
+ return i >> j
+
+#XXX some code from src/int.h seems missing
+#def int_mul_ovf(x, y): #HAVE_LONG_LONG version
+# '''{ \
+# PY_LONG_LONG lr = (PY_LONG_LONG)(x) * (PY_LONG_LONG)(y); \
+# r = (long)lr; \
+# if ((PY_LONG_LONG)r == lr); \
+# else FAIL_OVF(err, "integer multiplication"); \
+# }
+# '''
+# lr = r_longlong(x) * r_longlong(y);
+# r = intmask(lr)
+# if r_longlong(r) == lr:
+# return r
+# else:
+# raise OverflowError("integer multiplication")
+
+#not HAVE_LONG_LONG version
+def _int_mul_ovf(a, b): #long a, long b, long *longprod):
+ longprod = a * b
+ doubleprod = float(a) * float(b)
+ doubled_longprod = float(longprod)
+
+ # Fast path for normal case: small multiplicands, and no info is lost in either method.
+ if doubled_longprod == doubleprod:
+ return longprod
+
+ # Somebody somewhere lost info. Close enough, or way off? Note
+ # that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
+ # The difference either is or isn't significant compared to the
+ # true value (of which doubleprod is a good approximation).
+ # absdiff/absprod <= 1/32 iff 32 * absdiff <= absprod -- 5 good bits is "close enough"
+ if 32.0 * abs(doubled_longprod - doubleprod) <= abs(doubleprod):
+ return longprod
+
+ raise OverflowError("integer multiplication")
diff --git a/rpython/rtyper/raisingops/__init__.py b/rpython/rtyper/raisingops/__init__.py
deleted file mode 100644
diff --git a/rpython/rtyper/raisingops/raisingops.py b/rpython/rtyper/raisingops/raisingops.py
deleted file mode 100644
--- a/rpython/rtyper/raisingops/raisingops.py
+++ /dev/null
@@ -1,295 +0,0 @@
-import sys
-from rpython.rlib.rarithmetic import r_longlong, r_uint, intmask
-from rpython.rtyper.lltypesystem.lloperation import llop
-from rpython.rtyper.lltypesystem.lltype import Signed, SignedLongLong, \
- UnsignedLongLong
-
-#XXX original SIGNED_RIGHT_SHIFT_ZERO_FILLS not taken into account
-#XXX assuming HAVE_LONG_LONG (int_mul_ovf)
-#XXX should int_mod and int_floordiv return an intmask(...) instead?
-
-LONG_MAX = sys.maxint
-LONG_MIN = -sys.maxint-1
-
-LLONG_MAX = r_longlong(2 ** (r_longlong.BITS-1) - 1)
-LLONG_MIN = -LLONG_MAX-1
-
-def int_floordiv_zer(x, y):
- '''#define OP_INT_FLOORDIV_ZER(x,y,r,err) \
- if ((y)) { OP_INT_FLOORDIV(x,y,r,err); } \
- else FAIL_ZER(err, "integer division")
- '''
- if y:
- return llop.int_floordiv(Signed, x, y)
- else:
- raise ZeroDivisionError("integer division")
-
-def uint_floordiv_zer(x, y):
- '''#define OP_UINT_FLOORDIV_ZER(x,y,r,err) \
- if ((y)) { OP_UINT_FLOORDIV(x,y,r,err); } \
- else FAIL_ZER(err, "unsigned integer division")
- '''
- if y:
- return x / y
- else:
- raise ZeroDivisionError("unsigned integer division")
-
-def llong_floordiv_zer(x, y):
- '''#define OP_LLONG_FLOORDIV_ZER(x,y,r) \
- if ((y)) { OP_LLONG_FLOORDIV(x,y,r); } \
- else FAIL_ZER("integer division")
- '''
- if y:
- return llop.llong_floordiv(SignedLongLong, x, y)
- else:
- raise ZeroDivisionError("integer division")
-
-def ullong_floordiv_zer(x, y):
- '''#define OP_ULLONG_FLOORDIV_ZER(x,y,r) \
- if ((y)) { OP_ULLONG_FLOORDIV(x,y,r); } \
- else FAIL_ZER("unsigned integer division")
- '''
- if y:
- return llop.llong_floordiv(UnsignedLongLong, x, y)
- else:
- raise ZeroDivisionError("unsigned integer division")
-
-
-def int_neg_ovf(x):
- if x == LONG_MIN:
- raise OverflowError("integer negate")
- return -x
-
-def llong_neg_ovf(x):
- if x == LLONG_MIN:
- raise OverflowError("integer negate")
- return -x
-
-def int_abs_ovf(x):
- if x == LONG_MIN:
- raise OverflowError("integer absolute")
- if x < 0:
- return -x
- else:
- return x
-
-def llong_abs_ovf(x):
- if x == LLONG_MIN:
- raise OverflowError("integer absolute")
- if x < 0:
- return -x
- else:
- return x
-
-def _int_add_ovf(x, y):
- '''#define OP_INT_ADD_OVF(x,y,r,err) \
- OP_INT_ADD(x,y,r,err); \
- if ((r^(x)) >= 0 || (r^(y)) >= 0); \
- else FAIL_OVF(err, "integer addition")
- '''
- r = x + y
- if r^x >= 0 or r^y >= 0:
- return r
- else:
- raise OverflowError("integer addition")
-
-def _int_add_nonneg_ovf(x, y):
- '''
- OP_INT_ADD(x,y,r); \
- if (r >= (x)); \
- else FAIL_OVF("integer addition")
- '''
- r = x + y
- if r >= x:
- return r
- else:
- raise OverflowError("integer addition")
-
-def _int_sub_ovf(x, y):
- '''#define OP_INT_SUB_OVF(x,y,r,err) \
- OP_INT_SUB(x,y,r,err); \
- if ((r^(x)) >= 0 || (r^~(y)) >= 0); \
- else FAIL_OVF(err, "integer subtraction")
- '''
- r = x - y
- if r^x >= 0 or r^~y >= 0:
- return r
- else:
- raise OverflowError("integer subtraction")
-
-def int_lshift_ovf(x, y):
- '''#define OP_INT_LSHIFT_OVF(x,y,r,err) \
- OP_INT_LSHIFT(x,y,r,err); \
- if ((x) != Py_ARITHMETIC_RIGHT_SHIFT(long, r, (y))) \
- FAIL_OVF(err, "x<<y losing bits or changing sign")
- '''
- r = x << y
- if x != _Py_ARITHMETIC_RIGHT_SHIFT(r, y):
- raise OverflowError("x<<y losing bits or changing sign")
- else:
- return r
-
-def int_rshift_val(x, y):
- '''#define OP_INT_RSHIFT_VAL(x,y,r,err) \
- if ((y) >= 0) { OP_INT_RSHIFT(x,y,r,err); } \
- else FAIL_VAL(err, "negative shift count")
- '''
- if y >= 0:
- return _Py_ARITHMETIC_RIGHT_SHIFT(x, y)
- else:
- raise ValueError("negative shift count")
-
-def int_lshift_val(x, y):
- '''#define OP_INT_LSHIFT_VAL(x,y,r,err) \
- if ((y) >= 0) { OP_INT_LSHIFT(x,y,r,err); } \
- else FAIL_VAL(err, "negative shift count")
- '''
- if y >= 0:
- return x << y
- else:
- raise ValueError("negative shift count")
-
-def int_lshift_ovf_val(x, y):
- '''#define OP_INT_LSHIFT_OVF_VAL(x,y,r,err) \
- if ((y) >= 0) { OP_INT_LSHIFT_OVF(x,y,r,err); } \
- else FAIL_VAL(err, "negative shift count")
- '''
- if y >= 0:
- return int_lshift_ovf(x, y)
- else:
- raise ValueError("negative shift count")
-
-def int_floordiv_ovf(x, y):
- '''#define OP_INT_FLOORDIV_OVF(x,y,r,err) \
- if ((y) == -1 && (x) < 0 && ((unsigned long)(x) << 1) == 0) \
- FAIL_OVF(err, "integer division"); \
- OP_INT_FLOORDIV(x,y,r,err)
- '''
- if y == -1 and x < 0 and (r_uint(x) << 1) == 0:
- raise OverflowError("integer division")
- else:
- return llop.int_floordiv(Signed, x, y)
-
-def int_floordiv_ovf_zer(x, y):
- '''#define OP_INT_FLOORDIV_OVF_ZER(x,y,r,err) \
- if ((y)) { OP_INT_FLOORDIV_OVF(x,y,r,err); } \
- else FAIL_ZER(err, "integer division")
- '''
- if y:
- return int_floordiv_ovf(x, y)
- else:
- raise ZeroDivisionError("integer division")
-
-def int_mod_ovf(x, y):
- '''#define OP_INT_MOD_OVF(x,y,r,err) \
- if ((y) == -1 && (x) < 0 && ((unsigned long)(x) << 1) == 0) \
- FAIL_OVF(err, "integer modulo"); \
- OP_INT_MOD(x,y,r,err)
- '''
- if y == -1 and x < 0 and (r_uint(x) << 1) == 0:
- raise OverflowError("integer modulo")
- else:
- return llop.int_mod(Signed, x, y)
-
-def int_mod_zer(x, y):
- '''#define OP_INT_MOD_ZER(x,y,r,err) \
- if ((y)) { OP_INT_MOD(x,y,r,err); } \
- else FAIL_ZER(err, "integer modulo")
- '''
- if y:
- return llop.int_mod(Signed, x, y)
- else:
- raise ZeroDivisionError("integer modulo")
-
-def uint_mod_zer(x, y):
- '''#define OP_UINT_MOD_ZER(x,y,r,err) \
- if ((y)) { OP_UINT_MOD(x,y,r,err); } \
- else FAIL_ZER(err, "unsigned integer modulo")
- '''
- if y:
- return x % y
- else:
- raise ZeroDivisionError("unsigned integer modulo")
-
More information about the pypy-commit
mailing list