[Zope3-checkins] CVS: Zope3/lib/python/Zope/Configuration/tests - testXML.py:1.6

Tim Peters tim.one@comcast.net
Wed, 13 Nov 2002 15:28:07 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Configuration/tests
In directory cvs.zope.org:/tmp/cvs-serv1238/lib/python/Zope/Configuration/tests

Modified Files:
	testXML.py 
Log Message:
The use of NamedTemporaryFile here doesn't work on Windows flavors at or
after Win2K, and that's not shallow (temp files are faked on Windows
via the Win O_TEMPORARY flag, and that has more than one effect on
file semantics).  So used a different gimmick that should work everywhere.


=== Zope3/lib/python/Zope/Configuration/tests/testXML.py 1.5 => 1.6 ===
--- Zope3/lib/python/Zope/Configuration/tests/testXML.py:1.5	Fri Nov  8 14:04:48 2002
+++ Zope3/lib/python/Zope/Configuration/tests/testXML.py	Wed Nov 13 15:28:06 2002
@@ -2,32 +2,43 @@
 #
 # Copyright (c) 2001, 2002 Zope Corporation and Contributors.
 # All Rights Reserved.
-# 
+#
 # This software is subject to the provisions of the Zope Public License,
 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
-# 
+#
 ##############################################################################
 import sys, unittest
 from cStringIO import StringIO
-try: from tempfile import NamedTemporaryFile
-except ImportError:
-    # 2.2 backward compatability
-    from tempfile import mktemp
+
+# Caution:  tempfile.NamedTemporaryFile cannot be used on Windows, because
+# the tests here want to open the file more than once.  You can't do that
+# with an O_TEMPORARY file on Win2K (or higher).
+import tempfile
+
+class TempFile:
     from os import remove
-    class NamedTemporaryFile:
-        def __init__(self):
-            self.file = open(mktemp(),'w')
-        def write(self,buffer): self.file.write(buffer)
-        def _name(self): return self.file.name
-        name = property(_name)
-        def flush(self): self.file.flush()
-        def close(self):
+
+    def __init__(self):
+        self.file = open(tempfile.mktemp(), 'w')
+        self.closed = 0
+    def write(self,buffer):
+        self.file.write(buffer)
+    def _name(self):
+        return self.file.name
+    name = property(_name)
+    def flush(self):
+        self.file.flush()
+    def close(self):
+        if not self.closed:
             self.file.close()
-            remove(self.file.name)
+            self.remove(self.file.name)
+            self.closed = 1
+    def __del__(self):
+        self.close()
 
 from Zope.Configuration.xmlconfig import xmlconfig
 from Zope.Configuration.xmlconfig import testxmlconfig
@@ -35,9 +46,9 @@
 from Zope.Testing.CleanUp import CleanUp
 
 class Test(CleanUp, unittest.TestCase):
-        
+
     def testInclude(self):
-        file = NamedTemporaryFile()
+        file = TempFile()
         name = file.name
         file.write(
             """<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
@@ -53,12 +64,12 @@
 
     def testIncludeNoPackageAndIncluderNoPackage(self):
         from os.path import split
-        file = NamedTemporaryFile()
+        file = TempFile()
         full_name = file.name
-        file1 = NamedTemporaryFile()
+        file1 = TempFile()
         full_name1 = file1.name
         name1 = split(full_name1)[-1]
-        
+
         file.write(
             """<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
                  <include file="%s" />