Skip to content

[BUG] telegram.ext._utils.stack.was_called_by gives incorrect result on 64-bit machine #3536

@hyfc

Description

@hyfc

Steps to Reproduce

  1. Set up virtualenv using python3 -m venv on a 64-bit machine.
  2. Initialize an application object using telegram.ext.ApplicationBuilder:
from telegram.ext import ApplicationBuilder
def main() -> None:
    application = ApplicationBuilder().token("TOKEN").build()

if __name__ == "__main__":
    main()
  1. Run the bot in virtualenv and it will give a warning messgae like PTBUserWarning: `Application` instances should be built via the `ApplicationBuilder`.

Expected behaviour

The warning message shouldn't be given since ApplicationBuilder is being used.

Actual behaviour

$ python test.py 
../venv/lib64/python3.11/site-packages/telegram/ext/_applicationbuilder.py:292: 
PTBUserWarning: `Application` instances should be built via the `ApplicationBuilder`.
  ] = DefaultValue.get_value(  # pylint: disable=not-callable

Operating System

Fedora Linux 37 (Server Edition)

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 20.0
Bot API 6.4
Python 3.11.1 (main, Dec  7 2022, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)]

Relevant log output

No response

Additional Context

I believe this is caused by comparing a resolved path with an unresolved path here.

In my case, it finds ../venv/lib/python3.11/site-packages/telegram/ext/_applicationbuilder.py not equal to ../venv/lib64/python3.11/site-packages/telegram/ext/_applicationbuilder.py, the directory lib64 being a symlink to lib.

A quick (maybe not final) fix is to modify stack.py so that was_called_by always resolves paths from frame:

    while frame.f_back:
        frame = frame.f_back
        if Path(frame.f_code.co_filename).resolve() == caller:
            return True

I have tested it and the warning no longer appears.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions