From 1a343ea698c156b99b777a35b226cca3a15c9463 Mon Sep 17 00:00:00 2001 From: sunhailin Date: Fri, 17 May 2024 09:26:22 +0800 Subject: [PATCH] fix error and new version for 1.4.1 --- CHANGELOG.md | 3 +++ fastapi_profiler/_version.py | 2 +- fastapi_profiler/profiler.py | 28 +++++++++++++++------------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b33aa09..8b54c63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # ChangeLog +### version 1.4.1 + * fix `AttributeError`[issue#17](https://github.com/sunhailin-Leo/fastapi_profiler/issues/17) + ### version 1.4.0 * add support for speedscope diff --git a/fastapi_profiler/_version.py b/fastapi_profiler/_version.py index 9058ae0..6e1032c 100644 --- a/fastapi_profiler/_version.py +++ b/fastapi_profiler/_version.py @@ -1,2 +1,2 @@ -__version__ = "1.4.0" +__version__ = "1.4.1" __author__ = "sunhailin-Leo" diff --git a/fastapi_profiler/profiler.py b/fastapi_profiler/profiler.py index c2bf19a..74dd13c 100644 --- a/fastapi_profiler/profiler.py +++ b/fastapi_profiler/profiler.py @@ -128,11 +128,11 @@ async def get_profiler_result(self): output_filename=os.path.abspath(html_file_name), ) else: - html_code = renderer.render(session=self._profiler.last_session) - with codecs.open(html_file_name, "w", "utf-8") as f: - f.write(html_code) - - logger.info("Done writing profile to %r", html_file_name) + if self._profiler.last_session: + html_code = renderer.render(session=self._profiler.last_session) + with codecs.open(html_file_name, "w", "utf-8") as f: + f.write(html_code) + logger.info("Done writing profile to %r", html_file_name) elif self._output_type == "prof": prof_file_name = self.DEFAULT_PROF_FILENAME if self._prof_file_name is not None: @@ -155,10 +155,11 @@ async def get_profiler_result(self): prof_file_name, ) - renderer = JSONRenderer() - with codecs.open(prof_file_name, "w", "utf-8") as f: - f.write(renderer.render(session=self._profiler.last_session)) - logger.info("Done writing profile to %r", prof_file_name) + if self._profiler.last_session: + renderer = JSONRenderer() + with codecs.open(prof_file_name, "w", "utf-8") as f: + f.write(renderer.render(session=self._profiler.last_session)) + logger.info("Done writing profile to %r", prof_file_name) elif self._output_type == "speedscope": prof_file_name = self.DEFAULT_SPEEDSCOPE_FILENAME if self._prof_file_name is not None: @@ -169,7 +170,8 @@ async def get_profiler_result(self): prof_file_name, ) - renderer = SpeedscopeRenderer() - with codecs.open(prof_file_name, "w", "utf-8") as f: - f.write(renderer.render(session=self._profiler.last_session)) - logger.info("Done writing profile to %r", prof_file_name) + if self._profiler.last_session: + renderer = SpeedscopeRenderer() + with codecs.open(prof_file_name, "w", "utf-8") as f: + f.write(renderer.render(session=self._profiler.last_session)) + logger.info("Done writing profile to %r", prof_file_name)