[pypy-commit] pypy stdlib-2.7.5: merge default into branch
mattip
noreply at buildbot.pypy.org
Sun Nov 10 00:05:48 CET 2013
Author: Matti Picus <matti.picus at gmail.com>
Branch: stdlib-2.7.5
Changeset: r67916:61633f279ebe
Date: 2013-11-09 22:51 +0200
http://bitbucket.org/pypy/pypy/changeset/61633f279ebe/
Log: merge default into branch
diff too long, truncating to 2000 out of 42148 lines
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,38 @@
all: pypy-c
+PYPY_EXECUTABLE := $(shell which pypy)
+URAM := $(shell python -c "import sys; print 4.5 if sys.maxint>1<<32 else 2.5")
+
+ifeq ($(PYPY_EXECUTABLE),)
+RUNINTERP = python
+else
+RUNINTERP = $(PYPY_EXECUTABLE)
+endif
+
pypy-c:
- @echo "Building PyPy with JIT, it'll take about 40 minutes and 4G of RAM"
- @sleep 3
- rpython/bin/rpython -Ojit pypy/goal/targetpypystandalone.py
+ @echo
+ @echo "===================================================================="
+ifeq ($(PYPY_EXECUTABLE),)
+ @echo "Building a regular (jitting) version of PyPy, using CPython."
+ @echo "This takes around 2 hours and $(URAM) GB of RAM."
+ @echo "Note that pre-installing a PyPy binary would reduce this time"
+ @echo "and produce basically the same result."
+else
+ @echo "Building a regular (jitting) version of PyPy, using"
+ @echo "$(PYPY_EXECUTABLE) to run the translation itself."
+ @echo "This takes up to 1 hour and $(URAM) GB of RAM."
+endif
+ @echo
+ @echo "For more control (e.g. to use multiple CPU cores during part of"
+ @echo "the process) you need to run \`\`rpython/bin/rpython'' directly."
+ @echo "For more information see \`\`http://pypy.org/download.html''."
+ @echo "===================================================================="
+ @echo
+ @sleep 5
+ $(RUNINTERP) rpython/bin/rpython -Ojit pypy/goal/targetpypystandalone.py
+
+# Note: the -jN option, or MAKEFLAGS=-jN, are not usable. They are
+# replaced with an opaque --jobserver option by the time this Makefile
+# runs. We cannot get their original value either:
+# http://lists.gnu.org/archive/html/help-make/2010-08/msg00106.html
diff --git a/README.rst b/README.rst
--- a/README.rst
+++ b/README.rst
@@ -1,5 +1,5 @@
=====================================
-PyPy: Python in Python Implementation
+PyPy: Python in Python Implementation
=====================================
Welcome to PyPy!
@@ -26,9 +26,11 @@
Building
========
-build with::
+build with:
- rpython/bin/rpython -Ojit pypy/goal/targetpypystandalone.py
+.. code-block:: console
+
+ $ rpython/bin/rpython -Ojit pypy/goal/targetpypystandalone.py
This ends up with ``pypy-c`` binary in the main pypy directory. We suggest
to use virtualenv with the resulting pypy-c as the interpreter, you can
diff --git a/lib-python/2.7/argparse.py b/lib-python/2.7/argparse.py
--- a/lib-python/2.7/argparse.py
+++ b/lib-python/2.7/argparse.py
@@ -1780,7 +1780,19 @@
# error if this argument is not allowed with other previously
# seen arguments, assuming that actions that use the default
# value don't really count as "present"
- if argument_values is not action.default:
+
+ # XXX PyPy bug-to-bug compatibility: "is" on primitive types
+ # is not consistent in CPython. We'll assume it is close
+ # enough for ints (which is true only for "small ints"), but
+ # for floats and longs and complexes we'll go for the option
+ # of forcing "is" to say False, like it usually does on
+ # CPython. A fix is pending on CPython trunk
+ # (http://bugs.python.org/issue18943) but that might change
+ # the details of the semantics and so not be applied to 2.7.
+ # See the line AA below.
+
+ if (argument_values is not action.default or
+ type(argument_values) in (float, long, complex)): # AA
seen_non_default_actions.add(action)
for conflict_action in action_conflicts.get(action, []):
if conflict_action in seen_non_default_actions:
diff --git a/lib-python/2.7/string.py b/lib-python/2.7/string.py
--- a/lib-python/2.7/string.py
+++ b/lib-python/2.7/string.py
@@ -66,16 +66,17 @@
must be of the same length.
"""
- if len(fromstr) != len(tostr):
+ n = len(fromstr)
+ if n != len(tostr):
raise ValueError, "maketrans arguments must have same length"
- global _idmapL
- if not _idmapL:
- _idmapL = list(_idmap)
- L = _idmapL[:]
- fromstr = map(ord, fromstr)
- for i in range(len(fromstr)):
- L[fromstr[i]] = tostr[i]
- return ''.join(L)
+ # this function has been rewritten to suit PyPy better; it is
+ # almost 10x faster than the original.
+ buf = bytearray(256)
+ for i in range(256):
+ buf[i] = i
+ for i in range(n):
+ buf[ord(fromstr[i])] = tostr[i]
+ return str(buf)
diff --git a/lib-python/2.7/uuid.py b/lib-python/2.7/uuid.py
--- a/lib-python/2.7/uuid.py
+++ b/lib-python/2.7/uuid.py
@@ -44,6 +44,8 @@
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
"""
+import struct
+
__author__ = 'Ka-Ping Yee <ping at zesty.ca>'
RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
@@ -125,25 +127,39 @@
overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
"""
- if [hex, bytes, bytes_le, fields, int].count(None) != 4:
- raise TypeError('need one of hex, bytes, bytes_le, fields, or int')
if hex is not None:
+ if (bytes is not None or bytes_le is not None or
+ fields is not None or int is not None):
+ raise TypeError('if the hex argument is given, bytes,'
+ ' bytes_le, fields, and int need to be None')
hex = hex.replace('urn:', '').replace('uuid:', '')
hex = hex.strip('{}').replace('-', '')
if len(hex) != 32:
raise ValueError('badly formed hexadecimal UUID string')
int = long(hex, 16)
- if bytes_le is not None:
+ elif bytes_le is not None:
+ if bytes is not None or fields is not None or int is not None:
+ raise TypeError('if the bytes_le argument is given, bytes,'
+ ' fields, and int need to be None')
if len(bytes_le) != 16:
raise ValueError('bytes_le is not a 16-char string')
bytes = (bytes_le[3] + bytes_le[2] + bytes_le[1] + bytes_le[0] +
bytes_le[5] + bytes_le[4] + bytes_le[7] + bytes_le[6] +
bytes_le[8:])
- if bytes is not None:
+ int = (struct.unpack('>Q', bytes[:8])[0] << 64 |
+ struct.unpack('>Q', bytes[8:])[0])
+ elif bytes is not None:
+ if fields is not None or int is not None:
+ raise TypeError('if the bytes argument is given, fields '
+ 'and int need to be None')
if len(bytes) != 16:
raise ValueError('bytes is not a 16-char string')
- int = long(('%02x'*16) % tuple(map(ord, bytes)), 16)
- if fields is not None:
+ int = (struct.unpack('>Q', bytes[:8])[0] << 64 |
+ struct.unpack('>Q', bytes[8:])[0])
+ elif fields is not None:
+ if int is not None:
+ raise TypeError('if the fields argument is given, int needs'
+ ' to be None')
if len(fields) != 6:
raise ValueError('fields is not a 6-tuple')
(time_low, time_mid, time_hi_version,
@@ -163,9 +179,12 @@
clock_seq = (clock_seq_hi_variant << 8L) | clock_seq_low
int = ((time_low << 96L) | (time_mid << 80L) |
(time_hi_version << 64L) | (clock_seq << 48L) | node)
- if int is not None:
+ elif int is not None:
if not 0 <= int < 1<<128L:
raise ValueError('int is out of range (need a 128-bit value)')
+ else:
+ raise TypeError('one of hex, bytes, bytes_le, fields,'
+ ' or int need to be not None')
if version is not None:
if not 1 <= version <= 5:
raise ValueError('illegal version number')
@@ -175,7 +194,7 @@
# Set the version number.
int &= ~(0xf000 << 64L)
int |= version << 76L
- self.__dict__['int'] = int
+ object.__setattr__(self, 'int', int)
def __cmp__(self, other):
if isinstance(other, UUID):
diff --git a/lib_pypy/_curses.py b/lib_pypy/_curses.py
--- a/lib_pypy/_curses.py
+++ b/lib_pypy/_curses.py
@@ -1,6 +1,9 @@
"""Reimplementation of the standard extension module '_curses' using cffi."""
import sys
+if sys.platform == 'win32':
+ #This module does not exist in windows
+ raise ImportError('No module named _curses')
from functools import wraps
from cffi import FFI
diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -363,9 +363,11 @@
pass
-def connect(database, **kwargs):
- factory = kwargs.get("factory", Connection)
- return factory(database, **kwargs)
+def connect(database, timeout=5.0, detect_types=0, isolation_level="",
+ check_same_thread=True, factory=None, cached_statements=100):
+ factory = Connection if not factory else factory
+ return factory(database, timeout, detect_types, isolation_level,
+ check_same_thread, factory, cached_statements)
def _unicode_text_factory(x):
@@ -1238,7 +1240,10 @@
if cvt is not None:
param = cvt(param)
- param = adapt(param)
+ try:
+ param = adapt(param)
+ except:
+ pass # And use previous value
if param is None:
rc = _lib.sqlite3_bind_null(self._statement, idx)
diff --git a/lib_pypy/_tkinter/__init__.py b/lib_pypy/_tkinter/__init__.py
--- a/lib_pypy/_tkinter/__init__.py
+++ b/lib_pypy/_tkinter/__init__.py
@@ -22,6 +22,7 @@
READABLE = tklib.TCL_READABLE
WRITABLE = tklib.TCL_WRITABLE
EXCEPTION = tklib.TCL_EXCEPTION
+DONT_WAIT = tklib.TCL_DONT_WAIT
def create(screenName=None, baseName=None, className=None,
interactive=False, wantobjects=False, wantTk=True,
diff --git a/lib_pypy/_tkinter/app.py b/lib_pypy/_tkinter/app.py
--- a/lib_pypy/_tkinter/app.py
+++ b/lib_pypy/_tkinter/app.py
@@ -4,7 +4,23 @@
from . import TclError
from .tclobj import TclObject, FromObj, AsObj, TypeCache
+import contextlib
import sys
+import threading
+import time
+
+
+class _DummyLock(object):
+ "A lock-like object that does not do anything"
+ def acquire(self):
+ pass
+ def release(self):
+ pass
+ def __enter__(self):
+ pass
+ def __exit__(self, *exc):
+ pass
+
def varname_converter(input):
if isinstance(input, TclObject):
@@ -37,17 +53,18 @@
def PythonCmd(clientData, interp, argc, argv):
self = tkffi.from_handle(clientData)
assert self.app.interp == interp
- try:
- args = [tkffi.string(arg) for arg in argv[1:argc]]
- result = self.func(*args)
- obj = AsObj(result)
- tklib.Tcl_SetObjResult(interp, obj)
- except:
- self.app.errorInCmd = True
- self.app.exc_info = sys.exc_info()
- return tklib.TCL_ERROR
- else:
- return tklib.TCL_OK
+ with self.app._tcl_lock_released():
+ try:
+ args = [tkffi.string(arg) for arg in argv[1:argc]]
+ result = self.func(*args)
+ obj = AsObj(result)
+ tklib.Tcl_SetObjResult(interp, obj)
+ except:
+ self.app.errorInCmd = True
+ self.app.exc_info = sys.exc_info()
+ return tklib.TCL_ERROR
+ else:
+ return tklib.TCL_OK
@tkffi.callback("Tcl_CmdDeleteProc")
def PythonCmdDelete(clientData):
@@ -58,6 +75,8 @@
class TkApp(object):
+ _busywaitinterval = 0.02 # 20ms.
+
def __new__(cls, screenName, baseName, className,
interactive, wantobjects, wantTk, sync, use):
if not wantobjects:
@@ -73,6 +92,12 @@
self.quitMainLoop = False
self.errorInCmd = False
+ if not self.threaded:
+ # TCL is not thread-safe, calls needs to be serialized.
+ self._tcl_lock = threading.Lock()
+ else:
+ self._tcl_lock = _DummyLock()
+
self._typeCache = TypeCache()
self._commands = {}
@@ -133,6 +158,13 @@
if self.threaded and self.thread_id != tklib.Tcl_GetCurrentThread():
raise RuntimeError("Calling Tcl from different appartment")
+ @contextlib.contextmanager
+ def _tcl_lock_released(self):
+ "Context manager to temporarily release the tcl lock."
+ self._tcl_lock.release()
+ yield
+ self._tcl_lock.acquire()
+
def loadtk(self):
# We want to guard against calling Tk_Init() multiple times
err = tklib.Tcl_Eval(self.interp, "info exists tk_version")
@@ -159,22 +191,25 @@
flags=tklib.TCL_LEAVE_ERR_MSG
if global_only:
flags |= tklib.TCL_GLOBAL_ONLY
- res = tklib.Tcl_GetVar2Ex(self.interp, name1, name2, flags)
- if not res:
- self.raiseTclError()
- assert self._wantobjects
- return FromObj(self, res)
+ with self._tcl_lock:
+ res = tklib.Tcl_GetVar2Ex(self.interp, name1, name2, flags)
+ if not res:
+ self.raiseTclError()
+ assert self._wantobjects
+ return FromObj(self, res)
def _setvar(self, name1, value, global_only=False):
name1 = varname_converter(name1)
+ # XXX Acquire tcl lock???
newval = AsObj(value)
flags=tklib.TCL_LEAVE_ERR_MSG
if global_only:
flags |= tklib.TCL_GLOBAL_ONLY
- res = tklib.Tcl_SetVar2Ex(self.interp, name1, tkffi.NULL,
- newval, flags)
- if not res:
- self.raiseTclError()
+ with self._tcl_lock:
+ res = tklib.Tcl_SetVar2Ex(self.interp, name1, tkffi.NULL,
+ newval, flags)
+ if not res:
+ self.raiseTclError()
def _unsetvar(self, name1, name2=None, global_only=False):
name1 = varname_converter(name1)
@@ -183,9 +218,10 @@
flags=tklib.TCL_LEAVE_ERR_MSG
if global_only:
flags |= tklib.TCL_GLOBAL_ONLY
- res = tklib.Tcl_UnsetVar2(self.interp, name1, name2, flags)
- if res == tklib.TCL_ERROR:
- self.raiseTclError()
+ with self._tcl_lock:
+ res = tklib.Tcl_UnsetVar2(self.interp, name1, name2, flags)
+ if res == tklib.TCL_ERROR:
+ self.raiseTclError()
def getvar(self, name1, name2=None):
return self._var_invoke(self._getvar, name1, name2)
@@ -219,9 +255,10 @@
if self.threaded and self.thread_id != tklib.Tcl_GetCurrentThread():
raise NotImplementedError("Call from another thread")
- res = tklib.Tcl_CreateCommand(
- self.interp, cmdName, _CommandData.PythonCmd,
- clientData, _CommandData.PythonCmdDelete)
+ with self._tcl_lock:
+ res = tklib.Tcl_CreateCommand(
+ self.interp, cmdName, _CommandData.PythonCmd,
+ clientData, _CommandData.PythonCmdDelete)
if not res:
raise TclError("can't create Tcl command")
@@ -229,7 +266,8 @@
if self.threaded and self.thread_id != tklib.Tcl_GetCurrentThread():
raise NotImplementedError("Call from another thread")
- res = tklib.Tcl_DeleteCommand(self.interp, cmdName)
+ with self._tcl_lock:
+ res = tklib.Tcl_DeleteCommand(self.interp, cmdName)
if res == -1:
raise TclError("can't delete Tcl command")
@@ -256,11 +294,12 @@
tklib.Tcl_IncrRefCount(obj)
objects[i] = obj
- res = tklib.Tcl_EvalObjv(self.interp, argc, objects, flags)
- if res == tklib.TCL_ERROR:
- self.raiseTclError()
- else:
- result = self._callResult()
+ with self._tcl_lock:
+ res = tklib.Tcl_EvalObjv(self.interp, argc, objects, flags)
+ if res == tklib.TCL_ERROR:
+ self.raiseTclError()
+ else:
+ result = self._callResult()
finally:
for obj in objects:
if obj:
@@ -280,17 +319,19 @@
def eval(self, script):
self._check_tcl_appartment()
- res = tklib.Tcl_Eval(self.interp, script)
- if res == tklib.TCL_ERROR:
- self.raiseTclError()
- return tkffi.string(tklib.Tcl_GetStringResult(self.interp))
+ with self._tcl_lock:
+ res = tklib.Tcl_Eval(self.interp, script)
+ if res == tklib.TCL_ERROR:
+ self.raiseTclError()
+ return tkffi.string(tklib.Tcl_GetStringResult(self.interp))
def evalfile(self, filename):
self._check_tcl_appartment()
- res = tklib.Tcl_EvalFile(self.interp, filename)
- if res == tklib.TCL_ERROR:
- self.raiseTclError()
- return tkffi.string(tklib.Tcl_GetStringResult(self.interp))
+ with self._tcl_lock:
+ res = tklib.Tcl_EvalFile(self.interp, filename)
+ if res == tklib.TCL_ERROR:
+ self.raiseTclError()
+ return tkffi.string(tklib.Tcl_GetStringResult(self.interp))
def split(self, arg):
if isinstance(arg, tuple):
@@ -375,7 +416,10 @@
if self.threaded:
result = tklib.Tcl_DoOneEvent(0)
else:
- raise NotImplementedError("TCL configured without threads")
+ with self._tcl_lock:
+ result = tklib.Tcl_DoOneEvent(tklib.TCL_DONT_WAIT)
+ if result == 0:
+ time.sleep(self._busywaitinterval)
if result < 0:
break
diff --git a/lib_pypy/_tkinter/tclobj.py b/lib_pypy/_tkinter/tclobj.py
--- a/lib_pypy/_tkinter/tclobj.py
+++ b/lib_pypy/_tkinter/tclobj.py
@@ -28,9 +28,11 @@
return result
elif value.typePtr == typeCache.BooleanType:
- return result
+ return bool(value.internalRep.longValue)
elif value.typePtr == typeCache.ByteArrayType:
- return result
+ size = tkffi.new('int*')
+ data = tklib.Tcl_GetByteArrayFromObj(value, size)
+ return tkffi.buffer(data, size[0])[:]
elif value.typePtr == typeCache.DoubleType:
return value.internalRep.doubleValue
elif value.typePtr == typeCache.IntType:
@@ -50,7 +52,7 @@
result.append(FromObj(app, tcl_elem[0]))
return tuple(result)
elif value.typePtr == typeCache.ProcBodyType:
- return result
+ pass # fall through and return tcl object.
elif value.typePtr == typeCache.StringType:
buf = tklib.Tcl_GetUnicode(value)
length = tklib.Tcl_GetCharLength(value)
diff --git a/lib_pypy/_tkinter/tklib.py b/lib_pypy/_tkinter/tklib.py
--- a/lib_pypy/_tkinter/tklib.py
+++ b/lib_pypy/_tkinter/tklib.py
@@ -1,6 +1,7 @@
# C bindings with libtcl and libtk.
from cffi import FFI
+import sys
tkffi = FFI()
@@ -18,6 +19,8 @@
#define TCL_EVAL_DIRECT ...
#define TCL_EVAL_GLOBAL ...
+#define TCL_DONT_WAIT ...
+
typedef unsigned short Tcl_UniChar;
typedef ... Tcl_Interp;
typedef ...* Tcl_ThreadId;
@@ -69,6 +72,7 @@
int Tcl_GetBoolean(Tcl_Interp* interp, const char* src, int* boolPtr);
char *Tcl_GetString(Tcl_Obj* objPtr);
char *Tcl_GetStringFromObj(Tcl_Obj* objPtr, int* lengthPtr);
+unsigned char *Tcl_GetByteArrayFromObj(Tcl_Obj* objPtr, int* lengthPtr);
Tcl_UniChar *Tcl_GetUnicode(Tcl_Obj* objPtr);
int Tcl_GetCharLength(Tcl_Obj* objPtr);
@@ -102,6 +106,17 @@
int Tk_GetNumMainWindows();
""")
+# XXX find a better way to detect paths
+# XXX pick up CPPFLAGS and LDFLAGS and add to these paths?
+if sys.platform.startswith("openbsd"):
+ incdirs = ['/usr/local/include/tcl8.5', '/usr/local/include/tk8.5', '/usr/X11R6/include']
+ linklibs = ['tk85', 'tcl85']
+ libdirs = ['/usr/local/lib', '/usr/X11R6/lib']
+else:
+ incdirs=['/usr/include/tcl']
+ linklibs=['tcl', 'tk']
+ libdirs = []
+
tklib = tkffi.verify("""
#include <tcl.h>
#include <tk.h>
@@ -109,6 +124,7 @@
char *get_tk_version() { return TK_VERSION; }
char *get_tcl_version() { return TCL_VERSION; }
""",
-include_dirs=['/usr/include/tcl'],
-libraries=['tcl', 'tk'],
+include_dirs=incdirs,
+libraries=linklibs,
+library_dirs = libdirs
)
diff --git a/lib_pypy/cffi/__init__.py b/lib_pypy/cffi/__init__.py
--- a/lib_pypy/cffi/__init__.py
+++ b/lib_pypy/cffi/__init__.py
@@ -4,5 +4,5 @@
from .api import FFI, CDefError, FFIError
from .ffiplatform import VerificationError, VerificationMissing
-__version__ = "0.7"
-__version_info__ = (0, 7)
+__version__ = "0.7.2"
+__version_info__ = (0, 7, 2)
diff --git a/lib_pypy/cffi/api.py b/lib_pypy/cffi/api.py
--- a/lib_pypy/cffi/api.py
+++ b/lib_pypy/cffi/api.py
@@ -54,7 +54,8 @@
# _cffi_backend.so compiled.
import _cffi_backend as backend
from . import __version__
- assert backend.__version__ == __version__
+ assert (backend.__version__ == __version__ or
+ backend.__version__ == __version__[:3])
# (If you insist you can also try to pass the option
# 'backend=backend_ctypes.CTypesBackend()', but don't
# rely on it! It's probably not going to work well.)
diff --git a/lib_pypy/cffi/commontypes.py b/lib_pypy/cffi/commontypes.py
--- a/lib_pypy/cffi/commontypes.py
+++ b/lib_pypy/cffi/commontypes.py
@@ -30,7 +30,9 @@
elif result in model.PrimitiveType.ALL_PRIMITIVE_TYPES:
result = model.PrimitiveType(result)
else:
- assert commontype != result
+ if commontype == result:
+ raise api.FFIError("Unsupported type: %r. Please file a bug "
+ "if you think it should be." % (commontype,))
result = resolve_common_type(result) # recursively
assert isinstance(result, model.BaseTypeByIdentity)
_CACHE[commontype] = result
diff --git a/lib_pypy/cffi/cparser.py b/lib_pypy/cffi/cparser.py
--- a/lib_pypy/cffi/cparser.py
+++ b/lib_pypy/cffi/cparser.py
@@ -290,13 +290,26 @@
# assume a primitive type. get it from .names, but reduce
# synonyms to a single chosen combination
names = list(type.names)
- if names == ['signed'] or names == ['unsigned']:
- names.append('int')
- if names[0] == 'signed' and names != ['signed', 'char']:
- names.pop(0)
- if (len(names) > 1 and names[-1] == 'int'
- and names != ['unsigned', 'int']):
- names.pop()
+ if names != ['signed', 'char']: # keep this unmodified
+ prefixes = {}
+ while names:
+ name = names[0]
+ if name in ('short', 'long', 'signed', 'unsigned'):
+ prefixes[name] = prefixes.get(name, 0) + 1
+ del names[0]
+ else:
+ break
+ # ignore the 'signed' prefix below, and reorder the others
+ newnames = []
+ for prefix in ('unsigned', 'short', 'long'):
+ for i in range(prefixes.get(prefix, 0)):
+ newnames.append(prefix)
+ if not names:
+ names = ['int'] # implicitly
+ if names == ['int']: # but kill it if 'short' or 'long'
+ if 'short' in prefixes or 'long' in prefixes:
+ names = []
+ names = newnames + names
ident = ' '.join(names)
if ident == 'void':
return model.void_type
@@ -500,8 +513,8 @@
self._partial_length = True
return None
#
- raise api.FFIError("unsupported non-constant or "
- "not immediately constant expression")
+ raise api.FFIError("unsupported expression: expected a "
+ "simple numeric constant")
def _build_enum_type(self, explicit_name, decls):
if decls is not None:
diff --git a/lib_pypy/cffi/vengine_gen.py b/lib_pypy/cffi/vengine_gen.py
--- a/lib_pypy/cffi/vengine_gen.py
+++ b/lib_pypy/cffi/vengine_gen.py
@@ -61,7 +61,9 @@
def load_library(self):
# import it with the CFFI backend
backend = self.ffi._backend
- module = backend.load_library(self.verifier.modulefilename)
+ # needs to make a path that contains '/', on Posix
+ filename = os.path.join(os.curdir, self.verifier.modulefilename)
+ module = backend.load_library(filename)
#
# call loading_gen_struct() to get the struct layout inferred by
# the C compiler
diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -40,9 +40,9 @@
# for all computations. See the book for algorithms for converting between
# proleptic Gregorian ordinals and many other calendar systems.
-_DAYS_IN_MONTH = [None, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+_DAYS_IN_MONTH = [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
-_DAYS_BEFORE_MONTH = [None]
+_DAYS_BEFORE_MONTH = [-1]
dbm = 0
for dim in _DAYS_IN_MONTH[1:]:
_DAYS_BEFORE_MONTH.append(dbm)
diff --git a/lib_pypy/numpy.py b/lib_pypy/numpy.py
deleted file mode 100644
--- a/lib_pypy/numpy.py
+++ /dev/null
@@ -1,5 +0,0 @@
-raise ImportError(
- "The 'numpy' module of PyPy is in-development and not complete. "
- "To try it out anyway, you can either import from 'numpypy', "
- "or just write 'import numpypy' first in your program and then "
- "import from 'numpy' as usual.")
diff --git a/lib_pypy/numpypy/__init__.py b/lib_pypy/numpypy/__init__.py
deleted file mode 100644
--- a/lib_pypy/numpypy/__init__.py
+++ /dev/null
@@ -1,14 +0,0 @@
-import core
-from core import *
-import lib
-from lib import *
-
-from __builtin__ import bool, int, long, float, complex, object, unicode, str
-from core import abs, max, min
-
-__all__ = []
-__all__ += core.__all__
-__all__ += lib.__all__
-
-import sys
-sys.modules.setdefault('numpy', sys.modules['numpypy'])
diff --git a/lib_pypy/numpypy/core/__init__.py b/lib_pypy/numpypy/core/__init__.py
deleted file mode 100644
--- a/lib_pypy/numpypy/core/__init__.py
+++ /dev/null
@@ -1,14 +0,0 @@
-import numeric
-from numeric import *
-import fromnumeric
-from fromnumeric import *
-import shape_base
-from shape_base import *
-
-from fromnumeric import amax as max, amin as min
-from numeric import absolute as abs
-
-__all__ = []
-__all__ += numeric.__all__
-__all__ += fromnumeric.__all__
-__all__ += shape_base.__all__
diff --git a/lib_pypy/numpypy/core/_methods.py b/lib_pypy/numpypy/core/_methods.py
deleted file mode 100644
--- a/lib_pypy/numpypy/core/_methods.py
+++ /dev/null
@@ -1,109 +0,0 @@
-# Array methods which are called by the both the C-code for the method
-# and the Python code for the NumPy-namespace function
-
-import multiarray as mu
-import umath as um
-from numeric import asanyarray
-
-def _amax(a, axis=None, out=None, keepdims=False):
- return um.maximum.reduce(a, axis=axis,
- out=out, keepdims=keepdims)
-
-def _amin(a, axis=None, out=None, keepdims=False):
- return um.minimum.reduce(a, axis=axis,
- out=out, keepdims=keepdims)
-
-def _sum(a, axis=None, dtype=None, out=None, keepdims=False):
- return um.add.reduce(a, axis=axis, dtype=dtype,
- out=out, keepdims=keepdims)
-
-def _prod(a, axis=None, dtype=None, out=None, keepdims=False):
- return um.multiply.reduce(a, axis=axis, dtype=dtype,
- out=out, keepdims=keepdims)
-
-def _any(a, axis=None, dtype=None, out=None, keepdims=False):
- return um.logical_or.reduce(a, axis=axis, dtype=dtype, out=out,
- keepdims=keepdims)
-
-def _all(a, axis=None, dtype=None, out=None, keepdims=False):
- return um.logical_and.reduce(a, axis=axis, dtype=dtype, out=out,
- keepdims=keepdims)
-
-def _count_reduce_items(arr, axis):
- if axis is None:
- axis = tuple(xrange(arr.ndim))
- if not isinstance(axis, tuple):
- axis = (axis,)
- items = 1
- for ax in axis:
- items *= arr.shape[ax]
- return items
-
-def _mean(a, axis=None, dtype=None, out=None, keepdims=False):
- arr = asanyarray(a)
-
- # Upgrade bool, unsigned int, and int to float64
- if dtype is None and arr.dtype.kind in ['b','u','i']:
- ret = um.add.reduce(arr, axis=axis, dtype='f8',
- out=out, keepdims=keepdims)
- else:
- ret = um.add.reduce(arr, axis=axis, dtype=dtype,
- out=out, keepdims=keepdims)
- rcount = _count_reduce_items(arr, axis)
- if isinstance(ret, mu.ndarray):
- ret = um.true_divide(ret, rcount,
- out=ret, casting='unsafe', subok=False)
- else:
- ret = ret / float(rcount)
- return ret
-
-def _var(a, axis=None, dtype=None, out=None, ddof=0,
- keepdims=False):
- arr = asanyarray(a)
-
- # First compute the mean, saving 'rcount' for reuse later
- if dtype is None and arr.dtype.kind in ['b','u','i']:
- arrmean = um.add.reduce(arr, axis=axis, dtype='f8', keepdims=True)
- else:
- arrmean = um.add.reduce(arr, axis=axis, dtype=dtype, keepdims=True)
- rcount = _count_reduce_items(arr, axis)
- if isinstance(arrmean, mu.ndarray):
- arrmean = um.true_divide(arrmean, rcount,
- out=arrmean, casting='unsafe', subok=False)
- else:
- arrmean = arrmean / float(rcount)
-
- # arr - arrmean
- x = arr - arrmean
-
- # (arr - arrmean) ** 2
- if arr.dtype.kind == 'c':
- x = um.multiply(x, um.conjugate(x), out=x).real
- else:
- x = um.multiply(x, x, out=x)
-
- # add.reduce((arr - arrmean) ** 2, axis)
- ret = um.add.reduce(x, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
-
- # add.reduce((arr - arrmean) ** 2, axis) / (n - ddof)
- if not keepdims and isinstance(rcount, mu.ndarray):
- rcount = rcount.squeeze(axis=axis)
- rcount -= ddof
- if isinstance(ret, mu.ndarray):
- ret = um.true_divide(ret, rcount,
- out=ret, casting='unsafe', subok=False)
- else:
- ret = ret / float(rcount)
-
- return ret
-
-def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
- ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
- keepdims=keepdims)
-
- if isinstance(ret, mu.ndarray):
- ret = um.sqrt(ret, out=ret)
- else:
- ret = um.sqrt(ret)
-
- return ret
diff --git a/lib_pypy/numpypy/core/arrayprint.py b/lib_pypy/numpypy/core/arrayprint.py
deleted file mode 100644
--- a/lib_pypy/numpypy/core/arrayprint.py
+++ /dev/null
@@ -1,750 +0,0 @@
-"""Array printing function
-
-$Id: arrayprint.py,v 1.9 2005/09/13 13:58:44 teoliphant Exp $
-"""
-__all__ = ["array2string", "set_printoptions", "get_printoptions"]
-__docformat__ = 'restructuredtext'
-
-#
-# Written by Konrad Hinsen <hinsenk at ere.umontreal.ca>
-# last revision: 1996-3-13
-# modified by Jim Hugunin 1997-3-3 for repr's and str's (and other details)
-# and by Perry Greenfield 2000-4-1 for numarray
-# and by Travis Oliphant 2005-8-22 for numpy
-
-import sys
-import numerictypes as _nt
-from umath import maximum, minimum, absolute, not_equal, isnan, isinf
-#from multiarray import format_longfloat, datetime_as_string, datetime_data
-from fromnumeric import ravel
-
-
-def product(x, y): return x*y
-
-_summaryEdgeItems = 3 # repr N leading and trailing items of each dimension
-_summaryThreshold = 1000 # total items > triggers array summarization
-
-_float_output_precision = 8
-_float_output_suppress_small = False
-_line_width = 75
-_nan_str = 'nan'
-_inf_str = 'inf'
-_formatter = None # formatting function for array elements
-
-if sys.version_info[0] >= 3:
- from functools import reduce
-
-def set_printoptions(precision=None, threshold=None, edgeitems=None,
- linewidth=None, suppress=None,
- nanstr=None, infstr=None,
- formatter=None):
- """
- Set printing options.
-
- These options determine the way floating point numbers, arrays and
- other NumPy objects are displayed.
-
- Parameters
- ----------
- precision : int, optional
- Number of digits of precision for floating point output (default 8).
- threshold : int, optional
- Total number of array elements which trigger summarization
- rather than full repr (default 1000).
- edgeitems : int, optional
- Number of array items in summary at beginning and end of
- each dimension (default 3).
- linewidth : int, optional
- The number of characters per line for the purpose of inserting
- line breaks (default 75).
- suppress : bool, optional
- Whether or not suppress printing of small floating point values
- using scientific notation (default False).
- nanstr : str, optional
- String representation of floating point not-a-number (default nan).
- infstr : str, optional
- String representation of floating point infinity (default inf).
- formatter : dict of callables, optional
- If not None, the keys should indicate the type(s) that the respective
- formatting function applies to. Callables should return a string.
- Types that are not specified (by their corresponding keys) are handled
- by the default formatters. Individual types for which a formatter
- can be set are::
-
- - 'bool'
- - 'int'
- - 'timedelta' : a `numpy.timedelta64`
- - 'datetime' : a `numpy.datetime64`
- - 'float'
- - 'longfloat' : 128-bit floats
- - 'complexfloat'
- - 'longcomplexfloat' : composed of two 128-bit floats
- - 'numpy_str' : types `numpy.string_` and `numpy.unicode_`
- - 'str' : all other strings
-
- Other keys that can be used to set a group of types at once are::
-
- - 'all' : sets all types
- - 'int_kind' : sets 'int'
- - 'float_kind' : sets 'float' and 'longfloat'
- - 'complex_kind' : sets 'complexfloat' and 'longcomplexfloat'
- - 'str_kind' : sets 'str' and 'numpystr'
-
- See Also
- --------
- get_printoptions, set_string_function, array2string
-
- Notes
- -----
- `formatter` is always reset with a call to `set_printoptions`.
-
- Examples
- --------
- Floating point precision can be set:
-
- >>> np.set_printoptions(precision=4)
- >>> print np.array([1.123456789])
- [ 1.1235]
-
- Long arrays can be summarised:
-
- >>> np.set_printoptions(threshold=5)
- >>> print np.arange(10)
- [0 1 2 ..., 7 8 9]
-
- Small results can be suppressed:
-
- >>> eps = np.finfo(float).eps
- >>> x = np.arange(4.)
- >>> x**2 - (x + eps)**2
- array([ -4.9304e-32, -4.4409e-16, 0.0000e+00, 0.0000e+00])
- >>> np.set_printoptions(suppress=True)
- >>> x**2 - (x + eps)**2
- array([-0., -0., 0., 0.])
-
- A custom formatter can be used to display array elements as desired:
-
- >>> np.set_printoptions(formatter={'all':lambda x: 'int: '+str(-x)})
- >>> x = np.arange(3)
- >>> x
- array([int: 0, int: -1, int: -2])
- >>> np.set_printoptions() # formatter gets reset
- >>> x
- array([0, 1, 2])
-
- To put back the default options, you can use:
-
- >>> np.set_printoptions(edgeitems=3,infstr='inf',
- ... linewidth=75, nanstr='nan', precision=8,
- ... suppress=False, threshold=1000, formatter=None)
- """
-
- global _summaryThreshold, _summaryEdgeItems, _float_output_precision, \
- _line_width, _float_output_suppress_small, _nan_str, _inf_str, \
- _formatter
- if linewidth is not None:
- _line_width = linewidth
- if threshold is not None:
- _summaryThreshold = threshold
- if edgeitems is not None:
- _summaryEdgeItems = edgeitems
- if precision is not None:
- _float_output_precision = precision
- if suppress is not None:
- _float_output_suppress_small = not not suppress
- if nanstr is not None:
- _nan_str = nanstr
- if infstr is not None:
- _inf_str = infstr
- _formatter = formatter
-
-def get_printoptions():
- """
- Return the current print options.
-
- Returns
- -------
- print_opts : dict
- Dictionary of current print options with keys
-
- - precision : int
- - threshold : int
- - edgeitems : int
- - linewidth : int
- - suppress : bool
- - nanstr : str
- - infstr : str
- - formatter : dict of callables
-
- For a full description of these options, see `set_printoptions`.
-
- See Also
- --------
- set_printoptions, set_string_function
-
- """
- d = dict(precision=_float_output_precision,
- threshold=_summaryThreshold,
- edgeitems=_summaryEdgeItems,
- linewidth=_line_width,
- suppress=_float_output_suppress_small,
- nanstr=_nan_str,
- infstr=_inf_str,
- formatter=_formatter)
- return d
-
-def _leading_trailing(a):
- import numeric as _nc
- if a.ndim == 1:
- if len(a) > 2*_summaryEdgeItems:
- b = _nc.concatenate((a[:_summaryEdgeItems],
- a[-_summaryEdgeItems:]))
- else:
- b = a
- else:
- if len(a) > 2*_summaryEdgeItems:
- l = [_leading_trailing(a[i]) for i in range(
- min(len(a), _summaryEdgeItems))]
- l.extend([_leading_trailing(a[-i]) for i in range(
- min(len(a), _summaryEdgeItems),0,-1)])
- else:
- l = [_leading_trailing(a[i]) for i in range(0, len(a))]
- b = _nc.concatenate(tuple(l))
- return b
-
-def _boolFormatter(x):
- if x:
- return ' True'
- else:
- return 'False'
-
-
-def repr_format(x):
- return repr(x)
-
-def _array2string(a, max_line_width, precision, suppress_small, separator=' ',
- prefix="", formatter=None):
-
- if max_line_width is None:
- max_line_width = _line_width
-
- if precision is None:
- precision = _float_output_precision
-
- if suppress_small is None:
- suppress_small = _float_output_suppress_small
-
- if formatter is None:
- formatter = _formatter
-
- if a.size > _summaryThreshold:
- summary_insert = "..., "
- data = _leading_trailing(a)
- else:
- summary_insert = ""
- data = ravel(a)
-
- formatdict = {'bool' : _boolFormatter,
- 'int' : IntegerFormat(data),
- 'float' : FloatFormat(data, precision, suppress_small),
- 'longfloat' : LongFloatFormat(precision),
- 'complexfloat' : ComplexFormat(data, precision,
- suppress_small),
- 'longcomplexfloat' : LongComplexFormat(precision),
- 'datetime' : DatetimeFormat(data),
- 'timedelta' : TimedeltaFormat(data),
- 'numpystr' : repr_format,
- 'str' : str}
-
- if formatter is not None:
- fkeys = [k for k in formatter.keys() if formatter[k] is not None]
- if 'all' in fkeys:
- for key in formatdict.keys():
- formatdict[key] = formatter['all']
- if 'int_kind' in fkeys:
- for key in ['int']:
- formatdict[key] = formatter['int_kind']
- if 'float_kind' in fkeys:
- for key in ['float', 'longfloat']:
- formatdict[key] = formatter['float_kind']
- if 'complex_kind' in fkeys:
- for key in ['complexfloat', 'longcomplexfloat']:
- formatdict[key] = formatter['complex_kind']
- if 'str_kind' in fkeys:
- for key in ['numpystr', 'str']:
- formatdict[key] = formatter['str_kind']
- for key in formatdict.keys():
- if key in fkeys:
- formatdict[key] = formatter[key]
-
- try:
- format_function = a._format
- msg = "The `_format` attribute is deprecated in Numpy 2.0 and " \
- "will be removed in 2.1. Use the `formatter` kw instead."
- import warnings
- warnings.warn(msg, DeprecationWarning)
- except AttributeError:
- # find the right formatting function for the array
- dtypeobj = a.dtype.type
- if issubclass(dtypeobj, _nt.bool_):
- format_function = formatdict['bool']
- elif issubclass(dtypeobj, _nt.integer):
- #if issubclass(dtypeobj, _nt.timedelta64):
- # format_function = formatdict['timedelta']
- #else:
- format_function = formatdict['int']
- elif issubclass(dtypeobj, _nt.floating):
- if hasattr(_nt, 'longfloat') and issubclass(dtypeobj, _nt.longfloat):
- format_function = formatdict['longfloat']
- else:
- format_function = formatdict['float']
- elif issubclass(dtypeobj, _nt.complexfloating):
- if hasattr(_nt, 'clongfloat') and issubclass(dtypeobj, _nt.clongfloat):
- format_function = formatdict['longcomplexfloat']
- else:
- format_function = formatdict['complexfloat']
- elif issubclass(dtypeobj, (_nt.unicode_, _nt.string_)):
- format_function = formatdict['numpystr']
- #elif issubclass(dtypeobj, _nt.datetime64):
- # format_function = formatdict['datetime']
- else:
- format_function = formatdict['str']
-
- # skip over "["
- next_line_prefix = " "
- # skip over array(
- next_line_prefix += " "*len(prefix)
-
- lst = _formatArray(a, format_function, len(a.shape), max_line_width,
- next_line_prefix, separator,
- _summaryEdgeItems, summary_insert)[:-1]
- return lst
-
-def _convert_arrays(obj):
- import numeric as _nc
- newtup = []
- for k in obj:
- if isinstance(k, _nc.ndarray):
- k = k.tolist()
- elif isinstance(k, tuple):
- k = _convert_arrays(k)
- newtup.append(k)
- return tuple(newtup)
-
-
-def array2string(a, max_line_width=None, precision=None,
- suppress_small=None, separator=' ', prefix="",
- style=repr, formatter=None):
- """
- Return a string representation of an array.
-
- Parameters
- ----------
- a : ndarray
- Input array.
- max_line_width : int, optional
- The maximum number of columns the string should span. Newline
- characters splits the string appropriately after array elements.
- precision : int, optional
- Floating point precision. Default is the current printing
- precision (usually 8), which can be altered using `set_printoptions`.
- suppress_small : bool, optional
- Represent very small numbers as zero. A number is "very small" if it
- is smaller than the current printing precision.
- separator : str, optional
- Inserted between elements.
- prefix : str, optional
- An array is typically printed as::
-
- 'prefix(' + array2string(a) + ')'
-
- The length of the prefix string is used to align the
- output correctly.
- style : function, optional
- A function that accepts an ndarray and returns a string. Used only
- when the shape of `a` is equal to ``()``, i.e. for 0-D arrays.
- formatter : dict of callables, optional
- If not None, the keys should indicate the type(s) that the respective
- formatting function applies to. Callables should return a string.
- Types that are not specified (by their corresponding keys) are handled
- by the default formatters. Individual types for which a formatter
- can be set are::
-
- - 'bool'
- - 'int'
- - 'timedelta' : a `numpy.timedelta64`
- - 'datetime' : a `numpy.datetime64`
- - 'float'
- - 'longfloat' : 128-bit floats
- - 'complexfloat'
- - 'longcomplexfloat' : composed of two 128-bit floats
- - 'numpy_str' : types `numpy.string_` and `numpy.unicode_`
- - 'str' : all other strings
-
- Other keys that can be used to set a group of types at once are::
-
- - 'all' : sets all types
- - 'int_kind' : sets 'int'
- - 'float_kind' : sets 'float' and 'longfloat'
- - 'complex_kind' : sets 'complexfloat' and 'longcomplexfloat'
- - 'str_kind' : sets 'str' and 'numpystr'
-
- Returns
- -------
- array_str : str
- String representation of the array.
-
- Raises
- ------
- TypeError : if a callable in `formatter` does not return a string.
-
- See Also
- --------
- array_str, array_repr, set_printoptions, get_printoptions
-
- Notes
- -----
- If a formatter is specified for a certain type, the `precision` keyword is
- ignored for that type.
-
- Examples
- --------
- >>> x = np.array([1e-16,1,2,3])
- >>> print np.array2string(x, precision=2, separator=',',
- ... suppress_small=True)
- [ 0., 1., 2., 3.]
-
- >>> x = np.arange(3.)
- >>> np.array2string(x, formatter={'float_kind':lambda x: "%.2f" % x})
- '[0.00 1.00 2.00]'
-
- >>> x = np.arange(3)
- >>> np.array2string(x, formatter={'int':lambda x: hex(x)})
- '[0x0L 0x1L 0x2L]'
-
- """
-
- if a.shape == ():
- x = a.item()
- try:
- lst = a._format(x)
- msg = "The `_format` attribute is deprecated in Numpy " \
- "2.0 and will be removed in 2.1. Use the " \
- "`formatter` kw instead."
- import warnings
- warnings.warn(msg, DeprecationWarning)
- except AttributeError:
- if isinstance(x, tuple):
- x = _convert_arrays(x)
- lst = style(x)
- elif reduce(product, a.shape) == 0:
- # treat as a null array if any of shape elements == 0
- lst = "[]"
- else:
- lst = _array2string(a, max_line_width, precision, suppress_small,
- separator, prefix, formatter=formatter)
- return lst
-
-def _extendLine(s, line, word, max_line_len, next_line_prefix):
- if len(line.rstrip()) + len(word.rstrip()) >= max_line_len:
- s += line.rstrip() + "\n"
- line = next_line_prefix
- line += word
- return s, line
-
-
-def _formatArray(a, format_function, rank, max_line_len,
- next_line_prefix, separator, edge_items, summary_insert):
- """formatArray is designed for two modes of operation:
-
- 1. Full output
-
- 2. Summarized output
-
- """
- if rank == 0:
- obj = a.item()
- if isinstance(obj, tuple):
- obj = _convert_arrays(obj)
- return str(obj)
-
- if summary_insert and 2*edge_items < len(a):
- leading_items, trailing_items, summary_insert1 = \
- edge_items, edge_items, summary_insert
- else:
- leading_items, trailing_items, summary_insert1 = 0, len(a), ""
-
- if rank == 1:
- s = ""
- line = next_line_prefix
- for i in xrange(leading_items):
- word = format_function(a[i]) + separator
- s, line = _extendLine(s, line, word, max_line_len, next_line_prefix)
-
- if summary_insert1:
- s, line = _extendLine(s, line, summary_insert1, max_line_len, next_line_prefix)
-
- for i in xrange(trailing_items, 1, -1):
- word = format_function(a[-i]) + separator
- s, line = _extendLine(s, line, word, max_line_len, next_line_prefix)
-
- word = format_function(a[-1])
- s, line = _extendLine(s, line, word, max_line_len, next_line_prefix)
- s += line + "]\n"
- s = '[' + s[len(next_line_prefix):]
- else:
- s = '['
- sep = separator.rstrip()
- for i in xrange(leading_items):
- if i > 0:
- s += next_line_prefix
- s += _formatArray(a[i], format_function, rank-1, max_line_len,
- " " + next_line_prefix, separator, edge_items,
- summary_insert)
- s = s.rstrip() + sep.rstrip() + '\n'*max(rank-1,1)
-
- if summary_insert1:
- s += next_line_prefix + summary_insert1 + "\n"
-
- for i in xrange(trailing_items, 1, -1):
- if leading_items or i != trailing_items:
- s += next_line_prefix
- s += _formatArray(a[-i], format_function, rank-1, max_line_len,
- " " + next_line_prefix, separator, edge_items,
- summary_insert)
- s = s.rstrip() + sep.rstrip() + '\n'*max(rank-1,1)
- if leading_items or trailing_items > 1:
- s += next_line_prefix
- s += _formatArray(a[-1], format_function, rank-1, max_line_len,
- " " + next_line_prefix, separator, edge_items,
- summary_insert).rstrip()+']\n'
- return s
-
-class FloatFormat(object):
- def __init__(self, data, precision, suppress_small, sign=False):
- self.precision = precision
- self.suppress_small = suppress_small
- self.sign = sign
- self.exp_format = False
- self.large_exponent = False
- self.max_str_len = 0
- try:
- self.fillFormat(data)
- except (TypeError, NotImplementedError):
- # if reduce(data) fails, this instance will not be called, just
- # instantiated in formatdict.
- pass
-
- def fillFormat(self, data):
- import numeric as _nc
- errstate = _nc.seterr(all='ignore')
- try:
- special = isnan(data) | isinf(data)
- valid = not_equal(data, 0) & ~special
- non_zero = absolute(data.compress(valid))
- if len(non_zero) == 0:
- max_val = 0.
- min_val = 0.
- else:
- max_val = maximum.reduce(non_zero)
- min_val = minimum.reduce(non_zero)
- if max_val >= 1.e8:
- self.exp_format = True
- if not self.suppress_small and (min_val < 0.0001
- or max_val/min_val > 1000.):
- self.exp_format = True
- finally:
- _nc.seterr(**errstate)
-
- if self.exp_format:
- self.large_exponent = 0 < min_val < 1e-99 or max_val >= 1e100
- self.max_str_len = 8 + self.precision
- if self.large_exponent:
- self.max_str_len += 1
- if self.sign:
- format = '%+'
- else:
- format = '%'
- format = format + '%d.%de' % (self.max_str_len, self.precision)
- else:
- format = '%%.%df' % (self.precision,)
- if len(non_zero):
- precision = max([_digits(x, self.precision, format)
- for x in non_zero])
- else:
- precision = 0
- precision = min(self.precision, precision)
- self.max_str_len = len(str(int(max_val))) + precision + 2
- if _nc.any(special):
- self.max_str_len = max(self.max_str_len,
- len(_nan_str),
- len(_inf_str)+1)
- if self.sign:
- format = '%#+'
- else:
- format = '%#'
- format = format + '%d.%df' % (self.max_str_len, precision)
-
- self.special_fmt = '%%%ds' % (self.max_str_len,)
- self.format = format
-
- def __call__(self, x, strip_zeros=True):
- import numeric as _nc
- err = _nc.seterr(invalid='ignore')
- try:
- if isnan(x):
- if self.sign:
- return self.special_fmt % ('+' + _nan_str,)
- else:
- return self.special_fmt % (_nan_str,)
- elif isinf(x):
- if x > 0:
- if self.sign:
- return self.special_fmt % ('+' + _inf_str,)
- else:
- return self.special_fmt % (_inf_str,)
- else:
- return self.special_fmt % ('-' + _inf_str,)
- finally:
- _nc.seterr(**err)
-
- s = self.format % x
- if self.large_exponent:
- # 3-digit exponent
- expsign = s[-3]
- if expsign == '+' or expsign == '-':
- s = s[1:-2] + '0' + s[-2:]
- elif self.exp_format:
- # 2-digit exponent
- if s[-3] == '0':
- s = ' ' + s[:-3] + s[-2:]
- elif strip_zeros:
- z = s.rstrip('0')
- s = z + ' '*(len(s)-len(z))
- return s
-
-
-def _digits(x, precision, format):
- s = format % x
- z = s.rstrip('0')
- return precision - len(s) + len(z)
-
-
-_MAXINT = sys.maxint
-_MININT = -sys.maxint-1
-class IntegerFormat(object):
- def __init__(self, data):
- try:
- max_str_len = max(len(str(maximum.reduce(data))),
- len(str(minimum.reduce(data))))
- self.format = '%' + str(max_str_len) + 'd'
- except (TypeError, NotImplementedError):
- # if reduce(data) fails, this instance will not be called, just
- # instantiated in formatdict.
- pass
- except ValueError:
- # this occurs when everything is NA
- pass
-
- def __call__(self, x):
- if _MININT < x < _MAXINT:
- return self.format % x
- else:
- return "%s" % x
-
-class LongFloatFormat(object):
- # XXX Have to add something to determine the width to use a la FloatFormat
- # Right now, things won't line up properly
- def __init__(self, precision, sign=False):
- self.precision = precision
- self.sign = sign
-
- def __call__(self, x):
- if isnan(x):
- if self.sign:
- return '+' + _nan_str
- else:
- return ' ' + _nan_str
- elif isinf(x):
- if x > 0:
- if self.sign:
- return '+' + _inf_str
- else:
- return ' ' + _inf_str
- else:
- return '-' + _inf_str
- elif x >= 0:
- if self.sign:
- return '+' + format_longfloat(x, self.precision)
- else:
- return ' ' + format_longfloat(x, self.precision)
- else:
- return format_longfloat(x, self.precision)
-
-
-class LongComplexFormat(object):
- def __init__(self, precision):
- self.real_format = LongFloatFormat(precision)
- self.imag_format = LongFloatFormat(precision, sign=True)
-
- def __call__(self, x):
- r = self.real_format(x.real)
- i = self.imag_format(x.imag)
- return r + i + 'j'
-
-
-class ComplexFormat(object):
- def __init__(self, x, precision, suppress_small):
- self.real_format = FloatFormat(x.real, precision, suppress_small)
- self.imag_format = FloatFormat(x.imag, precision, suppress_small,
- sign=True)
-
- def __call__(self, x):
- r = self.real_format(x.real, strip_zeros=False)
- i = self.imag_format(x.imag, strip_zeros=False)
- if not self.imag_format.exp_format:
- z = i.rstrip('0')
- i = z + 'j' + ' '*(len(i)-len(z))
- else:
- i = i + 'j'
- return r + i
-
-class DatetimeFormat(object):
- def __init__(self, x, unit=None,
- timezone=None, casting='same_kind'):
- # Get the unit from the dtype
- if unit is None:
- if x.dtype.kind == 'M':
- unit = datetime_data(x.dtype)[0]
- else:
- unit = 's'
-
- # If timezone is default, make it 'local' or 'UTC' based on the unit
- if timezone is None:
- # Date units -> UTC, time units -> local
- if unit in ('Y', 'M', 'W', 'D'):
- self.timezone = 'UTC'
- else:
- self.timezone = 'local'
- else:
- self.timezone = timezone
- self.unit = unit
- self.casting = casting
-
- def __call__(self, x):
- return "'%s'" % datetime_as_string(x,
- unit=self.unit,
- timezone=self.timezone,
- casting=self.casting)
-
-class TimedeltaFormat(object):
- def __init__(self, data):
- if data.dtype.kind == 'm':
- v = data.view('i8')
- max_str_len = max(len(str(maximum.reduce(v))),
- len(str(minimum.reduce(v))))
- self.format = '%' + str(max_str_len) + 'd'
-
- def __call__(self, x):
- return self.format % x.astype('i8')
-
diff --git a/lib_pypy/numpypy/core/fromnumeric.py b/lib_pypy/numpypy/core/fromnumeric.py
deleted file mode 100644
--- a/lib_pypy/numpypy/core/fromnumeric.py
+++ /dev/null
@@ -1,2431 +0,0 @@
-######################################################################
-# This is a copy of numpy/core/fromnumeric.py modified for numpypy
-######################################################################
-# Each name in __all__ was a function in 'numeric' that is now
-# a method in 'numpy'.
-# When the corresponding method is added to numpypy BaseArray
-# each function should be added as a module function
-# at the applevel
-# This can be as simple as doing the following
-#
-# def func(a, ...):
-# if not hasattr(a, 'func')
-# a = numpypy.array(a)
-# return a.func(...)
-#
-######################################################################
-
-import numpypy
-import _numpypy
-
-# Module containing non-deprecated functions borrowed from Numeric.
-__docformat__ = "restructuredtext en"
-
-# functions that are now methods
-__all__ = ['take', 'reshape', 'choose', 'repeat', 'put',
- 'swapaxes', 'transpose', 'sort', 'argsort', 'argmax', 'argmin',
- 'searchsorted', 'alen',
- 'resize', 'diagonal', 'trace', 'ravel', 'nonzero', 'shape',
- 'compress', 'clip', 'sum', 'product', 'prod', 'sometrue', 'alltrue',
- 'any', 'all', 'cumsum', 'cumproduct', 'cumprod', 'ptp', 'ndim',
- 'rank', 'size', 'around', 'round_', 'mean', 'std', 'var', 'squeeze',
- 'amax', 'amin',
- ]
-
-def take(a, indices, axis=None, out=None, mode='raise'):
- """
- Take elements from an array along an axis.
-
- This function does the same thing as "fancy" indexing (indexing arrays
- using arrays); however, it can be easier to use if you need elements
- along a given axis.
-
- Parameters
- ----------
- a : array_like
- The source array.
- indices : array_like
- The indices of the values to extract.
- axis : int, optional
- The axis over which to select values. By default, the flattened
- input array is used.
- out : ndarray, optional
- If provided, the result will be placed in this array. It should
- be of the appropriate shape and dtype.
- mode : {'raise', 'wrap', 'clip'}, optional
- Specifies how out-of-bounds indices will behave.
-
- * 'raise' -- raise an error (default)
- * 'wrap' -- wrap around
- * 'clip' -- clip to the range
-
- 'clip' mode means that all indices that are too large are replaced
- by the index that addresses the last element along that axis. Note
- that this disables indexing with negative numbers.
-
- Returns
- -------
- subarray : ndarray
- The returned array has the same type as `a`.
-
- See Also
- --------
- ndarray.take : equivalent method
-
- Examples
- --------
- >>> a = [4, 3, 5, 7, 6, 8]
- >>> indices = [0, 1, 4]
- >>> np.take(a, indices)
- array([4, 3, 6])
-
- In this example if `a` is an ndarray, "fancy" indexing can be used.
-
- >>> a = np.array(a)
- >>> a[indices]
- array([4, 3, 6])
-
- """
- raise NotImplementedError('Waiting on interp level method')
-
-
-# not deprecated --- copy if necessary, view otherwise
-def reshape(a, newshape, order='C'):
- """
- Gives a new shape to an array without changing its data.
-
- Parameters
- ----------
- a : array_like
- Array to be reshaped.
- newshape : int or tuple of ints
- The new shape should be compatible with the original shape. If
- an integer, then the result will be a 1-D array of that length.
- One shape dimension can be -1. In this case, the value is inferred
- from the length of the array and remaining dimensions.
- order : {'C', 'F', 'A'}, optional
- Determines whether the array data should be viewed as in C
- (row-major) order, FORTRAN (column-major) order, or the C/FORTRAN
- order should be preserved.
-
- Returns
- -------
- reshaped_array : ndarray
- This will be a new view object if possible; otherwise, it will
- be a copy.
-
-
- See Also
- --------
- ndarray.reshape : Equivalent method.
-
- Notes
- -----
-
- It is not always possible to change the shape of an array without
- copying the data. If you want an error to be raise if the data is copied,
- you should assign the new shape to the shape attribute of the array::
-
- >>> a = np.zeros((10, 2))
- # A transpose make the array non-contiguous
- >>> b = a.T
- # Taking a view makes it possible to modify the shape without modiying the
- # initial object.
- >>> c = b.view()
- >>> c.shape = (20)
- AttributeError: incompatible shape for a non-contiguous array
-
-
- Examples
- --------
- >>> a = np.array([[1,2,3], [4,5,6]])
- >>> np.reshape(a, 6)
- array([1, 2, 3, 4, 5, 6])
- >>> np.reshape(a, 6, order='F')
- array([1, 4, 2, 5, 3, 6])
-
- >>> np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2
- array([[1, 2],
- [3, 4],
- [5, 6]])
-
- """
- assert order == 'C'
- if not hasattr(a, 'reshape'):
- a = numpypy.array(a)
- return a.reshape(newshape)
-
-
-def choose(a, choices, out=None, mode='raise'):
- """
- Construct an array from an index array and a set of arrays to choose from.
-
- First of all, if confused or uncertain, definitely look at the Examples -
- in its full generality, this function is less simple than it might
- seem from the following code description (below ndi =
- `numpy.lib.index_tricks`):
-
- ``np.choose(a,c) == np.array([c[a[I]][I] for I in ndi.ndindex(a.shape)])``.
-
- But this omits some subtleties. Here is a fully general summary:
-
- Given an "index" array (`a`) of integers and a sequence of `n` arrays
- (`choices`), `a` and each choice array are first broadcast, as necessary,
- to arrays of a common shape; calling these *Ba* and *Bchoices[i], i =
- 0,...,n-1* we have that, necessarily, ``Ba.shape == Bchoices[i].shape``
- for each `i`. Then, a new array with shape ``Ba.shape`` is created as
- follows:
-
- * if ``mode=raise`` (the default), then, first of all, each element of
- `a` (and thus `Ba`) must be in the range `[0, n-1]`; now, suppose that
- `i` (in that range) is the value at the `(j0, j1, ..., jm)` position
- in `Ba` - then the value at the same position in the new array is the
- value in `Bchoices[i]` at that same position;
-
- * if ``mode=wrap``, values in `a` (and thus `Ba`) may be any (signed)
- integer; modular arithmetic is used to map integers outside the range
- `[0, n-1]` back into that range; and then the new array is constructed
- as above;
-
- * if ``mode=clip``, values in `a` (and thus `Ba`) may be any (signed)
- integer; negative integers are mapped to 0; values greater than `n-1`
- are mapped to `n-1`; and then the new array is constructed as above.
-
- Parameters
- ----------
- a : int array
- This array must contain integers in `[0, n-1]`, where `n` is the number
- of choices, unless ``mode=wrap`` or ``mode=clip``, in which cases any
- integers are permissible.
- choices : sequence of arrays
- Choice arrays. `a` and all of the choices must be broadcastable to the
- same shape. If `choices` is itself an array (not recommended), then
- its outermost dimension (i.e., the one corresponding to
- ``choices.shape[0]``) is taken as defining the "sequence".
- out : array, optional
- If provided, the result will be inserted into this array. It should
- be of the appropriate shape and dtype.
- mode : {'raise' (default), 'wrap', 'clip'}, optional
- Specifies how indices outside `[0, n-1]` will be treated:
-
- * 'raise' : an exception is raised
- * 'wrap' : value becomes value mod `n`
- * 'clip' : values < 0 are mapped to 0, values > n-1 are mapped to n-1
-
- Returns
- -------
- merged_array : array
- The merged result.
-
- Raises
- ------
- ValueError: shape mismatch
- If `a` and each choice array are not all broadcastable to the same
- shape.
-
- See Also
- --------
- ndarray.choose : equivalent method
-
- Notes
- -----
- To reduce the chance of misinterpretation, even though the following
- "abuse" is nominally supported, `choices` should neither be, nor be
- thought of as, a single array, i.e., the outermost sequence-like container
- should be either a list or a tuple.
-
- Examples
- --------
-
- >>> choices = [[0, 1, 2, 3], [10, 11, 12, 13],
- ... [20, 21, 22, 23], [30, 31, 32, 33]]
- >>> np.choose([2, 3, 1, 0], choices
- ... # the first element of the result will be the first element of the
- ... # third (2+1) "array" in choices, namely, 20; the second element
- ... # will be the second element of the fourth (3+1) choice array, i.e.,
- ... # 31, etc.
- ... )
- array([20, 31, 12, 3])
- >>> np.choose([2, 4, 1, 0], choices, mode='clip') # 4 goes to 3 (4-1)
- array([20, 31, 12, 3])
- >>> # because there are 4 choice arrays
- >>> np.choose([2, 4, 1, 0], choices, mode='wrap') # 4 goes to (4 mod 4)
- array([20, 1, 12, 3])
- >>> # i.e., 0
-
- A couple examples illustrating how choose broadcasts:
-
- >>> a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
- >>> choices = [-10, 10]
- >>> np.choose(a, choices)
- array([[ 10, -10, 10],
- [-10, 10, -10],
- [ 10, -10, 10]])
-
- >>> # With thanks to Anne Archibald
- >>> a = np.array([0, 1]).reshape((2,1,1))
- >>> c1 = np.array([1, 2, 3]).reshape((1,3,1))
- >>> c2 = np.array([-1, -2, -3, -4, -5]).reshape((1,1,5))
- >>> np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2
- array([[[ 1, 1, 1, 1, 1],
- [ 2, 2, 2, 2, 2],
- [ 3, 3, 3, 3, 3]],
- [[-1, -2, -3, -4, -5],
- [-1, -2, -3, -4, -5],
- [-1, -2, -3, -4, -5]]])
-
- """
- return _numpypy.choose(a, choices, out, mode)
-
-
-def repeat(a, repeats, axis=None):
- """
- Repeat elements of an array.
-
- Parameters
- ----------
- a : array_like
- Input array.
- repeats : {int, array of ints}
- The number of repetitions for each element. `repeats` is broadcasted
- to fit the shape of the given axis.
- axis : int, optional
- The axis along which to repeat values. By default, use the
- flattened input array, and return a flat output array.
-
- Returns
- -------
- repeated_array : ndarray
- Output array which has the same shape as `a`, except along
- the given axis.
-
- See Also
- --------
- tile : Tile an array.
-
- Examples
- --------
- >>> x = np.array([[1,2],[3,4]])
- >>> np.repeat(x, 2)
- array([1, 1, 2, 2, 3, 3, 4, 4])
- >>> np.repeat(x, 3, axis=1)
- array([[1, 1, 1, 2, 2, 2],
- [3, 3, 3, 4, 4, 4]])
- >>> np.repeat(x, [1, 2], axis=0)
- array([[1, 2],
- [3, 4],
- [3, 4]])
-
- """
- return _numpypy.repeat(a, repeats, axis)
-
-
-def put(a, ind, v, mode='raise'):
- """
- Replaces specified elements of an array with given values.
-
- The indexing works on the flattened target array. `put` is roughly
- equivalent to:
-
- ::
-
- a.flat[ind] = v
-
- Parameters
- ----------
- a : ndarray
- Target array.
- ind : array_like
- Target indices, interpreted as integers.
- v : array_like
- Values to place in `a` at target indices. If `v` is shorter than
- `ind` it will be repeated as necessary.
- mode : {'raise', 'wrap', 'clip'}, optional
- Specifies how out-of-bounds indices will behave.
-
- * 'raise' -- raise an error (default)
- * 'wrap' -- wrap around
- * 'clip' -- clip to the range
-
- 'clip' mode means that all indices that are too large are replaced
- by the index that addresses the last element along that axis. Note
- that this disables indexing with negative numbers.
-
- See Also
- --------
- putmask, place
-
- Examples
- --------
- >>> a = np.arange(5)
- >>> np.put(a, [0, 2], [-44, -55])
- >>> a
- array([-44, 1, -55, 3, 4])
-
- >>> a = np.arange(5)
- >>> np.put(a, 22, -5, mode='clip')
- >>> a
- array([ 0, 1, 2, 3, -5])
-
- """
- raise NotImplementedError('Waiting on interp level method')
-
-
-def swapaxes(a, axis1, axis2):
- """
- Interchange two axes of an array.
-
- Parameters
- ----------
- a : array_like
- Input array.
- axis1 : int
- First axis.
- axis2 : int
- Second axis.
-
- Returns
- -------
- a_swapped : ndarray
- If `a` is an ndarray, then a view of `a` is returned; otherwise
- a new array is created.
-
- Examples
- --------
- >>> x = np.array([[1,2,3]])
- >>> np.swapaxes(x,0,1)
- array([[1],
- [2],
- [3]])
More information about the pypy-commit
mailing list