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

firefox_profile.py: use with statement in zipfile as Python 2.x support is dropped #14489

Merged
merged 5 commits into from
Sep 12, 2024

Conversation

Delta456
Copy link
Contributor

@Delta456 Delta456 commented Sep 12, 2024

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Use with statement in zipFile as TODO says and support for Python 2.x is now dropped

Motivation and Context

Solves the TODO Comment

 # Bug 944361 - We cannot use 'with' together with zipFile because
 # it will cause an exception thrown in Python 2.6.
 # TODO: use with statement when Python 2.x is no longer supported

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

enhancement


Description

  • Updated firefox_profile.py to use the with statement for handling zipfile.ZipFile, improving code readability and resource management.
  • Removed outdated comments and code related to Python 2.x compatibility, as support for Python 2.x has been dropped.

Changes walkthrough 📝

Relevant files
Enhancement
firefox_profile.py
Use `with` statement for zipfile handling in Firefox profile

py/selenium/webdriver/firefox/firefox_profile.py

  • Replaced manual try-finally block with with statement for
    zipfile.ZipFile.
  • Removed outdated comments related to Python 2.6 compatibility.
  • +1/-7     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Error Handling
    The new implementation using 'with' statement doesn't explicitly handle potential exceptions from ZipFile operations.

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Adjust the try-except block placement to focus on file operations

    Consider moving the try block to encompass only the file operations, as the OSError
    check is now redundant with the with statement's built-in exception handling.

    py/selenium/webdriver/firefox/firefox_profile.py [277-283]

    -try:
    -    if zipfile.is_zipfile(addon_path):
    -        with zipfile.ZipFile(addon_path, "r") as compressed_file:
    -            if "manifest.json" in compressed_file.namelist():
    -                return parse_manifest_json(compressed_file.read("manifest.json"))
    +if zipfile.is_zipfile(addon_path):
    +    with zipfile.ZipFile(addon_path, "r") as compressed_file:
    +        if "manifest.json" in compressed_file.namelist():
    +            return parse_manifest_json(compressed_file.read("manifest.json"))
     
    -            manifest = compressed_file.read("install.rdf")
    +        manifest = compressed_file.read("install.rdf")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion correctly identifies that the try block is unnecessary around the with statement, as the with statement already handles exceptions related to file operations. This improves code clarity and maintainability.

    8
    Best practice
    Use a context manager for file operations consistently throughout the function

    Consider using a context manager for opening the manifest.json file in the directory
    case, similar to how it's done for the zip file.

    py/selenium/webdriver/firefox/firefox_profile.py [284-287]

     elif os.path.isdir(addon_path):
         manifest_json_filename = os.path.join(addon_path, "manifest.json")
         if os.path.exists(manifest_json_filename):
             with open(manifest_json_filename, encoding="utf-8") as f:
    +            return parse_manifest_json(f.read())
     
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: The suggestion to use a context manager for reading the manifest.json file is already implemented in the PR code. The suggestion does not add new value or improvement, as the context manager is already used.

    5

    @Delta456 Delta456 changed the title firefox_profile.py: use with statement in zipfile as Python 2.x support firefox_profile.py: use with statement in zipfile as Python 2.x support is dropped Sep 12, 2024
    Copy link
    Member

    @harsha509 harsha509 left a comment

    Choose a reason for hiding this comment

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

    LGTM!

    @AutomatedTester AutomatedTester merged commit 71a0629 into SeleniumHQ:trunk Sep 12, 2024
    13 checks passed
    @Delta456 Delta456 deleted the py_with branch September 12, 2024 17:24
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants