2021-10-09 13:56:50 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2023-01-01 21:31:29 +01:00
|
|
|
# Copyright (C) 2015-2023
|
2021-10-09 13:56:50 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
"""This module contains helper functions related to inspecting the program stack.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2021-10-09 13:56:50 +02:00
|
|
|
|
|
|
|
Warning:
|
|
|
|
Contents of this module are intended to be used internally by the library and *not* by the
|
|
|
|
user. Changes to this module are not considered breaking changes and may not be documented in
|
|
|
|
the changelog.
|
|
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
from types import FrameType
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
def was_called_by(frame: Optional[FrameType], caller: Path) -> bool:
|
|
|
|
"""Checks if the passed frame was called by the specified file.
|
|
|
|
|
|
|
|
Example:
|
2022-04-24 12:38:09 +02:00
|
|
|
.. code:: pycon
|
2021-10-09 13:56:50 +02:00
|
|
|
|
|
|
|
>>> was_called_by(inspect.currentframe(), Path(__file__))
|
|
|
|
True
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
frame (:obj:`FrameType`): The frame - usually the return value of
|
|
|
|
``inspect.currentframe()``. If :obj:`None` is passed, the return value will be
|
|
|
|
:obj:`False`.
|
|
|
|
caller (:obj:`pathlib.Path`): File that should be the caller.
|
|
|
|
|
|
|
|
Returns:
|
2022-04-24 12:38:09 +02:00
|
|
|
:obj:`bool`: Whether the frame was called by the specified file.
|
2021-10-09 13:56:50 +02:00
|
|
|
"""
|
|
|
|
if frame is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# https://stackoverflow.com/a/57712700/10606962
|
|
|
|
if Path(frame.f_code.co_filename) == caller:
|
|
|
|
return True
|
|
|
|
while frame.f_back:
|
|
|
|
frame = frame.f_back
|
|
|
|
if Path(frame.f_code.co_filename) == caller:
|
|
|
|
return True
|
|
|
|
return False
|