[pypy-commit] pypy py3k: use struct.pack for datetime getstate (faster, works in both py2/py3)

bdkearns noreply at buildbot.pypy.org
Fri Mar 8 05:04:04 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: py3k
Changeset: r62205:a7f2fbea4eaf
Date: 2013-03-07 22:44 -0500
http://bitbucket.org/pypy/pypy/changeset/a7f2fbea4eaf/

Log:	use struct.pack for datetime getstate (faster, works in both
	py2/py3)

diff --git a/lib-python/3/datetime.py b/lib-python/3/datetime.py
--- a/lib-python/3/datetime.py
+++ b/lib-python/3/datetime.py
@@ -18,6 +18,7 @@
 
 import time as _time
 import math as _math
+import struct as _struct
 
 def _cmp(x, y):
     return 0 if x == y else 1 if x > y else -1
@@ -929,7 +930,7 @@
 
     def _getstate(self):
         yhi, ylo = divmod(self._year, 256)
-        return bytes([yhi, ylo, self._month, self._day]),
+        return (_struct.pack('4B', yhi, ylo, self._month, self._day),)
 
     def __setstate(self, string):
         if len(string) != 4 or not (1 <= string[2] <= 12):
@@ -1300,8 +1301,8 @@
     def _getstate(self):
         us2, us3 = divmod(self._microsecond, 256)
         us1, us2 = divmod(us2, 256)
-        basestate = bytes([self._hour, self._minute, self._second,
-                           us1, us2, us3])
+        basestate = _struct.pack('6B', self._hour, self._minute, self._second,
+                                       us1, us2, us3)
         if self._tzinfo is None:
             return (basestate,)
         else:
@@ -1774,9 +1775,9 @@
         yhi, ylo = divmod(self._year, 256)
         us2, us3 = divmod(self._microsecond, 256)
         us1, us2 = divmod(us2, 256)
-        basestate = bytes([yhi, ylo, self._month, self._day,
-                           self._hour, self._minute, self._second,
-                           us1, us2, us3])
+        basestate = _struct.pack('10B', yhi, ylo, self._month, self._day,
+                                        self._hour, self._minute, self._second,
+                                        us1, us2, us3)
         if self._tzinfo is None:
             return (basestate,)
         else:


More information about the pypy-commit mailing list