Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of Scope Variable in Exception Block in constants.py #2283

Closed
wants to merge 9 commits into from

Conversation

Preetam-Das26
Copy link
Collaborator

Purpose of PR?:

Fixes #2274

Does this PR introduce a breaking change?

If the changes in this PR are manually verified, list down the scenarios covered::

Additional information for reviewer? :
Mention if this PR is part of any design or a continuation of previous PRs

Does this PR results in some Documentation changes?
If yes, include the list of Documentation changes

Checklist:

  • Bug fix. Fixes #
  • New feature (Non-API breaking changes that adds functionality)
  • PR Title follows the convention of <type>: <subject>
  • Commit has unit tests

@@ -34,11 +34,12 @@
# ToDo refactor to generic functions, keep only constants
HOME = os.path.expanduser(f"~{os.path.sep}")
MSUI_CONFIG_PATH = os.getenv("MSUI_CONFIG_PATH", os.path.join(HOME, ".config", "msui"))
_fs = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't solve the issue. Now it is possible that .makedirs is called on None.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this _fs = None line, it should not be necessary.

if '://' in MSUI_CONFIG_PATH:
try:
_fs = fs.open_fs(MSUI_CONFIG_PATH)
_fs = fs.open_fs(fs.path.dirname(MSUI_CONFIG_PATH))
Copy link
Member

@ReimarBauer ReimarBauer Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look at the idea in the issue. There should be only one attempt for the fs.errors.CreateFailed because it could be a remote system,

and it would be good to have a test for describing the problem and evaluating the fix by that

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@Preetam-Das26
Copy link
Collaborator Author

Kindly checkout the changes @ReimarBauer @matrss

_fs = fs.open_fs(MSUI_CONFIG_PATH)
except fs.errors.CreateFailed:
_fs.makedirs(MSUI_CONFIG_PATH)
_fs = fs.open_fs(fs.path.dirname(MSUI_CONFIG_PATH))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced that fs.path.dirname is correct there. For the default value of ~/.config/msui defined above this would open ~/.config, which seems wrong to me.

@matrss
Copy link
Collaborator

matrss commented Mar 25, 2024

The code seems to get more complicated than it needs to be (but I am not completely sure). Therefore, I would like to take a step back and ask this: what is this code intended to do? What is the intended outcome of the code that was found to be buggy in #2274? Because I am not sure on that yet, and I can't properly review a PR without having that knowledge... This is probably a question for @ReimarBauer.

@ReimarBauer
Copy link
Member

I provide tom. a testcase. This looks now a bit too much for a constant

@ReimarBauer
Copy link
Member

This shows an example to illustrate the problem"s". There can be a lot cases why a remote address can fail. I prefer to crash here, without catching all unknown possibilities dependent on the potential used module. We should just not try to create a remote folder.

import fs
import os
import logging

# ToDo refactor to generic functions, keep only constants
HOME = os.path.expanduser(f"~{os.path.sep}")
MSUI_CONFIG_PATH = "ftp://ftp.de.debian.org/debian/.config/msui"

if '://' in MSUI_CONFIG_PATH:
    try:
        _fs = fs.open_fs(MSUI_CONFIG_PATH)
    except fs.opener.errors.UnsupportedProtocol:
        logging.error('FS url "%s" not supported', MSUI_CONFIG_PATH)
    except Exception as ex:
        logging.error('FS url "%s" can''t be used, check your remote ressource. '
                  'Reasons can be: path does not exists, %s', MSUI_CONFIG_PATH, ex)
        raise fs.errors.ResourceError(MSUI_CONFIG_PATH, exc=None, msg="FS url can't be used")
else:
    _dir = os.path.expanduser(MSUI_CONFIG_PATH)
    if not os.path.exists(_dir):
        os.makedirs(_dir)

The error looks now like

ERROR:root:FS url "ftp://ftp.de.debian.org/debian/.config/msui" cant be used, check your remote ressource. Reasons can be: path does not exists, unable to create filesystem, resource 'debian/.config/msui' not found
Traceback (most recent call last):
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/ftpfs.py", line 77, in ftp_errors
    yield
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/ftpfs.py", line 674, in getinfo
    response = self.ftp.sendcmd(
               ^^^^^^^^^^^^^^^^^
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/ftplib.py", line 281, in sendcmd
    return self.getresp()
           ^^^^^^^^^^^^^^
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/ftplib.py", line 254, in getresp
    raise error_perm(resp)
ftplib.error_perm: 550 '/debian/.config/msui' cannot be listed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/errors.py", line 125, in new_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/opener/ftpfs.py", line 55, in open_fs
    return ftp_fs.opendir(dir_path, factory=ClosingSubFS)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/base.py", line 1269, in opendir
    if not self.getinfo(path).is_dir:
           ^^^^^^^^^^^^^^^^^^
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/ftpfs.py", line 673, in getinfo
    with ftp_errors(self, path=path):
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/ftpfs.py", line 96, in ftp_errors
    raise errors.ResourceNotFound(path=cast(str, path))
fs.errors.ResourceNotFound: resource 'debian/.config/msui' not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/PycharmProjects/MSS/mslib/msui/constants.py", line 39, in <module>
    _fs = fs.open_fs(MSUI_CONFIG_PATH)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/opener/registry.py", line 220, in open_fs
    _fs, _path = self.open(
                 ^^^^^^^^^^
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/opener/registry.py", line 177, in open
    open_fs = opener.open_fs(fs_url, parse_result, writeable, create, cwd)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/Miniforge/envs/mssdev/lib/python3.11/site-packages/fs/errors.py", line 129, in new_func
    raise cls(exc=e)
fs.errors.CreateFailed: unable to create filesystem, resource 'debian/.config/msui' not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/PycharmProjects/MSS/mslib/msui/msui.py", line 48, in <module>
    from mslib.utils.qt import ui_mainwindow as ui
  File "/home/user/PycharmProjects/MSS/mslib/utils/qt.py", line 40, in <module>
    from mslib.utils.config import config_loader
  File "/home/user/PycharmProjects/MSS/mslib/utils/config.py", line 40, in <module>
    from mslib.msui import constants
  File "/home/user/PycharmProjects/MSS/mslib/msui/constants.py", line 46, in <module>
    raise fs.errors.ResourceError(MSUI_CONFIG_PATH, exc=None, msg="FS url can't be used")
fs.errors.ResourceError: FS url can't be used

@ReimarBauer
Copy link
Member

there is a different solution #2343

@ReimarBauer ReimarBauer closed this May 8, 2024
@Preetam-Das26 Preetam-Das26 deleted the exception branch May 13, 2024 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants