[pypy-commit] pypy py3k: hg merge default
amauryfa
noreply at buildbot.pypy.org
Sat Jun 29 18:29:50 CEST 2013
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r65099:1b957582573c
Date: 2013-06-29 16:38 +0200
http://bitbucket.org/pypy/pypy/changeset/1b957582573c/
Log: hg merge default
diff too long, truncating to 2000 out of 2591 lines
diff --git a/lib_pypy/ctypes_config_cache/syslog.ctc.py b/lib_pypy/ctypes_config_cache/syslog.ctc.py
deleted file mode 100644
--- a/lib_pypy/ctypes_config_cache/syslog.ctc.py
+++ /dev/null
@@ -1,75 +0,0 @@
-"""
-'ctypes_configure' source for syslog.py.
-Run this to rebuild _syslog_cache.py.
-"""
-
-from ctypes_configure.configure import (configure,
- ExternalCompilationInfo, ConstantInteger, DefinedConstantInteger)
-import dumpcache
-
-
-_CONSTANTS = (
- 'LOG_EMERG',
- 'LOG_ALERT',
- 'LOG_CRIT',
- 'LOG_ERR',
- 'LOG_WARNING',
- 'LOG_NOTICE',
- 'LOG_INFO',
- 'LOG_DEBUG',
-
- 'LOG_PID',
- 'LOG_CONS',
- 'LOG_NDELAY',
-
- 'LOG_KERN',
- 'LOG_USER',
- 'LOG_MAIL',
- 'LOG_DAEMON',
- 'LOG_AUTH',
- 'LOG_LPR',
- 'LOG_LOCAL0',
- 'LOG_LOCAL1',
- 'LOG_LOCAL2',
- 'LOG_LOCAL3',
- 'LOG_LOCAL4',
- 'LOG_LOCAL5',
- 'LOG_LOCAL6',
- 'LOG_LOCAL7',
-)
-_OPTIONAL_CONSTANTS = (
- 'LOG_NOWAIT',
- 'LOG_PERROR',
-
- 'LOG_SYSLOG',
- 'LOG_CRON',
- 'LOG_UUCP',
- 'LOG_NEWS',
-)
-
-# Constant aliases if there are not defined
-_ALIAS = (
- ('LOG_SYSLOG', 'LOG_DAEMON'),
- ('LOG_CRON', 'LOG_DAEMON'),
- ('LOG_NEWS', 'LOG_MAIL'),
- ('LOG_UUCP', 'LOG_MAIL'),
-)
-
-class SyslogConfigure:
- _compilation_info_ = ExternalCompilationInfo(includes=['sys/syslog.h'])
-for key in _CONSTANTS:
- setattr(SyslogConfigure, key, ConstantInteger(key))
-for key in _OPTIONAL_CONSTANTS:
- setattr(SyslogConfigure, key, DefinedConstantInteger(key))
-
-config = configure(SyslogConfigure)
-for key in _OPTIONAL_CONSTANTS:
- if config[key] is None:
- del config[key]
-for alias, key in _ALIAS:
- config.setdefault(alias, config[key])
-
-all_constants = config.keys()
-all_constants.sort()
-config['ALL_CONSTANTS'] = tuple(all_constants)
-dumpcache.dumpcache2('syslog', config)
diff --git a/lib_pypy/greenlet.py b/lib_pypy/greenlet.py
--- a/lib_pypy/greenlet.py
+++ b/lib_pypy/greenlet.py
@@ -46,16 +46,16 @@
if parent is not None:
self.parent = parent
- def switch(self, *args):
+ def switch(self, *args, **kwds):
"Switch execution to this greenlet, optionally passing the values "
"given as argument(s). Returns the value passed when switching back."
- return self.__switch('switch', args)
+ return self.__switch('switch', (args, kwds))
def throw(self, typ=GreenletExit, val=None, tb=None):
"raise exception in greenlet, return value passed when switching back"
return self.__switch('throw', typ, val, tb)
- def __switch(target, methodname, *args):
+ def __switch(target, methodname, *baseargs):
current = getcurrent()
#
while not (target.__main or _continulet.is_pending(target)):
@@ -65,9 +65,9 @@
greenlet_func = _greenlet_start
else:
greenlet_func = _greenlet_throw
- _continulet.__init__(target, greenlet_func, *args)
+ _continulet.__init__(target, greenlet_func, *baseargs)
methodname = 'switch'
- args = ()
+ baseargs = ()
target.__started = True
break
# already done, go to the parent instead
@@ -78,11 +78,15 @@
#
try:
unbound_method = getattr(_continulet, methodname)
- args = unbound_method(current, *args, to=target)
+ args, kwds = unbound_method(current, *baseargs, to=target)
finally:
_tls.current = current
#
- if len(args) == 1:
+ if kwds:
+ if args:
+ return args, kwds
+ return kwds
+ elif len(args) == 1:
return args[0]
else:
return args
@@ -128,15 +132,15 @@
_tls.main = gmain
_tls.current = gmain
-def _greenlet_start(greenlet, args):
+def _greenlet_start(greenlet, (args, kwds)):
_tls.current = greenlet
try:
- res = greenlet.run(*args)
+ res = greenlet.run(*args, **kwds)
except GreenletExit as e:
res = e
finally:
_continuation.permute(greenlet, greenlet.parent)
- return (res,)
+ return ((res,), None)
def _greenlet_throw(greenlet, exc, value, tb):
_tls.current = greenlet
diff --git a/lib_pypy/grp.py b/lib_pypy/grp.py
--- a/lib_pypy/grp.py
+++ b/lib_pypy/grp.py
@@ -9,6 +9,7 @@
from ctypes import Structure, c_char_p, c_int, POINTER
from ctypes_support import standard_c_lib as libc
+import _structseq
try: from __pypy__ import builtinify
except ImportError: builtinify = lambda f: f
@@ -24,32 +25,13 @@
('gr_mem', POINTER(c_char_p)),
)
-class Group(object):
- def __init__(self, gr_name, gr_passwd, gr_gid, gr_mem):
- self.gr_name = gr_name
- self.gr_passwd = gr_passwd
- self.gr_gid = gr_gid
- self.gr_mem = gr_mem
+class struct_group:
+ __metaclass__ = _structseq.structseqtype
- def __getitem__(self, item):
- if item == 0:
- return self.gr_name
- elif item == 1:
- return self.gr_passwd
- elif item == 2:
- return self.gr_gid
- elif item == 3:
- return self.gr_mem
- else:
- raise IndexError(item)
-
- def __len__(self):
- return 4
-
- def __repr__(self):
- return str((self.gr_name, self.gr_passwd, self.gr_gid, self.gr_mem))
-
- # whatever else...
+ gr_name = _structseq.structseqfield(0)
+ gr_passwd = _structseq.structseqfield(1)
+ gr_gid = _structseq.structseqfield(2)
+ gr_mem = _structseq.structseqfield(3)
libc.getgrgid.argtypes = [gid_t]
libc.getgrgid.restype = POINTER(GroupStruct)
@@ -72,10 +54,10 @@
while res.contents.gr_mem[i]:
mem.append(res.contents.gr_mem[i])
i += 1
- return Group(os.fsdecode(res.contents.gr_name),
- os.fsdecode(res.contents.gr_passwd),
- res.contents.gr_gid,
- mem)
+ return struct_group((os.fsdecode(res.contents.gr_name),
+ os.fsdecode(res.contents.gr_passwd),
+ res.contents.gr_gid,
+ mem))
@builtinify
def getgrgid(gid):
diff --git a/lib_pypy/pyrepl/curses.py b/lib_pypy/pyrepl/curses.py
--- a/lib_pypy/pyrepl/curses.py
+++ b/lib_pypy/pyrepl/curses.py
@@ -19,11 +19,15 @@
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-# avoid importing the whole curses, if possible
-try:
+# If we are running on top of pypy, we import only _minimal_curses.
+# Don't try to fall back to _curses, because that's going to use cffi
+# and fall again more loudly.
+import sys
+if '__pypy__' in sys.builtin_module_names:
# pypy case
import _minimal_curses as _curses
-except ImportError:
+else:
+ # cpython case
try:
import _curses
except ImportError:
diff --git a/lib_pypy/syslog.py b/lib_pypy/syslog.py
--- a/lib_pypy/syslog.py
+++ b/lib_pypy/syslog.py
@@ -1,3 +1,4 @@
+# this cffi version was rewritten based on the
# ctypes implementation: Victor Stinner, 2008-05-08
"""
This module provides an interface to the Unix syslog library routines.
@@ -9,34 +10,84 @@
if sys.platform == 'win32':
raise ImportError("No syslog on Windows")
-# load the platform-specific cache made by running syslog.ctc.py
-from ctypes_config_cache._syslog_cache import *
-
-from ctypes_support import standard_c_lib as libc
-from ctypes import c_int, c_char_p
+from cffi import FFI
try: from __pypy__ import builtinify
except ImportError: builtinify = lambda f: f
+ffi = FFI()
-# Real prototype is:
-# void syslog(int priority, const char *format, ...);
-# But we also need format ("%s") and one format argument (message)
-_syslog = libc.syslog
-_syslog.argtypes = (c_int, c_char_p, c_char_p)
-_syslog.restype = None
+ffi.cdef("""
+/* mandatory constants */
+#define LOG_EMERG ...
+#define LOG_ALERT ...
+#define LOG_CRIT ...
+#define LOG_ERR ...
+#define LOG_WARNING ...
+#define LOG_NOTICE ...
+#define LOG_INFO ...
+#define LOG_DEBUG ...
-_openlog = libc.openlog
-_openlog.argtypes = (c_char_p, c_int, c_int)
-_openlog.restype = None
+#define LOG_PID ...
+#define LOG_CONS ...
+#define LOG_NDELAY ...
-_closelog = libc.closelog
-_closelog.argtypes = None
-_closelog.restype = None
+#define LOG_KERN ...
+#define LOG_USER ...
+#define LOG_MAIL ...
+#define LOG_DAEMON ...
+#define LOG_AUTH ...
+#define LOG_LPR ...
+#define LOG_LOCAL0 ...
+#define LOG_LOCAL1 ...
+#define LOG_LOCAL2 ...
+#define LOG_LOCAL3 ...
+#define LOG_LOCAL4 ...
+#define LOG_LOCAL5 ...
+#define LOG_LOCAL6 ...
+#define LOG_LOCAL7 ...
-_setlogmask = libc.setlogmask
-_setlogmask.argtypes = (c_int,)
-_setlogmask.restype = c_int
+/* optional constants, gets defined to -919919 if missing */
+#define LOG_NOWAIT ...
+#define LOG_PERROR ...
+
+/* aliased constants, gets defined as some other constant if missing */
+#define LOG_SYSLOG ...
+#define LOG_CRON ...
+#define LOG_UUCP ...
+#define LOG_NEWS ...
+
+/* functions */
+void openlog(const char *ident, int option, int facility);
+void syslog(int priority, const char *format, const char *string);
+// NB. the signature of syslog() is specialized to the only case we use
+void closelog(void);
+int setlogmask(int mask);
+""")
+
+lib = ffi.verify("""
+#include <syslog.h>
+
+#ifndef LOG_NOWAIT
+#define LOG_NOWAIT -919919
+#endif
+#ifndef LOG_PERROR
+#define LOG_PERROR -919919
+#endif
+#ifndef LOG_SYSLOG
+#define LOG_SYSLOG LOG_DAEMON
+#endif
+#ifndef LOG_CRON
+#define LOG_CRON LOG_DAEMON
+#endif
+#ifndef LOG_UUCP
+#define LOG_UUCP LOG_MAIL
+#endif
+#ifndef LOG_NEWS
+#define LOG_NEWS LOG_MAIL
+#endif
+""")
+
_S_log_open = False
_S_ident_o = None
@@ -52,17 +103,19 @@
return None
@builtinify
-def openlog(ident=None, logoption=0, facility=LOG_USER):
+def openlog(ident=None, logoption=0, facility=lib.LOG_USER):
global _S_ident_o, _S_log_open
if ident is None:
ident = _get_argv()
- if ident is not None:
+ if ident is None:
+ _S_ident_o = ffi.NULL
+ else:
if not isinstance(ident, str):
msg = "openlog() argument 1 must be a str, not {!r}"
raise TypeError(msg.format(type(ident).__name__))
ident = ident.encode(sys.getdefaultencoding())
- _S_ident_o = c_char_p(ident) # keepalive
- _openlog(_S_ident_o, logoption, facility)
+ _S_ident_o = ffi.new("char[]", ident) # keepalive
+ lib.openlog(_S_ident_o, logoption, facility)
_S_log_open = True
@builtinify
@@ -78,19 +131,19 @@
raise TypeError("syslog() message must be a str, not {!r}".format(
type(message).__name__))
message = message.encode(sys.getdefaultencoding())
- _syslog(priority, b"%s", message)
+ lib.syslog(priority, b"%s", message)
@builtinify
def closelog():
global _S_log_open, S_ident_o
if _S_log_open:
- _closelog()
+ lib.closelog()
_S_log_open = False
_S_ident_o = None
@builtinify
def setlogmask(mask):
- return _setlogmask(mask)
+ return lib.setlogmask(mask)
@builtinify
def LOG_MASK(pri):
@@ -100,8 +153,15 @@
def LOG_UPTO(pri):
return (1 << (pri + 1)) - 1
-__all__ = ALL_CONSTANTS + (
+__all__ = []
+
+for name in sorted(lib.__dict__):
+ if name.startswith('LOG_'):
+ value = getattr(lib, name)
+ if value != -919919:
+ globals()[name] = value
+ __all__.append(name)
+
+__all__ = tuple(__all__) + (
'openlog', 'syslog', 'closelog', 'setlogmask',
'LOG_MASK', 'LOG_UPTO')
-
-del ALL_CONSTANTS
diff --git a/pypy/config/pypyoption.py b/pypy/config/pypyoption.py
--- a/pypy/config/pypyoption.py
+++ b/pypy/config/pypyoption.py
@@ -147,7 +147,7 @@
requires=module_dependencies.get(modname, []),
suggests=module_suggests.get(modname, []),
negation=modname not in essential_modules,
- validator=get_module_validator(modname))
+ ) #validator=get_module_validator(modname))
for modname in all_modules]),
BoolOption("allworkingmodules", "use as many working modules as possible",
diff --git a/pypy/doc/whatsnew-head.rst b/pypy/doc/whatsnew-head.rst
--- a/pypy/doc/whatsnew-head.rst
+++ b/pypy/doc/whatsnew-head.rst
@@ -61,3 +61,7 @@
.. branch: faster-str-of-bigint
Improve performance of str(long).
+
+.. branch: ndarray-view
+Add view to ndarray and zeroD arrays, not on dtype scalars yet
+
diff --git a/pypy/module/_csv/interp_reader.py b/pypy/module/_csv/interp_reader.py
--- a/pypy/module/_csv/interp_reader.py
+++ b/pypy/module/_csv/interp_reader.py
@@ -41,8 +41,8 @@
def save_field(self, field_builder):
field = field_builder.build()
if self.numeric_field:
- from pypy.objspace.std.strutil import ParseStringError
- from pypy.objspace.std.strutil import string_to_float
+ from rpython.rlib.rstring import ParseStringError
+ from rpython.rlib.rfloat import string_to_float
self.numeric_field = False
try:
ff = string_to_float(field)
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
@@ -293,9 +293,9 @@
_checkReadable = interp2app(check_readable_w),
_checkWritable = interp2app(check_writable_w),
_checkSeekable = interp2app(check_seekable_w),
+ _checkClosed = interp2app(W_IOBase.check_closed_w),
closed = GetSetProperty(W_IOBase.closed_get_w,
doc="True if the file is closed"),
- _checkClosed = interp2app(W_IOBase.check_closed_w),
__dict__ = GetSetProperty(descr_get_dict, descr_set_dict, cls=W_IOBase),
__weakref__ = make_weakref_descr(W_IOBase),
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
@@ -71,6 +71,7 @@
'complex_': 'interp_boxes.W_Complex128Box',
'complex128': 'interp_boxes.W_Complex128Box',
'complex64': 'interp_boxes.W_Complex64Box',
+ 'cfloat': 'interp_boxes.W_Complex64Box',
}
if ENABLED_LONG_DOUBLE:
long_double_dtypes = [
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -75,6 +75,12 @@
else:
return None
+ def get_view(self, orig_array, dtype, new_shape):
+ strides, backstrides = support.calc_strides(new_shape, dtype,
+ self.order)
+ return SliceArray(self.start, strides, backstrides, new_shape,
+ self, orig_array, dtype=dtype)
+
def get_real(self, orig_array):
strides = self.get_strides()
backstrides = self.get_backstrides()
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -63,6 +63,11 @@
def transpose(self, _):
return self
+ def get_view(self, orig_array, dtype, new_shape):
+ scalar = Scalar(dtype)
+ scalar.value = self.value.convert_to(dtype)
+ return scalar
+
def get_real(self, orig_array):
if self.dtype.is_complex_type():
scalar = Scalar(self.dtype.float_type)
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
@@ -51,6 +51,7 @@
w_IndexError = W_TypeObject("IndexError")
w_OverflowError = W_TypeObject("OverflowError")
w_NotImplementedError = W_TypeObject("NotImplementedError")
+ w_AttributeError = W_TypeObject("AttributeError")
w_None = None
w_bool = W_TypeObject("bool")
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
@@ -240,6 +240,16 @@
v = self.convert_to(self.get_dtype(space))
return self.get_dtype(space).itemtype.round(v, decimals)
+ def descr_view(self, space, w_dtype):
+ from pypy.module.micronumpy.interp_dtype import W_Dtype
+ dtype = space.interp_w(W_Dtype,
+ space.call_function(space.gettypefor(W_Dtype), w_dtype))
+ if dtype.get_size() != self.get_dtype(space).get_size():
+ raise OperationError(space.w_ValueError, space.wrap(
+ "new type not compatible with array."))
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "view not implelemnted yet"))
+
class W_BoolBox(W_GenericBox, PrimitiveBox):
descr__new__, _get_dtype, descr_reduce = new_dtype_getter("bool")
@@ -507,6 +517,7 @@
all = interp2app(W_GenericBox.descr_all),
ravel = interp2app(W_GenericBox.descr_ravel),
round = interp2app(W_GenericBox.descr_round),
+ view = interp2app(W_GenericBox.descr_view),
)
W_BoolBox.typedef = TypeDef("bool_", W_GenericBox.typedef,
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
@@ -618,8 +618,40 @@
"trace not implemented yet"))
def descr_view(self, space, w_dtype=None, w_type=None) :
- raise OperationError(space.w_NotImplementedError, space.wrap(
- "view not implemented yet"))
+ if w_type is not None:
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "view(... type=<class>) not implemented yet"))
+ if w_dtype:
+ dtype = space.interp_w(interp_dtype.W_Dtype,
+ space.call_function(space.gettypefor(interp_dtype.W_Dtype),
+ w_dtype))
+ else:
+ dtype = self.get_dtype()
+ old_itemsize = self.get_dtype().get_size()
+ new_itemsize = dtype.get_size()
+ impl = self.implementation
+ new_shape = self.get_shape()
+ dims = len(new_shape)
+ if dims == 0:
+ # Cannot resize scalars
+ if old_itemsize != new_itemsize:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "new type not compatible with array shape"))
+ else:
+ if dims == 1 or impl.get_strides()[0] < impl.get_strides()[-1]:
+ # Column-major, resize first dimension
+ if new_shape[0] * old_itemsize % new_itemsize != 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "new type not compatible with array."))
+ new_shape[0] = new_shape[0] * old_itemsize / new_itemsize
+ else:
+ # Row-major, resize last dimension
+ if new_shape[-1] * old_itemsize % new_itemsize != 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "new type not compatible with array."))
+ new_shape[-1] = new_shape[-1] * old_itemsize / new_itemsize
+ return W_NDimArray(impl.get_view(self, dtype, new_shape))
+
# --------------------- operations ----------------------------
@@ -996,6 +1028,7 @@
round = interp2app(W_NDimArray.descr_round),
data = GetSetProperty(W_NDimArray.descr_get_data),
diagonal = interp2app(W_NDimArray.descr_diagonal),
+ view = interp2app(W_NDimArray.descr_view),
ctypes = GetSetProperty(W_NDimArray.descr_get_ctypes), # XXX unimplemented
__array_interface__ = GetSetProperty(W_NDimArray.descr_array_iface),
@@ -1028,15 +1061,21 @@
order)
dtype = interp_dtype.decode_w_dtype(space, w_dtype)
- if isinstance(w_object, W_NDimArray):
- if (not space.is_none(w_dtype) and
- w_object.get_dtype() is not dtype):
- raise OperationError(space.w_NotImplementedError, space.wrap(
- "copying over different dtypes unsupported"))
+ if isinstance(w_object, W_NDimArray) and \
+ (space.is_none(w_dtype) or w_object.get_dtype() is dtype):
+ shape = w_object.get_shape()
if copy:
- return w_object.descr_copy(space)
- return w_object
-
+ w_ret = w_object.descr_copy(space)
+ else:
+ if ndmin<= len(shape):
+ return w_object
+ new_impl = w_object.implementation.set_shape(space, w_object, shape)
+ w_ret = W_NDimArray(new_impl)
+ if ndmin > len(shape):
+ shape = [1] * (ndmin - len(shape)) + shape
+ w_ret.implementation = w_ret.implementation.set_shape(space,
+ w_ret, shape)
+ return w_ret
shape, elems_w = find_shape_and_elems(space, w_object, dtype)
if dtype is None or (
dtype.is_str_or_unicode() and dtype.itemtype.get_size() < 1):
diff --git a/pypy/module/micronumpy/interp_support.py b/pypy/module/micronumpy/interp_support.py
--- a/pypy/module/micronumpy/interp_support.py
+++ b/pypy/module/micronumpy/interp_support.py
@@ -2,7 +2,7 @@
from pypy.interpreter.gateway import unwrap_spec, WrappedDefault
from rpython.rtyper.lltypesystem import lltype, rffi
from pypy.module.micronumpy import interp_dtype, loop
-from pypy.objspace.std.strutil import strip_spaces
+from rpython.rlib.rstring import strip_spaces
from rpython.rlib.rarithmetic import maxint
from pypy.module.micronumpy.base import W_NDimArray
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -584,6 +584,7 @@
import numpypy as numpy
assert numpy.complex_ is numpy.complex128
+ assert numpy.cfloat is numpy.complex64
assert numpy.complex64.__mro__ == (numpy.complex64,
numpy.complexfloating, numpy.inexact, numpy.number, numpy.generic,
object)
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -276,6 +276,24 @@
arr = array([1], ndmin=3)
assert arr.shape == (1, 1, 1)
+ def test_array_copy(self):
+ from numpypy import array
+ a = array(range(12)).reshape(3,4)
+ b = array(a, ndmin=4)
+ assert b.shape == (1, 1, 3, 4)
+ b = array(a, copy=False)
+ b[0, 0] = 100
+ assert a[0, 0] == 100
+ b = array(a, copy=True, ndmin=2)
+ b[0, 0] = 0
+ assert a[0, 0] == 100
+ b = array(a, dtype=float)
+ assert (b[0] == [100, 1, 2, 3]).all()
+ assert b.dtype.kind == 'f'
+ b = array(a, copy=False, ndmin=4)
+ b[0,0,0,0] = 0
+ assert a[0, 0] == 0
+
def test_type(self):
from numpypy import array
ar = array(range(5))
@@ -325,13 +343,16 @@
assert a[1] == 1.0
def test_ones(self):
- from numpypy import ones
+ from numpypy import ones, dtype
a = ones(3)
assert len(a) == 3
assert a[0] == 1
raises(IndexError, "a[3]")
a[2] = 4
assert a[2] == 4
+ b = ones(3, complex)
+ assert b[0] == 1+0j
+ assert b.dtype is dtype(complex)
def test_copy(self):
from numpypy import arange, array
@@ -1390,6 +1411,44 @@
assert a[3].imag == -10
assert a[2].imag == -5
+ def test_ndarray_view(self):
+ from numpypy import array, int8, int16, dtype
+ x = array([(1, 2)], dtype=[('a', int8), ('b', int8)])
+ y = x.view(dtype=int16)
+ print y,y.shape
+ assert y[0] == 513
+ assert y.dtype == dtype('int16')
+ y[0] = 670
+ assert x['a'] == -98
+ assert x['b'] == 2
+ f = array([1000, -1234], dtype='i4')
+ nnp = self.non_native_prefix
+ d = f.view(dtype=nnp + 'i4')
+ assert (d == [-402456576, 788267007]).all()
+ x = array(range(15), dtype='i2').reshape(3,5)
+ exc = raises(ValueError, x.view, dtype='i4')
+ assert exc.value[0] == "new type not compatible with array."
+ assert x.view('int8').shape == (3, 10)
+ x = array(range(15), dtype='int16').reshape(3,5).T
+ assert x.view('int8').shape == (10, 3)
+
+ def test_ndarray_view_empty(self):
+ from numpypy import array, int8, int16, dtype
+ x = array([], dtype=[('a', int8), ('b', int8)])
+ y = x.view(dtype=int16)
+
+ def test_scalar_view(self):
+ from numpypy import int64, array
+ a = array(0, dtype='int32')
+ b = a.view(dtype='float32')
+ assert b.shape == ()
+ assert b == 0
+ s = int64(12)
+ exc = raises(ValueError, s.view, 'int8')
+ assert exc.value[0] == "new type not compatible with array."
+ skip('not implemented yet')
+ assert s.view('double') < 7e-323
+
def test_tolist_scalar(self):
from numpypy import int32, bool_
x = int32(23)
@@ -2176,6 +2235,10 @@
d.fill(100)
assert d == 100
+ e = array(10, dtype=complex)
+ e.fill(1.5-3j)
+ assert e == 1.5-3j
+
def test_array_indexing_bool(self):
from numpypy import arange
a = arange(10)
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -1100,6 +1100,13 @@
op = '+' if imag >= 0 else ''
return ''.join(['(', real_str, op, imag_str, ')'])
+ def fill(self, storage, width, box, start, stop, offset):
+ real, imag = self.unbox(box)
+ for i in xrange(start, stop, width):
+ raw_storage_setitem(storage, i+offset, real)
+ raw_storage_setitem(storage,
+ i+offset+rffi.sizeof(self.T), imag)
+
@staticmethod
def for_computation(v):
return float(v[0]), float(v[1])
diff --git a/pypy/module/pypyjit/test_pypy_c/test_string.py b/pypy/module/pypyjit/test_pypy_c/test_string.py
--- a/pypy/module/pypyjit/test_pypy_c/test_string.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_string.py
@@ -80,39 +80,11 @@
i23 = strgetitem(p10, i19)
p25 = newstr(1)
strsetitem(p25, 0, i23)
- p28 = call(ConstClass(strip_spaces), p25, descr=<Callr . r EF=4>)
+ p93 = call(ConstClass(fromstr), p25, 16, ConstPtr(ptr70), descr=<Callr . rir EF=3>)
guard_no_exception(descr=...)
- i29 = strlen(p28)
- i30 = int_is_true(i29)
- guard_true(i30, descr=...)
- i32 = int_sub(i29, 1)
- i33 = strgetitem(p28, i32)
- i35 = int_eq(i33, 108)
- guard_false(i35, descr=...)
- i37 = int_eq(i33, 76)
- guard_false(i37, descr=...)
- i39 = strgetitem(p28, 0)
- i41 = int_eq(i39, 45)
- guard_false(i41, descr=...)
- i43 = int_eq(i39, 43)
- guard_false(i43, descr=...)
- i43 = call(ConstClass(ll_startswith__rpy_stringPtr_rpy_stringPtr), p28, ConstPtr(ptr42), descr=<Calli 1 rr EF=0>)
- guard_false(i43, descr=...)
- i46 = call(ConstClass(ll_startswith__rpy_stringPtr_rpy_stringPtr), p28, ConstPtr(ptr45), descr=<Calli 1 rr EF=0>)
- guard_false(i46, descr=...)
- p51 = new_with_vtable(...)
- setfield_gc(p51, _, descr=...) # 7 setfields, but the order is dict-order-dependent
- setfield_gc(p51, _, descr=...)
- setfield_gc(p51, _, descr=...)
- setfield_gc(p51, _, descr=...)
- setfield_gc(p51, _, descr=...)
- setfield_gc(p51, _, descr=...)
- setfield_gc(p51, _, descr=...)
- p55 = call(ConstClass(parse_digit_string), p51, descr=<Callr . r EF=4>)
+ i94 = call(ConstClass(rbigint.toint), p93, descr=<Calli . r EF=3>)
guard_no_exception(descr=...)
- i57 = call(ConstClass(rbigint.toint), p55, descr=<Calli . r EF=3>)
- guard_no_exception(descr=...)
- i58 = int_add_ovf(i6, i57)
+ i95 = int_add_ovf(i6, i94)
guard_no_overflow(descr=...)
--TICK--
jump(..., descr=...)
diff --git a/pypy/module/test_lib_pypy/test_greenlet.py b/pypy/module/test_lib_pypy/test_greenlet.py
--- a/pypy/module/test_lib_pypy/test_greenlet.py
+++ b/pypy/module/test_lib_pypy/test_greenlet.py
@@ -319,3 +319,25 @@
g = G(lambda: 42)
x = g.switch()
assert x == 42
+
+ def test_kwargs_to_f(self):
+ import greenlet
+ seen = []
+ def f(*args, **kwds):
+ seen.append([args, kwds])
+ g = greenlet.greenlet(f)
+ g.switch(1, 2, x=3, y=4)
+ assert seen == [[(1, 2), {'x': 3, 'y': 4}]]
+
+ def test_kwargs_to_switch(self):
+ import greenlet
+ main = greenlet.getcurrent()
+ assert main.switch() == ()
+ assert main.switch(5) == 5
+ assert main.switch(5, 6) == (5, 6)
+ #
+ assert main.switch(x=5) == {'x': 5}
+ assert main.switch(x=5, y=6) == {'x': 5, 'y': 6}
+ assert main.switch(3, x=5) == ((3,), {'x': 5})
+ assert main.switch(3, x=5, y=6) == ((3,), {'x': 5, 'y': 6})
+ assert main.switch(2, 3, x=6) == ((2, 3), {'x': 6})
diff --git a/pypy/module/test_lib_pypy/test_grp_extra.py b/pypy/module/test_lib_pypy/test_grp_extra.py
--- a/pypy/module/test_lib_pypy/test_grp_extra.py
+++ b/pypy/module/test_lib_pypy/test_grp_extra.py
@@ -4,6 +4,22 @@
except ImportError:
py.test.skip("No grp module on this platform")
+def test_basic():
+ g = grp.getgrnam("root")
+ assert g.gr_gid == 0
+ assert g.gr_mem == ['root']
+ assert g.gr_name == 'root'
+ assert isinstance(g.gr_passwd, str) # usually just 'x', don't hope :-)
+
def test_extra():
py.test.raises(TypeError, grp.getgrnam, False)
py.test.raises(TypeError, grp.getgrnam, None)
+
+def test_struct_group():
+ g = grp.struct_group((10, 20, 30, 40))
+ assert len(g) == 4
+ assert list(g) == [10, 20, 30, 40]
+ assert g.gr_name == 10
+ assert g.gr_passwd == 20
+ assert g.gr_gid == 30
+ assert g.gr_mem == 40
diff --git a/pypy/module/test_lib_pypy/test_syslog.py b/pypy/module/test_lib_pypy/test_syslog.py
--- a/pypy/module/test_lib_pypy/test_syslog.py
+++ b/pypy/module/test_lib_pypy/test_syslog.py
@@ -6,9 +6,5 @@
# XXX very minimal test
-from lib_pypy.ctypes_config_cache import rebuild
-rebuild.rebuild_one('syslog.ctc.py')
-
-
def test_syslog():
assert hasattr(syslog, 'LOG_ALERT')
diff --git a/pypy/objspace/std/complextype.py b/pypy/objspace/std/complextype.py
--- a/pypy/objspace/std/complextype.py
+++ b/pypy/objspace/std/complextype.py
@@ -2,11 +2,12 @@
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.strutil import string_to_float, ParseStringError
from pypy.objspace.std.noneobject import W_NoneObject
from pypy.objspace.std.stdtypedef import GetSetProperty, StdTypeDef
from pypy.objspace.std.stdtypedef import StdObjSpaceMultiMethod
from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
+from rpython.rlib.rfloat import string_to_float
+from rpython.rlib.rstring import ParseStringError
# ERRORCODES
diff --git a/pypy/objspace/std/floattype.py b/pypy/objspace/std/floattype.py
--- a/pypy/objspace/std/floattype.py
+++ b/pypy/objspace/std/floattype.py
@@ -8,10 +8,9 @@
from pypy.interpreter.error import OperationError
from pypy.objspace.std.register_all import register_all
from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.objspace.std.strutil import ParseStringError
-from pypy.objspace.std.strutil import string_to_float
from pypy.objspace.std.model import W_Object
from rpython.rlib.rbigint import rbigint
+from rpython.rlib.rstring import ParseStringError
float_as_integer_ratio = SMM("as_integer_ratio", 1)
@@ -41,7 +40,7 @@
space.isinstance_w(w_value, space.w_bytearray)):
strvalue = space.bufferstr_w(w_value)
try:
- value = string_to_float(strvalue.decode('latin-1'))
+ value = rfloat.string_to_float(strvalue.decode('latin-1'))
except ParseStringError, e:
raise OperationError(space.w_ValueError,
space.wrap(e.msg))
@@ -49,7 +48,7 @@
from unicodeobject import unicode_to_decimal_w
strvalue = unicode_to_decimal_w(space, w_value)
try:
- value = string_to_float(strvalue)
+ value = rfloat.string_to_float(strvalue)
except ParseStringError, e:
raise OperationError(space.w_ValueError,
space.wrap(e.msg))
diff --git a/pypy/objspace/std/inttype.py b/pypy/objspace/std/inttype.py
--- a/pypy/objspace/std/inttype.py
+++ b/pypy/objspace/std/inttype.py
@@ -5,13 +5,12 @@
from pypy.interpreter.buffer import Buffer
from pypy.objspace.std.register_all import register_all
from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.objspace.std.strutil import (string_to_int, string_to_bigint,
- ParseStringError,
- ParseStringOverflowError)
from pypy.objspace.std.model import W_Object
-from rpython.rlib.rarithmetic import r_uint
+from rpython.rlib.rarithmetic import r_uint, string_to_int
from rpython.rlib.objectmodel import instantiate
from rpython.rlib.rbigint import rbigint
+from rpython.rlib.rstring import ParseStringError, ParseStringOverflowError
+from rpython.rlib import jit
# ____________________________________________________________
@@ -70,6 +69,7 @@
# ____________________________________________________________
+## @jit.elidable
## def string_to_int_or_long(space, string, base=10):
## w_longval = None
## value = 0
@@ -82,15 +82,14 @@
## w_longval = retry_to_w_long(space, e.parser)
## return value, w_longval
-## def retry_to_w_long(space, parser, base=0):
+## def retry_to_w_long(space, parser):
## parser.rewind()
## try:
-## bigint = string_to_bigint(None, base=base, parser=parser)
+## bigint = rbigint._from_numberstring_parser(parser)
## except ParseStringError, e:
## raise OperationError(space.w_ValueError,
## space.wrap(e.msg))
-## from pypy.objspace.std.longobject import newlong
-## return newlong(space, bigint)
+## return space.newlong_from_rbigint(bigint)
## @unwrap_spec(w_x = WrappedDefault(0))
## def descr__new__(space, w_inttype, w_x, w_base=None):
diff --git a/pypy/objspace/std/longtype.py b/pypy/objspace/std/longtype.py
--- a/pypy/objspace/std/longtype.py
+++ b/pypy/objspace/std/longtype.py
@@ -3,8 +3,8 @@
from pypy.interpreter.gateway import (
WrappedDefault, applevel, interp2app, interpindirect2app, unwrap_spec)
from pypy.objspace.std.model import W_Object
-from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.objspace.std.strutil import string_to_bigint, ParseStringError
+from pypy.objspace.std.stdtypedef import StdTypeDef
+from rpython.rlib.rstring import ParseStringError
from rpython.rlib.rbigint import rbigint, InvalidEndiannessError, InvalidSignednessError
def descr_conjugate(space, w_int):
@@ -71,7 +71,7 @@
def string_to_w_long(space, w_longtype, s, base=10):
try:
- bigint = string_to_bigint(s, base)
+ bigint = rbigint.fromstr(s, base)
except ParseStringError, e:
raise OperationError(space.w_ValueError,
space.wrap(e.msg))
diff --git a/pypy/objspace/std/strutil.py b/pypy/objspace/std/strutil.py
deleted file mode 100644
--- a/pypy/objspace/std/strutil.py
+++ /dev/null
@@ -1,212 +0,0 @@
-"""
-Pure Python implementation of string utilities.
-"""
-
-from rpython.tool.sourcetools import with_unicode_literals
-from rpython.rlib.objectmodel import enforceargs
-from rpython.rlib.rarithmetic import ovfcheck
-from rpython.rlib.rfloat import rstring_to_float, INFINITY, NAN
-from rpython.rlib.rbigint import rbigint, parse_digit_string
-from pypy.interpreter.error import OperationError
-import math
-
-# XXX factor more functions out of stringobject.py.
-# This module is independent from PyPy.
-
- at enforceargs(unicode)
- at with_unicode_literals
-def strip_spaces(s):
- # XXX this is not locale-dependent
- p = 0
- q = len(s)
- while p < q and s[p] in ' \f\n\r\t\v':
- p += 1
- while p < q and s[q-1] in ' \f\n\r\t\v':
- q -= 1
- assert q >= p # annotator hint, don't remove
- return s[p:q]
-
-class ParseStringError(Exception):
- @enforceargs(None, unicode)
- def __init__(self, msg):
- self.msg = msg
-
-class ParseStringOverflowError(Exception):
- def __init__(self, parser):
- self.parser = parser
-
-# iterator-like class
-class NumberStringParser:
-
- def error(self):
- raise ParseStringError(u"invalid literal for %s() with base %d: '%s'" %
- (self.fname, self.original_base, self.literal))
-
- @enforceargs(None, unicode, unicode, int, unicode)
- @with_unicode_literals
- def __init__(self, s, literal, base, fname):
- self.literal = literal
- self.fname = fname
- sign = 1
- if s.startswith('-'):
- sign = -1
- s = strip_spaces(s[1:])
- elif s.startswith('+'):
- s = strip_spaces(s[1:])
- self.sign = sign
- self.original_base = base
-
- if base == 0:
- if s.startswith('0x') or s.startswith('0X'):
- base = 16
- elif s.startswith('0b') or s.startswith('0B'):
- base = 2
- elif s.startswith('0'): # also covers the '0o' case
- base = 8
- else:
- base = 10
- elif base < 2 or base > 36:
- raise ParseStringError, u"%s() base must be >= 2 and <= 36" % (fname,)
- self.base = base
-
- if base == 16 and (s.startswith('0x') or s.startswith('0X')):
- s = s[2:]
- if base == 8 and (s.startswith('0o') or s.startswith('0O')):
- s = s[2:]
- if base == 2 and (s.startswith('0b') or s.startswith('0B')):
- s = s[2:]
- if not s:
- self.error()
- self.s = s
- self.n = len(s)
- self.i = 0
-
- def rewind(self):
- self.i = 0
-
- @with_unicode_literals
- def next_digit(self): # -1 => exhausted
- if self.i < self.n:
- c = self.s[self.i]
- digit = ord(c)
- if '0' <= c <= '9':
- digit -= ord('0')
- elif 'A' <= c <= 'Z':
- digit = (digit - ord('A')) + 10
- elif 'a' <= c <= 'z':
- digit = (digit - ord('a')) + 10
- else:
- self.error()
- if digit >= self.base:
- self.error()
- self.i += 1
- return digit
- else:
- return -1
-
- at enforceargs(unicode, None)
-def string_to_int(s, base=10):
- """Utility to converts a string to an integer.
- If base is 0, the proper base is guessed based on the leading
- characters of 's'. Raises ParseStringError in case of error.
- Raises ParseStringOverflowError in case the result does not fit.
- """
- s = literal = strip_spaces(s)
- p = NumberStringParser(s, literal, base, u'int')
- base = p.base
- result = 0
- while True:
- digit = p.next_digit()
- if digit == -1:
- return result
-
- if p.sign == -1:
- digit = -digit
-
- try:
- result = ovfcheck(result * base)
- result = ovfcheck(result + digit)
- except OverflowError:
- raise ParseStringOverflowError(p)
-
- at enforceargs(unicode, None, None)
-def string_to_bigint(s, base=10, parser=None):
- """As string_to_int() but returns an rbigint."""
- if parser is None:
- s = literal = strip_spaces(s)
- p = NumberStringParser(s, literal, base, u'int')
- else:
- p = parser
- return parse_digit_string(p)
-
-# Tim's comment:
-# 57 bits are more than needed in any case.
-# to allow for some rounding, we take one
-# digit more.
-
-# In the PyPy case, we can compute everything at compile time:
-# XXX move this stuff to some central place, it is now also
-# in _float_formatting.
-
-def calc_mantissa_bits():
- bits = 1 # I know it is almost always 53, but let it compute...
- while 1:
- pattern = (1L << bits) - 1
- comp = long(float(pattern))
- if comp != pattern:
- return bits - 1
- bits += 1
-
-MANTISSA_BITS = calc_mantissa_bits()
-del calc_mantissa_bits
-MANTISSA_DIGITS = len(str( (1L << MANTISSA_BITS)-1 )) + 1
-
- at enforceargs(unicode)
-def string_to_float(s):
- """
- Conversion of string to float.
- This version tries to only raise on invalid literals.
- Overflows should be converted to infinity whenever possible.
-
- Expects an unwrapped string and return an unwrapped float.
- """
-
- s = strip_spaces(s)
-
- if not s:
- raise ParseStringError(u"empty string for float()")
-
-
- try:
- ascii_s = s.encode('ascii')
- except UnicodeEncodeError:
- # if s is not ASCII, it certainly is not a float literal (because the
- # unicode-decimal to ascii-decimal conversion already happened
- # earlier). We just set ascii_s to something which will fail when
- # passed to rstring_to_float, to keep the code as similar as possible
- # to the one we have on default.
- #
- # Note that CPython does something different and it encodes the string
- # to UTF-8 before trying to parse it. We cannot since .encode('utf-8')
- # is not RPython. However, it doesn't change anything since the UTF-8
- # encoded string would make rstring_to_float to fail anyway.
- ascii_s = "not a float"
- else:
- low = ascii_s.lower()
- if low == "-inf" or low == "-infinity":
- return -INFINITY
- elif low == "inf" or low == "+inf":
- return INFINITY
- elif low == "infinity" or low == "+infinity":
- return INFINITY
- elif low == "nan" or low == "+nan":
- return NAN
- elif low == "-nan":
- return -NAN
-
- try:
- return rstring_to_float(ascii_s)
- except ValueError:
- # note that we still put the original unicode string in the error
- # message, not ascii_s
- raise ParseStringError(u"invalid literal for float(): '%s'" % s)
diff --git a/pypy/objspace/std/test/test_strutil.py b/pypy/objspace/std/test/test_strutil.py
deleted file mode 100644
--- a/pypy/objspace/std/test/test_strutil.py
+++ /dev/null
@@ -1,193 +0,0 @@
-# in default string_to_int accepts str, in py3k it accepts unicode. We use
-# __future__.unicode_literals here to minimize the diff
-from __future__ import unicode_literals
-
-import py, random
-from pypy.objspace.std.strutil import *
-from pypy.interpreter.error import OperationError
-
-
-class TestStrUtil:
-
- def test_string_to_int(self):
- space = self.space
- cases = [('0', 0),
- ('1', 1),
- ('9', 9),
- ('10', 10),
- ('09', 9),
- ('0000101', 101), # not octal unless base 0 or 8
- ('5123', 5123),
- (' 0', 0),
- ('0 ', 0),
- (' \t \n 32313 \f \v \r \n\r ', 32313),
- ('+12', 12),
- ('-5', -5),
- ('- 5', -5),
- ('+ 5', 5),
- (' -123456789 ', -123456789),
- ]
- for s, expected in cases:
- assert string_to_int(s) == expected
- assert string_to_bigint(s).tolong() == expected
-
- def test_string_to_int_base(self):
- space = self.space
- cases = [('111', 2, 7),
- ('010', 2, 2),
- ('102', 3, 11),
- ('103', 4, 19),
- ('107', 8, 71),
- ('109', 10, 109),
- ('10A', 11, 131),
- ('10a', 11, 131),
- ('10f', 16, 271),
- ('10F', 16, 271),
- ('0x10f', 16, 271),
- ('0x10F', 16, 271),
- ('10z', 36, 1331),
- ('10Z', 36, 1331),
- ('12', 0, 12),
- ('015', 0, 13),
- ('0x10', 0, 16),
- ('0XE', 0, 14),
- ('0', 0, 0),
- ('0b11', 2, 3),
- ('0B10', 2, 2),
- ('0o77', 8, 63),
- ]
- for s, base, expected in cases:
- assert string_to_int(s, base) == expected
- assert string_to_int('+'+s, base) == expected
- assert string_to_int('-'+s, base) == -expected
- assert string_to_int(s+'\n', base) == expected
- assert string_to_int(' +'+s, base) == expected
- assert string_to_int('-'+s+' ', base) == -expected
-
- def test_string_to_int_error(self):
- space = self.space
- cases = ['0x123', # must use base 0 or 16
- ' 0X12 ',
- '0b01',
- '0o01',
- '',
- '++12',
- '+-12',
- '-+12',
- '--12',
- '12a6',
- '12A6',
- 'f',
- 'Z',
- '.',
- '@',
- ]
- for s in cases:
- raises(ParseStringError, string_to_int, s)
- raises(ParseStringError, string_to_int, ' '+s)
- raises(ParseStringError, string_to_int, s+' ')
- raises(ParseStringError, string_to_int, '+'+s)
- raises(ParseStringError, string_to_int, '-'+s)
- raises(ParseStringError, string_to_int, '0x', 16)
- raises(ParseStringError, string_to_int, '-0x', 16)
-
- exc = raises(ParseStringError, string_to_int, '')
- assert exc.value.msg == "invalid literal for int() with base 10: ''"
- exc = raises(ParseStringError, string_to_int, '', 0)
- assert exc.value.msg == "invalid literal for int() with base 0: ''"
-
- def test_string_to_int_overflow(self):
- import sys
- space = self.space
- raises(ParseStringOverflowError, string_to_int,
- unicode(sys.maxint*17))
-
- def test_string_to_int_not_overflow(self):
- import sys
- for x in [-sys.maxint-1, sys.maxint]:
- y = string_to_int(unicode(x))
- assert y == x
-
- def test_string_to_int_base_error(self):
- space = self.space
- cases = [('1', 1),
- ('1', 37),
- ('a', 0),
- ('9', 9),
- ('0x123', 7),
- ('145cdf', 15),
- ('12', 37),
- ('12', 98172),
- ('12', -1),
- ('12', -908),
- ('12.3', 10),
- ('12.3', 13),
- ('12.3', 16),
- ]
- for s, base in cases:
- raises(ParseStringError, string_to_int, s, base)
- raises(ParseStringError, string_to_int, ' '+s, base)
- raises(ParseStringError, string_to_int, s+' ', base)
- raises(ParseStringError, string_to_int, '+'+s, base)
- raises(ParseStringError, string_to_int, '-'+s, base)
-
- def test_string_to_bigint(self):
- assert string_to_bigint('123').tolong() == 123
- assert string_to_bigint('123 ').tolong() == 123
- raises(ParseStringError, string_to_bigint, 'L')
- raises(ParseStringError, string_to_bigint, 'L ')
- assert string_to_bigint('123', 4).tolong() == 27
- assert string_to_bigint('123L', 30).tolong() == 27000 + 1800 + 90 + 21
- assert string_to_bigint('123L', 22).tolong() == 10648 + 968 + 66 + 21
- assert string_to_bigint('123', 21).tolong() == 441 + 42 + 3
- assert string_to_bigint('1891234174197319').tolong() == 1891234174197319
-
- def test_string_to_float(self):
- assert string_to_float('0') == 0.0
- assert string_to_float('1') == 1.0
- assert string_to_float('-1.5') == -1.5
- assert string_to_float('1.5E2') == 150.0
- assert string_to_float('2.5E-1') == 0.25
- assert string_to_float('1e1111111111111') == float('1e1111111111111')
- assert string_to_float('1e-1111111111111') == float('1e-1111111111111')
- assert string_to_float('-1e1111111111111') == float('-1e1111111111111')
- assert string_to_float('-1e-1111111111111') == float('-1e-1111111111111')
- assert string_to_float('1e111111111111111111111') == float('1e111111111111111111111')
- assert string_to_float('1e-111111111111111111111') == float('1e-111111111111111111111')
- assert string_to_float('-1e111111111111111111111') == float('-1e111111111111111111111')
- assert string_to_float('-1e-111111111111111111111') == float('-1e-111111111111111111111')
-
- valid_parts = [['', ' ', ' \f\n\r\t\v'],
- ['', '+', '-'],
- ['00', '90', '.5', '2.4', '3.', '0.07',
- '12.3489749871982471987198371293717398256187563298638726'
- '2187362820947193247129871083561249818451804287437824015'
- '013816418758104762348932657836583048761487632840726386'],
- ['', 'e0', 'E+1', 'E-01', 'E42'],
- ['', ' ', ' \f\n\r\t\v'],
- ]
- invalid_parts = [['#'],
- ['++', '+-', '-+', '--'],
- ['', '1.2.3', '.', '5..6'],
- ['E+', 'E-', 'e', 'e++', 'E++2'],
- ['#'],
- ]
- for part0 in valid_parts[0]:
- for part1 in valid_parts[1]:
- for part2 in valid_parts[2]:
- for part3 in valid_parts[3]:
- for part4 in valid_parts[4]:
- s = part0+part1+part2+part3+part4
- assert (abs(string_to_float(s) - float(s)) <=
- 1E-13 * abs(float(s)))
-
- for j in range(len(invalid_parts)):
- for invalid in invalid_parts[j]:
- for i in range(20):
- parts = [random.choice(lst) for lst in valid_parts]
- parts[j] = invalid
- s = ''.join(parts)
- print repr(s)
- if s.strip(): # empty s raises OperationError directly
- py.test.raises(ParseStringError, string_to_float, s)
- py.test.raises(ParseStringError, string_to_float, "")
diff --git a/pypy/tool/release/package.py b/pypy/tool/release/package.py
--- a/pypy/tool/release/package.py
+++ b/pypy/tool/release/package.py
@@ -69,6 +69,7 @@
subprocess.check_call([str(pypy_c), '-c', 'import _sqlite3'])
if not sys.platform == 'win32':
subprocess.check_call([str(pypy_c), '-c', 'import _curses'])
+ subprocess.check_call([str(pypy_c), '-c', 'import syslog'])
if sys.platform == 'win32' and not rename_pypy_c.lower().endswith('.exe'):
rename_pypy_c += '.exe'
binaries = [(pypy_c, rename_pypy_c)]
diff --git a/rpython/jit/backend/x86/regalloc.py b/rpython/jit/backend/x86/regalloc.py
--- a/rpython/jit/backend/x86/regalloc.py
+++ b/rpython/jit/backend/x86/regalloc.py
@@ -3,35 +3,34 @@
"""
import os
-from rpython.jit.metainterp.history import (Box, Const, ConstInt, ConstPtr,
- ConstFloat, BoxInt,
- BoxFloat, INT, REF, FLOAT,
- TargetToken)
-from rpython.jit.backend.x86.regloc import *
-from rpython.rtyper.lltypesystem import lltype, rffi, rstr
-from rpython.rtyper.annlowlevel import cast_instance_to_gcref
-from rpython.rlib.objectmodel import we_are_translated
-from rpython.rlib import rgc
from rpython.jit.backend.llsupport import symbolic
+from rpython.jit.backend.llsupport.descr import (ArrayDescr, CallDescr,
+ unpack_arraydescr, unpack_fielddescr, unpack_interiorfielddescr)
+from rpython.jit.backend.llsupport.gcmap import allocate_gcmap
+from rpython.jit.backend.llsupport.regalloc import (FrameManager, BaseRegalloc,
+ RegisterManager, TempBox, compute_vars_longevity, is_comparison_or_ovf_op)
+from rpython.jit.backend.x86 import rx86
+from rpython.jit.backend.x86.arch import (WORD, JITFRAME_FIXED_SIZE, IS_X86_32,
+ IS_X86_64)
from rpython.jit.backend.x86.jump import remap_frame_layout_mixed
+from rpython.jit.backend.x86.regloc import (FrameLoc, RegLoc, ConstFloatLoc,
+ FloatImmedLoc, ImmedLoc, imm, imm0, imm1, ecx, eax, edx, ebx, esi, edi,
+ ebp, r8, r9, r10, r11, r12, r13, r14, r15, xmm0, xmm1, xmm2, xmm3, xmm4,
+ xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14,
+ X86_64_SCRATCH_REG, X86_64_XMM_SCRATCH_REG)
from rpython.jit.codewriter import longlong
from rpython.jit.codewriter.effectinfo import EffectInfo
+from rpython.jit.metainterp.history import (Box, Const, ConstInt, ConstPtr,
+ ConstFloat, BoxInt, BoxFloat, INT, REF, FLOAT, TargetToken)
from rpython.jit.metainterp.resoperation import rop
-from rpython.jit.backend.llsupport.descr import ArrayDescr
-from rpython.jit.backend.llsupport.descr import CallDescr
-from rpython.jit.backend.llsupport.descr import unpack_arraydescr
-from rpython.jit.backend.llsupport.descr import unpack_fielddescr
-from rpython.jit.backend.llsupport.descr import unpack_interiorfielddescr
-from rpython.jit.backend.llsupport.gcmap import allocate_gcmap
-from rpython.jit.backend.llsupport.regalloc import FrameManager, BaseRegalloc,\
- RegisterManager, TempBox, compute_vars_longevity, is_comparison_or_ovf_op
-from rpython.jit.backend.x86.arch import WORD, JITFRAME_FIXED_SIZE
-from rpython.jit.backend.x86.arch import IS_X86_32, IS_X86_64
-from rpython.jit.backend.x86 import rx86
+from rpython.rlib import rgc
+from rpython.rlib.objectmodel import we_are_translated
from rpython.rlib.rarithmetic import r_longlong, r_uint
+from rpython.rtyper.annlowlevel import cast_instance_to_gcref
+from rpython.rtyper.lltypesystem import lltype, rffi, rstr
+
class X86RegisterManager(RegisterManager):
-
box_types = [INT, REF]
all_regs = [ecx, eax, edx, ebx, esi, edi]
no_lower_byte_regs = [esi, edi]
@@ -886,7 +885,6 @@
gcmap[val // WORD // 8] |= r_uint(1) << (val % (WORD * 8))
return gcmap
-
def consider_setfield_gc(self, op):
ofs, size, _ = unpack_fielddescr(op.getdescr())
ofs_loc = imm(ofs)
@@ -950,7 +948,7 @@
def consider_setarrayitem_gc(self, op):
itemsize, ofs, _ = unpack_arraydescr(op.getdescr())
args = op.getarglist()
- base_loc = self.rm.make_sure_var_in_reg(op.getarg(0), args)
+ base_loc = self.rm.make_sure_var_in_reg(op.getarg(0), args)
if itemsize == 1:
need_lower_byte = True
else:
@@ -1311,7 +1309,6 @@
#if jump_op is not None and jump_op.getdescr() is descr:
# self._compute_hint_frame_locations_from_descr(descr)
-
def consider_keepalive(self, op):
pass
diff --git a/rpython/rlib/rarithmetic.py b/rpython/rlib/rarithmetic.py
--- a/rpython/rlib/rarithmetic.py
+++ b/rpython/rlib/rarithmetic.py
@@ -668,3 +668,36 @@
if T == lltype.Float:
return longlong2float(rffi.cast(rffi.LONGLONG, res))
return rffi.cast(T, res)
+
+
+# String parsing support
+# ---------------------------
+
+def string_to_int(s, base=10):
+ """Utility to converts a string to an integer.
+ If base is 0, the proper base is guessed based on the leading
+ characters of 's'. Raises ParseStringError in case of error.
+ Raises ParseStringOverflowError in case the result does not fit.
+ """
+ from rpython.rlib.rstring import NumberStringParser, \
+ ParseStringOverflowError, \
+ ParseStringError, strip_spaces
+ s = literal = strip_spaces(s)
+ p = NumberStringParser(s, literal, base, 'int')
+ base = p.base
+ result = 0
+ while True:
+ digit = p.next_digit()
+ if digit == -1:
+ return result
+
+ if p.sign == -1:
+ digit = -digit
+
+ try:
+ result = ovfcheck(result * base)
+ result = ovfcheck(result + digit)
+ except OverflowError:
+ raise ParseStringOverflowError(p)
+
+
diff --git a/rpython/rlib/rbigint.py b/rpython/rlib/rbigint.py
--- a/rpython/rlib/rbigint.py
+++ b/rpython/rlib/rbigint.py
@@ -61,7 +61,7 @@
KARATSUBA_CUTOFF = 19
else:
KARATSUBA_CUTOFF = 38
-
+
KARATSUBA_SQUARE_CUTOFF = 2 * KARATSUBA_CUTOFF
# For exponentiation, use the binary left-to-right algorithm
@@ -85,38 +85,41 @@
def _load_unsigned_digit(x):
return rffi.cast(UNSIGNED_TYPE, x)
-
+
_load_unsigned_digit._always_inline_ = True
NULLDIGIT = _store_digit(0)
-ONEDIGIT = _store_digit(1)
+ONEDIGIT = _store_digit(1)
def _check_digits(l):
for x in l:
assert type(x) is type(NULLDIGIT)
assert UDIGIT_MASK(x) & MASK == UDIGIT_MASK(x)
-
+
class InvalidEndiannessError(Exception):
pass
class InvalidSignednessError(Exception):
pass
+
class Entry(extregistry.ExtRegistryEntry):
_about_ = _check_digits
+
def compute_result_annotation(self, s_list):
from rpython.annotator import model as annmodel
assert isinstance(s_list, annmodel.SomeList)
s_DIGIT = self.bookkeeper.valueoftype(type(NULLDIGIT))
assert s_DIGIT.contains(s_list.listdef.listitem.s_value)
+
def specialize_call(self, hop):
hop.exception_cannot_occur()
+
class rbigint(object):
"""This is a reimplementation of longs using a list of digits."""
_immutable_ = True
_immutable_fields_ = ["_digits"]
-
def __init__(self, digits=[NULLDIGIT], sign=0, size=0):
if not we_are_translated():
@@ -164,7 +167,7 @@
def numdigits(self):
return self.size
numdigits._always_inline_ = True
-
+
@staticmethod
@jit.elidable
def fromint(intval):
@@ -180,15 +183,14 @@
ival = r_uint(intval)
else:
return NULLRBIGINT
-
+
carry = ival >> SHIFT
if carry:
return rbigint([_store_digit(ival & MASK),
_store_digit(carry)], sign, 2)
else:
return rbigint([_store_digit(ival & MASK)], sign, 1)
-
-
+
@staticmethod
@jit.elidable
def frombool(b):
@@ -246,12 +248,30 @@
@staticmethod
@jit.elidable
def fromdecimalstr(s):
- # This function is marked as pure, so you must not call it and
+ # This function is marked as elidable, so you must not call it and
# then modify the result.
return _decimalstr_to_bigint(s)
@staticmethod
@jit.elidable
+ def fromstr(s, base=0):
+ """As string_to_int(), but ignores an optional 'l' or 'L' suffix
+ and returns an rbigint."""
+ from rpython.rlib.rstring import NumberStringParser, \
+ strip_spaces
+ s = literal = strip_spaces(s)
+ if (s.endswith('l') or s.endswith('L')) and base < 22:
+ # in base 22 and above, 'L' is a valid digit! try: long('L',22)
+ s = s[:-1]
+ parser = NumberStringParser(s, literal, base, 'long')
+ return rbigint._from_numberstring_parser(parser)
+
+ @staticmethod
+ def _from_numberstring_parser(parser):
+ return parse_digit_string(parser)
+
+ @staticmethod
+ @jit.elidable
def frombytes(s, byteorder, signed):
if byteorder not in ('big', 'little'):
raise InvalidEndiannessError()
@@ -319,7 +339,7 @@
# Avoid bogus 0's
s = d ^ MASK if self.sign == -1 else d
while s:
- s >>=1
+ s >>= 1
accumbits += 1
else:
accumbits += SHIFT
@@ -340,7 +360,7 @@
if self.sign == -1:
# Add a sign bit
- accum |= (~_widen_digit(0)) << accumbits;
+ accum |= (~_widen_digit(0)) << accumbits
result.append(chr(accum & 0xFF))
@@ -539,15 +559,15 @@
def mul(self, b):
asize = self.numdigits()
bsize = b.numdigits()
-
+
a = self
-
+
if asize > bsize:
a, b, asize, bsize = b, a, bsize, asize
if a.sign == 0 or b.sign == 0:
return NULLRBIGINT
-
+
if asize == 1:
if a._digits[0] == NULLDIGIT:
return NULLRBIGINT
@@ -560,14 +580,14 @@
return rbigint([_store_digit(res & MASK), _store_digit(carry)], a.sign * b.sign, 2)
else:
return rbigint([_store_digit(res & MASK)], a.sign * b.sign, 1)
-
- result = _x_mul(a, b, a.digit(0))
+
+ result = _x_mul(a, b, a.digit(0))
elif USE_KARATSUBA:
if a is b:
i = KARATSUBA_SQUARE_CUTOFF
else:
i = KARATSUBA_CUTOFF
-
+
if asize <= i:
result = _x_mul(a, b)
"""elif 2 * asize <= bsize:
@@ -593,13 +613,13 @@
return rbigint(self._digits[:self.size], 1, self.size)
elif digit and digit & (digit - 1) == 0:
return self.rshift(ptwotable[digit])
-
+
div, mod = _divrem(self, other)
if mod.sign * other.sign == -1:
if div.sign == 0:
return ONENEGATIVERBIGINT
div = div.sub(ONERBIGINT)
-
+
return div
@jit.look_inside
@@ -610,7 +630,7 @@
def mod(self, other):
if self.sign == 0:
return NULLRBIGINT
-
+
if other.sign != 0 and other.numdigits() == 1:
digit = other.digit(0)
if digit == 1:
@@ -633,7 +653,7 @@
size -= 1
else:
rem = self.digit(0) % digit
-
+
if rem == 0:
return NULLRBIGINT
mod = rbigint([_store_digit(rem)], -1 if self.sign < 0 else 1, 1)
@@ -684,9 +704,9 @@
"cannot be negative when 3rd argument specified")
# XXX failed to implement
raise ValueError("bigint pow() too negative")
-
+
size_b = b.numdigits()
-
+
if c is not None:
if c.sign == 0:
raise ValueError("pow() 3rd argument cannot be 0")
@@ -702,13 +722,13 @@
# return 0
if c.numdigits() == 1 and c._digits[0] == ONEDIGIT:
return NULLRBIGINT
-
+
# if base < 0:
# base = base % modulus
# Having the base positive just makes things easier.
if a.sign < 0:
a = a.mod(c)
-
+
elif b.sign == 0:
return ONERBIGINT
elif a.sign == 0:
@@ -730,12 +750,12 @@
if a.sign == -1 and not digit % 2:
ret.sign = 1
return ret
-
+
# At this point a, b, and c are guaranteed non-negative UNLESS
# c is NULL, in which case a may be negative. */
z = rbigint([ONEDIGIT], 1, 1)
-
+
# python adaptation: moved macros REDUCE(X) and MULT(X, Y, result)
# into helper function result = _help_mult(x, y, c)
if size_b <= FIVEARY_CUTOFF:
@@ -751,7 +771,7 @@
z = _help_mult(z, a, c)
j >>= 1
size_b -= 1
-
+
else:
# Left-to-right 5-ary exponentiation (HAC Algorithm 14.82)
# This is only useful in the case where c != None.
@@ -784,7 +804,7 @@
# must get the next digit from 'b' in order to complete
if size_b == 0:
break # Done
-
+
size_b -= 1
assert size_b >= 0
bi = b.udigit(size_b)
@@ -798,7 +818,7 @@
z = _help_mult(z, table[index], c)
#
assert j == -5
-
+
if negativeOutput and z.sign != 0:
z = z.sub(c)
return z
@@ -817,12 +837,12 @@
def invert(self): #Implement ~x as -(x + 1)
if self.sign == 0:
return ONENEGATIVERBIGINT
-
+
ret = self.add(ONERBIGINT)
ret.sign = -ret.sign
return ret
-
- @jit.elidable
+
+ @jit.elidable
def lshift(self, int_other):
if int_other < 0:
raise ValueError("negative shift count")
@@ -831,14 +851,14 @@
# wordshift, remshift = divmod(int_other, SHIFT)
wordshift = int_other // SHIFT
- remshift = int_other - wordshift * SHIFT
+ remshift = int_other - wordshift * SHIFT
if not remshift:
# So we can avoid problems with eq, AND avoid the need for normalize.
if self.sign == 0:
return self
return rbigint([NULLDIGIT] * wordshift + self._digits, self.sign, self.size + wordshift)
-
+
oldsize = self.numdigits()
newsize = oldsize + wordshift + 1
z = rbigint([NULLDIGIT] * newsize, self.sign, newsize)
@@ -850,7 +870,7 @@
accum >>= SHIFT
wordshift += 1
j += 1
-
+
newsize -= 1
assert newsize >= 0
z.setdigit(newsize, accum)
@@ -858,7 +878,7 @@
z._normalize()
return z
lshift._always_inline_ = True # It's so fast that it's always benefitial.
-
+
@jit.elidable
def lqshift(self, int_other):
" A quicker one with much less checks, int_other is valid and for the most part constant."
@@ -878,7 +898,7 @@
z._normalize()
return z
lqshift._always_inline_ = True # It's so fast that it's always benefitial.
-
+
@jit.elidable
def rshift(self, int_other, dont_invert=False):
if int_other < 0:
@@ -910,7 +930,7 @@
z._normalize()
return z
rshift._always_inline_ = 'try' # It's so fast that it's always benefitial.
-
+
@jit.elidable
def and_(self, other):
return _bitwise(self, '&', other)
@@ -968,7 +988,7 @@
self._digits = [NULLDIGIT]
_normalize._always_inline_ = True
-
+
@jit.elidable
def bit_length(self):
i = self.numdigits()
@@ -1018,7 +1038,7 @@
# is NULL.
if c is not None:
res = res.mod(c)
-
+
return res
def digits_from_nonneg_long(l):
@@ -1103,7 +1123,7 @@
def _x_sub(a, b):
""" Subtract the absolute values of two integers. """
-
+
size_a = a.numdigits()
size_b = b.numdigits()
sign = 1
@@ -1124,7 +1144,7 @@
sign = -1
a, b = b, a
size_a = size_b = i+1
-
+
z = rbigint([NULLDIGIT] * size_a, sign, size_a)
borrow = UDIGIT_TYPE(0)
i = _load_unsigned_digit(0)
@@ -1142,7 +1162,7 @@
borrow >>= SHIFT
#borrow &= 1
i += 1
-
+
assert borrow == 0
z._normalize()
return z
@@ -1152,7 +1172,7 @@
for x in range(SHIFT-1):
ptwotable[r_longlong(2 << x)] = x+1
ptwotable[r_longlong(-2 << x)] = x+1
-
+
def _x_mul(a, b, digit=0):
"""
Grade school multiplication, ignoring the signs.
@@ -1201,14 +1221,14 @@
i += 1
z._normalize()
return z
-
+
elif digit:
if digit & (digit - 1) == 0:
return b.lqshift(ptwotable[digit])
-
+
# Even if it's not power of two it can still be useful.
return _muladd1(b, digit)
-
+
z = rbigint([NULLDIGIT] * (size_a + size_b), 1)
# gradeschool long mult
i = UDIGIT_TYPE(0)
@@ -1258,7 +1278,7 @@
"""
asize = a.numdigits()
bsize = b.numdigits()
-
+
# (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
# Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
# Then the original product is
@@ -1341,7 +1361,7 @@
t2 = _x_add(bh, bl)
t3 = t1.mul(t2)
- assert t3.sign >=0
+ assert t3.sign >= 0
# Add t3. It's not obvious why we can't run out of room here.
# See the (*) comment after this function.
@@ -1421,9 +1441,9 @@
#bslice = rbigint([0] * asize, 1)
# XXX we cannot pre-allocate, see comments below!
# XXX prevent one list from being created.
- bslice = rbigint(sign = 1)
More information about the pypy-commit
mailing list