简体   繁体   中英

How to catch Sublime Text exceptions on their Python interpreter?

How to catch Sublime Text exceptions on their Python interpreter?

This was migrated to https://github.com/SublimeTextIssues/Core/issues/1359

I am trying to catch this exception:

    try:

        print( 'RUNNING' )
        sublime.active_window().run_command( "side_bar_update_sync" )
        isNotSyncedSideBarEnabled = False
        print( 'RUNNING THIS' )

    except BaseException:

        isNotSyncedSideBarEnabled = True
        print( 'RUNNING THIS ALSO' )

But when it is ran, it just does not catches it. Does not matter if I try it within TypeError , Exception or BaseException classes. This below is the full exception output.

reloading plugin SublimeDefaultSyntax.default_syntax
READ_PREF_ASYNC!!!!
    updateIsSyncedSideBarEnabled!!!!
RUNNING
Traceback (most recent call last):
  File "D:\User\Dropbox\Applications\SoftwareVersioning\SublimeText\sublime_plugin.py", line 538, in run_
    return self.run()
TypeError: run() missing 1 required positional argument: 'enable'
RUNNING THIS
isNotSyncedSideBarEnabled: False

The problem is, the python cannot catch the exception throwed by run_command( "side_bar_update_sync" ) . The exception catching for errors like trying to call self.view , when there is no self passed by, are working fine. This is the full code:

def plugin_loaded():

    global isNotSyncedSideBarEnabled

    packageControlSettings = sublime.load_settings('Package Control.sublime-settings')
    userSettings           = sublime.load_settings('Preferences.sublime-settings')

    def updateIsSyncedSideBarEnabled():

        global isNotSyncedSideBarEnabled

        print('    updateIsSyncedSideBarEnabled!!!!')
        sublime.active_window().run_command( "reveal_in_side_bar" )

        try:

            print( 'RUNNING' )
            sublime.active_window().run_command( "side_bar_update_sync" )
            isNotSyncedSideBarEnabled = False
            print( 'RUNNING THIS' )

        except BaseException:

            isNotSyncedSideBarEnabled = True
            print( 'RUNNING THIS ALSO' )

        print( 'isNotSyncedSideBarEnabled: ' + str( isNotSyncedSideBarEnabled ) )



    def read_pref_async():

        print('READ_PREF_ASYNC!!!!')
        updateIsSyncedSideBarEnabled()


    def read_pref_package():

        print('READ_PREF_PACKAGE!!!!')
        updateIsSyncedSideBarEnabled()


    def read_pref_preferences():

        print('READ_PREF_PREFERENCES!!!!')
        updateIsSyncedSideBarEnabled()


    # read initial setting, after all packages being loaded
    sublime.set_timeout_async( read_pref_async, 1000 )

    # listen for changes
    packageControlSettings.add_on_change( "Package Control", read_pref_package )
    userSettings.add_on_change( "Preferences", read_pref_preferences )

    #print( userSettings.get( "ignored_packages" ) )
    #print( packageControlSettings.get( "installed_packages" ) )

This discussion may be followed by this Sublime Text Forum's thread: https://forum.sublimetext.com/t/how-to-add-remove-a-default-menu-entry-when-ax-package-is-isnt-enabled-installed/22753?u=addons_zz

This are the lines from the file showed on the exception:

532 class ApplicationCommand(Command):
533     def run_(self, edit_token, args):
534         args = self.filter_args(args)
535         if args:
536             return self.run(**args)
537         else:
538             return self.run()
539
540    def run(self):
541        pass1
542
543 ... other classes

As stated on the question, it is indeed a bug on Sublime Text. Here's a smaller repro from the bug report:

import sublime_plugin

class TestCommandCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        try:
            ret = self.view.window().run_command("toggle_comment", {"comment": "no"})
        except:
            print("an exception")
        else:
            print(ret)

Console:

reloading plugin User.test
command: test_command
Traceback (most recent call last):
  File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 792, in run_
    return self.run(edit, **args)
TypeError: run() got an unexpected keyword argument 'comment'
None

Needless to say, this only happens for Python commands and not internal commands since those do not error at all. Also when do you try to run a command as sublime.active_window().run_command( "reveal_in_side_bar" ) and it does not exist, Sublime Text does not throw an exception. This is to allow plugins or menu entries do not throw errors when they are not available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM