-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathtempfile.go
More file actions
283 lines (235 loc) · 7.03 KB
/
tempfile.go
File metadata and controls
283 lines (235 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package tempfile provides the implementation of the python's 'tempfile' module.
package tempfile
import (
"fmt"
"os"
"github.com/go-python/gpython/py"
)
var (
gblTempDir py.Object = py.None
)
const tempfile_doc = `Temporary files.
This module provides generic, low- and high-level interfaces for
creating temporary files and directories. All of the interfaces
provided by this module can be used without fear of race conditions
except for 'mktemp'. 'mktemp' is subject to race conditions and
should not be used; it is provided for backward compatibility only.
The default path names are returned as str. If you supply bytes as
input, all return values will be in bytes. Ex:
>>> tempfile.mkstemp()
(4, '/tmp/tmptpu9nin8')
>>> tempfile.mkdtemp(suffix=b'')
b'/tmp/tmppbi8f0hy'
This module also provides some data items to the user:
TMP_MAX - maximum number of names that will be tried before
giving up.
tempdir - If this is set to a string before the first use of
any routine from this module, it will be considered as
another candidate location to store temporary files.`
func init() {
py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "tempfile",
Doc: tempfile_doc,
},
Methods: []*py.Method{
py.MustNewMethod("gettempdir", gettempdir, 0, gettempdir_doc),
py.MustNewMethod("gettempdirb", gettempdirb, 0, gettempdirb_doc),
py.MustNewMethod("mkdtemp", mkdtemp, 0, mkdtemp_doc),
py.MustNewMethod("mkstemp", mkstemp, 0, mkstemp_doc),
},
Globals: py.StringDict{
"tempdir": gblTempDir,
},
})
}
const gettempdir_doc = `Returns tempfile.tempdir as str.`
func gettempdir(self py.Object) (py.Object, error) {
// FIXME(sbinet): lock access to glbTempDir?
if gblTempDir != py.None {
switch dir := gblTempDir.(type) {
case py.String:
return dir, nil
case py.Bytes:
return py.String(dir), nil
default:
return nil, py.ExceptionNewf(py.TypeError, "expected str, bytes or os.PathLike object, not %s", dir.Type().Name)
}
}
return py.String(os.TempDir()), nil
}
const gettempdirb_doc = `Returns tempfile.tempdir as bytes.`
func gettempdirb(self py.Object) (py.Object, error) {
// FIXME(sbinet): lock access to glbTempDir?
if gblTempDir != py.None {
switch dir := gblTempDir.(type) {
case py.String:
return py.Bytes(dir), nil
case py.Bytes:
return dir, nil
default:
return nil, py.ExceptionNewf(py.TypeError, "expected str, bytes or os.PathLike object, not %s", dir.Type().Name)
}
}
return py.Bytes(os.TempDir()), nil
}
const mkdtemp_doc = `mkdtemp(suffix=None, prefix=None, dir=None)
User-callable function to create and return a unique temporary
directory. The return value is the pathname of the directory.
Arguments are as for mkstemp, except that the 'text' argument is
not accepted.
The directory is readable, writable, and searchable only by the
creating user.
Caller is responsible for deleting the directory when done with it.`
func mkdtemp(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pysuffix py.Object = py.None
pyprefix py.Object = py.None
pydir py.Object = py.None
)
err := py.ParseTupleAndKeywords(args, kwargs,
"|z#z#z#:mkdtemp",
[]string{"suffix", "prefix", "dir"},
&pysuffix, &pyprefix, &pydir,
)
if err != nil {
return nil, err
}
str := func(v py.Object, typ *uint8) string {
switch v := v.(type) {
case py.Bytes:
*typ = 2
return string(v)
case py.String:
*typ = 1
return string(v)
case py.NoneType:
*typ = 0
return ""
default:
panic(fmt.Errorf("tempfile: invalid type %T (v=%+v)", v, v))
}
}
var (
t1, t2, t3 uint8
suffix = str(pysuffix, &t1)
prefix = str(pyprefix, &t2)
dir = str(pydir, &t3)
pattern = prefix + "*" + suffix
)
cmp := func(t1, t2 uint8) bool {
if t1 > 0 && t2 > 0 {
return t1 == t2
}
return true
}
if !cmp(t1, t2) || !cmp(t1, t3) || !cmp(t2, t3) {
return nil, py.ExceptionNewf(py.TypeError, "Can't mix bytes and non-bytes in path components")
}
tmp, err := os.MkdirTemp(dir, pattern)
if err != nil {
return nil, err
}
typ := t1
if typ == 0 {
typ = t2
}
if typ == 0 {
typ = t3
}
switch typ {
case 2:
return py.Bytes(tmp), nil
default:
return py.String(tmp), nil
}
}
const mkstemp_doc = `mkstemp(suffix=None, prefix=None, dir=None, text=False)
User-callable function to create and return a unique temporary
file. The return value is a pair (fd, name) where fd is the
file descriptor returned by os.open, and name is the filename.
If 'suffix' is not None, the file name will end with that suffix,
otherwise there will be no suffix.
If 'prefix' is not None, the file name will begin with that prefix,
otherwise a default prefix is used.
If 'dir' is not None, the file will be created in that directory,
otherwise a default directory is used.
If 'text' is specified and true, the file is opened in text
mode. Else (the default) the file is opened in binary mode.
If any of 'suffix', 'prefix' and 'dir' are not None, they must be the
same type. If they are bytes, the returned name will be bytes; str
otherwise.
The file is readable and writable only by the creating user ID.
If the operating system uses permission bits to indicate whether a
file is executable, the file is executable by no one. The file
descriptor is not inherited by children of this process.
Caller is responsible for deleting the file when done with it.`
func mkstemp(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pysuffix py.Object = py.None
pyprefix py.Object = py.None
pydir py.Object = py.None
pytext py.Object = py.False // FIXME(sbinet): can we do something with that?
)
err := py.ParseTupleAndKeywords(args, kwargs,
"|z#z#z#p:mkstemp",
[]string{"suffix", "prefix", "dir", "text"},
&pysuffix, &pyprefix, &pydir, &pytext,
)
if err != nil {
return nil, err
}
str := func(v py.Object, typ *uint8) string {
switch v := v.(type) {
case py.Bytes:
*typ = 2
return string(v)
case py.String:
*typ = 1
return string(v)
case py.NoneType:
*typ = 0
return ""
default:
panic(fmt.Errorf("tempfile: invalid type %T (v=%+v)", v, v))
}
}
var (
t1, t2, t3 uint8
suffix = str(pysuffix, &t1)
prefix = str(pyprefix, &t2)
dir = str(pydir, &t3)
pattern = prefix + "*" + suffix
)
cmp := func(t1, t2 uint8) bool {
if t1 > 0 && t2 > 0 {
return t1 == t2
}
return true
}
if !cmp(t1, t2) || !cmp(t1, t3) || !cmp(t2, t3) {
return nil, py.ExceptionNewf(py.TypeError, "Can't mix bytes and non-bytes in path components")
}
f, err := os.CreateTemp(dir, pattern)
if err != nil {
return nil, err
}
typ := t1
if typ == 0 {
typ = t2
}
if typ == 0 {
typ = t3
}
tuple := py.Tuple{py.Int(f.Fd())}
switch typ {
case 2:
tuple = append(tuple, py.Bytes(f.Name()))
default:
tuple = append(tuple, py.String(f.Name()))
}
return tuple, nil
}