23
23
# https://www.gnu.org/licenses/
24
24
# ****************************************************************************
25
25
26
- import io
26
+ import atexit
27
27
import os
28
28
import tempfile
29
-
30
- import atexit
29
+ from typing import IO
31
30
32
31
# Until tmp_dir() and tmp_filename() are removed, we use this directory
33
32
# as the parent for all temporary files & directories created by them.
41
40
# temporary directory
42
41
#################################################################
43
42
44
- def tmp_dir (name = 'dir_' , ext = '' ):
43
+ def tmp_dir (name = 'dir_' , ext = '' ) -> str :
45
44
r"""
46
45
Create and return a temporary directory in
47
46
``$HOME/.sage/temp/hostname/pid/``
@@ -84,7 +83,7 @@ def tmp_dir(name='dir_', ext=''):
84
83
# temporary filename
85
84
#################################################################
86
85
87
- def tmp_filename (name = 'tmp_' , ext = '' ):
86
+ def tmp_filename (name = 'tmp_' , ext = '' ) -> str :
88
87
r"""
89
88
Create and return a temporary file in
90
89
``$HOME/.sage/temp/hostname/pid/``
@@ -163,8 +162,8 @@ class atomic_write:
163
162
mode bits of the file were changed manually). (Not to be confused with
164
163
the file opening mode.)
165
164
166
- - ``binary`` -- boolean (default: ``True`` on Python 2, `` False`` on Python
167
- 3); the underlying file is opened in binary mode. If ``False`` then it is
165
+ - ``binary`` -- boolean (default: ``False``);
166
+ the underlying file is opened in binary mode. If ``False`` then it is
168
167
opened in text mode and an encoding with which to write the file may be
169
168
supplied.
170
169
@@ -299,7 +298,7 @@ class atomic_write:
299
298
False
300
299
"""
301
300
def __init__ (self , target_filename , append = False , mode = 0o666 ,
302
- binary = None , ** kwargs ):
301
+ binary = False , ** kwargs ) -> None :
303
302
"""
304
303
TESTS::
305
304
@@ -320,13 +319,11 @@ def __init__(self, target_filename, append=False, mode=0o666,
320
319
os .umask (umask )
321
320
self .mode = mode & (~ umask )
322
321
323
- # 'binary' mode is the default on Python 2, whereas 'text' mode is the
324
- # default on Python 3--this reflects consistent handling of the default
325
- # str type on the two platforms
326
- self .binary = False if binary is None else binary
322
+ # 'text' mode is the default on Python 3
323
+ self .binary = binary
327
324
self .kwargs = kwargs
328
325
329
- def __enter__ (self ):
326
+ def __enter__ (self ) -> IO :
330
327
"""
331
328
Create and return a temporary file in ``self.tmpdir`` (normally
332
329
the same directory as the target file).
@@ -372,7 +369,7 @@ def __enter__(self):
372
369
373
370
return self .tempfile
374
371
375
- def __exit__ (self , exc_type , exc_val , exc_tb ):
372
+ def __exit__ (self , exc_type , exc_val , exc_tb ) -> None :
376
373
"""
377
374
If the ``with`` block was successful, move the temporary file
378
375
to the target file. Otherwise, delete the temporary file.
@@ -457,7 +454,7 @@ class atomic_dir:
457
454
....: h.read()
458
455
'Second'
459
456
"""
460
- def __init__ (self , target_directory ):
457
+ def __init__ (self , target_directory ) -> None :
461
458
r"""
462
459
TESTS::
463
460
@@ -492,7 +489,7 @@ def __enter__(self):
492
489
self .tempname = os .path .abspath (tdir .name )
493
490
return tdir
494
491
495
- def __exit__ (self , exc_type , exc_val , exc_tb ):
492
+ def __exit__ (self , exc_type , exc_val , exc_tb ) -> None :
496
493
"""
497
494
If the ``with`` block was successful, move the temporary directory
498
495
to the target directory. Otherwise, delete the temporary directory.
@@ -518,7 +515,8 @@ def __exit__(self, exc_type, exc_val, exc_tb):
518
515
try :
519
516
os .rename (self .tempname , self .target )
520
517
except OSError :
521
- # Race: Another thread or process must have created the directory
518
+ # Race: Another thread or process must have created
519
+ # the directory
522
520
pass
523
521
else :
524
522
# Failure: delete temporary file
@@ -528,7 +526,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
528
526
_spyx_tmp = None
529
527
530
528
531
- def spyx_tmp ():
529
+ def spyx_tmp () -> str :
532
530
r"""
533
531
The temporary directory used to store pyx files.
534
532
0 commit comments