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