Author: Brian Kearns <[email protected]>
Branch:
Changeset: r62202:04ef71ea0eb7
Date: 2013-03-07 23:00 -0500
http://bitbucket.org/pypy/pypy/changeset/04ef71ea0eb7/
Log: further reduce diffs in datetime.py between 2.x and 3.x
diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -19,11 +19,15 @@
import time as _time
import math as _math
+def _cmp(x, y):
+ return 0 if x == y else 1 if x > y else -1
+
def _round(x):
return _math.floor(x + 0.5) if x >= 0.0 else _math.ceil(x - 0.5)
MINYEAR = 1
MAXYEAR = 9999
+_MINYEARFMT = 1900
# Utility functions, adapted from Python's Demo/classes/Dates.py, which
# also assumes the current Gregorian calendar indefinitely extended in
@@ -172,10 +176,11 @@
# Correctly substitute for %z and %Z escapes in strftime formats.
def _wrap_strftime(object, format, timetuple):
year = timetuple[0]
- if year < 1900:
- raise ValueError("year=%d is before 1900; the datetime strftime() "
- "methods require year >= 1900" % year)
- # Don't call _utcoffset() or tzname() unless actually needed.
+ if year < _MINYEARFMT:
+ raise ValueError("year=%d is before %d; the datetime strftime() "
+ "methods require year >= %d" %
+ (year, _MINYEARFMT, _MINYEARFMT))
+ # Don't call utcoffset() or tzname() unless actually needed.
freplace = None # the string to use for %f
zreplace = None # the string to use for %z
Zreplace = None # the string to use for %Z
@@ -263,9 +268,9 @@
raise ValueError("tzinfo.%s() must return a whole number "
"of minutes" % name)
offset = minutes
- if -1440 < offset < 1440:
- return offset
- raise ValueError("%s()=%d, must be in -1439..1439" % (name, offset))
+ if not -1440 < offset < 1440:
+ raise ValueError("%s()=%d, must be in -1439..1439" % (name, offset))
+ return offset
def _check_int_field(value):
if isinstance(value, int):
@@ -690,7 +695,7 @@
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())
@@ -750,7 +755,7 @@
year, month, day (required, base 1)
"""
- if isinstance(year, str) and len(year) == 4:
+ if isinstance(year, bytes) and len(year) == 4:
# Pickle support
self = object.__new__(cls)
self.__setstate(year)
@@ -938,7 +943,7 @@
assert isinstance(other, date)
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))
+ return _cmp((y, m, d), (y2, m2, d2))
def __hash__(self):
"Hash."
@@ -1021,8 +1026,9 @@
def __setstate(self, string):
if len(string) != 4 or not (1 <= ord(string[2]) <= 12):
raise TypeError("not enough arguments")
- self._month, self._day = ord(string[2]), ord(string[3])
- self._year = ord(string[0]) * 256 + ord(string[1])
+ yhi, ylo, self._month, self._day = (ord(string[0]), ord(string[1]),
+ ord(string[2]), ord(string[3]))
+ self._year = yhi * 256 + ylo
def __reduce__(self):
return (self.__class__, self._getstate())
@@ -1140,7 +1146,7 @@
second, microsecond (default to zero)
tzinfo (default to None)
"""
- if isinstance(hour, str):
+ if isinstance(hour, bytes) and len(hour) == 6:
# Pickle support
self = object.__new__(cls)
self.__setstate(hour, minute or None)
@@ -1235,16 +1241,16 @@
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:
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))
+ return _cmp((myhhmm, self._second, self._microsecond),
+ (othhmm, other._second, other._microsecond))
def __hash__(self):
"""Hash."""
@@ -1408,10 +1414,10 @@
def __setstate(self, string, tzinfo):
if len(string) != 6 or ord(string[0]) >= 24:
raise TypeError("an integer is required")
- self._hour, self._minute, self._second = ord(string[0]), \
- ord(string[1]), ord(string[2])
- self._microsecond = (((ord(string[3]) << 8) | \
- ord(string[4])) << 8) | ord(string[5])
+ self._hour, self._minute, self._second, us1, us2, us3 = (
+ ord(string[0]), ord(string[1]), ord(string[2]),
+ ord(string[3]), ord(string[4]), ord(string[5]))
+ self._microsecond = (((us1 << 8) | us2) << 8) | us3
self._tzinfo = tzinfo
def __reduce__(self):
@@ -1433,7 +1439,7 @@
def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0,
microsecond=0, tzinfo=None):
- if isinstance(year, str) and len(year) == 10:
+ if isinstance(year, bytes) and len(year) == 10:
# Pickle support
self = date.__new__(cls, year[:4])
self.__setstate(year, month)
@@ -1812,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:
raise TypeError("cannot compare naive and aware datetimes")
# XXX What follows could be done more efficiently...
@@ -1892,11 +1898,13 @@
return (basestate, self._tzinfo)
def __setstate(self, string, tzinfo):
- (self._month, self._day, self._hour, self._minute,
- self._second) = (ord(string[2]), ord(string[3]), ord(string[4]),
- ord(string[5]), ord(string[6]))
- self._year = ord(string[0]) * 256 + ord(string[1])
- self._microsecond = (((ord(string[7]) << 8) | ord(string[8])) << 8) |
ord(string[9])
+ (yhi, ylo, self._month, self._day, self._hour,
+ self._minute, self._second, us1, us2, us3) = (ord(string[0]),
+ ord(string[1]), ord(string[2]), ord(string[3]),
+ ord(string[4]), ord(string[5]), ord(string[6]),
+ ord(string[7]), ord(string[8]), ord(string[9]))
+ self._year = yhi * 256 + ylo
+ self._microsecond = (((us1 << 8) | us2) << 8) | us3
self._tzinfo = tzinfo
def __reduce__(self):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit