[pypy-commit] pypy numppy-flatitter: merged default in
alex_gaynor
noreply at buildbot.pypy.org
Tue Jan 24 17:45:12 CET 2012
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numppy-flatitter
Changeset: r51730:9d3652817442
Date: 2012-01-23 16:18 -0500
http://bitbucket.org/pypy/pypy/changeset/9d3652817442/
Log: merged default in
diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -20,6 +20,8 @@
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
+#
+# Note: This software has been modified for use in PyPy.
from ctypes import c_void_p, c_int, c_double, c_int64, c_char_p, cdll
from ctypes import POINTER, byref, string_at, CFUNCTYPE, cast
@@ -27,7 +29,6 @@
from collections import OrderedDict
import datetime
import sys
-import time
import weakref
from threading import _get_ident as thread_get_ident
@@ -606,7 +607,7 @@
def authorizer(userdata, action, arg1, arg2, dbname, source):
try:
return int(callback(action, arg1, arg2, dbname, source))
- except Exception, e:
+ except Exception:
return SQLITE_DENY
c_authorizer = AUTHORIZER(authorizer)
@@ -653,7 +654,7 @@
if not aggregate_ptr[0]:
try:
aggregate = cls()
- except Exception, e:
+ except Exception:
msg = ("user-defined aggregate's '__init__' "
"method raised error")
sqlite.sqlite3_result_error(context, msg, len(msg))
@@ -667,7 +668,7 @@
params = _convert_params(context, argc, c_params)
try:
aggregate.step(*params)
- except Exception, e:
+ except Exception:
msg = ("user-defined aggregate's 'step' "
"method raised error")
sqlite.sqlite3_result_error(context, msg, len(msg))
@@ -683,7 +684,7 @@
aggregate = self.aggregate_instances[aggregate_ptr[0]]
try:
val = aggregate.finalize()
- except Exception, e:
+ except Exception:
msg = ("user-defined aggregate's 'finalize' "
"method raised error")
sqlite.sqlite3_result_error(context, msg, len(msg))
@@ -771,7 +772,7 @@
self.statement.item = None
self.statement.exhausted = True
- if self.statement.kind == DML or self.statement.kind == DDL:
+ if self.statement.kind == DML:
self.statement.reset()
self.rowcount = -1
@@ -791,7 +792,7 @@
if self.statement.kind == DML:
self.connection._begin()
else:
- raise ProgrammingError, "executemany is only for DML statements"
+ raise ProgrammingError("executemany is only for DML statements")
self.rowcount = 0
for params in many_params:
@@ -861,8 +862,6 @@
except StopIteration:
return None
- return nextrow
-
def fetchmany(self, size=None):
self._check_closed()
self._check_reset()
@@ -915,7 +914,7 @@
def __init__(self, connection, sql):
self.statement = None
if not isinstance(sql, str):
- raise ValueError, "sql must be a string"
+ raise ValueError("sql must be a string")
self.con = connection
self.sql = sql # DEBUG ONLY
first_word = self._statement_kind = sql.lstrip().split(" ")[0].upper()
@@ -944,8 +943,8 @@
raise self.con._get_exception(ret)
self.con._remember_statement(self)
if _check_remaining_sql(next_char.value):
- raise Warning, "One and only one statement required: %r" % (
- next_char.value,)
+ raise Warning("One and only one statement required: %r" % (
+ next_char.value,))
# sql_char should remain alive until here
self._build_row_cast_map()
@@ -1016,7 +1015,7 @@
elif type(param) is buffer:
sqlite.sqlite3_bind_blob(self.statement, idx, str(param), len(param), SQLITE_TRANSIENT)
else:
- raise InterfaceError, "parameter type %s is not supported" % str(type(param))
+ raise InterfaceError("parameter type %s is not supported" % str(type(param)))
def set_params(self, params):
ret = sqlite.sqlite3_reset(self.statement)
@@ -1045,11 +1044,11 @@
for idx in range(1, sqlite.sqlite3_bind_parameter_count(self.statement) + 1):
param_name = sqlite.sqlite3_bind_parameter_name(self.statement, idx)
if param_name is None:
- raise ProgrammingError, "need named parameters"
+ raise ProgrammingError("need named parameters")
param_name = param_name[1:]
try:
param = params[param_name]
- except KeyError, e:
+ except KeyError:
raise ProgrammingError("missing parameter '%s'" %param)
self.set_param(idx, param)
@@ -1260,7 +1259,7 @@
params = _convert_params(context, nargs, c_params)
try:
val = real_cb(*params)
- except Exception, e:
+ except Exception:
msg = "user-defined function raised exception"
sqlite.sqlite3_result_error(context, msg, len(msg))
else:
diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -13,7 +13,7 @@
Sources for time zone and DST data: http://www.twinsun.com/tz/tz-link.htm
This was originally copied from the sandbox of the CPython CVS repository.
-Thanks to Tim Peters for suggesting using it.
+Thanks to Tim Peters for suggesting using it.
"""
import time as _time
@@ -271,6 +271,8 @@
raise ValueError("%s()=%d, must be in -1439..1439" % (name, offset))
def _check_date_fields(year, month, day):
+ if not isinstance(year, (int, long)):
+ raise TypeError('int expected')
if not MINYEAR <= year <= MAXYEAR:
raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year)
if not 1 <= month <= 12:
@@ -280,6 +282,8 @@
raise ValueError('day must be in 1..%d' % dim, day)
def _check_time_fields(hour, minute, second, microsecond):
+ if not isinstance(hour, (int, long)):
+ raise TypeError('int expected')
if not 0 <= hour <= 23:
raise ValueError('hour must be in 0..23', hour)
if not 0 <= minute <= 59:
@@ -543,61 +547,76 @@
self = object.__new__(cls)
- self.__days = d
- self.__seconds = s
- self.__microseconds = us
+ self._days = d
+ self._seconds = s
+ self._microseconds = us
if abs(d) > 999999999:
raise OverflowError("timedelta # of days is too large: %d" % d)
return self
def __repr__(self):
- if self.__microseconds:
+ if self._microseconds:
return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__,
- self.__days,
- self.__seconds,
- self.__microseconds)
- if self.__seconds:
+ self._days,
+ self._seconds,
+ self._microseconds)
+ if self._seconds:
return "%s(%d, %d)" % ('datetime.' + self.__class__.__name__,
- self.__days,
- self.__seconds)
- return "%s(%d)" % ('datetime.' + self.__class__.__name__, self.__days)
+ self._days,
+ self._seconds)
+ return "%s(%d)" % ('datetime.' + self.__class__.__name__, self._days)
def __str__(self):
- mm, ss = divmod(self.__seconds, 60)
+ mm, ss = divmod(self._seconds, 60)
hh, mm = divmod(mm, 60)
s = "%d:%02d:%02d" % (hh, mm, ss)
- if self.__days:
+ if self._days:
def plural(n):
return n, abs(n) != 1 and "s" or ""
- s = ("%d day%s, " % plural(self.__days)) + s
- if self.__microseconds:
- s = s + ".%06d" % self.__microseconds
+ s = ("%d day%s, " % plural(self._days)) + s
+ if self._microseconds:
+ s = s + ".%06d" % self._microseconds
return s
- days = property(lambda self: self.__days, doc="days")
- seconds = property(lambda self: self.__seconds, doc="seconds")
- microseconds = property(lambda self: self.__microseconds,
- doc="microseconds")
-
def total_seconds(self):
return ((self.days * 86400 + self.seconds) * 10**6
+ self.microseconds) / 1e6
+ # Read-only field accessors
+ @property
+ def days(self):
+ """days"""
+ return self._days
+
+ @property
+ def seconds(self):
+ """seconds"""
+ return self._seconds
+
+ @property
+ def microseconds(self):
+ """microseconds"""
+ return self._microseconds
+
def __add__(self, other):
if isinstance(other, timedelta):
# for CPython compatibility, we cannot use
# our __class__ here, but need a real timedelta
- return timedelta(self.__days + other.__days,
- self.__seconds + other.__seconds,
- self.__microseconds + other.__microseconds)
+ return timedelta(self._days + other._days,
+ self._seconds + other._seconds,
+ self._microseconds + other._microseconds)
return NotImplemented
__radd__ = __add__
def __sub__(self, other):
if isinstance(other, timedelta):
- return self + -other
+ # for CPython compatibility, we cannot use
+ # our __class__ here, but need a real timedelta
+ return timedelta(self._days - other._days,
+ self._seconds - other._seconds,
+ self._microseconds - other._microseconds)
return NotImplemented
def __rsub__(self, other):
@@ -606,17 +625,17 @@
return NotImplemented
def __neg__(self):
- # for CPython compatibility, we cannot use
- # our __class__ here, but need a real timedelta
- return timedelta(-self.__days,
- -self.__seconds,
- -self.__microseconds)
+ # for CPython compatibility, we cannot use
+ # our __class__ here, but need a real timedelta
+ return timedelta(-self._days,
+ -self._seconds,
+ -self._microseconds)
def __pos__(self):
return self
def __abs__(self):
- if self.__days < 0:
+ if self._days < 0:
return -self
else:
return self
@@ -625,81 +644,81 @@
if isinstance(other, (int, long)):
# for CPython compatibility, we cannot use
# our __class__ here, but need a real timedelta
- return timedelta(self.__days * other,
- self.__seconds * other,
- self.__microseconds * other)
+ return timedelta(self._days * other,
+ self._seconds * other,
+ self._microseconds * other)
return NotImplemented
__rmul__ = __mul__
def __div__(self, other):
if isinstance(other, (int, long)):
- usec = ((self.__days * (24*3600L) + self.__seconds) * 1000000 +
- self.__microseconds)
+ usec = ((self._days * (24*3600L) + self._seconds) * 1000000 +
+ self._microseconds)
return timedelta(0, 0, usec // other)
return NotImplemented
__floordiv__ = __div__
- # Comparisons.
+ # Comparisons of timedelta objects with other.
def __eq__(self, other):
if isinstance(other, timedelta):
- return self.__cmp(other) == 0
+ return self._cmp(other) == 0
else:
return False
def __ne__(self, other):
if isinstance(other, timedelta):
- return self.__cmp(other) != 0
+ return self._cmp(other) != 0
else:
return True
def __le__(self, other):
if isinstance(other, timedelta):
- return self.__cmp(other) <= 0
+ return self._cmp(other) <= 0
else:
_cmperror(self, other)
def __lt__(self, other):
if isinstance(other, timedelta):
- return self.__cmp(other) < 0
+ return self._cmp(other) < 0
else:
_cmperror(self, other)
def __ge__(self, other):
if isinstance(other, timedelta):
- return self.__cmp(other) >= 0
+ return self._cmp(other) >= 0
else:
_cmperror(self, other)
def __gt__(self, other):
if isinstance(other, timedelta):
- return self.__cmp(other) > 0
+ return self._cmp(other) > 0
else:
_cmperror(self, other)
- def __cmp(self, other):
+ def _cmp(self, other):
assert isinstance(other, timedelta)
- return cmp(self.__getstate(), other.__getstate())
+ return cmp(self._getstate(), other._getstate())
def __hash__(self):
- return hash(self.__getstate())
+ return hash(self._getstate())
def __nonzero__(self):
- return (self.__days != 0 or
- self.__seconds != 0 or
- self.__microseconds != 0)
+ return (self._days != 0 or
+ self._seconds != 0 or
+ self._microseconds != 0)
# Pickle support.
__safe_for_unpickling__ = True # For Python 2.2
- def __getstate(self):
- return (self.__days, self.__seconds, self.__microseconds)
+ def _getstate(self):
+ return (self._days, self._seconds, self._microseconds)
def __reduce__(self):
- return (self.__class__, self.__getstate())
+ return (self.__class__, self._getstate())
timedelta.min = timedelta(-999999999)
timedelta.max = timedelta(days=999999999, hours=23, minutes=59, seconds=59,
@@ -749,25 +768,26 @@
return self
_check_date_fields(year, month, day)
self = object.__new__(cls)
- self.__year = year
- self.__month = month
- self.__day = day
+ self._year = year
+ self._month = month
+ self._day = day
return self
# Additional constructors
+ @classmethod
def fromtimestamp(cls, t):
"Construct a date from a POSIX timestamp (like time.time())."
y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
return cls(y, m, d)
- fromtimestamp = classmethod(fromtimestamp)
+ @classmethod
def today(cls):
"Construct a date from time.time()."
t = _time.time()
return cls.fromtimestamp(t)
- today = classmethod(today)
+ @classmethod
def fromordinal(cls, n):
"""Contruct a date from a proleptic Gregorian ordinal.
@@ -776,16 +796,24 @@
"""
y, m, d = _ord2ymd(n)
return cls(y, m, d)
- fromordinal = classmethod(fromordinal)
# Conversions to string
def __repr__(self):
- "Convert to formal string, for repr()."
+ """Convert to formal string, for repr().
+
+ >>> dt = datetime(2010, 1, 1)
+ >>> repr(dt)
+ 'datetime.datetime(2010, 1, 1, 0, 0)'
+
+ >>> dt = datetime(2010, 1, 1, tzinfo=timezone.utc)
+ >>> repr(dt)
+ 'datetime.datetime(2010, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)'
+ """
return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__,
- self.__year,
- self.__month,
- self.__day)
+ self._year,
+ self._month,
+ self._day)
# XXX These shouldn't depend on time.localtime(), because that
# clips the usable dates to [1970 .. 2038). At least ctime() is
# easily done without using strftime() -- that's better too because
@@ -793,12 +821,20 @@
def ctime(self):
"Format a la ctime()."
- return tmxxx(self.__year, self.__month, self.__day).ctime()
+ return tmxxx(self._year, self._month, self._day).ctime()
def strftime(self, fmt):
"Format using strftime()."
return _wrap_strftime(self, fmt, self.timetuple())
+ def __format__(self, fmt):
+ if not isinstance(fmt, (str, unicode)):
+ raise ValueError("__format__ excepts str or unicode, not %s" %
+ fmt.__class__.__name__)
+ if len(fmt) != 0:
+ return self.strftime(fmt)
+ return str(self)
+
def isoformat(self):
"""Return the date formatted according to ISO.
@@ -808,29 +844,31 @@
- http://www.w3.org/TR/NOTE-datetime
- http://www.cl.cam.ac.uk/~mgk25/iso-time.html
"""
- return "%04d-%02d-%02d" % (self.__year, self.__month, self.__day)
+ return "%04d-%02d-%02d" % (self._year, self._month, self._day)
__str__ = isoformat
- def __format__(self, format):
- if not isinstance(format, (str, unicode)):
- raise ValueError("__format__ excepts str or unicode, not %s" %
- format.__class__.__name__)
- if not format:
- return str(self)
- return self.strftime(format)
+ # Read-only field accessors
+ @property
+ def year(self):
+ """year (1-9999)"""
+ return self._year
- # Read-only field accessors
- year = property(lambda self: self.__year,
- doc="year (%d-%d)" % (MINYEAR, MAXYEAR))
- month = property(lambda self: self.__month, doc="month (1-12)")
- day = property(lambda self: self.__day, doc="day (1-31)")
+ @property
+ def month(self):
+ """month (1-12)"""
+ return self._month
+
+ @property
+ def day(self):
+ """day (1-31)"""
+ return self._day
# Standard conversions, __cmp__, __hash__ (and helpers)
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
- return _build_struct_time(self.__year, self.__month, self.__day,
+ return _build_struct_time(self._year, self._month, self._day,
0, 0, 0, -1)
def toordinal(self):
@@ -839,24 +877,24 @@
January 1 of year 1 is day 1. Only the year, month and day values
contribute to the result.
"""
- return _ymd2ord(self.__year, self.__month, self.__day)
+ return _ymd2ord(self._year, self._month, self._day)
def replace(self, year=None, month=None, day=None):
"""Return a new date with new values for the specified fields."""
if year is None:
- year = self.__year
+ year = self._year
if month is None:
- month = self.__month
+ month = self._month
if day is None:
- day = self.__day
+ day = self._day
_check_date_fields(year, month, day)
return date(year, month, day)
- # Comparisons.
+ # Comparisons of date objects with other.
def __eq__(self, other):
if isinstance(other, date):
- return self.__cmp(other) == 0
+ return self._cmp(other) == 0
elif hasattr(other, "timetuple"):
return NotImplemented
else:
@@ -864,7 +902,7 @@
def __ne__(self, other):
if isinstance(other, date):
- return self.__cmp(other) != 0
+ return self._cmp(other) != 0
elif hasattr(other, "timetuple"):
return NotImplemented
else:
@@ -872,7 +910,7 @@
def __le__(self, other):
if isinstance(other, date):
- return self.__cmp(other) <= 0
+ return self._cmp(other) <= 0
elif hasattr(other, "timetuple"):
return NotImplemented
else:
@@ -880,7 +918,7 @@
def __lt__(self, other):
if isinstance(other, date):
- return self.__cmp(other) < 0
+ return self._cmp(other) < 0
elif hasattr(other, "timetuple"):
return NotImplemented
else:
@@ -888,7 +926,7 @@
def __ge__(self, other):
if isinstance(other, date):
- return self.__cmp(other) >= 0
+ return self._cmp(other) >= 0
elif hasattr(other, "timetuple"):
return NotImplemented
else:
@@ -896,21 +934,21 @@
def __gt__(self, other):
if isinstance(other, date):
- return self.__cmp(other) > 0
+ return self._cmp(other) > 0
elif hasattr(other, "timetuple"):
return NotImplemented
else:
_cmperror(self, other)
- def __cmp(self, other):
+ def _cmp(self, other):
assert isinstance(other, date)
- y, m, d = self.__year, self.__month, self.__day
- y2, m2, d2 = other.__year, other.__month, other.__day
+ y, m, d = self._year, self._month, self._day
+ y2, m2, d2 = other._year, other._month, other._day
return cmp((y, m, d), (y2, m2, d2))
def __hash__(self):
"Hash."
- return hash(self.__getstate())
+ return hash(self._getstate())
# Computations
@@ -922,9 +960,9 @@
def __add__(self, other):
"Add a date to a timedelta."
if isinstance(other, timedelta):
- t = tmxxx(self.__year,
- self.__month,
- self.__day + other.days)
+ t = tmxxx(self._year,
+ self._month,
+ self._day + other.days)
self._checkOverflow(t.year)
result = date(t.year, t.month, t.day)
return result
@@ -966,9 +1004,9 @@
ISO calendar algorithm taken from
http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm
"""
- year = self.__year
+ year = self._year
week1monday = _isoweek1monday(year)
- today = _ymd2ord(self.__year, self.__month, self.__day)
+ today = _ymd2ord(self._year, self._month, self._day)
# Internally, week and day have origin 0
week, day = divmod(today - week1monday, 7)
if week < 0:
@@ -985,18 +1023,18 @@
__safe_for_unpickling__ = True # For Python 2.2
- def __getstate(self):
- yhi, ylo = divmod(self.__year, 256)
- return ("%c%c%c%c" % (yhi, ylo, self.__month, self.__day), )
+ def _getstate(self):
+ yhi, ylo = divmod(self._year, 256)
+ return ("%c%c%c%c" % (yhi, ylo, self._month, self._day), )
def __setstate(self, string):
if len(string) != 4 or not (1 <= ord(string[2]) <= 12):
raise TypeError("not enough arguments")
- yhi, ylo, self.__month, self.__day = map(ord, string)
- self.__year = yhi * 256 + ylo
+ yhi, ylo, self._month, self._day = map(ord, string)
+ self._year = yhi * 256 + ylo
def __reduce__(self):
- return (self.__class__, self.__getstate())
+ return (self.__class__, self._getstate())
_date_class = date # so functions w/ args named "date" can get at the class
@@ -1118,62 +1156,80 @@
return self
_check_tzinfo_arg(tzinfo)
_check_time_fields(hour, minute, second, microsecond)
- self.__hour = hour
- self.__minute = minute
- self.__second = second
- self.__microsecond = microsecond
+ self._hour = hour
+ self._minute = minute
+ self._second = second
+ self._microsecond = microsecond
self._tzinfo = tzinfo
return self
# Read-only field accessors
- hour = property(lambda self: self.__hour, doc="hour (0-23)")
- minute = property(lambda self: self.__minute, doc="minute (0-59)")
- second = property(lambda self: self.__second, doc="second (0-59)")
- microsecond = property(lambda self: self.__microsecond,
- doc="microsecond (0-999999)")
- tzinfo = property(lambda self: self._tzinfo, doc="timezone info object")
+ @property
+ def hour(self):
+ """hour (0-23)"""
+ return self._hour
+
+ @property
+ def minute(self):
+ """minute (0-59)"""
+ return self._minute
+
+ @property
+ def second(self):
+ """second (0-59)"""
+ return self._second
+
+ @property
+ def microsecond(self):
+ """microsecond (0-999999)"""
+ return self._microsecond
+
+ @property
+ def tzinfo(self):
+ """timezone info object"""
+ return self._tzinfo
# Standard conversions, __hash__ (and helpers)
- # Comparisons.
+ # Comparisons of time objects with other.
def __eq__(self, other):
if isinstance(other, time):
- return self.__cmp(other) == 0
+ return self._cmp(other) == 0
else:
return False
def __ne__(self, other):
if isinstance(other, time):
- return self.__cmp(other) != 0
+ return self._cmp(other) != 0
else:
return True
def __le__(self, other):
if isinstance(other, time):
- return self.__cmp(other) <= 0
+ return self._cmp(other) <= 0
else:
_cmperror(self, other)
def __lt__(self, other):
if isinstance(other, time):
- return self.__cmp(other) < 0
+ return self._cmp(other) < 0
else:
_cmperror(self, other)
def __ge__(self, other):
if isinstance(other, time):
- return self.__cmp(other) >= 0
+ return self._cmp(other) >= 0
else:
_cmperror(self, other)
def __gt__(self, other):
if isinstance(other, time):
- return self.__cmp(other) > 0
+ return self._cmp(other) > 0
else:
_cmperror(self, other)
- def __cmp(self, other):
+ def _cmp(self, other):
assert isinstance(other, time)
mytz = self._tzinfo
ottz = other._tzinfo
@@ -1187,23 +1243,23 @@
base_compare = myoff == otoff
if base_compare:
- return cmp((self.__hour, self.__minute, self.__second,
- self.__microsecond),
- (other.__hour, other.__minute, other.__second,
- other.__microsecond))
+ return cmp((self._hour, self._minute, self._second,
+ self._microsecond),
+ (other._hour, other._minute, other._second,
+ other._microsecond))
if myoff is None or otoff is None:
# XXX Buggy in 2.2.2.
raise TypeError("cannot compare naive and aware times")
- myhhmm = self.__hour * 60 + self.__minute - myoff
- othhmm = other.__hour * 60 + other.__minute - otoff
- return cmp((myhhmm, self.__second, self.__microsecond),
- (othhmm, other.__second, other.__microsecond))
+ myhhmm = self._hour * 60 + self._minute - myoff
+ othhmm = other._hour * 60 + other._minute - otoff
+ return cmp((myhhmm, self._second, self._microsecond),
+ (othhmm, other._second, other._microsecond))
def __hash__(self):
"""Hash."""
tzoff = self._utcoffset()
if not tzoff: # zero or None
- return hash(self.__getstate()[0])
+ return hash(self._getstate()[0])
h, m = divmod(self.hour * 60 + self.minute - tzoff, 60)
if 0 <= h < 24:
return hash(time(h, m, self.second, self.microsecond))
@@ -1227,14 +1283,14 @@
def __repr__(self):
"""Convert to formal string, for repr()."""
- if self.__microsecond != 0:
- s = ", %d, %d" % (self.__second, self.__microsecond)
- elif self.__second != 0:
- s = ", %d" % self.__second
+ if self._microsecond != 0:
+ s = ", %d, %d" % (self._second, self._microsecond)
+ elif self._second != 0:
+ s = ", %d" % self._second
else:
s = ""
s= "%s(%d, %d%s)" % ('datetime.' + self.__class__.__name__,
- self.__hour, self.__minute, s)
+ self._hour, self._minute, s)
if self._tzinfo is not None:
assert s[-1:] == ")"
s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")"
@@ -1246,8 +1302,8 @@
This is 'HH:MM:SS.mmmmmm+zz:zz', or 'HH:MM:SS+zz:zz' if
self.microsecond == 0.
"""
- s = _format_time(self.__hour, self.__minute, self.__second,
- self.__microsecond)
+ s = _format_time(self._hour, self._minute, self._second,
+ self._microsecond)
tz = self._tzstr()
if tz:
s += tz
@@ -1255,14 +1311,6 @@
__str__ = isoformat
- def __format__(self, format):
- if not isinstance(format, (str, unicode)):
- raise ValueError("__format__ excepts str or unicode, not %s" %
- format.__class__.__name__)
- if not format:
- return str(self)
- return self.strftime(format)
-
def strftime(self, fmt):
"""Format using strftime(). The date part of the timestamp passed
to underlying strftime should not be used.
@@ -1270,10 +1318,18 @@
# The year must be >= 1900 else Python's strftime implementation
# can raise a bogus exception.
timetuple = (1900, 1, 1,
- self.__hour, self.__minute, self.__second,
+ self._hour, self._minute, self._second,
0, 1, -1)
return _wrap_strftime(self, fmt, timetuple)
+ def __format__(self, fmt):
+ if not isinstance(fmt, (str, unicode)):
+ raise ValueError("__format__ excepts str or unicode, not %s" %
+ fmt.__class__.__name__)
+ if len(fmt) != 0:
+ return self.strftime(fmt)
+ return str(self)
+
# Timezone functions
def utcoffset(self):
@@ -1350,10 +1406,10 @@
__safe_for_unpickling__ = True # For Python 2.2
- def __getstate(self):
- us2, us3 = divmod(self.__microsecond, 256)
+ def _getstate(self):
+ us2, us3 = divmod(self._microsecond, 256)
us1, us2 = divmod(us2, 256)
- basestate = ("%c" * 6) % (self.__hour, self.__minute, self.__second,
+ basestate = ("%c" * 6) % (self._hour, self._minute, self._second,
us1, us2, us3)
if self._tzinfo is None:
return (basestate,)
@@ -1363,13 +1419,13 @@
def __setstate(self, string, tzinfo):
if len(string) != 6 or ord(string[0]) >= 24:
raise TypeError("an integer is required")
- self.__hour, self.__minute, self.__second, us1, us2, us3 = \
+ self._hour, self._minute, self._second, us1, us2, us3 = \
map(ord, string)
- self.__microsecond = (((us1 << 8) | us2) << 8) | us3
+ self._microsecond = (((us1 << 8) | us2) << 8) | us3
self._tzinfo = tzinfo
def __reduce__(self):
- return (time, self.__getstate())
+ return (time, self._getstate())
_time_class = time # so functions w/ args named "time" can get at the class
@@ -1378,9 +1434,11 @@
time.resolution = timedelta(microseconds=1)
class datetime(date):
+ """datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
- # XXX needs docstrings
- # See http://www.zope.org/Members/fdrake/DateTimeWiki/TimeZoneInfo
+ The year, month and day arguments are required. tzinfo may be None, or an
+ instance of a tzinfo subclass. The remaining arguments may be ints or longs.
+ """
def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0,
microsecond=0, tzinfo=None):
@@ -1393,24 +1451,43 @@
_check_time_fields(hour, minute, second, microsecond)
self = date.__new__(cls, year, month, day)
# XXX This duplicates __year, __month, __day for convenience :-(
- self.__year = year
- self.__month = month
- self.__day = day
- self.__hour = hour
- self.__minute = minute
- self.__second = second
- self.__microsecond = microsecond
+ self._year = year
+ self._month = month
+ self._day = day
+ self._hour = hour
+ self._minute = minute
+ self._second = second
+ self._microsecond = microsecond
self._tzinfo = tzinfo
return self
# Read-only field accessors
- hour = property(lambda self: self.__hour, doc="hour (0-23)")
- minute = property(lambda self: self.__minute, doc="minute (0-59)")
- second = property(lambda self: self.__second, doc="second (0-59)")
- microsecond = property(lambda self: self.__microsecond,
- doc="microsecond (0-999999)")
- tzinfo = property(lambda self: self._tzinfo, doc="timezone info object")
+ @property
+ def hour(self):
+ """hour (0-23)"""
+ return self._hour
+ @property
+ def minute(self):
+ """minute (0-59)"""
+ return self._minute
+
+ @property
+ def second(self):
+ """second (0-59)"""
+ return self._second
+
+ @property
+ def microsecond(self):
+ """microsecond (0-999999)"""
+ return self._microsecond
+
+ @property
+ def tzinfo(self):
+ """timezone info object"""
+ return self._tzinfo
+
+ @classmethod
def fromtimestamp(cls, t, tz=None):
"""Construct a datetime from a POSIX timestamp (like time.time()).
@@ -1438,37 +1515,42 @@
if tz is not None:
result = tz.fromutc(result)
return result
- fromtimestamp = classmethod(fromtimestamp)
+ @classmethod
def utcfromtimestamp(cls, t):
"Construct a UTC datetime from a POSIX timestamp (like time.time())."
- if 1 - (t % 1.0) < 0.0000005:
- t = float(int(t)) + 1
- if t < 0:
- t -= 1
+ t, frac = divmod(t, 1.0)
+ us = round(frac * 1e6)
+
+ # If timestamp is less than one microsecond smaller than a
+ # full second, us can be rounded up to 1000000. In this case,
+ # roll over to seconds, otherwise, ValueError is raised
+ # by the constructor.
+ if us == 1000000:
+ t += 1
+ us = 0
y, m, d, hh, mm, ss, weekday, jday, dst = _time.gmtime(t)
- us = int((t % 1.0) * 1000000)
ss = min(ss, 59) # clamp out leap seconds if the platform has them
return cls(y, m, d, hh, mm, ss, us)
- utcfromtimestamp = classmethod(utcfromtimestamp)
# XXX This is supposed to do better than we *can* do by using time.time(),
# XXX if the platform supports a more accurate way. The C implementation
# XXX uses gettimeofday on platforms that have it, but that isn't
# XXX available from Python. So now() may return different results
# XXX across the implementations.
+ @classmethod
def now(cls, tz=None):
"Construct a datetime from time.time() and optional time zone info."
t = _time.time()
return cls.fromtimestamp(t, tz)
- now = classmethod(now)
+ @classmethod
def utcnow(cls):
"Construct a UTC datetime from time.time()."
t = _time.time()
return cls.utcfromtimestamp(t)
- utcnow = classmethod(utcnow)
+ @classmethod
def combine(cls, date, time):
"Construct a datetime from a given date and a given time."
if not isinstance(date, _date_class):
@@ -1478,7 +1560,6 @@
return cls(date.year, date.month, date.day,
time.hour, time.minute, time.second, time.microsecond,
time.tzinfo)
- combine = classmethod(combine)
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
@@ -1504,7 +1585,7 @@
def date(self):
"Return the date part."
- return date(self.__year, self.__month, self.__day)
+ return date(self._year, self._month, self._day)
def time(self):
"Return the time part, with tzinfo None."
@@ -1564,8 +1645,8 @@
def ctime(self):
"Format a la ctime()."
- t = tmxxx(self.__year, self.__month, self.__day, self.__hour,
- self.__minute, self.__second)
+ t = tmxxx(self._year, self._month, self._day, self._hour,
+ self._minute, self._second)
return t.ctime()
def isoformat(self, sep='T'):
@@ -1580,10 +1661,10 @@
Optional argument sep specifies the separator between date and
time, default 'T'.
"""
- s = ("%04d-%02d-%02d%c" % (self.__year, self.__month, self.__day,
+ s = ("%04d-%02d-%02d%c" % (self._year, self._month, self._day,
sep) +
- _format_time(self.__hour, self.__minute, self.__second,
- self.__microsecond))
+ _format_time(self._hour, self._minute, self._second,
+ self._microsecond))
off = self._utcoffset()
if off is not None:
if off < 0:
@@ -1596,13 +1677,13 @@
return s
def __repr__(self):
- "Convert to formal string, for repr()."
- L = [self.__year, self.__month, self.__day, # These are never zero
- self.__hour, self.__minute, self.__second, self.__microsecond]
+ """Convert to formal string, for repr()."""
+ L = [self._year, self._month, self._day, # These are never zero
+ self._hour, self._minute, self._second, self._microsecond]
if L[-1] == 0:
del L[-1]
if L[-1] == 0:
- del L[-1]
+ del L[-1]
s = ", ".join(map(str, L))
s = "%s(%s)" % ('datetime.' + self.__class__.__name__, s)
if self._tzinfo is not None:
@@ -1675,7 +1756,7 @@
def __eq__(self, other):
if isinstance(other, datetime):
- return self.__cmp(other) == 0
+ return self._cmp(other) == 0
elif hasattr(other, "timetuple") and not isinstance(other, date):
return NotImplemented
else:
@@ -1683,7 +1764,7 @@
def __ne__(self, other):
if isinstance(other, datetime):
- return self.__cmp(other) != 0
+ return self._cmp(other) != 0
elif hasattr(other, "timetuple") and not isinstance(other, date):
return NotImplemented
else:
@@ -1691,7 +1772,7 @@
def __le__(self, other):
if isinstance(other, datetime):
- return self.__cmp(other) <= 0
+ return self._cmp(other) <= 0
elif hasattr(other, "timetuple") and not isinstance(other, date):
return NotImplemented
else:
@@ -1699,7 +1780,7 @@
def __lt__(self, other):
if isinstance(other, datetime):
- return self.__cmp(other) < 0
+ return self._cmp(other) < 0
elif hasattr(other, "timetuple") and not isinstance(other, date):
return NotImplemented
else:
@@ -1707,7 +1788,7 @@
def __ge__(self, other):
if isinstance(other, datetime):
- return self.__cmp(other) >= 0
+ return self._cmp(other) >= 0
elif hasattr(other, "timetuple") and not isinstance(other, date):
return NotImplemented
else:
@@ -1715,13 +1796,13 @@
def __gt__(self, other):
if isinstance(other, datetime):
- return self.__cmp(other) > 0
+ return self._cmp(other) > 0
elif hasattr(other, "timetuple") and not isinstance(other, date):
return NotImplemented
else:
_cmperror(self, other)
- def __cmp(self, other):
+ def _cmp(self, other):
assert isinstance(other, datetime)
mytz = self._tzinfo
ottz = other._tzinfo
@@ -1737,12 +1818,12 @@
base_compare = myoff == otoff
if base_compare:
- return cmp((self.__year, self.__month, self.__day,
- self.__hour, self.__minute, self.__second,
- self.__microsecond),
- (other.__year, other.__month, other.__day,
- other.__hour, other.__minute, other.__second,
- other.__microsecond))
+ return cmp((self._year, self._month, self._day,
+ self._hour, self._minute, self._second,
+ self._microsecond),
+ (other._year, other._month, other._day,
+ other._hour, other._minute, other._second,
+ other._microsecond))
if myoff is None or otoff is None:
# XXX Buggy in 2.2.2.
raise TypeError("cannot compare naive and aware datetimes")
@@ -1756,13 +1837,13 @@
"Add a datetime and a timedelta."
if not isinstance(other, timedelta):
return NotImplemented
- t = tmxxx(self.__year,
- self.__month,
- self.__day + other.days,
- self.__hour,
- self.__minute,
- self.__second + other.seconds,
- self.__microsecond + other.microseconds)
+ t = tmxxx(self._year,
+ self._month,
+ self._day + other.days,
+ self._hour,
+ self._minute,
+ self._second + other.seconds,
+ self._microsecond + other.microseconds)
self._checkOverflow(t.year)
result = datetime(t.year, t.month, t.day,
t.hour, t.minute, t.second,
@@ -1780,11 +1861,11 @@
days1 = self.toordinal()
days2 = other.toordinal()
- secs1 = self.__second + self.__minute * 60 + self.__hour * 3600
- secs2 = other.__second + other.__minute * 60 + other.__hour * 3600
+ secs1 = self._second + self._minute * 60 + self._hour * 3600
+ secs2 = other._second + other._minute * 60 + other._hour * 3600
base = timedelta(days1 - days2,
secs1 - secs2,
- self.__microsecond - other.__microsecond)
+ self._microsecond - other._microsecond)
if self._tzinfo is other._tzinfo:
return base
myoff = self._utcoffset()
@@ -1792,13 +1873,13 @@
if myoff == otoff:
return base
if myoff is None or otoff is None:
- raise TypeError, "cannot mix naive and timezone-aware time"
+ raise TypeError("cannot mix naive and timezone-aware time")
return base + timedelta(minutes = otoff-myoff)
def __hash__(self):
tzoff = self._utcoffset()
if tzoff is None:
- return hash(self.__getstate()[0])
+ return hash(self._getstate()[0])
days = _ymd2ord(self.year, self.month, self.day)
seconds = self.hour * 3600 + (self.minute - tzoff) * 60 + self.second
return hash(timedelta(days, seconds, self.microsecond))
@@ -1807,12 +1888,12 @@
__safe_for_unpickling__ = True # For Python 2.2
- def __getstate(self):
- yhi, ylo = divmod(self.__year, 256)
- us2, us3 = divmod(self.__microsecond, 256)
+ def _getstate(self):
+ yhi, ylo = divmod(self._year, 256)
+ us2, us3 = divmod(self._microsecond, 256)
us1, us2 = divmod(us2, 256)
- basestate = ("%c" * 10) % (yhi, ylo, self.__month, self.__day,
- self.__hour, self.__minute, self.__second,
+ basestate = ("%c" * 10) % (yhi, ylo, self._month, self._day,
+ self._hour, self._minute, self._second,
us1, us2, us3)
if self._tzinfo is None:
return (basestate,)
@@ -1820,14 +1901,14 @@
return (basestate, self._tzinfo)
def __setstate(self, string, tzinfo):
- (yhi, ylo, self.__month, self.__day, self.__hour,
- self.__minute, self.__second, us1, us2, us3) = map(ord, string)
- self.__year = yhi * 256 + ylo
- self.__microsecond = (((us1 << 8) | us2) << 8) | us3
+ (yhi, ylo, self._month, self._day, self._hour,
+ self._minute, self._second, us1, us2, us3) = map(ord, string)
+ self._year = yhi * 256 + ylo
+ self._microsecond = (((us1 << 8) | us2) << 8) | us3
self._tzinfo = tzinfo
def __reduce__(self):
- return (self.__class__, self.__getstate())
+ return (self.__class__, self._getstate())
datetime.min = datetime(1, 1, 1)
@@ -2009,7 +2090,7 @@
Because we know z.d said z was in daylight time (else [5] would have held and
we would have stopped then), and we know z.d != z'.d (else [8] would have held
-and we we have stopped then), and there are only 2 possible values dst() can
+and we have stopped then), and there are only 2 possible values dst() can
return in Eastern, it follows that z'.d must be 0 (which it is in the example,
but the reasoning doesn't depend on the example -- it depends on there being
two possible dst() outcomes, one zero and the other non-zero). Therefore
diff --git a/lib_pypy/numpypy/core/fromnumeric.py b/lib_pypy/numpypy/core/fromnumeric.py
--- a/lib_pypy/numpypy/core/fromnumeric.py
+++ b/lib_pypy/numpypy/core/fromnumeric.py
@@ -149,6 +149,7 @@
[5, 6]])
"""
+ assert order == 'C'
if not hasattr(a, 'reshape'):
a = numpypy.array(a)
return a.reshape(newshape)
@@ -690,6 +691,7 @@
1
"""
+ assert axis is None
if not hasattr(a, 'argmax'):
a = numpypy.array(a)
return a.argmax()
@@ -705,6 +707,7 @@
documentation.
"""
+ assert axis is None
if not hasattr(a, 'argmin'):
a = numpypy.array(a)
return a.argmin()
@@ -1354,9 +1357,11 @@
-128
"""
+ assert dtype is None
+ assert out is None
if not hasattr(a, "sum"):
a = numpypy.array(a)
- return a.sum()
+ return a.sum(axis=axis)
def product (a, axis=None, dtype=None, out=None):
@@ -1382,6 +1387,8 @@
any : equivalent function
"""
+ assert axis is None
+ assert out is None
if not hasattr(a, 'any'):
a = numpypy.array(a)
return a.any()
@@ -1396,6 +1403,8 @@
numpy.all : Equivalent function; see for details.
"""
+ assert axis is None
+ assert out is None
if not hasattr(a, 'all'):
a = numpypy.array(a)
return a.all()
@@ -1464,6 +1473,8 @@
(191614240, 191614240)
"""
+ assert axis is None
+ assert out is None
if not hasattr(a, 'any'):
a = numpypy.array(a)
return a.any()
@@ -1526,6 +1537,8 @@
(28293632, 28293632, array([ True], dtype=bool))
"""
+ assert axis is None
+ assert out is None
if not hasattr(a, 'all'):
a = numpypy.array(a)
return a.all()
@@ -1705,6 +1718,8 @@
4.0
"""
+ assert axis is None
+ assert out is None
if not hasattr(a, "max"):
a = numpypy.array(a)
return a.max()
@@ -1766,6 +1781,8 @@
"""
# amin() is equivalent to min()
+ assert axis is None
+ assert out is None
if not hasattr(a, 'min'):
a = numpypy.array(a)
return a.min()
@@ -2214,9 +2231,11 @@
0.55000000074505806
"""
+ assert dtype is None
+ assert out is None
if not hasattr(a, "mean"):
a = numpypy.array(a)
- return a.mean()
+ return a.mean(axis=axis)
def std(a, axis=None, dtype=None, out=None, ddof=0):
@@ -2305,6 +2324,10 @@
0.44999999925552653
"""
+ assert axis is None
+ assert dtype is None
+ assert out is None
+ assert ddof == 0
if not hasattr(a, "std"):
a = numpypy.array(a)
return a.std()
@@ -2398,6 +2421,10 @@
0.20250000000000001
"""
+ assert axis is None
+ assert dtype is None
+ assert out is None
+ assert ddof == 0
if not hasattr(a, "var"):
a = numpypy.array(a)
return a.var()
diff --git a/pypy/doc/translation.rst b/pypy/doc/translation.rst
--- a/pypy/doc/translation.rst
+++ b/pypy/doc/translation.rst
@@ -155,7 +155,7 @@
function. The two input variables are the exception class
and the exception value, respectively. (No other block will
actually link to the exceptblock if the function does not
- explicitely raise exceptions.)
+ explicitly raise exceptions.)
``Block``
@@ -325,7 +325,7 @@
Mutable objects need special treatment during annotation, because
the annotation of contained values needs to be possibly updated to account
for mutation operations, and consequently the annotation information
-reflown through the relevant parts of the flow the graphs.
+reflown through the relevant parts of the flow graphs.
* ``SomeList`` stands for a list of homogeneous type (i.e. all the
elements of the list are represented by a single common ``SomeXxx``
@@ -503,8 +503,8 @@
Since RPython is a garbage collected language there is a lot of heap memory
allocation going on all the time, which would either not occur at all in a more
-traditional explicitely managed language or results in an object which dies at
-a time known in advance and can thus be explicitely deallocated. For example a
+traditional explicitly managed language or results in an object which dies at
+a time known in advance and can thus be explicitly deallocated. For example a
loop of the following form::
for i in range(n):
@@ -696,7 +696,7 @@
So far it is the second most mature high level backend after GenCLI:
it still can't translate the full Standard Interpreter, but after the
-Leysin sprint we were able to compile and run the rpytstone and
+Leysin sprint we were able to compile and run the rpystone and
richards benchmarks.
GenJVM is almost entirely the work of Niko Matsakis, who worked on it
diff --git a/pypy/interpreter/executioncontext.py b/pypy/interpreter/executioncontext.py
--- a/pypy/interpreter/executioncontext.py
+++ b/pypy/interpreter/executioncontext.py
@@ -445,6 +445,7 @@
AsyncAction.__init__(self, space)
self.dying_objects = []
self.finalizers_lock_count = 0
+ self.enabled_at_app_level = True
def register_callback(self, w_obj, callback, descrname):
self.dying_objects.append((w_obj, callback, descrname))
diff --git a/pypy/jit/backend/test/runner_test.py b/pypy/jit/backend/test/runner_test.py
--- a/pypy/jit/backend/test/runner_test.py
+++ b/pypy/jit/backend/test/runner_test.py
@@ -551,6 +551,28 @@
res = self.execute_operation(rop.CALL, [funcbox] + map(BoxInt, args), 'int', descr=calldescr)
assert res.value == func(*args)
+ def test_call_box_func(self):
+ def a(a1, a2):
+ return a1 + a2
+ def b(b1, b2):
+ return b1 * b2
+
+ arg1 = 40
+ arg2 = 2
+ for f in [a, b]:
+ TP = lltype.Signed
+ FPTR = self.Ptr(self.FuncType([TP, TP], TP))
+ func_ptr = llhelper(FPTR, f)
+ FUNC = deref(FPTR)
+ funcconst = self.get_funcbox(self.cpu, func_ptr)
+ funcbox = funcconst.clonebox()
+ calldescr = self.cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
+ EffectInfo.MOST_GENERAL)
+ res = self.execute_operation(rop.CALL,
+ [funcbox, BoxInt(arg1), BoxInt(arg2)],
+ 'int', descr=calldescr)
+ assert res.getint() == f(arg1, arg2)
+
def test_call_stack_alignment(self):
# test stack alignment issues, notably for Mac OS/X.
# also test the ordering of the arguments.
@@ -1872,6 +1894,7 @@
values.append(descr)
values.append(self.cpu.get_latest_value_int(0))
values.append(self.cpu.get_latest_value_int(1))
+ values.append(token)
FUNC = self.FuncType([lltype.Signed, lltype.Signed], lltype.Void)
func_ptr = llhelper(lltype.Ptr(FUNC), maybe_force)
@@ -1902,7 +1925,8 @@
assert fail.identifier == 1
assert self.cpu.get_latest_value_int(0) == 1
assert self.cpu.get_latest_value_int(1) == 10
- assert values == [faildescr, 1, 10]
+ token = self.cpu.get_latest_force_token()
+ assert values == [faildescr, 1, 10, token]
def test_force_operations_returning_int(self):
values = []
@@ -1911,6 +1935,7 @@
self.cpu.force(token)
values.append(self.cpu.get_latest_value_int(0))
values.append(self.cpu.get_latest_value_int(2))
+ values.append(token)
return 42
FUNC = self.FuncType([lltype.Signed, lltype.Signed], lltype.Signed)
@@ -1944,7 +1969,8 @@
assert self.cpu.get_latest_value_int(0) == 1
assert self.cpu.get_latest_value_int(1) == 42
assert self.cpu.get_latest_value_int(2) == 10
- assert values == [1, 10]
+ token = self.cpu.get_latest_force_token()
+ assert values == [1, 10, token]
def test_force_operations_returning_float(self):
values = []
@@ -1953,6 +1979,7 @@
self.cpu.force(token)
values.append(self.cpu.get_latest_value_int(0))
values.append(self.cpu.get_latest_value_int(2))
+ values.append(token)
return 42.5
FUNC = self.FuncType([lltype.Signed, lltype.Signed], lltype.Float)
@@ -1988,7 +2015,8 @@
x = self.cpu.get_latest_value_float(1)
assert longlong.getrealfloat(x) == 42.5
assert self.cpu.get_latest_value_int(2) == 10
- assert values == [1, 10]
+ token = self.cpu.get_latest_force_token()
+ assert values == [1, 10, token]
def test_call_to_c_function(self):
from pypy.rlib.libffi import CDLL, types, ArgChain, FUNCFLAG_CDECL
diff --git a/pypy/jit/backend/x86/assembler.py b/pypy/jit/backend/x86/assembler.py
--- a/pypy/jit/backend/x86/assembler.py
+++ b/pypy/jit/backend/x86/assembler.py
@@ -1118,6 +1118,12 @@
for src, dst in singlefloats:
self.mc.MOVD(dst, src)
# Finally remap the arguments in the main regs
+ # If x is a register and is in dst_locs, then oups, it needs to
+ # be moved away:
+ if x in dst_locs:
+ src_locs.append(x)
+ dst_locs.append(r10)
+ x = r10
remap_frame_layout(self, src_locs, dst_locs, X86_64_SCRATCH_REG)
self._regalloc.reserve_param(len(pass_on_stack))
@@ -2042,10 +2048,7 @@
size = sizeloc.value
signloc = arglocs[1]
- if isinstance(op.getarg(0), Const):
- x = imm(op.getarg(0).getint())
- else:
- x = arglocs[2]
+ x = arglocs[2] # the function address
if x is eax:
tmp = ecx
else:
diff --git a/pypy/jit/backend/x86/runner.py b/pypy/jit/backend/x86/runner.py
--- a/pypy/jit/backend/x86/runner.py
+++ b/pypy/jit/backend/x86/runner.py
@@ -6,7 +6,7 @@
from pypy.jit.codewriter import longlong
from pypy.jit.metainterp import history, compile
from pypy.jit.backend.x86.assembler import Assembler386
-from pypy.jit.backend.x86.arch import FORCE_INDEX_OFS
+from pypy.jit.backend.x86.arch import FORCE_INDEX_OFS, IS_X86_32
from pypy.jit.backend.x86.profagent import ProfileAgent
from pypy.jit.backend.llsupport.llmodel import AbstractLLCPU
from pypy.jit.backend.x86 import regloc
@@ -142,7 +142,9 @@
cast_ptr_to_int._annspecialcase_ = 'specialize:arglltype(0)'
cast_ptr_to_int = staticmethod(cast_ptr_to_int)
- all_null_registers = lltype.malloc(rffi.LONGP.TO, 24,
+ all_null_registers = lltype.malloc(rffi.LONGP.TO,
+ IS_X86_32 and (16+8) # 16 + 8 regs
+ or (16+16), # 16 + 16 regs
flavor='raw', zero=True,
immortal=True)
diff --git a/pypy/module/_codecs/test/test_ztranslation.py b/pypy/module/_codecs/test/test_ztranslation.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/_codecs/test/test_ztranslation.py
@@ -0,0 +1,5 @@
+from pypy.objspace.fake.checkmodule import checkmodule
+
+
+def test__codecs_translates():
+ checkmodule('_codecs')
diff --git a/pypy/module/_hashlib/interp_hashlib.py b/pypy/module/_hashlib/interp_hashlib.py
--- a/pypy/module/_hashlib/interp_hashlib.py
+++ b/pypy/module/_hashlib/interp_hashlib.py
@@ -34,8 +34,12 @@
ctx = lltype.malloc(ropenssl.EVP_MD_CTX.TO, flavor='raw')
rgc.add_memory_pressure(HASH_MALLOC_SIZE + self.digest_size)
- ropenssl.EVP_DigestInit(ctx, digest_type)
- self.ctx = ctx
+ try:
+ ropenssl.EVP_DigestInit(ctx, digest_type)
+ self.ctx = ctx
+ except:
+ lltype.free(ctx, flavor='raw')
+ raise
def __del__(self):
# self.lock.free()
diff --git a/pypy/module/gc/__init__.py b/pypy/module/gc/__init__.py
--- a/pypy/module/gc/__init__.py
+++ b/pypy/module/gc/__init__.py
@@ -1,18 +1,18 @@
from pypy.interpreter.mixedmodule import MixedModule
class Module(MixedModule):
- appleveldefs = {
- 'enable': 'app_gc.enable',
- 'disable': 'app_gc.disable',
- 'isenabled': 'app_gc.isenabled',
- }
interpleveldefs = {
'collect': 'interp_gc.collect',
+ 'enable': 'interp_gc.enable',
+ 'disable': 'interp_gc.disable',
+ 'isenabled': 'interp_gc.isenabled',
'enable_finalizers': 'interp_gc.enable_finalizers',
'disable_finalizers': 'interp_gc.disable_finalizers',
'garbage' : 'space.newlist([])',
#'dump_heap_stats': 'interp_gc.dump_heap_stats',
}
+ appleveldefs = {
+ }
def __init__(self, space, w_name):
if (not space.config.translating or
diff --git a/pypy/module/gc/app_gc.py b/pypy/module/gc/app_gc.py
deleted file mode 100644
--- a/pypy/module/gc/app_gc.py
+++ /dev/null
@@ -1,21 +0,0 @@
-# NOT_RPYTHON
-
-enabled = True
-
-def isenabled():
- global enabled
- return enabled
-
-def enable():
- global enabled
- import gc
- if not enabled:
- gc.enable_finalizers()
- enabled = True
-
-def disable():
- global enabled
- import gc
- if enabled:
- gc.disable_finalizers()
- enabled = False
diff --git a/pypy/module/gc/interp_gc.py b/pypy/module/gc/interp_gc.py
--- a/pypy/module/gc/interp_gc.py
+++ b/pypy/module/gc/interp_gc.py
@@ -17,6 +17,26 @@
rgc.collect()
return space.wrap(0)
+def enable(space):
+ """Non-recursive version. Enable finalizers now.
+ If they were already enabled, no-op.
+ If they were disabled even several times, enable them anyway.
+ """
+ if not space.user_del_action.enabled_at_app_level:
+ space.user_del_action.enabled_at_app_level = True
+ enable_finalizers(space)
+
+def disable(space):
+ """Non-recursive version. Disable finalizers now. Several calls
+ to this function are ignored.
+ """
+ if space.user_del_action.enabled_at_app_level:
+ space.user_del_action.enabled_at_app_level = False
+ disable_finalizers(space)
+
+def isenabled(space):
+ return space.newbool(space.user_del_action.enabled_at_app_level)
+
def enable_finalizers(space):
if space.user_del_action.finalizers_lock_count == 0:
raise OperationError(space.w_ValueError,
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
@@ -70,6 +70,7 @@
("exp", "exp"),
("fabs", "fabs"),
("floor", "floor"),
+ ("ceil", "ceil"),
("greater", "greater"),
("greater_equal", "greater_equal"),
("less", "less"),
@@ -85,6 +86,8 @@
("subtract", "subtract"),
('sqrt', 'sqrt'),
("tan", "tan"),
+ ('bitwise_and', 'bitwise_and'),
+ ('bitwise_or', 'bitwise_or'),
]:
interpleveldefs[exposed] = "interp_ufuncs.get(space).%s" % impl
diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -47,6 +47,10 @@
def getitem(self, storage, i):
return self.itemtype.read(storage, self.itemtype.get_element_size(), i, 0)
+ def getitem_bool(self, storage, i):
+ isize = self.itemtype.get_element_size()
+ return self.itemtype.read_bool(storage, isize, i, 0)
+
def setitem(self, storage, i, box):
self.itemtype.store(storage, self.itemtype.get_element_size(), i, 0, box)
@@ -85,6 +89,12 @@
def descr_get_shape(self, space):
return space.newtuple([])
+ def is_int_type(self):
+ return self.kind == SIGNEDLTR or self.kind == UNSIGNEDLTR
+
+ def is_bool_type(self):
+ return self.kind == BOOLLTR
+
W_Dtype.typedef = TypeDef("dtype",
__module__ = "numpypy",
__new__ = interp2app(W_Dtype.descr__new__.im_func),
diff --git a/pypy/module/micronumpy/interp_iter.py b/pypy/module/micronumpy/interp_iter.py
--- a/pypy/module/micronumpy/interp_iter.py
+++ b/pypy/module/micronumpy/interp_iter.py
@@ -4,6 +4,19 @@
from pypy.module.micronumpy.strides import calculate_broadcast_strides,\
calculate_slice_strides
+# structures to describe slicing
+
+class Chunk(object):
+ def __init__(self, start, stop, step, lgt):
+ self.start = start
+ self.stop = stop
+ self.step = step
+ self.lgt = lgt
+
+ def extend_shape(self, shape):
+ if self.step != 0:
+ shape.append(self.lgt)
+
class BaseTransform(object):
pass
@@ -38,11 +51,18 @@
self.size = size
def next(self, shapelen):
+ return self._next(1)
+
+ def _next(self, ofs):
arr = instantiate(ArrayIterator)
arr.size = self.size
- arr.offset = self.offset + 1
+ arr.offset = self.offset + ofs
return arr
+ def next_no_increase(self, shapelen):
+ # a hack to make JIT believe this is always virtual
+ return self._next(0)
+
def done(self):
return self.offset >= self.size
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
@@ -2,14 +2,15 @@
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.gateway import interp2app, NoneNotWrapped
from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from pypy.module.micronumpy import interp_ufuncs, interp_dtype, signature
+from pypy.module.micronumpy import interp_ufuncs, interp_dtype, signature,\
+ interp_boxes
from pypy.module.micronumpy.strides import calculate_slice_strides
from pypy.rlib import jit
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.tool.sourcetools import func_with_new_name
from pypy.rlib.rstring import StringBuilder
from pypy.module.micronumpy.interp_iter import ArrayIterator, OneDimIterator,\
- SkipLastAxisIterator
+ SkipLastAxisIterator, Chunk, ViewIterator
numpy_driver = jit.JitDriver(
greens=['shapelen', 'sig'],
@@ -39,7 +40,24 @@
get_printable_location=signature.new_printable_location('slice'),
name='numpy_slice',
)
-
+count_driver = jit.JitDriver(
+ greens=['shapelen'],
+ virtualizables=['frame'],
+ reds=['s', 'frame', 'iter', 'arr'],
+ name='numpy_count'
+)
+filter_driver = jit.JitDriver(
+ greens=['shapelen', 'sig'],
+ virtualizables=['frame'],
+ reds=['concr', 'argi', 'ri', 'frame', 'v', 'res', 'self'],
+ name='numpy_filter',
+)
+filter_set_driver = jit.JitDriver(
+ greens=['shapelen', 'sig'],
+ virtualizables=['frame'],
+ reds=['idx', 'idxi', 'frame', 'arr'],
+ name='numpy_filterset',
+)
def _find_shape_and_elems(space, w_iterable):
shape = [space.len_w(w_iterable)]
@@ -156,15 +174,17 @@
# fits the new shape, using those steps. If there is a shape/step mismatch
# (meaning that the realignment of elements crosses from one step into another)
# return None so that the caller can raise an exception.
-def calc_new_strides(new_shape, old_shape, old_strides):
+def calc_new_strides(new_shape, old_shape, old_strides, order):
+ # Return the proper strides for new_shape, or None if the mapping crosses
+ # stepping boundaries
+
# Assumes that prod(old_shape) == prod(new_shape), len(old_shape) > 1, and
# len(new_shape) > 0
steps = []
last_step = 1
oldI = 0
new_strides = []
- if old_strides[0] < old_strides[-1]:
- #Start at old_shape[0], old_stides[0]
+ if order == 'F':
for i in range(len(old_shape)):
steps.append(old_strides[i] / last_step)
last_step *= old_shape[i]
@@ -181,12 +201,10 @@
n_old_elems_to_use *= old_shape[oldI]
if n_new_elems_used == n_old_elems_to_use:
oldI += 1
- if oldI >= len(old_shape):
- continue
- cur_step = steps[oldI]
- n_old_elems_to_use *= old_shape[oldI]
- else:
- #Start at old_shape[-1], old_strides[-1]
+ if oldI < len(old_shape):
+ cur_step = steps[oldI]
+ n_old_elems_to_use *= old_shape[oldI]
+ elif order == 'C':
for i in range(len(old_shape) - 1, -1, -1):
steps.insert(0, old_strides[i] / last_step)
last_step *= old_shape[i]
@@ -205,10 +223,10 @@
n_old_elems_to_use *= old_shape[oldI]
if n_new_elems_used == n_old_elems_to_use:
oldI -= 1
- if oldI < -len(old_shape):
- continue
- cur_step = steps[oldI]
- n_old_elems_to_use *= old_shape[oldI]
+ if oldI >= -len(old_shape):
+ cur_step = steps[oldI]
+ n_old_elems_to_use *= old_shape[oldI]
+ assert len(new_strides) == len(new_shape)
return new_strides
class BaseArray(Wrappable):
@@ -270,6 +288,9 @@
descr_gt = _binop_impl("greater")
descr_ge = _binop_impl("greater_equal")
+ descr_and = _binop_impl("bitwise_and")
+ descr_or = _binop_impl("bitwise_or")
+
def _binop_right_impl(ufunc_name):
def impl(self, space, w_other):
w_other = scalar_w(space,
@@ -409,9 +430,15 @@
def descr_copy(self, space):
return self.copy(space)
+ def descr_flatten(self, space):
+ return self.flatten(space)
+
def copy(self, space):
return self.get_concrete().copy(space)
+ def flatten(self, space):
+ return self.get_concrete().flatten(space)
+
def descr_len(self, space):
if len(self.shape):
return space.wrap(self.shape[0])
@@ -479,11 +506,69 @@
def _prepare_slice_args(self, space, w_idx):
if (space.isinstance_w(w_idx, space.w_int) or
space.isinstance_w(w_idx, space.w_slice)):
- return [space.decode_index4(w_idx, self.shape[0])]
- return [space.decode_index4(w_item, self.shape[i]) for i, w_item in
+ return [Chunk(*space.decode_index4(w_idx, self.shape[0]))]
+ return [Chunk(*space.decode_index4(w_item, self.shape[i])) for i, w_item in
enumerate(space.fixedview(w_idx))]
+ def count_all_true(self, arr):
+ sig = arr.find_sig()
+ frame = sig.create_frame(self)
+ shapelen = len(arr.shape)
+ s = 0
+ iter = None
+ while not frame.done():
+ count_driver.jit_merge_point(arr=arr, frame=frame, iter=iter, s=s,
+ shapelen=shapelen)
+ iter = frame.get_final_iter()
+ s += arr.dtype.getitem_bool(arr.storage, iter.offset)
+ frame.next(shapelen)
+ return s
+
+ def getitem_filter(self, space, arr):
+ concr = arr.get_concrete()
+ size = self.count_all_true(concr)
+ res = W_NDimArray(size, [size], self.find_dtype())
+ ri = ArrayIterator(size)
+ shapelen = len(self.shape)
+ argi = concr.create_iter()
+ sig = self.find_sig()
+ frame = sig.create_frame(self)
+ v = None
+ while not frame.done():
+ filter_driver.jit_merge_point(concr=concr, argi=argi, ri=ri,
+ frame=frame, v=v, res=res, sig=sig,
+ shapelen=shapelen, self=self)
+ if concr.dtype.getitem_bool(concr.storage, argi.offset):
+ v = sig.eval(frame, self)
+ res.setitem(ri.offset, v)
+ ri = ri.next(1)
+ else:
+ ri = ri.next_no_increase(1)
+ argi = argi.next(shapelen)
+ frame.next(shapelen)
+ return res
+
+ def setitem_filter(self, space, idx, val):
+ size = self.count_all_true(idx)
+ arr = SliceArray([size], self.dtype, self, val)
+ sig = arr.find_sig()
+ shapelen = len(self.shape)
+ frame = sig.create_frame(arr)
+ idxi = idx.create_iter()
+ while not frame.done():
+ filter_set_driver.jit_merge_point(idx=idx, idxi=idxi, sig=sig,
+ frame=frame, arr=arr,
+ shapelen=shapelen)
+ if idx.dtype.getitem_bool(idx.storage, idxi.offset):
+ sig.eval(frame, arr)
+ frame.next_from_second(1)
+ frame.next_first(shapelen)
+ idxi = idxi.next(shapelen)
+
def descr_getitem(self, space, w_idx):
+ if (isinstance(w_idx, BaseArray) and w_idx.shape == self.shape and
+ w_idx.find_dtype().is_bool_type()):
+ return self.getitem_filter(space, w_idx)
if self._single_item_result(space, w_idx):
concrete = self.get_concrete()
item = concrete._index_of_single_item(space, w_idx)
@@ -493,6 +578,11 @@
def descr_setitem(self, space, w_idx, w_value):
self.invalidated()
+ if (isinstance(w_idx, BaseArray) and w_idx.shape == self.shape and
+ w_idx.find_dtype().is_bool_type()):
+ return self.get_concrete().setitem_filter(space,
+ w_idx.get_concrete(),
+ convert_to_array(space, w_value))
if self._single_item_result(space, w_idx):
concrete = self.get_concrete()
item = concrete._index_of_single_item(space, w_idx)
@@ -509,9 +599,8 @@
def create_slice(self, chunks):
shape = []
i = -1
- for i, (start_, stop, step, lgt) in enumerate(chunks):
- if step != 0:
- shape.append(lgt)
+ for i, chunk in enumerate(chunks):
+ chunk.extend_shape(shape)
s = i + 1
assert s >= 0
shape += self.shape[s:]
@@ -542,8 +631,8 @@
concrete = self.get_concrete()
new_shape = get_shape_from_iterable(space, concrete.size, w_shape)
# Since we got to here, prod(new_shape) == self.size
- new_strides = calc_new_strides(new_shape,
- concrete.shape, concrete.strides)
+ new_strides = calc_new_strides(new_shape, concrete.shape,
+ concrete.strides, concrete.order)
if new_strides:
# We can create a view, strides somehow match up.
ndims = len(new_shape)
@@ -686,6 +775,11 @@
def copy(self, space):
return Scalar(self.dtype, self.value)
+ def flatten(self, space):
+ array = W_NDimArray(self.size, [self.size], self.dtype)
+ array.setitem(0, self.value)
+ return array
+
def fill(self, space, w_value):
self.value = self.dtype.coerce(space, w_value)
@@ -724,8 +818,7 @@
frame=frame,
ri=ri,
self=self, result=result)
- result.dtype.setitem(result.storage, ri.offset,
- sig.eval(frame, self))
+ result.setitem(ri.offset, sig.eval(frame, self))
frame.next(shapelen)
ri = ri.next(shapelen)
return result
@@ -832,14 +925,15 @@
self.left.create_sig(), self.right.create_sig())
class SliceArray(Call2):
- def __init__(self, shape, dtype, left, right):
+ def __init__(self, shape, dtype, left, right, no_broadcast=False):
+ self.no_broadcast = no_broadcast
Call2.__init__(self, None, 'sliceloop', shape, dtype, dtype, left,
right)
def create_sig(self):
lsig = self.left.create_sig()
rsig = self.right.create_sig()
- if self.shape != self.right.shape:
+ if not self.no_broadcast and self.shape != self.right.shape:
return signature.SliceloopBroadcastSignature(self.ufunc,
self.name,
self.calc_dtype,
@@ -945,7 +1039,7 @@
builder.append('\n' + indent)
else:
builder.append(indent)
- view = self.create_slice([(i, 0, 0, 1)]).get_concrete()
+ view = self.create_slice([Chunk(i, 0, 0, 1)]).get_concrete()
view.to_str(space, comma, builder, indent=indent + ' ',
use_ellipsis=use_ellipsis)
if i < self.shape[0] - 1:
@@ -962,7 +1056,7 @@
builder.append(indent)
# create_slice requires len(chunks) > 1 in order to reduce
# shape
- view = self.create_slice([(i, 0, 0, 1)]).get_concrete()
+ view = self.create_slice([Chunk(i, 0, 0, 1)]).get_concrete()
view.to_str(space, comma, builder, indent=indent + ' ',
use_ellipsis=use_ellipsis)
i += 1
@@ -1068,6 +1162,15 @@
array.setslice(space, self)
return array
+ def flatten(self, space):
+ array = W_NDimArray(self.size, [self.size], self.dtype, self.order)
+ if self.supports_fast_slicing():
+ array._fast_setslice(space, self)
+ else:
+ arr = SliceArray(array.shape, array.dtype, array, self, no_broadcast=True)
+ array._sliceloop(arr)
+ return array
+
def fill(self, space, w_value):
self.setslice(space, scalar_w(space, self.dtype, w_value))
@@ -1091,6 +1194,10 @@
parent)
self.start = start
+ def create_iter(self):
+ return ViewIterator(self.start, self.strides, self.backstrides,
+ self.shape)
+
def setshape(self, space, new_shape):
if len(self.shape) < 1:
return
@@ -1114,7 +1221,8 @@
self.backstrides = backstrides
self.shape = new_shape
return
- new_strides = calc_new_strides(new_shape, self.shape, self.strides)
+ new_strides = calc_new_strides(new_shape, self.shape, self.strides,
+ self.order)
if new_strides is None:
raise OperationError(space.w_AttributeError, space.wrap(
"incompatible shape for a non-contiguous array"))
@@ -1137,6 +1245,9 @@
self.shape = new_shape
self.calc_strides(new_shape)
+ def create_iter(self):
+ return ArrayIterator(self.size)
+
def create_sig(self):
return signature.ArraySignature(self.dtype)
@@ -1191,6 +1302,7 @@
arr = W_NDimArray(size, shape[:], dtype=dtype, order=order)
shapelen = len(shape)
arr_iter = ArrayIterator(arr.size)
+ # XXX we might want to have a jitdriver here
for i in range(len(elems_w)):
w_elem = elems_w[i]
dtype.setitem(arr.storage, arr_iter.offset,
@@ -1257,6 +1369,9 @@
__gt__ = interp2app(BaseArray.descr_gt),
__ge__ = interp2app(BaseArray.descr_ge),
+ __and__ = interp2app(BaseArray.descr_and),
+ __or__ = interp2app(BaseArray.descr_or),
+
__repr__ = interp2app(BaseArray.descr_repr),
__str__ = interp2app(BaseArray.descr_str),
__array_interface__ = GetSetProperty(BaseArray.descr_array_iface),
@@ -1286,6 +1401,7 @@
fill = interp2app(BaseArray.descr_fill),
copy = interp2app(BaseArray.descr_copy),
+ flatten = interp2app(BaseArray.descr_flatten),
reshape = interp2app(BaseArray.descr_reshape),
tolist = interp2app(BaseArray.descr_tolist),
)
diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -249,15 +249,16 @@
class W_Ufunc2(W_Ufunc):
- _immutable_fields_ = ["comparison_func", "func", "name"]
+ _immutable_fields_ = ["comparison_func", "func", "name", "int_only"]
argcount = 2
def __init__(self, func, name, promote_to_float=False, promote_bools=False,
- identity=None, comparison_func=False):
+ identity=None, comparison_func=False, int_only=False):
W_Ufunc.__init__(self, name, promote_to_float, promote_bools, identity)
self.func = func
self.comparison_func = comparison_func
+ self.int_only = int_only
def call(self, space, args_w):
from pypy.module.micronumpy.interp_numarray import (Call2,
@@ -268,6 +269,7 @@
w_rhs = convert_to_array(space, w_rhs)
calc_dtype = find_binop_result_dtype(space,
w_lhs.find_dtype(), w_rhs.find_dtype(),
+ int_only=self.int_only,
promote_to_float=self.promote_to_float,
promote_bools=self.promote_bools,
)
@@ -304,10 +306,12 @@
def find_binop_result_dtype(space, dt1, dt2, promote_to_float=False,
- promote_bools=False):
+ promote_bools=False, int_only=False):
# dt1.num should be <= dt2.num
if dt1.num > dt2.num:
dt1, dt2 = dt2, dt1
+ if int_only and (not dt1.is_int_type() or not dt2.is_int_type()):
+ raise OperationError(space.w_TypeError, space.wrap("Unsupported types"))
# Some operations promote op(bool, bool) to return int8, rather than bool
if promote_bools and (dt1.kind == dt2.kind == interp_dtype.BOOLLTR):
return interp_dtype.get_dtype_cache(space).w_int8dtype
@@ -425,6 +429,10 @@
("add", "add", 2, {"identity": 0}),
("subtract", "sub", 2),
("multiply", "mul", 2, {"identity": 1}),
+ ("bitwise_and", "bitwise_and", 2, {"identity": 1,
+ 'int_only': True}),
+ ("bitwise_or", "bitwise_or", 2, {"identity": 0,
+ 'int_only': True}),
("divide", "div", 2, {"promote_bools": True}),
("mod", "mod", 2, {"promote_bools": True}),
("power", "pow", 2, {"promote_bools": True}),
@@ -449,6 +457,7 @@
("fabs", "fabs", 1, {"promote_to_float": True}),
("floor", "floor", 1, {"promote_to_float": True}),
+ ("ceil", "ceil", 1, {"promote_to_float": True}),
("exp", "exp", 1, {"promote_to_float": True}),
('sqrt', 'sqrt', 1, {'promote_to_float': True}),
@@ -475,7 +484,7 @@
extra_kwargs["identity"] = identity
func = ufunc_dtype_caller(space, ufunc_name, op_name, argcount,
- comparison_func=extra_kwargs.get("comparison_func", False)
+ comparison_func=extra_kwargs.get("comparison_func", False),
)
if argcount == 1:
ufunc = W_Ufunc1(func, ufunc_name, **extra_kwargs)
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
@@ -82,6 +82,16 @@
for i in range(len(self.iterators)):
self.iterators[i] = self.iterators[i].next(shapelen)
+ @unroll_safe
+ def next_from_second(self, shapelen):
+ """ Don't increase the first iterator
+ """
+ for i in range(1, len(self.iterators)):
+ self.iterators[i] = self.iterators[i].next(shapelen)
+
+ def next_first(self, shapelen):
+ self.iterators[0] = self.iterators[0].next(shapelen)
+
def get_final_iter(self):
final_iter = promote(self.final_iter)
if final_iter < 0:
diff --git a/pypy/module/micronumpy/strides.py b/pypy/module/micronumpy/strides.py
--- a/pypy/module/micronumpy/strides.py
+++ b/pypy/module/micronumpy/strides.py
@@ -10,12 +10,12 @@
rstart = start
rshape = []
i = -1
- for i, (start_, stop, step, lgt) in enumerate(chunks):
- if step != 0:
- rstrides.append(strides[i] * step)
- rbackstrides.append(strides[i] * (lgt - 1) * step)
- rshape.append(lgt)
- rstart += strides[i] * start_
+ for i, chunk in enumerate(chunks):
+ if chunk.step != 0:
+ rstrides.append(strides[i] * chunk.step)
+ rbackstrides.append(strides[i] * (chunk.lgt - 1) * chunk.step)
+ rshape.append(chunk.lgt)
+ rstart += strides[i] * chunk.start
# add a reminder
s = i + 1
assert s >= 0
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
@@ -2,6 +2,7 @@
import py
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
from pypy.module.micronumpy.interp_numarray import W_NDimArray, shape_agreement
+from pypy.module.micronumpy.interp_iter import Chunk
from pypy.module.micronumpy import signature
from pypy.interpreter.error import OperationError
from pypy.conftest import gettestobjspace
@@ -37,53 +38,54 @@
def test_create_slice_f(self):
a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice([(3, 0, 0, 1)])
+ s = a.create_slice([Chunk(3, 0, 0, 1)])
assert s.start == 3
assert s.strides == [10, 50]
assert s.backstrides == [40, 100]
- s = a.create_slice([(1, 9, 2, 4)])
+ s = a.create_slice([Chunk(1, 9, 2, 4)])
assert s.start == 1
assert s.strides == [2, 10, 50]
assert s.backstrides == [6, 40, 100]
- s = a.create_slice([(1, 5, 3, 2), (1, 2, 1, 1), (1, 0, 0, 1)])
+ s = a.create_slice([Chunk(1, 5, 3, 2), Chunk(1, 2, 1, 1), Chunk(1, 0, 0, 1)])
assert s.shape == [2, 1]
assert s.strides == [3, 10]
assert s.backstrides == [3, 0]
- s = a.create_slice([(0, 10, 1, 10), (2, 0, 0, 1)])
+ s = a.create_slice([Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
assert s.start == 20
assert s.shape == [10, 3]
def test_create_slice_c(self):
a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'C')
- s = a.create_slice([(3, 0, 0, 1)])
+ s = a.create_slice([Chunk(3, 0, 0, 1)])
assert s.start == 45
assert s.strides == [3, 1]
assert s.backstrides == [12, 2]
- s = a.create_slice([(1, 9, 2, 4)])
+ s = a.create_slice([Chunk(1, 9, 2, 4)])
assert s.start == 15
assert s.strides == [30, 3, 1]
assert s.backstrides == [90, 12, 2]
- s = a.create_slice([(1, 5, 3, 2), (1, 2, 1, 1), (1, 0, 0, 1)])
+ s = a.create_slice([Chunk(1, 5, 3, 2), Chunk(1, 2, 1, 1),
+ Chunk(1, 0, 0, 1)])
assert s.start == 19
assert s.shape == [2, 1]
assert s.strides == [45, 3]
assert s.backstrides == [45, 0]
- s = a.create_slice([(0, 10, 1, 10), (2, 0, 0, 1)])
+ s = a.create_slice([Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
assert s.start == 6
assert s.shape == [10, 3]
def test_slice_of_slice_f(self):
a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice([(5, 0, 0, 1)])
+ s = a.create_slice([Chunk(5, 0, 0, 1)])
assert s.start == 5
- s2 = s.create_slice([(3, 0, 0, 1)])
+ s2 = s.create_slice([Chunk(3, 0, 0, 1)])
assert s2.shape == [3]
assert s2.strides == [50]
assert s2.parent is a
assert s2.backstrides == [100]
assert s2.start == 35
- s = a.create_slice([(1, 5, 3, 2)])
- s2 = s.create_slice([(0, 2, 1, 2), (2, 0, 0, 1)])
+ s = a.create_slice([Chunk(1, 5, 3, 2)])
+ s2 = s.create_slice([Chunk(0, 2, 1, 2), Chunk(2, 0, 0, 1)])
assert s2.shape == [2, 3]
assert s2.strides == [3, 50]
assert s2.backstrides == [3, 100]
@@ -91,16 +93,16 @@
def test_slice_of_slice_c(self):
a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), order='C')
- s = a.create_slice([(5, 0, 0, 1)])
+ s = a.create_slice([Chunk(5, 0, 0, 1)])
assert s.start == 15 * 5
- s2 = s.create_slice([(3, 0, 0, 1)])
+ s2 = s.create_slice([Chunk(3, 0, 0, 1)])
assert s2.shape == [3]
assert s2.strides == [1]
assert s2.parent is a
assert s2.backstrides == [2]
assert s2.start == 5 * 15 + 3 * 3
- s = a.create_slice([(1, 5, 3, 2)])
- s2 = s.create_slice([(0, 2, 1, 2), (2, 0, 0, 1)])
+ s = a.create_slice([Chunk(1, 5, 3, 2)])
+ s2 = s.create_slice([Chunk(0, 2, 1, 2), Chunk(2, 0, 0, 1)])
assert s2.shape == [2, 3]
assert s2.strides == [45, 1]
assert s2.backstrides == [45, 2]
@@ -108,14 +110,14 @@
def test_negative_step_f(self):
a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice([(9, -1, -2, 5)])
+ s = a.create_slice([Chunk(9, -1, -2, 5)])
assert s.start == 9
assert s.strides == [-2, 10, 50]
assert s.backstrides == [-8, 40, 100]
def test_negative_step_c(self):
a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), order='C')
- s = a.create_slice([(9, -1, -2, 5)])
+ s = a.create_slice([Chunk(9, -1, -2, 5)])
assert s.start == 135
assert s.strides == [-30, 3, 1]
assert s.backstrides == [-120, 12, 2]
@@ -124,7 +126,7 @@
a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F')
r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
assert r == 1 + 2 * 10 + 2 * 50
- s = a.create_slice([(0, 10, 1, 10), (2, 0, 0, 1)])
+ s = a.create_slice([Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
r = s._index_of_single_item(self.space, self.newtuple(1, 0))
assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 0))
r = s._index_of_single_item(self.space, self.newtuple(1, 1))
@@ -134,7 +136,7 @@
a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'C')
r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
assert r == 1 * 3 * 5 + 2 * 3 + 2
- s = a.create_slice([(0, 10, 1, 10), (2, 0, 0, 1)])
+ s = a.create_slice([Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
r = s._index_of_single_item(self.space, self.newtuple(1, 0))
assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 0))
r = s._index_of_single_item(self.space, self.newtuple(1, 1))
@@ -152,13 +154,18 @@
def test_calc_new_strides(self):
from pypy.module.micronumpy.interp_numarray import calc_new_strides
- assert calc_new_strides([2, 4], [4, 2], [4, 2]) == [8, 2]
- assert calc_new_strides([2, 4, 3], [8, 3], [1, 16]) == [1, 2, 16]
- assert calc_new_strides([2, 3, 4], [8, 3], [1, 16]) is None
- assert calc_new_strides([24], [2, 4, 3], [48, 6, 1]) is None
- assert calc_new_strides([24], [2, 4, 3], [24, 6, 2]) == [2]
- assert calc_new_strides([105, 1], [3, 5, 7], [35, 7, 1]) == [1, 1]
- assert calc_new_strides([1, 105], [3, 5, 7], [35, 7, 1]) == [105, 1]
+ assert calc_new_strides([2, 4], [4, 2], [4, 2], "C") == [8, 2]
+ assert calc_new_strides([2, 4, 3], [8, 3], [1, 16], 'F') == [1, 2, 16]
+ assert calc_new_strides([2, 3, 4], [8, 3], [1, 16], 'F') is None
+ assert calc_new_strides([24], [2, 4, 3], [48, 6, 1], 'C') is None
+ assert calc_new_strides([24], [2, 4, 3], [24, 6, 2], 'C') == [2]
+ assert calc_new_strides([105, 1], [3, 5, 7], [35, 7, 1],'C') == [1, 1]
+ assert calc_new_strides([1, 105], [3, 5, 7], [35, 7, 1],'C') == [105, 1]
+ assert calc_new_strides([1, 105], [3, 5, 7], [35, 7, 1],'F') is None
+ assert calc_new_strides([1, 1, 1, 105, 1], [15, 7], [7, 1],'C') == \
+ [105, 105, 105, 1, 1]
+ assert calc_new_strides([1, 1, 105, 1, 1], [7, 15], [1, 7],'F') == \
+ [1, 1, 1, 105, 105]
class AppTestNumArray(BaseNumpyAppTest):
@@ -389,6 +396,8 @@
a.shape = ()
#numpy allows this
a.shape = (1,)
+ a = array(range(6)).reshape(2,3).T
+ raises(AttributeError, 'a.shape = 6')
def test_reshape(self):
from _numpypy import array, zeros
@@ -732,7 +741,7 @@
assert d[1] == 12
def test_mean(self):
- from _numpypy import array
+ from _numpypy import array, arange
a = array(range(5))
assert a.mean() == 2.0
assert a[:4].mean() == 1.5
@@ -742,6 +751,7 @@
assert a.mean(axis=0)[0, 0] == 35
assert (b == array(range(35, 70), dtype=float).reshape(5, 7)).all()
assert (a.mean(2) == array(range(0, 15), dtype=float).reshape(3, 5) * 7 + 3).all()
+ assert (arange(10).reshape(5, 2).mean(axis=1) == [0.5, 2.5, 4.5, 6.5, 8.5]).all()
def test_sum(self):
from _numpypy import array
@@ -1025,11 +1035,15 @@
assert a[0].tolist() == [17.1, 27.2]
def test_var(self):
- from _numpypy import array
+ from _numpypy import array, arange
a = array(range(10))
assert a.var() == 8.25
a = array([5.0])
assert a.var() == 0.0
+ a = arange(10).reshape(5, 2)
+ assert a.var() == 8.25
+ #assert (a.var(0) == [8, 8]).all()
+ #assert (a.var(1) == [.25] * 5).all()
def test_std(self):
from _numpypy import array
@@ -1038,6 +1052,22 @@
a = array([5.0])
assert a.std() == 0.0
+ def test_flatten(self):
+ from _numpypy import array
+
+ a = array([[1, 2, 3], [4, 5, 6]])
+ assert (a.flatten() == [1, 2, 3, 4, 5, 6]).all()
+ a = array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
+ assert (a.flatten() == [1, 2, 3, 4, 5, 6, 7, 8]).all()
+ a = array([1, 2, 3, 4, 5, 6, 7, 8])
+ assert (a[::2].flatten() == [1, 3, 5, 7]).all()
+ a = array([1, 2, 3])
+ assert ((a + a).flatten() == [2, 4, 6]).all()
+ a = array(2)
+ assert (a.flatten() == [2]).all()
+ a = array([[1, 2], [3, 4]])
+ assert (a.T.flatten() == [1, 3, 2, 4]).all()
+
class AppTestMultiDim(BaseNumpyAppTest):
def test_init(self):
@@ -1361,15 +1391,25 @@
assert isinstance(i['data'][0], int)
raises(TypeError, getattr, array(3), '__array_interface__')
+ def test_array_indexing_one_elem(self):
+ skip("not yet")
+ from _numpypy import array, arange
+ raises(IndexError, 'arange(3)[array([3.5])]')
+ a = arange(3)[array([1])]
+ assert a == 1
+ assert a[0] == 1
+ raises(IndexError,'arange(3)[array([15])]')
+ assert arange(3)[array([-3])] == 0
+ raises(IndexError,'arange(3)[array([-15])]')
+ assert arange(3)[array(1)] == 1
+
def test_fill(self):
from _numpypy import array
-
a = array([1, 2, 3])
a.fill(10)
assert (a == [10, 10, 10]).all()
a.fill(False)
assert (a == [0, 0, 0]).all()
-
b = a[:1]
b.fill(4)
assert (b == [4]).all()
@@ -1383,6 +1423,24 @@
d.fill(100)
assert d == 100
+ def test_array_indexing_bool(self):
+ from _numpypy import arange
+ a = arange(10)
+ assert (a[a > 3] == [4, 5, 6, 7, 8, 9]).all()
+ a = arange(10).reshape(5, 2)
+ assert (a[a > 3] == [4, 5, 6, 7, 8, 9]).all()
+ assert (a[a & 1 == 1] == [1, 3, 5, 7, 9]).all()
+
+ def test_array_indexing_bool_setitem(self):
+ from _numpypy import arange, array
+ a = arange(6)
+ a[a > 3] = 15
+ assert (a == [0, 1, 2, 3, 15, 15]).all()
+ a = arange(6).reshape(3, 2)
+ a[a & 1 == 1] = array([8, 9, 10])
+ assert (a == [[0, 8], [2, 9], [4, 10]]).all()
+
+
class AppTestSupport(BaseNumpyAppTest):
def setup_class(cls):
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -190,14 +190,24 @@
for i in range(3):
assert c[i] == a[i] - b[i]
- def test_floor(self):
- from _numpypy import array, floor
-
- reference = [-2.0, -1.0, 0.0, 1.0, 1.0]
- a = array([-1.4, -1.0, 0.0, 1.0, 1.4])
+ def test_floorceil(self):
+ from _numpypy import array, floor, ceil
+ import math
+ reference = [-2.0, -2.0, -1.0, 0.0, 1.0, 1.0, 0]
+ a = array([-1.4, -1.5, -1.0, 0.0, 1.0, 1.4, 0.5])
b = floor(a)
for i in range(5):
assert b[i] == reference[i]
+ reference = [-1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 1.0]
+ a = array([-1.4, -1.5, -1.0, 0.0, 1.0, 1.4, 0.5])
+ b = ceil(a)
+ assert (reference == b).all()
+ inf = float("inf")
+ data = [1.5, 2.9999, -1.999, inf]
+ results = [math.floor(x) for x in data]
+ assert (floor(data) == results).all()
+ results = [math.ceil(x) for x in data]
+ assert (ceil(data) == results).all()
def test_copysign(self):
from _numpypy import array, copysign
@@ -238,7 +248,7 @@
assert b[i] == math.sin(a[i])
a = sin(array([True, False], dtype=bool))
- assert abs(a[0] - sin(1)) < 1e-7 # a[0] will be less precise
+ assert abs(a[0] - sin(1)) < 1e-7 # a[0] will be less precise
assert a[1] == 0.0
def test_cos(self):
@@ -259,7 +269,6 @@
for i in range(len(a)):
assert b[i] == math.tan(a[i])
-
def test_arcsin(self):
import math
from _numpypy import array, arcsin
@@ -283,7 +292,6 @@
for i in range(len(a)):
assert b[i] == math.acos(a[i])
-
a = array([-10, -1.5, -1.01, 1.01, 1.5, 10, float('nan'), float('inf'), float('-inf')])
b = arccos(a)
for f in b:
@@ -347,11 +355,20 @@
raises(ValueError, maximum.reduce, [])
def test_reduceND(self):
- from numpypy import add, arange
+ from _numpypy import add, arange
a = arange(12).reshape(3, 4)
assert (add.reduce(a, 0) == [12, 15, 18, 21]).all()
assert (add.reduce(a, 1) == [6.0, 22.0, 38.0]).all()
+ def test_bitwise(self):
+ from _numpypy import bitwise_and, bitwise_or, arange, array
+ a = arange(6).reshape(2, 3)
+ assert (a & 1 == [[0, 1, 0], [1, 0, 1]]).all()
+ assert (a & 1 == bitwise_and(a, 1)).all()
+ assert (a | 1 == [[1, 1, 3], [3, 5, 5]]).all()
+ assert (a | 1 == bitwise_or(a, 1)).all()
+ raises(TypeError, 'array([1.0]) & 1')
+
def test_comparisons(self):
import operator
from _numpypy import equal, not_equal, less, less_equal, greater, greater_equal
diff --git a/pypy/module/micronumpy/test/test_zjit.py b/pypy/module/micronumpy/test/test_zjit.py
--- a/pypy/module/micronumpy/test/test_zjit.py
+++ b/pypy/module/micronumpy/test/test_zjit.py
@@ -217,6 +217,7 @@
# This is the sum of the ops for both loops, however if you remove the
# optimization then you end up with 2 float_adds, so we can still be
# sure it was optimized correctly.
+ py.test.skip("too fragile")
self.check_resops({'setinteriorfield_raw': 4, 'getfield_gc': 22,
'getarrayitem_gc': 4, 'getarrayitem_gc_pure': 2,
'getfield_gc_pure': 8,
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
@@ -94,6 +94,9 @@
width, storage, i, offset
))
+ def read_bool(self, storage, width, i, offset):
+ raise NotImplementedError
+
def store(self, storage, width, i, offset, box):
value = self.unbox(box)
libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
@@ -168,6 +171,7 @@
@simple_binary_op
def min(self, v1, v2):
return min(v1, v2)
+
class Bool(BaseType, Primitive):
T = lltype.Bool
@@ -185,6 +189,11 @@
else:
return self.False
+
+ def read_bool(self, storage, width, i, offset):
+ return libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
+ width, storage, i, offset)
+
def coerce_subtype(self, space, w_subtype, w_item):
# Doesn't return subclasses so it can return the constants.
return self._coerce(space, w_item)
@@ -253,6 +262,14 @@
assert v == 0
return 0
+ @simple_binary_op
+ def bitwise_and(self, v1, v2):
+ return v1 & v2
+
+ @simple_binary_op
+ def bitwise_or(self, v1, v2):
+ return v1 | v2
+
class Int8(BaseType, Integer):
T = rffi.SIGNEDCHAR
BoxType = interp_boxes.W_Int8Box
@@ -374,6 +391,10 @@
return math.floor(v)
@simple_unary_op
+ def ceil(self, v):
+ return math.ceil(v)
+
+ @simple_unary_op
def exp(self, v):
try:
return math.exp(v)
@@ -436,4 +457,4 @@
class Float64(BaseType, Float):
T = rffi.DOUBLE
BoxType = interp_boxes.W_Float64Box
- format_code = "d"
\ No newline at end of file
+ format_code = "d"
diff --git a/pypy/module/pypyjit/test_pypy_c/test_math.py b/pypy/module/pypyjit/test_pypy_c/test_math.py
--- a/pypy/module/pypyjit/test_pypy_c/test_math.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_math.py
@@ -90,3 +90,19 @@
--TICK--
jump(..., descr=)
""")
+
+ def test_pow_two(self):
+ def main(n):
+ s = 0.123
+ while n > 0:
+ s -= s ** 2 # ID: pow
+ n -= 1
+ return s
+ log = self.run(main, [500])
+ assert abs(log.result - main(500)) < 1e-9
+ loop, = log.loops_by_filename(self.filepath)
+ assert loop.match_by_id("pow", """
+ guard_not_invalidated(descr=...)
+ f38 = float_mul(f30, f30)
+ f39 = float_sub(f30, f38)
+ """)
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
@@ -18,8 +18,8 @@
from numpypy import array, arange, argmin
a = arange(6).reshape((2,3))
assert argmin(a) == 0
- assert (argmin(a, axis=0) == array([0, 0, 0])).all()
- assert (argmin(a, axis=1) == array([0, 0])).all()
+ #assert (argmin(a, axis=0) == array([0, 0, 0])).all()
+ #assert (argmin(a, axis=1) == array([0, 0])).all()
b = arange(6)
b[1] = 0
assert argmin(b) == 0
@@ -40,8 +40,8 @@
assert sum([0.5, 1.5])== 2.0
assert sum([[0, 1], [0, 5]]) == 6
# assert sum([0.5, 0.7, 0.2, 1.5], dtype=int32) == 1
- # assert (sum([[0, 1], [0, 5]], axis=0) == array([0, 6])).all()
- # assert (sum([[0, 1], [0, 5]], axis=1) == array([1, 5])).all()
+ assert (sum([[0, 1], [0, 5]], axis=0) == array([0, 6])).all()
+ assert (sum([[0, 1], [0, 5]], axis=1) == array([1, 5])).all()
# If the accumulator is too small, overflow occurs:
# assert ones(128, dtype=int8).sum(dtype=int8) == -128
@@ -98,20 +98,22 @@
from numpypy import array, var
a = array([[1,2],[3,4]])
assert var(a) == 1.25
- # assert (np.var(a,0) == array([ 1., 1.])).all()
- # assert (np.var(a,1) == array([ 0.25, 0.25])).all()
+ #assert (var(a,0) == array([ 1., 1.])).all()
+ #assert (var(a,1) == array([ 0.25, 0.25])).all()
def test_std(self):
from numpypy import array, std
a = array([[1, 2], [3, 4]])
assert std(a) == 1.1180339887498949
- # assert (std(a, axis=0) == array([ 1., 1.])).all()
- # assert (std(a, axis=1) == array([ 0.5, 0.5]).all()
+ #assert (std(a, axis=0) == array([ 1., 1.])).all()
+ #assert (std(a, axis=1) == array([ 0.5, 0.5])).all()
def test_mean(self):
- from numpypy import array, mean
+ from numpypy import array, mean, arange
assert mean(array(range(5))) == 2.0
assert mean(range(5)) == 2.0
+ assert (mean(arange(10).reshape(5, 2), axis=0) == [4, 5]).all()
+ assert (mean(arange(10).reshape(5, 2), axis=1) == [0.5, 2.5, 4.5, 6.5, 8.5]).all()
def test_reshape(self):
from numpypy import arange, array, dtype, reshape
diff --git a/pypy/module/test_lib_pypy/test_datetime.py b/pypy/module/test_lib_pypy/test_datetime.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/test_datetime.py
@@ -0,0 +1,24 @@
+"""Additional tests for datetime."""
+
+import time
+import datetime
+import os
+
+def test_utcfromtimestamp():
+ """Confirm that utcfromtimestamp and fromtimestamp give consistent results.
+
+ Based on danchr's test script in https://bugs.pypy.org/issue986
+ """
+ try:
+ prev_tz = os.environ.get("TZ")
+ os.environ["TZ"] = "GMT"
+ for unused in xrange(100):
+ now = time.time()
+ delta = (datetime.datetime.utcfromtimestamp(now) -
+ datetime.datetime.fromtimestamp(now))
+ assert delta.days * 86400 + delta.seconds == 0
+ finally:
+ if prev_tz is None:
+ del os.environ["TZ"]
+ else:
+ os.environ["TZ"] = prev_tz
diff --git a/pypy/module/test_lib_pypy/test_sqlite3.py b/pypy/module/test_lib_pypy/test_sqlite3.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/test_sqlite3.py
@@ -0,0 +1,10 @@
+"""Tests for _sqlite3.py"""
+
+def test_list_ddl():
+ """From issue996. Mostly just looking for lack of exceptions."""
+ from sqlite3.dbapi2 import connect
+ connection = connect(':memory:')
+ cursor = connection.cursor()
+ cursor.execute('CREATE TABLE foo (bar INTEGER)')
+ result = list(cursor)
+ assert result == []
diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -624,8 +624,8 @@
if step == 1:
assert start >= 0
- assert slicelength >= 0
- del items[start:start+slicelength]
+ if slicelength > 0:
+ del items[start:start+slicelength]
else:
n = len(items)
i = start
@@ -662,10 +662,11 @@
while i >= lim:
items[i] = items[i-delta]
i -= 1
- elif start >= 0:
+ elif delta == 0:
+ pass
+ else:
+ assert start >= 0 # start<0 is only possible with slicelength==0
del items[start:start+delta]
- else:
- assert delta==0 # start<0 is only possible with slicelength==0
elif len2 != slicelength: # No resize for extended slices
raise operationerrfmt(space.w_ValueError, "attempt to "
"assign sequence of size %d to extended slice of size %d",
diff --git a/pypy/objspace/std/complexobject.py b/pypy/objspace/std/complexobject.py
--- a/pypy/objspace/std/complexobject.py
+++ b/pypy/objspace/std/complexobject.py
@@ -8,6 +8,7 @@
from pypy.rlib.rbigint import rbigint
from pypy.rlib.rfloat import (
formatd, DTSF_STR_PRECISION, isinf, isnan, copysign)
+from pypy.rlib import jit
import math
@@ -129,10 +130,10 @@
ir = len * math.sin(phase)
return W_ComplexObject(rr, ir)
- def pow_int(self, n):
- if n > 100 or n < -100:
- return self.pow(W_ComplexObject(1.0 * n, 0.0))
- elif n > 0:
+ def pow_small_int(self, n):
+ if n >= 0:
+ if jit.isconstant(n) and n == 2:
+ return self.mul(self)
return self.pow_positive_int(n)
else:
return w_one.div(self.pow_positive_int(-n))
@@ -217,10 +218,10 @@
def pow__Complex_Complex_ANY(space, w_complex, w_exponent, thirdArg):
if not space.is_w(thirdArg, space.w_None):
raise OperationError(space.w_ValueError, space.wrap('complex modulo'))
- int_exponent = int(w_exponent.realval)
try:
- if w_exponent.imagval == 0.0 and w_exponent.realval == int_exponent:
- w_p = w_complex.pow_int(int_exponent)
+ r = w_exponent.realval
+ if w_exponent.imagval == 0.0 and -100.0 <= r <= 100.0 and r == int(r):
+ w_p = w_complex.pow_small_int(int(r))
else:
w_p = w_complex.pow(w_exponent)
except ZeroDivisionError:
diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py
--- a/pypy/objspace/std/floatobject.py
+++ b/pypy/objspace/std/floatobject.py
@@ -443,6 +443,8 @@
y = w_float2.floatval
# Sort out special cases here instead of relying on pow()
+ if y == 2.0: # special case for performance:
+ return W_FloatObject(x * x) # x * x is always correct
if y == 0.0:
# x**0 is 1, even 0**0
return W_FloatObject(1.0)
diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -804,10 +804,11 @@
while i >= lim:
items[i] = items[i-delta]
i -= 1
- elif start >= 0:
+ elif delta == 0:
+ pass
+ else:
+ assert start >= 0 # start<0 is only possible with slicelength==0
del items[start:start+delta]
- else:
- assert delta==0 # start<0 is only possible with slicelength==0
elif len2 != slicelength: # No resize for extended slices
raise operationerrfmt(self.space.w_ValueError, "attempt to "
"assign sequence of size %d to extended slice of size %d",
@@ -851,8 +852,8 @@
if step == 1:
assert start >= 0
- assert slicelength >= 0
- del items[start:start+slicelength]
+ if slicelength > 0:
+ del items[start:start+slicelength]
else:
n = len(items)
i = start
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -1,5 +1,9 @@
+from pypy import conftest
class AppTestBytesArray:
+ def setup_class(cls):
+ cls.w_runappdirect = cls.space.wrap(conftest.option.runappdirect)
+
def test_basics(self):
b = bytearray()
assert type(b) is bytearray
@@ -439,3 +443,15 @@
def test_reduce(self):
assert bytearray('caf\xe9').__reduce__() == (
bytearray, (u'caf\xe9', 'latin-1'), None)
+
+ def test_setitem_slice_performance(self):
+ # because of a complexity bug, this used to take forever on a
+ # translated pypy. On CPython2.6 -A, it takes around 8 seconds.
+ if self.runappdirect:
+ count = 16*1024*1024
+ else:
+ count = 1024
+ b = bytearray(count)
+ for i in range(count):
+ b[i:i+1] = 'y'
+ assert str(b) == 'y' * count
diff --git a/pypy/objspace/std/test/test_complexobject.py b/pypy/objspace/std/test/test_complexobject.py
--- a/pypy/objspace/std/test/test_complexobject.py
+++ b/pypy/objspace/std/test/test_complexobject.py
@@ -71,7 +71,7 @@
assert _powu((0.0,1.0),2) == (-1.0,0.0)
def _powi((r1, i1), n):
- w_res = W_ComplexObject(r1, i1).pow_int(n)
+ w_res = W_ComplexObject(r1, i1).pow_small_int(n)
return w_res.realval, w_res.imagval
assert _powi((0.0,2.0),0) == (1.0,0.0)
assert _powi((0.0,0.0),2) == (0.0,0.0)
@@ -213,6 +213,7 @@
assert a ** 105 == a ** 105
assert a ** -105 == a ** -105
assert a ** -30 == a ** -30
+ assert a ** 2 == a * a
assert 0.0j ** 0 == 1
diff --git a/pypy/objspace/std/test/test_listobject.py b/pypy/objspace/std/test/test_listobject.py
--- a/pypy/objspace/std/test/test_listobject.py
+++ b/pypy/objspace/std/test/test_listobject.py
@@ -397,6 +397,7 @@
on_cpython = (option.runappdirect and
not hasattr(sys, 'pypy_translation_info'))
cls.w_on_cpython = cls.space.wrap(on_cpython)
+ cls.w_runappdirect = cls.space.wrap(option.runappdirect)
def test_getstrategyfromlist_w(self):
l0 = ["a", "2", "a", True]
@@ -898,6 +899,18 @@
l[::-1] = l
assert l == [6,5,4,3,2,1]
+ def test_setitem_slice_performance(self):
+ # because of a complexity bug, this used to take forever on a
+ # translated pypy. On CPython2.6 -A, it takes around 5 seconds.
+ if self.runappdirect:
+ count = 16*1024*1024
+ else:
+ count = 1024
+ b = [None] * count
+ for i in range(count):
+ b[i:i+1] = ['y']
+ assert b == ['y'] * count
+
def test_recursive_repr(self):
l = []
assert repr(l) == '[]'
diff --git a/pypy/rlib/rweakref.py b/pypy/rlib/rweakref.py
--- a/pypy/rlib/rweakref.py
+++ b/pypy/rlib/rweakref.py
@@ -1,11 +1,14 @@
"""
-Weakref support in RPython. Supports ref() without callbacks,
+Weakref support in RPython. Basic regular weakrefs without callbacks
+are supported. This file contains the following additions:
a form of WeakKeyDictionary, and a limited version of WeakValueDictionary.
LLType only for now!
"""
import weakref
+ref = weakref.ref # basic regular weakrefs are supported in RPython
+
class RWeakValueDictionary(object):
"""A dictionary containing weak values."""
diff --git a/pypy/tool/test/test_version.py b/pypy/tool/test/test_version.py
--- a/pypy/tool/test/test_version.py
+++ b/pypy/tool/test/test_version.py
@@ -1,6 +1,22 @@
import os, sys
import py
-from pypy.tool.version import get_repo_version_info
+from pypy.tool.version import get_repo_version_info, _get_hg_archive_version
+
+def test_hg_archival_version(tmpdir):
+ def version_for(name, **kw):
+ path = tmpdir.join(name)
+ path.write('\n'.join('%s: %s' % x for x in kw.items()))
+ return _get_hg_archive_version(str(path))
+
+ assert version_for('release',
+ tag='release-123',
+ node='000',
+ ) == ('PyPy', 'release-123', '000')
+ assert version_for('somebranch',
+ node='000',
+ branch='something',
+ ) == ('PyPy', 'something', '000')
+
def test_get_repo_version_info():
assert get_repo_version_info(None)
diff --git a/pypy/tool/version.py b/pypy/tool/version.py
--- a/pypy/tool/version.py
+++ b/pypy/tool/version.py
@@ -3,111 +3,139 @@
from subprocess import Popen, PIPE
import pypy
pypydir = os.path.dirname(os.path.abspath(pypy.__file__))
+pypyroot = os.path.dirname(pypydir)
+default_retval = 'PyPy', '?', '?'
+
+def maywarn(err, repo_type='Mercurial'):
+ if not err:
+ return
+
+ from pypy.tool.ansi_print import ansi_log
+ log = py.log.Producer("version")
+ py.log.setconsumer("version", ansi_log)
+ log.WARNING('Errors getting %s information: %s' % (repo_type, err))
def get_repo_version_info(hgexe=None):
'''Obtain version information by invoking the 'hg' or 'git' commands.'''
- # TODO: support extracting from .hg_archival.txt
-
- default_retval = 'PyPy', '?', '?'
- pypyroot = os.path.abspath(os.path.join(pypydir, '..'))
-
- def maywarn(err, repo_type='Mercurial'):
- if not err:
- return
-
- from pypy.tool.ansi_print import ansi_log
- log = py.log.Producer("version")
- py.log.setconsumer("version", ansi_log)
- log.WARNING('Errors getting %s information: %s' % (repo_type, err))
# Try to see if we can get info from Git if hgexe is not specified.
if not hgexe:
if os.path.isdir(os.path.join(pypyroot, '.git')):
- gitexe = py.path.local.sysfind('git')
- if gitexe:
- try:
- p = Popen(
- [str(gitexe), 'rev-parse', 'HEAD'],
- stdout=PIPE, stderr=PIPE, cwd=pypyroot
- )
- except OSError, e:
- maywarn(e, 'Git')
- return default_retval
- if p.wait() != 0:
- maywarn(p.stderr.read(), 'Git')
- return default_retval
- revision_id = p.stdout.read().strip()[:12]
- p = Popen(
- [str(gitexe), 'describe', '--tags', '--exact-match'],
- stdout=PIPE, stderr=PIPE, cwd=pypyroot
- )
- if p.wait() != 0:
- p = Popen(
- [str(gitexe), 'branch'], stdout=PIPE, stderr=PIPE,
- cwd=pypyroot
- )
- if p.wait() != 0:
- maywarn(p.stderr.read(), 'Git')
- return 'PyPy', '?', revision_id
- branch = '?'
- for line in p.stdout.read().strip().split('\n'):
- if line.startswith('* '):
- branch = line[1:].strip()
- if branch == '(no branch)':
- branch = '?'
- break
- return 'PyPy', branch, revision_id
- return 'PyPy', p.stdout.read().strip(), revision_id
+ return _get_git_version()
# Fallback to trying Mercurial.
if hgexe is None:
hgexe = py.path.local.sysfind('hg')
- if not os.path.isdir(os.path.join(pypyroot, '.hg')):
+ if os.path.isfile(os.path.join(pypyroot, '.hg_archival.txt')):
+ return _get_hg_archive_version(os.path.join(pypyroot, '.hg_archival.txt'))
+ elif not os.path.isdir(os.path.join(pypyroot, '.hg')):
maywarn('Not running from a Mercurial repository!')
return default_retval
elif not hgexe:
maywarn('Cannot find Mercurial command!')
return default_retval
else:
- env = dict(os.environ)
- # get Mercurial into scripting mode
- env['HGPLAIN'] = '1'
- # disable user configuration, extensions, etc.
- env['HGRCPATH'] = os.devnull
+ return _get_hg_version(hgexe)
- try:
- p = Popen([str(hgexe), 'version', '-q'],
- stdout=PIPE, stderr=PIPE, env=env)
- except OSError, e:
- maywarn(e)
- return default_retval
- if not p.stdout.read().startswith('Mercurial Distributed SCM'):
- maywarn('command does not identify itself as Mercurial')
- return default_retval
+def _get_hg_version(hgexe):
+ env = dict(os.environ)
+ # get Mercurial into scripting mode
+ env['HGPLAIN'] = '1'
+ # disable user configuration, extensions, etc.
+ env['HGRCPATH'] = os.devnull
- p = Popen([str(hgexe), 'id', '-i', pypyroot],
+ try:
+ p = Popen([str(hgexe), 'version', '-q'],
stdout=PIPE, stderr=PIPE, env=env)
- hgid = p.stdout.read().strip()
+ except OSError, e:
+ maywarn(e)
+ return default_retval
+
+ if not p.stdout.read().startswith('Mercurial Distributed SCM'):
+ maywarn('command does not identify itself as Mercurial')
+ return default_retval
+
+ p = Popen([str(hgexe), 'id', '-i', pypyroot],
+ stdout=PIPE, stderr=PIPE, env=env)
+ hgid = p.stdout.read().strip()
+ maywarn(p.stderr.read())
+ if p.wait() != 0:
+ hgid = '?'
+
+ p = Popen([str(hgexe), 'id', '-t', pypyroot],
+ stdout=PIPE, stderr=PIPE, env=env)
+ hgtags = [t for t in p.stdout.read().strip().split() if t != 'tip']
+ maywarn(p.stderr.read())
+ if p.wait() != 0:
+ hgtags = ['?']
+
+ if hgtags:
+ return 'PyPy', hgtags[0], hgid
+ else:
+ # use the branch instead
+ p = Popen([str(hgexe), 'id', '-b', pypyroot],
+ stdout=PIPE, stderr=PIPE, env=env)
+ hgbranch = p.stdout.read().strip()
maywarn(p.stderr.read())
+
+ return 'PyPy', hgbranch, hgid
+
+
+def _get_hg_archive_version(path):
+ fp = open(path)
+ try:
+ data = dict(x.split(': ', 1) for x in fp.read().splitlines())
+ finally:
+ fp.close()
+ if 'tag' in data:
+ return 'PyPy', data['tag'], data['node']
+ else:
+ return 'PyPy', data['branch'], data['node']
+
+
+def _get_git_version():
+ #XXX: this function is a untested hack,
+ # so the git mirror tav made will work
+ gitexe = py.path.local.sysfind('git')
+ if not gitexe:
+ return default_retval
+
+ try:
+ p = Popen(
+ [str(gitexe), 'rev-parse', 'HEAD'],
+ stdout=PIPE, stderr=PIPE, cwd=pypyroot
+ )
+ except OSError, e:
+ maywarn(e, 'Git')
+ return default_retval
+ if p.wait() != 0:
+ maywarn(p.stderr.read(), 'Git')
+ return default_retval
+ revision_id = p.stdout.read().strip()[:12]
+ p = Popen(
+ [str(gitexe), 'describe', '--tags', '--exact-match'],
+ stdout=PIPE, stderr=PIPE, cwd=pypyroot
+ )
+ if p.wait() != 0:
+ p = Popen(
+ [str(gitexe), 'branch'], stdout=PIPE, stderr=PIPE,
+ cwd=pypyroot
+ )
if p.wait() != 0:
- hgid = '?'
+ maywarn(p.stderr.read(), 'Git')
+ return 'PyPy', '?', revision_id
+ branch = '?'
+ for line in p.stdout.read().strip().split('\n'):
+ if line.startswith('* '):
+ branch = line[1:].strip()
+ if branch == '(no branch)':
+ branch = '?'
+ break
+ return 'PyPy', branch, revision_id
+ return 'PyPy', p.stdout.read().strip(), revision_id
- p = Popen([str(hgexe), 'id', '-t', pypyroot],
- stdout=PIPE, stderr=PIPE, env=env)
- hgtags = [t for t in p.stdout.read().strip().split() if t != 'tip']
- maywarn(p.stderr.read())
- if p.wait() != 0:
- hgtags = ['?']
- if hgtags:
- return 'PyPy', hgtags[0], hgid
- else:
- # use the branch instead
- p = Popen([str(hgexe), 'id', '-b', pypyroot],
- stdout=PIPE, stderr=PIPE, env=env)
- hgbranch = p.stdout.read().strip()
- maywarn(p.stderr.read())
-
- return 'PyPy', hgbranch, hgid
+if __name__ == '__main__':
+ print get_repo_version_info()
diff --git a/pypy/translator/c/src/profiling.c b/pypy/translator/c/src/profiling.c
--- a/pypy/translator/c/src/profiling.c
+++ b/pypy/translator/c/src/profiling.c
@@ -29,33 +29,33 @@
profiling_setup = 0;
}
}
-
-#elif defined(_WIN32)
-#include <windows.h>
-
-DWORD_PTR base_affinity_mask;
-int profiling_setup = 0;
-
-void pypy_setup_profiling() {
- if (!profiling_setup) {
- DWORD_PTR affinity_mask, system_affinity_mask;
- GetProcessAffinityMask(GetCurrentProcess(),
- &base_affinity_mask, &system_affinity_mask);
- affinity_mask = 1;
- /* Pick one cpu allowed by the system */
- if (system_affinity_mask)
- while ((affinity_mask & system_affinity_mask) == 0)
- affinity_mask <<= 1;
- SetProcessAffinityMask(GetCurrentProcess(), affinity_mask);
- profiling_setup = 1;
- }
-}
-
-void pypy_teardown_profiling() {
- if (profiling_setup) {
- SetProcessAffinityMask(GetCurrentProcess(), base_affinity_mask);
- profiling_setup = 0;
- }
+
+#elif defined(_WIN32)
+#include <windows.h>
+
+DWORD_PTR base_affinity_mask;
+int profiling_setup = 0;
+
+void pypy_setup_profiling() {
+ if (!profiling_setup) {
+ DWORD_PTR affinity_mask, system_affinity_mask;
+ GetProcessAffinityMask(GetCurrentProcess(),
+ &base_affinity_mask, &system_affinity_mask);
+ affinity_mask = 1;
+ /* Pick one cpu allowed by the system */
+ if (system_affinity_mask)
+ while ((affinity_mask & system_affinity_mask) == 0)
+ affinity_mask <<= 1;
+ SetProcessAffinityMask(GetCurrentProcess(), affinity_mask);
+ profiling_setup = 1;
+ }
+}
+
+void pypy_teardown_profiling() {
+ if (profiling_setup) {
+ SetProcessAffinityMask(GetCurrentProcess(), base_affinity_mask);
+ profiling_setup = 0;
+ }
}
#else
diff --git a/pypy/translator/goal/translate.py b/pypy/translator/goal/translate.py
--- a/pypy/translator/goal/translate.py
+++ b/pypy/translator/goal/translate.py
@@ -293,17 +293,18 @@
drv.exe_name = targetspec_dic['__name__'] + '-%(backend)s'
# Double check to ensure we are not overwriting the current interpreter
- try:
- this_exe = py.path.local(sys.executable).new(ext='')
- exe_name = drv.compute_exe_name()
- samefile = this_exe.samefile(exe_name)
- assert not samefile, (
- 'Output file %s is the currently running '
- 'interpreter (use --output=...)'% exe_name)
- except EnvironmentError:
- pass
+ goals = translateconfig.goals
+ if not goals or 'compile' in goals:
+ try:
+ this_exe = py.path.local(sys.executable).new(ext='')
+ exe_name = drv.compute_exe_name()
+ samefile = this_exe.samefile(exe_name)
+ assert not samefile, (
+ 'Output file %s is the currently running '
+ 'interpreter (use --output=...)'% exe_name)
+ except EnvironmentError:
+ pass
- goals = translateconfig.goals
try:
drv.proceed(goals)
finally:
More information about the pypy-commit
mailing list