From aa1f4f1c5836ddda802e687107ea72ba00de0ec0 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 17 Nov 2022 19:18:53 +0100 Subject: [PATCH 1/5] STYLE enable pylint: method-cache-max-size-none --- pandas/io/formats/excel.py | 2 +- pyproject.toml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index a26b85390fd49..4040988be5753 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -173,7 +173,7 @@ def __init__(self, inherited: str | None = None) -> None: compute_css = CSSResolver() - @lru_cache(maxsize=None) + @lru_cache(maxsize=None) # pylint: disable=method-cache-max-size-none def __call__( self, declarations: str | frozenset[tuple[str, str]] ) -> dict[str, dict[str, str]]: diff --git a/pyproject.toml b/pyproject.toml index 8b7204af7c089..d845e8f90e842 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -135,7 +135,6 @@ disable = [ "invalid-envvar-default", "invalid-overridden-method", "keyword-arg-before-vararg", - "method-cache-max-size-none", "non-parent-init-called", "overridden-final-method", "pointless-statement", From d9df02427dd97005479088e7bce25698b9c0c94a Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Fri, 18 Nov 2022 19:54:24 +0100 Subject: [PATCH 2/5] STYLE enable pylint: method-cache-max-size-none II --- pandas/io/formats/excel.py | 10 +++++++++- pandas/tests/io/formats/test_to_excel.py | 10 +++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 4040988be5753..0a262bd43463a 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -170,10 +170,13 @@ def __init__(self, inherited: str | None = None) -> None: self.inherited = self.compute_css(inherited) else: self.inherited = None + # We should avoid lru_cache on the __call__ method. + # Otherwise once the method __call__ has been called + # garbage collection no longer deletes converter. + self._call_cached = lru_cache(maxsize=None)(self._call_uncached) compute_css = CSSResolver() - @lru_cache(maxsize=None) # pylint: disable=method-cache-max-size-none def __call__( self, declarations: str | frozenset[tuple[str, str]] ) -> dict[str, dict[str, str]]: @@ -193,6 +196,11 @@ def __call__( A style as interpreted by ExcelWriter when found in ExcelCell.style. """ + return self._call_cached(declarations) + + def _call_uncached( + self, declarations: str | frozenset[tuple[str, str]] + ) -> dict[str, dict[str, str]]: properties = self.compute_css(declarations, self.inherited) return self.build_xlstyle(properties) diff --git a/pandas/tests/io/formats/test_to_excel.py b/pandas/tests/io/formats/test_to_excel.py index 7481baaee94f6..2a0f9f59972ef 100644 --- a/pandas/tests/io/formats/test_to_excel.py +++ b/pandas/tests/io/formats/test_to_excel.py @@ -357,7 +357,7 @@ def test_css_excel_cell_precedence(styles, expected): """It applies favors latter declarations over former declarations""" # See GH 47371 converter = CSSToExcelConverter() - converter.__call__.cache_clear() + converter._call_cached.cache_clear() css_styles = {(0, 0): styles} cell = CssExcelCell( row=0, @@ -369,7 +369,7 @@ def test_css_excel_cell_precedence(styles, expected): css_col=0, css_converter=converter, ) - converter.__call__.cache_clear() + converter._call_cached.cache_clear() assert cell.style == converter(expected) @@ -410,7 +410,7 @@ def test_css_excel_cell_cache(styles, cache_hits, cache_misses): """It caches unique cell styles""" # See GH 47371 converter = CSSToExcelConverter() - converter.__call__.cache_clear() + converter._call_cached.cache_clear() css_styles = {(0, i): _style for i, _style in enumerate(styles)} for css_row, css_col in css_styles: @@ -424,8 +424,8 @@ def test_css_excel_cell_cache(styles, cache_hits, cache_misses): css_col=css_col, css_converter=converter, ) - cache_info = converter.__call__.cache_info() - converter.__call__.cache_clear() + cache_info = converter._call_cached.cache_info() + converter._call_cached.cache_clear() assert cache_info.hits == cache_hits assert cache_info.misses == cache_misses From db12dc1ef215afb473d41a5e5e45dc2ca2e349d9 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva <91160475+natmokval@users.noreply.github.com> Date: Fri, 18 Nov 2022 20:53:48 +0100 Subject: [PATCH 3/5] Minor comment update Co-authored-by: Marco Edward Gorelli <33491632+MarcoGorelli@users.noreply.github.com> --- pandas/io/formats/excel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 0a262bd43463a..1c2aa8f4262f6 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -172,7 +172,7 @@ def __init__(self, inherited: str | None = None) -> None: self.inherited = None # We should avoid lru_cache on the __call__ method. # Otherwise once the method __call__ has been called - # garbage collection no longer deletes converter. + # garbage collection no longer deletes the instance. self._call_cached = lru_cache(maxsize=None)(self._call_uncached) compute_css = CSSResolver() From 87a2f18f4641e13875a2e0190ee6d26ab19da44d Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Fri, 18 Nov 2022 21:12:53 +0100 Subject: [PATCH 4/5] add a note to the v1.5.2 whatsnew --- doc/source/whatsnew/v1.5.2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index dd909415d9e85..7fcac66d1379d 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -27,7 +27,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`) -- +- Memory leak has been fixed in class CSSToExcelConverter (:issue:`48855`) .. --------------------------------------------------------------------------- .. _whatsnew_152.other: From c32e7c8f7dc94cc25434c4332cbf70fd097dddd2 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva <91160475+natmokval@users.noreply.github.com> Date: Fri, 18 Nov 2022 21:28:15 +0100 Subject: [PATCH 5/5] text improvement Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v1.5.2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index 7fcac66d1379d..4654b7630c882 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -27,7 +27,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`) -- Memory leak has been fixed in class CSSToExcelConverter (:issue:`48855`) +- Fixed memory leak in :meth:`.Styler.to_excel` (:issue:`49751`) .. --------------------------------------------------------------------------- .. _whatsnew_152.other: