Skip to content

Package fails to load entirely on Python 3.8+ (ST4): from cgi import escape raises ImportError #8

Description

@dpc00

CopyAsHtml.py does:

from cgi import escape

cgi.escape was deprecated in Python 3.2 and removed entirely in Python 3.8. Sublime Text 4's embedded plugin_host runs Python 3.8, so this import fails immediately:

>>> from cgi import escape
ImportError: cannot import name 'escape' from 'cgi' (...\python3.8.zip\cgi.pyc)

Since this is a top-level import, the entire module fails to load and CopyAsHtmlCommand never gets registered as a command -- confirmed live: after installing the package (v2.0.1) fresh via Package Control, sublime_plugin.text_command_classes contains no CopyAsHtml-related entry at all, and invoking copy_as_html via view.run_command is a silent no-op (no exception surfaced to Sublime's dispatcher, no clipboard change) -- the package is completely non-functional out of the box on any current Sublime Text 4 install, with no error shown to the user to explain why the context-menu/command-palette entry does nothing.

This is likely also the underlying cause of #5 ("Not able to install this package on sublime's latest version").

Fix: replace from cgi import escape with from html import escape (the stdlib replacement, available since Python 3.2, with an equivalent signature for this use case -- escape(s, quote=True) HTML-escapes &, <, >, and optionally quotes).

Second, currently-masked bug that will surface once the above is fixed: the file does from sublime import * (not import sublime), so the bare name sublime is never bound. But CopyAsHtmlCommand.run() calls sublime.load_settings('Preferences.sublime-settings') -- this will raise NameError: name 'sublime' is not defined as soon as the cgi.escape ImportError above is fixed and the method actually gets a chance to execute. Confirmed via a minimal repro: exec('from sublime import *') does not bind sublime itself, only bind the individual names Sublime's API module exports (like load_settings, set_clipboard, etc. directly). Fix: either add import sublime alongside the star-import, or drop the sublime. prefix and call load_settings(...) directly (which is already in scope from the star-import).

Happy to send a PR fixing both issues if useful.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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