@@ -67,25 +67,44 @@ def __call__(self):
67
67
68
68
69
69
def getWritableObject (ofile ):
70
- """try to make a writable file-like object from argument"""
70
+ """try to make a writable file-like object from argument. If the argument is None,
71
+ construct a new StringIO for writing
72
+
73
+ The second return value indicates whether the returned value needs to be closed.
74
+ Note that if a file-like object was passed in, it is assumed that this object
75
+ will be closed by the code that created it.
76
+
77
+ :Arguments:
78
+ - ofile: file-like object, string with the filename, or None
79
+ :Returns:
80
+ - a file-like object
81
+ - a bool indicating whether the file should be closed after writing
82
+ """
71
83
if ofile is None :
72
84
return StringIO (), False
73
85
elif type (ofile ) == str :
74
86
return open (ofile , 'w' ), True
75
- elif type (ofile ) in ( file , StringIO ):
87
+ elif hasattr (ofile , 'write' ):
76
88
return ofile , False
77
89
else :
78
90
raise Exception ("Can't make a writable object from argument!" )
79
91
80
92
81
93
def getReadableObject (ifile ):
82
- """try to make a readable file-like object from argument"""
94
+ """try to make a readable file-like object from argument
95
+
96
+ :Arguments:
97
+ - ifile: file-like object or string with the filename
98
+ :Returns:
99
+ - a file-like object
100
+ - a bool indicating whether the file should be closed after reading
101
+ """
83
102
if type (ifile ) == str :
84
103
try :
85
104
return open (ifile , 'r' ), True
86
105
except :
87
106
return StringIO (ifile ), True
88
- elif type (ifile ) in ( file , StringIO ):
107
+ elif hasattr (ifile , 'read' ):
89
108
return ifile , False
90
109
else :
91
110
raise Exception ("Can't make a readable object from argument!" )
0 commit comments