How can I modify locals in Python 2.6's pdb? - python-2.6

How can I modify local variables in pdb or ipdb? These debuggers effectively ignore changes to existing local bindings, e.g. here's an example using ipython.
In [3]: def foo():
...: bar = 123
...: import ipdb; ipdb.set_trace()
...: print bar
...:
In [4]: foo()
> <ipython-input-3-ee9873383395>(4)foo()
3 import ipdb; ipdb.set_trace()
----> 4 print bar
5
ipdb> del bar # effect is limited too the current line
ipdb> bar # bar is still bound to the old value
123
ipdb> del bar; print bar # shows it worked within the line
<function bar at 0x2b7e488>
ipdb> print bar # but the effect goes away on the next debugger line
123
ipdb> c # confirmation that the binding wasn't changed
123
Note that directly modifying locals() has no effect either.
In case it matters, I'm using Python 2.6.
Background: I'm trying to debug some destructor issues and want to more explicitly control when each local is deleted to track down my bug.
EDIT: gave a complete standalone example.
EDIT 2: This is at least partially fixed in Python 2.7. Any solutions for Python 2.6 would still be appreciated.

Related

How to get progress bar with tqdm in a for loop over directory

I am trying to conditionally load some files from a directory. I would like to have a progress bar from tqdm on the process. I currently running this:
loaddir = r'D:\Folder'
# loop the files in the directory
print('Data load initiated')
for subdir, dirs, files in os.walk(loaddir_res):
for name in tqdm(files):
if name.startswith('Test'):
#do things
which gives
Data load initiated
0%| | 0/6723 [00:00<?, ?it/s]
0%| | 26/6723 [00:00<00:28, 238.51it/s]
1%| | 47/6723 [00:00<00:31, 213.62it/s]
1%| | 72/6723 [00:00<00:30, 220.84it/s]
1%|▏ | 91/6723 [00:00<00:31, 213.59it/s]
2%|▏ | 115/6723 [00:00<00:30, 213.73it/s]
This has two problems:
When progress is updated a new line appears in my IPython console in Spyder
I am actually timing the loop over the files and not over the files that start with 'Test' and therefore progress and remaining time are not accurate.
However, if I try this:
loaddir = r'D:\Folder'
# loop the files in the directory
print('Data load initiated')
for subdir, dirs, files in os.walk(loaddir_res):
for name in files:
if tqdm(name.startswith('Test')):
#do things
I get the following error.
Traceback (most recent call last):
File "<ipython-input-80-b801165d4cdb>", line 21, in <module>
if tqdm(name.startswith('Probe')):
TypeError: 'NoneType' object cannot be interpreted as an integer
I would like to have a progress bar in only one line that updates whenever the startswith loop is activated.
----UPDATE----
I also found out here that it can also be used like this:
files = [f for f in tqdm(files) if f.startswith('Test')]
Which allows to track progress with list comprehension by wrapping the iterable with tqdm. However in spyder this results in a separate line for each progress update.
----UPDATE2----
It actually works fine in spyder. Sometimes if the loop fails, it might go back to printing one line of progress update. But i haven't seen this very often after the latest updates.
firstly the answer:
loaddir = r'D:\surfdrive\COMSOL files\Batch folder\Current batch simulation files'
# loop the files in the directory
print('Data load initiated')
for subdir, dirs, files in os.walk(loaddir_res):
files = [f for f in files if f.startswith('Test')]
for name in tqdm(files):
#do things
This will work in any decent environment (including a bare terminal). The solution is to not give tqdm the unused filenames. You may find https://github.com/tqdm/tqdm/wiki/How-to-make-a-great-Progress-Bar insightful.
Secondly the issue with multiple lines output is well-known and due to some environments being broken (https://github.com/tqdm/tqdm#faq-and-known-issues) by not supporting carriage return (\r).
The correct links for this problem in Spyder are https://github.com/tqdm/tqdm/issues/512 and https://github.com/spyder-ide/spyder/issues/6172
(Spyder maintainer here) This is a known limitation of TQDM progress bars in Spyder. I'd recommend you to open an issue about it in its Github repository.
Specify position=0 and leave=True like this:
for i in tqdm(range(10), position=0, leave=True):
# Some code
Or in a list comprehension:
nums = [i for i in tqdm(range(10), position=0, leave=True)]
It's worth to mention that you can set `position=0` and `leave=True` to be the default settings, so you won't need to specify them each time, like this:
from tqdm import tqdm
from functools import partial
tqdm = partial(tqdm, position=0, leave=True) # this line does the magic
# for loop
for i in tqdm(range(10)):
# Some code
# list comprehension
nums = [for i in tqdm(range(10))]

How to set skipping of uninteresting functions while stepping from gdbinit script?

I'm trying to setup a set of functions to be skipped by gdb from stepping in by commands like:
skip myfunction
. But if I place them in ~/.gdbinit instead of just saying in the terminal gdb prompt, I get the error:
No function found named myfunction.
Ignore function pending future shared library load? (y or [n]) [answered N; input not from terminal]
So I need GDB to get Y answer. I've tried what was suggested for breakpoints as well as set confirm off suggested in a comment to this question. But these don't help with skip command.
How can I set skip in a .gdbinit script, answering Y about future library load?
you can use Python to wait for the execution to start, which is equivalent to pending on:
import gdb
to_skip = []
def try_pending_skips(evt=None):
for skip in list(to_skip): # make a copy for safe remove
try:
# test if the function (aka symbol is defined)
symb, _ = gdb.lookup_symbol(skip)
if not symb:
continue
except gdb.error:
# no frame ?
continue
# yes, we can skip it
gdb.execute("skip {}".format(skip))
to_skip.remove(skip)
if not to_skip:
# no more functions to skip
try:
gdb.events.new_objfile.disconnect(try_pending_skips) # event fired when the binary is loaded
except ValueError:
pass # was not connected
class cmd_pending_skip(gdb.Command):
self = None
def __init__ (self):
gdb.Command.__init__(self, "pending_skip", gdb.COMMAND_OBSCURE)
def invoke (self, args, from_tty):
global to_skip
if not args:
if not to_skip:
print("No pending skip.")
else:
print("Pending skips:")
for skip in to_skip:
print("\t{}".format(skip))
return
new_skips = args.split()
to_skip += new_skips
for skip in new_skips:
print("Pending skip for function '{}' registered.".format(skip))
try:
gdb.events.new_objfile.disconnect(try_pending_skips)
except ValueError: pass # was not connected
# new_objfile event fired when the binary and libraries are loaded in memory
gdb.events.new_objfile.connect(try_pending_skips)
# try right away, just in case
try_pending_skips()
cmd_pending_skip()
Save this code into a Python file pending_skip.py (or surrounded with python ... end in your .gdbinit), then:
source pending_skip.py
pending_skip fct1
pending_skip fct2 fct3
pending_skip # to list pending skips
Documentation references:
GDB Python TOC
Basic Python
Events in Python
Symbols in Python
This feature has been proposed here:
https://sourceware.org/ml/gdb-prs/2015-q2/msg00417.html
https://sourceware.org/bugzilla/show_bug.cgi?id=18531
So far, there's been no activity on that issue for 6 months though. As of writing this, the feature is not included in GDB 7.10.

Completely disable IPython output caching

I'm dealing with some GB-sized numpy arrays in IPython. When I delete them, I definitely want them gone, in order to recover the memory. IPythons output cache is quite annoying there, as it keeps the objects alive even after deleting the last actively intended reference to them. I already set
c.TerminalInteractiveShell.cache_size = 0
in the IPython configuration, but this only disables caching of entries to _oh, the other variables like _, __ and so on are still created. I'm also aware of %xdel, but anyways, I'd prefer to disable it completely, as I rarely use the output history anyways, so that a plain del would work again right away.
Looking at IPython/core/displayhook.py Line 209-214 I would say that it is not configurable. You could try making a PR to add an option to disable it totally.
Enter
echo "__builtin__._ = True" > ~/.config/ipython/profile_default/startup/00-disable-history.py
and your history should be gone.
Edit:
Seems like the path to the config directory is sometimes a bit different, either ~/.config/ipython or just ~/.ipython/. So just check which one you got and adjust the path accordingly. The solution still works with jupyter console.
Seems that we can suppress the output cache by putting a ";" at the end of the line now.
See http://ipython.org/ipython-doc/stable/interactive/tips.html#suppress-output
Create an ipython profile:
!ipython profile create
The output might be (for ipython v4.0):
[ProfileCreate] Generating default config file: '/root/.ipython/profile_default/ipython_config.py'
[ProfileCreate] Generating default config file: '/root/.ipython/profile_default/ipython_kernel_config.py'
Then add the line 'c.InteractiveShell.cache_size = 0' to the ipython_kernel_config.py file by
!echo 'c.InteractiveShell.cache_size = 0' >> /root/.ipython/profile_default/ipython_kernel_config.py
Load another ipython kernel and check if this work
In [1]: 123
Out[1]: 123
In [2]: _1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-51-21553803e553> in <module>()
----> 1 _1
NameError: name '_1' is not defined
In [3]: len(Out)
Out[3]: 0

Setting breakpoint in different file has no effect

The ruby debugger does not halt on breakpoints I set in files different from the on the execution starts in. For example, consider these two files, foo.rb:
# foo.rb
class Foo
def bar
puts "baz"
end
end
and main.rb:
# main.rb
require './foo'
Foo.new.bar
I start debugging using ruby -r debug .\main.rb. Now, when I try to set a breakpoint on a specific line in another file using b ./foo.rb:4, I get the message Set breakpoint 1 at foo.rb:4, but when I cont, the program executes to the end, and the debugger never halts. However, if I break on a line in main.rb, e.g. b ./main.rb:3, or a method, e.g. b Foo.bar, the debugger halts as expected.
Why doesn't the debugger halt at breakpoints in files other than the main file?
Update: I have tried this with Ruby 1.9.3 on Windows 7 as well as OS X 10.8; it doesn't work in either environment.
I have also just realized that the debugger quits after the script has run till the end: I start debugging main.rb, use cont, then baz is printed on the console and I'm right back in the shell. Is this the expected behaviour, or might the debugger have crashed?
Wow, that is weird. Not sure if this helps, but maybe you could do this. Step over the require with next so that Foo is loaded then
b Foo:bar
that should at least break on bar

How to enable ruby methods visualizing in vim

a have googled the question, but found nothing - maybe I do not know how to define the search keywords properly in this case.
I like to use folding in vim when I'm developing Ruby on Rails applications. And my foldcolumn is set to 4. But its visualizing of the start and the end of the ruby method is not so simple and obvious ("-" - "def", "|" - "end"):
-def foo
bar = 1
|end
The question is - is there any plugin for vim, that will show markers (arrows or stmh) near every "def" and "end" like it is done in TextMate (1)?
v def foo
bar = 1
^ end
Also, as I do not have much experience in vim/ruby, maybe there is another, more elegant way to check that all def-end pairs are closed in a particular file? (matchit.vim is not very comfortable for this need)
I hope there is more convenient way to catch lost "ends" than to read "Syntax error" in the console :)
I'm not sure whether it's quite what you need, but have you tried the 'foldcolumn' option? For example, with:
:set foldcolumn=4
You'll get something like this:
- def foo
| bar = 1
| end
- def foo2
| bar = 2
|- if x == 1
|| bar = 3
|| end
| end
See :help 'foldcolumn' for more information. Note that you can click on the - signs to close the folds if your Vim is mouse-enabled.
Edit
If you don't like the fold method, you could use signs (assuming your Vim is signs enabled). Try something like this:
command! RubySigns call RubySigns()
" Optional:
au BufReadPost *.rb call RubySigns()
function! RubySigns()
sign define ruby_end text=^
sign define ruby_def text=v
sign unplace *
g/^\s*\(def\|class\|begin\)\>/exe 'sign place '.line('.').' line='.line('.').' name=ruby_def buffer='.bufnr('%')
g/^\s*end\>/exe 'sign place '.line('.').' line='.line('.').' name=ruby_end buffer='.bufnr('%')
endfunction
It's probably not perfect (I don't know ruby), but it might give you something to get started.

Resources