From 2c389836e3e202b185349313855a3763cd13724d Mon Sep 17 00:00:00 2001 From: Tolker-KU Date: Wed, 26 Jul 2023 17:10:24 +0200 Subject: [PATCH] Fix stacklevel of warnings --- CHANGELOG.md | 1 + fpdf/deprecation.py | 28 +++++++++++++++++-- fpdf/fpdf.py | 47 +++++++++++++++++++------------- fpdf/html.py | 3 ++- fpdf/recorder.py | 4 ++- fpdf/template.py | 7 ++--- test/errors/test_FPDF_errors.py | 3 ++- test/fonts/test_add_font.py | 48 ++++++++++++++++++++++++++------- test/fonts/test_set_font.py | 10 +++++-- test/html/test_html.py | 5 +++- test/shapes/test_polyline.py | 5 +++- test/test_output.py | 4 ++- test/text/test_cell.py | 10 +++++-- test/text/test_multi_cell.py | 9 +++++-- test/text/test_render_styled.py | 11 ++++++-- test/text/test_unbreakable.py | 9 +++++-- 16 files changed, 154 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9e2ace6b..05a310ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default', - text overflow is better handled by `FPDF.write()` & `FPDF.write_html()` - _cf._ [issue #847](https://github.com/PyFPDF/fpdf2/issues/847) - the initial text color is preserved when using `FPDF.write_html()` - _cf._ [issue #846](https://github.com/PyFPDF/fpdf2/issues/846) - handle superscript and subscript correctly when rendering `TextLine`- [Pull Request #862](https://github.com/PyFPDF/fpdf2/pull/862) +- make sure warnings always point to the users code - _cf._ [Pull request #869](https://github.com/PyFPDF/fpdf2/pull/869) ### Deprecated - the `center` optional parameter of [`FPDF.cell()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.cell) is **no more** deprecated, as it allows for horizontal positioning, which is different from text alignment control with `align="C"` diff --git a/fpdf/deprecation.py b/fpdf/deprecation.py index 194587e0f..7ad6b42e1 100644 --- a/fpdf/deprecation.py +++ b/fpdf/deprecation.py @@ -1,3 +1,6 @@ +import contextlib +import inspect +import os.path import warnings from types import ModuleType @@ -19,7 +22,7 @@ def __getattr__(self, name): " have been deprecated in favour of" " FPDF(font_cache_dir=...)", DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) return None return super().__getattribute__(name) @@ -32,7 +35,28 @@ def __setattr__(self, name, value): " have been deprecated in favour of" " FPDF(font_cache_dir=...)", DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) return super().__setattr__(name, value) + + +def get_stack_level() -> int: + """Get the first place in the call stack that is not inside fpdf2""" + + # pylint: disable=import-outside-toplevel + import fpdf # pylint: disable=cyclic-import + + pkg_dir = os.path.dirname(fpdf.__file__) + contextlib_dir = os.path.dirname(contextlib.__file__) + + frame = inspect.currentframe() + n = 0 + while frame is not None: + fname = inspect.getfile(frame) + if fname.startswith(pkg_dir) or fname.startswith(contextlib_dir): + frame = frame.f_back + n += 1 + else: + break + return n diff --git a/fpdf/fpdf.py b/fpdf/fpdf.py index c1851c458..cea9afb22 100644 --- a/fpdf/fpdf.py +++ b/fpdf/fpdf.py @@ -47,7 +47,7 @@ class Image: PDFEmbeddedFile, DEFAULT_ANNOT_FLAGS, ) -from .deprecation import WarnOnDeprecatedModuleAttributes +from .deprecation import get_stack_level, WarnOnDeprecatedModuleAttributes from .encryption import StandardSecurityHandler from .enums import ( AccessPermission, @@ -257,7 +257,7 @@ def __init__( warnings.warn( '"font_cache_dir" parameter is deprecated, unused and will soon be removed', DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) super().__init__() self.page = 0 # current page number @@ -698,7 +698,7 @@ def set_doc_option(self, opt, value): "set_doc_option() is deprecated and will be removed in a future release. " "Simply set the `.core_fonts_encoding` property as a replacement.", DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) if opt != "core_fonts_encoding": raise FPDFException(f'Unknown document option "{opt}"') @@ -1196,7 +1196,7 @@ def polyline(self, point_list, fill=False, polygon=False, style=None): warnings.warn( '"fill" parameter is deprecated, use style="F" or style="DF" instead', DeprecationWarning, - stacklevel=5 if polygon else 3, + stacklevel=get_stack_level(), ) if fill and style is None: style = RenderStyle.DF @@ -1253,7 +1253,7 @@ def dashed_line(self, x1, y1, x2, y2, dash_length=1, space_length=1): "dashed_line() is deprecated, and will be removed in a future release. " "Use set_dash_pattern() and the normal drawing operations instead.", DeprecationWarning, - stacklevel=3, + stacklevel=get_stack_level(), ) self.set_dash_pattern(dash_length, space_length) self.line(x1, y1, x2, y2) @@ -1734,7 +1734,7 @@ def add_font(self, family=None, style="", fname=None, uni="DEPRECATED"): warnings.warn( '"uni" parameter is deprecated, unused and will soon be removed', DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) style = "".join(sorted(style.upper())) @@ -1759,7 +1759,10 @@ def add_font(self, family=None, style="", fname=None, uni="DEPRECATED"): fontkey = f"{family.lower()}{style}" # Check if font already added or one of the core fonts if fontkey in self.fonts or fontkey in CORE_FONTS: - warnings.warn(f"Core font or font already added '{fontkey}': doing nothing") + warnings.warn( + f"Core font or font already added '{fontkey}': doing nothing", + stacklevel=get_stack_level(), + ) return self.fonts[fontkey] = TTFFont(self, font_file_path, fontkey, style) @@ -1808,13 +1811,15 @@ def set_font(self, family=None, style="", size=0): if family in self.font_aliases and family + style not in self.fonts: warnings.warn( f"Substituting font {family} by core font " - f"{self.font_aliases[family]}" + f"{self.font_aliases[family]}", + stacklevel=get_stack_level(), ) family = self.font_aliases[family] elif family in ("symbol", "zapfdingbats") and style: warnings.warn( f"Built-in font {family} only has a single 'style' and can't be bold " - f"or italic" + f"or italic", + stacklevel=get_stack_level(), ) style = "" @@ -2352,7 +2357,7 @@ def rotate(self, angle, x=None, y=None): "It will be removed in a future release. " "Use the rotation() context manager instead.", DeprecationWarning, - stacklevel=3, + stacklevel=get_stack_level(), ) if x is None: x = self.x @@ -2658,7 +2663,8 @@ def cell( ) if isinstance(border, int) and border not in (0, 1): warnings.warn( - 'Integer values for "border" parameter other than 1 are currently ignored' + 'Integer values for "border" parameter other than 1 are currently ignored', + stacklevel=get_stack_level(), ) border = 1 new_x = XPos.coerce(new_x) @@ -2690,7 +2696,7 @@ def cell( f" Instead of ln={ln} use new_x=XPos.{new_x.name}, new_y=YPos.{new_y.name}." ), DeprecationWarning, - stacklevel=3, + stacklevel=get_stack_level(), ) # Font styles preloading must be performed before any call to FPDF.get_string_width: txt = self.normalize_text(txt) @@ -2766,7 +2772,8 @@ def _render_styled_text_line( """ if isinstance(border, int) and border not in (0, 1): warnings.warn( - 'Integer values for "border" parameter other than 1 are currently ignored' + 'Integer values for "border" parameter other than 1 are currently ignored', + stacklevel=get_stack_level(), ) border = 1 styled_txt_width = text_line.text_width @@ -3344,7 +3351,7 @@ def multi_cell( 'The parameter "split_only" is deprecated.' ' Use instead dry_run=True and output="LINES".', DeprecationWarning, - stacklevel=3, + stacklevel=get_stack_level(), ) if dry_run or split_only: with self._disable_writing(): @@ -3404,7 +3411,7 @@ def multi_cell( f" Instead of ln={ln} use new_x=XPos.{new_x.name}, new_y=YPos.{new_y.name}." ), DeprecationWarning, - stacklevel=3, + stacklevel=get_stack_level(), ) align = Align.coerce(align) @@ -3709,7 +3716,7 @@ def image( warnings.warn( '"type" parameter is deprecated, unused and will soon be removed', DeprecationWarning, - stacklevel=3, + stacklevel=get_stack_level(), ) if str(name).endswith(".svg"): # Insert it as a PDF path: @@ -3851,7 +3858,8 @@ def _vector_image( svg = SVGObject(img.getvalue()) if not svg.viewbox and svg.width and svg.height: warnings.warn( - ' has no "viewBox", using its "width" & "height" as default "viewBox"' + ' has no "viewBox", using its "width" & "height" as default "viewBox"', + stacklevel=get_stack_level(), ) svg.viewbox = 0, 0, svg.width, svg.height if w == 0 and h == 0: @@ -4345,7 +4353,8 @@ def code39(self, txt, x, y, w=1.5, h=5): warnings.warn( # pylint: disable=implicit-str-concat "Code 39 input must start and end with a '*' character to be valid." - " This method does not insert it automatically." + " This method does not insert it automatically.", + stacklevel=get_stack_level(), ) chars = { "0": "nnnwwnwnn", @@ -4717,7 +4726,7 @@ def output( warnings.warn( '"dest" parameter is deprecated, unused and will soon be removed', DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) # Finish document if necessary: if not self.buffer: diff --git a/fpdf/html.py b/fpdf/html.py index 736705467..68014ec69 100644 --- a/fpdf/html.py +++ b/fpdf/html.py @@ -5,6 +5,7 @@ from .enums import TextEmphasis, XPos, YPos from .errors import FPDFException +from .deprecation import get_stack_level from .fonts import FontFace from .table import Table, TableBordersLayout @@ -726,5 +727,5 @@ def __init__(self, *args, **kwargs): "The HTMLMixin class is deprecated. " "Simply use the FPDF class as a replacement.", DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) diff --git a/fpdf/recorder.py b/fpdf/recorder.py index 573eb75ec..5f233fd34 100644 --- a/fpdf/recorder.py +++ b/fpdf/recorder.py @@ -1,6 +1,7 @@ import types, warnings from copy import deepcopy +from .deprecation import get_stack_level from .errors import FPDFException @@ -48,7 +49,8 @@ def replay(self): result = func(*args, **kwargs) if isinstance(result, types.GeneratorType): warnings.warn( - "Detected usage of a context manager inside an unbreakable() section, which is not supported" + "Detected usage of a context manager inside an unbreakable() section, which is not supported", + stacklevel=get_stack_level(), ) # The results of other methods can also be invalidated: .pages_count, page_no(), get_x() / get_y(), will_page_break() except Exception as error: diff --git a/fpdf/template.py b/fpdf/template.py index 1450259de..a66b162c5 100644 --- a/fpdf/template.py +++ b/fpdf/template.py @@ -6,6 +6,7 @@ import csv, locale, warnings +from .deprecation import get_stack_level from .errors import FPDFException from .fpdf import FPDF @@ -480,7 +481,7 @@ def _code39( warnings.warn( "code39 arguments x/y/w/h are deprecated, please use x1/y1/y2/size instead", DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) pdf = self.pdf if pdf.fill_color.serialize().lower() != _rgb_as_str(foreground): @@ -637,7 +638,7 @@ def __init__( warnings.warn( '"infile" is deprecated, unused and will soon be removed', DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) for arg in ( "format", @@ -684,7 +685,7 @@ def render(self, outfile=None, dest=None): warnings.warn( '"dest" is deprecated, unused and will soon be removed', DeprecationWarning, - stacklevel=2, + stacklevel=get_stack_level(), ) self.pdf.set_font("helvetica", "B", 16) self.pdf.set_auto_page_break(False, margin=0) diff --git a/test/errors/test_FPDF_errors.py b/test/errors/test_FPDF_errors.py index 87f8bf2e7..cb3caac4d 100644 --- a/test/errors/test_FPDF_errors.py +++ b/test/errors/test_FPDF_errors.py @@ -73,7 +73,7 @@ def test_units(): def test_doc_option_only_core_fonts_encoding(): pdf = fpdf.FPDF() - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: pdf.set_doc_option("core_fonts_encoding", 4) assert pdf.core_fonts_encoding == 4 @@ -82,6 +82,7 @@ def test_doc_option_only_core_fonts_encoding(): msg = 'Unknown document option "not core_fonts_encoding"' assert str(e.value) == msg + assert all(r.filename == __file__ for r in record) def test_adding_content_after_closing(): diff --git a/test/fonts/test_add_font.py b/test/fonts/test_add_font.py index 5829d9e81..aabdaa87d 100644 --- a/test/fonts/test_add_font.py +++ b/test/fonts/test_add_font.py @@ -31,28 +31,50 @@ def test_deprecation_warning_for_FPDF_CACHE_DIR(): # pylint: disable=import-outside-toplevel,pointless-statement,reimported from fpdf import fpdf - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: fpdf.FPDF_CACHE_DIR - with pytest.warns(DeprecationWarning): + assert len(record) == 1 + assert record[0].filename == __file__ + + with pytest.warns(DeprecationWarning) as record: fpdf.FPDF_CACHE_DIR = "/tmp" - with pytest.warns(DeprecationWarning): + assert len(record) == 1 + assert record[0].filename == __file__ + + with pytest.warns(DeprecationWarning) as record: fpdf.FPDF_CACHE_MODE - with pytest.warns(DeprecationWarning): + assert len(record) == 1 + assert record[0].filename == __file__ + + with pytest.warns(DeprecationWarning) as record: fpdf.FPDF_CACHE_MODE = 1 + assert len(record) == 1 + assert record[0].filename == __file__ fpdf.SOME = 1 assert fpdf.SOME == 1 import fpdf - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: fpdf.FPDF_CACHE_DIR - with pytest.warns(DeprecationWarning): + assert len(record) == 1 + assert record[0].filename == __file__ + + with pytest.warns(DeprecationWarning) as record: fpdf.FPDF_CACHE_DIR = "/tmp" - with pytest.warns(DeprecationWarning): + assert len(record) == 1 + assert record[0].filename == __file__ + + with pytest.warns(DeprecationWarning) as record: fpdf.FPDF_CACHE_MODE - with pytest.warns(DeprecationWarning): + assert len(record) == 1 + assert record[0].filename == __file__ + + with pytest.warns(DeprecationWarning) as record: fpdf.FPDF_CACHE_MODE = 1 + assert len(record) == 1 + assert record[0].filename == __file__ fpdf.SOME = 1 assert fpdf.SOME == 1 @@ -61,7 +83,7 @@ def test_deprecation_warning_for_FPDF_CACHE_DIR(): def test_add_font_with_str_fname_ok(tmp_path): font_file_path = str(HERE / "Roboto-Regular.ttf") for font_cache_dir in (True, str(tmp_path), None): - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: pdf = FPDF(font_cache_dir=font_cache_dir) pdf.add_font(fname=font_file_path) pdf.set_font("Roboto-Regular", size=64) @@ -69,13 +91,17 @@ def test_add_font_with_str_fname_ok(tmp_path): pdf.cell(txt="Hello World!") assert_pdf_equal(pdf, HERE / "add_font_unicode.pdf", tmp_path) + assert all( + r.filename == __file__ for r in record if r.category == DeprecationWarning + ) + def test_add_core_fonts(): font_file_path = HERE / "Roboto-Regular.ttf" pdf = FPDF() pdf.add_page() - with pytest.warns(UserWarning): # "already added". + with pytest.warns(UserWarning) as record: # "already added". pdf.add_font("Helvetica", fname=font_file_path) pdf.add_font("Helvetica", style="B", fname=font_file_path) pdf.add_font("helvetica", style="IB", fname=font_file_path) @@ -83,6 +109,8 @@ def test_add_core_fonts(): pdf.add_font("courier", fname=font_file_path) assert not pdf.fonts # No fonts added, as all of them are core fonts + assert all(r.filename == __file__ for r in record) + def test_render_en_dash(tmp_path): # issue-166 pdf = FPDF() diff --git a/test/fonts/test_set_font.py b/test/fonts/test_set_font.py index 324206297..b91d0a769 100644 --- a/test/fonts/test_set_font.py +++ b/test/fonts/test_set_font.py @@ -79,9 +79,12 @@ def test_set_font_aliases_as_font(): with pytest.warns( UserWarning, match=f"Substituting font {alias.lower()} by core font {alternative}", - ): + ) as record: pdf.set_font(alias) + assert len(record) == 1 + assert record[0].filename == __file__ + # Test if font family is set correctly assert pdf.font_family == alternative # Test if the fonts were added in this order and without duplicats: @@ -161,11 +164,14 @@ def test_set_font_zapfdingbats_symbol_with_style(): UserWarning, match=f"Built-in font {family} only has a single 'style' and " f"can't be bold or italic", - ): + ) as record: pdf.set_font(family, style=style) # Test if style is set correctly (== no style) assert pdf.font_style == "" + assert len(record) == 1 + assert record[0].filename == __file__ + # Test if underline is set correctly assert pdf.underline == int("U" in style) diff --git a/test/html/test_html.py b/test/html/test_html.py index 040aa83dc..cf2a4a14f 100644 --- a/test/html/test_html.py +++ b/test/html/test_html.py @@ -337,7 +337,7 @@ class PDF(FPDF, HTMLMixin): "Simply use the FPDF class as a replacement." ) - with pytest.warns(DeprecationWarning, match=msg): + with pytest.warns(DeprecationWarning, match=msg) as record: pdf = PDF() pdf.add_page() pdf.write_html( @@ -352,6 +352,9 @@ class PDF(FPDF, HTMLMixin): ) assert_pdf_equal(pdf, HERE / "html_description.pdf", tmp_path) + assert len(record) == 1 + assert record[0].filename == __file__ + def test_html_whitespace_handling(tmp_path): # Issue 547 """Testing whitespace handling for write_html().""" diff --git a/test/shapes/test_polyline.py b/test/shapes/test_polyline.py index cfecc96a3..e5a95b293 100644 --- a/test/shapes/test_polyline.py +++ b/test/shapes/test_polyline.py @@ -28,12 +28,15 @@ def test_polyline_command_all_k(unit, factor): assert "".join(data) == "10.00 831.89 m40.00 831.89 l10.00 801.89 l S" data.clear() - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: # keep one "fill=True" to test the warning pdf.polyline(scale_points(POLYLINE_COORDINATES, factor), fill=True) assert "".join(data) == "10.00 831.89 m40.00 831.89 l10.00 801.89 l B" data.clear() + assert len(record) == 1 + assert record[0].filename == __file__ + pdf.polyline(scale_points(POLYLINE_COORDINATES, factor), polygon=True) assert "".join(data) == "10.00 831.89 m40.00 831.89 l10.00 801.89 l h S" data.clear() diff --git a/test/test_output.py b/test/test_output.py index dc571193b..8a2824620 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -13,8 +13,10 @@ def test_repeated_calls_to_output(tmp_path): def test_deprecation_warning(tmp_path): pdf = fpdf.FPDF() - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: pdf.output(tmp_path / "empty.pdf", "F") + assert len(record) == 1 + assert record[0].filename == __file__ def test_save_to_absolute_path(tmp_path): diff --git a/test/text/test_cell.py b/test/text/test_cell.py index 213779286..27f77b414 100644 --- a/test/text/test_cell.py +++ b/test/text/test_cell.py @@ -60,8 +60,11 @@ def test_cell_ln_1(tmp_path): doc = FPDF() doc.add_page() doc.set_font("helvetica", size=TEXT_SIZE) - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: doc.cell(w=100, h=LINE_HEIGHT, border=1, txt="Lorem ipsum", ln=1) + assert len(record) == 1 + assert record[0].filename == __file__ + doc.cell(w=100, h=LINE_HEIGHT, border=1, txt="Ut nostrud irure") assert_pdf_equal(doc, HERE / "ln_1.pdf", tmp_path) @@ -243,8 +246,11 @@ def test_cell_newpos_badinput(): pdf.add_page() pdf.set_font("Times", size=16) with pytest.raises(ValueError): - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: pdf.cell(w=0, ln=5) + assert len(record) == 1 + assert record[0].filename == __file__ + with pytest.raises(TypeError): pdf.cell(w=0, new_x=5) with pytest.raises(TypeError): diff --git a/test/text/test_multi_cell.py b/test/text/test_multi_cell.py index dd6ebf20b..e308060f2 100644 --- a/test/text/test_multi_cell.py +++ b/test/text/test_multi_cell.py @@ -260,8 +260,10 @@ def test_multi_cell_split_only(): # discussion 314 ] with pytest.warns( DeprecationWarning, match='The parameter "split_only" is deprecated.' - ): + ) as record: assert pdf.multi_cell(w=0, h=LINE_HEIGHT, txt=text, split_only=True) == expected + assert len(record) == 1 + assert record[0].filename == __file__ def test_multi_cell_with_empty_contents(tmp_path): # issue 349 @@ -281,8 +283,11 @@ def test_multicell_newpos_badinput(): pdf.add_page() pdf.set_font("Times", size=16) with pytest.raises(ValueError): - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: pdf.multi_cell(0, ln=5) + assert len(record) == 1 + assert record[0].filename == __file__ + with pytest.raises(TypeError): pdf.multi_cell(0, new_x=5) with pytest.raises(TypeError): diff --git a/test/text/test_render_styled.py b/test/text/test_render_styled.py index faaa8ed6f..9e719909d 100644 --- a/test/text/test_render_styled.py +++ b/test/text/test_render_styled.py @@ -492,7 +492,7 @@ def test_cell_lnpos(tmp_path): align = item[1] if align == "J": continue - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: doc.cell( twidth, txt=s, @@ -500,6 +500,10 @@ def test_cell_lnpos(tmp_path): align=align, ln=ln, ) + + assert len(record) == 1 + assert record[0].filename == __file__ + # mark the new position in the file with crosshairs for verification with doc.rotation(i * -15, doc.x, doc.y): doc.circle(doc.x - 3, doc.y - 3, 6) @@ -532,7 +536,7 @@ def test_multi_cell_ln_newpos(tmp_path): s = item[0] align = item[1] ln = item[2] - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning) as record: doc.multi_cell( twidth, txt=s + " xxxxxxxxxxxxxxxxxxxx", # force auto break @@ -540,6 +544,9 @@ def test_multi_cell_ln_newpos(tmp_path): align=align, ln=ln, ) + assert len(record) == 1 + assert record[0].filename == __file__ + # mark the new position in the file with crosshairs for verification with doc.rotation(i * -15, doc.x, doc.y): doc.circle(doc.x - 3, doc.y - 3, 6) diff --git a/test/text/test_unbreakable.py b/test/text/test_unbreakable.py index 1c21dc5d4..a09d6343a 100644 --- a/test/text/test_unbreakable.py +++ b/test/text/test_unbreakable.py @@ -119,7 +119,7 @@ def test_multi_cell_table_unbreakable_with_split_only(tmp_path): # issue 359 for row in data: max_no_of_lines_in_cell = 1 for cell in row: - with pytest.warns(DeprecationWarning, match=expected_warn): + with pytest.warns(DeprecationWarning, match=expected_warn) as record: result = pdf.multi_cell( cell_width, l_height, @@ -131,6 +131,9 @@ def test_multi_cell_table_unbreakable_with_split_only(tmp_path): # issue 359 max_line_height=l_height, split_only=True, ) + + assert all(r.filename == __file__ for r in record) + no_of_lines_in_cell = len(result) if no_of_lines_in_cell > max_no_of_lines_in_cell: max_no_of_lines_in_cell = no_of_lines_in_cell @@ -167,7 +170,7 @@ def test_multi_cell_table_unbreakable_with_split_only(tmp_path): # issue 359 pdf.ln() - with pytest.warns(DeprecationWarning, match=expected_warn): + with pytest.warns(DeprecationWarning, match=expected_warn) as record: with pdf.unbreakable() as doc: for _ in range(4): for row in data: @@ -218,6 +221,8 @@ def test_multi_cell_table_unbreakable_with_split_only(tmp_path): # issue 359 ) doc.ln(cell_height) + assert all(r.filename == __file__ for r in record) + assert_pdf_equal( pdf, HERE / "multi_cell_table_unbreakable_with_split_only.pdf", tmp_path )