Skip to content

Commit ef88c2e

Browse files
committed
Fix Tempfile handling in output_data
1 parent 838c1bc commit ef88c2e

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

lib/youplot/command.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,12 @@ def output_data(input)
184184
# Then handle path strings and IO-like objects.
185185
case out
186186
when Tempfile
187-
File.open(out.path, 'w') do |f|
188-
f.print(input)
189-
end
187+
# Keep file descriptor state consistent with the Tempfile object.
188+
out.truncate(0) # clear existing content
189+
out.rewind # move pointer to the beginning
190+
out.print(input) # write new content
191+
out.flush # ensure content is written to disk
192+
out.rewind # move pointer back to the beginning for out.read
190193
when String
191194
File.open(out, 'w') do |f|
192195
f.print(input)
@@ -203,9 +206,12 @@ def output_plot(plot)
203206
# Then handle path strings and IO-like objects.
204207
case out
205208
when Tempfile
206-
File.open(out.path, 'w') do |f|
207-
plot.render(f)
208-
end
209+
# Keep file descriptor state consistent with the Tempfile object.
210+
out.truncate(0) # clear existing content
211+
out.rewind # move pointer to the beginning
212+
plot.render(out) # write new content
213+
out.flush # ensure content is written to disk
214+
out.rewind # move pointer back to the beginning for out.read
209215
when String
210216
File.open(out, 'w') do |f|
211217
plot.render(f)

0 commit comments

Comments
 (0)