1
+ from __future__ import print_function
1
2
import unittest
2
3
from pywin32_testutil import str2bytes , TestSkipped , testmain
3
4
import win32api , win32file , win32pipe , pywintypes , winerror , win32event
@@ -142,7 +143,9 @@ def testFileTimesTimezones(self):
142
143
# there is nothing you can do to avoid it being skipped!
143
144
return
144
145
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 )
146
149
now_local = now_utc .astimezone (win32timezone .TimeZoneInfo .local ())
147
150
h = win32file .CreateFile (filename ,
148
151
win32file .GENERIC_READ | win32file .GENERIC_WRITE ,
@@ -166,7 +169,9 @@ def testFileTimesTimezones(self):
166
169
def testFileTimes (self ):
167
170
if issubclass (pywintypes .TimeType , datetime .datetime ):
168
171
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 )
170
175
nowish = now + datetime .timedelta (seconds = 1 )
171
176
later = now + datetime .timedelta (seconds = 120 )
172
177
else :
@@ -195,15 +200,14 @@ def testFileTimes(self):
195
200
self .failUnless ( now <= wt <= nowish , (now , wt ))
196
201
197
202
# Now set the times.
198
- win32file .SetFileTime (f , later , later , later )
203
+ win32file .SetFileTime (f , later , later , later , UTCTimes = True )
199
204
# Get them back.
200
205
ct , at , wt = win32file .GetFileTime (f )
201
206
# XXX - the builtin PyTime type appears to be out by a dst offset.
202
207
# 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 )
207
211
208
212
finally :
209
213
f .Close ()
@@ -300,7 +304,7 @@ def testCompletionPortsMultiple(self):
300
304
try :
301
305
win32file .CloseHandle (hv )
302
306
raise RuntimeError ("Expected close to fail!" )
303
- except win32file .error , details :
307
+ except win32file .error as details :
304
308
self .failUnlessEqual (details .winerror , winerror .ERROR_INVALID_HANDLE )
305
309
306
310
def testCompletionPortsQueued (self ):
@@ -550,12 +554,12 @@ def _watcherThread(self, dn, dh, changes):
550
554
flags = win32con .FILE_NOTIFY_CHANGE_FILE_NAME
551
555
while 1 :
552
556
try :
553
- print "waiting" , dh
557
+ print ( "waiting" , dh )
554
558
changes = win32file .ReadDirectoryChangesW (dh ,
555
559
8192 ,
556
560
False , #sub-tree
557
561
flags )
558
- print "got" , changes
562
+ print ( "got" , changes )
559
563
except :
560
564
raise
561
565
changes .extend (changes )
@@ -588,7 +592,7 @@ def _watcherThreadOverlapped(self, dn, dh, changes):
588
592
# print "looks like dir handle was closed!"
589
593
return
590
594
else :
591
- print "ERROR: Watcher thread timed-out!"
595
+ print ( "ERROR: Watcher thread timed-out!" )
592
596
return # kill the thread!
593
597
594
598
def tearDown (self ):
@@ -602,13 +606,13 @@ def tearDown(self):
602
606
try :
603
607
shutil .rmtree (dn )
604
608
except OSError :
605
- print "FAILED to remove directory" , dn
609
+ print ( "FAILED to remove directory" , dn )
606
610
607
611
for t in self .watcher_threads :
608
612
# closing dir handle should have killed threads!
609
613
t .join (5 )
610
614
if t .isAlive ():
611
- print "FAILED to wait for thread termination"
615
+ print ( "FAILED to wait for thread termination" )
612
616
613
617
def stablize (self ):
614
618
time .sleep (0.5 )
@@ -643,10 +647,10 @@ def testEncrypt(self):
643
647
try :
644
648
try :
645
649
win32file .EncryptFile (fname )
646
- except win32file .error , details :
650
+ except win32file .error as details :
647
651
if details .winerror != winerror .ERROR_ACCESS_DENIED :
648
652
raise
649
- print "It appears this is not NTFS - cant encrypt/decrypt"
653
+ print ( "It appears this is not NTFS - cant encrypt/decrypt" )
650
654
win32file .DecryptFile (fname )
651
655
finally :
652
656
if f is not None :
@@ -703,7 +707,7 @@ def test_connect_with_payload(self):
703
707
s2 .bind (('0.0.0.0' , 0 )) # connectex requires the socket be bound beforehand
704
708
try :
705
709
win32file .ConnectEx (s2 , self .addr , ol , str2bytes ("some expected request" ))
706
- except win32file .error , exc :
710
+ except win32file .error as exc :
707
711
win32event .SetEvent (giveup_event )
708
712
if exc .winerror == 10022 : # WSAEINVAL
709
713
raise TestSkipped ("ConnectEx is not available on this platform" )
@@ -730,7 +734,7 @@ def test_connect_without_payload(self):
730
734
s2 .bind (('0.0.0.0' , 0 )) # connectex requires the socket be bound beforehand
731
735
try :
732
736
win32file .ConnectEx (s2 , self .addr , ol )
733
- except win32file .error , exc :
737
+ except win32file .error as exc :
734
738
win32event .SetEvent (giveup_event )
735
739
if exc .winerror == 10022 : # WSAEINVAL
736
740
raise TestSkipped ("ConnectEx is not available on this platform" )
@@ -830,11 +834,11 @@ def test_basics(self):
830
834
self .assertRaises (win32file .error , win32file .WSAEnumNetworkEvents , s , h )
831
835
try :
832
836
win32file .WSAEnumNetworkEvents (h )
833
- except win32file .error , e :
837
+ except win32file .error as e :
834
838
self .assertEquals (e .winerror , win32file .WSAENOTSOCK )
835
839
try :
836
840
win32file .WSAEnumNetworkEvents (s , h )
837
- except win32file .error , e :
841
+ except win32file .error as e :
838
842
# According to the docs it would seem reasonable that
839
843
# this would fail with WSAEINVAL, but it doesn't.
840
844
self .assertEquals (e .winerror , win32file .WSAENOTSOCK )
@@ -890,7 +894,7 @@ def test_functional(self):
890
894
while sent < 16 * 1024 * 1024 :
891
895
try :
892
896
sent += client .send (data )
893
- except socket .error , e :
897
+ except socket .error as e :
894
898
if e .args [0 ] == win32file .WSAEINTR :
895
899
continue
896
900
elif e .args [0 ] in (win32file .WSAEWOULDBLOCK , win32file .WSAENOBUFS ):
@@ -912,7 +916,7 @@ def test_functional(self):
912
916
while received < sent :
913
917
try :
914
918
received += len (server .recv (16 * 1024 ))
915
- except socket .error , e :
919
+ except socket .error as e :
916
920
if e .args [0 ] in [win32file .WSAEINTR , win32file .WSAEWOULDBLOCK ]:
917
921
continue
918
922
else :
0 commit comments