[pypy-commit] pypy default: merge heads
arigo
noreply at buildbot.pypy.org
Fri Apr 20 14:06:45 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r54579:9e290b906bf5
Date: 2012-04-20 14:05 +0200
http://bitbucket.org/pypy/pypy/changeset/9e290b906bf5/
Log: merge heads
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
@@ -30,6 +30,7 @@
'isna': 'interp_numarray.isna',
'concatenate': 'interp_numarray.concatenate',
'repeat': 'interp_numarray.repeat',
+ 'where': 'interp_arrayops.where',
'set_string_function': 'appbridge.set_string_function',
diff --git a/pypy/module/micronumpy/interp_arrayops.py b/pypy/module/micronumpy/interp_arrayops.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/interp_arrayops.py
@@ -0,0 +1,90 @@
+
+from pypy.module.micronumpy.interp_numarray import convert_to_array,\
+ VirtualArray
+from pypy.module.micronumpy import signature
+
+class WhereArray(VirtualArray):
+ def __init__(self, arr, x, y):
+ self.arr = arr
+ self.x = x
+ self.y = y
+ VirtualArray.__init__(self, 'where', arr.shape[:],
+ x.find_dtype())
+
+ def create_sig(self):
+ if self.forced_result is not None:
+ return self.forced_result.create_sig()
+ return signature.WhereSignature(self.res_dtype, self.arr.find_dtype(),
+ self.arr.create_sig(),
+ self.x.create_sig(),
+ self.y.create_sig())
+
+ def _del_sources(self):
+ self.arr = None
+ self.x = None
+ self.y = None
+
+def where(space, w_arr, w_x, w_y):
+ """where(condition, [x, y])
+
+ Return elements, either from `x` or `y`, depending on `condition`.
+
+ If only `condition` is given, return ``condition.nonzero()``.
+
+ Parameters
+ ----------
+ condition : array_like, bool
+ When True, yield `x`, otherwise yield `y`.
+ x, y : array_like, optional
+ Values from which to choose. `x` and `y` need to have the same
+ shape as `condition`.
+
+ Returns
+ -------
+ out : ndarray or tuple of ndarrays
+ If both `x` and `y` are specified, the output array contains
+ elements of `x` where `condition` is True, and elements from
+ `y` elsewhere.
+
+ If only `condition` is given, return the tuple
+ ``condition.nonzero()``, the indices where `condition` is True.
+
+ See Also
+ --------
+ nonzero, choose
+
+ Notes
+ -----
+ If `x` and `y` are given and input arrays are 1-D, `where` is
+ equivalent to::
+
+ [xv if c else yv for (c,xv,yv) in zip(condition,x,y)]
+
+ Examples
+ --------
+ >>> np.where([[True, False], [True, True]],
+ ... [[1, 2], [3, 4]],
+ ... [[9, 8], [7, 6]])
+ array([[1, 8],
+ [3, 4]])
+
+ >>> np.where([[0, 1], [1, 0]])
+ (array([0, 1]), array([1, 0]))
+
+ >>> x = np.arange(9.).reshape(3, 3)
+ >>> np.where( x > 5 )
+ (array([2, 2, 2]), array([0, 1, 2]))
+ >>> x[np.where( x > 3.0 )] # Note: result is 1D.
+ array([ 4., 5., 6., 7., 8.])
+ >>> np.where(x < 5, x, -1) # Note: broadcasting.
+ array([[ 0., 1., 2.],
+ [ 3., 4., -1.],
+ [-1., -1., -1.]])
+
+
+ NOTE: support for not passing x and y is unsupported
+ """
+ arr = convert_to_array(space, w_arr)
+ x = convert_to_array(space, w_x)
+ y = convert_to_array(space, w_y)
+ return WhereArray(arr, x, y)
diff --git a/pypy/module/micronumpy/signature.py b/pypy/module/micronumpy/signature.py
--- a/pypy/module/micronumpy/signature.py
+++ b/pypy/module/micronumpy/signature.py
@@ -498,3 +498,63 @@
arr.left.setitem(iterator.offset, value)
def debug_repr(self):
return 'AxisReduceSig(%s, %s)' % (self.name, self.right.debug_repr())
+
+class WhereSignature(Signature):
+ _immutable_fields_ = ['dtype', 'arrdtype', 'arrsig', 'xsig', 'ysig']
+
+ def __init__(self, dtype, arrdtype, arrsig, xsig, ysig):
+ self.dtype = dtype
+ self.arrdtype = arrdtype
+ self.arrsig = arrsig
+ self.xsig = xsig
+ self.ysig = ysig
+
+ def hash(self):
+ return (intmask(self.arrsig.hash() << 1) ^
+ intmask(self.xsig.hash() << 2) ^
+ intmask(self.ysig.hash() << 3))
+
+ def eq(self, other, compare_array_no=True):
+ if type(self) is not type(other):
+ return False
+ assert isinstance(other, WhereSignature)
+ return (self.arrsig.eq(other.arrsig, compare_array_no) and
+ self.xsig.eq(other.xsig, compare_array_no) and
+ self.ysig.eq(other.ysig, compare_array_no))
+
+ def _invent_array_numbering(self, arr, cache):
+ from pypy.module.micronumpy.interp_arrayops import WhereArray
+ assert isinstance(arr, WhereArray)
+ self.arrsig._invent_array_numbering(arr.arr, cache)
+ self.xsig._invent_array_numbering(arr.x, cache)
+ self.ysig._invent_array_numbering(arr.y, cache)
+
+ def _invent_numbering(self, cache, allnumbers):
+ self.arrsig._invent_numbering(cache, allnumbers)
+ self.xsig._invent_numbering(cache, allnumbers)
+ self.ysig._invent_numbering(cache, allnumbers)
+
+ def _create_iter(self, iterlist, arraylist, arr, transforms):
+ from pypy.module.micronumpy.interp_arrayops import WhereArray
+
+ assert isinstance(arr, WhereArray)
+ # XXX this does not support broadcasting correctly
+ self.arrsig._create_iter(iterlist, arraylist, arr.arr, transforms)
+ self.xsig._create_iter(iterlist, arraylist, arr.x, transforms)
+ self.ysig._create_iter(iterlist, arraylist, arr.y, transforms)
+
+ def eval(self, frame, arr):
+ from pypy.module.micronumpy.interp_arrayops import WhereArray
+ assert isinstance(arr, WhereArray)
+ lhs = self.xsig.eval(frame, arr.x).convert_to(self.dtype)
+ rhs = self.ysig.eval(frame, arr.y).convert_to(self.dtype)
+ w_val = self.arrsig.eval(frame, arr.arr)
+ if self.arrdtype.itemtype.bool(w_val):
+ return lhs
+ else:
+ return rhs
+
+ def debug_repr(self):
+ return 'Where(%s, %s, %s)' % (self.arrsig.debug_repr(),
+ self.xsig.debug_repr(),
+ self.ysig.debug_repr())
diff --git a/pypy/module/micronumpy/test/test_arrayops.py b/pypy/module/micronumpy/test/test_arrayops.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/test/test_arrayops.py
@@ -0,0 +1,9 @@
+
+from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+
+class AppTestNumSupport(BaseNumpyAppTest):
+ def test_where(self):
+ from _numpypy import where, ones, zeros, array
+ a = [1, 2, 3, 0, -3]
+ a = where(array(a) > 0, ones(5), zeros(5))
+ assert (a == [1, 1, 1, 0, 0]).all()
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
@@ -1,9 +1,8 @@
import py
-from pypy.conftest import gettestobjspace, option
+from pypy.conftest import option
from pypy.interpreter.error import OperationError
-from pypy.module.micronumpy import signature
from pypy.module.micronumpy.appbridge import get_appbridge_cache
from pypy.module.micronumpy.interp_iter import Chunk, Chunks
from pypy.module.micronumpy.interp_numarray import W_NDimArray, shape_agreement
diff --git a/pypy/module/rctime/interp_time.py b/pypy/module/rctime/interp_time.py
--- a/pypy/module/rctime/interp_time.py
+++ b/pypy/module/rctime/interp_time.py
@@ -24,10 +24,9 @@
from pypy.module.thread import ll_thread as thread
eci = ExternalCompilationInfo(
+ includes = ['windows.h'],
post_include_bits = ["BOOL pypy_timemodule_setCtrlHandler(HANDLE event);"],
separate_module_sources=['''
- #include <windows.h>
-
static HANDLE interrupt_event;
static BOOL WINAPI CtrlHandlerRoutine(
diff --git a/pypy/module/zipimport/interp_zipimport.py b/pypy/module/zipimport/interp_zipimport.py
--- a/pypy/module/zipimport/interp_zipimport.py
+++ b/pypy/module/zipimport/interp_zipimport.py
@@ -229,7 +229,11 @@
startpos = fullname.rfind('.') + 1 # 0 when not found
assert startpos >= 0
subname = fullname[startpos:]
- return self.prefix + subname.replace('.', '/')
+ if ZIPSEP == os.path.sep:
+ return self.prefix + subname.replace('.', '/')
+ else:
+ return self.prefix.replace(os.path.sep, ZIPSEP) + \
+ subname.replace('.', '/')
def make_co_filename(self, filename):
"""
diff --git a/pypy/module/zipimport/test/test_zipimport.py b/pypy/module/zipimport/test/test_zipimport.py
--- a/pypy/module/zipimport/test/test_zipimport.py
+++ b/pypy/module/zipimport/test/test_zipimport.py
@@ -313,13 +313,11 @@
assert z.get_filename("package") == mod.__file__
def test_subdirectory_twice(self):
- import os, zipimport
+ #import os, zipimport
self.writefile("package/__init__.py", "")
self.writefile("package/subpackage/__init__.py", "")
self.writefile("package/subpackage/foo.py", "")
- import sys
- print sys.path
mod = __import__('package.subpackage.foo', None, None, [])
assert mod
More information about the pypy-commit
mailing list