Skip to content

Commit 2aee7cb

Browse files
committed
test_win32file now passes time tests on py3 and on py2 with UTCTimes=True
1 parent a21c13f commit 2aee7cb

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

win32/test/test_win32file.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import unittest
23
from pywin32_testutil import str2bytes, TestSkipped, testmain
34
import win32api, win32file, win32pipe, pywintypes, winerror, win32event
@@ -142,7 +143,9 @@ def testFileTimesTimezones(self):
142143
# there is nothing you can do to avoid it being skipped!
143144
return
144145
filename = tempfile.mktemp("-testFileTimes")
145-
now_utc = win32timezone.utcnow()
146+
# now() is always returning a timestamp with microseconds but the
147+
# file APIs all have zero microseconds, so some comparisons fail.
148+
now_utc = win32timezone.utcnow().replace(microsecond=0)
146149
now_local = now_utc.astimezone(win32timezone.TimeZoneInfo.local())
147150
h = win32file.CreateFile(filename,
148151
win32file.GENERIC_READ|win32file.GENERIC_WRITE,
@@ -166,7 +169,9 @@ def testFileTimesTimezones(self):
166169
def testFileTimes(self):
167170
if issubclass(pywintypes.TimeType, datetime.datetime):
168171
from win32timezone import TimeZoneInfo
169-
now = datetime.datetime.now(tz=TimeZoneInfo.local())
172+
# now() is always returning a timestamp with microseconds but the
173+
# file APIs all have zero microseconds, so some comparisons fail.
174+
now = datetime.datetime.now(tz=TimeZoneInfo.utc()).replace(microsecond=0)
170175
nowish = now + datetime.timedelta(seconds=1)
171176
later = now + datetime.timedelta(seconds=120)
172177
else:
@@ -195,15 +200,14 @@ def testFileTimes(self):
195200
self.failUnless( now <= wt <= nowish, (now, wt))
196201

197202
# Now set the times.
198-
win32file.SetFileTime(f, later, later, later)
203+
win32file.SetFileTime(f, later, later, later, UTCTimes=True)
199204
# Get them back.
200205
ct, at, wt = win32file.GetFileTime(f)
201206
# XXX - the builtin PyTime type appears to be out by a dst offset.
202207
# just ignore that type here...
203-
if issubclass(pywintypes.TimeType, datetime.datetime):
204-
self.failUnlessEqual(ct, later)
205-
self.failUnlessEqual(at, later)
206-
self.failUnlessEqual(wt, later)
208+
self.failUnlessEqual(ct, later)
209+
self.failUnlessEqual(at, later)
210+
self.failUnlessEqual(wt, later)
207211

208212
finally:
209213
f.Close()
@@ -300,7 +304,7 @@ def testCompletionPortsMultiple(self):
300304
try:
301305
win32file.CloseHandle(hv)
302306
raise RuntimeError("Expected close to fail!")
303-
except win32file.error, details:
307+
except win32file.error as details:
304308
self.failUnlessEqual(details.winerror, winerror.ERROR_INVALID_HANDLE)
305309

306310
def testCompletionPortsQueued(self):
@@ -550,12 +554,12 @@ def _watcherThread(self, dn, dh, changes):
550554
flags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME
551555
while 1:
552556
try:
553-
print "waiting", dh
557+
print("waiting", dh)
554558
changes = win32file.ReadDirectoryChangesW(dh,
555559
8192,
556560
False, #sub-tree
557561
flags)
558-
print "got", changes
562+
print("got", changes)
559563
except:
560564
raise
561565
changes.extend(changes)
@@ -588,7 +592,7 @@ def _watcherThreadOverlapped(self, dn, dh, changes):
588592
# print "looks like dir handle was closed!"
589593
return
590594
else:
591-
print "ERROR: Watcher thread timed-out!"
595+
print("ERROR: Watcher thread timed-out!")
592596
return # kill the thread!
593597

594598
def tearDown(self):
@@ -602,13 +606,13 @@ def tearDown(self):
602606
try:
603607
shutil.rmtree(dn)
604608
except OSError:
605-
print "FAILED to remove directory", dn
609+
print("FAILED to remove directory", dn)
606610

607611
for t in self.watcher_threads:
608612
# closing dir handle should have killed threads!
609613
t.join(5)
610614
if t.isAlive():
611-
print "FAILED to wait for thread termination"
615+
print("FAILED to wait for thread termination")
612616

613617
def stablize(self):
614618
time.sleep(0.5)
@@ -643,10 +647,10 @@ def testEncrypt(self):
643647
try:
644648
try:
645649
win32file.EncryptFile(fname)
646-
except win32file.error, details:
650+
except win32file.error as details:
647651
if details.winerror != winerror.ERROR_ACCESS_DENIED:
648652
raise
649-
print "It appears this is not NTFS - cant encrypt/decrypt"
653+
print("It appears this is not NTFS - cant encrypt/decrypt")
650654
win32file.DecryptFile(fname)
651655
finally:
652656
if f is not None:
@@ -703,7 +707,7 @@ def test_connect_with_payload(self):
703707
s2.bind(('0.0.0.0', 0)) # connectex requires the socket be bound beforehand
704708
try:
705709
win32file.ConnectEx(s2, self.addr, ol, str2bytes("some expected request"))
706-
except win32file.error, exc:
710+
except win32file.error as exc:
707711
win32event.SetEvent(giveup_event)
708712
if exc.winerror == 10022: # WSAEINVAL
709713
raise TestSkipped("ConnectEx is not available on this platform")
@@ -730,7 +734,7 @@ def test_connect_without_payload(self):
730734
s2.bind(('0.0.0.0', 0)) # connectex requires the socket be bound beforehand
731735
try:
732736
win32file.ConnectEx(s2, self.addr, ol)
733-
except win32file.error, exc:
737+
except win32file.error as exc:
734738
win32event.SetEvent(giveup_event)
735739
if exc.winerror == 10022: # WSAEINVAL
736740
raise TestSkipped("ConnectEx is not available on this platform")
@@ -830,11 +834,11 @@ def test_basics(self):
830834
self.assertRaises(win32file.error, win32file.WSAEnumNetworkEvents, s, h)
831835
try:
832836
win32file.WSAEnumNetworkEvents(h)
833-
except win32file.error, e:
837+
except win32file.error as e:
834838
self.assertEquals(e.winerror, win32file.WSAENOTSOCK)
835839
try:
836840
win32file.WSAEnumNetworkEvents(s, h)
837-
except win32file.error, e:
841+
except win32file.error as e:
838842
# According to the docs it would seem reasonable that
839843
# this would fail with WSAEINVAL, but it doesn't.
840844
self.assertEquals(e.winerror, win32file.WSAENOTSOCK)
@@ -890,7 +894,7 @@ def test_functional(self):
890894
while sent < 16 * 1024 * 1024:
891895
try:
892896
sent += client.send(data)
893-
except socket.error, e:
897+
except socket.error as e:
894898
if e.args[0] == win32file.WSAEINTR:
895899
continue
896900
elif e.args[0] in (win32file.WSAEWOULDBLOCK, win32file.WSAENOBUFS):
@@ -912,7 +916,7 @@ def test_functional(self):
912916
while received < sent:
913917
try:
914918
received += len(server.recv(16 * 1024))
915-
except socket.error, e:
919+
except socket.error as e:
916920
if e.args[0] in [win32file.WSAEINTR, win32file.WSAEWOULDBLOCK]:
917921
continue
918922
else:

0 commit comments

Comments
 (0)