ExtractSublimePackage icon indicating copy to clipboard operation
ExtractSublimePackage copied to clipboard

Invalid .sublime-package still raises UnboundLocalError: the except branch has no return

Open dpc00 opened this issue 4 weeks ago • 0 comments

Extracting an invalid .sublime-package still crashes with UnboundLocalError, although #5 ("An invalid .sublime-package leads to UnboundLocalError") is closed.

Environment: Sublime Text 4 build 4215 (plugin host Python 3.14.6), Windows 10.0.26300, Extract Sublime Package 2026.09.25.15.13.54 from Package Control.

Steps

  1. Make a file that is not a zip: echo not a zip > AuditBadPkg.sublime-package.
  2. Open it in Sublime (its name ends in .sublime-package, so the command is enabled).
  3. Run Extract Sublime Package: Extract open Package File.

Result

ExtractSublimePackage.py, line 87, in run
    extract_package(view.file_name())
ExtractSublimePackage.py, line 62, in extract_package
    with contextlib.closing(arc):
UnboundLocalError: cannot access local variable 'arc' where it is not associated with a value

The user gets a traceback in the console instead of the intended "package file … is invalid" message, and the command reports nothing else.

Cause — the except branch has no return:

    try:
        arc = zipfile.ZipFile(filename)
    except zipfile.BadZipfile:
        # can't open the file
        _msg("package file %s is invalid" % base_name)
    with contextlib.closing(arc):     # <-- reached with arc unbound

The except does match (the BadZipfile alias still exists in zipfile), prints the message, and then falls through. master has the same code, so this is not a released-versus-master gap.

Fix: return after _msg(...) in the except branch (or move the rest under an else:). Suggested:

    try:
        arc = zipfile.ZipFile(filename)
    except zipfile.BadZipfile:
        _msg("package file %s is invalid" % base_name)
        return

What does work, so the scope is only the error path: a valid archive extracts correctly — a .sublime-package containing readme.txt and sub/inner.txt produced Packages/AuditTestPkg/readme.txt and Packages/AuditTestPkg/sub/inner.txt with content intact, and no partial directory is left behind in the failure case.

dpc00 avatar Nov 07 '26 14:11 dpc00