From 60ca41a2ef41122dec91ff6f028bfe19d3acb5ef Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Thu, 1 Jun 2017 17:19:17 +1000 Subject: [PATCH 1/5] Run pudb as a subprocess This allows breakpoint to be set correctly when the python version that vim is compiled into is not the same as the python version that pudb would run as (e.g. when using virtualenv). --- plugin/load_breakpoints.py | 28 ++++++++++++++++++++++++++ plugin/pudb.vim | 38 ++++++++++------------------------- plugin/set_breakpoint.py | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 plugin/load_breakpoints.py create mode 100644 plugin/set_breakpoint.py diff --git a/plugin/load_breakpoints.py b/plugin/load_breakpoints.py new file mode 100644 index 0000000..2d8f40e --- /dev/null +++ b/plugin/load_breakpoints.py @@ -0,0 +1,28 @@ +""" +Usage: load_breakpoints.py + +Print breakpoints in + +Example usage: + + $ ./load_breakpoints.py foo.py + 3 + 50 + 57 + +""" + +import sys + +from pudb.settings import load_breakpoints +from pudb import NUM_VERSION + +args = () if NUM_VERSION >= (2013, 1) else (None,) +bps = load_breakpoints(*args) + +filename = sys.argv[1] + +bps = [bp[1] for bp in bps if bp[0] == filename] + +for bp in bps: + print(bp) diff --git a/plugin/pudb.vim b/plugin/pudb.vim index 4117e90..22e5599 100644 --- a/plugin/pudb.vim +++ b/plugin/pudb.vim @@ -19,6 +19,8 @@ sign define PudbBreakPoint text=Ø) texthl=error let s:first_sign_id = 10000 let s:next_sign_id = s:first_sign_id +let s:plugin_dir = expand(":p:h") + augroup pudb autocmd BufReadPost *.py call s:UpdateBreakPoints() augroup end @@ -40,20 +42,19 @@ let b:pudb_sign_ids = [] python << EOF import vim -from pudb.settings import load_breakpoints -from pudb import NUM_VERSION +import os +import subprocess filename = vim.eval('expand("%:p")') -args = () if NUM_VERSION >= (2013, 1) else (None,) -bps = load_breakpoints(*args) +scriptname = os.path.join(vim.eval('s:plugin_dir'), 'load_breakpoints.py') +bps = subprocess.Popen(['python', scriptname, filename], stdout=subprocess.PIPE) -for bp in bps: - if bp[0] != filename: - continue +for bp in bps.stdout: + bp = int(bp.strip()) sign_id = vim.eval("s:next_sign_id") - vim.command("sign place %s line=%s name=PudbBreakPoint file=%s" % (sign_id, bp[1], filename)) + vim.command("sign place %s line=%s name=PudbBreakPoint file=%s" % (sign_id, bp, filename)) vim.eval("add(b:pudb_sign_ids, s:next_sign_id)") vim.command("let s:next_sign_id += 1") EOF @@ -63,29 +64,12 @@ endfunction function! s:ToggleBreakPoint() python << EOF import vim -from pudb.settings import load_breakpoints, save_breakpoints -from pudb import NUM_VERSION - -args = () if NUM_VERSION >= (2013, 1) else (None,) -bps = [bp[:2] for bp in load_breakpoints(*args)] filename = vim.eval('expand("%:p")') row, col = vim.current.window.cursor -bp = (filename, row) -if bp in bps: - bps.pop(bps.index(bp)) -else: - bps.append(bp) - -class BP(object): - def __init__(self, fn, ln): - self.file = fn - self.line = ln - -bp_list = [BP(bp[0], bp[1]) for bp in bps] - -save_breakpoints(bp_list) +scriptname = os.path.join(vim.eval('s:plugin_dir'), 'save_breakpoints.py') +proc = subprocess.Popen(['python', scriptname, filename, str(row)]) vim.command('call s:UpdateBreakPoints()') EOF diff --git a/plugin/set_breakpoint.py b/plugin/set_breakpoint.py new file mode 100644 index 0000000..230f0e4 --- /dev/null +++ b/plugin/set_breakpoint.py @@ -0,0 +1,41 @@ +""" +Usage: set_breakpoint.py + +Set a breakpoint in at + +Example usage: + + $ ./load_breakpoints.py foo.py + 12 + $ ./set_breakpoint.py foo.py 11 + $ ./load_breakpoints.py foo.py + 12 + 11 +""" + +import sys + +from pudb.settings import load_breakpoints, save_breakpoints +from pudb import NUM_VERSION + +args = () if NUM_VERSION >= (2013, 1) else (None,) +bps = [bp[:2] for bp in load_breakpoints(*args)] + +filename = sys.argv[1] +row = int(sys.argv[2]) + +bp = (filename, row) +if bp in bps: + bps.pop(bps.index(bp)) +else: + bps.append(bp) + +class BP(object): + def __init__(self, fn, ln): + self.file = fn + self.line = ln + self.cond = None + +bp_list = [BP(bp[0], bp[1]) for bp in bps] + +save_breakpoints(bp_list) From 02c537c379d817f9d80b2ee21c53c43c61bc30d0 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Thu, 1 Jun 2017 17:32:57 +1000 Subject: [PATCH 2/5] Support vim with +python3 Drops support for vim with just +python --- plugin/pudb.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin/pudb.vim b/plugin/pudb.vim index 22e5599..921620e 100644 --- a/plugin/pudb.vim +++ b/plugin/pudb.vim @@ -9,8 +9,8 @@ if exists('g:loaded_pudb_plugin') || &cp endif let g:loaded_pudb_plugin = 1 -if !has("python") - echo "Error: Required vim compiled with +python" +if !has("python3") + echo "Error: Required vim compiled with +python3" finish endif @@ -40,7 +40,7 @@ endfor let b:pudb_sign_ids = [] -python << EOF +python3 << EOF import vim import os import subprocess @@ -62,13 +62,13 @@ EOF endfunction function! s:ToggleBreakPoint() -python << EOF +python3 << EOF import vim filename = vim.eval('expand("%:p")') row, col = vim.current.window.cursor -scriptname = os.path.join(vim.eval('s:plugin_dir'), 'save_breakpoints.py') +scriptname = os.path.join(vim.eval('s:plugin_dir'), 'set_breakpoint.py') proc = subprocess.Popen(['python', scriptname, filename, str(row)]) vim.command('call s:UpdateBreakPoints()') From e168aa3e0154584c67e76c9b21efe8962f686d12 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Thu, 7 Sep 2017 18:59:23 +1000 Subject: [PATCH 3/5] Fix breakpoints not displaying correctly after toggle There was a race condition where set_breakpoint.py subprocess has not finished saving breakpoint when UpdateBreakPoints() is called. --- plugin/pudb.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin/pudb.vim b/plugin/pudb.vim index 921620e..9589d79 100644 --- a/plugin/pudb.vim +++ b/plugin/pudb.vim @@ -70,6 +70,7 @@ row, col = vim.current.window.cursor scriptname = os.path.join(vim.eval('s:plugin_dir'), 'set_breakpoint.py') proc = subprocess.Popen(['python', scriptname, filename, str(row)]) +proc.wait() vim.command('call s:UpdateBreakPoints()') EOF From dfd4cc3824ae4f17999afa394aec9de07aa24775 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Tue, 13 Feb 2018 09:51:16 +1100 Subject: [PATCH 4/5] Catch error when pudb is not installed --- plugin/load_breakpoints.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugin/load_breakpoints.py b/plugin/load_breakpoints.py index 2d8f40e..472dbd9 100644 --- a/plugin/load_breakpoints.py +++ b/plugin/load_breakpoints.py @@ -14,8 +14,11 @@ import sys -from pudb.settings import load_breakpoints -from pudb import NUM_VERSION +try: + from pudb.settings import load_breakpoints + from pudb import NUM_VERSION +except ImportError: + exit(-1) args = () if NUM_VERSION >= (2013, 1) else (None,) bps = load_breakpoints(*args) From 16fa348db9ce2a9ff04336ce47f37a278fad8080 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 25 Oct 2021 01:16:25 +1100 Subject: [PATCH 5/5] Pick `python3` if `python` is not available In some systems, only `python` is installed. --- plugin/pudb.vim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugin/pudb.vim b/plugin/pudb.vim index 9589d79..0e95e62 100644 --- a/plugin/pudb.vim +++ b/plugin/pudb.vim @@ -44,11 +44,13 @@ python3 << EOF import vim import os import subprocess +import shutil filename = vim.eval('expand("%:p")') scriptname = os.path.join(vim.eval('s:plugin_dir'), 'load_breakpoints.py') -bps = subprocess.Popen(['python', scriptname, filename], stdout=subprocess.PIPE) +python_path = shutil.which('python') or shutil.which('python3') +bps = subprocess.Popen([python_path, scriptname, filename], stdout=subprocess.PIPE) for bp in bps.stdout: bp = int(bp.strip()) @@ -69,7 +71,8 @@ filename = vim.eval('expand("%:p")') row, col = vim.current.window.cursor scriptname = os.path.join(vim.eval('s:plugin_dir'), 'set_breakpoint.py') -proc = subprocess.Popen(['python', scriptname, filename, str(row)]) +python_path = shutil.which('python') or shutil.which('python3') +proc = subprocess.Popen([python_path, scriptname, filename, str(row)]) proc.wait() vim.command('call s:UpdateBreakPoints()')