44
44
GHOSTSCRIPT_BINARY = shutil .which ("gs" )
45
45
46
46
47
+ def _get_write_target (convert ) -> Any :
48
+ target = convert
49
+ if callable (convert ):
50
+ with NamedTemporaryFile (suffix = ".pdf" , delete = False ) as temporary :
51
+ target = temporary .name
52
+ return target
53
+
54
+
47
55
def test_writer_exception_non_binary (tmp_path , caplog ):
48
56
src = RESOURCE_ROOT / "pdflatex-outline.pdf"
49
57
@@ -223,14 +231,8 @@ def writer_operate(writer: PdfWriter) -> None:
223
231
],
224
232
)
225
233
def test_writer_operations_by_traditional_usage (convert , needs_cleanup ):
226
- if callable (convert ):
227
- with NamedTemporaryFile (suffix = ".pdf" , delete = False ) as fo :
228
- write_data_here = convert (fo .name )
229
- else :
230
- write_data_here = convert
231
-
234
+ write_data_here = _get_write_target (convert )
232
235
writer = PdfWriter ()
233
-
234
236
writer_operate (writer )
235
237
236
238
# finally, write "output" to pypdf-output.pdf
@@ -254,11 +256,7 @@ def test_writer_operations_by_traditional_usage(convert, needs_cleanup):
254
256
],
255
257
)
256
258
def test_writer_operations_by_semi_traditional_usage (convert , needs_cleanup ):
257
- if callable (convert ):
258
- with NamedTemporaryFile (suffix = ".pdf" , delete = False ) as fo :
259
- write_data_here = convert (fo .name )
260
- else :
261
- write_data_here = convert
259
+ write_data_here = _get_write_target (convert )
262
260
263
261
with PdfWriter () as writer :
264
262
writer_operate (writer )
@@ -283,12 +281,10 @@ def test_writer_operations_by_semi_traditional_usage(convert, needs_cleanup):
283
281
(BytesIO (), False ),
284
282
],
285
283
)
286
- def test_writer_operations_by_semi_new_traditional_usage (convert , needs_cleanup ):
287
- if callable (convert ):
288
- with NamedTemporaryFile (suffix = ".pdf" , delete = False ) as fo :
289
- write_data_here = convert (fo .name )
290
- else :
291
- write_data_here = convert
284
+ def test_writer_operations_by_semi_new_traditional_usage (
285
+ convert , needs_cleanup
286
+ ):
287
+ write_data_here = _get_write_target (convert )
292
288
293
289
with PdfWriter () as writer :
294
290
writer_operate (writer )
@@ -309,11 +305,7 @@ def test_writer_operations_by_semi_new_traditional_usage(convert, needs_cleanup)
309
305
],
310
306
)
311
307
def test_writer_operation_by_new_usage (convert , needs_cleanup ):
312
- if callable (convert ):
313
- with NamedTemporaryFile (suffix = ".pdf" , delete = False ) as fo :
314
- write_data_here = convert (fo .name )
315
- else :
316
- write_data_here = convert
308
+ write_data_here = _get_write_target (convert )
317
309
318
310
# This includes write "output" to pypdf-output.pdf
319
311
with PdfWriter (write_data_here ) as writer :
0 commit comments