[pypy-commit] pypy stm-gc: hg merge default
arigo
noreply at buildbot.pypy.org
Thu Mar 22 16:33:02 CET 2012
Author: Armin Rigo <arigo at tunes.org>
Branch: stm-gc
Changeset: r53902:cc22c467e315
Date: 2012-03-22 15:57 +0100
http://bitbucket.org/pypy/pypy/changeset/cc22c467e315/
Log: hg merge default
diff too long, truncating to 10000 out of 21783 lines
diff --git a/lib-python/2.7/SimpleXMLRPCServer.py b/lib-python/2.7/SimpleXMLRPCServer.py
--- a/lib-python/2.7/SimpleXMLRPCServer.py
+++ b/lib-python/2.7/SimpleXMLRPCServer.py
@@ -486,7 +486,10 @@
L = []
while size_remaining:
chunk_size = min(size_remaining, max_chunk_size)
- L.append(self.rfile.read(chunk_size))
+ chunk = self.rfile.read(chunk_size)
+ if not chunk:
+ break
+ L.append(chunk)
size_remaining -= len(L[-1])
data = ''.join(L)
diff --git a/lib-python/2.7/test/test_xmlrpc.py b/lib-python/2.7/test/test_xmlrpc.py
--- a/lib-python/2.7/test/test_xmlrpc.py
+++ b/lib-python/2.7/test/test_xmlrpc.py
@@ -308,7 +308,7 @@
global ADDR, PORT, URL
ADDR, PORT = serv.socket.getsockname()
#connect to IP address directly. This avoids socket.create_connection()
- #trying to connect to to "localhost" using all address families, which
+ #trying to connect to "localhost" using all address families, which
#causes slowdown e.g. on vista which supports AF_INET6. The server listens
#on AF_INET only.
URL = "http://%s:%d"%(ADDR, PORT)
@@ -367,7 +367,7 @@
global ADDR, PORT, URL
ADDR, PORT = serv.socket.getsockname()
#connect to IP address directly. This avoids socket.create_connection()
- #trying to connect to to "localhost" using all address families, which
+ #trying to connect to "localhost" using all address families, which
#causes slowdown e.g. on vista which supports AF_INET6. The server listens
#on AF_INET only.
URL = "http://%s:%d"%(ADDR, PORT)
@@ -472,6 +472,9 @@
# protocol error; provide additional information in test output
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
+ def test_unicode_host(self):
+ server = xmlrpclib.ServerProxy(u"http://%s:%d/RPC2"%(ADDR, PORT))
+ self.assertEqual(server.add("a", u"\xe9"), u"a\xe9")
# [ch] The test 404 is causing lots of false alarms.
def XXXtest_404(self):
@@ -586,6 +589,12 @@
# This avoids waiting for the socket timeout.
self.test_simple1()
+ def test_partial_post(self):
+ # Check that a partial POST doesn't make the server loop: issue #14001.
+ conn = httplib.HTTPConnection(ADDR, PORT)
+ conn.request('POST', '/RPC2 HTTP/1.0\r\nContent-Length: 100\r\n\r\nbye')
+ conn.close()
+
class MultiPathServerTestCase(BaseServerTestCase):
threadFunc = staticmethod(http_multi_server)
request_count = 2
diff --git a/lib-python/conftest.py b/lib-python/conftest.py
--- a/lib-python/conftest.py
+++ b/lib-python/conftest.py
@@ -311,7 +311,7 @@
RegrTest('test_mimetypes.py'),
RegrTest('test_MimeWriter.py', core=False),
RegrTest('test_minidom.py'),
- RegrTest('test_mmap.py'),
+ RegrTest('test_mmap.py', usemodules="mmap"),
RegrTest('test_module.py', core=True),
RegrTest('test_modulefinder.py'),
RegrTest('test_msilib.py', skip=only_win32),
diff --git a/lib-python/modified-2.7/ctypes/test/test_arrays.py b/lib-python/modified-2.7/ctypes/test/test_arrays.py
--- a/lib-python/modified-2.7/ctypes/test/test_arrays.py
+++ b/lib-python/modified-2.7/ctypes/test/test_arrays.py
@@ -1,12 +1,23 @@
import unittest
from ctypes import *
+from test.test_support import impl_detail
formats = "bBhHiIlLqQfd"
+# c_longdouble commented out for PyPy, look at the commend in test_longdouble
formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \
- c_long, c_ulonglong, c_float, c_double, c_longdouble
+ c_long, c_ulonglong, c_float, c_double #, c_longdouble
class ArrayTestCase(unittest.TestCase):
+
+ @impl_detail('long double not supported by PyPy', pypy=False)
+ def test_longdouble(self):
+ """
+ This test is empty. It's just here to remind that we commented out
+ c_longdouble in "formats". If pypy will ever supports c_longdouble, we
+ should kill this test and uncomment c_longdouble inside formats.
+ """
+
def test_simple(self):
# create classes holding simple numeric types, and check
# various properties.
diff --git a/lib-python/modified-2.7/distutils/command/bdist_wininst.py b/lib-python/modified-2.7/distutils/command/bdist_wininst.py
--- a/lib-python/modified-2.7/distutils/command/bdist_wininst.py
+++ b/lib-python/modified-2.7/distutils/command/bdist_wininst.py
@@ -298,7 +298,8 @@
bitmaplen, # number of bytes in bitmap
)
file.write(header)
- file.write(open(arcname, "rb").read())
+ with open(arcname, "rb") as arcfile:
+ file.write(arcfile.read())
# create_exe()
diff --git a/lib-python/modified-2.7/distutils/sysconfig_pypy.py b/lib-python/modified-2.7/distutils/sysconfig_pypy.py
--- a/lib-python/modified-2.7/distutils/sysconfig_pypy.py
+++ b/lib-python/modified-2.7/distutils/sysconfig_pypy.py
@@ -60,6 +60,7 @@
g['EXE'] = ""
g['SO'] = _get_so_extension() or ".so"
g['SOABI'] = g['SO'].rsplit('.')[0]
+ g['LIBDIR'] = os.path.join(sys.prefix, 'lib')
global _config_vars
_config_vars = g
diff --git a/lib-python/modified-2.7/opcode.py b/lib-python/modified-2.7/opcode.py
--- a/lib-python/modified-2.7/opcode.py
+++ b/lib-python/modified-2.7/opcode.py
@@ -192,5 +192,6 @@
def_op('LOOKUP_METHOD', 201) # Index in name list
hasname.append(201)
def_op('CALL_METHOD', 202) # #args not including 'self'
+def_op('BUILD_LIST_FROM_ARG', 203)
del def_op, name_op, jrel_op, jabs_op
diff --git a/lib-python/modified-2.7/site.py b/lib-python/modified-2.7/site.py
--- a/lib-python/modified-2.7/site.py
+++ b/lib-python/modified-2.7/site.py
@@ -550,9 +550,18 @@
"'import usercustomize' failed; use -v for traceback"
+def import_builtin_stuff():
+ """PyPy specific: pre-import a few built-in modules, because
+ some programs actually rely on them to be in sys.modules :-("""
+ import exceptions
+ if 'zipimport' in sys.builtin_module_names:
+ import zipimport
+
+
def main():
global ENABLE_USER_SITE
+ import_builtin_stuff()
abs__file__()
known_paths = removeduppaths()
if (os.name == "posix" and sys.path and
diff --git a/lib-python/modified-2.7/test/test_dis.py b/lib-python/modified-2.7/test/test_dis.py
new file mode 100644
--- /dev/null
+++ b/lib-python/modified-2.7/test/test_dis.py
@@ -0,0 +1,150 @@
+# Minimal tests for dis module
+
+from test.test_support import run_unittest
+import unittest
+import sys
+import dis
+import StringIO
+
+
+def _f(a):
+ print a
+ return 1
+
+dis_f = """\
+ %-4d 0 LOAD_FAST 0 (a)
+ 3 PRINT_ITEM
+ 4 PRINT_NEWLINE
+
+ %-4d 5 LOAD_CONST 1 (1)
+ 8 RETURN_VALUE
+"""%(_f.func_code.co_firstlineno + 1,
+ _f.func_code.co_firstlineno + 2)
+
+
+def bug708901():
+ for res in range(1,
+ 10):
+ pass
+
+dis_bug708901 = """\
+ %-4d 0 SETUP_LOOP 23 (to 26)
+ 3 LOAD_GLOBAL 0 (range)
+ 6 LOAD_CONST 1 (1)
+
+ %-4d 9 LOAD_CONST 2 (10)
+ 12 CALL_FUNCTION 2
+ 15 GET_ITER
+ >> 16 FOR_ITER 6 (to 25)
+ 19 STORE_FAST 0 (res)
+
+ %-4d 22 JUMP_ABSOLUTE 16
+ >> 25 POP_BLOCK
+ >> 26 LOAD_CONST 0 (None)
+ 29 RETURN_VALUE
+"""%(bug708901.func_code.co_firstlineno + 1,
+ bug708901.func_code.co_firstlineno + 2,
+ bug708901.func_code.co_firstlineno + 3)
+
+
+def bug1333982(x=[]):
+ assert 0, ([s for s in x] +
+ 1)
+ pass
+
+dis_bug1333982 = """\
+ %-4d 0 LOAD_CONST 1 (0)
+ 3 POP_JUMP_IF_TRUE 38
+ 6 LOAD_GLOBAL 0 (AssertionError)
+ 9 LOAD_FAST 0 (x)
+ 12 BUILD_LIST_FROM_ARG 0
+ 15 GET_ITER
+ >> 16 FOR_ITER 12 (to 31)
+ 19 STORE_FAST 1 (s)
+ 22 LOAD_FAST 1 (s)
+ 25 LIST_APPEND 2
+ 28 JUMP_ABSOLUTE 16
+
+ %-4d >> 31 LOAD_CONST 2 (1)
+ 34 BINARY_ADD
+ 35 RAISE_VARARGS 2
+
+ %-4d >> 38 LOAD_CONST 0 (None)
+ 41 RETURN_VALUE
+"""%(bug1333982.func_code.co_firstlineno + 1,
+ bug1333982.func_code.co_firstlineno + 2,
+ bug1333982.func_code.co_firstlineno + 3)
+
+_BIG_LINENO_FORMAT = """\
+%3d 0 LOAD_GLOBAL 0 (spam)
+ 3 POP_TOP
+ 4 LOAD_CONST 0 (None)
+ 7 RETURN_VALUE
+"""
+
+class DisTests(unittest.TestCase):
+ def do_disassembly_test(self, func, expected):
+ s = StringIO.StringIO()
+ save_stdout = sys.stdout
+ sys.stdout = s
+ dis.dis(func)
+ sys.stdout = save_stdout
+ got = s.getvalue()
+ # Trim trailing blanks (if any).
+ lines = got.split('\n')
+ lines = [line.rstrip() for line in lines]
+ expected = expected.split("\n")
+ import difflib
+ if expected != lines:
+ self.fail(
+ "events did not match expectation:\n" +
+ "\n".join(difflib.ndiff(expected,
+ lines)))
+
+ def test_opmap(self):
+ self.assertEqual(dis.opmap["STOP_CODE"], 0)
+ self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
+ self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
+
+ def test_opname(self):
+ self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
+
+ def test_boundaries(self):
+ self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
+ self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
+
+ def test_dis(self):
+ self.do_disassembly_test(_f, dis_f)
+
+ def test_bug_708901(self):
+ self.do_disassembly_test(bug708901, dis_bug708901)
+
+ def test_bug_1333982(self):
+ # This one is checking bytecodes generated for an `assert` statement,
+ # so fails if the tests are run with -O. Skip this test then.
+ if __debug__:
+ self.do_disassembly_test(bug1333982, dis_bug1333982)
+
+ def test_big_linenos(self):
+ def func(count):
+ namespace = {}
+ func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
+ exec func in namespace
+ return namespace['foo']
+
+ # Test all small ranges
+ for i in xrange(1, 300):
+ expected = _BIG_LINENO_FORMAT % (i + 2)
+ self.do_disassembly_test(func(i), expected)
+
+ # Test some larger ranges too
+ for i in xrange(300, 5000, 10):
+ expected = _BIG_LINENO_FORMAT % (i + 2)
+ self.do_disassembly_test(func(i), expected)
+
+def test_main():
+ run_unittest(DisTests)
+
+
+if __name__ == "__main__":
+ test_main()
diff --git a/lib_pypy/_csv.py b/lib_pypy/_csv.py
--- a/lib_pypy/_csv.py
+++ b/lib_pypy/_csv.py
@@ -414,7 +414,7 @@
def _parse_add_char(self, c):
if len(self.field) + len(c) > _field_limit:
- raise Error("field larget than field limit (%d)" % (_field_limit))
+ raise Error("field larger than field limit (%d)" % (_field_limit))
self.field += c
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,9 +1,9 @@
-
+import _ffi
import _rawffi
from _ctypes.basics import _CData, cdata_from_address, _CDataMeta, sizeof
from _ctypes.basics import keepalive_key, store_reference, ensure_objects
-from _ctypes.basics import CArgObject
+from _ctypes.basics import CArgObject, as_ffi_pointer
class ArrayMeta(_CDataMeta):
def __new__(self, name, cls, typedict):
@@ -211,6 +211,9 @@
def _to_ffi_param(self):
return self._get_buffer_value()
+ def _as_ffi_pointer_(self, ffitype):
+ return as_ffi_pointer(self, ffitype)
+
ARRAY_CACHE = {}
def create_array_type(base, length):
@@ -228,5 +231,6 @@
_type_ = base
)
cls = ArrayMeta(name, (Array,), tpdict)
+ cls._ffiargtype = _ffi.types.Pointer(base.get_ffi_argtype())
ARRAY_CACHE[key] = cls
return cls
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
@@ -230,5 +230,16 @@
}
+# called from primitive.py, pointer.py, array.py
+def as_ffi_pointer(value, ffitype):
+ my_ffitype = type(value).get_ffi_argtype()
+ # for now, we always allow types.pointer, else a lot of tests
+ # break. We need to rethink how pointers are represented, though
+ if my_ffitype is not ffitype and ffitype is not _ffi.types.void_p:
+ raise ArgumentError("expected %s instance, got %s" % (type(value),
+ ffitype))
+ return value._get_buffer_value()
+
+
# used by "byref"
from _ctypes.pointer import pointer
diff --git a/lib_pypy/_ctypes/builtin.py b/lib_pypy/_ctypes/builtin.py
--- a/lib_pypy/_ctypes/builtin.py
+++ b/lib_pypy/_ctypes/builtin.py
@@ -31,24 +31,20 @@
arg = cobj._get_buffer_value()
return _rawffi.wcharp2rawunicode(arg, lgt)
-class ErrorObject(local):
- def __init__(self):
- self.errno = 0
- self.winerror = 0
-_error_object = ErrorObject()
+_err = local()
def get_errno():
- return _error_object.errno
+ return getattr(_err, "errno", 0)
def set_errno(errno):
- old_errno = _error_object.errno
- _error_object.errno = errno
+ old_errno = get_errno()
+ _err.errno = errno
return old_errno
def get_last_error():
- return _error_object.winerror
+ return getattr(_err, "winerror", 0)
def set_last_error(winerror):
- old_winerror = _error_object.winerror
- _error_object.winerror = winerror
+ old_winerror = get_last_error()
+ _err.winerror = winerror
return old_winerror
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
@@ -3,7 +3,7 @@
from _ctypes.primitive import SimpleType, _SimpleCData
from _ctypes.basics import ArgumentError, keepalive_key
from _ctypes.basics import is_struct_shape
-from _ctypes.builtin import set_errno, set_last_error
+from _ctypes.builtin import get_errno, set_errno, get_last_error, set_last_error
import _rawffi
import _ffi
import sys
@@ -350,16 +350,24 @@
def _call_funcptr(self, funcptr, *newargs):
if self._flags_ & _rawffi.FUNCFLAG_USE_ERRNO:
- set_errno(_rawffi.get_errno())
+ tmp = _rawffi.get_errno()
+ _rawffi.set_errno(get_errno())
+ set_errno(tmp)
if self._flags_ & _rawffi.FUNCFLAG_USE_LASTERROR:
- set_last_error(_rawffi.get_last_error())
+ tmp = _rawffi.get_last_error()
+ _rawffi.set_last_error(get_last_error())
+ set_last_error(tmp)
try:
result = funcptr(*newargs)
finally:
if self._flags_ & _rawffi.FUNCFLAG_USE_ERRNO:
- set_errno(_rawffi.get_errno())
+ tmp = _rawffi.get_errno()
+ _rawffi.set_errno(get_errno())
+ set_errno(tmp)
if self._flags_ & _rawffi.FUNCFLAG_USE_LASTERROR:
- set_last_error(_rawffi.get_last_error())
+ tmp = _rawffi.get_last_error()
+ _rawffi.set_last_error(get_last_error())
+ set_last_error(tmp)
#
try:
return self._build_result(self._restype_, result, newargs)
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
@@ -3,7 +3,7 @@
import _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
+from _ctypes.basics import sizeof, byref, as_ffi_pointer
from _ctypes.array import Array, array_get_slice_params, array_slice_getitem,\
array_slice_setitem
@@ -119,14 +119,6 @@
def _as_ffi_pointer_(self, ffitype):
return as_ffi_pointer(self, ffitype)
-def as_ffi_pointer(value, ffitype):
- my_ffitype = type(value).get_ffi_argtype()
- # for now, we always allow types.pointer, else a lot of tests
- # break. We need to rethink how pointers are represented, though
- if my_ffitype is not ffitype and ffitype is not _ffi.types.void_p:
- raise ArgumentError("expected %s instance, got %s" % (type(value),
- ffitype))
- return value._get_buffer_value()
def _cast_addr(obj, _, tp):
if not (isinstance(tp, _CDataMeta) and tp._is_pointer_like()):
diff --git a/lib_pypy/cPickle.py b/lib_pypy/cPickle.py
--- a/lib_pypy/cPickle.py
+++ b/lib_pypy/cPickle.py
@@ -2,16 +2,95 @@
# One-liner implementation of cPickle
#
-from pickle import *
+from pickle import Pickler, dump, dumps, PickleError, PicklingError, UnpicklingError, _EmptyClass
from pickle import __doc__, __version__, format_version, compatible_formats
+from types import *
+from copy_reg import dispatch_table
+from copy_reg import _extension_registry, _inverted_registry, _extension_cache
+import marshal, struct, sys
try: from __pypy__ import builtinify
except ImportError: builtinify = lambda f: f
+# These are purely informational; no code uses these.
+format_version = "2.0" # File format version we write
+compatible_formats = ["1.0", # Original protocol 0
+ "1.1", # Protocol 0 with INST added
+ "1.2", # Original protocol 1
+ "1.3", # Protocol 1 with BINFLOAT added
+ "2.0", # Protocol 2
+ ] # Old format versions we can read
+
+# Keep in synch with cPickle. This is the highest protocol number we
+# know how to read.
+HIGHEST_PROTOCOL = 2
BadPickleGet = KeyError
UnpickleableError = PicklingError
+MARK = ord('(') # push special markobject on stack
+STOP = ord('.') # every pickle ends with STOP
+POP = ord('0') # discard topmost stack item
+POP_MARK = ord('1') # discard stack top through topmost markobject
+DUP = ord('2') # duplicate top stack item
+FLOAT = ord('F') # push float object; decimal string argument
+INT = ord('I') # push integer or bool; decimal string argument
+BININT = ord('J') # push four-byte signed int
+BININT1 = ord('K') # push 1-byte unsigned int
+LONG = ord('L') # push long; decimal string argument
+BININT2 = ord('M') # push 2-byte unsigned int
+NONE = ord('N') # push None
+PERSID = ord('P') # push persistent object; id is taken from string arg
+BINPERSID = ord('Q') # " " " ; " " " " stack
+REDUCE = ord('R') # apply callable to argtuple, both on stack
+STRING = ord('S') # push string; NL-terminated string argument
+BINSTRING = ord('T') # push string; counted binary string argument
+SHORT_BINSTRING = ord('U') # " " ; " " " " < 256 bytes
+UNICODE = ord('V') # push Unicode string; raw-unicode-escaped'd argument
+BINUNICODE = ord('X') # " " " ; counted UTF-8 string argument
+APPEND = ord('a') # append stack top to list below it
+BUILD = ord('b') # call __setstate__ or __dict__.update()
+GLOBAL = ord('c') # push self.find_class(modname, name); 2 string args
+DICT = ord('d') # build a dict from stack items
+EMPTY_DICT = ord('}') # push empty dict
+APPENDS = ord('e') # extend list on stack by topmost stack slice
+GET = ord('g') # push item from memo on stack; index is string arg
+BINGET = ord('h') # " " " " " " ; " " 1-byte arg
+INST = ord('i') # build & push class instance
+LONG_BINGET = ord('j') # push item from memo on stack; index is 4-byte arg
+LIST = ord('l') # build list from topmost stack items
+EMPTY_LIST = ord(']') # push empty list
+OBJ = ord('o') # build & push class instance
+PUT = ord('p') # store stack top in memo; index is string arg
+BINPUT = ord('q') # " " " " " ; " " 1-byte arg
+LONG_BINPUT = ord('r') # " " " " " ; " " 4-byte arg
+SETITEM = ord('s') # add key+value pair to dict
+TUPLE = ord('t') # build tuple from topmost stack items
+EMPTY_TUPLE = ord(')') # push empty tuple
+SETITEMS = ord('u') # modify dict by adding topmost key+value pairs
+BINFLOAT = ord('G') # push float; arg is 8-byte float encoding
+
+TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py
+FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py
+
+# Protocol 2
+
+PROTO = ord('\x80') # identify pickle protocol
+NEWOBJ = ord('\x81') # build object by applying cls.__new__ to argtuple
+EXT1 = ord('\x82') # push object from extension registry; 1-byte index
+EXT2 = ord('\x83') # ditto, but 2-byte index
+EXT4 = ord('\x84') # ditto, but 4-byte index
+TUPLE1 = ord('\x85') # build 1-tuple from stack top
+TUPLE2 = ord('\x86') # build 2-tuple from two topmost stack items
+TUPLE3 = ord('\x87') # build 3-tuple from three topmost stack items
+NEWTRUE = ord('\x88') # push True
+NEWFALSE = ord('\x89') # push False
+LONG1 = ord('\x8a') # push long from < 256 bytes
+LONG4 = ord('\x8b') # push really big long
+
+_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
+
+
# ____________________________________________________________
# XXX some temporary dark magic to produce pickled dumps that are
# closer to the ones produced by cPickle in CPython
@@ -44,3 +123,474 @@
file = StringIO()
Pickler(file, protocol).dump(obj)
return file.getvalue()
+
+# Why use struct.pack() for pickling but marshal.loads() for
+# unpickling? struct.pack() is 40% faster than marshal.dumps(), but
+# marshal.loads() is twice as fast as struct.unpack()!
+mloads = marshal.loads
+
+# Unpickling machinery
+
+class Unpickler(object):
+
+ def __init__(self, file):
+ """This takes a file-like object for reading a pickle data stream.
+
+ The protocol version of the pickle is detected automatically, so no
+ proto argument is needed.
+
+ The file-like object must have two methods, a read() method that
+ takes an integer argument, and a readline() method that requires no
+ arguments. Both methods should return a string. Thus file-like
+ object can be a file object opened for reading, a StringIO object,
+ or any other custom object that meets this interface.
+ """
+ self.readline = file.readline
+ self.read = file.read
+ self.memo = {}
+
+ def load(self):
+ """Read a pickled object representation from the open file.
+
+ Return the reconstituted object hierarchy specified in the file.
+ """
+ self.mark = object() # any new unique object
+ self.stack = []
+ self.append = self.stack.append
+ try:
+ key = ord(self.read(1))
+ while key != STOP:
+ self.dispatch[key](self)
+ key = ord(self.read(1))
+ except TypeError:
+ if self.read(1) == '':
+ raise EOFError
+ raise
+ return self.stack.pop()
+
+ # Return largest index k such that self.stack[k] is self.mark.
+ # If the stack doesn't contain a mark, eventually raises IndexError.
+ # This could be sped by maintaining another stack, of indices at which
+ # the mark appears. For that matter, the latter stack would suffice,
+ # and we wouldn't need to push mark objects on self.stack at all.
+ # Doing so is probably a good thing, though, since if the pickle is
+ # corrupt (or hostile) we may get a clue from finding self.mark embedded
+ # in unpickled objects.
+ def marker(self):
+ k = len(self.stack)-1
+ while self.stack[k] is not self.mark: k -= 1
+ return k
+
+ dispatch = {}
+
+ def load_proto(self):
+ proto = ord(self.read(1))
+ if not 0 <= proto <= 2:
+ raise ValueError, "unsupported pickle protocol: %d" % proto
+ dispatch[PROTO] = load_proto
+
+ def load_persid(self):
+ pid = self.readline()[:-1]
+ self.append(self.persistent_load(pid))
+ dispatch[PERSID] = load_persid
+
+ def load_binpersid(self):
+ pid = self.stack.pop()
+ self.append(self.persistent_load(pid))
+ dispatch[BINPERSID] = load_binpersid
+
+ def load_none(self):
+ self.append(None)
+ dispatch[NONE] = load_none
+
+ def load_false(self):
+ self.append(False)
+ dispatch[NEWFALSE] = load_false
+
+ def load_true(self):
+ self.append(True)
+ dispatch[NEWTRUE] = load_true
+
+ def load_int(self):
+ data = self.readline()
+ if data == FALSE[1:]:
+ val = False
+ elif data == TRUE[1:]:
+ val = True
+ else:
+ try:
+ val = int(data)
+ except ValueError:
+ val = long(data)
+ self.append(val)
+ dispatch[INT] = load_int
+
+ def load_binint(self):
+ self.append(mloads('i' + self.read(4)))
+ dispatch[BININT] = load_binint
+
+ def load_binint1(self):
+ self.append(ord(self.read(1)))
+ dispatch[BININT1] = load_binint1
+
+ def load_binint2(self):
+ self.append(mloads('i' + self.read(2) + '\000\000'))
+ dispatch[BININT2] = load_binint2
+
+ def load_long(self):
+ self.append(long(self.readline()[:-1], 0))
+ dispatch[LONG] = load_long
+
+ def load_long1(self):
+ n = ord(self.read(1))
+ bytes = self.read(n)
+ self.append(decode_long(bytes))
+ dispatch[LONG1] = load_long1
+
+ def load_long4(self):
+ n = mloads('i' + self.read(4))
+ bytes = self.read(n)
+ self.append(decode_long(bytes))
+ dispatch[LONG4] = load_long4
+
+ def load_float(self):
+ self.append(float(self.readline()[:-1]))
+ dispatch[FLOAT] = load_float
+
+ def load_binfloat(self, unpack=struct.unpack):
+ self.append(unpack('>d', self.read(8))[0])
+ dispatch[BINFLOAT] = load_binfloat
+
+ def load_string(self):
+ rep = self.readline()
+ if len(rep) < 3:
+ raise ValueError, "insecure string pickle"
+ if rep[0] == "'" == rep[-2]:
+ rep = rep[1:-2]
+ elif rep[0] == '"' == rep[-2]:
+ rep = rep[1:-2]
+ else:
+ raise ValueError, "insecure string pickle"
+ self.append(rep.decode("string-escape"))
+ dispatch[STRING] = load_string
+
+ def load_binstring(self):
+ L = mloads('i' + self.read(4))
+ self.append(self.read(L))
+ dispatch[BINSTRING] = load_binstring
+
+ def load_unicode(self):
+ self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
+ dispatch[UNICODE] = load_unicode
+
+ def load_binunicode(self):
+ L = mloads('i' + self.read(4))
+ self.append(unicode(self.read(L),'utf-8'))
+ dispatch[BINUNICODE] = load_binunicode
+
+ def load_short_binstring(self):
+ L = ord(self.read(1))
+ self.append(self.read(L))
+ dispatch[SHORT_BINSTRING] = load_short_binstring
+
+ def load_tuple(self):
+ k = self.marker()
+ self.stack[k:] = [tuple(self.stack[k+1:])]
+ dispatch[TUPLE] = load_tuple
+
+ def load_empty_tuple(self):
+ self.stack.append(())
+ dispatch[EMPTY_TUPLE] = load_empty_tuple
+
+ def load_tuple1(self):
+ self.stack[-1] = (self.stack[-1],)
+ dispatch[TUPLE1] = load_tuple1
+
+ def load_tuple2(self):
+ self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
+ dispatch[TUPLE2] = load_tuple2
+
+ def load_tuple3(self):
+ self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
+ dispatch[TUPLE3] = load_tuple3
+
+ def load_empty_list(self):
+ self.stack.append([])
+ dispatch[EMPTY_LIST] = load_empty_list
+
+ def load_empty_dictionary(self):
+ self.stack.append({})
+ dispatch[EMPTY_DICT] = load_empty_dictionary
+
+ def load_list(self):
+ k = self.marker()
+ self.stack[k:] = [self.stack[k+1:]]
+ dispatch[LIST] = load_list
+
+ def load_dict(self):
+ k = self.marker()
+ d = {}
+ items = self.stack[k+1:]
+ for i in range(0, len(items), 2):
+ key = items[i]
+ value = items[i+1]
+ d[key] = value
+ self.stack[k:] = [d]
+ dispatch[DICT] = load_dict
+
+ # INST and OBJ differ only in how they get a class object. It's not
+ # only sensible to do the rest in a common routine, the two routines
+ # previously diverged and grew different bugs.
+ # klass is the class to instantiate, and k points to the topmost mark
+ # object, following which are the arguments for klass.__init__.
+ def _instantiate(self, klass, k):
+ args = tuple(self.stack[k+1:])
+ del self.stack[k:]
+ instantiated = 0
+ if (not args and
+ type(klass) is ClassType and
+ not hasattr(klass, "__getinitargs__")):
+ try:
+ value = _EmptyClass()
+ value.__class__ = klass
+ instantiated = 1
+ except RuntimeError:
+ # In restricted execution, assignment to inst.__class__ is
+ # prohibited
+ pass
+ if not instantiated:
+ try:
+ value = klass(*args)
+ except TypeError, err:
+ raise TypeError, "in constructor for %s: %s" % (
+ klass.__name__, str(err)), sys.exc_info()[2]
+ self.append(value)
+
+ def load_inst(self):
+ module = self.readline()[:-1]
+ name = self.readline()[:-1]
+ klass = self.find_class(module, name)
+ self._instantiate(klass, self.marker())
+ dispatch[INST] = load_inst
+
+ def load_obj(self):
+ # Stack is ... markobject classobject arg1 arg2 ...
+ k = self.marker()
+ klass = self.stack.pop(k+1)
+ self._instantiate(klass, k)
+ dispatch[OBJ] = load_obj
+
+ def load_newobj(self):
+ args = self.stack.pop()
+ cls = self.stack[-1]
+ obj = cls.__new__(cls, *args)
+ self.stack[-1] = obj
+ dispatch[NEWOBJ] = load_newobj
+
+ def load_global(self):
+ module = self.readline()[:-1]
+ name = self.readline()[:-1]
+ klass = self.find_class(module, name)
+ self.append(klass)
+ dispatch[GLOBAL] = load_global
+
+ def load_ext1(self):
+ code = ord(self.read(1))
+ self.get_extension(code)
+ dispatch[EXT1] = load_ext1
+
+ def load_ext2(self):
+ code = mloads('i' + self.read(2) + '\000\000')
+ self.get_extension(code)
+ dispatch[EXT2] = load_ext2
+
+ def load_ext4(self):
+ code = mloads('i' + self.read(4))
+ self.get_extension(code)
+ dispatch[EXT4] = load_ext4
+
+ def get_extension(self, code):
+ nil = []
+ obj = _extension_cache.get(code, nil)
+ if obj is not nil:
+ self.append(obj)
+ return
+ key = _inverted_registry.get(code)
+ if not key:
+ raise ValueError("unregistered extension code %d" % code)
+ obj = self.find_class(*key)
+ _extension_cache[code] = obj
+ self.append(obj)
+
+ def find_class(self, module, name):
+ # Subclasses may override this
+ __import__(module)
+ mod = sys.modules[module]
+ klass = getattr(mod, name)
+ return klass
+
+ def load_reduce(self):
+ args = self.stack.pop()
+ func = self.stack[-1]
+ value = self.stack[-1](*args)
+ self.stack[-1] = value
+ dispatch[REDUCE] = load_reduce
+
+ def load_pop(self):
+ del self.stack[-1]
+ dispatch[POP] = load_pop
+
+ def load_pop_mark(self):
+ k = self.marker()
+ del self.stack[k:]
+ dispatch[POP_MARK] = load_pop_mark
+
+ def load_dup(self):
+ self.append(self.stack[-1])
+ dispatch[DUP] = load_dup
+
+ def load_get(self):
+ self.append(self.memo[self.readline()[:-1]])
+ dispatch[GET] = load_get
+
+ def load_binget(self):
+ i = ord(self.read(1))
+ self.append(self.memo[repr(i)])
+ dispatch[BINGET] = load_binget
+
+ def load_long_binget(self):
+ i = mloads('i' + self.read(4))
+ self.append(self.memo[repr(i)])
+ dispatch[LONG_BINGET] = load_long_binget
+
+ def load_put(self):
+ self.memo[self.readline()[:-1]] = self.stack[-1]
+ dispatch[PUT] = load_put
+
+ def load_binput(self):
+ i = ord(self.read(1))
+ self.memo[repr(i)] = self.stack[-1]
+ dispatch[BINPUT] = load_binput
+
+ def load_long_binput(self):
+ i = mloads('i' + self.read(4))
+ self.memo[repr(i)] = self.stack[-1]
+ dispatch[LONG_BINPUT] = load_long_binput
+
+ def load_append(self):
+ value = self.stack.pop()
+ self.stack[-1].append(value)
+ dispatch[APPEND] = load_append
+
+ def load_appends(self):
+ stack = self.stack
+ mark = self.marker()
+ lst = stack[mark - 1]
+ lst.extend(stack[mark + 1:])
+ del stack[mark:]
+ dispatch[APPENDS] = load_appends
+
+ def load_setitem(self):
+ stack = self.stack
+ value = stack.pop()
+ key = stack.pop()
+ dict = stack[-1]
+ dict[key] = value
+ dispatch[SETITEM] = load_setitem
+
+ def load_setitems(self):
+ stack = self.stack
+ mark = self.marker()
+ dict = stack[mark - 1]
+ for i in range(mark + 1, len(stack), 2):
+ dict[stack[i]] = stack[i + 1]
+
+ del stack[mark:]
+ dispatch[SETITEMS] = load_setitems
+
+ def load_build(self):
+ stack = self.stack
+ state = stack.pop()
+ inst = stack[-1]
+ setstate = getattr(inst, "__setstate__", None)
+ if setstate:
+ setstate(state)
+ return
+ slotstate = None
+ if isinstance(state, tuple) and len(state) == 2:
+ state, slotstate = state
+ if state:
+ try:
+ d = inst.__dict__
+ try:
+ for k, v in state.iteritems():
+ d[intern(k)] = v
+ # keys in state don't have to be strings
+ # don't blow up, but don't go out of our way
+ except TypeError:
+ d.update(state)
+
+ except RuntimeError:
+ # XXX In restricted execution, the instance's __dict__
+ # is not accessible. Use the old way of unpickling
+ # the instance variables. This is a semantic
+ # difference when unpickling in restricted
+ # vs. unrestricted modes.
+ # Note, however, that cPickle has never tried to do the
+ # .update() business, and always uses
+ # PyObject_SetItem(inst.__dict__, key, value) in a
+ # loop over state.items().
+ for k, v in state.items():
+ setattr(inst, k, v)
+ if slotstate:
+ for k, v in slotstate.items():
+ setattr(inst, k, v)
+ dispatch[BUILD] = load_build
+
+ def load_mark(self):
+ self.append(self.mark)
+ dispatch[MARK] = load_mark
+
+#from pickle import decode_long
+
+def decode_long(data):
+ r"""Decode a long from a two's complement little-endian binary string.
+
+ >>> decode_long('')
+ 0L
+ >>> decode_long("\xff\x00")
+ 255L
+ >>> decode_long("\xff\x7f")
+ 32767L
+ >>> decode_long("\x00\xff")
+ -256L
+ >>> decode_long("\x00\x80")
+ -32768L
+ >>> decode_long("\x80")
+ -128L
+ >>> decode_long("\x7f")
+ 127L
+ """
+
+ nbytes = len(data)
+ if nbytes == 0:
+ return 0L
+ ind = nbytes - 1
+ while ind and ord(data[ind]) == 0:
+ ind -= 1
+ n = ord(data[ind])
+ while ind:
+ n <<= 8
+ ind -= 1
+ if ord(data[ind]):
+ n += ord(data[ind])
+ if ord(data[nbytes - 1]) >= 128:
+ n -= 1L << (nbytes << 3)
+ return n
+
+def load(f):
+ return Unpickler(f).load()
+
+def loads(str):
+ f = StringIO(str)
+ return Unpickler(f).load()
diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -1032,8 +1032,8 @@
def __setstate(self, string):
if len(string) != 4 or not (1 <= ord(string[2]) <= 12):
raise TypeError("not enough arguments")
- yhi, ylo, self._month, self._day = map(ord, string)
- self._year = yhi * 256 + ylo
+ self._month, self._day = ord(string[2]), ord(string[3])
+ self._year = ord(string[0]) * 256 + ord(string[1])
def __reduce__(self):
return (self.__class__, self._getstate())
@@ -1421,9 +1421,10 @@
def __setstate(self, string, tzinfo):
if len(string) != 6 or ord(string[0]) >= 24:
raise TypeError("an integer is required")
- self._hour, self._minute, self._second, us1, us2, us3 = \
- map(ord, string)
- self._microsecond = (((us1 << 8) | us2) << 8) | us3
+ self._hour, self._minute, self._second = ord(string[0]), \
+ ord(string[1]), ord(string[2])
+ self._microsecond = (((ord(string[3]) << 8) | \
+ ord(string[4])) << 8) | ord(string[5])
self._tzinfo = tzinfo
def __reduce__(self):
@@ -1903,10 +1904,11 @@
return (basestate, self._tzinfo)
def __setstate(self, string, tzinfo):
- (yhi, ylo, self._month, self._day, self._hour,
- self._minute, self._second, us1, us2, us3) = map(ord, string)
- self._year = yhi * 256 + ylo
- self._microsecond = (((us1 << 8) | us2) << 8) | us3
+ (self._month, self._day, self._hour, self._minute,
+ self._second) = (ord(string[2]), ord(string[3]), ord(string[4]),
+ ord(string[5]), ord(string[6]))
+ self._year = ord(string[0]) * 256 + ord(string[1])
+ self._microsecond = (((ord(string[7]) << 8) | ord(string[8])) << 8) | ord(string[9])
self._tzinfo = tzinfo
def __reduce__(self):
diff --git a/lib_pypy/numpypy/core/numeric.py b/lib_pypy/numpypy/core/numeric.py
--- a/lib_pypy/numpypy/core/numeric.py
+++ b/lib_pypy/numpypy/core/numeric.py
@@ -6,7 +6,7 @@
import _numpypy as multiarray # ARGH
from numpypy.core.arrayprint import array2string
-
+newaxis = None
def asanyarray(a, dtype=None, order=None, maskna=None, ownmaskna=False):
"""
@@ -319,4 +319,4 @@
False_ = bool_(False)
True_ = bool_(True)
e = math.e
-pi = math.pi
\ No newline at end of file
+pi = math.pi
diff --git a/lib_pypy/pypy_test/test_site_extra.py b/lib_pypy/pypy_test/test_site_extra.py
new file mode 100644
--- /dev/null
+++ b/lib_pypy/pypy_test/test_site_extra.py
@@ -0,0 +1,13 @@
+import sys, os
+
+
+def test_preimported_modules():
+ lst = ['__builtin__', '_codecs', '_warnings', 'codecs', 'encodings',
+ 'exceptions', 'signal', 'sys', 'zipimport']
+ g = os.popen("'%s' -c 'import sys; print sorted(sys.modules)'" %
+ (sys.executable,))
+ real_data = g.read()
+ g.close()
+ for name in lst:
+ quoted_name = repr(name)
+ assert quoted_name in real_data
diff --git a/pypy/__init__.py b/pypy/__init__.py
--- a/pypy/__init__.py
+++ b/pypy/__init__.py
@@ -1,1 +1,16 @@
# Empty
+
+# XXX Should be empty again, soon.
+# XXX hack for win64:
+# This patch must stay here until the END OF STAGE 1
+# When all tests work, this branch will be merged
+# and the branch stage 2 is started, where we remove this patch.
+import sys
+if hasattr(sys, "maxsize"):
+ if sys.maxint != sys.maxsize:
+ sys.maxint = sys.maxsize
+ import warnings
+ warnings.warn("""\n
+---> This win64 port is now in stage 1: sys.maxint was modified.
+---> When pypy/__init__.py becomes empty again, we have reached stage 2.
+""")
diff --git a/pypy/annotation/builtin.py b/pypy/annotation/builtin.py
--- a/pypy/annotation/builtin.py
+++ b/pypy/annotation/builtin.py
@@ -37,7 +37,11 @@
try:
realresult = func(*args)
except (ValueError, OverflowError):
- return s_ImpossibleValue # no possible answer for this precise input
+ # no possible answer for this precise input. Be conservative
+ # and keep the computation non-constant. Example:
+ # unichr(constant-that-doesn't-fit-16-bits) on platforms where
+ # the underlying Python has sys.maxunicode == 0xffff.
+ return s_result
s_realresult = immutablevalue(realresult)
if not s_result.contains(s_realresult):
raise Exception("%s%r returned %r, which is not contained in %s" % (
@@ -163,7 +167,7 @@
r.const = False
return r
- assert not issubclass(typ, (int,long)) or typ in (bool, int), (
+ assert not issubclass(typ, (int, long)) or typ in (bool, int, long), (
"for integers only isinstance(.,int|r_uint) are supported")
if s_obj.is_constant():
@@ -297,7 +301,7 @@
def robjmodel_instantiate(s_clspbc):
assert isinstance(s_clspbc, SomePBC)
clsdef = None
- more_than_one = len(s_clspbc.descriptions)
+ more_than_one = len(s_clspbc.descriptions) > 1
for desc in s_clspbc.descriptions:
cdef = desc.getuniqueclassdef()
if more_than_one:
diff --git a/pypy/annotation/classdef.py b/pypy/annotation/classdef.py
--- a/pypy/annotation/classdef.py
+++ b/pypy/annotation/classdef.py
@@ -134,13 +134,19 @@
if self.name not in homedef.classdesc.all_enforced_attrs:
self.attr_allowed = False
if not self.readonly:
- raise NoSuchAttrError(homedef, self.name)
+ raise NoSuchAttrError(
+ "setting forbidden attribute %r on %r" % (
+ self.name, homedef))
def modified(self, classdef='?'):
self.readonly = False
if not self.attr_allowed:
- raise NoSuchAttrError(classdef, self.name)
-
+ raise NoSuchAttrError(
+ "Attribute %r on %r should be read-only.\n" % (self.name,
+ classdef) +
+ "This error can be caused by another 'getattr' that promoted\n"
+ "the attribute here; the list of read locations is:\n" +
+ '\n'.join([str(loc[0]) for loc in self.read_locations]))
class ClassDef(object):
"Wraps a user class."
diff --git a/pypy/annotation/description.py b/pypy/annotation/description.py
--- a/pypy/annotation/description.py
+++ b/pypy/annotation/description.py
@@ -398,7 +398,6 @@
cls = pyobj
base = object
baselist = list(cls.__bases__)
- baselist.reverse()
# special case: skip BaseException in Python 2.5, and pretend
# that all exceptions ultimately inherit from Exception instead
@@ -408,17 +407,27 @@
elif baselist == [py.builtin.BaseException]:
baselist = [Exception]
+ mixins_before = []
+ mixins_after = []
for b1 in baselist:
if b1 is object:
continue
if b1.__dict__.get('_mixin_', False):
- self.add_mixin(b1)
+ if base is object:
+ mixins_before.append(b1)
+ else:
+ mixins_after.append(b1)
else:
assert base is object, ("multiple inheritance only supported "
"with _mixin_: %r" % (cls,))
base = b1
+ if mixins_before and mixins_after:
+ raise Exception("unsupported: class %r has mixin bases both"
+ " before and after the regular base" % (self,))
+ self.add_mixins(mixins_after, check_not_in=base)
+ self.add_mixins(mixins_before)
+ self.add_sources_for_class(cls)
- self.add_sources_for_class(cls)
if base is not object:
self.basedesc = bookkeeper.getdesc(base)
@@ -480,14 +489,30 @@
return
self.classdict[name] = Constant(value)
- def add_mixin(self, base):
- for subbase in base.__bases__:
- if subbase is object:
- continue
- assert subbase.__dict__.get("_mixin_", False), ("Mixin class %r has non"
- "mixin base class %r" % (base, subbase))
- self.add_mixin(subbase)
- self.add_sources_for_class(base, mixin=True)
+ def add_mixins(self, mixins, check_not_in=object):
+ if not mixins:
+ return
+ A = type('tmp', tuple(mixins) + (object,), {})
+ mro = A.__mro__
+ assert mro[0] is A and mro[-1] is object
+ mro = mro[1:-1]
+ #
+ skip = set()
+ def add(cls):
+ if cls is not object:
+ for base in cls.__bases__:
+ add(base)
+ for name in cls.__dict__:
+ skip.add(name)
+ add(check_not_in)
+ #
+ for base in reversed(mro):
+ assert base.__dict__.get("_mixin_", False), ("Mixin class %r has non"
+ "mixin base class %r" % (mixins, base))
+ for name, value in base.__dict__.items():
+ if name in skip:
+ continue
+ self.add_source_attribute(name, value, mixin=True)
def add_sources_for_class(self, cls, mixin=False):
for name, value in cls.__dict__.items():
diff --git a/pypy/annotation/model.py b/pypy/annotation/model.py
--- a/pypy/annotation/model.py
+++ b/pypy/annotation/model.py
@@ -786,12 +786,15 @@
#
# safety check that no-one is trying to make annotation and translation
# faster by providing the -O option to Python.
-try:
- assert False
-except AssertionError:
- pass # fine
-else:
- raise RuntimeError("The annotator relies on 'assert' statements from the\n"
+import os
+if "WINGDB_PYTHON" not in os.environ:
+ # ...but avoiding this boring check in the IDE
+ try:
+ assert False
+ except AssertionError:
+ pass # fine
+ else:
+ raise RuntimeError("The annotator relies on 'assert' statements from the\n"
"\tannotated program: you cannot run it with 'python -O'.")
# this has the side-effect of registering the unary and binary operations
diff --git a/pypy/annotation/test/test_annrpython.py b/pypy/annotation/test/test_annrpython.py
--- a/pypy/annotation/test/test_annrpython.py
+++ b/pypy/annotation/test/test_annrpython.py
@@ -1,15 +1,12 @@
from __future__ import with_statement
-import autopath
import py.test
import sys
from pypy import conftest
-from pypy.tool.udir import udir
from pypy.annotation import model as annmodel
from pypy.annotation.annrpython import RPythonAnnotator as _RPythonAnnotator
from pypy.translator.translator import graphof as tgraphof
from pypy.annotation import policy
-from pypy.annotation import specialize
from pypy.annotation.listdef import ListDef, ListChangeUnallowed
from pypy.annotation.dictdef import DictDef
from pypy.objspace.flow.model import *
@@ -2431,6 +2428,93 @@
assert isinstance(s.items[1], annmodel.SomeChar)
assert isinstance(s.items[2], annmodel.SomeChar)
+ def test_mixin_first(self):
+ class Mixin(object):
+ _mixin_ = True
+ def foo(self): return 4
+ class Base(object):
+ def foo(self): return 5
+ class Concrete(Mixin, Base):
+ pass
+ def f():
+ return Concrete().foo()
+
+ assert f() == 4
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert s.const == 4
+
+ def test_mixin_last(self):
+ class Mixin(object):
+ _mixin_ = True
+ def foo(self): return 4
+ class Base(object):
+ def foo(self): return 5
+ class Concrete(Base, Mixin):
+ pass
+ def f():
+ return Concrete().foo()
+
+ assert f() == 5
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert s.const == 5
+
+ def test_mixin_concrete(self):
+ class Mixin(object):
+ _mixin_ = True
+ def foo(self): return 4
+ class Concrete(Mixin):
+ def foo(self): return 5
+ def f():
+ return Concrete().foo()
+
+ assert f() == 5
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert s.const == 5
+
+ def test_multiple_mixins_mro(self):
+ # an obscure situation, but it occurred in module/micronumpy/types.py
+ class A(object):
+ _mixin_ = True
+ def foo(self): return 1
+ class B(A):
+ _mixin_ = True
+ def foo(self): return 2
+ class C(A):
+ _mixin_ = True
+ class D(B, C):
+ _mixin_ = True
+ class Concrete(D):
+ pass
+ def f():
+ return Concrete().foo()
+
+ assert f() == 2
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert s.const == 2
+
+ def test_multiple_mixins_mro_2(self):
+ class A(object):
+ _mixin_ = True
+ def foo(self): return 1
+ class B(A):
+ _mixin_ = True
+ def foo(self): return 2
+ class C(A):
+ _mixin_ = True
+ class Concrete(C, B):
+ pass
+ def f():
+ return Concrete().foo()
+
+ assert f() == 2
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert s.const == 2
+
def test___class___attribute(self):
class Base(object): pass
class A(Base): pass
@@ -2469,6 +2553,26 @@
s = a.build_types(f, [int])
assert s.knowntype == int
+ def test_slots_reads(self):
+ class A(object):
+ __slots__ = ()
+ class B(A):
+ def __init__(self, x):
+ self.x = x
+ def f(x):
+ if x:
+ a = A()
+ else:
+ a = B(x)
+ return a.x # should explode here
+
+ a = self.RPythonAnnotator()
+ e = py.test.raises(Exception, a.build_types, f, [int])
+ # this should explode on reading the attribute 'a.x', but it can
+ # sometimes explode on 'self.x = x', which does not make much sense.
+ # But it looks hard to fix in general: we don't know yet during 'a.x'
+ # if the attribute x will be read-only or read-write.
+
def test_unboxed_value(self):
class A(object):
__slots__ = ()
diff --git a/pypy/bin/rpython b/pypy/bin/rpython
new file mode 100755
--- /dev/null
+++ b/pypy/bin/rpython
@@ -0,0 +1,18 @@
+#!/usr/bin/env pypy
+
+"""RPython translation usage:
+
+rpython <translation options> target <targetoptions>
+
+run with --help for more information
+"""
+
+import sys
+from pypy.translator.goal.translate import main
+
+# no implicit targets
+if len(sys.argv) == 1:
+ print __doc__
+ sys.exit(1)
+
+main()
diff --git a/pypy/config/pypyoption.py b/pypy/config/pypyoption.py
--- a/pypy/config/pypyoption.py
+++ b/pypy/config/pypyoption.py
@@ -13,7 +13,7 @@
and not p.basename.startswith('test')]
essential_modules = dict.fromkeys(
- ["exceptions", "_file", "sys", "__builtin__", "posix"]
+ ["exceptions", "_file", "sys", "__builtin__", "posix", "_warnings"]
)
default_modules = essential_modules.copy()
@@ -176,9 +176,6 @@
cmdline="--translationmodules",
suggests=[("objspace.allworkingmodules", False)]),
- BoolOption("geninterp", "specify whether geninterp should be used",
- default=False),
-
BoolOption("logbytecodes",
"keep track of bytecode usage",
default=False),
@@ -393,10 +390,6 @@
config.objspace.std.suggest(withsmalllong=True)
# xxx other options? ropes maybe?
- # completely disable geninterp in a level 0 translation
- if level == '0':
- config.objspace.suggest(geninterp=False)
-
# some optimizations have different effects depending on the typesystem
if type_system == 'ootype':
config.objspace.std.suggest(multimethods="doubledispatch")
diff --git a/pypy/config/translationoption.py b/pypy/config/translationoption.py
--- a/pypy/config/translationoption.py
+++ b/pypy/config/translationoption.py
@@ -188,11 +188,6 @@
# Flags of the TranslationContext:
BoolOption("simplifying", "Simplify flow graphs", default=True),
- BoolOption("builtins_can_raise_exceptions",
- "When true, assume any call to a 'simple' builtin such as "
- "'hex' can raise an arbitrary exception",
- default=False,
- cmdline=None),
BoolOption("list_comprehension_operations",
"When true, look for and special-case the sequence of "
"operations that results from a list comprehension and "
diff --git a/pypy/doc/cpython_differences.rst b/pypy/doc/cpython_differences.rst
--- a/pypy/doc/cpython_differences.rst
+++ b/pypy/doc/cpython_differences.rst
@@ -313,5 +313,10 @@
implementation detail that shows up because of internal C-level slots
that PyPy does not have.
+* the ``__dict__`` attribute of new-style classes returns a normal dict, as
+ opposed to a dict proxy like in CPython. Mutating the dict will change the
+ type and vice versa. For builtin types, a dictionary will be returned that
+ cannot be changed (but still looks and behaves like a normal dictionary).
+
.. include:: _ref.txt
diff --git a/pypy/interpreter/astcompiler/assemble.py b/pypy/interpreter/astcompiler/assemble.py
--- a/pypy/interpreter/astcompiler/assemble.py
+++ b/pypy/interpreter/astcompiler/assemble.py
@@ -610,6 +610,8 @@
ops.JUMP_IF_FALSE_OR_POP : 0,
ops.POP_JUMP_IF_TRUE : -1,
ops.POP_JUMP_IF_FALSE : -1,
+
+ ops.BUILD_LIST_FROM_ARG: 1,
}
diff --git a/pypy/interpreter/astcompiler/codegen.py b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -965,7 +965,7 @@
self.emit_op_arg(ops.CALL_METHOD, (kwarg_count << 8) | arg_count)
return True
- def _listcomp_generator(self, gens, gen_index, elt):
+ def _listcomp_generator(self, gens, gen_index, elt, single=False):
start = self.new_block()
skip = self.new_block()
if_cleanup = self.new_block()
@@ -973,6 +973,8 @@
gen = gens[gen_index]
assert isinstance(gen, ast.comprehension)
gen.iter.walkabout(self)
+ if single:
+ self.emit_op_arg(ops.BUILD_LIST_FROM_ARG, 0)
self.emit_op(ops.GET_ITER)
self.use_next_block(start)
self.emit_jump(ops.FOR_ITER, anchor)
@@ -998,8 +1000,12 @@
def visit_ListComp(self, lc):
self.update_position(lc.lineno)
- self.emit_op_arg(ops.BUILD_LIST, 0)
- self._listcomp_generator(lc.generators, 0, lc.elt)
+ if len(lc.generators) != 1 or lc.generators[0].ifs:
+ single = False
+ self.emit_op_arg(ops.BUILD_LIST, 0)
+ else:
+ single = True
+ self._listcomp_generator(lc.generators, 0, lc.elt, single=single)
def _comp_generator(self, node, generators, gen_index):
start = self.new_block()
diff --git a/pypy/interpreter/astcompiler/test/test_compiler.py b/pypy/interpreter/astcompiler/test/test_compiler.py
--- a/pypy/interpreter/astcompiler/test/test_compiler.py
+++ b/pypy/interpreter/astcompiler/test/test_compiler.py
@@ -58,7 +58,8 @@
w_res = pyco_expr.exec_host_bytecode(w_dict, w_dict)
res = space.str_w(space.repr(w_res))
if not isinstance(expected, float):
- assert res == repr(expected)
+ noL = lambda expr: expr.replace('L', '')
+ assert noL(res) == noL(repr(expected))
else:
# Float representation can vary a bit between interpreter
# versions, compare the numbers instead.
@@ -908,3 +909,17 @@
return d['f'](5)
""")
assert 'generator' in space.str_w(space.repr(w_generator))
+
+ def test_list_comprehension(self):
+ source = "def f(): [i for i in l]"
+ source2 = "def f(): [i for i in l for j in l]"
+ source3 = "def f(): [i for i in l if i]"
+ counts = self.count_instructions(source)
+ assert ops.BUILD_LIST not in counts
+ assert counts[ops.BUILD_LIST_FROM_ARG] == 1
+ counts = self.count_instructions(source2)
+ assert counts[ops.BUILD_LIST] == 1
+ assert ops.BUILD_LIST_FROM_ARG not in counts
+ counts = self.count_instructions(source3)
+ assert counts[ops.BUILD_LIST] == 1
+ assert ops.BUILD_LIST_FROM_ARG not in counts
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -7,7 +7,8 @@
from pypy.interpreter.miscutils import ThreadLocals
from pypy.tool.cache import Cache
from pypy.tool.uid import HUGEVAL_BYTES
-from pypy.rlib.objectmodel import we_are_translated, newlist, compute_unique_id
+from pypy.rlib.objectmodel import we_are_translated, newlist_hint,\
+ compute_unique_id
from pypy.rlib.debug import make_sure_not_resized
from pypy.rlib.timer import DummyTimer, Timer
from pypy.rlib.rarithmetic import r_uint
@@ -328,7 +329,7 @@
raise
modname = self.str_w(w_modname)
mod = self.interpclass_w(w_mod)
- if isinstance(mod, Module):
+ if isinstance(mod, Module) and not mod.startup_called:
self.timer.start("startup " + modname)
mod.init(self)
self.timer.stop("startup " + modname)
@@ -833,7 +834,7 @@
items = []
else:
try:
- items = newlist(lgt_estimate)
+ items = newlist_hint(lgt_estimate)
except MemoryError:
items = [] # it might have lied
#
@@ -1335,7 +1336,7 @@
if not self.is_true(self.isinstance(w_obj, self.w_str)):
raise OperationError(self.w_TypeError,
self.wrap('argument must be a string'))
- return self.str_w(w_obj)
+ return self.str_w(w_obj)
def unicode_w(self, w_obj):
return w_obj.unicode_w(self)
@@ -1471,8 +1472,8 @@
def warn(self, msg, w_warningcls):
self.appexec([self.wrap(msg), w_warningcls], """(msg, warningcls):
- import warnings
- warnings.warn(msg, warningcls, stacklevel=2)
+ import _warnings
+ _warnings.warn(msg, warningcls, stacklevel=2)
""")
def resolve_target(self, w_obj):
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -20,6 +20,7 @@
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.error import OperationError
from pypy.rlib.objectmodel import compute_hash
+from pypy.rlib.rstring import StringBuilder
class Buffer(Wrappable):
@@ -152,12 +153,13 @@
if space.isinstance_w(w_object, space.w_unicode):
# unicode objects support the old buffer interface
# but not the new buffer interface (change in python 2.7)
- from pypy.rlib.rstruct.unichar import pack_unichar
- charlist = []
- for unich in space.unicode_w(w_object):
- pack_unichar(unich, charlist)
+ from pypy.rlib.rstruct.unichar import pack_unichar, UNICODE_SIZE
+ unistr = space.unicode_w(w_object)
+ builder = StringBuilder(len(unistr) * UNICODE_SIZE)
+ for unich in unistr:
+ pack_unichar(unich, builder)
from pypy.interpreter.buffer import StringBuffer
- w_buffer = space.wrap(StringBuffer(''.join(charlist)))
+ w_buffer = space.wrap(StringBuffer(builder.build()))
else:
w_buffer = space.buffer(w_object)
diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -47,6 +47,11 @@
def async(self, space):
"Check if this is an exception that should better not be caught."
+ if not space.full_exceptions:
+ # flow objspace does not support such exceptions and more
+ # importantly, raises KeyboardInterrupt if you try to access
+ # space.w_KeyboardInterrupt
+ return False
return (self.match(space, space.w_SystemExit) or
self.match(space, space.w_KeyboardInterrupt))
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -901,24 +901,20 @@
def __init__(self, source, filename=None, modname='__builtin__'):
# HAAACK (but a good one)
+ self.filename = filename
+ self.source = str(py.code.Source(source).deindent())
+ self.modname = modname
if filename is None:
f = sys._getframe(1)
filename = '<%s:%d>' % (f.f_code.co_filename, f.f_lineno)
+ if not os.path.exists(filename):
+ # make source code available for tracebacks
+ lines = [x + "\n" for x in source.split("\n")]
+ py.std.linecache.cache[filename] = (1, None, lines, filename)
self.filename = filename
- self.source = str(py.code.Source(source).deindent())
- self.modname = modname
- # look at the first three lines for a NOT_RPYTHON tag
- first = "\n".join(source.split("\n", 3)[:3])
- if "NOT_RPYTHON" in first:
- self.can_use_geninterp = False
- else:
- self.can_use_geninterp = True
- # make source code available for tracebacks
- lines = [x + "\n" for x in source.split("\n")]
- py.std.linecache.cache[filename] = (1, None, lines, filename)
def __repr__(self):
- return "<ApplevelClass filename=%r can_use_geninterp=%r>" % (self.filename, self.can_use_geninterp)
+ return "<ApplevelClass filename=%r>" % (self.filename,)
def getwdict(self, space):
return space.fromcache(ApplevelCache).getorbuild(self)
@@ -979,10 +975,7 @@
def build(self, app):
"NOT_RPYTHON. Called indirectly by Applevel.getwdict()."
- if self.space.config.objspace.geninterp and app.can_use_geninterp:
- return PyPyCacheDir.build_applevelinterp_dict(app, self.space)
- else:
- return build_applevel_dict(app, self.space)
+ return build_applevel_dict(app, self.space)
# __________ pure applevel version __________
@@ -996,157 +989,6 @@
filename=self.filename)
return w_glob
-# __________ geninterplevel version __________
-
-class PyPyCacheDir:
- "NOT_RPYTHON"
- # similar to applevel, but using translation to interp-level.
- # This version maintains a cache folder with single files.
-
- def build_applevelinterp_dict(cls, self, space):
- "NOT_RPYTHON"
- # N.B. 'self' is the ApplevelInterp; this is a class method,
- # just so that we have a convenient place to store the global state.
- if not cls._setup_done:
- cls._setup()
-
- from pypy.translator.geninterplevel import translate_as_module
- import marshal
- scramble = md5(cls.seed)
- scramble.update(marshal.dumps(self.source))
- key = scramble.hexdigest()
- initfunc = cls.known_code.get(key)
- if not initfunc:
- # try to get it from file
- name = key
- if self.filename:
- prename = os.path.splitext(os.path.basename(self.filename))[0]
- else:
- prename = 'zznoname'
- name = "%s_%s" % (prename, name)
- try:
- __import__("pypy._cache."+name)
- except ImportError, x:
- # print x
- pass
- else:
- initfunc = cls.known_code[key]
- if not initfunc:
- # build it and put it into a file
- initfunc, newsrc = translate_as_module(
- self.source, self.filename, self.modname)
- fname = cls.cache_path.join(name+".py").strpath
- f = file(get_tmp_file_name(fname), "w")
- print >> f, """\
-# self-destruct on double-click:
-if __name__ == "__main__":
- from pypy import _cache
- import os
- namestart = os.path.join(os.path.split(_cache.__file__)[0], '%s')
- for ending in ('.py', '.pyc', '.pyo'):
- try:
- os.unlink(namestart+ending)
- except os.error:
- pass""" % name
- print >> f
- print >> f, newsrc
- print >> f, "from pypy._cache import known_code"
- print >> f, "known_code[%r] = %s" % (key, initfunc.__name__)
- f.close()
- rename_tmp_to_eventual_file_name(fname)
- w_glob = initfunc(space)
- return w_glob
- build_applevelinterp_dict = classmethod(build_applevelinterp_dict)
-
- _setup_done = False
-
- def _setup(cls):
- """NOT_RPYTHON"""
- lp = py.path.local
- import pypy, os
- p = lp(pypy.__file__).new(basename='_cache').ensure(dir=1)
- cls.cache_path = p
- ini = p.join('__init__.py')
- try:
- if not ini.check():
- raise ImportError # don't import if only a .pyc file left!!!
- from pypy._cache import known_code, \
- GI_VERSION_RENDERED
- except ImportError:
- GI_VERSION_RENDERED = 0
- from pypy.translator.geninterplevel import GI_VERSION
- cls.seed = md5(str(GI_VERSION)).digest()
- if GI_VERSION != GI_VERSION_RENDERED or GI_VERSION is None:
- for pth in p.listdir():
- if pth.check(file=1):
- try:
- pth.remove()
- except: pass
- f = file(get_tmp_file_name(str(ini)), "w")
- f.write("""\
-# This folder acts as a cache for code snippets which have been
-# compiled by compile_as_module().
-# It will get a new entry for every piece of code that has
-# not been seen, yet.
-#
-# Caution! Only the code snippet is checked. If something
-# is imported, changes are not detected. Also, changes
-# to geninterplevel or gateway are also not checked.
-# Exception: There is a checked version number in geninterplevel.py
-#
-# If in doubt, remove this file from time to time.
-
-GI_VERSION_RENDERED = %r
-
-known_code = {}
-
-# self-destruct on double-click:
-def harakiri():
- import pypy._cache as _c
- import py
- lp = py.path.local
- for pth in lp(_c.__file__).dirpath().listdir():
- try:
- pth.remove()
- except: pass
-
-if __name__ == "__main__":
- harakiri()
-
-del harakiri
-""" % GI_VERSION)
- f.close()
- rename_tmp_to_eventual_file_name(str(ini))
- import pypy._cache
- cls.known_code = pypy._cache.known_code
- cls._setup_done = True
- _setup = classmethod(_setup)
-
-
-def gethostname(_cache=[]):
- if not _cache:
- try:
- import socket
- hostname = socket.gethostname()
- except:
- hostname = ''
- _cache.append(hostname)
- return _cache[0]
-
-def get_tmp_file_name(fname):
- return '%s~%s~%d' % (fname, gethostname(), os.getpid())
-
-def rename_tmp_to_eventual_file_name(fname):
- # generated files are first written to the host- and process-specific
- # file 'tmpname', and then atomically moved to their final 'fname'
- # to avoid problems if py.py is started several times in parallel
- tmpname = get_tmp_file_name(fname)
- try:
- os.rename(tmpname, fname)
- except (OSError, IOError):
- os.unlink(fname) # necessary on Windows
- os.rename(tmpname, fname)
-
# ____________________________________________________________
def appdef(source, applevel=ApplevelClass, filename=None):
@@ -1184,11 +1026,6 @@
return build_applevel_dict(self, space)
-class applevelinterp_temp(ApplevelClass):
- hidden_applevel = False
- def getwdict(self, space): # no cache
- return PyPyCacheDir.build_applevelinterp_dict(self, space)
-
# app2interp_temp is used for testing mainly
def app2interp_temp(func, applevel_temp=applevel_temp, filename=None):
""" NOT_RPYTHON """
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -15,9 +15,8 @@
from pypy.rlib.rarithmetic import r_uint, intmask
from pypy.rlib.unroll import unrolling_iterable
from pypy.rlib.debug import check_nonneg
-from pypy.tool.stdlib_opcode import (bytecode_spec, host_bytecode_spec,
- unrolling_all_opcode_descs, opmap,
- host_opmap)
+from pypy.tool.stdlib_opcode import (bytecode_spec,
+ unrolling_all_opcode_descs)
def unaryoperation(operationname):
"""NOT_RPYTHON"""
@@ -713,6 +712,19 @@
w_list = self.space.newlist(items)
self.pushvalue(w_list)
+ def BUILD_LIST_FROM_ARG(self, _, next_instr):
+ # this is a little dance, because list has to be before the
+ # value
+ last_val = self.popvalue()
+ try:
+ lgt = self.space.len_w(last_val)
+ except OperationError, e:
+ if e.async(self.space):
+ raise
+ lgt = 0 # oh well
+ self.pushvalue(self.space.newlist([], sizehint=lgt))
+ self.pushvalue(last_val)
+
def LOAD_ATTR(self, nameindex, next_instr):
"obj.attributename"
w_obj = self.popvalue()
@@ -1419,11 +1431,9 @@
if lastchar.isspace() and lastchar != ' ':
return
file_softspace(stream, True)
- print_item_to._annspecialcase_ = "specialize:argtype(0)"
def print_item(x):
print_item_to(x, sys_stdout())
- print_item._annspecialcase_ = "flowspace:print_item"
def print_newline_to(stream):
stream.write("\n")
@@ -1431,7 +1441,6 @@
def print_newline():
print_newline_to(sys_stdout())
- print_newline._annspecialcase_ = "flowspace:print_newline"
def file_softspace(file, newflag):
try:
diff --git a/pypy/interpreter/pyparser/parsestring.py b/pypy/interpreter/pyparser/parsestring.py
--- a/pypy/interpreter/pyparser/parsestring.py
+++ b/pypy/interpreter/pyparser/parsestring.py
@@ -1,5 +1,6 @@
from pypy.interpreter.error import OperationError
from pypy.interpreter import unicodehelper
+from pypy.rlib.rstring import StringBuilder
def parsestr(space, encoding, s, unicode_literals=False):
# compiler.transformer.Transformer.decode_literal depends on what
@@ -115,21 +116,23 @@
the string is UTF-8 encoded and should be re-encoded in the
specified encoding.
"""
- lis = []
+ builder = StringBuilder(len(s))
ps = 0
end = len(s)
- while ps < end:
- if s[ps] != '\\':
- # note that the C code has a label here.
- # the logic is the same.
+ while 1:
+ ps2 = ps
+ while ps < end and s[ps] != '\\':
if recode_encoding and ord(s[ps]) & 0x80:
w, ps = decode_utf8(space, s, ps, end, recode_encoding)
- # Append bytes to output buffer.
- lis.append(w)
+ builder.append(w)
+ ps2 = ps
else:
- lis.append(s[ps])
ps += 1
- continue
+ if ps > ps2:
+ builder.append_slice(s, ps2, ps)
+ if ps == end:
+ break
+
ps += 1
if ps == end:
raise_app_valueerror(space, 'Trailing \\ in string')
@@ -140,25 +143,25 @@
if ch == '\n':
pass
elif ch == '\\':
- lis.append('\\')
+ builder.append('\\')
elif ch == "'":
- lis.append("'")
+ builder.append("'")
elif ch == '"':
- lis.append('"')
+ builder.append('"')
elif ch == 'b':
- lis.append("\010")
+ builder.append("\010")
elif ch == 'f':
- lis.append('\014') # FF
+ builder.append('\014') # FF
elif ch == 't':
- lis.append('\t')
+ builder.append('\t')
elif ch == 'n':
- lis.append('\n')
+ builder.append('\n')
elif ch == 'r':
- lis.append('\r')
+ builder.append('\r')
elif ch == 'v':
- lis.append('\013') # VT
+ builder.append('\013') # VT
elif ch == 'a':
- lis.append('\007') # BEL, not classic C
+ builder.append('\007') # BEL, not classic C
elif ch in '01234567':
# Look for up to two more octal digits
span = ps
@@ -168,13 +171,13 @@
# emulate a strange wrap-around behavior of CPython:
# \400 is the same as \000 because 0400 == 256
num = int(octal, 8) & 0xFF
- lis.append(chr(num))
+ builder.append(chr(num))
ps = span
elif ch == 'x':
if ps+2 <= end and isxdigit(s[ps]) and isxdigit(s[ps + 1]):
hexa = s[ps : ps + 2]
num = int(hexa, 16)
- lis.append(chr(num))
+ builder.append(chr(num))
ps += 2
else:
raise_app_valueerror(space, 'invalid \\x escape')
@@ -184,13 +187,13 @@
# this was not an escape, so the backslash
# has to be added, and we start over in
# non-escape mode.
- lis.append('\\')
+ builder.append('\\')
ps -= 1
assert ps >= 0
continue
# an arbitry number of unescaped UTF-8 bytes may follow.
- buf = ''.join(lis)
+ buf = builder.build()
return buf
diff --git a/pypy/interpreter/streamutil.py b/pypy/interpreter/streamutil.py
new file mode 100644
--- /dev/null
+++ b/pypy/interpreter/streamutil.py
@@ -0,0 +1,17 @@
+from pypy.rlib.streamio import StreamError
+from pypy.interpreter.error import OperationError, wrap_oserror2
+
+def wrap_streamerror(space, e, w_filename=None):
+ if isinstance(e, StreamError):
+ return OperationError(space.w_ValueError,
+ space.wrap(e.message))
+ elif isinstance(e, OSError):
+ return wrap_oserror_as_ioerror(space, e, w_filename)
+ else:
+ # should not happen: wrap_streamerror() is only called when
+ # StreamErrors = (OSError, StreamError) are raised
+ return OperationError(space.w_IOError, space.w_None)
+
+def wrap_oserror_as_ioerror(space, e, w_filename=None):
+ return wrap_oserror2(space, e, w_filename,
+ w_exception_class=space.w_IOError)
diff --git a/pypy/interpreter/test/test_appinterp.py b/pypy/interpreter/test/test_appinterp.py
--- a/pypy/interpreter/test/test_appinterp.py
+++ b/pypy/interpreter/test/test_appinterp.py
@@ -1,6 +1,6 @@
import py
-from pypy.interpreter.gateway import appdef, ApplevelClass, applevel_temp, applevelinterp_temp
+from pypy.interpreter.gateway import appdef, ApplevelClass, applevel_temp
from pypy.interpreter.error import OperationError
def test_execwith_novars(space):
@@ -82,9 +82,6 @@
w_res = g(space, space.wrap(10), space.wrap(1))
assert space.eq_w(w_res, space.wrap(-9))
-def test_applevelinterp_functions(space):
- test_applevel_functions(space, applevel_temp = applevelinterp_temp)
-
def test_applevel_class(space, applevel_temp = applevel_temp):
app = applevel_temp('''
class C(object):
@@ -99,9 +96,6 @@
w_clsattr = space.getattr(c, space.wrap('attr'))
assert space.eq_w(w_clsattr, space.wrap(17))
-def test_applevelinterp_class(space):
- test_applevel_class(space, applevel_temp = applevelinterp_temp)
-
def app_test_something_at_app_level():
x = 2
assert x/2 == 1
@@ -161,7 +155,7 @@
w_str = space1.getattr(w_mymod1, space1.wrap("hi"))
assert space1.str_w(w_str) == "hello"
- def test_geninterp_can_unfreeze(self):
+ def test_random_stuff_can_unfreeze(self):
# When a module contains an "import" statement in applevel code, the
# imported module is initialized, possibly after it has been already
# frozen.
diff --git a/pypy/interpreter/test/test_gateway.py b/pypy/interpreter/test/test_gateway.py
--- a/pypy/interpreter/test/test_gateway.py
+++ b/pypy/interpreter/test/test_gateway.py
@@ -101,14 +101,6 @@
g3 = gateway.app2interp_temp(noapp_g3, gateway.applevel_temp)
assert self.space.eq_w(g3(self.space, w('foo'), w('bar')), w('foobar'))
- def test_app2interp2(self):
- """same but using transformed code"""
- w = self.space.wrap
- def noapp_g3(a, b):
- return a+b
- g3 = gateway.app2interp_temp(noapp_g3, gateway.applevelinterp_temp)
- assert self.space.eq_w(g3(self.space, w('foo'), w('bar')), w('foobar'))
-
def test_app2interp_general_args(self):
w = self.space.wrap
def app_general(x, *args, **kwds):
diff --git a/pypy/interpreter/test/test_objspace.py b/pypy/interpreter/test/test_objspace.py
--- a/pypy/interpreter/test/test_objspace.py
+++ b/pypy/interpreter/test/test_objspace.py
@@ -322,3 +322,14 @@
space.ALL_BUILTIN_MODULES.pop()
del space._builtinmodule_list
mods = space.get_builtinmodule_to_install()
+
+ def test_dont_reload_builtin_mods_on_startup(self):
+ from pypy.tool.option import make_config, make_objspace
+ config = make_config(None)
+ space = make_objspace(config)
+ w_executable = space.wrap('executable')
+ assert space.str_w(space.getattr(space.sys, w_executable)) == 'py.py'
+ space.setattr(space.sys, w_executable, space.wrap('foobar'))
+ assert space.str_w(space.getattr(space.sys, w_executable)) == 'foobar'
+ space.startup()
+ assert space.str_w(space.getattr(space.sys, w_executable)) == 'foobar'
diff --git a/pypy/interpreter/test/test_typedef.py b/pypy/interpreter/test/test_typedef.py
--- a/pypy/interpreter/test/test_typedef.py
+++ b/pypy/interpreter/test/test_typedef.py
@@ -304,6 +304,42 @@
assert_method(w_o1, "c", True)
assert_method(w_o2, "c", False)
+ def test_total_ordering(self):
+ class W_SomeType(Wrappable):
+ def __init__(self, space, x):
+ self.space = space
+ self.x = x
+
+ def descr__lt(self, w_other):
+ assert isinstance(w_other, W_SomeType)
+ return self.space.wrap(self.x < w_other.x)
+
+ def descr__eq(self, w_other):
+ assert isinstance(w_other, W_SomeType)
+ return self.space.wrap(self.x == w_other.x)
+
+ W_SomeType.typedef = typedef.TypeDef(
+ 'some_type',
+ __total_ordering__ = 'auto',
+ __lt__ = interp2app(W_SomeType.descr__lt),
+ __eq__ = interp2app(W_SomeType.descr__eq),
+ )
+ space = self.space
+ w_b = space.wrap(W_SomeType(space, 2))
+ w_c = space.wrap(W_SomeType(space, 2))
+ w_a = space.wrap(W_SomeType(space, 1))
+ # explicitly defined
+ assert space.is_true(space.lt(w_a, w_b))
+ assert not space.is_true(space.eq(w_a, w_b))
+ assert space.is_true(space.eq(w_b, w_c))
+ # automatically defined
+ assert space.is_true(space.le(w_a, w_b))
+ assert space.is_true(space.le(w_b, w_c))
+ assert space.is_true(space.gt(w_b, w_a))
+ assert space.is_true(space.ge(w_b, w_a))
+ assert space.is_true(space.ge(w_b, w_c))
+ assert space.is_true(space.ne(w_a, w_b))
+ assert not space.is_true(space.ne(w_b, w_c))
class AppTestTypeDef:
diff --git a/pypy/interpreter/test/test_zpy.py b/pypy/interpreter/test/test_zpy.py
--- a/pypy/interpreter/test/test_zpy.py
+++ b/pypy/interpreter/test/test_zpy.py
@@ -17,14 +17,14 @@
def test_executable():
"""Ensures sys.executable points to the py.py script"""
# TODO : watch out for spaces/special chars in pypypath
- output = run(sys.executable, pypypath,
+ output = run(sys.executable, pypypath, '-S',
"-c", "import sys;print sys.executable")
assert output.splitlines()[-1] == pypypath
def test_special_names():
"""Test the __name__ and __file__ special global names"""
cmd = "print __name__; print '__file__' in globals()"
- output = run(sys.executable, pypypath, '-c', cmd)
+ output = run(sys.executable, pypypath, '-S', '-c', cmd)
assert output.splitlines()[-2] == '__main__'
assert output.splitlines()[-1] == 'False'
@@ -33,24 +33,24 @@
tmpfile.write("print __name__; print __file__\n")
tmpfile.close()
- output = run(sys.executable, pypypath, tmpfilepath)
+ output = run(sys.executable, pypypath, '-S', tmpfilepath)
assert output.splitlines()[-2] == '__main__'
assert output.splitlines()[-1] == str(tmpfilepath)
def test_argv_command():
"""Some tests on argv"""
# test 1 : no arguments
- output = run(sys.executable, pypypath,
+ output = run(sys.executable, pypypath, '-S',
"-c", "import sys;print sys.argv")
assert output.splitlines()[-1] == str(['-c'])
# test 2 : some arguments after
- output = run(sys.executable, pypypath,
+ output = run(sys.executable, pypypath, '-S',
"-c", "import sys;print sys.argv", "hello")
assert output.splitlines()[-1] == str(['-c','hello'])
# test 3 : additionnal pypy parameters
- output = run(sys.executable, pypypath,
+ output = run(sys.executable, pypypath, '-S',
"-O", "-c", "import sys;print sys.argv", "hello")
assert output.splitlines()[-1] == str(['-c','hello'])
@@ -65,15 +65,15 @@
tmpfile.close()
# test 1 : no arguments
- output = run(sys.executable, pypypath, tmpfilepath)
+ output = run(sys.executable, pypypath, '-S', tmpfilepath)
assert output.splitlines()[-1] == str([tmpfilepath])
# test 2 : some arguments after
- output = run(sys.executable, pypypath, tmpfilepath, "hello")
+ output = run(sys.executable, pypypath, '-S', tmpfilepath, "hello")
assert output.splitlines()[-1] == str([tmpfilepath,'hello'])
# test 3 : additionnal pypy parameters
- output = run(sys.executable, pypypath, "-O", tmpfilepath, "hello")
+ output = run(sys.executable, pypypath, '-S', "-O", tmpfilepath, "hello")
assert output.splitlines()[-1] == str([tmpfilepath,'hello'])
@@ -95,7 +95,7 @@
tmpfile.write(TB_NORMALIZATION_CHK)
tmpfile.close()
- popen = subprocess.Popen([sys.executable, str(pypypath), tmpfilepath],
+ popen = subprocess.Popen([sys.executable, str(pypypath), '-S', tmpfilepath],
stderr=subprocess.PIPE)
_, stderr = popen.communicate()
assert stderr.endswith('KeyError: <normalized>\n')
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -12,7 +12,7 @@
from pypy.rlib.jit import promote
class TypeDef:
- def __init__(self, __name, __base=None, **rawdict):
+ def __init__(self, __name, __base=None, __total_ordering__=None, **rawdict):
"NOT_RPYTHON: initialization-time only"
self.name = __name
if __base is None:
@@ -34,6 +34,9 @@
# xxx used by faking
self.fakedcpytype = None
self.add_entries(**rawdict)
+ assert __total_ordering__ in (None, 'auto'), "Unknown value for __total_ordering"
+ if __total_ordering__ == 'auto':
+ self.auto_total_ordering()
def add_entries(self, **rawdict):
# xxx fix the names of the methods to match what app-level expects
@@ -41,7 +44,15 @@
if isinstance(value, (interp2app, GetSetProperty)):
value.name = key
self.rawdict.update(rawdict)
-
+
+ def auto_total_ordering(self):
+ assert '__lt__' in self.rawdict, "__total_ordering='auto' requires __lt__"
+ assert '__eq__' in self.rawdict, "__total_ordering='auto' requires __eq__"
+ self.add_entries(__le__ = auto__le__,
+ __gt__ = auto__gt__,
+ __ge__ = auto__ge__,
+ __ne__ = auto__ne__)
+
def _freeze_(self):
# hint for the annotator: track individual constant instances of TypeDef
return True
@@ -50,6 +61,26 @@
return "<%s name=%r>" % (self.__class__.__name__, self.name)
+# generic special cmp methods defined on top of __lt__ and __eq__, used by
+# automatic total ordering
+
+ at interp2app
+def auto__le__(space, w_self, w_other):
+ return space.not_(space.lt(w_other, w_self))
+
+ at interp2app
+def auto__gt__(space, w_self, w_other):
+ return space.lt(w_other, w_self)
+
+ at interp2app
+def auto__ge__(space, w_self, w_other):
+ return space.not_(space.lt(w_self, w_other))
+
+ at interp2app
+def auto__ne__(space, w_self, w_other):
+ return space.not_(space.eq(w_self, w_other))
+
+
# ____________________________________________________________
# Hash support
diff --git a/pypy/jit/backend/llgraph/llimpl.py b/pypy/jit/backend/llgraph/llimpl.py
--- a/pypy/jit/backend/llgraph/llimpl.py
+++ b/pypy/jit/backend/llgraph/llimpl.py
@@ -171,7 +171,7 @@
'unicodesetitem' : (('ref', 'int', 'int'), 'int'),
'cast_ptr_to_int' : (('ref',), 'int'),
'cast_int_to_ptr' : (('int',), 'ref'),
- 'debug_merge_point': (('ref', 'int'), None),
+ 'debug_merge_point': (('ref', 'int', 'int'), None),
'force_token' : ((), 'int'),
'call_may_force' : (('int', 'varargs'), 'intorptr'),
'guard_not_forced': ((), None),
diff --git a/pypy/jit/backend/llsupport/gc.py b/pypy/jit/backend/llsupport/gc.py
--- a/pypy/jit/backend/llsupport/gc.py
+++ b/pypy/jit/backend/llsupport/gc.py
@@ -208,6 +208,7 @@
This is the class supporting --gcrootfinder=asmgcc.
"""
is_shadow_stack = False
+ is_64_bit = (WORD == 8)
LOC_REG = 0
LOC_ESP_PLUS = 1
@@ -336,17 +337,17 @@
self._gcmap_deadentries += 1
item += asmgcroot.arrayitemsize
- def get_basic_shape(self, is_64_bit=False):
+ def get_basic_shape(self):
# XXX: Should this code even really know about stack frame layout of
# the JIT?
- if is_64_bit:
- return [chr(self.LOC_EBP_PLUS | 8),
- chr(self.LOC_EBP_MINUS | 8),
- chr(self.LOC_EBP_MINUS | 16),
- chr(self.LOC_EBP_MINUS | 24),
- chr(self.LOC_EBP_MINUS | 32),
- chr(self.LOC_EBP_MINUS | 40),
- chr(self.LOC_EBP_PLUS | 0),
+ if self.is_64_bit:
+ return [chr(self.LOC_EBP_PLUS | 4), # return addr: at 8(%rbp)
+ chr(self.LOC_EBP_MINUS | 4), # saved %rbx: at -8(%rbp)
+ chr(self.LOC_EBP_MINUS | 8), # saved %r12: at -16(%rbp)
+ chr(self.LOC_EBP_MINUS | 12), # saved %r13: at -24(%rbp)
+ chr(self.LOC_EBP_MINUS | 16), # saved %r14: at -32(%rbp)
+ chr(self.LOC_EBP_MINUS | 20), # saved %r15: at -40(%rbp)
+ chr(self.LOC_EBP_PLUS | 0), # saved %rbp: at (%rbp)
chr(0)]
else:
return [chr(self.LOC_EBP_PLUS | 4), # return addr: at 4(%ebp)
@@ -366,7 +367,11 @@
shape.append(chr(number | flag))
def add_frame_offset(self, shape, offset):
- assert (offset & 3) == 0
+ if self.is_64_bit:
+ assert (offset & 7) == 0
+ offset >>= 1
+ else:
+ assert (offset & 3) == 0
if offset >= 0:
num = self.LOC_EBP_PLUS | offset
else:
@@ -518,7 +523,7 @@
def initialize(self):
pass
- def get_basic_shape(self, is_64_bit=False):
+ def get_basic_shape(self):
return []
def add_frame_offset(self, shape, offset):
@@ -594,7 +599,7 @@
# if convenient for the backend, we compute the info about
# the flag as (byte-offset, single-byte-flag).
import struct
- value = struct.pack("l", flag_word)
+ value = struct.pack(lltype.SignedFmt, flag_word)
assert value.count('\x00') == len(value) - 1 # only one byte is != 0
i = 0
while value[i] == '\x00': i += 1
@@ -769,11 +774,19 @@
self.generate_function('malloc_unicode', malloc_unicode,
[lltype.Signed])
- # Rarely called: allocate a fixed-size amount of bytes, but
- # not in the nursery, because it is too big. Implemented like
- # malloc_nursery_slowpath() above.
- self.generate_function('malloc_fixedsize', malloc_nursery_slowpath,
- [lltype.Signed])
+ # Never called as far as I can tell, but there for completeness:
+ # allocate a fixed-size object, but not in the nursery, because
+ # it is too big.
+ def malloc_big_fixedsize(size, tid):
+ if self.DEBUG:
+ self._random_usage_of_xmm_registers()
+ type_id = llop.extract_ushort(llgroup.HALFWORD, tid)
+ check_typeid(type_id)
+ return llop1.do_malloc_fixedsize_clear(llmemory.GCREF,
+ type_id, size,
+ False, False, False)
+ self.generate_function('malloc_big_fixedsize', malloc_big_fixedsize,
+ [lltype.Signed] * 2)
def _bh_malloc(self, sizedescr):
from pypy.rpython.memory.gctypelayout import check_typeid
diff --git a/pypy/jit/backend/llsupport/regalloc.py b/pypy/jit/backend/llsupport/regalloc.py
--- a/pypy/jit/backend/llsupport/regalloc.py
+++ b/pypy/jit/backend/llsupport/regalloc.py
@@ -321,7 +321,7 @@
except KeyError:
pass # 'var' is already not in a register
- def loc(self, box):
+ def loc(self, box, must_exist=False):
""" Return the location of 'box'.
"""
self._check_type(box)
@@ -332,6 +332,8 @@
except KeyError:
if box in self.bindings_to_frame_reg:
return self.frame_reg
+ if must_exist:
+ return self.frame_manager.bindings[box]
return self.frame_manager.loc(box)
def return_constant(self, v, forbidden_vars=[], selected_reg=None):
@@ -360,7 +362,7 @@
self._check_type(v)
if isinstance(v, Const):
return self.return_constant(v, forbidden_vars, selected_reg)
- prev_loc = self.loc(v)
+ prev_loc = self.loc(v, must_exist=True)
if prev_loc is self.frame_reg and selected_reg is None:
return prev_loc
loc = self.force_allocate_reg(v, forbidden_vars, selected_reg,
diff --git a/pypy/jit/backend/llsupport/rewrite.py b/pypy/jit/backend/llsupport/rewrite.py
--- a/pypy/jit/backend/llsupport/rewrite.py
+++ b/pypy/jit/backend/llsupport/rewrite.py
@@ -96,8 +96,10 @@
def handle_new_fixedsize(self, descr, op):
assert isinstance(descr, SizeDescr)
size = descr.size
- self.gen_malloc_nursery(size, op.result)
- self.gen_initialize_tid(op.result, descr.tid)
+ if self.gen_malloc_nursery(size, op.result):
+ self.gen_initialize_tid(op.result, descr.tid)
+ else:
+ self.gen_malloc_fixedsize(size, descr.tid, op.result)
def handle_new_array(self, arraydescr, op):
v_length = op.getarg(0)
@@ -112,8 +114,8 @@
pass # total_size is still -1
elif arraydescr.itemsize == 0:
total_size = arraydescr.basesize
- if 0 <= total_size <= 0xffffff: # up to 16MB, arbitrarily
- self.gen_malloc_nursery(total_size, op.result)
+ if (total_size >= 0 and
+ self.gen_malloc_nursery(total_size, op.result)):
self.gen_initialize_tid(op.result, arraydescr.tid)
self.gen_initialize_len(op.result, v_length, arraydescr.lendescr)
elif self.gc_ll_descr.kind == 'boehm':
@@ -147,13 +149,22 @@
# mark 'v_result' as freshly malloced
self.recent_mallocs[v_result] = None
- def gen_malloc_fixedsize(self, size, v_result):
- """Generate a CALL_MALLOC_GC(malloc_fixedsize_fn, Const(size)).
- Note that with the framework GC, this should be called very rarely.
+ def gen_malloc_fixedsize(self, size, typeid, v_result):
+ """Generate a CALL_MALLOC_GC(malloc_fixedsize_fn, ...).
+ Used on Boehm, and on the framework GC for large fixed-size
+ mallocs. (For all I know this latter case never occurs in
+ practice, but better safe than sorry.)
"""
- addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_fixedsize')
- self._gen_call_malloc_gc([ConstInt(addr), ConstInt(size)], v_result,
- self.gc_ll_descr.malloc_fixedsize_descr)
+ if self.gc_ll_descr.fielddescr_tid is not None: # framework GC
+ assert (size & (WORD-1)) == 0, "size not aligned?"
+ addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_big_fixedsize')
+ args = [ConstInt(addr), ConstInt(size), ConstInt(typeid)]
+ descr = self.gc_ll_descr.malloc_big_fixedsize_descr
+ else: # Boehm
+ addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_fixedsize')
+ args = [ConstInt(addr), ConstInt(size)]
+ descr = self.gc_ll_descr.malloc_fixedsize_descr
+ self._gen_call_malloc_gc(args, v_result, descr)
def gen_boehm_malloc_array(self, arraydescr, v_num_elem, v_result):
"""Generate a CALL_MALLOC_GC(malloc_array_fn, ...) for Boehm."""
@@ -211,8 +222,7 @@
"""
size = self.round_up_for_allocation(size)
if not self.gc_ll_descr.can_use_nursery_malloc(size):
- self.gen_malloc_fixedsize(size, v_result)
- return
+ return False
#
op = None
if self._op_malloc_nursery is not None:
@@ -238,6 +248,7 @@
self._previous_size = size
self._v_last_malloced_nursery = v_result
self.recent_mallocs[v_result] = None
+ return True
def gen_initialize_tid(self, v_newgcobj, tid):
if self.gc_ll_descr.fielddescr_tid is not None:
diff --git a/pypy/jit/backend/llsupport/test/test_descr.py b/pypy/jit/backend/llsupport/test/test_descr.py
--- a/pypy/jit/backend/llsupport/test/test_descr.py
+++ b/pypy/jit/backend/llsupport/test/test_descr.py
@@ -148,7 +148,7 @@
#
def get_alignment(code):
# Retrieve default alignment for the compiler/platform
- return struct.calcsize('l' + code) - struct.calcsize(code)
+ return struct.calcsize(lltype.SignedFmt + code) - struct.calcsize(code)
assert descr1.basesize == get_alignment('c')
assert descr2.basesize == get_alignment('p')
assert descr3.basesize == get_alignment('p')
diff --git a/pypy/jit/backend/llsupport/test/test_ffisupport.py b/pypy/jit/backend/llsupport/test/test_ffisupport.py
--- a/pypy/jit/backend/llsupport/test/test_ffisupport.py
+++ b/pypy/jit/backend/llsupport/test/test_ffisupport.py
@@ -2,6 +2,7 @@
from pypy.jit.codewriter.longlong import is_64_bit
from pypy.jit.backend.llsupport.descr import *
from pypy.jit.backend.llsupport.ffisupport import *
+from pypy.rlib.rarithmetic import is_emulated_long
class FakeCPU:
@@ -43,7 +44,7 @@
assert descr.result_flag == FLAG_UNSIGNED
assert descr.is_result_signed() == False
- if not is_64_bit:
+ if not is_64_bit or is_emulated_long:
descr = get_call_descr_dynamic(FakeCPU(), [], types.slonglong,
None, 42)
assert descr is None # missing longlongs
diff --git a/pypy/jit/backend/llsupport/test/test_gc.py b/pypy/jit/backend/llsupport/test/test_gc.py
--- a/pypy/jit/backend/llsupport/test/test_gc.py
+++ b/pypy/jit/backend/llsupport/test/test_gc.py
@@ -11,6 +11,7 @@
from pypy.jit.tool.oparser import parse
from pypy.rpython.lltypesystem.rclass import OBJECT, OBJECT_VTABLE
from pypy.jit.metainterp.optimizeopt.util import equaloplists
+from pypy.rlib.rarithmetic import is_valid_int
def test_boehm():
gc_ll_descr = GcLLDescr_boehm(None, None, None)
@@ -57,6 +58,7 @@
def frame_pos(n):
return -4*(4+n)
gcrootmap = GcRootMap_asmgcc()
+ gcrootmap.is_64_bit = False
num1 = frame_pos(-5)
num1a = num1|2
num2 = frame_pos(55)
@@ -102,7 +104,7 @@
gcrootmap.put(retaddr, shapeaddr)
assert gcrootmap._gcmap[0] == retaddr
assert gcrootmap._gcmap[1] == shapeaddr
- p = rffi.cast(rffi.LONGP, gcrootmap.gcmapstart())
+ p = rffi.cast(rffi.SIGNEDP, gcrootmap.gcmapstart())
assert p[0] == retaddr
assert (gcrootmap.gcmapend() ==
gcrootmap.gcmapstart() + rffi.sizeof(lltype.Signed) * 2)
@@ -418,9 +420,9 @@
assert newops[0].getarg(1) == v_value
assert newops[0].result is None
wbdescr = newops[0].getdescr()
- assert isinstance(wbdescr.jit_wb_if_flag, int)
- assert isinstance(wbdescr.jit_wb_if_flag_byteofs, int)
- assert isinstance(wbdescr.jit_wb_if_flag_singlebyte, int)
+ assert is_valid_int(wbdescr.jit_wb_if_flag)
+ assert is_valid_int(wbdescr.jit_wb_if_flag_byteofs)
+ assert is_valid_int(wbdescr.jit_wb_if_flag_singlebyte)
def test_get_rid_of_debug_merge_point(self):
operations = [
diff --git a/pypy/jit/backend/llsupport/test/test_regalloc.py b/pypy/jit/backend/llsupport/test/test_regalloc.py
--- a/pypy/jit/backend/llsupport/test/test_regalloc.py
+++ b/pypy/jit/backend/llsupport/test/test_regalloc.py
@@ -1,4 +1,4 @@
-
+import py
from pypy.jit.metainterp.history import BoxInt, ConstInt, BoxFloat, INT, FLOAT
from pypy.jit.backend.llsupport.regalloc import FrameManager
from pypy.jit.backend.llsupport.regalloc import RegisterManager as BaseRegMan
@@ -236,6 +236,16 @@
assert isinstance(loc, FakeFramePos)
assert len(asm.moves) == 1
+ def test_bogus_make_sure_var_in_reg(self):
+ b0, = newboxes(0)
+ longevity = {b0: (0, 1)}
+ fm = TFrameManager()
+ asm = MockAsm()
+ rm = RegisterManager(longevity, frame_manager=fm, assembler=asm)
+ rm.next_instruction()
+ # invalid call to make_sure_var_in_reg(): box unknown so far
+ py.test.raises(KeyError, rm.make_sure_var_in_reg, b0)
+
def test_return_constant(self):
asm = MockAsm()
boxes, longevity = boxes_and_longevity(5)
diff --git a/pypy/jit/backend/llsupport/test/test_rewrite.py b/pypy/jit/backend/llsupport/test/test_rewrite.py
--- a/pypy/jit/backend/llsupport/test/test_rewrite.py
+++ b/pypy/jit/backend/llsupport/test/test_rewrite.py
@@ -119,12 +119,19 @@
jump()
""", """
[]
- p0 = call_malloc_gc(ConstClass(malloc_fixedsize), \
- %(adescr.basesize + 10 * adescr.itemsize)d, \
- descr=malloc_fixedsize_descr)
- setfield_gc(p0, 10, descr=alendescr)
+ p0 = call_malloc_gc(ConstClass(malloc_array), \
+ %(adescr.basesize)d, \
+ 10, \
+ %(adescr.itemsize)d, \
+ %(adescr.lendescr.offset)d, \
+ descr=malloc_array_descr)
jump()
""")
+## should ideally be:
+## p0 = call_malloc_gc(ConstClass(malloc_fixedsize), \
+## %(adescr.basesize + 10 * adescr.itemsize)d, \
+## descr=malloc_fixedsize_descr)
+## setfield_gc(p0, 10, descr=alendescr)
def test_new_array_variable(self):
self.check_rewrite("""
@@ -178,13 +185,20 @@
jump()
""", """
[i1]
- p0 = call_malloc_gc(ConstClass(malloc_fixedsize), \
- %(unicodedescr.basesize + \
- 10 * unicodedescr.itemsize)d, \
- descr=malloc_fixedsize_descr)
- setfield_gc(p0, 10, descr=unicodelendescr)
+ p0 = call_malloc_gc(ConstClass(malloc_array), \
+ %(unicodedescr.basesize)d, \
+ 10, \
+ %(unicodedescr.itemsize)d, \
+ %(unicodelendescr.offset)d, \
+ descr=malloc_array_descr)
jump()
""")
+## should ideally be:
+## p0 = call_malloc_gc(ConstClass(malloc_fixedsize), \
+## %(unicodedescr.basesize + \
+## 10 * unicodedescr.itemsize)d, \
+## descr=malloc_fixedsize_descr)
+## setfield_gc(p0, 10, descr=unicodelendescr)
class TestFramework(RewriteTests):
@@ -203,7 +217,7 @@
#
class FakeCPU(object):
def sizeof(self, STRUCT):
- descr = SizeDescrWithVTable(102)
+ descr = SizeDescrWithVTable(104)
descr.tid = 9315
return descr
self.cpu = FakeCPU()
@@ -368,11 +382,9 @@
jump()
""", """
[]
- p0 = call_malloc_gc(ConstClass(malloc_fixedsize), \
- %(bdescr.basesize + 104)d, \
- descr=malloc_fixedsize_descr)
- setfield_gc(p0, 8765, descr=tiddescr)
- setfield_gc(p0, 103, descr=blendescr)
+ p0 = call_malloc_gc(ConstClass(malloc_array), 1, \
+ %(bdescr.tid)d, 103, \
+ descr=malloc_array_descr)
jump()
""")
@@ -435,9 +447,8 @@
jump()
""", """
[p1]
- p0 = call_malloc_gc(ConstClass(malloc_fixedsize), 104, \
- descr=malloc_fixedsize_descr)
- setfield_gc(p0, 9315, descr=tiddescr)
+ p0 = call_malloc_gc(ConstClass(malloc_big_fixedsize), 104, 9315, \
+ descr=malloc_big_fixedsize_descr)
setfield_gc(p0, ConstClass(o_vtable), descr=vtable_descr)
jump()
""")
diff --git a/pypy/jit/backend/test/runner_test.py b/pypy/jit/backend/test/runner_test.py
--- a/pypy/jit/backend/test/runner_test.py
+++ b/pypy/jit/backend/test/runner_test.py
@@ -16,9 +16,11 @@
from pypy.rpython.annlowlevel import llhelper
from pypy.rpython.llinterp import LLException
from pypy.jit.codewriter import heaptracker, longlong
-from pypy.rlib.rarithmetic import intmask
+from pypy.rlib import longlong2float
+from pypy.rlib.rarithmetic import intmask, is_valid_int
from pypy.jit.backend.detect_cpu import autodetect_main_model_and_size
+
def boxfloat(x):
return BoxFloat(longlong.getfloatstorage(x))
@@ -266,6 +268,38 @@
res = self.cpu.get_latest_value_int(0)
assert res == 20
+ def test_compile_big_bridge_out_of_small_loop(self):
+ i0 = BoxInt()
+ faildescr1 = BasicFailDescr(1)
+ looptoken = JitCellToken()
+ operations = [
+ ResOperation(rop.GUARD_FALSE, [i0], None, descr=faildescr1),
+ ResOperation(rop.FINISH, [], None, descr=BasicFailDescr(2)),
+ ]
+ inputargs = [i0]
+ operations[0].setfailargs([i0])
+ self.cpu.compile_loop(inputargs, operations, looptoken)
+
+ i1list = [BoxInt() for i in range(1000)]
+ bridge = []
+ iprev = i0
+ for i1 in i1list:
+ bridge.append(ResOperation(rop.INT_ADD, [iprev, ConstInt(1)], i1))
+ iprev = i1
+ bridge.append(ResOperation(rop.GUARD_FALSE, [i0], None,
+ descr=BasicFailDescr(3)))
+ bridge.append(ResOperation(rop.FINISH, [], None,
+ descr=BasicFailDescr(4)))
+ bridge[-2].setfailargs(i1list)
+
+ self.cpu.compile_bridge(faildescr1, [i0], bridge, looptoken)
+
+ fail = self.cpu.execute_token(looptoken, 1)
+ assert fail.identifier == 3
+ for i in range(1000):
+ res = self.cpu.get_latest_value_int(i)
+ assert res == 2 + i
+
def test_get_latest_value_count(self):
i0 = BoxInt()
i1 = BoxInt()
@@ -461,7 +495,7 @@
if cpu.supports_floats:
def func(f, i):
assert isinstance(f, float)
- assert isinstance(i, int)
+ assert is_valid_int(i)
return f - float(i)
FPTR = self.Ptr(self.FuncType([lltype.Float, lltype.Signed],
lltype.Float))
@@ -572,7 +606,7 @@
[funcbox, BoxInt(arg1), BoxInt(arg2)],
'int', descr=calldescr)
assert res.getint() == f(arg1, arg2)
-
+
def test_call_stack_alignment(self):
# test stack alignment issues, notably for Mac OS/X.
# also test the ordering of the arguments.
@@ -1458,18 +1492,36 @@
def test_noops(self):
c_box = self.alloc_string("hi there").constbox()
c_nest = ConstInt(0)
- self.execute_operation(rop.DEBUG_MERGE_POINT, [c_box, c_nest], 'void')
+ c_id = ConstInt(0)
+ self.execute_operation(rop.DEBUG_MERGE_POINT, [c_box, c_nest, c_id], 'void')
self.execute_operation(rop.JIT_DEBUG, [c_box, c_nest, c_nest,
c_nest, c_nest], 'void')
def test_read_timestamp(self):
+ if sys.platform == 'win32':
+ # windows quite often is very inexact (like the old Intel 8259 PIC),
+ # so we stretch the time a little bit.
+ # On my virtual Parallels machine in a 2GHz Core i7 Mac Mini,
+ # the test starts working at delay == 21670 and stops at 20600000.
+ # We take the geometric mean value.
+ from math import log, exp
+ delay_min = 21670
+ delay_max = 20600000
+ delay = int(exp((log(delay_min)+log(delay_max))/2))
+ def wait_a_bit():
+ for i in xrange(delay): pass
+ else:
+ def wait_a_bit():
+ pass
if longlong.is_64_bit:
got1 = self.execute_operation(rop.READ_TIMESTAMP, [], 'int')
+ wait_a_bit()
got2 = self.execute_operation(rop.READ_TIMESTAMP, [], 'int')
res1 = got1.getint()
res2 = got2.getint()
else:
got1 = self.execute_operation(rop.READ_TIMESTAMP, [], 'float')
+ wait_a_bit()
got2 = self.execute_operation(rop.READ_TIMESTAMP, [], 'float')
res1 = got1.getlonglong()
res2 = got2.getlonglong()
@@ -1565,6 +1617,12 @@
[BoxPtr(x)], 'int').value
assert res == -19
+ def test_convert_float_bytes(self):
+ t = 'int' if longlong.is_64_bit else 'float'
+ res = self.execute_operation(rop.CONVERT_FLOAT_BYTES_TO_LONGLONG,
+ [boxfloat(2.5)], t).value
+ assert res == longlong2float.float2longlong(2.5)
+
def test_ooops_non_gc(self):
x = lltype.malloc(lltype.Struct('x'), flavor='raw')
v = heaptracker.adr2int(llmemory.cast_ptr_to_adr(x))
@@ -3029,7 +3087,7 @@
ResOperation(rop.JUMP, [i2], None, descr=targettoken2),
]
self.cpu.compile_bridge(faildescr, inputargs, operations, looptoken)
-
+
fail = self.cpu.execute_token(looptoken, 2)
assert fail.identifier == 3
res = self.cpu.get_latest_value_int(0)
@@ -3074,7 +3132,7 @@
assert len(mc) == len(ops)
for i in range(len(mc)):
assert mc[i].split("\t")[-1].startswith(ops[i])
-
+
data = ctypes.string_at(info.asmaddr, info.asmlen)
mc = list(machine_code_dump(data, info.asmaddr, cpuname))
lines = [line for line in mc if line.count('\t') == 2]
diff --git a/pypy/jit/backend/test/support.py b/pypy/jit/backend/test/support.py
--- a/pypy/jit/backend/test/support.py
+++ b/pypy/jit/backend/test/support.py
@@ -3,6 +3,7 @@
from pypy.rlib.debug import debug_print
from pypy.translator.translator import TranslationContext, graphof
from pypy.jit.metainterp.optimizeopt import ALL_OPTS_NAMES
+from pypy.rlib.rarithmetic import is_valid_int
class BaseCompiledMixin(object):
@@ -24,7 +25,7 @@
from pypy.annotation import model as annmodel
for arg in args:
- assert isinstance(arg, int)
+ assert is_valid_int(arg)
self.pre_translation_hook()
t = self._get_TranslationContext()
diff --git a/pypy/jit/backend/test/test_random.py b/pypy/jit/backend/test/test_random.py
--- a/pypy/jit/backend/test/test_random.py
+++ b/pypy/jit/backend/test/test_random.py
@@ -449,6 +449,7 @@
OPERATIONS.append(CastFloatToIntOperation(rop.CAST_FLOAT_TO_INT))
OPERATIONS.append(CastIntToFloatOperation(rop.CAST_INT_TO_FLOAT))
+OPERATIONS.append(CastFloatToIntOperation(rop.CONVERT_FLOAT_BYTES_TO_LONGLONG))
OperationBuilder.OPERATIONS = OPERATIONS
@@ -502,11 +503,11 @@
else:
assert 0, "unknown backend %r" % pytest.config.option.backend
-# ____________________________________________________________
+# ____________________________________________________________
class RandomLoop(object):
dont_generate_more = False
-
+
def __init__(self, cpu, builder_factory, r, startvars=None):
self.cpu = cpu
if startvars is None:
diff --git a/pypy/jit/backend/x86/assembler.py b/pypy/jit/backend/x86/assembler.py
--- a/pypy/jit/backend/x86/assembler.py
+++ b/pypy/jit/backend/x86/assembler.py
@@ -88,7 +88,6 @@
self._debug = False
self.debug_counter_descr = cpu.fielddescrof(DEBUG_COUNTER, 'i')
self.fail_boxes_count = 0
- self._current_depths_cache = (0, 0)
self.datablockwrapper = None
self.stack_check_slowpath = 0
self.propagate_exception_path = 0
@@ -442,10 +441,8 @@
looppos = self.mc.get_relative_pos()
looptoken._x86_loop_code = looppos
clt.frame_depth = -1 # temporarily
- clt.param_depth = -1 # temporarily
- frame_depth, param_depth = self._assemble(regalloc, operations)
+ frame_depth = self._assemble(regalloc, operations)
clt.frame_depth = frame_depth
- clt.param_depth = param_depth
#
size_excluding_failure_stuff = self.mc.get_relative_pos()
self.write_pending_failure_recoveries()
@@ -459,8 +456,7 @@
rawstart + size_excluding_failure_stuff,
rawstart))
debug_stop("jit-backend-addr")
- self._patch_stackadjust(rawstart + stackadjustpos,
- frame_depth + param_depth)
+ self._patch_stackadjust(rawstart + stackadjustpos, frame_depth)
self.patch_pending_failure_recoveries(rawstart)
#
ops_offset = self.mc.ops_offset
@@ -500,14 +496,13 @@
assert ([loc.assembler() for loc in arglocs] ==
[loc.assembler() for loc in faildescr._x86_debug_faillocs])
regalloc = RegAlloc(self, self.cpu.translate_support_code)
- fail_depths = faildescr._x86_current_depths
startpos = self.mc.get_relative_pos()
- operations = regalloc.prepare_bridge(fail_depths, inputargs, arglocs,
+ operations = regalloc.prepare_bridge(inputargs, arglocs,
operations,
self.current_clt.allgcrefs)
stackadjustpos = self._patchable_stackadjust()
- frame_depth, param_depth = self._assemble(regalloc, operations)
+ frame_depth = self._assemble(regalloc, operations)
codeendpos = self.mc.get_relative_pos()
self.write_pending_failure_recoveries()
fullsize = self.mc.get_relative_pos()
@@ -517,19 +512,16 @@
debug_print("bridge out of Guard %d has address %x to %x" %
(descr_number, rawstart, rawstart + codeendpos))
debug_stop("jit-backend-addr")
- self._patch_stackadjust(rawstart + stackadjustpos,
- frame_depth + param_depth)
+ self._patch_stackadjust(rawstart + stackadjustpos, frame_depth)
self.patch_pending_failure_recoveries(rawstart)
if not we_are_translated():
# for the benefit of tests
faildescr._x86_bridge_frame_depth = frame_depth
- faildescr._x86_bridge_param_depth = param_depth
# patch the jump from original guard
self.patch_jump_for_descr(faildescr, rawstart)
ops_offset = self.mc.ops_offset
self.fixup_target_tokens(rawstart)
self.current_clt.frame_depth = max(self.current_clt.frame_depth, frame_depth)
- self.current_clt.param_depth = max(self.current_clt.param_depth, param_depth)
self.teardown()
# oprofile support
if self.cpu.profile_agent is not None:
@@ -614,7 +606,7 @@
else:
assert token
struct.number = compute_unique_id(token)
- self.loop_run_counters.append(struct)
+ self.loop_run_counters.append(struct)
return struct
def _find_failure_recovery_bytecode(self, faildescr):
@@ -673,7 +665,7 @@
ResOperation(rop.SETFIELD_RAW, [c_adr, box2],
None, descr=self.debug_counter_descr)]
operations.extend(ops)
-
+
@specialize.argtype(1)
def _inject_debugging_code(self, looptoken, operations, tp, number):
if self._debug:
@@ -700,15 +692,12 @@
regalloc.walk_operations(operations)
if we_are_translated() or self.cpu.dont_keepalive_stuff:
self._regalloc = None # else keep it around for debugging
- frame_depth = regalloc.fm.get_frame_depth()
- param_depth = regalloc.param_depth
+ frame_depth = regalloc.get_final_frame_depth()
jump_target_descr = regalloc.jump_target_descr
if jump_target_descr is not None:
target_frame_depth = jump_target_descr._x86_clt.frame_depth
- target_param_depth = jump_target_descr._x86_clt.param_depth
frame_depth = max(frame_depth, target_frame_depth)
- param_depth = max(param_depth, target_param_depth)
- return frame_depth, param_depth
+ return frame_depth
def _patchable_stackadjust(self):
# stack adjustment LEA
@@ -847,8 +836,8 @@
self.mc.MOVSD_sx(0, loc.value)
elif WORD == 4 and isinstance(loc, StackLoc) and loc.get_width() == 8:
# XXX evil trick
- self.mc.PUSH_b(get_ebp_ofs(loc.position))
- self.mc.PUSH_b(get_ebp_ofs(loc.position + 1))
+ self.mc.PUSH_b(loc.value + 4)
+ self.mc.PUSH_b(loc.value)
else:
self.mc.PUSH(loc)
@@ -858,8 +847,8 @@
self.mc.ADD_ri(esp.value, 8) # = size of doubles
elif WORD == 4 and isinstance(loc, StackLoc) and loc.get_width() == 8:
# XXX evil trick
- self.mc.POP_b(get_ebp_ofs(loc.position + 1))
- self.mc.POP_b(get_ebp_ofs(loc.position))
+ self.mc.POP_b(loc.value)
+ self.mc.POP_b(loc.value + 4)
else:
self.mc.POP(loc)
@@ -892,10 +881,9 @@
genop_math_list[oopspecindex](self, op, arglocs, resloc)
def regalloc_perform_with_guard(self, op, guard_op, faillocs,
- arglocs, resloc, current_depths):
+ arglocs, resloc):
faildescr = guard_op.getdescr()
assert isinstance(faildescr, AbstractFailDescr)
- faildescr._x86_current_depths = current_depths
failargs = guard_op.getfailargs()
guard_opnum = guard_op.getopnum()
guard_token = self.implement_guard_recovery(guard_opnum,
@@ -911,10 +899,9 @@
# must be added by the genop_guard_list[]()
assert guard_token is self.pending_guard_tokens[-1]
- def regalloc_perform_guard(self, guard_op, faillocs, arglocs, resloc,
- current_depths):
+ def regalloc_perform_guard(self, guard_op, faillocs, arglocs, resloc):
self.regalloc_perform_with_guard(None, guard_op, faillocs, arglocs,
- resloc, current_depths)
+ resloc)
def load_effective_addr(self, sizereg, baseofs, scale, result, frm=imm0):
self.mc.LEA(result, addr_add(frm, sizereg, baseofs, scale))
@@ -1038,13 +1025,14 @@
self.mc.MOV(tmp, loc)
self.mc.MOV_sr(p, tmp.value)
p += loc.get_width()
- self._regalloc.reserve_param(p//WORD)
# x is a location
self.mc.CALL(x)
self.mark_gc_roots(force_index)
#
if callconv != FFI_DEFAULT_ABI:
self._fix_stdcall(callconv, p)
+ #
+ self._regalloc.needed_extra_stack_locations(p//WORD)
def _fix_stdcall(self, callconv, p):
from pypy.rlib.clibffi import FFI_STDCALL
@@ -1127,9 +1115,9 @@
x = r10
remap_frame_layout(self, src_locs, dst_locs, X86_64_SCRATCH_REG)
- self._regalloc.reserve_param(len(pass_on_stack))
self.mc.CALL(x)
self.mark_gc_roots(force_index)
+ self._regalloc.needed_extra_stack_locations(len(pass_on_stack))
def call(self, addr, args, res):
force_index = self.write_new_force_index()
@@ -1254,6 +1242,15 @@
self.mc.MOVD_xr(resloc.value, loc0.value)
self.mc.CVTSS2SD_xx(resloc.value, resloc.value)
+ def genop_convert_float_bytes_to_longlong(self, op, arglocs, resloc):
+ loc0, = arglocs
+ if longlong.is_64_bit:
+ assert isinstance(resloc, RegLoc)
+ assert isinstance(loc0, RegLoc)
+ self.mc.MOVD(resloc, loc0)
+ else:
+ self.mov(loc0, resloc)
+
def genop_guard_int_is_true(self, op, guard_op, guard_token, arglocs, resloc):
guard_opnum = guard_op.getopnum()
self.mc.CMP(arglocs[0], imm0)
@@ -1966,8 +1963,6 @@
mc.PUSH_r(ebx.value)
elif IS_X86_64:
mc.MOV_rr(edi.value, ebx.value)
- # XXX: Correct to only align the stack on 64-bit?
- mc.AND_ri(esp.value, -16)
else:
raise AssertionError("Shouldn't happen")
@@ -2129,14 +2124,16 @@
# First, we need to save away the registers listed in
# 'save_registers' that are not callee-save. XXX We assume that
# the XMM registers won't be modified. We store them in
- # [ESP+4], [ESP+8], etc., leaving enough room in [ESP] for the
- # single argument to closestack_addr below.
- p = WORD
+ # [ESP+4], [ESP+8], etc.; on x86-32 we leave enough room in [ESP]
+ # for the single argument to closestack_addr below.
+ if IS_X86_32:
+ p = WORD
+ elif IS_X86_64:
+ p = 0
for reg in self._regalloc.rm.save_around_call_regs:
if reg in save_registers:
self.mc.MOV_sr(p, reg.value)
p += WORD
- self._regalloc.reserve_param(p//WORD)
#
if gcrootmap.is_shadow_stack:
args = []
@@ -2187,11 +2184,15 @@
#
self._emit_call(-1, imm(self.releasegil_addr), args)
# Finally, restore the registers saved above.
- p = WORD
+ if IS_X86_32:
+ p = WORD
+ elif IS_X86_64:
+ p = 0
for reg in self._regalloc.rm.save_around_call_regs:
if reg in save_registers:
self.mc.MOV_rs(reg.value, p)
p += WORD
+ self._regalloc.needed_extra_stack_locations(p//WORD)
def call_reacquire_gil(self, gcrootmap, save_loc):
# save the previous result (eax/xmm0) into the stack temporarily.
@@ -2199,7 +2200,6 @@
# to save xmm0 in this case.
if isinstance(save_loc, RegLoc) and not save_loc.is_xmm:
self.mc.MOV_sr(WORD, save_loc.value)
- self._regalloc.reserve_param(2)
# call the reopenstack() function (also reacquiring the GIL)
if gcrootmap.is_shadow_stack:
args = []
@@ -2219,6 +2219,7 @@
# restore the result from the stack
if isinstance(save_loc, RegLoc) and not save_loc.is_xmm:
self.mc.MOV_rs(save_loc.value, WORD)
+ self._regalloc.needed_extra_stack_locations(2)
def genop_guard_call_assembler(self, op, guard_op, guard_token,
arglocs, result_loc):
@@ -2495,11 +2496,6 @@
# copy of heap(nursery_free_adr), so that the final MOV below is
# a no-op.
- # reserve room for the argument to the real malloc and the
- # saved XMM regs (on 32 bit: 8 * 2 words; on 64 bit: 16 * 1
- # word)
- self._regalloc.reserve_param(1+16)
-
gcrootmap = self.cpu.gc_ll_descr.gcrootmap
shadow_stack = (gcrootmap is not None and gcrootmap.is_shadow_stack)
if not shadow_stack:
@@ -2510,6 +2506,11 @@
slowpath_addr2 = self.malloc_slowpath2
self.mc.CALL(imm(slowpath_addr2))
+ # reserve room for the argument to the real malloc and the
+ # saved XMM regs (on 32 bit: 8 * 2 words; on 64 bit: 16 * 1
+ # word)
+ self._regalloc.needed_extra_stack_locations(1+16)
+
offset = self.mc.get_relative_pos() - jmp_adr
assert 0 < offset <= 127
self.mc.overwrite(jmp_adr-1, chr(offset))
diff --git a/pypy/jit/backend/x86/codebuf.py b/pypy/jit/backend/x86/codebuf.py
--- a/pypy/jit/backend/x86/codebuf.py
+++ b/pypy/jit/backend/x86/codebuf.py
@@ -19,8 +19,8 @@
class MachineCodeBlockWrapper(BlockBuilderMixin,
- codebuilder_cls,
- LocationCodeBuilder):
+ LocationCodeBuilder,
+ codebuilder_cls):
def __init__(self):
self.init_block_builder()
# a list of relative positions; for each position p, the bytes
diff --git a/pypy/jit/backend/x86/regalloc.py b/pypy/jit/backend/x86/regalloc.py
--- a/pypy/jit/backend/x86/regalloc.py
+++ b/pypy/jit/backend/x86/regalloc.py
@@ -168,7 +168,7 @@
def _prepare(self, inputargs, operations, allgcrefs):
self.fm = X86FrameManager()
- self.param_depth = 0
+ self.min_frame_depth = 0
cpu = self.assembler.cpu
operations = cpu.gc_ll_descr.rewrite_assembler(cpu, operations,
allgcrefs)
@@ -193,11 +193,9 @@
self.min_bytes_before_label = 13
return operations
- def prepare_bridge(self, prev_depths, inputargs, arglocs, operations,
- allgcrefs):
+ def prepare_bridge(self, inputargs, arglocs, operations, allgcrefs):
operations = self._prepare(inputargs, operations, allgcrefs)
self._update_bindings(arglocs, inputargs)
- self.param_depth = prev_depths[1]
self.min_bytes_before_label = 0
return operations
@@ -205,8 +203,15 @@
self.min_bytes_before_label = max(self.min_bytes_before_label,
at_least_position)
- def reserve_param(self, n):
- self.param_depth = max(self.param_depth, n)
+ def needed_extra_stack_locations(self, n):
+ # call *after* you needed extra stack locations: (%esp), (%esp+4)...
+ min_frame_depth = self.fm.get_frame_depth() + n
+ if min_frame_depth > self.min_frame_depth:
+ self.min_frame_depth = min_frame_depth
+
+ def get_final_frame_depth(self):
+ self.needed_extra_stack_locations(0) # update min_frame_depth
+ return self.min_frame_depth
def _set_initial_bindings(self, inputargs):
if IS_X86_64:
@@ -376,25 +381,12 @@
def locs_for_fail(self, guard_op):
return [self.loc(v) for v in guard_op.getfailargs()]
- def get_current_depth(self):
- # return (self.fm.frame_depth, self.param_depth), but trying to share
- # the resulting tuple among several calls
- arg0 = self.fm.get_frame_depth()
- arg1 = self.param_depth
- result = self.assembler._current_depths_cache
- if result[0] != arg0 or result[1] != arg1:
- result = (arg0, arg1)
- self.assembler._current_depths_cache = result
- return result
-
def perform_with_guard(self, op, guard_op, arglocs, result_loc):
faillocs = self.locs_for_fail(guard_op)
self.rm.position += 1
self.xrm.position += 1
- current_depths = self.get_current_depth()
self.assembler.regalloc_perform_with_guard(op, guard_op, faillocs,
- arglocs, result_loc,
- current_depths)
+ arglocs, result_loc)
if op.result is not None:
self.possibly_free_var(op.result)
self.possibly_free_vars(guard_op.getfailargs())
@@ -407,10 +399,8 @@
arglocs))
else:
self.assembler.dump('%s(%s)' % (guard_op, arglocs))
- current_depths = self.get_current_depth()
self.assembler.regalloc_perform_guard(guard_op, faillocs, arglocs,
- result_loc,
- current_depths)
+ result_loc)
self.possibly_free_vars(guard_op.getfailargs())
def PerformDiscard(self, op, arglocs):
@@ -776,6 +766,18 @@
consider_cast_singlefloat_to_float = consider_cast_int_to_float
+ def consider_convert_float_bytes_to_longlong(self, op):
+ if longlong.is_64_bit:
+ loc0 = self.xrm.make_sure_var_in_reg(op.getarg(0))
+ loc1 = self.rm.force_allocate_reg(op.result)
+ self.Perform(op, [loc0], loc1)
+ self.xrm.possibly_free_var(op.getarg(0))
+ else:
+ loc0 = self.xrm.loc(op.getarg(0))
+ loc1 = self.xrm.force_allocate_reg(op.result)
+ self.Perform(op, [loc0], loc1)
+ self.xrm.possibly_free_var(op.getarg(0))
+
def _consider_llong_binop_xx(self, op):
# must force both arguments into xmm registers, because we don't
# know if they will be suitably aligned. Exception: if the second
@@ -1393,7 +1395,7 @@
self.force_spill_var(op.getarg(0))
def get_mark_gc_roots(self, gcrootmap, use_copy_area=False):
- shape = gcrootmap.get_basic_shape(IS_X86_64)
+ shape = gcrootmap.get_basic_shape()
for v, val in self.fm.bindings.items():
if (isinstance(v, BoxPtr) and self.rm.stays_alive(v)):
assert isinstance(val, StackLoc)
diff --git a/pypy/jit/backend/x86/rx86.py b/pypy/jit/backend/x86/rx86.py
--- a/pypy/jit/backend/x86/rx86.py
+++ b/pypy/jit/backend/x86/rx86.py
@@ -601,9 +601,10 @@
CVTSS2SD_xb = xmminsn('\xF3', rex_nw, '\x0F\x5A',
register(1, 8), stack_bp(2))
- MOVD_rx = xmminsn('\x66', rex_nw, '\x0F\x7E', register(2, 8), register(1), '\xC0')
- MOVD_xr = xmminsn('\x66', rex_nw, '\x0F\x6E', register(1, 8), register(2), '\xC0')
- MOVD_xb = xmminsn('\x66', rex_nw, '\x0F\x6E', register(1, 8), stack_bp(2))
+ # These work on machine sized registers.
+ MOVD_rx = xmminsn('\x66', rex_w, '\x0F\x7E', register(2, 8), register(1), '\xC0')
+ MOVD_xr = xmminsn('\x66', rex_w, '\x0F\x6E', register(1, 8), register(2), '\xC0')
+ MOVD_xb = xmminsn('\x66', rex_w, '\x0F\x6E', register(1, 8), stack_bp(2))
PSRAD_xi = xmminsn('\x66', rex_nw, '\x0F\x72', register(1), '\xE0', immediate(2, 'b'))
diff --git a/pypy/jit/backend/x86/support.py b/pypy/jit/backend/x86/support.py
--- a/pypy/jit/backend/x86/support.py
+++ b/pypy/jit/backend/x86/support.py
@@ -1,6 +1,7 @@
import sys
from pypy.rpython.lltypesystem import lltype, rffi, llmemory
from pypy.translator.tool.cbuild import ExternalCompilationInfo
+from pypy.jit.backend.x86.arch import WORD
def values_array(TP, size):
@@ -35,10 +36,15 @@
# ____________________________________________________________
-if sys.platform == 'win32':
- ensure_sse2_floats = lambda : None
+if WORD == 4:
+ extra = ['-DPYPY_X86_CHECK_SSE2']
else:
- ensure_sse2_floats = rffi.llexternal_use_eci(ExternalCompilationInfo(
- compile_extra = ['-msse2', '-mfpmath=sse',
- '-DPYPY_CPU_HAS_STANDARD_PRECISION'],
- ))
+ extra = []
+
+if sys.platform != 'win32':
+ extra = ['-msse2', '-mfpmath=sse',
+ '-DPYPY_CPU_HAS_STANDARD_PRECISION'] + extra
+
+ensure_sse2_floats = rffi.llexternal_use_eci(ExternalCompilationInfo(
+ compile_extra = extra,
+))
diff --git a/pypy/jit/backend/x86/test/conftest.py b/pypy/jit/backend/x86/test/conftest.py
--- a/pypy/jit/backend/x86/test/conftest.py
+++ b/pypy/jit/backend/x86/test/conftest.py
@@ -1,4 +1,4 @@
-import py
+import py, os
from pypy.jit.backend import detect_cpu
cpu = detect_cpu.autodetect()
@@ -6,5 +6,7 @@
if cpu not in ('x86', 'x86_64'):
py.test.skip("x86/x86_64 tests skipped: cpu is %r" % (cpu,))
if cpu == 'x86_64':
+ if os.name == "nt":
+ py.test.skip("Windows cannot allocate non-reserved memory")
from pypy.rpython.lltypesystem import ll2ctypes
ll2ctypes.do_allocation_in_far_regions()
diff --git a/pypy/jit/backend/x86/test/test_gc_integration.py b/pypy/jit/backend/x86/test/test_gc_integration.py
--- a/pypy/jit/backend/x86/test/test_gc_integration.py
+++ b/pypy/jit/backend/x86/test/test_gc_integration.py
@@ -28,7 +28,7 @@
class MockGcRootMap(object):
is_shadow_stack = False
- def get_basic_shape(self, is_64_bit):
+ def get_basic_shape(self):
return ['shape']
def add_frame_offset(self, shape, offset):
shape.append(offset)
@@ -184,6 +184,8 @@
self.addrs[1] = self.addrs[0] + 64
self.calls = []
def malloc_slowpath(size):
+ if self.gcrootmap is not None: # hook
+ self.gcrootmap.hook_malloc_slowpath()
self.calls.append(size)
# reset the nursery
nadr = rffi.cast(lltype.Signed, self.nursery)
@@ -257,3 +259,218 @@
assert gc_ll_descr.addrs[0] == nurs_adr + 24
# this should call slow path once
assert gc_ll_descr.calls == [24]
+
+ def test_save_regs_around_malloc(self):
+ S1 = lltype.GcStruct('S1')
+ S2 = lltype.GcStruct('S2', ('s0', lltype.Ptr(S1)),
+ ('s1', lltype.Ptr(S1)),
+ ('s2', lltype.Ptr(S1)),
+ ('s3', lltype.Ptr(S1)),
+ ('s4', lltype.Ptr(S1)),
+ ('s5', lltype.Ptr(S1)),
+ ('s6', lltype.Ptr(S1)),
+ ('s7', lltype.Ptr(S1)),
+ ('s8', lltype.Ptr(S1)),
+ ('s9', lltype.Ptr(S1)),
+ ('s10', lltype.Ptr(S1)),
+ ('s11', lltype.Ptr(S1)),
+ ('s12', lltype.Ptr(S1)),
+ ('s13', lltype.Ptr(S1)),
+ ('s14', lltype.Ptr(S1)),
+ ('s15', lltype.Ptr(S1)))
+ cpu = self.cpu
+ self.namespace = self.namespace.copy()
+ for i in range(16):
+ self.namespace['ds%i' % i] = cpu.fielddescrof(S2, 's%d' % i)
+ ops = '''
+ [p0]
+ p1 = getfield_gc(p0, descr=ds0)
+ p2 = getfield_gc(p0, descr=ds1)
+ p3 = getfield_gc(p0, descr=ds2)
+ p4 = getfield_gc(p0, descr=ds3)
+ p5 = getfield_gc(p0, descr=ds4)
+ p6 = getfield_gc(p0, descr=ds5)
+ p7 = getfield_gc(p0, descr=ds6)
+ p8 = getfield_gc(p0, descr=ds7)
+ p9 = getfield_gc(p0, descr=ds8)
+ p10 = getfield_gc(p0, descr=ds9)
+ p11 = getfield_gc(p0, descr=ds10)
+ p12 = getfield_gc(p0, descr=ds11)
+ p13 = getfield_gc(p0, descr=ds12)
+ p14 = getfield_gc(p0, descr=ds13)
+ p15 = getfield_gc(p0, descr=ds14)
+ p16 = getfield_gc(p0, descr=ds15)
+ #
+ # now all registers are in use
+ p17 = call_malloc_nursery(40)
+ p18 = call_malloc_nursery(40) # overflow
+ #
+ finish(p1, p2, p3, p4, p5, p6, p7, p8, \
+ p9, p10, p11, p12, p13, p14, p15, p16)
+ '''
+ s2 = lltype.malloc(S2)
+ for i in range(16):
+ setattr(s2, 's%d' % i, lltype.malloc(S1))
+ s2ref = lltype.cast_opaque_ptr(llmemory.GCREF, s2)
+ #
+ self.interpret(ops, [s2ref])
+ gc_ll_descr = cpu.gc_ll_descr
+ gc_ll_descr.check_nothing_in_nursery()
+ assert gc_ll_descr.calls == [40]
+ # check the returned pointers
+ for i in range(16):
+ s1ref = self.cpu.get_latest_value_ref(i)
+ s1 = lltype.cast_opaque_ptr(lltype.Ptr(S1), s1ref)
+ assert s1 == getattr(s2, 's%d' % i)
+
+
+class MockShadowStackRootMap(MockGcRootMap):
+ is_shadow_stack = True
+ MARKER_FRAME = 88 # this marker follows the frame addr
+ S1 = lltype.GcStruct('S1')
+
+ def __init__(self):
+ self.addrs = lltype.malloc(rffi.CArray(lltype.Signed), 20,
+ flavor='raw')
+ # root_stack_top
+ self.addrs[0] = rffi.cast(lltype.Signed, self.addrs) + 3*WORD
+ # random stuff
+ self.addrs[1] = 123456
+ self.addrs[2] = 654321
+ self.check_initial_and_final_state()
+ self.callshapes = {}
+ self.should_see = []
+
+ def check_initial_and_final_state(self):
+ assert self.addrs[0] == rffi.cast(lltype.Signed, self.addrs) + 3*WORD
+ assert self.addrs[1] == 123456
+ assert self.addrs[2] == 654321
+
+ def get_root_stack_top_addr(self):
+ return rffi.cast(lltype.Signed, self.addrs)
+
+ def compress_callshape(self, shape, datablockwrapper):
+ assert shape[0] == 'shape'
+ return ['compressed'] + shape[1:]
+
+ def write_callshape(self, mark, force_index):
+ assert mark[0] == 'compressed'
+ assert force_index not in self.callshapes
+ assert force_index == 42 + len(self.callshapes)
+ self.callshapes[force_index] = mark
+
+ def hook_malloc_slowpath(self):
+ num_entries = self.addrs[0] - rffi.cast(lltype.Signed, self.addrs)
+ assert num_entries == 5*WORD # 3 initially, plus 2 by the asm frame
+ assert self.addrs[1] == 123456 # unchanged
+ assert self.addrs[2] == 654321 # unchanged
+ frame_addr = self.addrs[3] # pushed by the asm frame
+ assert self.addrs[4] == self.MARKER_FRAME # pushed by the asm frame
+ #
+ from pypy.jit.backend.x86.arch import FORCE_INDEX_OFS
+ addr = rffi.cast(rffi.CArrayPtr(lltype.Signed),
+ frame_addr + FORCE_INDEX_OFS)
+ force_index = addr[0]
+ assert force_index == 43 # in this test: the 2nd call_malloc_nursery
+ #
+ # The callshapes[43] saved above should list addresses both in the
+ # COPY_AREA and in the "normal" stack, where all the 16 values p1-p16
+ # of test_save_regs_at_correct_place should have been stored. Here
+ # we replace them with new addresses, to emulate a moving GC.
+ shape = self.callshapes[force_index]
+ assert len(shape[1:]) == len(self.should_see)
+ new_objects = [None] * len(self.should_see)
+ for ofs in shape[1:]:
+ assert isinstance(ofs, int) # not a register at all here
+ addr = rffi.cast(rffi.CArrayPtr(lltype.Signed), frame_addr + ofs)
+ contains = addr[0]
+ for j in range(len(self.should_see)):
+ obj = self.should_see[j]
+ if contains == rffi.cast(lltype.Signed, obj):
+ assert new_objects[j] is None # duplicate?
+ break
+ else:
+ assert 0 # the value read from the stack looks random?
+ new_objects[j] = lltype.malloc(self.S1)
+ addr[0] = rffi.cast(lltype.Signed, new_objects[j])
+ self.should_see[:] = new_objects
+
+
+class TestMallocShadowStack(BaseTestRegalloc):
+
+ def setup_method(self, method):
+ cpu = CPU(None, None)
+ cpu.gc_ll_descr = GCDescrFastpathMalloc()
+ cpu.gc_ll_descr.gcrootmap = MockShadowStackRootMap()
+ cpu.setup_once()
+ for i in range(42):
+ cpu.reserve_some_free_fail_descr_number()
+ self.cpu = cpu
+
+ def test_save_regs_at_correct_place(self):
+ cpu = self.cpu
+ gc_ll_descr = cpu.gc_ll_descr
+ S1 = gc_ll_descr.gcrootmap.S1
+ S2 = lltype.GcStruct('S2', ('s0', lltype.Ptr(S1)),
+ ('s1', lltype.Ptr(S1)),
+ ('s2', lltype.Ptr(S1)),
+ ('s3', lltype.Ptr(S1)),
+ ('s4', lltype.Ptr(S1)),
+ ('s5', lltype.Ptr(S1)),
+ ('s6', lltype.Ptr(S1)),
+ ('s7', lltype.Ptr(S1)),
+ ('s8', lltype.Ptr(S1)),
+ ('s9', lltype.Ptr(S1)),
+ ('s10', lltype.Ptr(S1)),
+ ('s11', lltype.Ptr(S1)),
+ ('s12', lltype.Ptr(S1)),
+ ('s13', lltype.Ptr(S1)),
+ ('s14', lltype.Ptr(S1)),
+ ('s15', lltype.Ptr(S1)))
+ self.namespace = self.namespace.copy()
+ for i in range(16):
+ self.namespace['ds%i' % i] = cpu.fielddescrof(S2, 's%d' % i)
+ ops = '''
+ [p0]
+ p1 = getfield_gc(p0, descr=ds0)
+ p2 = getfield_gc(p0, descr=ds1)
+ p3 = getfield_gc(p0, descr=ds2)
+ p4 = getfield_gc(p0, descr=ds3)
+ p5 = getfield_gc(p0, descr=ds4)
+ p6 = getfield_gc(p0, descr=ds5)
+ p7 = getfield_gc(p0, descr=ds6)
+ p8 = getfield_gc(p0, descr=ds7)
+ p9 = getfield_gc(p0, descr=ds8)
+ p10 = getfield_gc(p0, descr=ds9)
+ p11 = getfield_gc(p0, descr=ds10)
+ p12 = getfield_gc(p0, descr=ds11)
+ p13 = getfield_gc(p0, descr=ds12)
+ p14 = getfield_gc(p0, descr=ds13)
+ p15 = getfield_gc(p0, descr=ds14)
+ p16 = getfield_gc(p0, descr=ds15)
+ #
+ # now all registers are in use
+ p17 = call_malloc_nursery(40)
+ p18 = call_malloc_nursery(40) # overflow
+ #
+ finish(p1, p2, p3, p4, p5, p6, p7, p8, \
+ p9, p10, p11, p12, p13, p14, p15, p16)
+ '''
+ s2 = lltype.malloc(S2)
+ for i in range(16):
+ s1 = lltype.malloc(S1)
+ setattr(s2, 's%d' % i, s1)
+ gc_ll_descr.gcrootmap.should_see.append(s1)
+ s2ref = lltype.cast_opaque_ptr(llmemory.GCREF, s2)
+ #
+ self.interpret(ops, [s2ref])
+ gc_ll_descr.check_nothing_in_nursery()
+ assert gc_ll_descr.calls == [40]
+ gc_ll_descr.gcrootmap.check_initial_and_final_state()
+ # check the returned pointers
+ for i in range(16):
+ s1ref = self.cpu.get_latest_value_ref(i)
+ s1 = lltype.cast_opaque_ptr(lltype.Ptr(S1), s1ref)
+ for j in range(16):
+ assert s1 != getattr(s2, 's%d' % j)
+ assert s1 == gc_ll_descr.gcrootmap.should_see[i]
diff --git a/pypy/jit/backend/x86/test/test_recompilation.py b/pypy/jit/backend/x86/test/test_recompilation.py
--- a/pypy/jit/backend/x86/test/test_recompilation.py
+++ b/pypy/jit/backend/x86/test/test_recompilation.py
@@ -34,7 +34,6 @@
'''
loop = self.interpret(ops, [0])
previous = loop._jitcelltoken.compiled_loop_token.frame_depth
- assert loop._jitcelltoken.compiled_loop_token.param_depth == 0
assert self.getint(0) == 20
ops = '''
[i1]
@@ -51,7 +50,6 @@
bridge = self.attach_bridge(ops, loop, -2)
descr = loop.operations[3].getdescr()
new = descr._x86_bridge_frame_depth
- assert descr._x86_bridge_param_depth == 0
# the force_spill() forces the stack to grow
assert new > previous
fail = self.run(loop, 0)
@@ -116,10 +114,8 @@
loop_frame_depth = loop._jitcelltoken.compiled_loop_token.frame_depth
bridge = self.attach_bridge(ops, loop, 6)
guard_op = loop.operations[6]
- assert loop._jitcelltoken.compiled_loop_token.param_depth == 0
# the force_spill() forces the stack to grow
assert guard_op.getdescr()._x86_bridge_frame_depth > loop_frame_depth
- assert guard_op.getdescr()._x86_bridge_param_depth == 0
self.run(loop, 0, 0, 0, 0, 0, 0)
assert self.getint(0) == 1
assert self.getint(1) == 20
diff --git a/pypy/jit/backend/x86/test/test_regalloc.py b/pypy/jit/backend/x86/test/test_regalloc.py
--- a/pypy/jit/backend/x86/test/test_regalloc.py
+++ b/pypy/jit/backend/x86/test/test_regalloc.py
@@ -606,23 +606,37 @@
assert self.getints(9) == [0, 1, 1, 1, 1, 1, 1, 1, 1]
class TestRegAllocCallAndStackDepth(BaseTestRegalloc):
- def expected_param_depth(self, num_args):
+ def expected_frame_depth(self, num_call_args, num_pushed_input_args=0):
# Assumes the arguments are all non-float
if IS_X86_32:
- return num_args
+ extra_esp = num_call_args
+ return extra_esp
elif IS_X86_64:
- return max(num_args - 6, 0)
+ # 'num_pushed_input_args' is for X86_64 only
+ extra_esp = max(num_call_args - 6, 0)
+ return num_pushed_input_args + extra_esp
def test_one_call(self):
ops = '''
- [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]
+ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i9b]
i10 = call(ConstClass(f1ptr), i0, descr=f1_calldescr)
- finish(i10, i1, i2, i3, i4, i5, i6, i7, i8, i9)
+ finish(i10, i1, i2, i3, i4, i5, i6, i7, i8, i9, i9b)
'''
- loop = self.interpret(ops, [4, 7, 9, 9 ,9, 9, 9, 9, 9, 9])
- assert self.getints(10) == [5, 7, 9, 9, 9, 9, 9, 9, 9, 9]
+ loop = self.interpret(ops, [4, 7, 9, 9 ,9, 9, 9, 9, 9, 9, 8])
+ assert self.getints(11) == [5, 7, 9, 9, 9, 9, 9, 9, 9, 9, 8]
clt = loop._jitcelltoken.compiled_loop_token
- assert clt.param_depth == self.expected_param_depth(1)
+ assert clt.frame_depth == self.expected_frame_depth(1, 5)
+
+ def test_one_call_reverse(self):
+ ops = '''
+ [i1, i2, i3, i4, i5, i6, i7, i8, i9, i9b, i0]
+ i10 = call(ConstClass(f1ptr), i0, descr=f1_calldescr)
+ finish(i10, i1, i2, i3, i4, i5, i6, i7, i8, i9, i9b)
+ '''
+ loop = self.interpret(ops, [7, 9, 9 ,9, 9, 9, 9, 9, 9, 8, 4])
+ assert self.getints(11) == [5, 7, 9, 9, 9, 9, 9, 9, 9, 9, 8]
+ clt = loop._jitcelltoken.compiled_loop_token
+ assert clt.frame_depth == self.expected_frame_depth(1, 6)
def test_two_calls(self):
ops = '''
@@ -634,7 +648,7 @@
loop = self.interpret(ops, [4, 7, 9, 9 ,9, 9, 9, 9, 9, 9])
assert self.getints(10) == [5*7, 7, 9, 9, 9, 9, 9, 9, 9, 9]
clt = loop._jitcelltoken.compiled_loop_token
- assert clt.param_depth == self.expected_param_depth(2)
+ assert clt.frame_depth == self.expected_frame_depth(2, 5)
def test_call_many_arguments(self):
# NB: The first and last arguments in the call are constants. This
@@ -648,25 +662,31 @@
loop = self.interpret(ops, [2, 3, 4, 5, 6, 7, 8, 9])
assert self.getint(0) == 55
clt = loop._jitcelltoken.compiled_loop_token
- assert clt.param_depth == self.expected_param_depth(10)
+ assert clt.frame_depth == self.expected_frame_depth(10)
def test_bridge_calls_1(self):
ops = '''
[i0, i1]
i2 = call(ConstClass(f1ptr), i0, descr=f1_calldescr)
- guard_value(i2, 0, descr=fdescr1) [i2, i1]
+ guard_value(i2, 0, descr=fdescr1) [i2, i0, i1]
finish(i1)
'''
loop = self.interpret(ops, [4, 7])
assert self.getint(0) == 5
+ clt = loop._jitcelltoken.compiled_loop_token
+ orgdepth = clt.frame_depth
+ assert orgdepth == self.expected_frame_depth(1, 2)
+
ops = '''
- [i2, i1]
+ [i2, i0, i1]
i3 = call(ConstClass(f2ptr), i2, i1, descr=f2_calldescr)
- finish(i3, descr=fdescr2)
+ finish(i3, i0, descr=fdescr2)
'''
bridge = self.attach_bridge(ops, loop, -2)
- assert loop.operations[-2].getdescr()._x86_bridge_param_depth == self.expected_param_depth(2)
+ assert clt.frame_depth == max(orgdepth, self.expected_frame_depth(2, 2))
+ assert loop.operations[-2].getdescr()._x86_bridge_frame_depth == \
+ self.expected_frame_depth(2, 2)
self.run(loop, 4, 7)
assert self.getint(0) == 5*7
@@ -676,10 +696,14 @@
[i0, i1]
i2 = call(ConstClass(f2ptr), i0, i1, descr=f2_calldescr)
guard_value(i2, 0, descr=fdescr1) [i2]
- finish(i1)
+ finish(i2)
'''
loop = self.interpret(ops, [4, 7])
assert self.getint(0) == 4*7
+ clt = loop._jitcelltoken.compiled_loop_token
+ orgdepth = clt.frame_depth
+ assert orgdepth == self.expected_frame_depth(2)
+
ops = '''
[i2]
i3 = call(ConstClass(f1ptr), i2, descr=f1_calldescr)
@@ -687,7 +711,9 @@
'''
bridge = self.attach_bridge(ops, loop, -2)
- assert loop.operations[-2].getdescr()._x86_bridge_param_depth == self.expected_param_depth(2)
+ assert clt.frame_depth == max(orgdepth, self.expected_frame_depth(1))
+ assert loop.operations[-2].getdescr()._x86_bridge_frame_depth == \
+ self.expected_frame_depth(1)
self.run(loop, 4, 7)
assert self.getint(0) == 29
diff --git a/pypy/jit/backend/x86/test/test_runner.py b/pypy/jit/backend/x86/test/test_runner.py
--- a/pypy/jit/backend/x86/test/test_runner.py
+++ b/pypy/jit/backend/x86/test/test_runner.py
@@ -371,7 +371,7 @@
operations = [
ResOperation(rop.LABEL, [i0], None, descr=targettoken),
- ResOperation(rop.DEBUG_MERGE_POINT, [FakeString("hello"), 0], None),
+ ResOperation(rop.DEBUG_MERGE_POINT, [FakeString("hello"), 0, 0], None),
ResOperation(rop.INT_ADD, [i0, ConstInt(1)], i1),
ResOperation(rop.INT_LE, [i1, ConstInt(9)], i2),
ResOperation(rop.GUARD_TRUE, [i2], None, descr=faildescr1),
@@ -390,7 +390,7 @@
bridge = [
ResOperation(rop.INT_LE, [i1b, ConstInt(19)], i3),
ResOperation(rop.GUARD_TRUE, [i3], None, descr=faildescr2),
- ResOperation(rop.DEBUG_MERGE_POINT, [FakeString("bye"), 0], None),
+ ResOperation(rop.DEBUG_MERGE_POINT, [FakeString("bye"), 0, 0], None),
ResOperation(rop.JUMP, [i1b], None, descr=targettoken),
]
bridge[1].setfailargs([i1b])
@@ -531,12 +531,12 @@
loop = """
[i0]
label(i0, descr=preambletoken)
- debug_merge_point('xyz', 0)
+ debug_merge_point('xyz', 0, 0)
i1 = int_add(i0, 1)
i2 = int_ge(i1, 10)
guard_false(i2) []
label(i1, descr=targettoken)
- debug_merge_point('xyz', 0)
+ debug_merge_point('xyz', 0, 0)
i11 = int_add(i1, 1)
i12 = int_ge(i11, 10)
guard_false(i12) []
@@ -569,7 +569,7 @@
loop = """
[i0]
label(i0, descr=targettoken)
- debug_merge_point('xyz', 0)
+ debug_merge_point('xyz', 0, 0)
i1 = int_add(i0, 1)
i2 = int_ge(i1, 10)
guard_false(i2) []
diff --git a/pypy/jit/backend/x86/test/test_zmath.py b/pypy/jit/backend/x86/test/test_zmath.py
--- a/pypy/jit/backend/x86/test/test_zmath.py
+++ b/pypy/jit/backend/x86/test/test_zmath.py
@@ -6,6 +6,8 @@
from pypy.translator.c.test.test_genc import compile
from pypy.jit.backend.x86.support import ensure_sse2_floats
from pypy.rlib import rfloat
+from pypy.rlib.unroll import unrolling_iterable
+from pypy.rlib.debug import debug_print
def get_test_case((fnname, args, expected)):
@@ -16,16 +18,32 @@
expect_valueerror = (expected == ValueError)
expect_overflowerror = (expected == OverflowError)
check = test_direct.get_tester(expected)
+ unroll_args = unrolling_iterable(args)
#
def testfn():
+ debug_print('calling', fnname, 'with arguments:')
+ for arg in unroll_args:
+ debug_print('\t', arg)
try:
got = fn(*args)
except ValueError:
- return expect_valueerror
+ if expect_valueerror:
+ return True
+ else:
+ debug_print('unexpected ValueError!')
+ return False
except OverflowError:
- return expect_overflowerror
+ if expect_overflowerror:
+ return True
+ else:
+ debug_print('unexpected OverflowError!')
+ return False
else:
- return check(got)
+ if check(got):
+ return True
+ else:
+ debug_print('unexpected result:', got)
+ return False
#
testfn.func_name = 'test_' + fnname
return testfn
diff --git a/pypy/jit/backend/x86/test/test_ztranslation.py b/pypy/jit/backend/x86/test/test_ztranslation.py
--- a/pypy/jit/backend/x86/test/test_ztranslation.py
+++ b/pypy/jit/backend/x86/test/test_ztranslation.py
@@ -52,6 +52,7 @@
set_param(jitdriver, "trace_eagerness", 2)
total = 0
frame = Frame(i)
+ j = float(j)
while frame.i > 3:
jitdriver.can_enter_jit(frame=frame, total=total, j=j)
jitdriver.jit_merge_point(frame=frame, total=total, j=j)
diff --git a/pypy/jit/backend/x86/tool/viewcode.py b/pypy/jit/backend/x86/tool/viewcode.py
--- a/pypy/jit/backend/x86/tool/viewcode.py
+++ b/pypy/jit/backend/x86/tool/viewcode.py
@@ -34,7 +34,7 @@
# I am porting it in a lazy fashion... See py-utils/xam.py
if sys.platform == "win32":
- XXX # lots more in Psyco
+ pass # lots more in Psyco
def machine_code_dump(data, originaddr, backend_name, label_list=None):
objdump_backend_option = {
diff --git a/pypy/jit/codewriter/jtransform.py b/pypy/jit/codewriter/jtransform.py
--- a/pypy/jit/codewriter/jtransform.py
+++ b/pypy/jit/codewriter/jtransform.py
@@ -291,6 +291,11 @@
op1 = SpaceOperation('-live-', [], None)
return [op, op1]
+ def _noop_rewrite(self, op):
+ return op
+
+ rewrite_op_convert_float_bytes_to_longlong = _noop_rewrite
+
# ----------
# Various kinds of calls
@@ -365,7 +370,7 @@
def handle_builtin_call(self, op):
oopspec_name, args = support.decode_builtin_call(op)
# dispatch to various implementations depending on the oopspec_name
- if oopspec_name.startswith('list.') or oopspec_name == 'newlist':
+ if oopspec_name.startswith('list.') or oopspec_name.startswith('newlist'):
prepare = self._handle_list_call
elif oopspec_name.startswith('stroruni.'):
prepare = self._handle_stroruni_call
@@ -1494,6 +1499,14 @@
arraydescr, v_length],
op.result)
+ def do_resizable_newlist_hint(self, op, args, arraydescr, lengthdescr,
+ itemsdescr, structdescr):
+ v_hint = self._get_initial_newlist_length(op, args)
+ return SpaceOperation('newlist_hint',
+ [structdescr, lengthdescr, itemsdescr,
+ arraydescr, v_hint],
+ op.result)
+
def do_resizable_list_getitem(self, op, args, arraydescr, lengthdescr,
itemsdescr, structdescr):
v_index, extraop = self._prepare_list_getset(op, lengthdescr, args,
diff --git a/pypy/jit/codewriter/support.py b/pypy/jit/codewriter/support.py
--- a/pypy/jit/codewriter/support.py
+++ b/pypy/jit/codewriter/support.py
@@ -144,6 +144,10 @@
_ll_1_newlist.need_result_type = True
_ll_2_newlist.need_result_type = True
+def _ll_1_newlist_hint(LIST, hint):
+ return LIST.ll_newlist_hint(hint)
+_ll_1_newlist_hint.need_result_type = True
+
def _ll_1_list_len(l):
return l.ll_length()
def _ll_2_list_getitem(l, index):
diff --git a/pypy/jit/codewriter/test/test_flatten.py b/pypy/jit/codewriter/test/test_flatten.py
--- a/pypy/jit/codewriter/test/test_flatten.py
+++ b/pypy/jit/codewriter/test/test_flatten.py
@@ -968,6 +968,21 @@
int_return %i2
""", transform=True)
+ def test_convert_float_bytes_to_int(self):
+ from pypy.rlib.longlong2float import float2longlong
+ def f(x):
+ return float2longlong(x)
+ if longlong.is_64_bit:
+ result_var = "%i0"
+ return_op = "int_return"
+ else:
+ result_var = "%f1"
+ return_op = "float_return"
+ self.encoding_test(f, [25.0], """
+ convert_float_bytes_to_longlong %%f0 -> %(result_var)s
+ %(return_op)s %(result_var)s
+ """ % {"result_var": result_var, "return_op": return_op})
+
def check_force_cast(FROM, TO, operations, value):
"""Check that the test is correctly written..."""
diff --git a/pypy/jit/codewriter/test/test_longlong.py b/pypy/jit/codewriter/test/test_longlong.py
--- a/pypy/jit/codewriter/test/test_longlong.py
+++ b/pypy/jit/codewriter/test/test_longlong.py
@@ -1,6 +1,6 @@
import py, sys
-from pypy.rlib.rarithmetic import r_longlong, intmask
+from pypy.rlib.rarithmetic import r_longlong, intmask, is_valid_int
from pypy.objspace.flow.model import SpaceOperation, Variable, Constant
from pypy.objspace.flow.model import Block, Link
from pypy.translator.unsimplify import varoftype
@@ -32,7 +32,7 @@
def test_functions():
xll = longlong.getfloatstorage(3.5)
assert longlong.getrealfloat(xll) == 3.5
- assert isinstance(longlong.gethash(xll), int)
+ assert is_valid_int(longlong.gethash(xll))
class TestLongLong:
diff --git a/pypy/jit/metainterp/blackhole.py b/pypy/jit/metainterp/blackhole.py
--- a/pypy/jit/metainterp/blackhole.py
+++ b/pypy/jit/metainterp/blackhole.py
@@ -1,15 +1,16 @@
+from pypy.jit.codewriter import heaptracker, longlong
+from pypy.jit.codewriter.jitcode import JitCode, SwitchDictDescr
+from pypy.jit.metainterp.compile import ResumeAtPositionDescr
+from pypy.jit.metainterp.jitexc import JitException, get_llexception, reraise
+from pypy.rlib import longlong2float
+from pypy.rlib.debug import debug_start, debug_stop, ll_assert, make_sure_not_resized
+from pypy.rlib.objectmodel import we_are_translated
+from pypy.rlib.rarithmetic import intmask, LONG_BIT, r_uint, ovfcheck
+from pypy.rlib.rtimer import read_timestamp
from pypy.rlib.unroll import unrolling_iterable
-from pypy.rlib.rtimer import read_timestamp
-from pypy.rlib.rarithmetic import intmask, LONG_BIT, r_uint, ovfcheck
-from pypy.rlib.objectmodel import we_are_translated
-from pypy.rlib.debug import debug_start, debug_stop, ll_assert
-from pypy.rlib.debug import make_sure_not_resized
from pypy.rpython.lltypesystem import lltype, llmemory, rclass
from pypy.rpython.lltypesystem.lloperation import llop
-from pypy.jit.codewriter.jitcode import JitCode, SwitchDictDescr
-from pypy.jit.codewriter import heaptracker, longlong
-from pypy.jit.metainterp.jitexc import JitException, get_llexception, reraise
-from pypy.jit.metainterp.compile import ResumeAtPositionDescr
+
def arguments(*argtypes, **kwds):
resulttype = kwds.pop('returns', None)
@@ -20,6 +21,9 @@
return function
return decorate
+LONGLONG_TYPECODE = 'i' if longlong.is_64_bit else 'f'
+
+
class LeaveFrame(JitException):
pass
@@ -663,6 +667,11 @@
a = float(a)
return longlong.getfloatstorage(a)
+ @arguments("f", returns=LONGLONG_TYPECODE)
+ def bhimpl_convert_float_bytes_to_longlong(a):
+ a = longlong.getrealfloat(a)
+ return longlong2float.float2longlong(a)
+
# ----------
# control flow operations
@@ -982,6 +991,15 @@
cpu.bh_setfield_gc_r(result, itemsdescr, items)
return result
+ @arguments("cpu", "d", "d", "d", "d", "i", returns="r")
+ def bhimpl_newlist_hint(cpu, structdescr, lengthdescr, itemsdescr,
+ arraydescr, lengthhint):
+ result = cpu.bh_new(structdescr)
+ cpu.bh_setfield_gc_i(result, lengthdescr, 0)
+ items = cpu.bh_new_array(arraydescr, lengthhint)
+ cpu.bh_setfield_gc_r(result, itemsdescr, items)
+ return result
+
@arguments("cpu", "r", "d", "d", "i", returns="i")
def bhimpl_getlistitem_gc_i(cpu, lst, itemsdescr, arraydescr, index):
items = cpu.bh_getfield_gc_r(lst, itemsdescr)
@@ -1176,14 +1194,14 @@
def bhimpl_getinteriorfield_gc_f(cpu, array, index, descr):
return cpu.bh_getinteriorfield_gc_f(array, index, descr)
- @arguments("cpu", "r", "i", "d", "i")
- def bhimpl_setinteriorfield_gc_i(cpu, array, index, descr, value):
+ @arguments("cpu", "r", "i", "i", "d")
+ def bhimpl_setinteriorfield_gc_i(cpu, array, index, value, descr):
cpu.bh_setinteriorfield_gc_i(array, index, descr, value)
- @arguments("cpu", "r", "i", "d", "r")
- def bhimpl_setinteriorfield_gc_r(cpu, array, index, descr, value):
+ @arguments("cpu", "r", "i", "r", "d")
+ def bhimpl_setinteriorfield_gc_r(cpu, array, index, value, descr):
cpu.bh_setinteriorfield_gc_r(array, index, descr, value)
- @arguments("cpu", "r", "i", "d", "f")
- def bhimpl_setinteriorfield_gc_f(cpu, array, index, descr, value):
+ @arguments("cpu", "r", "i", "f", "d")
+ def bhimpl_setinteriorfield_gc_f(cpu, array, index, value, descr):
cpu.bh_setinteriorfield_gc_f(array, index, descr, value)
@arguments("cpu", "r", "d", returns="i")
@@ -1300,7 +1318,7 @@
def bhimpl_copyunicodecontent(cpu, src, dst, srcstart, dststart, length):
cpu.bh_copyunicodecontent(src, dst, srcstart, dststart, length)
- @arguments(returns=(longlong.is_64_bit and "i" or "f"))
+ @arguments(returns=LONGLONG_TYPECODE)
def bhimpl_ll_read_timestamp():
return read_timestamp()
@@ -1379,7 +1397,8 @@
elif opnum == rop.GUARD_NO_OVERFLOW:
# Produced by int_xxx_ovf(). The pc is just after the opcode.
# We get here because it did not used to overflow, but now it does.
- return get_llexception(self.cpu, OverflowError())
+ if not dont_change_position:
+ return get_llexception(self.cpu, OverflowError())
#
elif opnum == rop.GUARD_OVERFLOW:
# Produced by int_xxx_ovf(). The pc is just after the opcode.
diff --git a/pypy/jit/metainterp/compile.py b/pypy/jit/metainterp/compile.py
--- a/pypy/jit/metainterp/compile.py
+++ b/pypy/jit/metainterp/compile.py
@@ -289,8 +289,21 @@
assert isinstance(token, TargetToken)
assert token.original_jitcell_token is None
token.original_jitcell_token = trace.original_jitcell_token
-
-
+
+
+def do_compile_loop(metainterp_sd, inputargs, operations, looptoken,
+ log=True, name=''):
+ metainterp_sd.logger_ops.log_loop(inputargs, operations, -2,
+ 'compiling', name=name)
+ return metainterp_sd.cpu.compile_loop(inputargs, operations, looptoken,
+ log=log, name=name)
+
+def do_compile_bridge(metainterp_sd, faildescr, inputargs, operations,
+ original_loop_token, log=True):
+ metainterp_sd.logger_ops.log_bridge(inputargs, operations, -2)
+ return metainterp_sd.cpu.compile_bridge(faildescr, inputargs, operations,
+ original_loop_token, log=log)
+
def send_loop_to_backend(greenkey, jitdriver_sd, metainterp_sd, loop, type):
vinfo = jitdriver_sd.virtualizable_info
if vinfo is not None:
@@ -319,9 +332,9 @@
metainterp_sd.profiler.start_backend()
debug_start("jit-backend")
try:
- asminfo = metainterp_sd.cpu.compile_loop(loop.inputargs, operations,
- original_jitcell_token,
- name=loopname)
+ asminfo = do_compile_loop(metainterp_sd, loop.inputargs,
+ operations, original_jitcell_token,
+ name=loopname)
finally:
debug_stop("jit-backend")
metainterp_sd.profiler.end_backend()
@@ -333,7 +346,6 @@
metainterp_sd.stats.compiled()
metainterp_sd.log("compiled new " + type)
#
- loopname = jitdriver_sd.warmstate.get_location_str(greenkey)
if asminfo is not None:
ops_offset = asminfo.ops_offset
else:
@@ -365,9 +377,9 @@
metainterp_sd.profiler.start_backend()
debug_start("jit-backend")
try:
- asminfo = metainterp_sd.cpu.compile_bridge(faildescr, inputargs,
- operations,
- original_loop_token)
+ asminfo = do_compile_bridge(metainterp_sd, faildescr, inputargs,
+ operations,
+ original_loop_token)
finally:
debug_stop("jit-backend")
metainterp_sd.profiler.end_backend()
diff --git a/pypy/jit/metainterp/executor.py b/pypy/jit/metainterp/executor.py
--- a/pypy/jit/metainterp/executor.py
+++ b/pypy/jit/metainterp/executor.py
@@ -2,7 +2,7 @@
"""
from pypy.rpython.lltypesystem import lltype, rstr
-from pypy.rlib.rarithmetic import ovfcheck, r_longlong
+from pypy.rlib.rarithmetic import ovfcheck, r_longlong, is_valid_int
from pypy.rlib.rtimer import read_timestamp
from pypy.rlib.unroll import unrolling_iterable
from pypy.jit.metainterp.history import BoxInt, BoxPtr, BoxFloat, check_descr
@@ -248,7 +248,7 @@
def do_read_timestamp(cpu, _):
x = read_timestamp()
if longlong.is_64_bit:
- assert isinstance(x, int) # 64-bit
+ assert is_valid_int(x) # 64-bit
return BoxInt(x)
else:
assert isinstance(x, r_longlong) # 32-bit
diff --git a/pypy/jit/metainterp/graphpage.py b/pypy/jit/metainterp/graphpage.py
--- a/pypy/jit/metainterp/graphpage.py
+++ b/pypy/jit/metainterp/graphpage.py
@@ -169,9 +169,9 @@
if op.getopnum() == rop.DEBUG_MERGE_POINT:
jd_sd = self.metainterp_sd.jitdrivers_sd[op.getarg(0).getint()]
if jd_sd._get_printable_location_ptr:
- s = jd_sd.warmstate.get_location_str(op.getarglist()[2:])
+ s = jd_sd.warmstate.get_location_str(op.getarglist()[3:])
s = s.replace(',', '.') # we use comma for argument splitting
- op_repr = "debug_merge_point(%d, '%s')" % (op.getarg(1).getint(), s)
+ op_repr = "debug_merge_point(%d, %d, '%s')" % (op.getarg(1).getint(), op.getarg(2).getint(), s)
lines.append(op_repr)
if is_interesting_guard(op):
tgt = op.getdescr()._debug_suboperations[0]
diff --git a/pypy/jit/metainterp/history.py b/pypy/jit/metainterp/history.py
--- a/pypy/jit/metainterp/history.py
+++ b/pypy/jit/metainterp/history.py
@@ -4,7 +4,8 @@
from pypy.rpython.ootypesystem import ootype
from pypy.rlib.objectmodel import we_are_translated, Symbolic
from pypy.rlib.objectmodel import compute_unique_id
-from pypy.rlib.rarithmetic import r_int64
+from pypy.rlib.rarithmetic import r_int64, is_valid_int
+
from pypy.conftest import option
from pypy.jit.metainterp.resoperation import ResOperation, rop
@@ -213,7 +214,7 @@
def __init__(self, value):
if not we_are_translated():
- if isinstance(value, int):
+ if is_valid_int(value):
value = int(value) # bool -> int
else:
assert isinstance(value, Symbolic)
@@ -448,7 +449,7 @@
def __init__(self, value=0):
if not we_are_translated():
- if isinstance(value, int):
+ if is_valid_int(value):
value = int(value) # bool -> int
else:
assert isinstance(value, Symbolic)
diff --git a/pypy/jit/metainterp/logger.py b/pypy/jit/metainterp/logger.py
--- a/pypy/jit/metainterp/logger.py
+++ b/pypy/jit/metainterp/logger.py
@@ -18,6 +18,10 @@
debug_start("jit-log-noopt-loop")
logops = self._log_operations(inputargs, operations, ops_offset)
debug_stop("jit-log-noopt-loop")
+ elif number == -2:
+ debug_start("jit-log-compiling-loop")
+ logops = self._log_operations(inputargs, operations, ops_offset)
+ debug_stop("jit-log-compiling-loop")
else:
debug_start("jit-log-opt-loop")
debug_print("# Loop", number, '(%s)' % name , ":", type,
@@ -31,6 +35,10 @@
debug_start("jit-log-noopt-bridge")
logops = self._log_operations(inputargs, operations, ops_offset)
debug_stop("jit-log-noopt-bridge")
+ elif number == -2:
+ debug_start("jit-log-compiling-bridge")
+ logops = self._log_operations(inputargs, operations, ops_offset)
+ debug_stop("jit-log-compiling-bridge")
else:
debug_start("jit-log-opt-bridge")
debug_print("# bridge out of Guard", number,
@@ -102,9 +110,9 @@
def repr_of_resop(self, op, ops_offset=None):
if op.getopnum() == rop.DEBUG_MERGE_POINT:
jd_sd = self.metainterp_sd.jitdrivers_sd[op.getarg(0).getint()]
- s = jd_sd.warmstate.get_location_str(op.getarglist()[2:])
+ s = jd_sd.warmstate.get_location_str(op.getarglist()[3:])
s = s.replace(',', '.') # we use comma for argument splitting
- return "debug_merge_point(%d, '%s')" % (op.getarg(1).getint(), s)
+ return "debug_merge_point(%d, %d, '%s')" % (op.getarg(1).getint(), op.getarg(2).getint(), s)
if ops_offset is None:
offset = -1
else:
@@ -141,7 +149,7 @@
if target_token.exported_state:
for op in target_token.exported_state.inputarg_setup_ops:
debug_print(' ' + self.repr_of_resop(op))
-
+
def _log_operations(self, inputargs, operations, ops_offset):
if not have_debug_prints():
return
diff --git a/pypy/jit/metainterp/optimizeopt/__init__.py b/pypy/jit/metainterp/optimizeopt/__init__.py
--- a/pypy/jit/metainterp/optimizeopt/__init__.py
+++ b/pypy/jit/metainterp/optimizeopt/__init__.py
@@ -9,7 +9,7 @@
from pypy.jit.metainterp.optimizeopt.simplify import OptSimplify
from pypy.jit.metainterp.optimizeopt.pure import OptPure
from pypy.jit.metainterp.optimizeopt.earlyforce import OptEarlyForce
-from pypy.rlib.jit import PARAMETERS
+from pypy.rlib.jit import PARAMETERS, ENABLE_ALL_OPTS
from pypy.rlib.unroll import unrolling_iterable
from pypy.rlib.debug import debug_start, debug_stop, debug_print
@@ -30,6 +30,9 @@
ALL_OPTS_LIST = [name for name, _ in ALL_OPTS]
ALL_OPTS_NAMES = ':'.join([name for name, _ in ALL_OPTS])
+assert ENABLE_ALL_OPTS == ALL_OPTS_NAMES, (
+ 'please fix rlib/jit.py to say ENABLE_ALL_OPTS = %r' % (ALL_OPTS_NAMES,))
+
def build_opt_chain(metainterp_sd, enable_opts):
config = metainterp_sd.config
optimizations = []
diff --git a/pypy/jit/metainterp/optimizeopt/intutils.py b/pypy/jit/metainterp/optimizeopt/intutils.py
--- a/pypy/jit/metainterp/optimizeopt/intutils.py
+++ b/pypy/jit/metainterp/optimizeopt/intutils.py
@@ -1,10 +1,9 @@
-from pypy.rlib.rarithmetic import ovfcheck, LONG_BIT
+from pypy.rlib.rarithmetic import ovfcheck, LONG_BIT, maxint, is_valid_int
from pypy.rlib.objectmodel import we_are_translated
from pypy.jit.metainterp.resoperation import rop, ResOperation
from pypy.jit.metainterp.history import BoxInt, ConstInt
-import sys
-MAXINT = sys.maxint
-MININT = -sys.maxint - 1
+MAXINT = maxint
+MININT = -maxint - 1
class IntBound(object):
_attrs_ = ('has_upper', 'has_lower', 'upper', 'lower')
@@ -16,8 +15,8 @@
self.lower = lower
# check for unexpected overflows:
if not we_are_translated():
- assert type(upper) is not long
- assert type(lower) is not long
+ assert type(upper) is not long or is_valid_int(upper)
+ assert type(lower) is not long or is_valid_int(lower)
# Returns True if the bound was updated
def make_le(self, other):
diff --git a/pypy/jit/metainterp/optimizeopt/test/test_multilabel.py b/pypy/jit/metainterp/optimizeopt/test/test_multilabel.py
--- a/pypy/jit/metainterp/optimizeopt/test/test_multilabel.py
+++ b/pypy/jit/metainterp/optimizeopt/test/test_multilabel.py
@@ -398,6 +398,40 @@
with raises(InvalidLoop):
self.optimize_loop(ops, ops)
+ def test_issue1045(self):
+ ops = """
+ [i55]
+ i73 = int_mod(i55, 2)
+ i75 = int_rshift(i73, 63)
+ i76 = int_and(2, i75)
+ i77 = int_add(i73, i76)
+ i81 = int_eq(i77, 1)
+ i0 = int_ge(i55, 1)
+ guard_true(i0) []
+ label(i55)
+ i3 = int_mod(i55, 2)
+ i5 = int_rshift(i3, 63)
+ i6 = int_and(2, i5)
+ i7 = int_add(i3, i6)
+ i8 = int_eq(i7, 1)
+ escape(i8)
+ jump(i55)
+ """
+ expected = """
+ [i55]
+ i73 = int_mod(i55, 2)
+ i75 = int_rshift(i73, 63)
+ i76 = int_and(2, i75)
+ i77 = int_add(i73, i76)
+ i81 = int_eq(i77, 1)
+ i0 = int_ge(i55, 1)
+ guard_true(i0) []
+ label(i55, i81)
+ escape(i81)
+ jump(i55, i81)
+ """
+ self.optimize_loop(ops, expected)
+
class OptRenameStrlen(Optimization):
def propagate_forward(self, op):
dispatch_opt(self, op)
@@ -423,7 +457,7 @@
metainterp_sd = FakeMetaInterpStaticData(self.cpu)
optimize_unroll(metainterp_sd, loop, [OptRenameStrlen(), OptPure()], True)
- def test_optimizer_renaming_boxes(self):
+ def test_optimizer_renaming_boxes1(self):
ops = """
[p1]
i1 = strlen(p1)
@@ -457,7 +491,6 @@
jump(p1, i11)
"""
self.optimize_loop(ops, expected)
-
class TestLLtype(OptimizeoptTestMultiLabel, LLtypeMixin):
diff --git a/pypy/jit/metainterp/optimizeopt/test/test_optimizebasic.py b/pypy/jit/metainterp/optimizeopt/test/test_optimizebasic.py
--- a/pypy/jit/metainterp/optimizeopt/test/test_optimizebasic.py
+++ b/pypy/jit/metainterp/optimizeopt/test/test_optimizebasic.py
@@ -5031,6 +5031,42 @@
"""
self.optimize_loop(ops, expected)
+ def test_str_copy_virtual(self):
+ ops = """
+ [i0]
+ p0 = newstr(8)
+ strsetitem(p0, 0, i0)
+ strsetitem(p0, 1, i0)
+ strsetitem(p0, 2, i0)
+ strsetitem(p0, 3, i0)
+ strsetitem(p0, 4, i0)
+ strsetitem(p0, 5, i0)
+ strsetitem(p0, 6, i0)
+ strsetitem(p0, 7, i0)
+ p1 = newstr(12)
+ copystrcontent(p0, p1, 0, 0, 8)
+ strsetitem(p1, 8, 3)
+ strsetitem(p1, 9, 0)
+ strsetitem(p1, 10, 0)
+ strsetitem(p1, 11, 0)
+ finish(p1)
+ """
+ expected = """
+ [i0]
+ p1 = newstr(12)
+ strsetitem(p1, 0, i0)
+ strsetitem(p1, 1, i0)
+ strsetitem(p1, 2, i0)
+ strsetitem(p1, 3, i0)
+ strsetitem(p1, 4, i0)
+ strsetitem(p1, 5, i0)
+ strsetitem(p1, 6, i0)
+ strsetitem(p1, 7, i0)
+ strsetitem(p1, 8, 3)
+ finish(p1)
+ """
+ self.optimize_strunicode_loop(ops, expected)
+
class TestLLtype(BaseTestOptimizeBasic, LLtypeMixin):
pass
diff --git a/pypy/jit/metainterp/optimizeopt/unroll.py b/pypy/jit/metainterp/optimizeopt/unroll.py
--- a/pypy/jit/metainterp/optimizeopt/unroll.py
+++ b/pypy/jit/metainterp/optimizeopt/unroll.py
@@ -9,7 +9,6 @@
from pypy.jit.metainterp.inliner import Inliner
from pypy.jit.metainterp.resoperation import rop, ResOperation
from pypy.jit.metainterp.resume import Snapshot
-from pypy.rlib.debug import debug_print
import sys, os
# FIXME: Introduce some VirtualOptimizer super class instead
@@ -121,9 +120,9 @@
limit = self.optimizer.metainterp_sd.warmrunnerdesc.memory_manager.retrace_limit
if cell_token.retraced_count < limit:
cell_token.retraced_count += 1
- debug_print('Retracing (%d/%d)' % (cell_token.retraced_count, limit))
+ #debug_print('Retracing (%d/%d)' % (cell_token.retraced_count, limit))
else:
- debug_print("Retrace count reached, jumping to preamble")
+ #debug_print("Retrace count reached, jumping to preamble")
assert cell_token.target_tokens[0].virtual_state is None
jumpop.setdescr(cell_token.target_tokens[0])
self.optimizer.send_extra_operation(jumpop)
@@ -260,7 +259,7 @@
if op and op.result:
preamble_value = exported_state.exported_values[op.result]
value = self.optimizer.getvalue(op.result)
- if not value.is_virtual():
+ if not value.is_virtual() and not value.is_constant():
imp = ValueImporter(self, preamble_value, op)
self.optimizer.importable_values[value] = imp
newvalue = self.optimizer.getvalue(op.result)
@@ -268,12 +267,14 @@
# note that emitting here SAME_AS should not happen, but
# in case it does, we would prefer to be suboptimal in asm
# to a fatal RPython exception.
- if newresult is not op.result and not newvalue.is_constant():
+ if newresult is not op.result and \
+ not self.short_boxes.has_producer(newresult) and \
+ not newvalue.is_constant():
op = ResOperation(rop.SAME_AS, [op.result], newresult)
self.optimizer._newoperations.append(op)
- if self.optimizer.loop.logops:
- debug_print(' Falling back to add extra: ' +
- self.optimizer.loop.logops.repr_of_resop(op))
+ #if self.optimizer.loop.logops:
+ # debug_print(' Falling back to add extra: ' +
+ # self.optimizer.loop.logops.repr_of_resop(op))
self.optimizer.flush()
self.optimizer.emitting_dissabled = False
@@ -339,8 +340,8 @@
if i == len(newoperations):
while j < len(jumpargs):
a = jumpargs[j]
- if self.optimizer.loop.logops:
- debug_print('J: ' + self.optimizer.loop.logops.repr_of_arg(a))
+ #if self.optimizer.loop.logops:
+ # debug_print('J: ' + self.optimizer.loop.logops.repr_of_arg(a))
self.import_box(a, inputargs, short_jumpargs, jumpargs)
j += 1
else:
@@ -351,11 +352,11 @@
if op.is_guard():
args = args + op.getfailargs()
- if self.optimizer.loop.logops:
- debug_print('OP: ' + self.optimizer.loop.logops.repr_of_resop(op))
+ #if self.optimizer.loop.logops:
+ # debug_print('OP: ' + self.optimizer.loop.logops.repr_of_resop(op))
for a in args:
- if self.optimizer.loop.logops:
- debug_print('A: ' + self.optimizer.loop.logops.repr_of_arg(a))
+ #if self.optimizer.loop.logops:
+ # debug_print('A: ' + self.optimizer.loop.logops.repr_of_arg(a))
self.import_box(a, inputargs, short_jumpargs, jumpargs)
i += 1
newoperations = self.optimizer.get_newoperations()
@@ -368,18 +369,18 @@
# that is compatible with the virtual state at the start of the loop
modifier = VirtualStateAdder(self.optimizer)
final_virtual_state = modifier.get_virtual_state(original_jumpargs)
- debug_start('jit-log-virtualstate')
- virtual_state.debug_print('Closed loop with ')
+ #debug_start('jit-log-virtualstate')
+ #virtual_state.debug_print('Closed loop with ')
bad = {}
if not virtual_state.generalization_of(final_virtual_state, bad):
# We ended up with a virtual state that is not compatible
# and we are thus unable to jump to the start of the loop
- final_virtual_state.debug_print("Bad virtual state at end of loop, ",
- bad)
- debug_stop('jit-log-virtualstate')
+ #final_virtual_state.debug_print("Bad virtual state at end of loop, ",
+ # bad)
+ #debug_stop('jit-log-virtualstate')
raise InvalidLoop
- debug_stop('jit-log-virtualstate')
+ #debug_stop('jit-log-virtualstate')
maxguards = self.optimizer.metainterp_sd.warmrunnerdesc.memory_manager.max_retrace_guards
if self.optimizer.emitted_guards > maxguards:
@@ -442,9 +443,9 @@
self.ensure_short_op_emitted(self.short_boxes.producer(a), optimizer,
seen)
- if self.optimizer.loop.logops:
- debug_print(' Emitting short op: ' +
- self.optimizer.loop.logops.repr_of_resop(op))
+ #if self.optimizer.loop.logops:
+ # debug_print(' Emitting short op: ' +
+ # self.optimizer.loop.logops.repr_of_resop(op))
optimizer.send_extra_operation(op)
seen[op.result] = True
@@ -525,8 +526,8 @@
args = jumpop.getarglist()
modifier = VirtualStateAdder(self.optimizer)
virtual_state = modifier.get_virtual_state(args)
- debug_start('jit-log-virtualstate')
- virtual_state.debug_print("Looking for ")
+ #debug_start('jit-log-virtualstate')
+ #virtual_state.debug_print("Looking for ")
for target in cell_token.target_tokens:
if not target.virtual_state:
@@ -535,10 +536,10 @@
extra_guards = []
bad = {}
- debugmsg = 'Did not match '
+ #debugmsg = 'Did not match '
if target.virtual_state.generalization_of(virtual_state, bad):
ok = True
- debugmsg = 'Matched '
+ #debugmsg = 'Matched '
else:
try:
cpu = self.optimizer.cpu
@@ -547,13 +548,13 @@
extra_guards)
ok = True
- debugmsg = 'Guarded to match '
+ #debugmsg = 'Guarded to match '
except InvalidLoop:
pass
- target.virtual_state.debug_print(debugmsg, bad)
+ #target.virtual_state.debug_print(debugmsg, bad)
if ok:
- debug_stop('jit-log-virtualstate')
+ #debug_stop('jit-log-virtualstate')
values = [self.getvalue(arg)
for arg in jumpop.getarglist()]
@@ -574,13 +575,13 @@
newop = inliner.inline_op(shop)
self.optimizer.send_extra_operation(newop)
except InvalidLoop:
- debug_print("Inlining failed unexpectedly",
- "jumping to preamble instead")
+ #debug_print("Inlining failed unexpectedly",
+ # "jumping to preamble instead")
assert cell_token.target_tokens[0].virtual_state is None
jumpop.setdescr(cell_token.target_tokens[0])
self.optimizer.send_extra_operation(jumpop)
return True
- debug_stop('jit-log-virtualstate')
+ #debug_stop('jit-log-virtualstate')
return False
class ValueImporter(object):
diff --git a/pypy/jit/metainterp/optimizeopt/virtualstate.py b/pypy/jit/metainterp/optimizeopt/virtualstate.py
--- a/pypy/jit/metainterp/optimizeopt/virtualstate.py
+++ b/pypy/jit/metainterp/optimizeopt/virtualstate.py
@@ -681,13 +681,14 @@
self.synthetic[op] = True
def debug_print(self, logops):
- debug_start('jit-short-boxes')
- for box, op in self.short_boxes.items():
- if op:
- debug_print(logops.repr_of_arg(box) + ': ' + logops.repr_of_resop(op))
- else:
- debug_print(logops.repr_of_arg(box) + ': None')
- debug_stop('jit-short-boxes')
+ if 0:
+ debug_start('jit-short-boxes')
+ for box, op in self.short_boxes.items():
+ if op:
+ debug_print(logops.repr_of_arg(box) + ': ' + logops.repr_of_resop(op))
+ else:
+ debug_print(logops.repr_of_arg(box) + ': None')
+ debug_stop('jit-short-boxes')
def operations(self):
if not we_are_translated(): # For tests
diff --git a/pypy/jit/metainterp/optimizeopt/vstring.py b/pypy/jit/metainterp/optimizeopt/vstring.py
--- a/pypy/jit/metainterp/optimizeopt/vstring.py
+++ b/pypy/jit/metainterp/optimizeopt/vstring.py
@@ -10,6 +10,8 @@
from pypy.rlib.unroll import unrolling_iterable
from pypy.rpython import annlowlevel
from pypy.rpython.lltypesystem import lltype, rstr
+from pypy.rlib.rarithmetic import is_valid_int
+
class StrOrUnicode(object):
@@ -505,14 +507,23 @@
if length.is_constant() and length.box.getint() == 0:
return
- copy_str_content(self,
- src.force_box(self),
- dst.force_box(self),
- srcstart.force_box(self),
- dststart.force_box(self),
- length.force_box(self),
- mode, need_next_offset=False
- )
+ elif (src.is_virtual() and dst.is_virtual() and srcstart.is_constant() and
+ dststart.is_constant() and length.is_constant()):
+
+ src_start = srcstart.force_box(self).getint()
+ dst_start = dststart.force_box(self).getint()
+ for index in range(length.force_box(self).getint()):
+ vresult = self.strgetitem(src, optimizer.ConstantValue(ConstInt(index + src_start)), mode)
+ dst.setitem(index + dst_start, vresult)
+ else:
+ copy_str_content(self,
+ src.force_box(self),
+ dst.force_box(self),
+ srcstart.force_box(self),
+ dststart.force_box(self),
+ length.force_box(self),
+ mode, need_next_offset=False
+ )
def optimize_CALL(self, op):
# dispatch based on 'oopspecindex' to a method that handles
@@ -721,7 +732,7 @@
for name in dir(OptString):
if name.startswith(prefix):
value = getattr(EffectInfo, 'OS_' + name[len(prefix):])
- assert isinstance(value, int) and value != 0
+ assert is_valid_int(value) and value != 0
result.append((value, getattr(OptString, name)))
return unrolling_iterable(result)
opt_call_oopspec_ops = _findall_call_oopspec()
diff --git a/pypy/jit/metainterp/pyjitpl.py b/pypy/jit/metainterp/pyjitpl.py
--- a/pypy/jit/metainterp/pyjitpl.py
+++ b/pypy/jit/metainterp/pyjitpl.py
@@ -223,6 +223,7 @@
'cast_float_to_singlefloat', 'cast_singlefloat_to_float',
'float_neg', 'float_abs',
'cast_ptr_to_int', 'cast_int_to_ptr',
+ 'convert_float_bytes_to_longlong',
]:
exec py.code.Source('''
@arguments("box")
@@ -509,6 +510,15 @@
self._opimpl_setfield_gc_any(sbox, itemsdescr, abox)
return sbox
+ @arguments("descr", "descr", "descr", "descr", "box")
+ def opimpl_newlist_hint(self, structdescr, lengthdescr, itemsdescr,
+ arraydescr, sizehintbox):
+ sbox = self.opimpl_new(structdescr)
+ self._opimpl_setfield_gc_any(sbox, lengthdescr, history.CONST_FALSE)
+ abox = self.opimpl_new_array(arraydescr, sizehintbox)
+ self._opimpl_setfield_gc_any(sbox, itemsdescr, abox)
+ return sbox
+
@arguments("box", "descr", "descr", "box")
def _opimpl_getlistitem_gc_any(self, listbox, itemsdescr, arraydescr,
indexbox):
@@ -974,9 +984,11 @@
any_operation = len(self.metainterp.history.operations) > 0
jitdriver_sd = self.metainterp.staticdata.jitdrivers_sd[jdindex]
self.verify_green_args(jitdriver_sd, greenboxes)
- self.debug_merge_point(jitdriver_sd, jdindex, self.metainterp.portal_call_depth,
+ self.debug_merge_point(jitdriver_sd, jdindex,
+ self.metainterp.portal_call_depth,
+ self.metainterp.call_ids[-1],
greenboxes)
-
+
if self.metainterp.seen_loop_header_for_jdindex < 0:
if not any_operation:
return
@@ -1028,11 +1040,11 @@
assembler_call=True)
raise ChangeFrame
- def debug_merge_point(self, jitdriver_sd, jd_index, portal_call_depth, greenkey):
+ def debug_merge_point(self, jitdriver_sd, jd_index, portal_call_depth, current_call_id, greenkey):
# debugging: produce a DEBUG_MERGE_POINT operation
loc = jitdriver_sd.warmstate.get_location_str(greenkey)
debug_print(loc)
- args = [ConstInt(jd_index), ConstInt(portal_call_depth)] + greenkey
+ args = [ConstInt(jd_index), ConstInt(portal_call_depth), ConstInt(current_call_id)] + greenkey
self.metainterp.history.record(rop.DEBUG_MERGE_POINT, args, None)
@arguments("box", "label")
@@ -1574,11 +1586,14 @@
self.call_pure_results = args_dict_box()
self.heapcache = HeapCache()
+ self.call_ids = []
+ self.current_call_id = 0
+
def retrace_needed(self, trace):
self.partial_trace = trace
self.retracing_from = len(self.history.operations) - 1
self.heapcache.reset()
-
+
def perform_call(self, jitcode, boxes, greenkey=None):
# causes the metainterp to enter the given subfunction
@@ -1592,6 +1607,8 @@
def newframe(self, jitcode, greenkey=None):
if jitcode.is_portal:
self.portal_call_depth += 1
+ self.call_ids.append(self.current_call_id)
+ self.current_call_id += 1
if greenkey is not None and self.is_main_jitcode(jitcode):
self.portal_trace_positions.append(
(greenkey, len(self.history.operations)))
@@ -1608,6 +1625,7 @@
jitcode = frame.jitcode
if jitcode.is_portal:
self.portal_call_depth -= 1
+ self.call_ids.pop()
if frame.greenkey is not None and self.is_main_jitcode(jitcode):
self.portal_trace_positions.append(
(None, len(self.history.operations)))
@@ -1976,7 +1994,7 @@
# Found! Compile it as a loop.
# raises in case it works -- which is the common case
if self.partial_trace:
- if start != self.retracing_from:
+ if start != self.retracing_from:
raise SwitchToBlackhole(ABORT_BAD_LOOP) # For now
self.compile_loop(original_boxes, live_arg_boxes, start, resumedescr)
# creation of the loop was cancelled!
@@ -2064,11 +2082,12 @@
pass # XXX we want to do something special in resume descr,
# but not now
elif opnum == rop.GUARD_NO_OVERFLOW: # an overflow now detected
- self.execute_raised(OverflowError(), constant=True)
- try:
- self.finishframe_exception()
- except ChangeFrame:
- pass
+ if not dont_change_position:
+ self.execute_raised(OverflowError(), constant=True)
+ try:
+ self.finishframe_exception()
+ except ChangeFrame:
+ pass
elif opnum == rop.GUARD_OVERFLOW: # no longer overflowing
self.clear_exception()
else:
@@ -2084,7 +2103,7 @@
if not token.target_tokens:
return None
return token
-
+
def compile_loop(self, original_boxes, live_arg_boxes, start, resume_at_jump_descr):
num_green_args = self.jitdriver_sd.num_green_args
greenkey = original_boxes[:num_green_args]
@@ -2349,7 +2368,7 @@
# warmstate.py.
virtualizable_box = self.virtualizable_boxes[-1]
virtualizable = vinfo.unwrap_virtualizable_box(virtualizable_box)
- assert not vinfo.gettoken(virtualizable)
+ assert not vinfo.is_token_nonnull_gcref(virtualizable)
# fill the virtualizable with the local boxes
self.synchronize_virtualizable()
#
diff --git a/pypy/jit/metainterp/resoperation.py b/pypy/jit/metainterp/resoperation.py
--- a/pypy/jit/metainterp/resoperation.py
+++ b/pypy/jit/metainterp/resoperation.py
@@ -419,6 +419,7 @@
'CAST_INT_TO_FLOAT/1', # need some messy code in the backend
'CAST_FLOAT_TO_SINGLEFLOAT/1',
'CAST_SINGLEFLOAT_TO_FLOAT/1',
+ 'CONVERT_FLOAT_BYTES_TO_LONGLONG/1',
#
'INT_LT/2b',
'INT_LE/2b',
diff --git a/pypy/jit/metainterp/resume.py b/pypy/jit/metainterp/resume.py
--- a/pypy/jit/metainterp/resume.py
+++ b/pypy/jit/metainterp/resume.py
@@ -1101,14 +1101,14 @@
virtualizable = self.decode_ref(numb.nums[index])
if self.resume_after_guard_not_forced == 1:
# in the middle of handle_async_forcing()
- assert vinfo.gettoken(virtualizable)
- vinfo.settoken(virtualizable, vinfo.TOKEN_NONE)
+ assert vinfo.is_token_nonnull_gcref(virtualizable)
+ vinfo.reset_token_gcref(virtualizable)
else:
# just jumped away from assembler (case 4 in the comment in
# virtualizable.py) into tracing (case 2); check that vable_token
# is and stays 0. Note the call to reset_vable_token() in
# warmstate.py.
- assert not vinfo.gettoken(virtualizable)
+ assert not vinfo.is_token_nonnull_gcref(virtualizable)
return vinfo.write_from_resume_data_partial(virtualizable, self, numb)
def load_value_of_type(self, TYPE, tagged):
diff --git a/pypy/jit/metainterp/test/test_ajit.py b/pypy/jit/metainterp/test/test_ajit.py
--- a/pypy/jit/metainterp/test/test_ajit.py
+++ b/pypy/jit/metainterp/test/test_ajit.py
@@ -3,6 +3,7 @@
import py
from pypy import conftest
+from pypy.jit.codewriter import longlong
from pypy.jit.codewriter.policy import JitPolicy, StopAtXPolicy
from pypy.jit.metainterp import pyjitpl, history
from pypy.jit.metainterp.optimizeopt import ALL_OPTS_DICT
@@ -14,7 +15,8 @@
loop_invariant, elidable, promote, jit_debug, assert_green,
AssertGreenFailed, unroll_safe, current_trace_length, look_inside_iff,
isconstant, isvirtual, promote_string, set_param, record_known_class)
-from pypy.rlib.rarithmetic import ovfcheck
+from pypy.rlib.longlong2float import float2longlong
+from pypy.rlib.rarithmetic import ovfcheck, is_valid_int
from pypy.rpython.lltypesystem import lltype, llmemory, rffi
from pypy.rpython.ootypesystem import ootype
@@ -144,7 +146,7 @@
'int_mul': 1, 'guard_true': 2, 'int_sub': 2})
- def test_loop_invariant_mul_ovf(self):
+ def test_loop_invariant_mul_ovf1(self):
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x'])
def f(x, y):
res = 0
@@ -235,6 +237,65 @@
'guard_true': 4, 'int_sub': 4, 'jump': 3,
'int_mul': 3, 'int_add': 4})
+ def test_loop_invariant_mul_ovf2(self):
+ myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x'])
+ def f(x, y):
+ res = 0
+ while y > 0:
+ myjitdriver.can_enter_jit(x=x, y=y, res=res)
+ myjitdriver.jit_merge_point(x=x, y=y, res=res)
+ b = y * 2
+ try:
+ res += ovfcheck(x * x) + b
+ except OverflowError:
+ res += 1
+ y -= 1
+ return res
+ res = self.meta_interp(f, [sys.maxint, 7])
+ assert res == f(sys.maxint, 7)
+ self.check_trace_count(1)
+ res = self.meta_interp(f, [6, 7])
+ assert res == 308
+
+ def test_loop_invariant_mul_bridge_ovf1(self):
+ myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x1', 'x2'])
+ def f(x1, x2, y):
+ res = 0
+ while y > 0:
+ myjitdriver.can_enter_jit(x1=x1, x2=x2, y=y, res=res)
+ myjitdriver.jit_merge_point(x1=x1, x2=x2, y=y, res=res)
+ try:
+ res += ovfcheck(x1 * x1)
+ except OverflowError:
+ res += 1
+ if y<32 and (y>>2)&1==0:
+ x1, x2 = x2, x1
+ y -= 1
+ return res
+ res = self.meta_interp(f, [6, sys.maxint, 48])
+ assert res == f(6, sys.maxint, 48)
+
+ def test_loop_invariant_mul_bridge_ovf2(self):
+ myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x1', 'x2', 'n'])
+ def f(x1, x2, n, y):
+ res = 0
+ while y > 0:
+ myjitdriver.can_enter_jit(x1=x1, x2=x2, y=y, res=res, n=n)
+ myjitdriver.jit_merge_point(x1=x1, x2=x2, y=y, res=res, n=n)
+ try:
+ res += ovfcheck(x1 * x1)
+ except OverflowError:
+ res += 1
+ y -= 1
+ if y&4 == 0:
+ x1, x2 = x2, x1
+ return res
+ res = self.meta_interp(f, [6, sys.maxint, 32, 48])
+ assert res == f(6, sys.maxint, 32, 48)
+ res = self.meta_interp(f, [sys.maxint, 6, 32, 48])
+ assert res == f(sys.maxint, 6, 32, 48)
+
+
def test_loop_invariant_intbox(self):
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x'])
class I:
@@ -894,7 +955,7 @@
self.meta_interp(f, [20], repeat=7)
# the loop and the entry path as a single trace
self.check_jitcell_token_count(1)
-
+
# we get:
# ENTER - compile the new loop and the entry bridge
# ENTER - compile the leaving path
@@ -1411,7 +1472,7 @@
assert res == f(299)
self.check_resops(guard_class=0, guard_nonnull=4,
guard_nonnull_class=4, guard_isnull=2)
-
+
def test_merge_guardnonnull_guardvalue(self):
from pypy.rlib.objectmodel import instantiate
@@ -1440,7 +1501,7 @@
assert res == f(299)
self.check_resops(guard_value=4, guard_class=0, guard_nonnull=4,
guard_nonnull_class=0, guard_isnull=2)
-
+
def test_merge_guardnonnull_guardvalue_2(self):
from pypy.rlib.objectmodel import instantiate
@@ -1469,7 +1530,7 @@
assert res == f(299)
self.check_resops(guard_value=4, guard_class=0, guard_nonnull=4,
guard_nonnull_class=0, guard_isnull=2)
-
+
def test_merge_guardnonnull_guardclass_guardvalue(self):
from pypy.rlib.objectmodel import instantiate
@@ -2237,7 +2298,7 @@
self.check_resops(int_rshift=3)
bigval = 1
- while (bigval << 3).__class__ is int:
+ while is_valid_int(bigval << 3):
bigval = bigval << 1
assert self.meta_interp(f, [bigval, 5]) == 0
@@ -2282,7 +2343,7 @@
self.check_resops(int_rshift=3)
bigval = 1
- while (bigval << 3).__class__ is int:
+ while is_valid_int(bigval << 3):
bigval = bigval << 1
assert self.meta_interp(f, [bigval, 5]) == 0
@@ -2577,7 +2638,7 @@
return sa
assert self.meta_interp(f, [20]) == f(20)
self.check_resops(int_lt=6, int_le=2, int_ge=4, int_gt=3)
-
+
def test_intbounds_not_generalized2(self):
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'node'])
@@ -2618,7 +2679,7 @@
assert self.meta_interp(f, [20, 3]) == f(20, 3)
self.check_jitcell_token_count(1)
self.check_target_token_count(5)
-
+
def test_max_retrace_guards(self):
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a'])
@@ -2756,7 +2817,7 @@
for cell in get_stats().get_all_jitcell_tokens():
# Initialal trace with two labels and 5 retraces
assert len(cell.target_tokens) <= 7
-
+
def test_nested_retrace(self):
myjitdriver = JitDriver(greens = ['pc'], reds = ['n', 'a', 'i', 'j', 'sa'])
@@ -2943,11 +3004,18 @@
self.check_resops(arraylen_gc=3)
def test_ulonglong_mod(self):
- myjitdriver = JitDriver(greens = [], reds = ['n', 'sa', 'i'])
+ myjitdriver = JitDriver(greens = [], reds = ['n', 'a'])
+ class A:
+ pass
def f(n):
sa = i = rffi.cast(rffi.ULONGLONG, 1)
+ a = A()
while i < rffi.cast(rffi.ULONGLONG, n):
- myjitdriver.jit_merge_point(sa=sa, n=n, i=i)
+ a.sa = sa
+ a.i = i
+ myjitdriver.jit_merge_point(n=n, a=a)
+ sa = a.sa
+ i = a.i
sa += sa % i
i += 1
res = self.meta_interp(f, [32])
@@ -3718,6 +3786,25 @@
assert res == 11 * 12 * 13
self.check_operations_history(int_add=3, int_mul=2)
+ def test_setinteriorfield(self):
+ A = lltype.GcArray(lltype.Struct('S', ('x', lltype.Signed)))
+ a = lltype.malloc(A, 5, immortal=True)
+ def g(n):
+ a[n].x = n + 2
+ return a[n].x
+ res = self.interp_operations(g, [1])
+ assert res == 3
+
+ def test_float2longlong(self):
+ def f(n):
+ return float2longlong(n)
+
+ for x in [2.5, float("nan"), -2.5, float("inf")]:
+ # There are tests elsewhere to verify the correctness of this.
+ expected = float2longlong(x)
+ res = self.interp_operations(f, [x])
+ assert longlong.getfloatstorage(res) == expected
+
class TestLLtype(BaseLLtypeTests, LLJitMixin):
def test_tagged(self):
diff --git a/pypy/jit/metainterp/test/test_compile.py b/pypy/jit/metainterp/test/test_compile.py
--- a/pypy/jit/metainterp/test/test_compile.py
+++ b/pypy/jit/metainterp/test/test_compile.py
@@ -14,7 +14,7 @@
ts = typesystem.llhelper
def __init__(self):
self.seen = []
- def compile_loop(self, inputargs, operations, token, name=''):
+ def compile_loop(self, inputargs, operations, token, log=True, name=''):
self.seen.append((inputargs, operations, token))
class FakeLogger(object):
diff --git a/pypy/jit/metainterp/test/test_list.py b/pypy/jit/metainterp/test/test_list.py
--- a/pypy/jit/metainterp/test/test_list.py
+++ b/pypy/jit/metainterp/test/test_list.py
@@ -1,4 +1,5 @@
import py
+from pypy.rlib.objectmodel import newlist_hint
from pypy.rlib.jit import JitDriver
from pypy.jit.metainterp.test.support import LLJitMixin, OOJitMixin
@@ -228,6 +229,28 @@
self.check_resops({'jump': 1, 'int_gt': 2, 'int_add': 2,
'guard_true': 2, 'int_sub': 2})
+ def test_newlist_hint(self):
+ def f(i):
+ l = newlist_hint(i)
+ l[0] = 55
+ return len(l)
+
+ r = self.interp_operations(f, [3])
+ assert r == 0
+
+ def test_newlist_hint_optimized(self):
+ driver = JitDriver(greens = [], reds = ['i'])
+
+ def f(i):
+ while i > 0:
+ driver.jit_merge_point(i=i)
+ l = newlist_hint(5)
+ l.append(1)
+ i -= l[0]
+
+ self.meta_interp(f, [10], listops=True)
+ self.check_resops(new_array=0, call=0)
+
class TestOOtype(ListTests, OOJitMixin):
pass
diff --git a/pypy/jit/metainterp/test/test_logger.py b/pypy/jit/metainterp/test/test_logger.py
--- a/pypy/jit/metainterp/test/test_logger.py
+++ b/pypy/jit/metainterp/test/test_logger.py
@@ -54,7 +54,7 @@
class FakeJitDriver(object):
class warmstate(object):
get_location_str = staticmethod(lambda args: "dupa")
-
+
class FakeMetaInterpSd:
cpu = AbstractCPU()
cpu.ts = self.ts
@@ -77,7 +77,7 @@
equaloplists(loop.operations, oloop.operations)
assert oloop.inputargs == loop.inputargs
return logger, loop, oloop
-
+
def test_simple(self):
inp = '''
[i0, i1, i2, p3, p4, p5]
@@ -116,12 +116,13 @@
def test_debug_merge_point(self):
inp = '''
[]
- debug_merge_point(0, 0)
+ debug_merge_point(0, 0, 0)
'''
_, loop, oloop = self.reparse(inp, check_equal=False)
assert loop.operations[0].getarg(1).getint() == 0
- assert oloop.operations[0].getarg(1)._get_str() == "dupa"
-
+ assert loop.operations[0].getarg(2).getint() == 0
+ assert oloop.operations[0].getarg(2)._get_str() == "dupa"
+
def test_floats(self):
inp = '''
[f0]
@@ -142,7 +143,7 @@
output = logger.log_loop(loop)
assert output.splitlines()[-1] == "jump(i0, descr=<Loop3>)"
pure_parse(output)
-
+
def test_guard_descr(self):
namespace = {'fdescr': BasicFailDescr()}
inp = '''
@@ -154,7 +155,7 @@
output = logger.log_loop(loop)
assert output.splitlines()[-1] == "guard_true(i0, descr=<Guard0>) [i0]"
pure_parse(output)
-
+
logger = Logger(self.make_metainterp_sd(), guard_number=False)
output = logger.log_loop(loop)
lastline = output.splitlines()[-1]
diff --git a/pypy/jit/metainterp/test/test_quasiimmut.py b/pypy/jit/metainterp/test/test_quasiimmut.py
--- a/pypy/jit/metainterp/test/test_quasiimmut.py
+++ b/pypy/jit/metainterp/test/test_quasiimmut.py
@@ -8,7 +8,7 @@
from pypy.jit.metainterp.quasiimmut import get_current_qmut_instance
from pypy.jit.metainterp.test.support import LLJitMixin
from pypy.jit.codewriter.policy import StopAtXPolicy
-from pypy.rlib.jit import JitDriver, dont_look_inside
+from pypy.rlib.jit import JitDriver, dont_look_inside, unroll_safe
def test_get_current_qmut_instance():
@@ -480,6 +480,32 @@
assert res == 1
self.check_jitcell_token_count(2)
+ def test_for_loop_array(self):
+ myjitdriver = JitDriver(greens=[], reds=["n", "i"])
+ class Foo(object):
+ _immutable_fields_ = ["x?[*]"]
+ def __init__(self, x):
+ self.x = x
+ f = Foo([1, 3, 5, 6])
+ @unroll_safe
+ def g(v):
+ for x in f.x:
+ if x & 1 == 0:
+ v += 1
+ return v
+ def main(n):
+ i = 0
+ while i < n:
+ myjitdriver.jit_merge_point(n=n, i=i)
+ i = g(i)
+ return i
+ res = self.meta_interp(main, [10])
+ assert res == 10
+ self.check_resops({
+ "int_add": 2, "int_lt": 2, "jump": 1, "guard_true": 2,
+ "guard_not_invalidated": 2
+ })
+
class TestLLtypeGreenFieldsTests(QuasiImmutTests, LLJitMixin):
pass
diff --git a/pypy/jit/metainterp/test/test_warmspot.py b/pypy/jit/metainterp/test/test_warmspot.py
--- a/pypy/jit/metainterp/test/test_warmspot.py
+++ b/pypy/jit/metainterp/test/test_warmspot.py
@@ -13,7 +13,7 @@
class WarmspotTests(object):
-
+
def test_basic(self):
mydriver = JitDriver(reds=['a'],
greens=['i'])
@@ -77,16 +77,16 @@
self.meta_interp(f, [123, 10])
assert len(get_stats().locations) >= 4
for loc in get_stats().locations:
- assert loc == (0, 123)
+ assert loc == (0, 0, 123)
def test_set_param_enable_opts(self):
from pypy.rpython.annlowlevel import llstr, hlstr
-
+
myjitdriver = JitDriver(greens = [], reds = ['n'])
class A(object):
def m(self, n):
return n-1
-
+
def g(n):
while n > 0:
myjitdriver.can_enter_jit(n=n)
@@ -332,7 +332,7 @@
ts = llhelper
translate_support_code = False
stats = "stats"
-
+
def get_fail_descr_number(self, d):
return -1
@@ -352,7 +352,7 @@
return "not callable"
driver = JitDriver(reds = ['red'], greens = ['green'])
-
+
def f(green):
red = 0
while red < 10:
diff --git a/pypy/jit/metainterp/virtualizable.py b/pypy/jit/metainterp/virtualizable.py
--- a/pypy/jit/metainterp/virtualizable.py
+++ b/pypy/jit/metainterp/virtualizable.py
@@ -262,15 +262,15 @@
force_now._dont_inline_ = True
self.force_now = force_now
- def gettoken(virtualizable):
+ def is_token_nonnull_gcref(virtualizable):
virtualizable = cast_gcref_to_vtype(virtualizable)
- return virtualizable.vable_token
- self.gettoken = gettoken
+ return bool(virtualizable.vable_token)
+ self.is_token_nonnull_gcref = is_token_nonnull_gcref
- def settoken(virtualizable, token):
+ def reset_token_gcref(virtualizable):
virtualizable = cast_gcref_to_vtype(virtualizable)
- virtualizable.vable_token = token
- self.settoken = settoken
+ virtualizable.vable_token = VirtualizableInfo.TOKEN_NONE
+ self.reset_token_gcref = reset_token_gcref
def _freeze_(self):
return True
diff --git a/pypy/jit/metainterp/warmspot.py b/pypy/jit/metainterp/warmspot.py
--- a/pypy/jit/metainterp/warmspot.py
+++ b/pypy/jit/metainterp/warmspot.py
@@ -100,7 +100,7 @@
if not kwds.get('translate_support_code', False):
warmrunnerdesc.metainterp_sd.profiler.finish()
warmrunnerdesc.metainterp_sd.cpu.finish_once()
- print '~~~ return value:', res
+ print '~~~ return value:', repr(res)
while repeat > 1:
print '~' * 79
res1 = interp.eval_graph(graph, args)
diff --git a/pypy/jit/tl/tinyframe/tinyframe.py b/pypy/jit/tl/tinyframe/tinyframe.py
--- a/pypy/jit/tl/tinyframe/tinyframe.py
+++ b/pypy/jit/tl/tinyframe/tinyframe.py
@@ -210,7 +210,7 @@
def repr(self):
return "<function %s(%s)>" % (self.outer.repr(), self.inner.repr())
-driver = JitDriver(greens = ['code', 'i'], reds = ['self'],
+driver = JitDriver(greens = ['i', 'code'], reds = ['self'],
virtualizables = ['self'])
class Frame(object):
diff --git a/pypy/jit/tl/tlc.py b/pypy/jit/tl/tlc.py
--- a/pypy/jit/tl/tlc.py
+++ b/pypy/jit/tl/tlc.py
@@ -6,6 +6,8 @@
from pypy.jit.tl.tlopcode import *
from pypy.jit.tl import tlopcode
from pypy.rlib.jit import JitDriver, elidable
+from pypy.rlib.rarithmetic import is_valid_int
+
class Obj(object):
@@ -219,7 +221,7 @@
class Frame(object):
def __init__(self, args, pc):
- assert isinstance(pc, int)
+ assert is_valid_int(pc)
self.args = args
self.pc = pc
self.stack = []
@@ -239,7 +241,7 @@
return interp_eval(code, pc, args, pool).int_o()
def interp_eval(code, pc, args, pool):
- assert isinstance(pc, int)
+ assert is_valid_int(pc)
frame = Frame(args, pc)
pc = frame.pc
diff --git a/pypy/jit/tool/test/test_oparser.py b/pypy/jit/tool/test/test_oparser.py
--- a/pypy/jit/tool/test/test_oparser.py
+++ b/pypy/jit/tool/test/test_oparser.py
@@ -146,16 +146,18 @@
def test_debug_merge_point(self):
x = '''
[]
- debug_merge_point(0, "info")
- debug_merge_point(0, 'info')
- debug_merge_point(1, '<some ('other.')> info')
- debug_merge_point(0, '(stuff) #1')
+ debug_merge_point(0, 0, "info")
+ debug_merge_point(0, 0, 'info')
+ debug_merge_point(1, 1, '<some ('other.')> info')
+ debug_merge_point(0, 0, '(stuff) #1')
'''
loop = self.parse(x)
- assert loop.operations[0].getarg(1)._get_str() == 'info'
- assert loop.operations[1].getarg(1)._get_str() == 'info'
- assert loop.operations[2].getarg(1)._get_str() == "<some ('other.')> info"
- assert loop.operations[3].getarg(1)._get_str() == "(stuff) #1"
+ assert loop.operations[0].getarg(2)._get_str() == 'info'
+ assert loop.operations[0].getarg(1).value == 0
+ assert loop.operations[1].getarg(2)._get_str() == 'info'
+ assert loop.operations[2].getarg(2)._get_str() == "<some ('other.')> info"
+ assert loop.operations[2].getarg(1).value == 1
+ assert loop.operations[3].getarg(2)._get_str() == "(stuff) #1"
def test_descr_with_obj_print(self):
diff --git a/pypy/module/__builtin__/app_inspect.py b/pypy/module/__builtin__/app_inspect.py
--- a/pypy/module/__builtin__/app_inspect.py
+++ b/pypy/module/__builtin__/app_inspect.py
@@ -8,8 +8,6 @@
from __pypy__ import lookup_special
def _caller_locals():
- # note: the reason why this is working is because the functions in here are
- # compiled by geninterp, so they don't have a frame
return sys._getframe(0).f_locals
def vars(*obj):
@@ -26,17 +24,6 @@
except AttributeError:
raise TypeError, "vars() argument must have __dict__ attribute"
-# Replaced by the interp-level helper space.callable():
-##def callable(ob):
-## import __builtin__ # XXX this is insane but required for now for geninterp
-## for c in type(ob).__mro__:
-## if '__call__' in c.__dict__:
-## if isinstance(ob, __builtin__._instance): # old style instance!
-## return getattr(ob, '__call__', None) is not None
-## return True
-## else:
-## return False
-
def dir(*args):
"""dir([object]) -> list of strings
diff --git a/pypy/module/__builtin__/interp_memoryview.py b/pypy/module/__builtin__/interp_memoryview.py
--- a/pypy/module/__builtin__/interp_memoryview.py
+++ b/pypy/module/__builtin__/interp_memoryview.py
@@ -69,6 +69,10 @@
return W_MemoryView(buf)
def descr_buffer(self, space):
+ """Note that memoryview() objects in PyPy support buffer(), whereas
+ not in CPython; but CPython supports passing memoryview() to most
+ built-in functions that accept buffers, with the notable exception
+ of the buffer() built-in."""
return space.wrap(self.buf)
def descr_tobytes(self, space):
diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py
--- a/pypy/module/__pypy__/__init__.py
+++ b/pypy/module/__pypy__/__init__.py
@@ -1,5 +1,5 @@
+import sys
-# Package initialisation
from pypy.interpreter.mixedmodule import MixedModule
from pypy.module.imp.importing import get_pyc_magic
@@ -12,6 +12,19 @@
"UnicodeBuilder": "interp_builders.W_UnicodeBuilder",
}
+class TimeModule(MixedModule):
+ appleveldefs = {}
+ interpleveldefs = {}
+ if sys.platform.startswith("linux"):
+ interpleveldefs["clock_gettime"] = "interp_time.clock_gettime"
+ interpleveldefs["clock_getres"] = "interp_time.clock_getres"
+ for name in [
+ "CLOCK_REALTIME", "CLOCK_MONOTONIC", "CLOCK_MONOTONIC_RAW",
+ "CLOCK_PROCESS_CPUTIME_ID", "CLOCK_THREAD_CPUTIME_ID"
+ ]:
+ interpleveldefs[name] = "space.wrap(interp_time.%s)" % name
+
+
class Module(MixedModule):
appleveldefs = {
}
@@ -32,6 +45,7 @@
submodules = {
"builders": BuildersModule,
+ "time": TimeModule,
}
def setup_after_space_initialization(self):
diff --git a/pypy/module/__pypy__/interp_time.py b/pypy/module/__pypy__/interp_time.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/__pypy__/interp_time.py
@@ -0,0 +1,64 @@
+import sys
+
+from pypy.interpreter.error import exception_from_errno
+from pypy.interpreter.gateway import unwrap_spec
+from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.rpython.tool import rffi_platform
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
+
+
+class CConfig:
+ _compilation_info_ = ExternalCompilationInfo(
+ includes=["time.h"],
+ libraries=["rt"],
+ )
+
+ HAS_CLOCK_GETTIME = rffi_platform.Has('clock_gettime')
+
+ CLOCK_REALTIME = rffi_platform.DefinedConstantInteger("CLOCK_REALTIME")
+ CLOCK_MONOTONIC = rffi_platform.DefinedConstantInteger("CLOCK_MONOTONIC")
+ CLOCK_MONOTONIC_RAW = rffi_platform.DefinedConstantInteger("CLOCK_MONOTONIC_RAW")
+ CLOCK_PROCESS_CPUTIME_ID = rffi_platform.DefinedConstantInteger("CLOCK_PROCESS_CPUTIME_ID")
+ CLOCK_THREAD_CPUTIME_ID = rffi_platform.DefinedConstantInteger("CLOCK_THREAD_CPUTIME_ID")
+
+ TIMESPEC = rffi_platform.Struct("struct timespec", [
+ ("tv_sec", rffi.TIME_T),
+ ("tv_nsec", rffi.LONG),
+ ])
+
+cconfig = rffi_platform.configure(CConfig)
+
+HAS_CLOCK_GETTIME = cconfig["HAS_CLOCK_GETTIME"]
+
+CLOCK_REALTIME = cconfig["CLOCK_REALTIME"]
+CLOCK_MONOTONIC = cconfig["CLOCK_MONOTONIC"]
+CLOCK_MONOTONIC_RAW = cconfig["CLOCK_MONOTONIC_RAW"]
+CLOCK_PROCESS_CPUTIME_ID = cconfig["CLOCK_PROCESS_CPUTIME_ID"]
+CLOCK_THREAD_CPUTIME_ID = cconfig["CLOCK_THREAD_CPUTIME_ID"]
+
+TIMESPEC = cconfig["TIMESPEC"]
+
+c_clock_gettime = rffi.llexternal("clock_gettime",
+ [lltype.Signed, lltype.Ptr(TIMESPEC)], rffi.INT,
+ compilation_info=CConfig._compilation_info_, threadsafe=False
+)
+c_clock_getres = rffi.llexternal("clock_getres",
+ [lltype.Signed, lltype.Ptr(TIMESPEC)], rffi.INT,
+ compilation_info=CConfig._compilation_info_, threadsafe=False
+)
+
+ at unwrap_spec(clk_id="c_int")
+def clock_gettime(space, clk_id):
+ with lltype.scoped_alloc(TIMESPEC) as tp:
+ ret = c_clock_gettime(clk_id, tp)
+ if ret != 0:
+ raise exception_from_errno(space, space.w_IOError)
+ return space.wrap(tp.c_tv_sec + tp.c_tv_nsec * 1e-9)
+
+ at unwrap_spec(clk_id="c_int")
+def clock_getres(space, clk_id):
+ with lltype.scoped_alloc(TIMESPEC) as tp:
+ ret = c_clock_getres(clk_id, tp)
+ if ret != 0:
+ raise exception_from_errno(space, space.w_IOError)
+ return space.wrap(tp.c_tv_sec + tp.c_tv_nsec * 1e-9)
diff --git a/pypy/module/__pypy__/test/test_time.py b/pypy/module/__pypy__/test/test_time.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/__pypy__/test/test_time.py
@@ -0,0 +1,26 @@
+import py
+
+from pypy.module.__pypy__.interp_time import HAS_CLOCK_GETTIME
+
+
+class AppTestTime(object):
+ def setup_class(cls):
+ if not HAS_CLOCK_GETTIME:
+ py.test.skip("need time.clock_gettime")
+
+ def test_clock_realtime(self):
+ from __pypy__ import time
+ res = time.clock_gettime(time.CLOCK_REALTIME)
+ assert isinstance(res, float)
+
+ def test_clock_monotonic(self):
+ from __pypy__ import time
+ a = time.clock_gettime(time.CLOCK_MONOTONIC)
+ b = time.clock_gettime(time.CLOCK_MONOTONIC)
+ assert a <= b
+
+ def test_clock_getres(self):
+ from __pypy__ import time
+ res = time.clock_getres(time.CLOCK_REALTIME)
+ assert res > 0.0
+ assert res <= 1.0
diff --git a/pypy/module/_ffi/test/test__ffi.py b/pypy/module/_ffi/test/test__ffi.py
--- a/pypy/module/_ffi/test/test__ffi.py
+++ b/pypy/module/_ffi/test/test__ffi.py
@@ -100,7 +100,10 @@
from _ffi import CDLL, types
libm = CDLL(self.libm_name)
pow_addr = libm.getaddressindll('pow')
- assert pow_addr == self.pow_addr & (sys.maxint*2-1)
+ fff = sys.maxint*2-1
+ if sys.platform == 'win32':
+ fff = sys.maxint*2+1
+ assert pow_addr == self.pow_addr & fff
def test_func_fromaddr(self):
import sys
diff --git a/pypy/module/_file/interp_file.py b/pypy/module/_file/interp_file.py
--- a/pypy/module/_file/interp_file.py
+++ b/pypy/module/_file/interp_file.py
@@ -5,14 +5,13 @@
from pypy.rlib import streamio
from pypy.rlib.rarithmetic import r_longlong
from pypy.rlib.rstring import StringBuilder
-from pypy.module._file.interp_stream import (W_AbstractStream, StreamErrors,
- wrap_streamerror, wrap_oserror_as_ioerror)
+from pypy.module._file.interp_stream import W_AbstractStream, StreamErrors
from pypy.module.posix.interp_posix import dispatch_filename
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.typedef import (TypeDef, GetSetProperty,
interp_attrproperty, make_weakref_descr, interp_attrproperty_w)
from pypy.interpreter.gateway import interp2app, unwrap_spec
-
+from pypy.interpreter.streamutil import wrap_streamerror, wrap_oserror_as_ioerror
class W_File(W_AbstractStream):
"""An interp-level file object. This implements the same interface than
diff --git a/pypy/module/_file/interp_stream.py b/pypy/module/_file/interp_stream.py
--- a/pypy/module/_file/interp_stream.py
+++ b/pypy/module/_file/interp_stream.py
@@ -2,27 +2,13 @@
from pypy.rlib import streamio
from pypy.rlib.streamio import StreamErrors
-from pypy.interpreter.error import OperationError, wrap_oserror2
+from pypy.interpreter.error import OperationError
from pypy.interpreter.baseobjspace import ObjSpace, Wrappable
from pypy.interpreter.typedef import TypeDef
from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.streamutil import wrap_streamerror, wrap_oserror_as_ioerror
-def wrap_streamerror(space, e, w_filename=None):
- if isinstance(e, streamio.StreamError):
- return OperationError(space.w_ValueError,
- space.wrap(e.message))
- elif isinstance(e, OSError):
- return wrap_oserror_as_ioerror(space, e, w_filename)
- else:
- # should not happen: wrap_streamerror() is only called when
- # StreamErrors = (OSError, StreamError) are raised
- return OperationError(space.w_IOError, space.w_None)
-
-def wrap_oserror_as_ioerror(space, e, w_filename=None):
- return wrap_oserror2(space, e, w_filename,
- w_exception_class=space.w_IOError)
-
class W_AbstractStream(Wrappable):
"""Base class for interp-level objects that expose streams to app-level"""
slock = None
diff --git a/pypy/module/_io/__init__.py b/pypy/module/_io/__init__.py
--- a/pypy/module/_io/__init__.py
+++ b/pypy/module/_io/__init__.py
@@ -28,6 +28,7 @@
}
def init(self, space):
+ MixedModule.init(self, space)
w_UnsupportedOperation = space.call_function(
space.w_type,
space.wrap('UnsupportedOperation'),
@@ -35,3 +36,9 @@
space.newdict())
space.setattr(self, space.wrap('UnsupportedOperation'),
w_UnsupportedOperation)
+
+ def shutdown(self, space):
+ # at shutdown, flush all open streams. Ignore I/O errors.
+ from pypy.module._io.interp_iobase import get_autoflushher
+ get_autoflushher(space).flush_all(space)
+
diff --git a/pypy/module/_io/interp_iobase.py b/pypy/module/_io/interp_iobase.py
--- a/pypy/module/_io/interp_iobase.py
+++ b/pypy/module/_io/interp_iobase.py
@@ -5,6 +5,8 @@
from pypy.interpreter.gateway import interp2app
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.rlib.rstring import StringBuilder
+from pypy.rlib import rweakref
+
DEFAULT_BUFFER_SIZE = 8192
@@ -43,6 +45,8 @@
self.space = space
self.w_dict = space.newdict()
self.__IOBase_closed = False
+ self.streamholder = None # needed by AutoFlusher
+ get_autoflushher(space).add(self)
def getdict(self, space):
return self.w_dict
@@ -98,6 +102,7 @@
space.call_method(self, "flush")
finally:
self.__IOBase_closed = True
+ get_autoflushher(space).remove(self)
def flush_w(self, space):
if self._CLOSED():
@@ -303,3 +308,60 @@
read = interp2app(W_RawIOBase.read_w),
readall = interp2app(W_RawIOBase.readall_w),
)
+
+
+# ------------------------------------------------------------
+# functions to make sure that all streams are flushed on exit
+# ------------------------------------------------------------
+
+class StreamHolder(object):
+
+ def __init__(self, w_iobase):
+ self.w_iobase_ref = rweakref.ref(w_iobase)
+ w_iobase.autoflusher = self
+
+ def autoflush(self, space):
+ w_iobase = self.w_iobase_ref()
+ if w_iobase is not None:
+ try:
+ space.call_method(w_iobase, 'flush')
+ except OperationError, e:
+ # if it's an IOError or ValueError, ignore it (ValueError is
+ # raised if by chance we are trying to flush a file which has
+ # already been closed)
+ if not (e.match(space, space.w_IOError) or
+ e.match(space, space.w_ValueError)):
+ raise
+
+
+class AutoFlusher(object):
+
+ def __init__(self, space):
+ self.streams = {}
+
+ def add(self, w_iobase):
+ assert w_iobase.streamholder is None
+ holder = StreamHolder(w_iobase)
+ w_iobase.streamholder = holder
+ self.streams[holder] = None
+
+ def remove(self, w_iobase):
+ holder = w_iobase.streamholder
+ if holder is not None:
+ del self.streams[holder]
+
+ def flush_all(self, space):
+ while self.streams:
+ for streamholder in self.streams.keys():
+ try:
+ del self.streams[streamholder]
+ except KeyError:
+ pass # key was removed in the meantime
+ else:
+ streamholder.autoflush(space)
+
+
+def get_autoflushher(space):
+ return space.fromcache(AutoFlusher)
+
+
diff --git a/pypy/module/_io/test/test_fileio.py b/pypy/module/_io/test/test_fileio.py
--- a/pypy/module/_io/test/test_fileio.py
+++ b/pypy/module/_io/test/test_fileio.py
@@ -160,3 +160,42 @@
f.close()
assert repr(f) == "<_io.FileIO [closed]>"
+def test_flush_at_exit():
+ from pypy import conftest
+ from pypy.tool.option import make_config, make_objspace
+ from pypy.tool.udir import udir
+
+ tmpfile = udir.join('test_flush_at_exit')
+ config = make_config(conftest.option)
+ space = make_objspace(config)
+ space.appexec([space.wrap(str(tmpfile))], """(tmpfile):
+ import io
+ f = io.open(tmpfile, 'w', encoding='ascii')
+ f.write('42')
+ # no flush() and no close()
+ import sys; sys._keepalivesomewhereobscure = f
+ """)
+ space.finish()
+ assert tmpfile.read() == '42'
+
+def test_flush_at_exit_IOError_and_ValueError():
+ from pypy import conftest
+ from pypy.tool.option import make_config, make_objspace
+
+ config = make_config(conftest.option)
+ space = make_objspace(config)
+ space.appexec([], """():
+ import io
+ class MyStream(io.IOBase):
+ def flush(self):
+ raise IOError
+
+ class MyStream2(io.IOBase):
+ def flush(self):
+ raise ValueError
+
+ s = MyStream()
+ s2 = MyStream2()
+ import sys; sys._keepalivesomewhereobscure = s
+ """)
+ space.finish() # the IOError has been ignored
diff --git a/pypy/module/_lsprof/interp_lsprof.py b/pypy/module/_lsprof/interp_lsprof.py
--- a/pypy/module/_lsprof/interp_lsprof.py
+++ b/pypy/module/_lsprof/interp_lsprof.py
@@ -22,7 +22,7 @@
eci = ExternalCompilationInfo(
separate_module_files=[srcdir.join('profiling.c')],
export_symbols=['pypy_setup_profiling', 'pypy_teardown_profiling'])
-
+
c_setup_profiling = rffi.llexternal('pypy_setup_profiling',
[], lltype.Void,
compilation_info = eci)
@@ -228,7 +228,7 @@
if w_self.builtins:
key = create_spec(space, w_arg)
w_self._enter_builtin_call(key)
- elif event == 'c_return':
+ elif event == 'c_return' or event == 'c_exception':
if w_self.builtins:
key = create_spec(space, w_arg)
w_self._enter_builtin_return(key)
@@ -237,7 +237,7 @@
pass
class W_Profiler(Wrappable):
-
+
def __init__(self, space, w_callable, time_unit, subcalls, builtins):
self.subcalls = subcalls
self.builtins = builtins
diff --git a/pypy/module/_lsprof/test/test_cprofile.py b/pypy/module/_lsprof/test/test_cprofile.py
--- a/pypy/module/_lsprof/test/test_cprofile.py
+++ b/pypy/module/_lsprof/test/test_cprofile.py
@@ -117,6 +117,20 @@
assert 0.9 < subentry.totaltime < 2.9
#assert 0.9 < subentry.inlinetime < 2.9
+ def test_builtin_exception(self):
+ import math
+ import _lsprof
+
+ prof = _lsprof.Profiler()
+ prof.enable()
+ try:
+ math.sqrt("a")
+ except TypeError:
+ pass
+ prof.disable()
+ stats = prof.getstats()
+ assert len(stats) == 2
+
def test_use_cprofile(self):
import sys, os
# XXX this is evil trickery to walk around the fact that we don't
diff --git a/pypy/module/_md5/test/test_md5.py b/pypy/module/_md5/test/test_md5.py
--- a/pypy/module/_md5/test/test_md5.py
+++ b/pypy/module/_md5/test/test_md5.py
@@ -28,7 +28,7 @@
assert self.md5.digest_size == 16
#assert self.md5.digestsize == 16 -- not on CPython
assert self.md5.md5().digest_size == 16
- if sys.version >= (2, 5):
+ if sys.version_info >= (2, 5):
assert self.md5.blocksize == 1
assert self.md5.md5().digestsize == 16
diff --git a/pypy/module/_multiprocessing/test/test_semaphore.py b/pypy/module/_multiprocessing/test/test_semaphore.py
--- a/pypy/module/_multiprocessing/test/test_semaphore.py
+++ b/pypy/module/_multiprocessing/test/test_semaphore.py
@@ -2,6 +2,7 @@
from pypy.module._multiprocessing.interp_semaphore import (
RECURSIVE_MUTEX, SEMAPHORE)
+
class AppTestSemaphore:
def setup_class(cls):
space = gettestobjspace(usemodules=('_multiprocessing', 'thread'))
diff --git a/pypy/module/_ssl/test/test_ssl.py b/pypy/module/_ssl/test/test_ssl.py
--- a/pypy/module/_ssl/test/test_ssl.py
+++ b/pypy/module/_ssl/test/test_ssl.py
@@ -2,6 +2,7 @@
import os
import py
+
class AppTestSSL:
def setup_class(cls):
space = gettestobjspace(usemodules=('_ssl', '_socket'))
@@ -29,7 +30,6 @@
assert isinstance(_ssl.SSL_ERROR_EOF, int)
assert isinstance(_ssl.SSL_ERROR_INVALID_ERROR_CODE, int)
- assert isinstance(_ssl.OPENSSL_VERSION_NUMBER, (int, long))
assert isinstance(_ssl.OPENSSL_VERSION_INFO, tuple)
assert len(_ssl.OPENSSL_VERSION_INFO) == 5
assert isinstance(_ssl.OPENSSL_VERSION, str)
diff --git a/pypy/module/array/interp_array.py b/pypy/module/array/interp_array.py
--- a/pypy/module/array/interp_array.py
+++ b/pypy/module/array/interp_array.py
@@ -11,6 +11,7 @@
from pypy.objspace.std.register_all import register_all
from pypy.rlib.rarithmetic import ovfcheck
from pypy.rlib.unroll import unrolling_iterable
+from pypy.rlib.objectmodel import specialize
from pypy.rpython.lltypesystem import lltype, rffi
@@ -159,13 +160,15 @@
def make_array(mytype):
+ W_ArrayBase = globals()['W_ArrayBase']
+
class W_Array(W_ArrayBase):
itemsize = mytype.bytes
typecode = mytype.typecode
@staticmethod
def register(typeorder):
- typeorder[W_Array] = []
+ typeorder[W_Array] = [(W_ArrayBase, None)]
def __init__(self, space):
self.space = space
@@ -583,13 +586,29 @@
raise OperationError(space.w_ValueError, space.wrap(msg))
# Compare methods
- def cmp__Array_ANY(space, self, other):
- if isinstance(other, W_ArrayBase):
- w_lst1 = array_tolist__Array(space, self)
- w_lst2 = space.call_method(other, 'tolist')
- return space.cmp(w_lst1, w_lst2)
- else:
- return space.w_NotImplemented
+ @specialize.arg(3)
+ def _cmp_impl(space, self, other, space_fn):
+ w_lst1 = array_tolist__Array(space, self)
+ w_lst2 = space.call_method(other, 'tolist')
+ return space_fn(w_lst1, w_lst2)
+
+ def eq__Array_ArrayBase(space, self, other):
+ return _cmp_impl(space, self, other, space.eq)
+
+ def ne__Array_ArrayBase(space, self, other):
+ return _cmp_impl(space, self, other, space.ne)
+
+ def lt__Array_ArrayBase(space, self, other):
+ return _cmp_impl(space, self, other, space.lt)
+
+ def le__Array_ArrayBase(space, self, other):
+ return _cmp_impl(space, self, other, space.le)
+
+ def gt__Array_ArrayBase(space, self, other):
+ return _cmp_impl(space, self, other, space.gt)
+
+ def ge__Array_ArrayBase(space, self, other):
+ return _cmp_impl(space, self, other, space.ge)
# Misc methods
diff --git a/pypy/module/array/test/test_array.py b/pypy/module/array/test/test_array.py
--- a/pypy/module/array/test/test_array.py
+++ b/pypy/module/array/test/test_array.py
@@ -536,12 +536,6 @@
assert (a >= c) is False
assert (c >= a) is True
- assert cmp(a, a) == 0
- assert cmp(a, b) == 0
- assert cmp(a, c) < 0
- assert cmp(b, a) == 0
- assert cmp(c, a) > 0
-
def test_reduce(self):
import pickle
a = self.array('i', [1, 2, 3])
@@ -851,8 +845,11 @@
cls.maxint = sys.maxint
class AppTestArray(BaseArrayTests):
+ OPTIONS = {}
+
def setup_class(cls):
- cls.space = gettestobjspace(usemodules=('array', 'struct', '_rawffi'))
+ cls.space = gettestobjspace(usemodules=('array', 'struct', '_rawffi'),
+ **cls.OPTIONS)
cls.w_array = cls.space.appexec([], """():
import array
return array.array
@@ -874,3 +871,7 @@
a = self.array('b', range(4))
a[::-1] = a
assert a == self.array('b', [3, 2, 1, 0])
+
+
+class AppTestArrayBuiltinShortcut(AppTestArray):
+ OPTIONS = {'objspace.std.builtinshortcut': True}
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -352,6 +352,9 @@
'PyObject_AsReadBuffer', 'PyObject_AsWriteBuffer', 'PyObject_CheckReadBuffer',
'PyOS_getsig', 'PyOS_setsig',
+ 'PyThread_create_key', 'PyThread_delete_key', 'PyThread_set_key_value',
+ 'PyThread_get_key_value', 'PyThread_delete_key_value',
+ 'PyThread_ReInitTLS',
'PyStructSequence_InitType', 'PyStructSequence_New',
]
@@ -385,6 +388,7 @@
"Tuple": "space.w_tuple",
"List": "space.w_list",
"Set": "space.w_set",
+ "FrozenSet": "space.w_frozenset",
"Int": "space.w_int",
"Bool": "space.w_bool",
"Float": "space.w_float",
@@ -406,7 +410,7 @@
}.items():
GLOBALS['Py%s_Type#' % (cpyname, )] = ('PyTypeObject*', pypyexpr)
- for cpyname in 'Method List Int Long Dict Tuple Class'.split():
+ for cpyname in 'Method List Long Dict Tuple Class'.split():
FORWARD_DECLS.append('typedef struct { PyObject_HEAD } '
'Py%sObject' % (cpyname, ))
build_exported_objects()
@@ -616,6 +620,10 @@
lambda space: init_pycobject(),
lambda space: init_capsule(),
])
+ from pypy.module.posix.interp_posix import add_fork_hook
+ reinit_tls = rffi.llexternal('PyThread_ReInitTLS', [], lltype.Void,
+ compilation_info=eci)
+ add_fork_hook('child', reinit_tls)
def init_function(func):
INIT_FUNCTIONS.append(func)
@@ -816,6 +824,8 @@
pypy_decls.append("#ifdef __cplusplus")
pypy_decls.append("extern \"C\" {")
pypy_decls.append("#endif\n")
+ pypy_decls.append('#define Signed long /* xxx temporary fix */\n')
+ pypy_decls.append('#define Unsigned unsigned long /* xxx temporary fix */\n')
for decl in FORWARD_DECLS:
pypy_decls.append("%s;" % (decl,))
@@ -847,6 +857,8 @@
typ = 'PyObject*'
pypy_decls.append('PyAPI_DATA(%s) %s;' % (typ, name))
+ pypy_decls.append('#undef Signed /* xxx temporary fix */\n')
+ pypy_decls.append('#undef Unsigned /* xxx temporary fix */\n')
pypy_decls.append("#ifdef __cplusplus")
pypy_decls.append("}")
pypy_decls.append("#endif")
@@ -925,6 +937,7 @@
source_dir / "structseq.c",
source_dir / "capsule.c",
source_dir / "pysignals.c",
+ source_dir / "thread.c",
],
separate_module_sources=separate_module_sources,
export_symbols=export_symbols_eci,
diff --git a/pypy/module/cpyext/eval.py b/pypy/module/cpyext/eval.py
--- a/pypy/module/cpyext/eval.py
+++ b/pypy/module/cpyext/eval.py
@@ -1,16 +1,24 @@
from pypy.interpreter.error import OperationError
+from pypy.interpreter.astcompiler import consts
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import (
cpython_api, CANNOT_FAIL, CONST_STRING, FILEP, fread, feof, Py_ssize_tP,
cpython_struct)
from pypy.module.cpyext.pyobject import PyObject, borrow_from
from pypy.module.cpyext.pyerrors import PyErr_SetFromErrno
+from pypy.module.cpyext.funcobject import PyCodeObject
from pypy.module.__builtin__ import compiling
PyCompilerFlags = cpython_struct(
- "PyCompilerFlags", ())
+ "PyCompilerFlags", (("cf_flags", rffi.INT),))
PyCompilerFlagsPtr = lltype.Ptr(PyCompilerFlags)
+PyCF_MASK = (consts.CO_FUTURE_DIVISION |
+ consts.CO_FUTURE_ABSOLUTE_IMPORT |
+ consts.CO_FUTURE_WITH_STATEMENT |
+ consts.CO_FUTURE_PRINT_FUNCTION |
+ consts.CO_FUTURE_UNICODE_LITERALS)
+
@cpython_api([PyObject, PyObject, PyObject], PyObject)
def PyEval_CallObjectWithKeywords(space, w_obj, w_arg, w_kwds):
return space.call(w_obj, w_arg, w_kwds)
@@ -48,6 +56,17 @@
return None
return borrow_from(None, caller.w_globals)
+ at cpython_api([PyCodeObject, PyObject, PyObject], PyObject)
+def PyEval_EvalCode(space, w_code, w_globals, w_locals):
+ """This is a simplified interface to PyEval_EvalCodeEx(), with just
+ the code object, and the dictionaries of global and local variables.
+ The other arguments are set to NULL."""
+ if w_globals is None:
+ w_globals = space.w_None
+ if w_locals is None:
+ w_locals = space.w_None
+ return compiling.eval(space, w_code, w_globals, w_locals)
+
@cpython_api([PyObject, PyObject], PyObject)
def PyObject_CallObject(space, w_obj, w_arg):
"""
@@ -74,7 +93,7 @@
Py_file_input = 257
Py_eval_input = 258
-def compile_string(space, source, filename, start):
+def compile_string(space, source, filename, start, flags=0):
w_source = space.wrap(source)
start = rffi.cast(lltype.Signed, start)
if start == Py_file_input:
@@ -86,7 +105,7 @@
else:
raise OperationError(space.w_ValueError, space.wrap(
"invalid mode parameter for compilation"))
- return compiling.compile(space, w_source, filename, mode)
+ return compiling.compile(space, w_source, filename, mode, flags)
def run_string(space, source, filename, start, w_globals, w_locals):
w_code = compile_string(space, source, filename, start)
@@ -109,6 +128,24 @@
filename = "<string>"
return run_string(space, source, filename, start, w_globals, w_locals)
+ at cpython_api([rffi.CCHARP, rffi.INT_real, PyObject, PyObject,
+ PyCompilerFlagsPtr], PyObject)
+def PyRun_StringFlags(space, source, start, w_globals, w_locals, flagsptr):
+ """Execute Python source code from str in the context specified by the
+ dictionaries globals and locals with the compiler flags specified by
+ flags. The parameter start specifies the start token that should be used to
+ parse the source code.
+
+ Returns the result of executing the code as a Python object, or NULL if an
+ exception was raised."""
+ source = rffi.charp2str(source)
+ if flagsptr:
+ flags = rffi.cast(lltype.Signed, flagsptr.c_cf_flags)
+ else:
+ flags = 0
+ w_code = compile_string(space, source, "<string>", start, flags)
+ return compiling.eval(space, w_code, w_globals, w_locals)
+
@cpython_api([FILEP, CONST_STRING, rffi.INT_real, PyObject, PyObject], PyObject)
def PyRun_File(space, fp, filename, start, w_globals, w_locals):
"""This is a simplified interface to PyRun_FileExFlags() below, leaving
@@ -150,7 +187,7 @@
@cpython_api([rffi.CCHARP, rffi.CCHARP, rffi.INT_real, PyCompilerFlagsPtr],
PyObject)
-def Py_CompileStringFlags(space, source, filename, start, flags):
+def Py_CompileStringFlags(space, source, filename, start, flagsptr):
"""Parse and compile the Python source code in str, returning the
resulting code object. The start token is given by start; this
can be used to constrain the code which can be compiled and should
@@ -160,7 +197,30 @@
returns NULL if the code cannot be parsed or compiled."""
source = rffi.charp2str(source)
filename = rffi.charp2str(filename)
- if flags:
- raise OperationError(space.w_NotImplementedError, space.wrap(
- "cpyext Py_CompileStringFlags does not accept flags"))
- return compile_string(space, source, filename, start)
+ if flagsptr:
+ flags = rffi.cast(lltype.Signed, flagsptr.c_cf_flags)
+ else:
+ flags = 0
+ return compile_string(space, source, filename, start, flags)
+
+ at cpython_api([PyCompilerFlagsPtr], rffi.INT_real, error=CANNOT_FAIL)
+def PyEval_MergeCompilerFlags(space, cf):
+ """This function changes the flags of the current evaluation
+ frame, and returns true on success, false on failure."""
+ flags = rffi.cast(lltype.Signed, cf.c_cf_flags)
+ result = flags != 0
+ current_frame = space.getexecutioncontext().gettopframe_nohidden()
+ if current_frame:
+ codeflags = current_frame.pycode.co_flags
+ compilerflags = codeflags & PyCF_MASK
+ if compilerflags:
+ result = 1
+ flags |= compilerflags
+ # No future keyword at the moment
+ # if codeflags & CO_GENERATOR_ALLOWED:
+ # result = 1
+ # flags |= CO_GENERATOR_ALLOWED
+ cf.c_cf_flags = rffi.cast(rffi.INT, flags)
+ return result
+
+
diff --git a/pypy/module/cpyext/funcobject.py b/pypy/module/cpyext/funcobject.py
--- a/pypy/module/cpyext/funcobject.py
+++ b/pypy/module/cpyext/funcobject.py
@@ -1,6 +1,6 @@
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import (
- PyObjectFields, generic_cpy_call, CONST_STRING,
+ PyObjectFields, generic_cpy_call, CONST_STRING, CANNOT_FAIL,
cpython_api, bootstrap_function, cpython_struct, build_type_checkers)
from pypy.module.cpyext.pyobject import (
PyObject, make_ref, from_ref, Py_DecRef, make_typedescr, borrow_from)
@@ -48,6 +48,7 @@
PyFunction_Check, PyFunction_CheckExact = build_type_checkers("Function", Function)
PyMethod_Check, PyMethod_CheckExact = build_type_checkers("Method", Method)
+PyCode_Check, PyCode_CheckExact = build_type_checkers("Code", PyCode)
def function_attach(space, py_obj, w_obj):
py_func = rffi.cast(PyFunctionObject, py_obj)
@@ -167,3 +168,9 @@
freevars=[],
cellvars=[]))
+ at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyCode_GetNumFree(space, w_co):
+ """Return the number of free variables in co."""
+ co = space.interp_w(PyCode, w_co)
+ return len(co.co_freevars)
+
diff --git a/pypy/module/cpyext/include/Python.h b/pypy/module/cpyext/include/Python.h
--- a/pypy/module/cpyext/include/Python.h
+++ b/pypy/module/cpyext/include/Python.h
@@ -113,6 +113,7 @@
#include "compile.h"
#include "frameobject.h"
#include "eval.h"
+#include "pymath.h"
#include "pymem.h"
#include "pycobject.h"
#include "pycapsule.h"
diff --git a/pypy/module/cpyext/include/code.h b/pypy/module/cpyext/include/code.h
--- a/pypy/module/cpyext/include/code.h
+++ b/pypy/module/cpyext/include/code.h
@@ -13,13 +13,19 @@
/* Masks for co_flags above */
/* These values are also in funcobject.py */
-#define CO_OPTIMIZED 0x0001
-#define CO_NEWLOCALS 0x0002
-#define CO_VARARGS 0x0004
-#define CO_VARKEYWORDS 0x0008
+#define CO_OPTIMIZED 0x0001
+#define CO_NEWLOCALS 0x0002
+#define CO_VARARGS 0x0004
+#define CO_VARKEYWORDS 0x0008
#define CO_NESTED 0x0010
#define CO_GENERATOR 0x0020
+#define CO_FUTURE_DIVISION 0x02000
+#define CO_FUTURE_ABSOLUTE_IMPORT 0x04000
+#define CO_FUTURE_WITH_STATEMENT 0x08000
+#define CO_FUTURE_PRINT_FUNCTION 0x10000
+#define CO_FUTURE_UNICODE_LITERALS 0x20000
+
#ifdef __cplusplus
}
#endif
diff --git a/pypy/module/cpyext/include/intobject.h b/pypy/module/cpyext/include/intobject.h
--- a/pypy/module/cpyext/include/intobject.h
+++ b/pypy/module/cpyext/include/intobject.h
@@ -7,6 +7,11 @@
extern "C" {
#endif
+typedef struct {
+ PyObject_HEAD
+ long ob_ival;
+} PyIntObject;
+
#ifdef __cplusplus
}
#endif
diff --git a/pypy/module/cpyext/include/object.h b/pypy/module/cpyext/include/object.h
--- a/pypy/module/cpyext/include/object.h
+++ b/pypy/module/cpyext/include/object.h
@@ -56,6 +56,8 @@
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
+#define _Py_ForgetReference(ob) /* nothing */
+
#define Py_None (&_Py_NoneStruct)
/*
diff --git a/pypy/module/cpyext/include/pymath.h b/pypy/module/cpyext/include/pymath.h
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/include/pymath.h
@@ -0,0 +1,20 @@
+#ifndef Py_PYMATH_H
+#define Py_PYMATH_H
+
+/**************************************************************************
+Symbols and macros to supply platform-independent interfaces to mathematical
+functions and constants
+**************************************************************************/
+
+/* HUGE_VAL is supposed to expand to a positive double infinity. Python
+ * uses Py_HUGE_VAL instead because some platforms are broken in this
+ * respect. We used to embed code in pyport.h to try to worm around that,
+ * but different platforms are broken in conflicting ways. If you're on
+ * a platform where HUGE_VAL is defined incorrectly, fiddle your Python
+ * config to #define Py_HUGE_VAL to something that works on your platform.
+ */
+#ifndef Py_HUGE_VAL
+#define Py_HUGE_VAL HUGE_VAL
+#endif
+
+#endif /* Py_PYMATH_H */
diff --git a/pypy/module/cpyext/include/pythonrun.h b/pypy/module/cpyext/include/pythonrun.h
--- a/pypy/module/cpyext/include/pythonrun.h
+++ b/pypy/module/cpyext/include/pythonrun.h
@@ -19,6 +19,14 @@
int cf_flags; /* bitmask of CO_xxx flags relevant to future */
} PyCompilerFlags;
+#define PyCF_MASK (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT | \
+ CO_FUTURE_WITH_STATEMENT | CO_FUTURE_PRINT_FUNCTION | \
+ CO_FUTURE_UNICODE_LITERALS)
+#define PyCF_MASK_OBSOLETE (CO_NESTED)
+#define PyCF_SOURCE_IS_UTF8 0x0100
+#define PyCF_DONT_IMPLY_DEDENT 0x0200
+#define PyCF_ONLY_AST 0x0400
+
#define Py_CompileString(str, filename, start) Py_CompileStringFlags(str, filename, start, NULL)
#ifdef __cplusplus
diff --git a/pypy/module/cpyext/include/pythread.h b/pypy/module/cpyext/include/pythread.h
--- a/pypy/module/cpyext/include/pythread.h
+++ b/pypy/module/cpyext/include/pythread.h
@@ -3,8 +3,26 @@
#define WITH_THREAD
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef void *PyThread_type_lock;
#define WAIT_LOCK 1
#define NOWAIT_LOCK 0
+/* Thread Local Storage (TLS) API */
+PyAPI_FUNC(int) PyThread_create_key(void);
+PyAPI_FUNC(void) PyThread_delete_key(int);
+PyAPI_FUNC(int) PyThread_set_key_value(int, void *);
+PyAPI_FUNC(void *) PyThread_get_key_value(int);
+PyAPI_FUNC(void) PyThread_delete_key_value(int key);
+
+/* Cleanup after a fork */
+PyAPI_FUNC(void) PyThread_ReInitTLS(void);
+
+#ifdef __cplusplus
+}
#endif
+
+#endif
diff --git a/pypy/module/cpyext/intobject.py b/pypy/module/cpyext/intobject.py
--- a/pypy/module/cpyext/intobject.py
+++ b/pypy/module/cpyext/intobject.py
@@ -2,11 +2,37 @@
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.interpreter.error import OperationError
from pypy.module.cpyext.api import (
- cpython_api, build_type_checkers, PyObject,
- CONST_STRING, CANNOT_FAIL, Py_ssize_t)
+ cpython_api, cpython_struct, build_type_checkers, bootstrap_function,
+ PyObject, PyObjectFields, CONST_STRING, CANNOT_FAIL, Py_ssize_t)
+from pypy.module.cpyext.pyobject import (
+ make_typedescr, track_reference, RefcountState, from_ref)
from pypy.rlib.rarithmetic import r_uint, intmask, LONG_TEST
+from pypy.objspace.std.intobject import W_IntObject
import sys
+PyIntObjectStruct = lltype.ForwardReference()
+PyIntObject = lltype.Ptr(PyIntObjectStruct)
+PyIntObjectFields = PyObjectFields + \
+ (("ob_ival", rffi.LONG),)
+cpython_struct("PyIntObject", PyIntObjectFields, PyIntObjectStruct)
+
+ at bootstrap_function
+def init_intobject(space):
+ "Type description of PyIntObject"
+ make_typedescr(space.w_int.instancetypedef,
+ basestruct=PyIntObject.TO,
+ realize=int_realize)
+
+def int_realize(space, obj):
+ intval = rffi.cast(lltype.Signed, rffi.cast(PyIntObject, obj).c_ob_ival)
+ w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
+ w_obj = space.allocate_instance(W_IntObject, w_type)
+ w_obj.__init__(intval)
+ track_reference(space, obj, w_obj)
+ state = space.fromcache(RefcountState)
+ state.set_lifeline(w_obj, obj)
+ return w_obj
+
PyInt_Check, PyInt_CheckExact = build_type_checkers("Int")
@cpython_api([], lltype.Signed, error=CANNOT_FAIL)
diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -193,7 +193,7 @@
if not obj:
PyErr_NoMemory(space)
obj.c_ob_type = type
- _Py_NewReference(space, obj)
+ obj.c_ob_refcnt = 1
return obj
@cpython_api([PyVarObject, PyTypeObjectPtr, Py_ssize_t], PyObject)
diff --git a/pypy/module/cpyext/pyobject.py b/pypy/module/cpyext/pyobject.py
--- a/pypy/module/cpyext/pyobject.py
+++ b/pypy/module/cpyext/pyobject.py
@@ -17,6 +17,7 @@
class BaseCpyTypedescr(object):
basestruct = PyObject.TO
+ W_BaseObject = W_ObjectObject
def get_dealloc(self, space):
from pypy.module.cpyext.typeobject import subtype_dealloc
@@ -51,10 +52,14 @@
def attach(self, space, pyobj, w_obj):
pass
- def realize(self, space, ref):
- # For most types, a reference cannot exist without
- # a real interpreter object
- raise InvalidPointerException(str(ref))
+ def realize(self, space, obj):
+ w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
+ w_obj = space.allocate_instance(self.W_BaseObject, w_type)
+ track_reference(space, obj, w_obj)
+ if w_type is not space.gettypefor(self.W_BaseObject):
+ state = space.fromcache(RefcountState)
+ state.set_lifeline(w_obj, obj)
+ return w_obj
typedescr_cache = {}
@@ -369,13 +374,7 @@
obj.c_ob_refcnt = 1
w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
assert isinstance(w_type, W_TypeObject)
- if w_type.is_cpytype():
- w_obj = space.allocate_instance(W_ObjectObject, w_type)
- track_reference(space, obj, w_obj)
- state = space.fromcache(RefcountState)
- state.set_lifeline(w_obj, obj)
- else:
- assert False, "Please add more cases in _Py_NewReference()"
+ get_typedescr(w_type.instancetypedef).realize(space, obj)
def _Py_Dealloc(space, obj):
from pypy.module.cpyext.api import generic_cpy_call_dont_decref
diff --git a/pypy/module/cpyext/pystate.py b/pypy/module/cpyext/pystate.py
--- a/pypy/module/cpyext/pystate.py
+++ b/pypy/module/cpyext/pystate.py
@@ -10,7 +10,7 @@
[('next', PyInterpreterState)],
PyInterpreterStateStruct)
PyThreadState = lltype.Ptr(cpython_struct(
- "PyThreadState",
+ "PyThreadState",
[('interp', PyInterpreterState),
('dict', PyObject),
]))
@@ -19,12 +19,15 @@
def PyEval_SaveThread(space):
"""Release the global interpreter lock (if it has been created and thread
support is enabled) and reset the thread state to NULL, returning the
- previous thread state (which is not NULL except in PyPy). If the lock has been created,
+ previous thread state. If the lock has been created,
the current thread must have acquired it. (This function is available even
when thread support is disabled at compile time.)"""
+ state = space.fromcache(InterpreterState)
if rffi.aroundstate.before:
rffi.aroundstate.before()
- return lltype.nullptr(PyThreadState.TO)
+ tstate = state.swap_thread_state(
+ space, lltype.nullptr(PyThreadState.TO))
+ return tstate
@cpython_api([PyThreadState], lltype.Void)
def PyEval_RestoreThread(space, tstate):
@@ -35,6 +38,8 @@
when thread support is disabled at compile time.)"""
if rffi.aroundstate.after:
rffi.aroundstate.after()
+ state = space.fromcache(InterpreterState)
+ state.swap_thread_state(space, tstate)
@cpython_api([], lltype.Void)
def PyEval_InitThreads(space):
@@ -67,28 +72,91 @@
dealloc=ThreadState_dealloc)
from pypy.interpreter.executioncontext import ExecutionContext
+
+# Keep track of the ThreadStateCapsule for a particular execution context. The
+# default is for new execution contexts not to have one; it is allocated on the
+# first cpyext-based request for it.
ExecutionContext.cpyext_threadstate = ThreadStateCapsule(None)
+# Also keep track of whether it has been initialized yet or not (None is a valid
+# PyThreadState for an execution context to have, when the GIL has been
+# released, so a check against that can't be used to determine the need for
+# initialization).
+ExecutionContext.cpyext_initialized_threadstate = False
+
+def cleanup_cpyext_state(self):
+ try:
+ del self.cpyext_threadstate
+ except AttributeError:
+ pass
+ self.cpyext_initialized_threadstate = False
+ExecutionContext.cleanup_cpyext_state = cleanup_cpyext_state
+
class InterpreterState(object):
def __init__(self, space):
self.interpreter_state = lltype.malloc(
PyInterpreterState.TO, flavor='raw', zero=True, immortal=True)
def new_thread_state(self, space):
+ """
+ Create a new ThreadStateCapsule to hold the PyThreadState for a
+ particular execution context.
+
+ :param space: A space.
+
+ :returns: A new ThreadStateCapsule holding a newly allocated
+ PyThreadState and referring to this interpreter state.
+ """
capsule = ThreadStateCapsule(space)
ts = capsule.memory
ts.c_interp = self.interpreter_state
ts.c_dict = make_ref(space, space.newdict())
return capsule
+
def get_thread_state(self, space):
+ """
+ Get the current PyThreadState for the current execution context.
+
+ :param space: A space.
+
+ :returns: The current PyThreadState for the current execution context,
+ or None if it does not have one.
+ """
ec = space.getexecutioncontext()
return self._get_thread_state(space, ec).memory
+
+ def swap_thread_state(self, space, tstate):
+ """
+ Replace the current thread state of the current execution context with a
+ new thread state.
+
+ :param space: The space.
+
+ :param tstate: The new PyThreadState for the current execution context.
+
+ :returns: The old thread state for the current execution context, either
+ None or a PyThreadState.
+ """
+ ec = space.getexecutioncontext()
+ capsule = self._get_thread_state(space, ec)
+ old_tstate = capsule.memory
+ capsule.memory = tstate
+ return old_tstate
+
def _get_thread_state(self, space, ec):
- if ec.cpyext_threadstate.memory == lltype.nullptr(PyThreadState.TO):
+ """
+ Get the ThreadStateCapsule for the given execution context, possibly
+ creating a new one if it does not already have one.
+
+ :param space: The space.
+ :param ec: The ExecutionContext of which to get the thread state.
+ :returns: The ThreadStateCapsule for the given execution context.
+ """
+ if not ec.cpyext_initialized_threadstate:
ec.cpyext_threadstate = self.new_thread_state(space)
-
+ ec.cpyext_initialized_threadstate = True
return ec.cpyext_threadstate
@cpython_api([], PyThreadState, error=CANNOT_FAIL)
@@ -105,13 +173,8 @@
def PyThreadState_Swap(space, tstate):
"""Swap the current thread state with the thread state given by the argument
tstate, which may be NULL. The global interpreter lock must be held."""
- # All cpyext calls release and acquire the GIL, so this function has no
- # side-effects
- if tstate:
- return lltype.nullptr(PyThreadState.TO)
- else:
- state = space.fromcache(InterpreterState)
- return state.get_thread_state(space)
+ state = space.fromcache(InterpreterState)
+ return state.swap_thread_state(space, tstate)
@cpython_api([PyThreadState], lltype.Void)
def PyEval_AcquireThread(space, tstate):
diff --git a/pypy/module/cpyext/src/getargs.c b/pypy/module/cpyext/src/getargs.c
--- a/pypy/module/cpyext/src/getargs.c
+++ b/pypy/module/cpyext/src/getargs.c
@@ -23,16 +23,33 @@
#define FLAG_COMPAT 1
#define FLAG_SIZE_T 2
+typedef int (*destr_t)(PyObject *, void *);
+
+
+/* Keep track of "objects" that have been allocated or initialized and
+ which will need to be deallocated or cleaned up somehow if overall
+ parsing fails.
+*/
+typedef struct {
+ void *item;
+ destr_t destructor;
+} freelistentry_t;
+
+typedef struct {
+ int first_available;
+ freelistentry_t *entries;
+} freelist_t;
+
/* Forward */
static int vgetargs1(PyObject *, const char *, va_list *, int);
static void seterror(int, const char *, int *, const char *, const char *);
static char *convertitem(PyObject *, const char **, va_list *, int, int *,
- char *, size_t, PyObject **);
+ char *, size_t, freelist_t *);
static char *converttuple(PyObject *, const char **, va_list *, int,
- int *, char *, size_t, int, PyObject **);
+ int *, char *, size_t, int, freelist_t *);
static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
- size_t, PyObject **);
+ size_t, freelist_t *);
static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
static int getbuffer(PyObject *, Py_buffer *, char**);
@@ -129,57 +146,56 @@
/* Handle cleanup of allocated memory in case of exception */
-static void
-cleanup_ptr(void *ptr)
+static int
+cleanup_ptr(PyObject *self, void *ptr)
{
- PyMem_FREE(ptr);
-}
-
-static void
-cleanup_buffer(void *ptr)
-{
- PyBuffer_Release((Py_buffer *) ptr);
+ if (ptr) {
+ PyMem_FREE(ptr);
+ }
+ return 0;
}
static int
-addcleanup(void *ptr, PyObject **freelist, void (*destr)(void *))
+cleanup_buffer(PyObject *self, void *ptr)
{
- PyObject *cobj;
- if (!*freelist) {
- *freelist = PyList_New(0);
- if (!*freelist) {
- destr(ptr);
- return -1;
- }
- }
- cobj = PyCObject_FromVoidPtr(ptr, destr);
- if (!cobj) {
- destr(ptr);
- return -1;
- }
- if (PyList_Append(*freelist, cobj)) {
- Py_DECREF(cobj);
- return -1;
- }
- Py_DECREF(cobj);
- return 0;
+ Py_buffer *buf = (Py_buffer *)ptr;
+ if (buf) {
+ PyBuffer_Release(buf);
+ }
+ return 0;
}
static int
-cleanreturn(int retval, PyObject *freelist)
+addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
{
- if (freelist && retval != 0) {
- /* We were successful, reset the destructors so that they
- don't get called. */
- Py_ssize_t len = PyList_GET_SIZE(freelist), i;
- for (i = 0; i < len; i++)
- ((PyCObject *) PyList_GET_ITEM(freelist, i))
- ->destructor = NULL;
- }
- Py_XDECREF(freelist);
- return retval;
+ int index;
+
+ index = freelist->first_available;
+ freelist->first_available += 1;
+
+ freelist->entries[index].item = ptr;
+ freelist->entries[index].destructor = destructor;
+
+ return 0;
}
+static int
+cleanreturn(int retval, freelist_t *freelist)
+{
+ int index;
+
+ if (retval == 0) {
+ /* A failure occurred, therefore execute all of the cleanup
+ functions.
+ */
+ for (index = 0; index < freelist->first_available; ++index) {
+ freelist->entries[index].destructor(NULL,
+ freelist->entries[index].item);
+ }
+ }
+ PyMem_Free(freelist->entries);
+ return retval;
+}
static int
vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
@@ -195,7 +211,7 @@
const char *formatsave = format;
Py_ssize_t i, len;
char *msg;
- PyObject *freelist = NULL;
+ freelist_t freelist = {0, NULL};
int compat = flags & FLAG_COMPAT;
assert(compat || (args != (PyObject*)NULL));
@@ -251,16 +267,18 @@
format = formatsave;
+ freelist.entries = PyMem_New(freelistentry_t, max);
+
if (compat) {
if (max == 0) {
if (args == NULL)
- return 1;
+ return cleanreturn(1, &freelist);
PyOS_snprintf(msgbuf, sizeof(msgbuf),
"%.200s%s takes no arguments",
fname==NULL ? "function" : fname,
fname==NULL ? "" : "()");
PyErr_SetString(PyExc_TypeError, msgbuf);
- return 0;
+ return cleanreturn(0, &freelist);
}
else if (min == 1 && max == 1) {
if (args == NULL) {
@@ -269,26 +287,26 @@
fname==NULL ? "function" : fname,
fname==NULL ? "" : "()");
PyErr_SetString(PyExc_TypeError, msgbuf);
- return 0;
+ return cleanreturn(0, &freelist);
}
msg = convertitem(args, &format, p_va, flags, levels,
msgbuf, sizeof(msgbuf), &freelist);
if (msg == NULL)
- return cleanreturn(1, freelist);
+ return cleanreturn(1, &freelist);
seterror(levels[0], msg, levels+1, fname, message);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
else {
PyErr_SetString(PyExc_SystemError,
"old style getargs format uses new features");
- return 0;
+ return cleanreturn(0, &freelist);
}
}
if (!PyTuple_Check(args)) {
PyErr_SetString(PyExc_SystemError,
"new style getargs format but argument is not a tuple");
- return 0;
+ return cleanreturn(0, &freelist);
}
len = PyTuple_GET_SIZE(args);
@@ -308,7 +326,7 @@
message = msgbuf;
}
PyErr_SetString(PyExc_TypeError, message);
- return 0;
+ return cleanreturn(0, &freelist);
}
for (i = 0; i < len; i++) {
@@ -319,7 +337,7 @@
sizeof(msgbuf), &freelist);
if (msg) {
seterror(i+1, msg, levels, fname, message);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
}
@@ -328,10 +346,10 @@
*format != '|' && *format != ':' && *format != ';') {
PyErr_Format(PyExc_SystemError,
"bad format string: %.200s", formatsave);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
- return cleanreturn(1, freelist);
+ return cleanreturn(1, &freelist);
}
@@ -395,7 +413,7 @@
static char *
converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
int *levels, char *msgbuf, size_t bufsize, int toplevel,
- PyObject **freelist)
+ freelist_t *freelist)
{
int level = 0;
int n = 0;
@@ -472,7 +490,7 @@
static char *
convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
- int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
+ int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
{
char *msg;
const char *format = *p_format;
@@ -539,7 +557,7 @@
static char *
convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
- char *msgbuf, size_t bufsize, PyObject **freelist)
+ char *msgbuf, size_t bufsize, freelist_t *freelist)
{
/* For # codes */
#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
@@ -1501,7 +1519,9 @@
const char *fname, *msg, *custom_msg, *keyword;
int min = INT_MAX;
int i, len, nargs, nkeywords;
- PyObject *freelist = NULL, *current_arg;
+ PyObject *current_arg;
+ freelist_t freelist = {0, NULL};
+
assert(args != NULL && PyTuple_Check(args));
assert(keywords == NULL || PyDict_Check(keywords));
@@ -1525,6 +1545,8 @@
for (len=0; kwlist[len]; len++)
continue;
+ freelist.entries = PyMem_New(freelistentry_t, len);
+
nargs = PyTuple_GET_SIZE(args);
nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
if (nargs + nkeywords > len) {
@@ -1535,7 +1557,7 @@
len,
(len == 1) ? "" : "s",
nargs + nkeywords);
- return 0;
+ return cleanreturn(0, &freelist);
}
/* convert tuple args and keyword args in same loop, using kwlist to drive process */
@@ -1549,7 +1571,7 @@
PyErr_Format(PyExc_RuntimeError,
"More keyword list entries (%d) than "
"format specifiers (%d)", len, i);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
current_arg = NULL;
if (nkeywords) {
@@ -1563,11 +1585,11 @@
"Argument given by name ('%s') "
"and position (%d)",
keyword, i+1);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
}
else if (nkeywords && PyErr_Occurred())
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
else if (i < nargs)
current_arg = PyTuple_GET_ITEM(args, i);
@@ -1576,7 +1598,7 @@
levels, msgbuf, sizeof(msgbuf), &freelist);
if (msg) {
seterror(i+1, msg, levels, fname, custom_msg);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
continue;
}
@@ -1585,14 +1607,14 @@
PyErr_Format(PyExc_TypeError, "Required argument "
"'%s' (pos %d) not found",
keyword, i+1);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
/* current code reports success when all required args
* fulfilled and no keyword args left, with no further
* validation. XXX Maybe skip this in debug build ?
*/
if (!nkeywords)
- return cleanreturn(1, freelist);
+ return cleanreturn(1, &freelist);
/* We are into optional args, skip thru to any remaining
* keyword args */
@@ -1600,7 +1622,7 @@
if (msg) {
PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
format);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
}
@@ -1608,7 +1630,7 @@
PyErr_Format(PyExc_RuntimeError,
"more argument specifiers than keyword list entries "
"(remaining format:'%s')", format);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
/* make sure there are no extraneous keyword arguments */
@@ -1621,7 +1643,7 @@
if (!PyString_Check(key)) {
PyErr_SetString(PyExc_TypeError,
"keywords must be strings");
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
ks = PyString_AsString(key);
for (i = 0; i < len; i++) {
@@ -1635,12 +1657,12 @@
"'%s' is an invalid keyword "
"argument for this function",
ks);
- return cleanreturn(0, freelist);
+ return cleanreturn(0, &freelist);
}
}
}
- return cleanreturn(1, freelist);
+ return cleanreturn(1, &freelist);
}
diff --git a/pypy/module/cpyext/src/thread.c b/pypy/module/cpyext/src/thread.c
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/src/thread.c
@@ -0,0 +1,313 @@
+#include <Python.h>
+#include "pythread.h"
+
+/* ------------------------------------------------------------------------
+Per-thread data ("key") support.
+
+Use PyThread_create_key() to create a new key. This is typically shared
+across threads.
+
+Use PyThread_set_key_value(thekey, value) to associate void* value with
+thekey in the current thread. Each thread has a distinct mapping of thekey
+to a void* value. Caution: if the current thread already has a mapping
+for thekey, value is ignored.
+
+Use PyThread_get_key_value(thekey) to retrieve the void* value associated
+with thekey in the current thread. This returns NULL if no value is
+associated with thekey in the current thread.
+
+Use PyThread_delete_key_value(thekey) to forget the current thread's associated
+value for thekey. PyThread_delete_key(thekey) forgets the values associated
+with thekey across *all* threads.
+
+While some of these functions have error-return values, none set any
+Python exception.
+
+None of the functions does memory management on behalf of the void* values.
+You need to allocate and deallocate them yourself. If the void* values
+happen to be PyObject*, these functions don't do refcount operations on
+them either.
+
+The GIL does not need to be held when calling these functions; they supply
+their own locking. This isn't true of PyThread_create_key(), though (see
+next paragraph).
+
+There's a hidden assumption that PyThread_create_key() will be called before
+any of the other functions are called. There's also a hidden assumption
+that calls to PyThread_create_key() are serialized externally.
+------------------------------------------------------------------------ */
+
+#ifdef MS_WINDOWS
+#include <windows.h>
+
+/* use native Windows TLS functions */
+#define Py_HAVE_NATIVE_TLS
+
+int
+PyThread_create_key(void)
+{
+ return (int) TlsAlloc();
+}
+
+void
+PyThread_delete_key(int key)
+{
+ TlsFree(key);
+}
+
+/* We must be careful to emulate the strange semantics implemented in thread.c,
+ * where the value is only set if it hasn't been set before.
+ */
+int
+PyThread_set_key_value(int key, void *value)
+{
+ BOOL ok;
+ void *oldvalue;
+
+ assert(value != NULL);
+ oldvalue = TlsGetValue(key);
+ if (oldvalue != NULL)
+ /* ignore value if already set */
+ return 0;
+ ok = TlsSetValue(key, value);
+ if (!ok)
+ return -1;
+ return 0;
+}
+
+void *
+PyThread_get_key_value(int key)
+{
+ /* because TLS is used in the Py_END_ALLOW_THREAD macro,
+ * it is necessary to preserve the windows error state, because
+ * it is assumed to be preserved across the call to the macro.
+ * Ideally, the macro should be fixed, but it is simpler to
+ * do it here.
+ */
+ DWORD error = GetLastError();
+ void *result = TlsGetValue(key);
+ SetLastError(error);
+ return result;
+}
+
+void
+PyThread_delete_key_value(int key)
+{
+ /* NULL is used as "key missing", and it is also the default
+ * given by TlsGetValue() if nothing has been set yet.
+ */
+ TlsSetValue(key, NULL);
+}
+
+/* reinitialization of TLS is not necessary after fork when using
+ * the native TLS functions. And forking isn't supported on Windows either.
+ */
+void
+PyThread_ReInitTLS(void)
+{}
+
+#else /* MS_WINDOWS */
+
+/* A singly-linked list of struct key objects remembers all the key->value
+ * associations. File static keyhead heads the list. keymutex is used
+ * to enforce exclusion internally.
+ */
+struct key {
+ /* Next record in the list, or NULL if this is the last record. */
+ struct key *next;
+
+ /* The thread id, according to PyThread_get_thread_ident(). */
+ long id;
+
+ /* The key and its associated value. */
+ int key;
+ void *value;
+};
+
+static struct key *keyhead = NULL;
+static PyThread_type_lock keymutex = NULL;
+static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
+
+/* Internal helper.
+ * If the current thread has a mapping for key, the appropriate struct key*
+ * is returned. NB: value is ignored in this case!
+ * If there is no mapping for key in the current thread, then:
+ * If value is NULL, NULL is returned.
+ * Else a mapping of key to value is created for the current thread,
+ * and a pointer to a new struct key* is returned; except that if
+ * malloc() can't find room for a new struct key*, NULL is returned.
+ * So when value==NULL, this acts like a pure lookup routine, and when
+ * value!=NULL, this acts like dict.setdefault(), returning an existing
+ * mapping if one exists, else creating a new mapping.
+ *
+ * Caution: this used to be too clever, trying to hold keymutex only
+ * around the "p->next = keyhead; keyhead = p" pair. That allowed
+ * another thread to mutate the list, via key deletion, concurrent with
+ * find_key() crawling over the list. Hilarity ensued. For example, when
+ * the for-loop here does "p = p->next", p could end up pointing at a
+ * record that PyThread_delete_key_value() was concurrently free()'ing.
+ * That could lead to anything, from failing to find a key that exists, to
+ * segfaults. Now we lock the whole routine.
+ */
+static struct key *
+find_key(int key, void *value)
+{
+ struct key *p, *prev_p;
+ long id = PyThread_get_thread_ident();
+
+ if (!keymutex)
+ return NULL;
+ PyThread_acquire_lock(keymutex, 1);
+ prev_p = NULL;
+ for (p = keyhead; p != NULL; p = p->next) {
+ if (p->id == id && p->key == key)
+ goto Done;
+ /* Sanity check. These states should never happen but if
+ * they do we must abort. Otherwise we'll end up spinning in
+ * in a tight loop with the lock held. A similar check is done
+ * in pystate.c tstate_delete_common(). */
+ if (p == prev_p)
+ Py_FatalError("tls find_key: small circular list(!)");
+ prev_p = p;
+ if (p->next == keyhead)
+ Py_FatalError("tls find_key: circular list(!)");
+ }
+ if (value == NULL) {
+ assert(p == NULL);
+ goto Done;
+ }
+ p = (struct key *)malloc(sizeof(struct key));
+ if (p != NULL) {
+ p->id = id;
+ p->key = key;
+ p->value = value;
+ p->next = keyhead;
+ keyhead = p;
+ }
+ Done:
+ PyThread_release_lock(keymutex);
+ return p;
+}
+
+/* Return a new key. This must be called before any other functions in
+ * this family, and callers must arrange to serialize calls to this
+ * function. No violations are detected.
+ */
+int
+PyThread_create_key(void)
+{
+ /* All parts of this function are wrong if it's called by multiple
+ * threads simultaneously.
+ */
+ if (keymutex == NULL)
+ keymutex = PyThread_allocate_lock();
+ return ++nkeys;
+}
+
+/* Forget the associations for key across *all* threads. */
+void
+PyThread_delete_key(int key)
+{
+ struct key *p, **q;
+
+ PyThread_acquire_lock(keymutex, 1);
+ q = &keyhead;
+ while ((p = *q) != NULL) {
+ if (p->key == key) {
+ *q = p->next;
+ free((void *)p);
+ /* NB This does *not* free p->value! */
+ }
+ else
+ q = &p->next;
+ }
+ PyThread_release_lock(keymutex);
+}
+
+/* Confusing: If the current thread has an association for key,
+ * value is ignored, and 0 is returned. Else an attempt is made to create
+ * an association of key to value for the current thread. 0 is returned
+ * if that succeeds, but -1 is returned if there's not enough memory
+ * to create the association. value must not be NULL.
+ */
+int
+PyThread_set_key_value(int key, void *value)
+{
+ struct key *p;
+
+ assert(value != NULL);
+ p = find_key(key, value);
+ if (p == NULL)
+ return -1;
+ else
+ return 0;
+}
+
+/* Retrieve the value associated with key in the current thread, or NULL
+ * if the current thread doesn't have an association for key.
+ */
+void *
+PyThread_get_key_value(int key)
+{
+ struct key *p = find_key(key, NULL);
+
+ if (p == NULL)
+ return NULL;
+ else
+ return p->value;
+}
+
+/* Forget the current thread's association for key, if any. */
+void
+PyThread_delete_key_value(int key)
+{
+ long id = PyThread_get_thread_ident();
+ struct key *p, **q;
+
+ PyThread_acquire_lock(keymutex, 1);
+ q = &keyhead;
+ while ((p = *q) != NULL) {
+ if (p->key == key && p->id == id) {
+ *q = p->next;
+ free((void *)p);
+ /* NB This does *not* free p->value! */
+ break;
+ }
+ else
+ q = &p->next;
+ }
+ PyThread_release_lock(keymutex);
+}
+
+/* Forget everything not associated with the current thread id.
+ * This function is called from PyOS_AfterFork(). It is necessary
+ * because other thread ids which were in use at the time of the fork
+ * may be reused for new threads created in the forked process.
+ */
+void
+PyThread_ReInitTLS(void)
+{
+ long id = PyThread_get_thread_ident();
+ struct key *p, **q;
+
+ if (!keymutex)
+ return;
+
+ /* As with interpreter_lock in PyEval_ReInitThreads()
+ we just create a new lock without freeing the old one */
+ keymutex = PyThread_allocate_lock();
+
+ /* Delete all keys which do not match the current thread id */
+ q = &keyhead;
+ while ((p = *q) != NULL) {
+ if (p->id != id) {
+ *q = p->next;
+ free((void *)p);
+ /* NB This does *not* free p->value! */
+ }
+ else
+ q = &p->next;
+ }
+}
+
+#endif /* !MS_WINDOWS */
diff --git a/pypy/module/cpyext/stringobject.py b/pypy/module/cpyext/stringobject.py
--- a/pypy/module/cpyext/stringobject.py
+++ b/pypy/module/cpyext/stringobject.py
@@ -130,6 +130,11 @@
@cpython_api([PyObject], rffi.CCHARP, error=0)
def PyString_AsString(space, ref):
+ if from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is space.w_str:
+ pass # typecheck returned "ok" without forcing 'ref' at all
+ elif not PyString_Check(space, ref): # otherwise, use the alternate way
+ raise OperationError(space.w_TypeError, space.wrap(
+ "PyString_AsString only support strings"))
ref_str = rffi.cast(PyStringObject, ref)
if not ref_str.c_buffer:
# copy string buffer
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -182,16 +182,6 @@
used as the positional and keyword parameters to the object's constructor."""
raise NotImplementedError
- at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
-def PyCode_Check(space, co):
- """Return true if co is a code object"""
- raise NotImplementedError
-
- at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
-def PyCode_GetNumFree(space, co):
- """Return the number of free variables in co."""
- raise NotImplementedError
-
@cpython_api([PyObject], rffi.INT_real, error=-1)
def PyCodec_Register(space, search_function):
"""Register a new codec search function.
@@ -1293,28 +1283,6 @@
that haven't been explicitly destroyed at that point."""
raise NotImplementedError
- at cpython_api([rffi.VOIDP], lltype.Void)
-def Py_AddPendingCall(space, func):
- """Post a notification to the Python main thread. If successful, func will
- be called with the argument arg at the earliest convenience. func will be
- called having the global interpreter lock held and can thus use the full
- Python API and can take any action such as setting object attributes to
- signal IO completion. It must return 0 on success, or -1 signalling an
- exception. The notification function won't be interrupted to perform another
- asynchronous notification recursively, but it can still be interrupted to
- switch threads if the global interpreter lock is released, for example, if it
- calls back into Python code.
-
- This function returns 0 on success in which case the notification has been
- scheduled. Otherwise, for example if the notification buffer is full, it
- returns -1 without setting any exception.
-
- This function can be called on any thread, be it a Python thread or some
- other system thread. If it is a Python thread, it doesn't matter if it holds
- the global interpreter lock or not.
- """
- raise NotImplementedError
-
@cpython_api([Py_tracefunc, PyObject], lltype.Void)
def PyEval_SetProfile(space, func, obj):
"""Set the profiler function to func. The obj parameter is passed to the
@@ -1875,26 +1843,6 @@
"""
raise NotImplementedError
- at cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
-def Py_UNICODE_ISTITLE(space, ch):
- """Return 1 or 0 depending on whether ch is a titlecase character."""
- raise NotImplementedError
-
- at cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
-def Py_UNICODE_ISDIGIT(space, ch):
- """Return 1 or 0 depending on whether ch is a digit character."""
- raise NotImplementedError
-
- at cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
-def Py_UNICODE_ISNUMERIC(space, ch):
- """Return 1 or 0 depending on whether ch is a numeric character."""
- raise NotImplementedError
-
- at cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
-def Py_UNICODE_ISALPHA(space, ch):
- """Return 1 or 0 depending on whether ch is an alphabetic character."""
- raise NotImplementedError
-
@cpython_api([rffi.CCHARP], PyObject)
def PyUnicode_FromFormat(space, format):
"""Take a C printf()-style format string and a variable number of
@@ -2339,17 +2287,6 @@
use the default error handling."""
raise NotImplementedError
- at cpython_api([PyObject, PyObject, Py_ssize_t, Py_ssize_t, rffi.INT_real], rffi.INT_real, error=-1)
-def PyUnicode_Tailmatch(space, str, substr, start, end, direction):
- """Return 1 if substr matches str*[*start:end] at the given tail end
- (direction == -1 means to do a prefix match, direction == 1 a suffix match),
- 0 otherwise. Return -1 if an error occurred.
-
- This function used an int type for start and end. This
- might require changes in your code for properly supporting 64-bit
- systems."""
- raise NotImplementedError
-
@cpython_api([PyObject, PyObject, Py_ssize_t, Py_ssize_t, rffi.INT_real], Py_ssize_t, error=-2)
def PyUnicode_Find(space, str, substr, start, end, direction):
"""Return the first position of substr in str*[*start:end] using the given
@@ -2373,16 +2310,6 @@
properly supporting 64-bit systems."""
raise NotImplementedError
- at cpython_api([PyObject, PyObject, PyObject, Py_ssize_t], PyObject)
-def PyUnicode_Replace(space, str, substr, replstr, maxcount):
- """Replace at most maxcount occurrences of substr in str with replstr and
- return the resulting Unicode object. maxcount == -1 means replace all
- occurrences.
-
- This function used an int type for maxcount. This might
- require changes in your code for properly supporting 64-bit systems."""
- raise NotImplementedError
-
@cpython_api([PyObject, PyObject, rffi.INT_real], PyObject)
def PyUnicode_RichCompare(space, left, right, op):
"""Rich compare two unicode strings and return one of the following:
@@ -2556,17 +2483,6 @@
source code is read from fp instead of an in-memory string."""
raise NotImplementedError
- at cpython_api([rffi.CCHARP, rffi.INT_real, PyObject, PyObject, PyCompilerFlags], PyObject)
-def PyRun_StringFlags(space, str, start, globals, locals, flags):
- """Execute Python source code from str in the context specified by the
- dictionaries globals and locals with the compiler flags specified by
- flags. The parameter start specifies the start token that should be used to
- parse the source code.
-
- Returns the result of executing the code as a Python object, or NULL if an
- exception was raised."""
- raise NotImplementedError
-
@cpython_api([FILE, rffi.CCHARP, rffi.INT_real, PyObject, PyObject, rffi.INT_real], PyObject)
def PyRun_FileEx(space, fp, filename, start, globals, locals, closeit):
"""This is a simplified interface to PyRun_FileExFlags() below, leaving
@@ -2587,13 +2503,6 @@
returns."""
raise NotImplementedError
- at cpython_api([PyCodeObject, PyObject, PyObject], PyObject)
-def PyEval_EvalCode(space, co, globals, locals):
- """This is a simplified interface to PyEval_EvalCodeEx(), with just
- the code object, and the dictionaries of global and local variables.
- The other arguments are set to NULL."""
- raise NotImplementedError
-
@cpython_api([PyCodeObject, PyObject, PyObject, PyObjectP, rffi.INT_real, PyObjectP, rffi.INT_real, PyObjectP, rffi.INT_real, PyObject], PyObject)
def PyEval_EvalCodeEx(space, co, globals, locals, args, argcount, kws, kwcount, defs, defcount, closure):
"""Evaluate a precompiled code object, given a particular environment for its
@@ -2618,12 +2527,6 @@
throw() methods of generator objects."""
raise NotImplementedError
- at cpython_api([PyCompilerFlags], rffi.INT_real, error=CANNOT_FAIL)
-def PyEval_MergeCompilerFlags(space, cf):
- """This function changes the flags of the current evaluation frame, and returns
- true on success, false on failure."""
- raise NotImplementedError
-
@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
def PyWeakref_Check(space, ob):
"""Return true if ob is either a reference or proxy object.
diff --git a/pypy/module/cpyext/stubsactive.py b/pypy/module/cpyext/stubsactive.py
--- a/pypy/module/cpyext/stubsactive.py
+++ b/pypy/module/cpyext/stubsactive.py
@@ -38,3 +38,31 @@
def Py_MakePendingCalls(space):
return 0
+pending_call = lltype.Ptr(lltype.FuncType([rffi.VOIDP], rffi.INT_real))
+ at cpython_api([pending_call, rffi.VOIDP], rffi.INT_real, error=-1)
+def Py_AddPendingCall(space, func, arg):
+ """Post a notification to the Python main thread. If successful,
+ func will be called with the argument arg at the earliest
+ convenience. func will be called having the global interpreter
+ lock held and can thus use the full Python API and can take any
+ action such as setting object attributes to signal IO completion.
+ It must return 0 on success, or -1 signalling an exception. The
+ notification function won't be interrupted to perform another
+ asynchronous notification recursively, but it can still be
+ interrupted to switch threads if the global interpreter lock is
+ released, for example, if it calls back into Python code.
+
+ This function returns 0 on success in which case the notification
+ has been scheduled. Otherwise, for example if the notification
+ buffer is full, it returns -1 without setting any exception.
+
+ This function can be called on any thread, be it a Python thread
+ or some other system thread. If it is a Python thread, it doesn't
+ matter if it holds the global interpreter lock or not.
+ """
+ return -1
+
+thread_func = lltype.Ptr(lltype.FuncType([rffi.VOIDP], lltype.Void))
+ at cpython_api([thread_func, rffi.VOIDP], rffi.INT_real, error=-1)
+def PyThread_start_new_thread(space, func, arg):
+ return -1
diff --git a/pypy/module/cpyext/test/test_cpyext.py b/pypy/module/cpyext/test/test_cpyext.py
--- a/pypy/module/cpyext/test/test_cpyext.py
+++ b/pypy/module/cpyext/test/test_cpyext.py
@@ -106,10 +106,7 @@
del obj
import gc; gc.collect()
- try:
- del space.getexecutioncontext().cpyext_threadstate
- except AttributeError:
- pass
+ space.getexecutioncontext().cleanup_cpyext_state()
for w_obj in state.non_heaptypes_w:
Py_DecRef(space, w_obj)
diff --git a/pypy/module/cpyext/test/test_eval.py b/pypy/module/cpyext/test/test_eval.py
--- a/pypy/module/cpyext/test/test_eval.py
+++ b/pypy/module/cpyext/test/test_eval.py
@@ -2,9 +2,10 @@
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.test.test_api import BaseApiTest
from pypy.module.cpyext.eval import (
- Py_single_input, Py_file_input, Py_eval_input)
+ Py_single_input, Py_file_input, Py_eval_input, PyCompilerFlags)
from pypy.module.cpyext.api import fopen, fclose, fileno, Py_ssize_tP
from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.astcompiler import consts
from pypy.tool.udir import udir
import sys, os
@@ -63,6 +64,22 @@
assert space.int_w(w_res) == 10
+ def test_evalcode(self, space, api):
+ w_f = space.appexec([], """():
+ def f(*args):
+ assert isinstance(args, tuple)
+ return len(args) + 8
+ return f
+ """)
+
+ w_t = space.newtuple([space.wrap(1), space.wrap(2)])
+ w_globals = space.newdict()
+ w_locals = space.newdict()
+ space.setitem(w_locals, space.wrap("args"), w_t)
+ w_res = api.PyEval_EvalCode(w_f.code, w_globals, w_locals)
+
+ assert space.int_w(w_res) == 10
+
def test_run_simple_string(self, space, api):
def run(code):
buf = rffi.str2charp(code)
@@ -96,6 +113,20 @@
assert 42 * 43 == space.unwrap(
api.PyObject_GetItem(w_globals, space.wrap("a")))
+ def test_run_string_flags(self, space, api):
+ flags = lltype.malloc(PyCompilerFlags, flavor='raw')
+ flags.c_cf_flags = rffi.cast(rffi.INT, consts.PyCF_SOURCE_IS_UTF8)
+ w_globals = space.newdict()
+ buf = rffi.str2charp("a = u'caf\xc3\xa9'")
+ try:
+ api.PyRun_StringFlags(buf, Py_single_input,
+ w_globals, w_globals, flags)
+ finally:
+ rffi.free_charp(buf)
+ w_a = space.getitem(w_globals, space.wrap("a"))
+ assert space.unwrap(w_a) == u'caf\xe9'
+ lltype.free(flags, flavor='raw')
+
def test_run_file(self, space, api):
filepath = udir / "cpyext_test_runfile.py"
filepath.write("raise ZeroDivisionError")
@@ -256,3 +287,21 @@
print dir(mod)
print mod.__dict__
assert mod.f(42) == 47
+
+ def test_merge_compiler_flags(self):
+ module = self.import_extension('foo', [
+ ("get_flags", "METH_NOARGS",
+ """
+ PyCompilerFlags flags;
+ flags.cf_flags = 0;
+ int result = PyEval_MergeCompilerFlags(&flags);
+ return Py_BuildValue("ii", result, flags.cf_flags);
+ """),
+ ])
+ assert module.get_flags() == (0, 0)
+
+ ns = {'module':module}
+ exec """from __future__ import division \nif 1:
+ def nested_flags():
+ return module.get_flags()""" in ns
+ assert ns['nested_flags']() == (1, 0x2000) # CO_FUTURE_DIVISION
diff --git a/pypy/module/cpyext/test/test_funcobject.py b/pypy/module/cpyext/test/test_funcobject.py
--- a/pypy/module/cpyext/test/test_funcobject.py
+++ b/pypy/module/cpyext/test/test_funcobject.py
@@ -81,6 +81,14 @@
rffi.free_charp(filename)
rffi.free_charp(funcname)
+ def test_getnumfree(self, space, api):
+ w_function = space.appexec([], """():
+ a = 5
+ def method(x): return a, x
+ return method
+ """)
+ assert api.PyCode_GetNumFree(w_function.code) == 1
+
def test_classmethod(self, space, api):
w_function = space.appexec([], """():
def method(x): return x
diff --git a/pypy/module/cpyext/test/test_intobject.py b/pypy/module/cpyext/test/test_intobject.py
--- a/pypy/module/cpyext/test/test_intobject.py
+++ b/pypy/module/cpyext/test/test_intobject.py
@@ -65,4 +65,97 @@
values = module.values()
types = [type(x) for x in values]
assert types == [int, long, int, int]
-
+
+ def test_int_subtype(self):
+ module = self.import_extension(
+ 'foo', [
+ ("newEnum", "METH_VARARGS",
+ """
+ EnumObject *enumObj;
+ long intval;
+ PyObject *name;
+
+ if (!PyArg_ParseTuple(args, "Oi", &name, &intval))
+ return NULL;
+
+ PyType_Ready(&Enum_Type);
+ enumObj = PyObject_New(EnumObject, &Enum_Type);
+ if (!enumObj) {
+ return NULL;
+ }
+
+ enumObj->ob_ival = intval;
+ Py_INCREF(name);
+ enumObj->ob_name = name;
+
+ return (PyObject *)enumObj;
+ """),
+ ],
+ prologue="""
+ typedef struct
+ {
+ PyObject_HEAD
+ long ob_ival;
+ PyObject* ob_name;
+ } EnumObject;
+
+ static void
+ enum_dealloc(EnumObject *op)
+ {
+ Py_DECREF(op->ob_name);
+ Py_TYPE(op)->tp_free((PyObject *)op);
+ }
+
+ static PyMemberDef enum_members[] = {
+ {"name", T_OBJECT, offsetof(EnumObject, ob_name), 0, NULL},
+ {NULL} /* Sentinel */
+ };
+
+ PyTypeObject Enum_Type = {
+ PyObject_HEAD_INIT(0)
+ /*ob_size*/ 0,
+ /*tp_name*/ "Enum",
+ /*tp_basicsize*/ sizeof(EnumObject),
+ /*tp_itemsize*/ 0,
+ /*tp_dealloc*/ enum_dealloc,
+ /*tp_print*/ 0,
+ /*tp_getattr*/ 0,
+ /*tp_setattr*/ 0,
+ /*tp_compare*/ 0,
+ /*tp_repr*/ 0,
+ /*tp_as_number*/ 0,
+ /*tp_as_sequence*/ 0,
+ /*tp_as_mapping*/ 0,
+ /*tp_hash*/ 0,
+ /*tp_call*/ 0,
+ /*tp_str*/ 0,
+ /*tp_getattro*/ 0,
+ /*tp_setattro*/ 0,
+ /*tp_as_buffer*/ 0,
+ /*tp_flags*/ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ /*tp_doc*/ 0,
+ /*tp_traverse*/ 0,
+ /*tp_clear*/ 0,
+ /*tp_richcompare*/ 0,
+ /*tp_weaklistoffset*/ 0,
+ /*tp_iter*/ 0,
+ /*tp_iternext*/ 0,
+ /*tp_methods*/ 0,
+ /*tp_members*/ enum_members,
+ /*tp_getset*/ 0,
+ /*tp_base*/ &PyInt_Type,
+ /*tp_dict*/ 0,
+ /*tp_descr_get*/ 0,
+ /*tp_descr_set*/ 0,
+ /*tp_dictoffset*/ 0,
+ /*tp_init*/ 0,
+ /*tp_alloc*/ 0,
+ /*tp_new*/ 0
+ };
+ """)
+
+ a = module.newEnum("ULTIMATE_ANSWER", 42)
+ assert type(a).__name__ == "Enum"
+ assert isinstance(a, int)
+ assert a == int(a) == 42
+ assert a.name == "ULTIMATE_ANSWER"
diff --git a/pypy/module/cpyext/test/test_longobject.py b/pypy/module/cpyext/test/test_longobject.py
--- a/pypy/module/cpyext/test/test_longobject.py
+++ b/pypy/module/cpyext/test/test_longobject.py
@@ -101,9 +101,9 @@
space.wrap((2, 7)))):
py.test.skip("unsupported before Python 2.7")
- assert api._PyLong_Sign(space.wrap(0L)) == 0
- assert api._PyLong_Sign(space.wrap(2L)) == 1
- assert api._PyLong_Sign(space.wrap(-2L)) == -1
+ assert api._PyLong_Sign(space.wraplong(0L)) == 0
+ assert api._PyLong_Sign(space.wraplong(2L)) == 1
+ assert api._PyLong_Sign(space.wraplong(-2L)) == -1
assert api._PyLong_NumBits(space.wrap(0)) == 0
assert api._PyLong_NumBits(space.wrap(1)) == 1
diff --git a/pypy/module/cpyext/test/test_number.py b/pypy/module/cpyext/test/test_number.py
--- a/pypy/module/cpyext/test/test_number.py
+++ b/pypy/module/cpyext/test/test_number.py
@@ -6,12 +6,12 @@
class TestIterator(BaseApiTest):
def test_check(self, space, api):
assert api.PyIndex_Check(space.wrap(12))
- assert api.PyIndex_Check(space.wrap(-12L))
+ assert api.PyIndex_Check(space.wraplong(-12L))
assert not api.PyIndex_Check(space.wrap(12.1))
assert not api.PyIndex_Check(space.wrap('12'))
assert api.PyNumber_Check(space.wrap(12))
- assert api.PyNumber_Check(space.wrap(-12L))
+ assert api.PyNumber_Check(space.wraplong(-12L))
assert api.PyNumber_Check(space.wrap(12.1))
assert not api.PyNumber_Check(space.wrap('12'))
assert not api.PyNumber_Check(space.wrap(1+3j))
@@ -21,7 +21,7 @@
assert api.PyLong_CheckExact(w_l)
def test_number_int(self, space, api):
- w_l = api.PyNumber_Int(space.wrap(123L))
+ w_l = api.PyNumber_Int(space.wraplong(123L))
assert api.PyInt_CheckExact(w_l)
w_l = api.PyNumber_Int(space.wrap(2 << 65))
assert api.PyLong_CheckExact(w_l)
@@ -29,7 +29,7 @@
assert api.PyInt_CheckExact(w_l)
def test_number_index(self, space, api):
- w_l = api.PyNumber_Index(space.wrap(123L))
+ w_l = api.PyNumber_Index(space.wraplong(123L))
assert api.PyLong_CheckExact(w_l)
w_l = api.PyNumber_Index(space.wrap(42.3))
assert w_l is None
diff --git a/pypy/module/cpyext/test/test_pystate.py b/pypy/module/cpyext/test/test_pystate.py
--- a/pypy/module/cpyext/test/test_pystate.py
+++ b/pypy/module/cpyext/test/test_pystate.py
@@ -3,6 +3,10 @@
from pypy.rpython.lltypesystem.lltype import nullptr
from pypy.module.cpyext.pystate import PyInterpreterState, PyThreadState
from pypy.module.cpyext.pyobject import from_ref
+from pypy.rpython.lltypesystem import lltype
+from pypy.module.cpyext.test.test_cpyext import LeakCheckingTest, freeze_refcnts
+from pypy.module.cpyext.pystate import PyThreadState_Get, PyInterpreterState_Head
+from pypy.tool import leakfinder
class AppTestThreads(AppTestCpythonExtensionBase):
def test_allow_threads(self):
@@ -21,6 +25,93 @@
# Should compile at least
module.test()
+
+ def test_thread_state_get(self):
+ module = self.import_extension('foo', [
+ ("get", "METH_NOARGS",
+ """
+ PyThreadState *tstate = PyThreadState_Get();
+ if (tstate == NULL) {
+ return PyLong_FromLong(0);
+ }
+ if (tstate->interp != PyInterpreterState_Head()) {
+ return PyLong_FromLong(1);
+ }
+ if (tstate->interp->next != NULL) {
+ return PyLong_FromLong(2);
+ }
+ return PyLong_FromLong(3);
+ """),
+ ])
+ assert module.get() == 3
+
+ def test_basic_threadstate_dance(self):
+ module = self.import_extension('foo', [
+ ("dance", "METH_NOARGS",
+ """
+ PyThreadState *old_tstate, *new_tstate;
+
+ old_tstate = PyThreadState_Swap(NULL);
+ if (old_tstate == NULL) {
+ return PyLong_FromLong(0);
+ }
+
+ new_tstate = PyThreadState_Get();
+ if (new_tstate != NULL) {
+ return PyLong_FromLong(1);
+ }
+
+ new_tstate = PyThreadState_Swap(old_tstate);
+ if (new_tstate != NULL) {
+ return PyLong_FromLong(2);
+ }
+
+ new_tstate = PyThreadState_Get();
+ if (new_tstate != old_tstate) {
+ return PyLong_FromLong(3);
+ }
+
+ return PyLong_FromLong(4);
+ """),
+ ])
+ assert module.dance() == 4
+
+ def test_threadstate_dict(self):
+ module = self.import_extension('foo', [
+ ("getdict", "METH_NOARGS",
+ """
+ PyObject *dict = PyThreadState_GetDict();
+ Py_INCREF(dict);
+ return dict;
+ """),
+ ])
+ assert isinstance(module.getdict(), dict)
+
+ def test_savethread(self):
+ module = self.import_extension('foo', [
+ ("bounce", "METH_NOARGS",
+ """
+ PyThreadState *tstate = PyEval_SaveThread();
+ if (tstate == NULL) {
+ return PyLong_FromLong(0);
+ }
+
+ if (PyThreadState_Get() != NULL) {
+ return PyLong_FromLong(1);
+ }
+
+ PyEval_RestoreThread(tstate);
+
+ if (PyThreadState_Get() != tstate) {
+ return PyLong_FromLong(2);
+ }
+
+ return PyLong_FromLong(3);
+ """),
+ ])
+
+
+
class TestInterpreterState(BaseApiTest):
def test_interpreter_head(self, space, api):
state = api.PyInterpreterState_Head()
@@ -29,31 +120,3 @@
def test_interpreter_next(self, space, api):
state = api.PyInterpreterState_Head()
assert nullptr(PyInterpreterState.TO) == api.PyInterpreterState_Next(state)
-
-class TestThreadState(BaseApiTest):
- def test_thread_state_get(self, space, api):
- ts = api.PyThreadState_Get()
- assert ts != nullptr(PyThreadState.TO)
-
- def test_thread_state_interp(self, space, api):
- ts = api.PyThreadState_Get()
- assert ts.c_interp == api.PyInterpreterState_Head()
- assert ts.c_interp.c_next == nullptr(PyInterpreterState.TO)
-
- def test_basic_threadstate_dance(self, space, api):
- # Let extension modules call these functions,
- # Not sure of the semantics in pypy though.
- # (cpyext always acquires and releases the GIL around calls)
- tstate = api.PyThreadState_Swap(None)
- assert tstate is not None
- assert not api.PyThreadState_Swap(tstate)
-
- api.PyEval_AcquireThread(tstate)
- api.PyEval_ReleaseThread(tstate)
-
- def test_threadstate_dict(self, space, api):
- ts = api.PyThreadState_Get()
- ref = ts.c_dict
- assert ref == api.PyThreadState_GetDict()
- w_obj = from_ref(space, ref)
- assert space.isinstance_w(w_obj, space.w_dict)
diff --git a/pypy/module/cpyext/test/test_stringobject.py b/pypy/module/cpyext/test/test_stringobject.py
--- a/pypy/module/cpyext/test/test_stringobject.py
+++ b/pypy/module/cpyext/test/test_stringobject.py
@@ -105,6 +105,15 @@
)])
assert module.string_as_string("huheduwe") == "huhe"
+ def test_py_string_as_string_None(self):
+ module = self.import_extension('foo', [
+ ("string_None", "METH_VARARGS",
+ '''
+ return PyString_AsString(Py_None);
+ '''
+ )])
+ raises(TypeError, module.string_None)
+
def test_AsStringAndSize(self):
module = self.import_extension('foo', [
("getstring", "METH_NOARGS",
diff --git a/pypy/module/cpyext/test/test_thread.py b/pypy/module/cpyext/test/test_thread.py
--- a/pypy/module/cpyext/test/test_thread.py
+++ b/pypy/module/cpyext/test/test_thread.py
@@ -5,6 +5,7 @@
from pypy.module.thread.ll_thread import allocate_ll_lock
from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
class TestPyThread(BaseApiTest):
@@ -38,3 +39,51 @@
api.PyThread_release_lock(lock)
assert api.PyThread_acquire_lock(lock, 0) == 1
api.PyThread_free_lock(lock)
+
+
+class AppTestThread(AppTestCpythonExtensionBase):
+ def test_tls(self):
+ module = self.import_extension('foo', [
+ ("create_key", "METH_NOARGS",
+ """
+ return PyInt_FromLong(PyThread_create_key());
+ """),
+ ("test_key", "METH_O",
+ """
+ int key = PyInt_AsLong(args);
+ if (PyThread_get_key_value(key) != NULL) {
+ PyErr_SetNone(PyExc_ValueError);
+ return NULL;
+ }
+ if (PyThread_set_key_value(key, (void*)123) < 0) {
+ PyErr_SetNone(PyExc_ValueError);
+ return NULL;
+ }
+ if (PyThread_get_key_value(key) != (void*)123) {
+ PyErr_SetNone(PyExc_ValueError);
+ return NULL;
+ }
+ Py_RETURN_NONE;
+ """),
+ ])
+ key = module.create_key()
+ assert key > 0
+ # Test value in main thread.
+ module.test_key(key)
+ raises(ValueError, module.test_key, key)
+ # Same test, in another thread.
+ result = []
+ import thread, time
+ def in_thread():
+ try:
+ module.test_key(key)
+ raises(ValueError, module.test_key, key)
+ except Exception, e:
+ result.append(e)
+ else:
+ result.append(True)
+ thread.start_new_thread(in_thread, ())
+ while not result:
+ print "."
+ time.sleep(.5)
+ assert result == [True]
diff --git a/pypy/module/cpyext/test/test_unicodeobject.py b/pypy/module/cpyext/test/test_unicodeobject.py
--- a/pypy/module/cpyext/test/test_unicodeobject.py
+++ b/pypy/module/cpyext/test/test_unicodeobject.py
@@ -204,8 +204,18 @@
assert api.Py_UNICODE_ISSPACE(unichr(char))
assert not api.Py_UNICODE_ISSPACE(u'a')
+ assert api.Py_UNICODE_ISALPHA(u'a')
+ assert not api.Py_UNICODE_ISALPHA(u'0')
+ assert api.Py_UNICODE_ISALNUM(u'a')
+ assert api.Py_UNICODE_ISALNUM(u'0')
+ assert not api.Py_UNICODE_ISALNUM(u'+')
+
assert api.Py_UNICODE_ISDECIMAL(u'\u0660')
assert not api.Py_UNICODE_ISDECIMAL(u'a')
+ assert api.Py_UNICODE_ISDIGIT(u'9')
+ assert not api.Py_UNICODE_ISDIGIT(u'@')
+ assert api.Py_UNICODE_ISNUMERIC(u'9')
+ assert not api.Py_UNICODE_ISNUMERIC(u'@')
for char in [0x0a, 0x0d, 0x1c, 0x1d, 0x1e, 0x85, 0x2028, 0x2029]:
assert api.Py_UNICODE_ISLINEBREAK(unichr(char))
@@ -216,6 +226,9 @@
assert not api.Py_UNICODE_ISUPPER(u'a')
assert not api.Py_UNICODE_ISLOWER(u'�')
assert api.Py_UNICODE_ISUPPER(u'�')
+ assert not api.Py_UNICODE_ISTITLE(u'A')
+ assert api.Py_UNICODE_ISTITLE(
+ u'\N{LATIN CAPITAL LETTER L WITH SMALL LETTER J}')
def test_TOLOWER(self, space, api):
assert api.Py_UNICODE_TOLOWER(u'�') == u'�'
@@ -429,3 +442,18 @@
w_char = api.PyUnicode_FromOrdinal(0xFFFF)
assert space.unwrap(w_char) == u'\uFFFF'
+ def test_replace(self, space, api):
+ w_str = space.wrap(u"abababab")
+ w_substr = space.wrap(u"a")
+ w_replstr = space.wrap(u"z")
+ assert u"zbzbabab" == space.unwrap(
+ api.PyUnicode_Replace(w_str, w_substr, w_replstr, 2))
+ assert u"zbzbzbzb" == space.unwrap(
+ api.PyUnicode_Replace(w_str, w_substr, w_replstr, -1))
+
+ def test_tailmatch(self, space, api):
+ w_str = space.wrap(u"abcdef")
+ assert api.PyUnicode_Tailmatch(w_str, space.wrap("cde"), 2, 10, 1) == 1
+ assert api.PyUnicode_Tailmatch(w_str, space.wrap("cde"), 1, 5, -1) == 1
+ self.raises(space, api, TypeError,
+ api.PyUnicode_Tailmatch, w_str, space.wrap(3), 2, 10, 1)
diff --git a/pypy/module/cpyext/unicodeobject.py b/pypy/module/cpyext/unicodeobject.py
--- a/pypy/module/cpyext/unicodeobject.py
+++ b/pypy/module/cpyext/unicodeobject.py
@@ -12,7 +12,7 @@
make_typedescr, get_typedescr)
from pypy.module.cpyext.stringobject import PyString_Check
from pypy.module.sys.interp_encoding import setdefaultencoding
-from pypy.objspace.std import unicodeobject, unicodetype
+from pypy.objspace.std import unicodeobject, unicodetype, stringtype
from pypy.rlib import runicode
from pypy.tool.sourcetools import func_renamer
import sys
@@ -89,6 +89,11 @@
return unicodedb.isspace(ord(ch))
@cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
+def Py_UNICODE_ISALPHA(space, ch):
+ """Return 1 or 0 depending on whether ch is an alphabetic character."""
+ return unicodedb.isalpha(ord(ch))
+
+ at cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
def Py_UNICODE_ISALNUM(space, ch):
"""Return 1 or 0 depending on whether ch is an alphanumeric character."""
return unicodedb.isalnum(ord(ch))
@@ -104,6 +109,16 @@
return unicodedb.isdecimal(ord(ch))
@cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
+def Py_UNICODE_ISDIGIT(space, ch):
+ """Return 1 or 0 depending on whether ch is a digit character."""
+ return unicodedb.isdigit(ord(ch))
+
+ at cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
+def Py_UNICODE_ISNUMERIC(space, ch):
+ """Return 1 or 0 depending on whether ch is a numeric character."""
+ return unicodedb.isnumeric(ord(ch))
+
+ at cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
def Py_UNICODE_ISLOWER(space, ch):
"""Return 1 or 0 depending on whether ch is a lowercase character."""
return unicodedb.islower(ord(ch))
@@ -113,6 +128,11 @@
"""Return 1 or 0 depending on whether ch is an uppercase character."""
return unicodedb.isupper(ord(ch))
+ at cpython_api([Py_UNICODE], rffi.INT_real, error=CANNOT_FAIL)
+def Py_UNICODE_ISTITLE(space, ch):
+ """Return 1 or 0 depending on whether ch is a titlecase character."""
+ return unicodedb.istitle(ord(ch))
+
@cpython_api([Py_UNICODE], Py_UNICODE, error=CANNOT_FAIL)
def Py_UNICODE_TOLOWER(space, ch):
"""Return the character ch converted to lower case."""
@@ -155,6 +175,11 @@
except KeyError:
return -1.0
+ at cpython_api([], Py_UNICODE, error=CANNOT_FAIL)
+def PyUnicode_GetMax(space):
+ """Get the maximum ordinal for a Unicode character."""
+ return runicode.UNICHR(runicode.MAXUNICODE)
+
@cpython_api([PyObject], rffi.CCHARP, error=CANNOT_FAIL)
def PyUnicode_AS_DATA(space, ref):
"""Return a pointer to the internal buffer of the object. o has to be a
@@ -548,6 +573,28 @@
@cpython_api([PyObject, PyObject], PyObject)
def PyUnicode_Join(space, w_sep, w_seq):
- """Join a sequence of strings using the given separator and return the resulting
- Unicode string."""
+ """Join a sequence of strings using the given separator and return
+ the resulting Unicode string."""
return space.call_method(w_sep, 'join', w_seq)
+
+ at cpython_api([PyObject, PyObject, PyObject, Py_ssize_t], PyObject)
+def PyUnicode_Replace(space, w_str, w_substr, w_replstr, maxcount):
+ """Replace at most maxcount occurrences of substr in str with replstr and
+ return the resulting Unicode object. maxcount == -1 means replace all
+ occurrences."""
+ return space.call_method(w_str, "replace", w_substr, w_replstr,
+ space.wrap(maxcount))
+
+ at cpython_api([PyObject, PyObject, Py_ssize_t, Py_ssize_t, rffi.INT_real],
+ rffi.INT_real, error=-1)
+def PyUnicode_Tailmatch(space, w_str, w_substr, start, end, direction):
+ """Return 1 if substr matches str[start:end] at the given tail end
+ (direction == -1 means to do a prefix match, direction == 1 a
+ suffix match), 0 otherwise. Return -1 if an error occurred."""
+ str = space.unicode_w(w_str)
+ substr = space.unicode_w(w_substr)
+ if rffi.cast(lltype.Signed, direction) >= 0:
+ return stringtype.stringstartswith(str, substr, start, end)
+ else:
+ return stringtype.stringendswith(str, substr, start, end)
+
diff --git a/pypy/module/imp/interp_imp.py b/pypy/module/imp/interp_imp.py
--- a/pypy/module/imp/interp_imp.py
+++ b/pypy/module/imp/interp_imp.py
@@ -1,10 +1,11 @@
from pypy.module.imp import importing
from pypy.module._file.interp_file import W_File
from pypy.rlib import streamio
+from pypy.rlib.streamio import StreamErrors
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.module import Module
from pypy.interpreter.gateway import unwrap_spec
-from pypy.module._file.interp_stream import StreamErrors, wrap_streamerror
+from pypy.interpreter.streamutil import wrap_streamerror
def get_suffixes(space):
diff --git a/pypy/module/imp/test/test_import.py b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -357,7 +357,7 @@
def test_cannot_write_pyc(self):
import sys, os
- p = os.path.join(sys.path[-1], 'readonly')
+ p = os.path.join(sys.path[0], 'readonly')
try:
os.chmod(p, 0555)
except:
diff --git a/pypy/module/marshal/interp_marshal.py b/pypy/module/marshal/interp_marshal.py
--- a/pypy/module/marshal/interp_marshal.py
+++ b/pypy/module/marshal/interp_marshal.py
@@ -327,8 +327,10 @@
# %r not supported in rpython
#u.raise_exc('invalid typecode in unmarshal: %r' % tc)
c = ord(tc)
- if c < 32 or c > 126:
- s = '\\x' + hex(c)
+ if c < 16:
+ s = '\\x0%x' % c
+ elif c < 32 or c > 126:
+ s = '\\x%x' % c
elif tc == '\\':
s = r'\\'
else:
diff --git a/pypy/module/marshal/test/make_test_marshal.py b/pypy/module/marshal/test/make_test_marshal.py
deleted file mode 100644
--- a/pypy/module/marshal/test/make_test_marshal.py
+++ /dev/null
@@ -1,78 +0,0 @@
-
-TESTCASES = """\
- None
- False
- True
- StopIteration
- Ellipsis
- 42
- -17
- sys.maxint
- -1.25
- -1.25 #2
- 2+5j
- 2+5j #2
- 42L
- -1234567890123456789012345678901234567890L
- hello # not interned
- "hello"
- ()
- (1, 2)
- []
- [3, 4]
- {}
- {5: 6, 7: 8}
- func.func_code
- scopefunc.func_code
- u'hello'
- set()
- set([1, 2])
- frozenset()
- frozenset([3, 4])
-""".strip().split('\n')
-
-def readable(s):
- for c, repl in (
- ("'", '_quote_'), ('"', '_Quote_'), (':', '_colon_'), ('.', '_dot_'),
- ('[', '_list_'), (']', '_tsil_'), ('{', '_dict_'), ('}', '_tcid_'),
- ('-', '_minus_'), ('+', '_plus_'),
- (',', '_comma_'), ('(', '_brace_'), (')', '_ecarb_') ):
- s = s.replace(c, repl)
- lis = list(s)
- for i, c in enumerate(lis):
- if c.isalnum() or c == '_':
- continue
- lis[i] = '_'
- return ''.join(lis)
-
-print """class AppTestMarshal:
-"""
-for line in TESTCASES:
- line = line.strip()
- name = readable(line)
- version = ''
- extra = ''
- if line.endswith('#2'):
- version = ', 2'
- extra = '; assert len(s) in (9, 17)'
- src = '''\
- def test_%(name)s(self):
- import sys
- hello = "he"
- hello += "llo"
- def func(x):
- return lambda y: x+y
- scopefunc = func(42)
- import marshal, StringIO
- case = %(line)s
- print "case: %%-30s func=%(name)s" %% (case, )
- s = marshal.dumps(case%(version)s)%(extra)s
- x = marshal.loads(s)
- assert x == case
- f = StringIO.StringIO()
- marshal.dump(case, f)
- f.seek(0)
- x = marshal.load(f)
- assert x == case
-''' % {'name': name, 'line': line, 'version' : version, 'extra': extra}
- print src
diff --git a/pypy/module/marshal/test/test_marshal.py b/pypy/module/marshal/test/test_marshal.py
--- a/pypy/module/marshal/test/test_marshal.py
+++ b/pypy/module/marshal/test/test_marshal.py
@@ -174,6 +174,11 @@
pass
raises(ValueError, marshal.dumps, subtype)
+ def test_bad_typecode(self):
+ import marshal
+ exc = raises(ValueError, marshal.loads, chr(1))
+ assert r"'\x01'" in exc.value.message
+
class AppTestRope(AppTestMarshal):
def setup_class(cls):
diff --git a/pypy/module/math/test/test_direct.py b/pypy/module/math/test/test_direct.py
--- a/pypy/module/math/test/test_direct.py
+++ b/pypy/module/math/test/test_direct.py
@@ -55,6 +55,15 @@
('frexp', (-1.25,), lambda x: x == (-0.625, 1)),
('modf', (4.25,), lambda x: x == (0.25, 4.0)),
('modf', (-4.25,), lambda x: x == (-0.25, -4.0)),
+ ('copysign', (1.5, 0.0), 1.5),
+ ('copysign', (1.5, -0.0), -1.5),
+ ('copysign', (1.5, INFINITY), 1.5),
+ ('copysign', (1.5, -INFINITY), -1.5),
+ ]
+ if sys.platform != 'win32': # all NaNs seem to be negative there...?
+ IRREGCASES += [
+ ('copysign', (1.5, NAN), 1.5),
+ ('copysign', (1.75, -NAN), -1.75), # special case for -NAN here
]
OVFCASES = [
diff --git a/pypy/module/math/test/test_math.py b/pypy/module/math/test/test_math.py
--- a/pypy/module/math/test/test_math.py
+++ b/pypy/module/math/test/test_math.py
@@ -1,3 +1,4 @@
+from __future__ import with_statement
import sys
from pypy.conftest import gettestobjspace
from pypy.module.math.test import test_direct
@@ -268,3 +269,7 @@
def __trunc__(self):
return "truncated"
assert math.trunc(foo()) == "truncated"
+
+ def test_copysign_nan(self):
+ import math
+ assert math.copysign(1.0, float('-nan')) == -1.0
diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -37,26 +37,44 @@
'True_': 'types.Bool.True',
'False_': 'types.Bool.False',
+ 'typeinfo': 'interp_dtype.get_dtype_cache(space).w_typeinfo',
+
'generic': 'interp_boxes.W_GenericBox',
'number': 'interp_boxes.W_NumberBox',
'integer': 'interp_boxes.W_IntegerBox',
'signedinteger': 'interp_boxes.W_SignedIntegerBox',
'unsignedinteger': 'interp_boxes.W_UnsignedIntegerBox',
'bool_': 'interp_boxes.W_BoolBox',
+ 'bool8': 'interp_boxes.W_BoolBox',
'int8': 'interp_boxes.W_Int8Box',
+ 'byte': 'interp_boxes.W_Int8Box',
'uint8': 'interp_boxes.W_UInt8Box',
+ 'ubyte': 'interp_boxes.W_UInt8Box',
'int16': 'interp_boxes.W_Int16Box',
+ 'short': 'interp_boxes.W_Int16Box',
'uint16': 'interp_boxes.W_UInt16Box',
+ 'ushort': 'interp_boxes.W_UInt16Box',
'int32': 'interp_boxes.W_Int32Box',
+ 'intc': 'interp_boxes.W_Int32Box',
'uint32': 'interp_boxes.W_UInt32Box',
+ 'uintc': 'interp_boxes.W_UInt32Box',
'int64': 'interp_boxes.W_Int64Box',
'uint64': 'interp_boxes.W_UInt64Box',
+ 'longlong': 'interp_boxes.W_LongLongBox',
+ 'ulonglong': 'interp_boxes.W_ULongLongBox',
'int_': 'interp_boxes.W_LongBox',
'inexact': 'interp_boxes.W_InexactBox',
'floating': 'interp_boxes.W_FloatingBox',
'float_': 'interp_boxes.W_Float64Box',
'float32': 'interp_boxes.W_Float32Box',
'float64': 'interp_boxes.W_Float64Box',
+ 'intp': 'types.IntP.BoxType',
+ 'uintp': 'types.UIntP.BoxType',
+ 'flexible': 'interp_boxes.W_FlexibleBox',
+ 'character': 'interp_boxes.W_CharacterBox',
+ 'str_': 'interp_boxes.W_StringBox',
+ 'unicode_': 'interp_boxes.W_UnicodeBox',
+ 'void': 'interp_boxes.W_VoidBox',
}
# ufuncs
@@ -67,6 +85,7 @@
("arccos", "arccos"),
("arcsin", "arcsin"),
("arctan", "arctan"),
+ ("arctan2", "arctan2"),
("arccosh", "arccosh"),
("arcsinh", "arcsinh"),
("arctanh", "arctanh"),
@@ -77,7 +96,10 @@
("true_divide", "true_divide"),
("equal", "equal"),
("exp", "exp"),
+ ("exp2", "exp2"),
+ ("expm1", "expm1"),
("fabs", "fabs"),
+ ("fmod", "fmod"),
("floor", "floor"),
("ceil", "ceil"),
("greater", "greater"),
@@ -89,8 +111,13 @@
("multiply", "multiply"),
("negative", "negative"),
("not_equal", "not_equal"),
+ ("radians", "radians"),
+ ("degrees", "degrees"),
+ ("deg2rad", "radians"),
+ ("rad2deg", "degrees"),
("reciprocal", "reciprocal"),
("sign", "sign"),
+ ("signbit", "signbit"),
("sin", "sin"),
("sinh", "sinh"),
("subtract", "subtract"),
@@ -103,10 +130,21 @@
('bitwise_not', 'invert'),
('isnan', 'isnan'),
('isinf', 'isinf'),
+ ('isneginf', 'isneginf'),
+ ('isposinf', 'isposinf'),
+ ('isfinite', 'isfinite'),
('logical_and', 'logical_and'),
('logical_xor', 'logical_xor'),
('logical_not', 'logical_not'),
('logical_or', 'logical_or'),
+ ('log', 'log'),
+ ('log2', 'log2'),
+ ('log10', 'log10'),
+ ('log1p', 'log1p'),
+ ('power', 'power'),
+ ('floor_divide', 'floor_divide'),
+ ('logaddexp', 'logaddexp'),
+ ('logaddexp2', 'logaddexp2'),
]:
interpleveldefs[exposed] = "interp_ufuncs.get(space).%s" % impl
diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -33,7 +33,7 @@
pass
SINGLE_ARG_FUNCTIONS = ["sum", "prod", "max", "min", "all", "any",
- "unegative", "flat"]
+ "unegative", "flat", "tostring"]
TWO_ARG_FUNCTIONS = ["dot", 'take']
class FakeSpace(object):
@@ -51,6 +51,8 @@
w_long = "long"
w_tuple = 'tuple'
w_slice = "slice"
+ w_str = "str"
+ w_unicode = "unicode"
def __init__(self):
"""NOT_RPYTHON"""
@@ -91,8 +93,12 @@
return BoolObject(obj)
elif isinstance(obj, int):
return IntObject(obj)
+ elif isinstance(obj, long):
+ return LongObject(obj)
elif isinstance(obj, W_Root):
return obj
+ elif isinstance(obj, str):
+ return StringObject(obj)
raise NotImplementedError
def newlist(self, items):
@@ -120,6 +126,11 @@
return int(w_obj.floatval)
raise NotImplementedError
+ def str_w(self, w_obj):
+ if isinstance(w_obj, StringObject):
+ return w_obj.v
+ raise NotImplementedError
+
def int(self, w_obj):
if isinstance(w_obj, IntObject):
return w_obj
@@ -151,7 +162,13 @@
return instantiate(klass)
def newtuple(self, list_w):
- raise ValueError
+ return ListObject(list_w)
+
+ def newdict(self):
+ return {}
+
+ def setitem(self, dict, item, value):
+ dict[item] = value
def len_w(self, w_obj):
if isinstance(w_obj, ListObject):
@@ -178,6 +195,11 @@
def __init__(self, intval):
self.intval = intval
+class LongObject(W_Root):
+ tp = FakeSpace.w_long
+ def __init__(self, intval):
+ self.intval = intval
+
class ListObject(W_Root):
tp = FakeSpace.w_list
def __init__(self, items):
@@ -190,6 +212,11 @@
self.stop = stop
self.step = step
+class StringObject(W_Root):
+ tp = FakeSpace.w_str
+ def __init__(self, v):
+ self.v = v
+
class InterpreterState(object):
def __init__(self, code):
self.code = code
@@ -407,6 +434,9 @@
w_res = neg.call(interp.space, [arr])
elif self.name == "flat":
w_res = arr.descr_get_flatiter(interp.space)
+ elif self.name == "tostring":
+ arr.descr_tostring(interp.space)
+ w_res = None
else:
assert False # unreachable code
elif self.name in TWO_ARG_FUNCTIONS:
diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -1,24 +1,25 @@
from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter.error import operationerrfmt
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.error import operationerrfmt, OperationError
+from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef
from pypy.objspace.std.floattype import float_typedef
+from pypy.objspace.std.stringtype import str_typedef
+from pypy.objspace.std.unicodetype import unicode_typedef, unicode_from_object
from pypy.objspace.std.inttype import int_typedef
from pypy.rlib.rarithmetic import LONG_BIT
from pypy.tool.sourcetools import func_with_new_name
-
MIXIN_32 = (int_typedef,) if LONG_BIT == 32 else ()
MIXIN_64 = (int_typedef,) if LONG_BIT == 64 else ()
def new_dtype_getter(name):
- def get_dtype(space):
+ def _get_dtype(space):
from pypy.module.micronumpy.interp_dtype import get_dtype_cache
return getattr(get_dtype_cache(space), "w_%sdtype" % name)
def new(space, w_subtype, w_value):
- dtype = get_dtype(space)
+ dtype = _get_dtype(space)
return dtype.itemtype.coerce_subtype(space, w_subtype, w_value)
- return func_with_new_name(new, name + "_box_new"), staticmethod(get_dtype)
+ return func_with_new_name(new, name + "_box_new"), staticmethod(_get_dtype)
class PrimitiveBox(object):
_mixin_ = True
@@ -29,7 +30,6 @@
def convert_to(self, dtype):
return dtype.box(self.value)
-
class W_GenericBox(Wrappable):
_attrs_ = ()
@@ -38,19 +38,22 @@
w_subtype.getname(space, '?')
)
+ def get_dtype(self, space):
+ return self._get_dtype(space)
+
def descr_str(self, space):
- return self.descr_repr(space)
-
- def descr_repr(self, space):
return space.wrap(self.get_dtype(space).itemtype.str_format(self))
+ def descr_format(self, space, w_spec):
+ return space.format(self.item(space), w_spec)
+
def descr_int(self, space):
- box = self.convert_to(W_LongBox.get_dtype(space))
+ box = self.convert_to(W_LongBox._get_dtype(space))
assert isinstance(box, W_LongBox)
return space.wrap(box.value)
def descr_float(self, space):
- box = self.convert_to(W_Float64Box.get_dtype(space))
+ box = self.convert_to(W_Float64Box._get_dtype(space))
assert isinstance(box, W_Float64Box)
return space.wrap(box.value)
@@ -81,6 +84,7 @@
descr_mul = _binop_impl("multiply")
descr_div = _binop_impl("divide")
descr_truediv = _binop_impl("true_divide")
+ descr_floordiv = _binop_impl("floor_divide")
descr_mod = _binop_impl("mod")
descr_pow = _binop_impl("power")
descr_lshift = _binop_impl("left_shift")
@@ -101,6 +105,7 @@
descr_rmul = _binop_right_impl("multiply")
descr_rdiv = _binop_right_impl("divide")
descr_rtruediv = _binop_right_impl("true_divide")
+ descr_rfloordiv = _binop_right_impl("floor_divide")
descr_rmod = _binop_right_impl("mod")
descr_rpow = _binop_right_impl("power")
descr_rlshift = _binop_right_impl("left_shift")
@@ -129,7 +134,7 @@
class W_BoolBox(W_GenericBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("bool")
+ descr__new__, _get_dtype = new_dtype_getter("bool")
class W_NumberBox(W_GenericBox):
_attrs_ = ()
@@ -145,34 +150,40 @@
pass
class W_Int8Box(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("int8")
+ descr__new__, _get_dtype = new_dtype_getter("int8")
class W_UInt8Box(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("uint8")
+ descr__new__, _get_dtype = new_dtype_getter("uint8")
class W_Int16Box(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("int16")
+ descr__new__, _get_dtype = new_dtype_getter("int16")
class W_UInt16Box(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("uint16")
+ descr__new__, _get_dtype = new_dtype_getter("uint16")
class W_Int32Box(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("int32")
+ descr__new__, _get_dtype = new_dtype_getter("int32")
class W_UInt32Box(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("uint32")
+ descr__new__, _get_dtype = new_dtype_getter("uint32")
class W_LongBox(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("long")
+ descr__new__, _get_dtype = new_dtype_getter("long")
class W_ULongBox(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("ulong")
+ descr__new__, _get_dtype = new_dtype_getter("ulong")
class W_Int64Box(W_SignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("int64")
+ descr__new__, _get_dtype = new_dtype_getter("int64")
+
+class W_LongLongBox(W_SignedIntegerBox, PrimitiveBox):
+ descr__new__, _get_dtype = new_dtype_getter('longlong')
class W_UInt64Box(W_UnsignedIntegerBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("uint64")
+ descr__new__, _get_dtype = new_dtype_getter("uint64")
+
+class W_ULongLongBox(W_SignedIntegerBox, PrimitiveBox):
+ descr__new__, _get_dtype = new_dtype_getter('ulonglong')
class W_InexactBox(W_NumberBox):
_attrs_ = ()
@@ -181,12 +192,71 @@
_attrs_ = ()
class W_Float32Box(W_FloatingBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("float32")
+ descr__new__, _get_dtype = new_dtype_getter("float32")
class W_Float64Box(W_FloatingBox, PrimitiveBox):
- descr__new__, get_dtype = new_dtype_getter("float64")
+ descr__new__, _get_dtype = new_dtype_getter("float64")
+class W_FlexibleBox(W_GenericBox):
+ def __init__(self, arr, ofs, dtype):
+ self.arr = arr # we have to keep array alive
+ self.ofs = ofs
+ self.dtype = dtype
+
+ def get_dtype(self, space):
+ return self.arr.dtype
+
+ at unwrap_spec(self=W_GenericBox)
+def descr_index(space, self):
+ return space.index(self.item(space))
+
+class W_VoidBox(W_FlexibleBox):
+ @unwrap_spec(item=str)
+ def descr_getitem(self, space, item):
+ try:
+ ofs, dtype = self.dtype.fields[item]
+ except KeyError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("Field %s does not exist" % item))
+ return dtype.itemtype.read(self.arr, 1, self.ofs, ofs, dtype)
+
+ @unwrap_spec(item=str)
+ def descr_setitem(self, space, item, w_value):
+ try:
+ ofs, dtype = self.dtype.fields[item]
+ except KeyError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("Field %s does not exist" % item))
+ dtype.itemtype.store(self.arr, 1, self.ofs, ofs,
+ dtype.coerce(space, w_value))
+
+class W_CharacterBox(W_FlexibleBox):
+ pass
+
+class W_StringBox(W_CharacterBox):
+ def descr__new__string_box(space, w_subtype, w_arg):
+ from pypy.module.micronumpy.interp_numarray import W_NDimArray
+ from pypy.module.micronumpy.interp_dtype import new_string_dtype
+
+ arg = space.str_w(space.str(w_arg))
+ arr = W_NDimArray([1], new_string_dtype(space, len(arg)))
+ for i in range(len(arg)):
+ arr.storage[i] = arg[i]
+ return W_StringBox(arr, 0, arr.dtype)
+
+
+class W_UnicodeBox(W_CharacterBox):
+ def descr__new__unicode_box(space, w_subtype, w_arg):
+ from pypy.module.micronumpy.interp_numarray import W_NDimArray
+ from pypy.module.micronumpy.interp_dtype import new_unicode_dtype
+
+ arg = space.unicode_w(unicode_from_object(space, w_arg))
+ arr = W_NDimArray([1], new_unicode_dtype(space, len(arg)))
+ # XXX not this way, we need store
+ #for i in range(len(arg)):
+ # arr.storage[i] = arg[i]
+ return W_UnicodeBox(arr, 0, arr.dtype)
W_GenericBox.typedef = TypeDef("generic",
__module__ = "numpypy",
@@ -194,7 +264,8 @@
__new__ = interp2app(W_GenericBox.descr__new__.im_func),
__str__ = interp2app(W_GenericBox.descr_str),
- __repr__ = interp2app(W_GenericBox.descr_repr),
+ __repr__ = interp2app(W_GenericBox.descr_str),
+ __format__ = interp2app(W_GenericBox.descr_format),
__int__ = interp2app(W_GenericBox.descr_int),
__float__ = interp2app(W_GenericBox.descr_float),
__nonzero__ = interp2app(W_GenericBox.descr_nonzero),
@@ -204,6 +275,7 @@
__mul__ = interp2app(W_GenericBox.descr_mul),
__div__ = interp2app(W_GenericBox.descr_div),
__truediv__ = interp2app(W_GenericBox.descr_truediv),
+ __floordiv__ = interp2app(W_GenericBox.descr_floordiv),
__mod__ = interp2app(W_GenericBox.descr_mod),
__divmod__ = interp2app(W_GenericBox.descr_divmod),
__pow__ = interp2app(W_GenericBox.descr_pow),
@@ -218,6 +290,7 @@
__rmul__ = interp2app(W_GenericBox.descr_rmul),
__rdiv__ = interp2app(W_GenericBox.descr_rdiv),
__rtruediv__ = interp2app(W_GenericBox.descr_rtruediv),
+ __rfloordiv__ = interp2app(W_GenericBox.descr_rfloordiv),
__rmod__ = interp2app(W_GenericBox.descr_rmod),
__rdivmod__ = interp2app(W_GenericBox.descr_rdivmod),
__rpow__ = interp2app(W_GenericBox.descr_rpow),
@@ -245,6 +318,8 @@
W_BoolBox.typedef = TypeDef("bool_", W_GenericBox.typedef,
__module__ = "numpypy",
__new__ = interp2app(W_BoolBox.descr__new__.im_func),
+
+ __index__ = interp2app(descr_index),
)
W_NumberBox.typedef = TypeDef("number", W_GenericBox.typedef,
@@ -266,36 +341,43 @@
W_Int8Box.typedef = TypeDef("int8", W_SignedIntegerBox.typedef,
__module__ = "numpypy",
__new__ = interp2app(W_Int8Box.descr__new__.im_func),
+ __index__ = interp2app(descr_index),
)
W_UInt8Box.typedef = TypeDef("uint8", W_UnsignedIntegerBox.typedef,
__module__ = "numpypy",
__new__ = interp2app(W_UInt8Box.descr__new__.im_func),
+ __index__ = interp2app(descr_index),
)
W_Int16Box.typedef = TypeDef("int16", W_SignedIntegerBox.typedef,
__module__ = "numpypy",
__new__ = interp2app(W_Int16Box.descr__new__.im_func),
+ __index__ = interp2app(descr_index),
)
W_UInt16Box.typedef = TypeDef("uint16", W_UnsignedIntegerBox.typedef,
__module__ = "numpypy",
__new__ = interp2app(W_UInt16Box.descr__new__.im_func),
+ __index__ = interp2app(descr_index),
)
W_Int32Box.typedef = TypeDef("int32", (W_SignedIntegerBox.typedef,) + MIXIN_32,
__module__ = "numpypy",
__new__ = interp2app(W_Int32Box.descr__new__.im_func),
+ __index__ = interp2app(descr_index),
)
W_UInt32Box.typedef = TypeDef("uint32", W_UnsignedIntegerBox.typedef,
__module__ = "numpypy",
__new__ = interp2app(W_UInt32Box.descr__new__.im_func),
+ __index__ = interp2app(descr_index),
)
W_Int64Box.typedef = TypeDef("int64", (W_SignedIntegerBox.typedef,) + MIXIN_64,
__module__ = "numpypy",
__new__ = interp2app(W_Int64Box.descr__new__.im_func),
+ __index__ = interp2app(descr_index),
)
if LONG_BIT == 32:
@@ -308,6 +390,7 @@
W_UInt64Box.typedef = TypeDef("uint64", W_UnsignedIntegerBox.typedef,
__module__ = "numpypy",
__new__ = interp2app(W_UInt64Box.descr__new__.im_func),
+ __index__ = interp2app(descr_index),
)
W_InexactBox.typedef = TypeDef("inexact", W_NumberBox.typedef,
@@ -330,3 +413,28 @@
__new__ = interp2app(W_Float64Box.descr__new__.im_func),
)
+
+W_FlexibleBox.typedef = TypeDef("flexible", W_GenericBox.typedef,
+ __module__ = "numpypy",
+)
+
+W_VoidBox.typedef = TypeDef("void", W_FlexibleBox.typedef,
+ __module__ = "numpypy",
+ __getitem__ = interp2app(W_VoidBox.descr_getitem),
+ __setitem__ = interp2app(W_VoidBox.descr_setitem),
+)
+
+W_CharacterBox.typedef = TypeDef("character", W_FlexibleBox.typedef,
+ __module__ = "numpypy",
+)
+
+W_StringBox.typedef = TypeDef("string_", (str_typedef, W_CharacterBox.typedef),
+ __module__ = "numpypy",
+ __new__ = interp2app(W_StringBox.descr__new__string_box.im_func),
+)
+
+W_UnicodeBox.typedef = TypeDef("unicode_", (unicode_typedef, W_CharacterBox.typedef),
+ __module__ = "numpypy",
+ __new__ = interp2app(W_UnicodeBox.descr__new__unicode_box.im_func),
+)
+
diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -1,26 +1,29 @@
+
+import sys
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import (TypeDef, GetSetProperty,
interp_attrproperty, interp_attrproperty_w)
-from pypy.module.micronumpy import types, signature, interp_boxes
+from pypy.module.micronumpy import types, interp_boxes
from pypy.rlib.objectmodel import specialize
-from pypy.rlib.rarithmetic import LONG_BIT
-from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.rlib.rarithmetic import LONG_BIT, r_longlong, r_ulonglong
UNSIGNEDLTR = "u"
SIGNEDLTR = "i"
BOOLLTR = "b"
FLOATINGLTR = "f"
-
-
-VOID_STORAGE = lltype.Array(lltype.Char, hints={'nolength': True, 'render_as_void': True})
+VOIDLTR = 'V'
+STRINGLTR = 'S'
+UNICODELTR = 'U'
class W_Dtype(Wrappable):
_immutable_fields_ = ["itemtype", "num", "kind"]
- def __init__(self, itemtype, num, kind, name, char, w_box_type, alternate_constructors=[], aliases=[]):
+ def __init__(self, itemtype, num, kind, name, char, w_box_type,
+ alternate_constructors=[], aliases=[],
+ fields=None, fieldnames=None, native=True):
self.itemtype = itemtype
self.num = num
self.kind = kind
@@ -29,53 +32,28 @@
self.w_box_type = w_box_type
self.alternate_constructors = alternate_constructors
self.aliases = aliases
-
- def malloc(self, length):
- # XXX find out why test_zjit explodes with tracking of allocations
- return lltype.malloc(VOID_STORAGE, self.itemtype.get_element_size() * length,
- zero=True, flavor="raw",
- track_allocation=False, add_memory_pressure=True
- )
+ self.fields = fields
+ self.fieldnames = fieldnames
+ self.native = native
@specialize.argtype(1)
def box(self, value):
return self.itemtype.box(value)
def coerce(self, space, w_item):
- return self.itemtype.coerce(space, w_item)
+ return self.itemtype.coerce(space, self, w_item)
- def getitem(self, storage, i):
- return self.itemtype.read(storage, self.itemtype.get_element_size(), i, 0)
+ def getitem(self, arr, i):
+ return self.itemtype.read(arr, 1, i, 0)
- def getitem_bool(self, storage, i):
- isize = self.itemtype.get_element_size()
- return self.itemtype.read_bool(storage, isize, i, 0)
+ def getitem_bool(self, arr, i):
+ return self.itemtype.read_bool(arr, 1, i, 0)
- def setitem(self, storage, i, box):
- self.itemtype.store(storage, self.itemtype.get_element_size(), i, 0, box)
+ def setitem(self, arr, i, box):
+ self.itemtype.store(arr, 1, i, 0, box)
def fill(self, storage, box, start, stop):
- self.itemtype.fill(storage, self.itemtype.get_element_size(), box, start, stop, 0)
-
- def descr__new__(space, w_subtype, w_dtype):
- cache = get_dtype_cache(space)
-
- if space.is_w(w_dtype, space.w_None):
- return cache.w_float64dtype
- elif space.isinstance_w(w_dtype, w_subtype):
- return w_dtype
- elif space.isinstance_w(w_dtype, space.w_str):
- name = space.str_w(w_dtype)
- for dtype in cache.builtin_dtypes:
- if dtype.name == name or dtype.char == name or name in dtype.aliases:
- return dtype
- else:
- for dtype in cache.builtin_dtypes:
- if w_dtype in dtype.alternate_constructors:
- return dtype
- if w_dtype is dtype.w_box_type:
- return dtype
- raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
+ self.itemtype.fill(storage, self.get_size(), box, start, stop, 0)
def descr_str(self, space):
return space.wrap(self.name)
@@ -86,6 +64,14 @@
def descr_get_itemsize(self, space):
return space.wrap(self.itemtype.get_element_size())
+ def descr_get_byteorder(self, space):
+ if self.native:
+ return space.wrap('=')
+ return space.wrap(nonnative_byteorder_prefix)
+
+ def descr_get_alignment(self, space):
+ return space.wrap(self.itemtype.alignment)
+
def descr_get_shape(self, space):
return space.newtuple([])
@@ -99,31 +85,193 @@
def descr_ne(self, space, w_other):
return space.wrap(not self.eq(space, w_other))
+ def descr_get_fields(self, space):
+ if self.fields is None:
+ return space.w_None
+ w_d = space.newdict()
+ for name, (offset, subdtype) in self.fields.iteritems():
+ space.setitem(w_d, space.wrap(name), space.newtuple([subdtype,
+ space.wrap(offset)]))
+ return w_d
+
+ def descr_get_names(self, space):
+ if self.fieldnames is None:
+ return space.w_None
+ return space.newtuple([space.wrap(name) for name in self.fieldnames])
+
+ @unwrap_spec(item=str)
+ def descr_getitem(self, space, item):
+ if self.fields is None:
+ raise OperationError(space.w_KeyError, space.wrap("There are no keys in dtypes %s" % self.name))
+ try:
+ return self.fields[item][1]
+ except KeyError:
+ raise OperationError(space.w_KeyError, space.wrap("Field named %s not found" % item))
+
def is_int_type(self):
return (self.kind == SIGNEDLTR or self.kind == UNSIGNEDLTR or
self.kind == BOOLLTR)
+ def is_signed(self):
+ return self.kind == SIGNEDLTR
+
def is_bool_type(self):
return self.kind == BOOLLTR
+ def is_record_type(self):
+ return self.fields is not None
+
+ def __repr__(self):
+ if self.fields is not None:
+ return '<DType %r>' % self.fields
+ return '<DType %r>' % self.itemtype
+
+ def get_size(self):
+ return self.itemtype.get_element_size()
+
+def dtype_from_list(space, w_lst):
+ lst_w = space.listview(w_lst)
+ fields = {}
+ offset = 0
+ ofs_and_items = []
+ fieldnames = []
+ for w_elem in lst_w:
+ w_fldname, w_flddesc = space.fixedview(w_elem, 2)
+ subdtype = descr__new__(space, space.gettypefor(W_Dtype), w_flddesc)
+ fldname = space.str_w(w_fldname)
+ if fldname in fields:
+ raise OperationError(space.w_ValueError, space.wrap("two fields with the same name"))
+ assert isinstance(subdtype, W_Dtype)
+ fields[fldname] = (offset, subdtype)
+ ofs_and_items.append((offset, subdtype.itemtype))
+ offset += subdtype.itemtype.get_element_size()
+ fieldnames.append(fldname)
+ itemtype = types.RecordType(ofs_and_items, offset)
+ return W_Dtype(itemtype, 20, VOIDLTR, "void" + str(8 * itemtype.get_element_size()),
+ "V", space.gettypefor(interp_boxes.W_VoidBox), fields=fields,
+ fieldnames=fieldnames)
+
+def dtype_from_dict(space, w_dict):
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "dtype from dict"))
+
+def variable_dtype(space, name):
+ if name[0] in '<>=':
+ name = name[1:]
+ char = name[0]
+ if len(name) == 1:
+ size = 0
+ else:
+ try:
+ size = int(name[1:])
+ except ValueError:
+ raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
+ if char == 'S':
+ itemtype = types.StringType(size)
+ basename = 'string'
+ num = 18
+ w_box_type = space.gettypefor(interp_boxes.W_StringBox)
+ elif char == 'V':
+ num = 20
+ basename = 'void'
+ w_box_type = space.gettypefor(interp_boxes.W_VoidBox)
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "pure void dtype"))
+ else:
+ assert char == 'U'
+ basename = 'unicode'
+ itemtype = types.UnicodeType(size)
+ num = 19
+ w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox)
+ return W_Dtype(itemtype, num, char,
+ basename + str(8 * itemtype.get_element_size()),
+ char, w_box_type)
+
+def dtype_from_spec(space, name):
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "dtype from spec"))
+
+def descr__new__(space, w_subtype, w_dtype):
+ cache = get_dtype_cache(space)
+
+ if space.is_w(w_dtype, space.w_None):
+ return cache.w_float64dtype
+ elif space.isinstance_w(w_dtype, w_subtype):
+ return w_dtype
+ elif space.isinstance_w(w_dtype, space.w_str):
+ name = space.str_w(w_dtype)
+ if ',' in name:
+ return dtype_from_spec(space, name)
+ try:
+ return cache.dtypes_by_name[name]
+ except KeyError:
+ pass
+ if name[0] in 'VSU' or name[0] in '<>=' and name[1] in 'VSU':
+ return variable_dtype(space, name)
+ elif space.isinstance_w(w_dtype, space.w_list):
+ return dtype_from_list(space, w_dtype)
+ elif space.isinstance_w(w_dtype, space.w_dict):
+ return dtype_from_dict(space, w_dtype)
+ else:
+ for dtype in cache.builtin_dtypes:
+ if w_dtype in dtype.alternate_constructors:
+ return dtype
+ if w_dtype is dtype.w_box_type:
+ return dtype
+ raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
+
W_Dtype.typedef = TypeDef("dtype",
__module__ = "numpypy",
- __new__ = interp2app(W_Dtype.descr__new__.im_func),
+ __new__ = interp2app(descr__new__),
__str__= interp2app(W_Dtype.descr_str),
__repr__ = interp2app(W_Dtype.descr_repr),
__eq__ = interp2app(W_Dtype.descr_eq),
__ne__ = interp2app(W_Dtype.descr_ne),
+ __getitem__ = interp2app(W_Dtype.descr_getitem),
num = interp_attrproperty("num", cls=W_Dtype),
kind = interp_attrproperty("kind", cls=W_Dtype),
+ char = interp_attrproperty("char", cls=W_Dtype),
type = interp_attrproperty_w("w_box_type", cls=W_Dtype),
+ byteorder = GetSetProperty(W_Dtype.descr_get_byteorder),
itemsize = GetSetProperty(W_Dtype.descr_get_itemsize),
+ alignment = GetSetProperty(W_Dtype.descr_get_alignment),
shape = GetSetProperty(W_Dtype.descr_get_shape),
name = interp_attrproperty('name', cls=W_Dtype),
+ fields = GetSetProperty(W_Dtype.descr_get_fields),
+ names = GetSetProperty(W_Dtype.descr_get_names),
)
W_Dtype.typedef.acceptable_as_base_class = False
+if sys.byteorder == 'little':
+ byteorder_prefix = '<'
+ nonnative_byteorder_prefix = '>'
+else:
+ byteorder_prefix = '>'
+ nonnative_byteorder_prefix = '<'
+
+def new_string_dtype(space, size):
+ return W_Dtype(
+ types.StringType(size),
+ num=18,
+ kind=STRINGLTR,
+ name='string',
+ char='S' + str(size),
+ w_box_type = space.gettypefor(interp_boxes.W_StringBox),
+ )
+
+def new_unicode_dtype(space, size):
+ return W_Dtype(
+ types.UnicodeType(size),
+ num=19,
+ kind=UNICODELTR,
+ name='unicode',
+ char='U' + str(size),
+ w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
+ )
+
+
class DtypeCache(object):
def __init__(self, space):
self.w_booldtype = W_Dtype(
@@ -211,7 +359,6 @@
name="int64",
char="q",
w_box_type=space.gettypefor(interp_boxes.W_Int64Box),
- alternate_constructors=[space.w_long],
)
self.w_uint64dtype = W_Dtype(
types.UInt64(),
@@ -239,18 +386,149 @@
alternate_constructors=[space.w_float],
aliases=["float"],
)
-
+ self.w_longlongdtype = W_Dtype(
+ types.Int64(),
+ num=9,
+ kind=SIGNEDLTR,
+ name='int64',
+ char='q',
+ w_box_type = space.gettypefor(interp_boxes.W_LongLongBox),
+ alternate_constructors=[space.w_long],
+ )
+ self.w_ulonglongdtype = W_Dtype(
+ types.UInt64(),
+ num=10,
+ kind=UNSIGNEDLTR,
+ name='uint64',
+ char='Q',
+ w_box_type = space.gettypefor(interp_boxes.W_ULongLongBox),
+ )
+ self.w_stringdtype = W_Dtype(
+ types.StringType(1),
+ num=18,
+ kind=STRINGLTR,
+ name='string',
+ char='S',
+ w_box_type = space.gettypefor(interp_boxes.W_StringBox),
+ alternate_constructors=[space.w_str],
+ )
+ self.w_unicodedtype = W_Dtype(
+ types.UnicodeType(1),
+ num=19,
+ kind=UNICODELTR,
+ name='unicode',
+ char='U',
+ w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
+ alternate_constructors=[space.w_unicode],
+ )
+ self.w_voiddtype = W_Dtype(
+ types.VoidType(0),
+ num=20,
+ kind=VOIDLTR,
+ name='void',
+ char='V',
+ w_box_type = space.gettypefor(interp_boxes.W_VoidBox),
+ #alternate_constructors=[space.w_buffer],
+ # XXX no buffer in space
+ )
self.builtin_dtypes = [
self.w_booldtype, self.w_int8dtype, self.w_uint8dtype,
self.w_int16dtype, self.w_uint16dtype, self.w_int32dtype,
self.w_uint32dtype, self.w_longdtype, self.w_ulongdtype,
- self.w_int64dtype, self.w_uint64dtype, self.w_float32dtype,
- self.w_float64dtype
+ self.w_longlongdtype, self.w_ulonglongdtype,
+ self.w_float32dtype,
+ self.w_float64dtype, self.w_stringdtype, self.w_unicodedtype,
+ self.w_voiddtype,
]
self.dtypes_by_num_bytes = sorted(
(dtype.itemtype.get_element_size(), dtype)
for dtype in self.builtin_dtypes
)
+ self.dtypes_by_name = {}
+ for dtype in self.builtin_dtypes:
+ self.dtypes_by_name[dtype.name] = dtype
+ can_name = dtype.kind + str(dtype.itemtype.get_element_size())
+ self.dtypes_by_name[can_name] = dtype
+ self.dtypes_by_name[byteorder_prefix + can_name] = dtype
+ self.dtypes_by_name['=' + can_name] = dtype
+ new_name = nonnative_byteorder_prefix + can_name
+ itemtypename = dtype.itemtype.__class__.__name__
+ itemtype = getattr(types, 'NonNative' + itemtypename)()
+ self.dtypes_by_name[new_name] = W_Dtype(
+ itemtype,
+ dtype.num, dtype.kind, new_name, dtype.char, dtype.w_box_type,
+ native=False)
+ for alias in dtype.aliases:
+ self.dtypes_by_name[alias] = dtype
+ self.dtypes_by_name[dtype.char] = dtype
+
+ typeinfo_full = {
+ 'LONGLONG': self.w_int64dtype,
+ 'SHORT': self.w_int16dtype,
+ 'VOID': self.w_voiddtype,
+ #'LONGDOUBLE':,
+ 'UBYTE': self.w_uint8dtype,
+ 'UINTP': self.w_ulongdtype,
+ 'ULONG': self.w_ulongdtype,
+ 'LONG': self.w_longdtype,
+ 'UNICODE': self.w_unicodedtype,
+ #'OBJECT',
+ 'ULONGLONG': self.w_ulonglongdtype,
+ 'STRING': self.w_stringdtype,
+ #'CDOUBLE',
+ #'DATETIME',
+ 'UINT': self.w_uint32dtype,
+ 'INTP': self.w_longdtype,
+ #'HALF',
+ 'BYTE': self.w_int8dtype,
+ #'CFLOAT': ,
+ #'TIMEDELTA',
+ 'INT': self.w_int32dtype,
+ 'DOUBLE': self.w_float64dtype,
+ 'USHORT': self.w_uint16dtype,
+ 'FLOAT': self.w_float32dtype,
+ 'BOOL': self.w_booldtype,
+ #, 'CLONGDOUBLE']
+ }
+ typeinfo_partial = {
+ 'Generic': interp_boxes.W_GenericBox,
+ 'Character': interp_boxes.W_CharacterBox,
+ 'Flexible': interp_boxes.W_FlexibleBox,
+ 'Inexact': interp_boxes.W_InexactBox,
+ 'Integer': interp_boxes.W_IntegerBox,
+ 'SignedInteger': interp_boxes.W_SignedIntegerBox,
+ 'UnsignedInteger': interp_boxes.W_UnsignedIntegerBox,
+ #'ComplexFloating',
+ 'Number': interp_boxes.W_NumberBox,
+ 'Floating': interp_boxes.W_FloatingBox
+ }
+ w_typeinfo = space.newdict()
+ for k, v in typeinfo_partial.iteritems():
+ space.setitem(w_typeinfo, space.wrap(k), space.gettypefor(v))
+ for k, dtype in typeinfo_full.iteritems():
+ itemsize = dtype.itemtype.get_element_size()
+ items_w = [space.wrap(dtype.char),
+ space.wrap(dtype.num),
+ space.wrap(itemsize * 8), # in case of changing
+ # number of bits per byte in the future
+ space.wrap(itemsize or 1)]
+ if dtype.is_int_type():
+ if dtype.kind == BOOLLTR:
+ w_maxobj = space.wrap(1)
+ w_minobj = space.wrap(0)
+ elif dtype.is_signed():
+ w_maxobj = space.wrap(r_longlong((1 << (itemsize*8 - 1))
+ - 1))
+ w_minobj = space.wrap(r_longlong(-1) << (itemsize*8 - 1))
+ else:
+ w_maxobj = space.wrap(r_ulonglong(1 << (itemsize*8)) - 1)
+ w_minobj = space.wrap(0)
+ items_w = items_w + [w_maxobj, w_minobj]
+ items_w = items_w + [dtype.w_box_type]
+
+ w_tuple = space.newtuple(items_w)
+ space.setitem(w_typeinfo, space.wrap(k), w_tuple)
+ self.w_typeinfo = w_typeinfo
def get_dtype_cache(space):
return space.fromcache(DtypeCache)
diff --git a/pypy/module/micronumpy/interp_iter.py b/pypy/module/micronumpy/interp_iter.py
--- a/pypy/module/micronumpy/interp_iter.py
+++ b/pypy/module/micronumpy/interp_iter.py
@@ -2,7 +2,7 @@
from pypy.rlib import jit
from pypy.rlib.objectmodel import instantiate
from pypy.module.micronumpy.strides import calculate_broadcast_strides,\
- calculate_slice_strides, calculate_dot_strides
+ calculate_slice_strides, calculate_dot_strides, enumerate_chunks
""" This is a mini-tutorial on iterators, strides, and
memory layout. It assumes you are familiar with the terms, see
@@ -42,28 +42,81 @@
we can go faster.
All the calculations happen in next()
-next_step_x() tries to do the iteration for a number of steps at once,
+next_skip_x() tries to do the iteration for a number of steps at once,
but then we cannot gaurentee that we only overflow one single shape
dimension, perhaps we could overflow times in one big step.
"""
# structures to describe slicing
-class Chunk(object):
+class BaseChunk(object):
+ pass
+
+class RecordChunk(BaseChunk):
+ def __init__(self, name):
+ self.name = name
+
+ def apply(self, arr):
+ from pypy.module.micronumpy.interp_numarray import W_NDimSlice
+
+ arr = arr.get_concrete()
+ ofs, subdtype = arr.dtype.fields[self.name]
+ # strides backstrides are identical, ofs only changes start
+ return W_NDimSlice(arr.start + ofs, arr.strides[:], arr.backstrides[:],
+ arr.shape[:], arr, subdtype)
+
+class Chunks(BaseChunk):
+ def __init__(self, l):
+ self.l = l
+
+ @jit.unroll_safe
+ def extend_shape(self, old_shape):
+ shape = []
+ i = -1
+ for i, c in enumerate_chunks(self.l):
+ if c.step != 0:
+ shape.append(c.lgt)
+ s = i + 1
+ assert s >= 0
+ return shape[:] + old_shape[s:]
+
+ def apply(self, arr):
+ from pypy.module.micronumpy.interp_numarray import W_NDimSlice,\
+ VirtualSlice, ConcreteArray
+
+ shape = self.extend_shape(arr.shape)
+ if not isinstance(arr, ConcreteArray):
+ return VirtualSlice(arr, self, shape)
+ r = calculate_slice_strides(arr.shape, arr.start, arr.strides,
+ arr.backstrides, self.l)
+ _, start, strides, backstrides = r
+ return W_NDimSlice(start, strides[:], backstrides[:],
+ shape[:], arr)
+
+
+class Chunk(BaseChunk):
+ axis_step = 1
+
def __init__(self, start, stop, step, lgt):
self.start = start
self.stop = stop
self.step = step
self.lgt = lgt
- def extend_shape(self, shape):
- if self.step != 0:
- shape.append(self.lgt)
-
def __repr__(self):
return 'Chunk(%d, %d, %d, %d)' % (self.start, self.stop, self.step,
self.lgt)
+class NewAxisChunk(Chunk):
+ start = 0
+ stop = 1
+ step = 1
+ lgt = 1
+ axis_step = 0
+
+ def __init__(self):
+ pass
+
class BaseTransform(object):
pass
@@ -95,17 +148,19 @@
raise NotImplementedError
class ArrayIterator(BaseIterator):
- def __init__(self, size):
+ def __init__(self, size, element_size):
self.offset = 0
self.size = size
+ self.element_size = element_size
def next(self, shapelen):
return self.next_skip_x(1)
- def next_skip_x(self, ofs):
+ def next_skip_x(self, x):
arr = instantiate(ArrayIterator)
arr.size = self.size
- arr.offset = self.offset + ofs
+ arr.offset = self.offset + x * self.element_size
+ arr.element_size = self.element_size
return arr
def next_no_increase(self, shapelen):
@@ -152,7 +207,7 @@
elif isinstance(t, ViewTransform):
r = calculate_slice_strides(self.res_shape, self.offset,
self.strides,
- self.backstrides, t.chunks)
+ self.backstrides, t.chunks.l)
return ViewIterator(r[1], r[2], r[3], r[0])
@jit.unroll_safe
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -7,10 +7,10 @@
from pypy.module.micronumpy.appbridge import get_appbridge_cache
from pypy.module.micronumpy.dot import multidim_dot, match_dot_shapes
from pypy.module.micronumpy.interp_iter import (ArrayIterator,
- SkipLastAxisIterator, Chunk, ViewIterator)
-from pypy.module.micronumpy.strides import (calculate_slice_strides,
- shape_agreement, find_shape_and_elems, get_shape_from_iterable,
- calc_new_strides, to_coords)
+ SkipLastAxisIterator, Chunk, ViewIterator, Chunks, RecordChunk,
+ NewAxisChunk)
+from pypy.module.micronumpy.strides import (shape_agreement,
+ find_shape_and_elems, get_shape_from_iterable, calc_new_strides, to_coords)
from pypy.rlib import jit
from pypy.rlib.rstring import StringBuilder
from pypy.rpython.lltypesystem import lltype, rffi
@@ -47,7 +47,7 @@
)
flat_set_driver = jit.JitDriver(
greens=['shapelen', 'base'],
- reds=['step', 'ai', 'lngth', 'arr', 'basei'],
+ reds=['step', 'lngth', 'ri', 'arr', 'basei'],
name='numpy_flatset',
)
@@ -79,8 +79,8 @@
dtype = space.interp_w(interp_dtype.W_Dtype,
space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
)
- size, shape = _find_size_and_shape(space, w_size)
- return space.wrap(W_NDimArray(size, shape[:], dtype=dtype))
+ shape = _find_shape(space, w_size)
+ return space.wrap(W_NDimArray(shape[:], dtype=dtype))
def _unaryop_impl(ufunc_name):
def impl(self, space):
@@ -102,6 +102,7 @@
descr_mul = _binop_impl("multiply")
descr_div = _binop_impl("divide")
descr_truediv = _binop_impl("true_divide")
+ descr_floordiv = _binop_impl("floor_divide")
descr_mod = _binop_impl("mod")
descr_pow = _binop_impl("power")
descr_lshift = _binop_impl("left_shift")
@@ -136,6 +137,7 @@
descr_rmul = _binop_right_impl("multiply")
descr_rdiv = _binop_right_impl("divide")
descr_rtruediv = _binop_right_impl("true_divide")
+ descr_rfloordiv = _binop_right_impl("floor_divide")
descr_rmod = _binop_right_impl("mod")
descr_rpow = _binop_right_impl("power")
descr_rlshift = _binop_right_impl("left_shift")
@@ -223,8 +225,7 @@
return scalar_w(space, dtype, space.wrap(0))
# Do the dims match?
out_shape, other_critical_dim = match_dot_shapes(space, self, other)
- out_size = support.product(out_shape)
- result = W_NDimArray(out_size, out_shape, dtype)
+ result = W_NDimArray(out_shape, dtype)
# This is the place to add fpypy and blas
return multidim_dot(space, self.get_concrete(),
other.get_concrete(), result, dtype,
@@ -243,7 +244,7 @@
return space.wrap(self.find_dtype().itemtype.get_element_size())
def descr_get_nbytes(self, space):
- return space.wrap(self.size * self.find_dtype().itemtype.get_element_size())
+ return space.wrap(self.size)
@jit.unroll_safe
def descr_get_shape(self, space):
@@ -251,13 +252,16 @@
def descr_set_shape(self, space, w_iterable):
new_shape = get_shape_from_iterable(space,
- self.size, w_iterable)
+ support.product(self.shape), w_iterable)
if isinstance(self, Scalar):
return
self.get_concrete().setshape(space, new_shape)
def descr_get_size(self, space):
- return space.wrap(self.size)
+ return space.wrap(self.get_size())
+
+ def get_size(self):
+ return self.size // self.find_dtype().get_size()
def descr_copy(self, space):
return self.copy(space)
@@ -277,7 +281,7 @@
def empty_copy(self, space, dtype):
shape = self.shape
- return W_NDimArray(support.product(shape), shape[:], dtype, 'C')
+ return W_NDimArray(shape[:], dtype, 'C')
def descr_len(self, space):
if len(self.shape):
@@ -318,7 +322,16 @@
""" The result of getitem/setitem is a single item if w_idx
is a list of scalars that match the size of shape
"""
+ if space.isinstance_w(w_idx, space.w_str):
+ return False
shape_len = len(self.shape)
+ if space.isinstance_w(w_idx, space.w_tuple):
+ for w_item in space.fixedview(w_idx):
+ if (space.isinstance_w(w_item, space.w_slice) or
+ space.is_w(w_item, space.w_None)):
+ return False
+ elif space.is_w(w_idx, space.w_None):
+ return False
if shape_len == 0:
raise OperationError(space.w_IndexError, space.wrap(
"0-d arrays can't be indexed"))
@@ -334,43 +347,55 @@
if lgt > shape_len:
raise OperationError(space.w_IndexError,
space.wrap("invalid index"))
- if lgt < shape_len:
- return False
- for w_item in space.fixedview(w_idx):
- if space.isinstance_w(w_item, space.w_slice):
- return False
- return True
+ return lgt == shape_len
@jit.unroll_safe
def _prepare_slice_args(self, space, w_idx):
+ if space.isinstance_w(w_idx, space.w_str):
+ idx = space.str_w(w_idx)
+ dtype = self.find_dtype()
+ if not dtype.is_record_type() or idx not in dtype.fields:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "field named %s not defined" % idx))
+ return RecordChunk(idx)
if (space.isinstance_w(w_idx, space.w_int) or
space.isinstance_w(w_idx, space.w_slice)):
- return [Chunk(*space.decode_index4(w_idx, self.shape[0]))]
- return [Chunk(*space.decode_index4(w_item, self.shape[i])) for i, w_item in
- enumerate(space.fixedview(w_idx))]
+ return Chunks([Chunk(*space.decode_index4(w_idx, self.shape[0]))])
+ elif space.is_w(w_idx, space.w_None):
+ return Chunks([NewAxisChunk()])
+ result = []
+ i = 0
+ for w_item in space.fixedview(w_idx):
+ if space.is_w(w_item, space.w_None):
+ result.append(NewAxisChunk())
+ else:
+ result.append(Chunk(*space.decode_index4(w_item,
+ self.shape[i])))
+ i += 1
+ return Chunks(result)
- def count_all_true(self, arr):
- sig = arr.find_sig()
- frame = sig.create_frame(arr)
- shapelen = len(arr.shape)
+ def count_all_true(self):
+ sig = self.find_sig()
+ frame = sig.create_frame(self)
+ shapelen = len(self.shape)
s = 0
iter = None
while not frame.done():
- count_driver.jit_merge_point(arr=arr, frame=frame, iter=iter, s=s,
+ count_driver.jit_merge_point(arr=self, frame=frame, iter=iter, s=s,
shapelen=shapelen)
iter = frame.get_final_iter()
- s += arr.dtype.getitem_bool(arr.storage, iter.offset)
+ s += self.dtype.getitem_bool(self, iter.offset)
frame.next(shapelen)
return s
def getitem_filter(self, space, arr):
concr = arr.get_concrete()
- if concr.size > self.size:
+ if concr.get_size() > self.get_size():
raise OperationError(space.w_IndexError,
space.wrap("index out of range for array"))
- size = self.count_all_true(concr)
- res = W_NDimArray(size, [size], self.find_dtype())
- ri = ArrayIterator(size)
+ size = concr.count_all_true()
+ res = W_NDimArray([size], self.find_dtype())
+ ri = res.create_iter()
shapelen = len(self.shape)
argi = concr.create_iter()
sig = self.find_sig()
@@ -380,7 +405,7 @@
filter_driver.jit_merge_point(concr=concr, argi=argi, ri=ri,
frame=frame, v=v, res=res, sig=sig,
shapelen=shapelen, self=self)
- if concr.dtype.getitem_bool(concr.storage, argi.offset):
+ if concr.dtype.getitem_bool(concr, argi.offset):
v = sig.eval(frame, self)
res.setitem(ri.offset, v)
ri = ri.next(1)
@@ -390,23 +415,6 @@
frame.next(shapelen)
return res
- def setitem_filter(self, space, idx, val):
- size = self.count_all_true(idx)
- arr = SliceArray([size], self.dtype, self, val)
- sig = arr.find_sig()
- shapelen = len(self.shape)
- frame = sig.create_frame(arr)
- idxi = idx.create_iter()
- while not frame.done():
- filter_set_driver.jit_merge_point(idx=idx, idxi=idxi, sig=sig,
- frame=frame, arr=arr,
- shapelen=shapelen)
- if idx.dtype.getitem_bool(idx.storage, idxi.offset):
- sig.eval(frame, arr)
- frame.next_from_second(1)
- frame.next_first(shapelen)
- idxi = idxi.next(shapelen)
-
def descr_getitem(self, space, w_idx):
if (isinstance(w_idx, BaseArray) and w_idx.shape == self.shape and
w_idx.find_dtype().is_bool_type()):
@@ -416,7 +424,24 @@
item = concrete._index_of_single_item(space, w_idx)
return concrete.getitem(item)
chunks = self._prepare_slice_args(space, w_idx)
- return self.create_slice(chunks)
+ return chunks.apply(self)
+
+ def setitem_filter(self, space, idx, val):
+ size = idx.count_all_true()
+ arr = SliceArray([size], self.dtype, self, val)
+ sig = arr.find_sig()
+ shapelen = len(self.shape)
+ frame = sig.create_frame(arr)
+ idxi = idx.create_iter()
+ while not frame.done():
+ filter_set_driver.jit_merge_point(idx=idx, idxi=idxi, sig=sig,
+ frame=frame, arr=arr,
+ shapelen=shapelen)
+ if idx.dtype.getitem_bool(idx, idxi.offset):
+ sig.eval(frame, arr)
+ frame.next_from_second(1)
+ frame.next_first(shapelen)
+ idxi = idxi.next(shapelen)
def descr_setitem(self, space, w_idx, w_value):
self.invalidated()
@@ -434,26 +459,9 @@
if not isinstance(w_value, BaseArray):
w_value = convert_to_array(space, w_value)
chunks = self._prepare_slice_args(space, w_idx)
- view = self.create_slice(chunks).get_concrete()
+ view = chunks.apply(self).get_concrete()
view.setslice(space, w_value)
- @jit.unroll_safe
- def create_slice(self, chunks):
- shape = []
- i = -1
- for i, chunk in enumerate(chunks):
- chunk.extend_shape(shape)
- s = i + 1
- assert s >= 0
- shape += self.shape[s:]
- if not isinstance(self, ConcreteArray):
- return VirtualSlice(self, chunks, shape)
- r = calculate_slice_strides(self.shape, self.start, self.strides,
- self.backstrides, chunks)
- _, start, strides, backstrides = r
- return W_NDimSlice(start, strides[:], backstrides[:],
- shape[:], self)
-
def descr_reshape(self, space, args_w):
"""reshape(...)
a.reshape(shape)
@@ -470,13 +478,16 @@
w_shape = args_w[0]
else:
w_shape = space.newtuple(args_w)
- new_shape = get_shape_from_iterable(space, self.size, w_shape)
+ new_shape = get_shape_from_iterable(space, support.product(self.shape),
+ w_shape)
return self.reshape(space, new_shape)
def reshape(self, space, new_shape):
concrete = self.get_concrete()
# Since we got to here, prod(new_shape) == self.size
- new_strides = calc_new_strides(new_shape, concrete.shape,
+ new_strides = None
+ if self.size > 0:
+ new_strides = calc_new_strides(new_shape, concrete.shape,
concrete.strides, concrete.order)
if new_strides:
# We can create a view, strides somehow match up.
@@ -506,7 +517,7 @@
def descr_mean(self, space, w_axis=None):
if space.is_w(w_axis, space.w_None):
w_axis = space.wrap(-1)
- w_denom = space.wrap(self.size)
+ w_denom = space.wrap(support.product(self.shape))
else:
dim = space.int_w(w_axis)
w_denom = space.wrap(self.shape[dim])
@@ -525,7 +536,7 @@
concr.fill(space, w_value)
def descr_nonzero(self, space):
- if self.size > 1:
+ if self.get_size() > 1:
raise OperationError(space.w_ValueError, space.wrap(
"The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"))
concr = self.get_concrete_or_scalar()
@@ -604,8 +615,7 @@
space.wrap("axis unsupported for take"))
index_i = index.create_iter()
res_shape = index.shape
- size = support.product(res_shape)
- res = W_NDimArray(size, res_shape[:], concr.dtype, concr.order)
+ res = W_NDimArray(res_shape[:], concr.dtype, concr.order)
res_i = res.create_iter()
shapelen = len(index.shape)
sig = concr.find_sig()
@@ -644,6 +654,11 @@
raise OperationError(space.w_NotImplementedError, space.wrap(
"non-int arg not supported"))
+ def descr_tostring(self, space):
+ ra = ToStringArray(self)
+ loop.compute(ra)
+ return space.wrap(ra.s.build())
+
def compute_first_step(self, sig, frame):
pass
@@ -665,8 +680,7 @@
"""
Intermediate class representing a literal.
"""
- size = 1
- _attrs_ = ["dtype", "value", "shape"]
+ _attrs_ = ["dtype", "value", "shape", "size"]
def __init__(self, dtype, value):
self.shape = []
@@ -674,6 +688,7 @@
self.dtype = dtype
assert isinstance(value, interp_boxes.W_GenericBox)
self.value = value
+ self.size = dtype.get_size()
def find_dtype(self):
return self.dtype
@@ -691,8 +706,7 @@
return self
def reshape(self, space, new_shape):
- size = support.product(new_shape)
- res = W_NDimArray(size, new_shape, self.dtype, 'C')
+ res = W_NDimArray(new_shape, self.dtype, 'C')
res.setitem(0, self.value)
return res
@@ -705,6 +719,7 @@
self.forced_result = None
self.res_dtype = res_dtype
self.name = name
+ self.size = support.product(self.shape) * res_dtype.get_size()
def _del_sources(self):
# Function for deleting references to source arrays,
@@ -712,7 +727,7 @@
raise NotImplementedError
def compute(self):
- ra = ResultArray(self, self.size, self.shape, self.res_dtype)
+ ra = ResultArray(self, self.shape, self.res_dtype)
loop.compute(ra)
return ra.left
@@ -740,7 +755,6 @@
def __init__(self, child, chunks, shape):
self.child = child
self.chunks = chunks
- self.size = support.product(shape)
VirtualArray.__init__(self, 'slice', shape, child.find_dtype())
def create_sig(self):
@@ -752,7 +766,7 @@
def force_if_needed(self):
if self.forced_result is None:
concr = self.child.get_concrete()
- self.forced_result = concr.create_slice(self.chunks)
+ self.forced_result = self.chunks.apply(concr)
def _del_sources(self):
self.child = None
@@ -779,15 +793,12 @@
"""
Intermediate class for performing binary operations.
"""
- _immutable_fields_ = ['left', 'right']
-
def __init__(self, ufunc, name, shape, calc_dtype, res_dtype, left, right):
VirtualArray.__init__(self, name, shape, res_dtype)
self.ufunc = ufunc
self.left = left
self.right = right
self.calc_dtype = calc_dtype
- self.size = support.product(self.shape)
def _del_sources(self):
self.left = None
@@ -815,15 +826,30 @@
self.left.create_sig(), self.right.create_sig())
class ResultArray(Call2):
- def __init__(self, child, size, shape, dtype, res=None, order='C'):
+ def __init__(self, child, shape, dtype, res=None, order='C'):
if res is None:
- res = W_NDimArray(size, shape, dtype, order)
+ res = W_NDimArray(shape, dtype, order)
Call2.__init__(self, None, 'assign', shape, dtype, dtype, res, child)
def create_sig(self):
return signature.ResultSignature(self.res_dtype, self.left.create_sig(),
self.right.create_sig())
+class ToStringArray(Call1):
+ def __init__(self, child):
+ dtype = child.find_dtype()
+ self.item_size = dtype.itemtype.get_element_size()
+ self.s = StringBuilder(child.size * self.item_size)
+ Call1.__init__(self, None, 'tostring', child.shape, dtype, dtype,
+ child)
+ self.res = W_NDimArray([1], dtype, 'C')
+ self.res_casted = rffi.cast(rffi.CArrayPtr(lltype.Char),
+ self.res.storage)
+
+ def create_sig(self):
+ return signature.ToStringSignature(self.calc_dtype,
+ self.values.create_sig())
+
def done_if_true(dtype, val):
return dtype.itemtype.bool(val)
@@ -856,8 +882,6 @@
self.right.create_sig(), done_func)
class AxisReduce(Call2):
- _immutable_fields_ = ['left', 'right']
-
def __init__(self, ufunc, name, identity, shape, dtype, left, right, dim):
Call2.__init__(self, ufunc, name, shape, dtype, dtype,
left, right)
@@ -897,13 +921,13 @@
"""
_immutable_fields_ = ['storage']
- def __init__(self, size, shape, dtype, order='C', parent=None):
- self.size = size
+ def __init__(self, shape, dtype, order='C', parent=None):
self.parent = parent
+ self.size = support.product(shape) * dtype.get_size()
if parent is not None:
self.storage = parent.storage
else:
- self.storage = dtype.malloc(size)
+ self.storage = dtype.itemtype.malloc(self.size)
self.order = order
self.dtype = dtype
More information about the pypy-commit
mailing list