From 035b33e20bc54c0c26942a0864c0384190881484 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 6 Mar 2012 21:38:18 +0200
Subject: [PATCH] Fix tempfile reset code & test on newer Python
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Python 2.7.3 (rc status) and 3.2.3/3.3 (rc, respectively alpha status)
have fixed http://bugs.python.org/issue12856 which we worked around
ourselves.

This means two things:

- we don't need to manually reset the module
- we can't test for the no-reset case

Unfortunately current Debian Sid has the 2.7.3 RC but still labeled as
2.7.2+ ☹, so we have to manually do an extra check (in the unit test
only); I expect Debian will update to official 2.7.3 as soon as it's
released, and then we can remove this override.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/utils/wrapper.py           |  8 +++++++-
 test/tempfile_fork_unittest.py | 10 +++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/lib/utils/wrapper.py b/lib/utils/wrapper.py
index 55cfbf214..bedc1da55 100644
--- a/lib/utils/wrapper.py
+++ b/lib/utils/wrapper.py
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
+# Copyright (C) 2006, 2007, 2010, 2011, 2012 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -22,6 +22,7 @@
 
 """
 
+import sys
 import time
 import socket
 import errno
@@ -182,6 +183,11 @@ def ResetTempfileModule(_time=time.time):
 
   """
   # pylint: disable=W0212
+  if ((sys.hexversion >= 0x020703F0 and sys.hexversion < 0x03000000) or
+      sys.hexversion >=0x030203F0):
+    # Python 2.7 automatically resets the RNG on pid changes (i.e. forking)
+    return
+
   try:
     lock = tempfile._once_lock
     lock.acquire()
diff --git a/test/tempfile_fork_unittest.py b/test/tempfile_fork_unittest.py
index 396c393a7..b794e33e0 100755
--- a/test/tempfile_fork_unittest.py
+++ b/test/tempfile_fork_unittest.py
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 
-# Copyright (C) 2010 Google Inc.
+# Copyright (C) 2010, 2012 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -47,6 +47,14 @@ class TestResetTempfileModule(unittest.TestCase):
     shutil.rmtree(self.tmpdir)
 
   def testNoReset(self):
+    if ((sys.hexversion >= 0x020703F0 and sys.hexversion < 0x03000000) or
+        sys.hexversion >=0x030203F0):
+      # We can't test the no_reset case on Python 2.7+
+      return
+    # evil Debian sid...
+    if (hasattr(tempfile._RandomNameSequence, "rng") and
+        type(tempfile._RandomNameSequence.rng) == property):
+      return
     self._Test(False)
 
   def testReset(self):
-- 
GitLab