@@ -871,22 +871,36 @@ def ChangeMode(self, path, mode, follow_symlinks=True):
871
871
(mode & PERM_ALL ))
872
872
file_object .st_ctime = time .time ()
873
873
874
- def UpdateTime (self , path , times , follow_symlinks = True ):
874
+ def UpdateTime (self , path , times = None , ns = None , follow_symlinks = True ):
875
875
"""Change the access and modified times of a file.
876
876
New in pyfakefs 3.0.
877
877
878
878
Args:
879
- path: (str) Path to the file.
880
- times: 2-tuple of numbers, of the form (atime, mtime) which is used to set
881
- the access and modified times, respectively. If None, file's access
882
- and modified times are set to the current time.
883
- follow_symlinks: if False and entry_path points to a symlink, the link itself is queried
884
- instead of the linked object.
879
+ path: (str) Path to the file.
880
+ times: 2-tuple of int or float numbers, of the form (atime, mtime)
881
+ which is used to set the access and modified times in seconds.
882
+ If None, both times are set to the current time.
883
+ ns: 2-tuple of int numbers, of the form (atime, mtime) which is
884
+ used to set the access and modified times in nanoseconds.
885
+ If None, both times are set to the current time.
886
+ New in Python 3.3. New in pyfakefs 3.3.
887
+ follow_symlinks: If `False` and entry_path points to a symlink,
888
+ the link itself is queried instead of the linked object.
889
+ New in Python 3.3. New in pyfakefs 3.0.
890
+
891
+ Raises:
892
+ TypeError: If anything other than the expected types is
893
+ specified in the passed `times` or `ns` tuple,
894
+ or if the tuple length is not equal to 2.
895
+ ValueError: If both times and ns are specified.
896
+ """
897
+ if times is not None and ns is not None :
898
+ raise ValueError ("utime: you may specify either 'times' or 'ns' but not both" )
899
+ if times is not None and len (times ) != 2 :
900
+ raise TypeError ("utime: 'times' must be either a tuple of two ints or None" )
901
+ if ns is not None and len (ns ) != 2 :
902
+ raise TypeError ("utime: 'ns' must be a tuple of two ints" )
885
903
886
- Raises:
887
- TypeError: If anything other than integers is specified in passed tuple or
888
- number of elements in the tuple is not equal to 2.
889
- """
890
904
try :
891
905
file_object = self .ResolveObject (path , follow_symlinks )
892
906
except IOError as io_error :
@@ -895,18 +909,23 @@ def UpdateTime(self, path, times, follow_symlinks=True):
895
909
'No such file or directory in fake filesystem' ,
896
910
path )
897
911
raise
898
- if times is None :
899
- file_object .st_atime = time .time ()
900
- file_object .st_mtime = time .time ()
901
- else :
902
- if len (times ) != 2 :
903
- raise TypeError ('utime() arg 2 must be a tuple (atime, mtime)' )
912
+ if times is not None :
904
913
for file_time in times :
905
914
if not isinstance (file_time , (int , float )):
906
915
raise TypeError ('atime and mtime must be numbers' )
907
916
908
917
file_object .st_atime = times [0 ]
909
918
file_object .st_mtime = times [1 ]
919
+ elif ns is not None :
920
+ for file_time in ns :
921
+ if not isinstance (file_time , int ):
922
+ raise TypeError ('atime and mtime must be ints' )
923
+
924
+ file_object .st_atime = ns [0 ] / 1e9
925
+ file_object .st_mtime = ns [1 ] / 1e9
926
+ else :
927
+ file_object .st_atime = time .time ()
928
+ file_object .st_mtime = time .time ()
910
929
911
930
def SetIno (self , path , st_ino ):
912
931
"""Set the self.st_ino attribute of file at 'path'.
@@ -3359,26 +3378,36 @@ def lchmod(self, path, mode):
3359
3378
raise (NameError , "name 'lchmod' is not defined" )
3360
3379
self .filesystem .ChangeMode (path , mode , follow_symlinks = False )
3361
3380
3362
- def utime (self , path , times , follow_symlinks = None ):
3381
+ def utime (self , path , times = None , ns = None , follow_symlinks = None ):
3363
3382
"""Change the access and modified times of a file.
3364
3383
3365
3384
Args:
3366
- path: (str) Path to the file.
3367
- times: 2-tuple of numbers, of the form (atime, mtime) which is used to set
3368
- the access and modified times, respectively. If None, file's access
3369
- and modified times are set to the current time.
3370
- follow_symlinks: if False and entry_path points to a symlink, the link itself is queried
3371
- instead of the linked object. New in Python 3.3. New in pyfakefs 3.0.
3372
-
3373
- Raises:
3374
- TypeError: If anything other than integers is specified in passed tuple or
3375
- number of elements in the tuple is not equal to 2.
3385
+ path: (str) Path to the file.
3386
+ times: 2-tuple of int or float numbers, of the form (atime, mtime)
3387
+ which is used to set the access and modified times in seconds.
3388
+ If None, both times are set to the current time.
3389
+ ns: 2-tuple of int numbers, of the form (atime, mtime) which is
3390
+ used to set the access and modified times in nanoseconds.
3391
+ If None, both times are set to the current time.
3392
+ New in Python 3.3. New in pyfakefs 3.3.
3393
+ follow_symlinks: If `False` and entry_path points to a symlink,
3394
+ the link itself is queried instead of the linked object.
3395
+ New in Python 3.3. New in pyfakefs 3.0.
3396
+
3397
+ Raises:
3398
+ TypeError: If anything other than the expected types is
3399
+ specified in the passed `times` or `ns` tuple,
3400
+ or if the tuple length is not equal to 2.
3401
+ ValueError: If both times and ns are specified.
3376
3402
"""
3377
3403
if follow_symlinks is None :
3378
3404
follow_symlinks = True
3379
3405
elif sys .version_info < (3 , 3 ):
3380
3406
raise TypeError ("utime() got an unexpected keyword argument 'follow_symlinks'" )
3381
- self .filesystem .UpdateTime (path , times , follow_symlinks )
3407
+ if ns is not None and sys .version_info < (3 , 3 ):
3408
+ raise TypeError ("utime() got an unexpected keyword argument 'ns'" )
3409
+
3410
+ self .filesystem .UpdateTime (path , times , ns , follow_symlinks )
3382
3411
3383
3412
def chown (self , path , uid , gid , follow_symlinks = None ):
3384
3413
"""Set ownership of a faked file.
0 commit comments