tempfile 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tempfile.rb +57 -19
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 441e2cb96bc4f1d0c808dbde645e2985849607d7edd297b2e26fa427094eaa59
4
- data.tar.gz: fcaf9af92f27ac9c91877ae3542dcada76e95e4c31196fbf01fa7a6213d47fea
3
+ metadata.gz: ce57b45a244b1e39fa3cb162b28a3d0a858d59083441ff31cb0dc5237c9d029f
4
+ data.tar.gz: 85d0949db220dc68bd4cef4e0bd98acd5629ef2c61af33a0c69acec346098f1e
5
5
  SHA512:
6
- metadata.gz: 664dd5cdf708cf51e87f1ed8b375bf754c48dfecac717c119cfff8298018132762a4debc1b65d420128071ef6db28f893e589a5f0dc6a5e13738afc167c7436b
7
- data.tar.gz: 4237b4700b4a8d365931e286e1e199a79c362f70ce9349d1c19bc43fb2a78f461b384e4ba1e49c7ca8967d0ae474865018c45481c8813d979bf63a47561cd2bf
6
+ metadata.gz: 25265f76b8e05707c0a20f0218a28f316f1439659c1c27a40a17b04f70bf01032d4c1449e55c688f1799ddb86420f365f16fa663400933d3daa0f83ee533989f
7
+ data.tar.gz: 324cffaebce2f03134f06bd2646fe79bd71c44ca11ef7f8f2a6d4cf680726f55c7405a5497943295f1a332e8ad6a7e42b0bc69ab48491d41976044c66674ab65
data/lib/tempfile.rb CHANGED
@@ -88,7 +88,7 @@ require 'tmpdir'
88
88
  # mutex.
89
89
  class Tempfile < DelegateClass(File)
90
90
 
91
- VERSION = "0.2.0"
91
+ VERSION = "0.2.1"
92
92
 
93
93
  # Creates a file in the underlying file system;
94
94
  # returns a new \Tempfile object based on that file.
@@ -152,26 +152,50 @@ class Tempfile < DelegateClass(File)
152
152
 
153
153
  @unlinked = false
154
154
  @mode = mode|File::RDWR|File::CREAT|File::EXCL
155
+ @finalizer_obj = Object.new
156
+ tmpfile = nil
155
157
  ::Dir::Tmpname.create(basename, tmpdir, **options) do |tmpname, n, opts|
156
158
  opts[:perm] = 0600
157
- @tmpfile = File.open(tmpname, @mode, **opts)
159
+ tmpfile = File.open(tmpname, @mode, **opts)
158
160
  @opts = opts.freeze
159
161
  end
160
- ObjectSpace.define_finalizer(self, Remover.new(@tmpfile))
162
+ ObjectSpace.define_finalizer(@finalizer_obj, Remover.new(tmpfile.path))
163
+ ObjectSpace.define_finalizer(self, Closer.new(tmpfile))
161
164
 
162
- super(@tmpfile)
165
+ super(tmpfile)
166
+ end
167
+
168
+ def initialize_dup(other)
169
+ initialize_copy_iv(other)
170
+ super(other)
171
+ ObjectSpace.define_finalizer(self, Closer.new(__getobj__))
172
+ end
173
+
174
+ def initialize_clone(other)
175
+ initialize_copy_iv(other)
176
+ super(other)
177
+ ObjectSpace.define_finalizer(self, Closer.new(__getobj__))
178
+ end
179
+
180
+ private def initialize_copy_iv(other)
181
+ @unlinked = other.unlinked
182
+ @mode = other.mode
183
+ @opts = other.opts
184
+ @finalizer_obj = other.finalizer_obj
163
185
  end
164
186
 
165
187
  # Opens or reopens the file with mode "r+".
166
188
  def open
167
189
  _close
190
+ ObjectSpace.undefine_finalizer(self)
168
191
  mode = @mode & ~(File::CREAT|File::EXCL)
169
- @tmpfile = File.open(@tmpfile.path, mode, **@opts)
170
- __setobj__(@tmpfile)
192
+ __setobj__(File.open(__getobj__.path, mode, **@opts))
193
+ ObjectSpace.define_finalizer(self, Closer.new(__getobj__))
194
+ __getobj__
171
195
  end
172
196
 
173
197
  def _close # :nodoc:
174
- @tmpfile.close
198
+ __getobj__.close
175
199
  end
176
200
  protected :_close
177
201
 
@@ -228,13 +252,13 @@ class Tempfile < DelegateClass(File)
228
252
  def unlink
229
253
  return if @unlinked
230
254
  begin
231
- File.unlink(@tmpfile.path)
255
+ File.unlink(__getobj__.path)
232
256
  rescue Errno::ENOENT
233
257
  rescue Errno::EACCES
234
258
  # may not be able to unlink on Windows; just ignore
235
259
  return
236
260
  end
237
- ObjectSpace.undefine_finalizer(self)
261
+ ObjectSpace.undefine_finalizer(@finalizer_obj)
238
262
  @unlinked = true
239
263
  end
240
264
  alias delete unlink
@@ -242,43 +266,57 @@ class Tempfile < DelegateClass(File)
242
266
  # Returns the full path name of the temporary file.
243
267
  # This will be nil if #unlink has been called.
244
268
  def path
245
- @unlinked ? nil : @tmpfile.path
269
+ @unlinked ? nil : __getobj__.path
246
270
  end
247
271
 
248
272
  # Returns the size of the temporary file. As a side effect, the IO
249
273
  # buffer is flushed before determining the size.
250
274
  def size
251
- if !@tmpfile.closed?
252
- @tmpfile.size # File#size calls rb_io_flush_raw()
275
+ if !__getobj__.closed?
276
+ __getobj__.size # File#size calls rb_io_flush_raw()
253
277
  else
254
- File.size(@tmpfile.path)
278
+ File.size(__getobj__.path)
255
279
  end
256
280
  end
257
281
  alias length size
258
282
 
259
283
  # :stopdoc:
260
284
  def inspect
261
- if @tmpfile.closed?
285
+ if __getobj__.closed?
262
286
  "#<#{self.class}:#{path} (closed)>"
263
287
  else
264
288
  "#<#{self.class}:#{path}>"
265
289
  end
266
290
  end
291
+ alias to_s inspect
267
292
 
268
- class Remover # :nodoc:
293
+ protected
294
+
295
+ attr_reader :unlinked, :mode, :opts, :finalizer_obj
296
+
297
+ class Closer # :nodoc:
269
298
  def initialize(tmpfile)
270
- @pid = Process.pid
271
299
  @tmpfile = tmpfile
272
300
  end
273
301
 
302
+ def call(*args)
303
+ @tmpfile.close
304
+ end
305
+ end
306
+
307
+ class Remover # :nodoc:
308
+ def initialize(path)
309
+ @pid = Process.pid
310
+ @path = path
311
+ end
312
+
274
313
  def call(*args)
275
314
  return if @pid != Process.pid
276
315
 
277
- $stderr.puts "removing #{@tmpfile.path}..." if $DEBUG
316
+ $stderr.puts "removing #{@path}..." if $DEBUG
278
317
 
279
- @tmpfile.close
280
318
  begin
281
- File.unlink(@tmpfile.path)
319
+ File.unlink(@path)
282
320
  rescue Errno::ENOENT
283
321
  end
284
322
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tempfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-07 00:00:00.000000000 Z
11
+ date: 2023-12-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A utility class for managing temporary files.
14
14
  email: