Merge lp:~fdo.perez/ipython/trunk-dev into lp:ipython/0.11

Proposed by Fernando Perez
Status: Merged
Merged at revision: not available
Proposed branch: lp:~fdo.perez/ipython/trunk-dev
Merge into: lp:ipython/0.11
Diff against target: 12304 lines (+6035/-2557)
88 files modified
IPython/__init__.py (+12/-9)
IPython/config/loader.py (+66/-25)
IPython/config/tests/test_loader.py (+9/-10)
IPython/core/application.py (+206/-81)
IPython/core/completer.py (+89/-73)
IPython/core/crashhandler.py (+41/-47)
IPython/core/debugger.py (+33/-0)
IPython/core/history.py (+47/-25)
IPython/core/hooks.py (+1/-2)
IPython/core/ipapp.py (+295/-184)
IPython/core/iplib.py (+240/-179)
IPython/core/magic.py (+96/-60)
IPython/core/prefilter.py (+72/-15)
IPython/core/prompts.py (+20/-8)
IPython/core/pylabtools.py (+145/-0)
IPython/core/quitter.py (+12/-7)
IPython/core/release.py (+1/-1)
IPython/core/tests/obj_del.py (+0/-35)
IPython/core/tests/simpleerr.py (+32/-0)
IPython/core/tests/tclass.py (+14/-10)
IPython/core/tests/test_completer.py (+35/-0)
IPython/core/tests/test_iplib.py (+216/-28)
IPython/core/tests/test_magic.py (+137/-190)
IPython/core/tests/test_prefilter.py (+34/-0)
IPython/core/tests/test_run.py (+174/-0)
IPython/core/ultratb.py (+60/-21)
IPython/core/usage.py (+34/-325)
IPython/extensions/parallelmagic.py (+4/-0)
IPython/extensions/pretty.py (+3/-58)
IPython/extensions/tests/test_pretty.py (+51/-6)
IPython/external/_numpy_testing_utils.py (+120/-0)
IPython/external/argparse.py (+89/-38)
IPython/external/decorators.py (+245/-51)
IPython/frontend/prefilterfrontend.py (+17/-37)
IPython/frontend/tests/test_prefilterfrontend.py (+9/-6)
IPython/gui/wx/ipshell_nonblocking.py (+15/-14)
IPython/kernel/__init__.py (+1/-1)
IPython/kernel/clusterdir.py (+53/-65)
IPython/kernel/core/interpreter.py (+3/-4)
IPython/kernel/core/tests/test_redirectors.py (+6/-6)
IPython/kernel/engineservice.py (+3/-3)
IPython/kernel/error.py (+2/-2)
IPython/kernel/ipclusterapp.py (+8/-19)
IPython/kernel/ipcontrollerapp.py (+14/-34)
IPython/kernel/ipengineapp.py (+8/-27)
IPython/kernel/tests/test_multienginefc.py (+9/-1)
IPython/kernel/tests/test_taskfc.py (+9/-1)
IPython/lib/inputhook.py (+49/-3)
IPython/quarantine/InterpreterPasteInput.py (+0/-124)
IPython/scripts/iptest (+19/-3)
IPython/testing/__init__.py (+18/-0)
IPython/testing/_doctest26.py (+110/-0)
IPython/testing/_paramtestpy2.py (+89/-0)
IPython/testing/_paramtestpy3.py (+62/-0)
IPython/testing/decorators.py (+84/-14)
IPython/testing/decorators_trial.py (+0/-132)
IPython/testing/globalipapp.py (+168/-0)
IPython/testing/iptest.py (+349/-185)
IPython/testing/ipunittest.py (+189/-0)
IPython/testing/nosepatch.py (+53/-0)
IPython/testing/parametric.py (+3/-0)
IPython/testing/plugin/ipdoctest.py (+27/-180)
IPython/testing/plugin/test_ipdoctest.py (+0/-19)
IPython/testing/tests/test_decorators.py (+71/-13)
IPython/testing/tests/test_decorators_trial.py (+9/-4)
IPython/testing/tests/test_ipunittest.py (+122/-0)
IPython/testing/tests/test_tools.py (+31/-9)
IPython/testing/tools.py (+255/-7)
IPython/utils/baseutils.py (+51/-0)
IPython/utils/genutils.py (+129/-41)
IPython/utils/platutils.py (+1/-1)
IPython/utils/platutils_posix.py (+14/-1)
IPython/utils/tests/test_genutils.py (+71/-51)
IPython/utils/tests/test_imports.py (+0/-1)
IPython/utils/tests/test_platutils.py (+19/-6)
MANIFEST.in (+1/-0)
README.txt (+22/-4)
docs/emacs/ipython.el (+1/-1)
docs/source/development/coding_guide.txt (+34/-0)
docs/source/development/index.txt (+1/-0)
docs/source/development/magic_blueprint.txt (+103/-0)
docs/source/development/testing.txt (+366/-32)
docs/sphinxext/ipython_directive.py (+641/-0)
iptest.py (+26/-0)
ipython.py (+6/-2)
setup.py (+44/-3)
setupegg.py (+1/-7)
tools/build_release (+6/-6)
To merge this branch: bzr merge lp:~fdo.perez/ipython/trunk-dev
Reviewer Review Type Date Requested Status
Brian Granger overview Needs Fixing
Review via email: mp+16695@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Fernando Perez (fdo.perez) wrote :

Ready for review, right now it fixes the pylab support and a few other things.

Highlights:

%pylab [GUINAME]

works at any time as a magic. -pylab and -*thread also work like they used to.

Revision history for this message
Fernando Perez (fdo.perez) wrote :

Last revision fixes %magic tab completion, bringing us closer to 0.10 everyday functionality...

lp:~fdo.perez/ipython/trunk-dev updated
1230. By Fernando Perez

Small fix to info message when pylab starts.

1231. By Fernando Perez

Fix tab-completion for magics, other completion cleanup and fixes.

Mostly simplifying the code and updating it, no major functionality changes
other than fixing magic completion, which had broken in the 0.11 refactor.

Also added a small test file for completion-specific tests.

1232. By Fernando Perez

Remove redundant decorators and unify into decorators.py

1233. By Fernando Perez

Added test support for better parametric tests and nose fixes.

These are the underlying support files needed by the rest.

1234. By Fernando Perez

Add new testing support machinery with better parametric tests.

Also included are new tools for doctests with ipython syntax.

1235. By Fernando Perez

Update numpy's decorators.py from upstream.

1236. By Fernando Perez

Fix quitting: now, typing bare 'exit' or 'quit' unconditionally quits.

We still ask for confirmation by default with C-d (EOF), since that can
happen accidentally quite easily (though users can always disble that safety
if desired).

1237. By Fernando Perez

Fix auto-quoting via ',' as the first character, which had broken in refactor.

1238. By Fernando Perez

Fix debugging with breakpoints.

URL: https://bugs.launchpad.net/ipython/+bug/381069

1239. By Fernando Perez

Fix bugs when readline is absent (pdb and quitting would crash badly)

https://bugs.launchpad.net/ipython/+bug/495173

1240. By Fernando Perez

Fix bug where output prompts were coming *after* output values.

This means that on exit/quit, we'll see something, so I added a simple 'bye'
to those. Not pretty, but easier than messing with the prompt logic.

1241. By Fernando Perez

Better handling of no-readline.

1242. By Fernando Perez

Fix broken multiline input - ugly fix, needs cleanup later.

See comment in code, my fix is nasty and should be cleaned up later, but
right now I can't do it (no time to understand the component tree machinery
well enough for a clean fix).

1243. By Fernando Perez

Minor fix to exit/quit

1244. By Fernando Perez

Update argparse to current SVN.

This fixes a few small bugs from upstream, but more importantly, syncs us so
we can add some local changes (see next commit)

1245. By Fernando Perez

Small improvements to argparse.

I've submitted these fixes upstream, let's hope they get included:

http://groups.google.com/group/argparse-devs/browse_thread/thread/2c03e4b3d28a3203

1246. By Fernando Perez

Cleaner access to log call.

At least now we're not walking the component tree manually... A bigger
refactor is still needed here, but this code is less brittle than multiple
.parent access.

1247. By Fernando Perez

Simpler/cleaner version of %who_ls.

Functionally equivalent but with simpler and faster code (I was fixing up
pylab support and saw this old code, fixed it up to more modern standards).

1248. By Fernando Perez

Testing '#:' attribute docstrings for sphinx.

This is supposed to work and give us class/instance docstrings, but it's not
yet. I have a question on the sphinx list about it, let's leave it in the
code until we figure out what we need to do to sphinx for it to work (it
doesn't hurt otherwise).

1249. By Fernando Perez

Added --gui to match %gui use, better docs and behavior for %pylab code.

Now both %gui and %pylab have identical syntax if used at startup as --gui
and --pylab. This can be useful to people setting up aliases or other
shell-based support.

Also cleaned up %pylab so it doesn't pollute the output of %who with all
loaded symbols.

1250. By Fernando Perez

Equivalent but simpler/faster code in loader.py.

1251. By Fernando Perez

Deactivate stray debug print statement.

1252. By Fernando Perez

Manage and propagate argv correctly.

All Application objects should take argv in their constructor, akin to how
the standard signature of C programs is "main(int argc, char *argv)". This
makes it possible to initialize them from code with different command-line
options (otherwise, they end up directly accessing sys.argv[1:] via
argparse).

1253. By Fernando Perez

Progress towards getting the test suite in shape again.

Work all over the place to get more tests to pass.

1254. By Fernando Perez

Close https://bugs.launchpad.net/ipython/+bug/503729

Removed stale tests that do not apply anymore.

1255. By Fernando Perez

Fix failing config test: https://bugs.launchpad.net/ipython/+bug/503731

Thanks to Brian for the fix!

Revision history for this message
Fernando Perez (fdo.perez) wrote :

OK, this is in pretty good shape now: except for the twisted problem (which I suspect is tiny, I just don't know what to fix) we have again a usable test suite. Ready for review.

lp:~fdo.perez/ipython/trunk-dev updated
1256. By Fernando Perez

Make testing easier by exposing a top-level test() function.

Fixes https://bugs.launchpad.net/ipython/+bug/504450

1257. By Fernando Perez

Work in multiple places to improve state of the test suite.

With these changes, on my system now all the test sub-suites pass except
for the Twisted one (see https://bugs.launchpad.net/ipython/+bug/504515 for
details on that one).

1258. By Fernando Perez

Improve test suite robustness by cleaning up stale processes when possible.

1259. By Fernando Perez

A few small fixes so ipythonx works, and PEP-8 cleanups I found along the way.

1260. By Fernando Perez

Minimal fixes so ipython-wx runs.

I've only done the most basic checks that it starts and simple things work,
this probably needs much more work in the future.

1261. By Fernando Perez

Cleaner error message in crash handler.

1262. By Fernando Perez

Move crash handling to the application level and simplify class structure.

Starting to try to take real advantage of the refactoring, to have generic
crash handling. This also lets us initialize the app without needing all
the self.attempt() wrappers, since now there's a good system-wide crash
handler at the app level (not inside the shell instance). I didn't yet
remove the attempt() method because we may have occasional uses for it (we
still do, but in one place only).

I also removed some extra class layers that weren't quite needed. Creating
classes solely for the purpose of passing parameters makes the code (IMO)
harder to understand, I kept getting lost in parts of the class hierarchy.
I think these changes provide the same flexibility but with easier to follow
code (less things to remember, basically). What I tried to do was to use
argument passing instead of inheritance for all cases I saw where the
inheritance wasn't really adding new functionality. In some cases, this
actually allowed me to remove methods that were effectively duplicated in
the subclasses.

1263. By Fernando Perez

Merging with upstream trunk.

1264. By Fernando Perez

Make IPython work if a recent numpy is not available.

I updated the numpy testing decorators module and didn't notice they
recently introduced dependencies that make it use the rest of numpy. We may
later want to revisit this, as my solution is ugly, but I simply hacked it
to load locally the code they added, which we now carry as
_numpy_testing_utils.

Ultimately the solution to this should be taken up with upstream numpy, so
that we all agree on a common set of testing decorators with no other
dependencies aside from the stdlib. But for now, this will let us move
forward as that discussion could take a while.

1265. By Fernando Perez

Make setupegg.py work on Windows.

Fixes: https://bugs.launchpad.net/ipython/+bug/505009

1266. By Fernando Perez

Fix deprecation warning from os.popen3.

Patch provided by Pim Schellart <p.schellart-AT-gmail.com>, slightly
modified to fix missing supbrocess references.

Fixes https://bugs.launchpad.net/ipython/+bug/488061

1267. By Fernando Perez

Fix broken %time magic.

Fixes https://bugs.launchpad.net/ipython/+bug/497187

1268. By Fernando Perez

Remove dead code accidentally left over in prior refactor.

1269. By Fernando Perez

Update version requirement to 2.5, since that's what we're using anyway.

Fixes https://bugs.launchpad.net/ipython/+bug/505090

1270. By Fernando Perez

Massive amount of work to improve the test suite, restores doctests.

After Brian's comments, I realized that our test machinery was NOT in
reality running all the ipython-syntax doctests we have. This is now fixed.
The test suite isn't completely passing, but this commit is for the
underlying machinery. I will now work on fixing as many broken tests as I
can.

Fixes https://bugs.launchpad.net/ipython/+bug/505071

1271. By Fernando Perez

Fix extensions test suite (small, but now it runs and passes!)

1272. By Fernando Perez

Fix test I broke when moving a file.

1273. By Fernando Perez

Fix config part of the test suite.

1274. By Fernando Perez

Fix failing doctest in utils.

1275. By Fernando Perez

Make all non-twisted tests pass.

Removed a test (builtins) that was already being done in test_run, fixed
broken doctest and fixed prefilter handling for empty buffers.

The only remaining failing test is now in twisted.

1276. By Fernando Perez

Disable completely two sets of Twisted tests so the suite runs.

The problem is that these tests are not just failing, they hang (even
ignoring Ctrl-C), so they prevent the suite from running at all. But I
don't know how to fix them, so I disabled them; this way others can at least
run the test suite, and hopefully Brian can pitch in with a fix.

1277. By Fernando Perez

Fix %history magics.

Fixes: https://bugs.launchpad.net/ipython/+bug/505343

1278. By Fernando Perez

Improve setuptools support.

Many thanks to Gael for the code/idea for the solution, see bug page for
details.

Fixes: https://bugs.launchpad.net/ipython/+bug/504968

1279. By Fernando Perez

Add file I forgot! Thanks to J. Hunter for report.

1280. By Fernando Perez

Patch by Beni Cherniavsky to improve tab-completion in emacs with 2.6.

https://bugs.launchpad.net/ipython/+bug/414967

1281. By Fernando Perez

Add transformers to understand code pasted with >>> or IPython prompts.

Now the following all work out of the box:

In [8]: In [6]: for i in range(5):
   ...: ...: print i,
   ...: ...:
   ...:
0 1 2 3 4

In [10]: >>> width = 20

In [11]: >>> height = 5*9

In [12]: >>> width * height
Out[12]: 900

And the history is still clean:

In [13]: %hist -n
[snipped]
for i in range(5):
    print i,

get_ipython().magic("hist -n")
width = 20
height = 5*9
width * height

This will be extremely useful when copy/pasting from interactive tutorials,
doctests and examples.

Also fixes %doctest_mode: https://bugs.launchpad.net/ipython/+bug/505404

1282. By Fernando Perez

Unify command-line usage information in one place.

Fixes: https://bugs.launchpad.net/ipython/+bug/505047

1283. By Fernando Perez

Simplify options handling code by using argparse argument_default.

The argparse.ArgumentParser class supports an instance-wide default, so we
can set that once instead of having every argument configured with the same
default always.

1284. By Fernando Perez

Apply argparse code simplification to all kernel scripts.

1285. By Fernando Perez

Update public revnum to make test installers for wider use.

At this point, all scripts, tests and doc build work on linux.

1286. By Fernando Perez

Update release tool to always produce zip file.

1287. By Fernando Perez

Fixes for python2.5 compatibility.

1288. By Fernando Perez

Minor fixes to build scripts.

build_rpm failing, don't know why; we may just stop providing rpms as those
are normally the distributor's problem.

1289. By Fernando Perez

Fix bug with autocall and multiline input recalled from readline buffer.

Reported by John Hunter on list.

1290. By Fernando Perez

Added new Tee class, that works much like Unix's 'tee' command.

I needed it to debug the IPython sphinx directive (next commit).

Also fixed some typos along the way.

1291. By Fernando Perez

Added ability to invoke pdb on IPython crashes.

For now it has to be turned on manually in the code, will add a flag later.
But very useful for debugging apps that use/extend IPython itself and can
possibly crash it.

1292. By Fernando Perez

Fix minor error that I saw in some odd cases, not sure how to test for it.

Unfortunately I don't have a reliable test case for this, it's deep in
traceback handling. But I did see a few times ultratb itself crash.

1293. By Fernando Perez

Ported the IPython Sphinx directive to 0.11.

This was originally written by John Hunter for the 0.10 API, now it works
with 0.11. We still need to automate its test suite, but at least now it
runs and the script itself can be executed as a test that produces screen
output and figures in a subdir.

1294. By Fernando Perez

Lots of work on exception handling, including tests for traceback printing.

We finally have some tests for various exception mode printing, via doctests
that exercise all three modes!

Also changed handling of sys.exit(X) to only print the summary message, as
SystemExit is most often a 'handled' exception. It can still be 100%
silenced via '%run -e', but now it's much less intrusive.

Added a new %tb magic to print the last available traceback with the current
xmode. One can then re-print the last traceback with more detail if
desired, without having to cause it again.

1295. By Fernando Perez

Changed %hist to default to NOT printing numbers, added -p and -o options.

-p: print python prompts before inputs
-o: print output as well

Together, these two make '%hist -op' an easy way to get doctest-valid
sessions generated from prior input.

1296. By Fernando Perez

Remove accidentally introduced runtime nose dependencies.

1297. By Fernando Perez

Fix bug with aliases, exposed by windows test suite.

1298. By Fernando Perez

Clean up and document better starting process of global IPython in tests.

1299. By Fernando Perez

Fix subtle bug in doctests (ipython namespace was being destroyed).

Exposed by windows test suite.

1300. By Fernando Perez

Various fixes for IPython.core tests to pass under win32.

1301. By Fernando Perez

Various fixes for test_genutils under win32, now all tests pass.

1302. By Fernando Perez

Remove executable bit, this is a module.

1303. By Fernando Perez

More fixes for win32 test suite.

1304. By Fernando Perez

Fix argument parsing bug in win32, clean up temp files in %hist doctest.

1305. By Fernando Perez

More win32 test fixes and a new test.

Mostly handling escaping of filenames across platforms.

1306. By Fernando Perez

Add new @onlyif decorator, the reverse of @skipif.

It's trivial, but it makes certain conditions much cleaner to express.

1307. By Fernando Perez

More win32 fixes and refinements to test suite.

1308. By Fernando Perez

Fix test suite when Twisted not available, cleanups to iptest for clarity.

1309. By Fernando Perez

Fix test failure when pexpect not available

1310. By Fernando Perez

Fix another win32 test failure.

As of this commit, the entire test suite passes on a system that only has:

- Python 2.6.4 from Python.org
- Pyreadline 1.5
- IPython (this branch)
- nose 0.11.1

Tests that require Twisted, graphics or other machinery get skipped, but
everything else runs and passes.

The glaring remaining problem on win32 is that right now, colored output is
broken for some reason (though prompts are OK). I'll try to fix that next.

1311. By Fernando Perez

Remove leftover print statement.

1312. By Fernando Perez

Add writeln convenience method to Term streams.

1313. By Fernando Perez

Fix exception color problems in win32.

In all the recent work to have the test suite play nice with doctest of full
ipython sessions, I inadvertedly started sending exceptions directly to
sys.stderr. On windows we MUST go via Term.cerr, which uses pyreadline to
handle color escapes, while sys.stderr just shows garbage on screen.

As of this revision, the test suite passes fully on win32 and linux, and
interactive use also seems OK on all fronts.

We're getting closer to RC status...

1314. By Fernando Perez

Move cleanup to main setup.py, where it belongs.

Distutils now generates .egg-info stuff even without setuptools, so we
should do the cleanup in the main script.

1315. By Fernando Perez

Fixes for test suite in win32 when all dependencies (esp. Twisted) are
installed.

Also activated testing.tools to be picked up by the test suite (was
excluded), this gives us a few more tests.

Status:

- On Linux, the full suite passes like before.

- On Win32, now that we have Twisted, we're seeing a few failures, because I
don't have the WinHPC server stuff. These should be easy for Brian to fix.
There are also two tests where the Skip nose exception isn't recognized by
Twisted, should also be easy. I'll file tickets for those.

1316. By Fernando Perez

Fix the two Twisted trial errors under win32.

I had incorrectly used a nose decorator instead of the twisted approach.

1317. By Fernando Perez

Changed trial skip to be a simple flag, now that I know this works.

I thought it needed to be a method, apparently not, use the simplest thing
that works.

1318. By Fernando Perez

Include 'kernel' in the nose tests as well; it picks up doctests that trial
doesn't.

1319. By Fernando Perez

Fix bug where python -c "import IPython; IPython.test()" could fail.

Depending on where one was located when typing it, it could fail. Made all
necessary paths absolute so this doesn't happen.

1320. By Fernando Perez

Added notes with ideas about new design for %magics.

1321. By Fernando Perez

Wrote up description with examples of our testing system.

1322. By Fernando Perez

Update revnum; ready for testing, will upload and send to the dev list.

Docs have been added, this is close to a reasonable RC for 0.11.

As of this revision, we're down to 0 errors on linux and 1 on windows (that
appears twice). The windows error is tracked here:

https://bugs.launchpad.net/ipython/+bug/506839

1323. By Fernando Perez

Clean up output of %who

1324. By Fernando Perez

Make an error message a bit easier to understand.

1325. By Fernando Perez

Fix %history to stop printing last line.

This was triggering a recursive calling of %hist in doctests, resulting in
spurious output from the test suite.

Closes https://bugs.launchpad.net/ipython/+bug/507705

Also minor pep-8 formatting cleanups.

1326. By Fernando Perez

Fix test I'd broken with change of reporting in %who.

1327. By Fernando Perez

Fix warning on startup if user didn't have personal copies of cluster config.

We use the builtin defaults in this case, we were just missing searching in
our own paths.

Also suppress deprecation warnings from Twisted.

1328. By Fernando Perez

Minimize stdout side effects in testing instances.

1329. By Fernando Perez

Fix frontend tests so they obtain the right global test IPython object.

1330. By Fernando Perez

Fix names so these doctests pass correctly when run in bare 'iptest IPython'

1331. By Fernando Perez

Let iptest pass arguments correctly to nose (in-process or in subprocess).

Fixes https://bugs.launchpad.net/ipython/+bug/507079

Also, remove relative imports from the iptest.py module so it can be run as
a script as well under 2.5 or 2.6.

1332. By Fernando Perez

Make it possible to run the tests from the source dir without installation.

Also clean up the handling of command-line options for iptest, so that all
options given at the top-level are correctly passed to nose.

Fixes: https://bugs.launchpad.net/ipython/+bug/507079

1333. By Fernando Perez

Moved twisted import into a function, so base exception classes in kernel
can be imported without twisted present.

1334. By Fernando Perez

Fixes to make test suite more robust on Fedora.

I fond some spurious warnings on Fedora, extra noise on stdout and other
small problems this commit fixes.

The test suite now runs cleanly on Fedora11 without Twisted available.

1335. By Fernando Perez

Do not call %run with quotes, since on Windows quotes aren't stripped.

On Posix, shlex strips quotes from filenames, but not on Windows. So if we
call %run "somefilename" on Win32, we get a 'file not found' error. This
problem was appearing for the test suite on win32.

1336. By Fernando Perez

Ensure that __builtin__ is always available and compute prompts from it.

__builtins__ (note the s) can change from module to dict somewhat at random,
see this for details:

http://mail.python.org/pipermail/python-dev/2001-April/014068.html

If this happened, our prompt computations could go out of whack in bizarre
ways, I saw it happen for continuation prompts after loading pylab, for
example. This change fixes it.

1337. By Fernando Perez

Comment out accidentally left over 'raise'.

This function promises NEVER to raise an exception, I was propagating it out
while debugging some odd behavior with __builtins__ and left it accidentally
in place.

1338. By Fernando Perez

Fix tests to return consistent results regardless of how they are called.

Now, calling:

- 'python iptest.py' from source dir or from src/IPython/testing should give
the same results

- 'python iptest.py IPython.SOMETHING' from either place should also give
the same results.

1339. By Fernando Perez

Make the test suite runnable without X11 connections.

This fix makes the test suite work in settings like a screen session that
can import but not initialize GTK.

1340. By Fernando Perez

Added %exit, %quit and %Quit as magics for consistency.

1341. By Fernando Perez

Doc updates to testing to reflect recent changes.

Updated revnumber to push an updated testing release out for this branch.

1342. By Fernando Perez

Include iptest.py in MANIFEST so it gets shipped.

1343. By Fernando Perez

Give good error message when starting tests if nose is missing.

1344. By Fernando Perez

Inform user at install time of minimal python requirements if not met.

Fixes: https://bugs.launchpad.net/ipython/+bug/505090

1345. By Fernando Perez

Robustness fixes in test suite machinery.

Added a module-level INSTALLED flag, which can be set to false if the test
suite is being run in-place (without ipython having been installed at all).
This is because how we call and import things must be done differently
depending on whether the code is installed or is being run in-place. The
only ones that can know this reliably are the entry-point scripts, so those
are responsible for setting this flag.

Also made the code that validates ipython in subprocesses report errors
better, by checking stderr for errors before validating stdout output, as
anything on stderr will be likely informative of the real problem.

1346. By Fernando Perez

Moved system info into its own utility, so we can use in in test suite too.

1347. By Fernando Perez

Added diagnostics printout at the end of the test suite.

This will make it easier for us to understand problem reports from users.

Revision history for this message
Brian Granger (ellisonbg) wrote :
Download full text (8.4 KiB)

Here are my comments for the first 50 commits.

1223
====

application.py: Why are profile_name, ipython_dir set to None in class
definition?

1224
====

magic.py

(80,10): Really wierd problem with making Magic a new style class!

(1571,7): Why self.shell instead of self?

I think you move some of this to another file later on.

1225
====

Maybe this explains why profile_name and ipython_dir are set to None
in r1223. I think this makes sense.

1226, 1227, 1228
================

1229
====

ipapp.py

I like how the pylab enabling has been implemented in ipapp and iplib.

(540,12): The call to InteractiveTB.set_mode(mode=self.shell.xmode) should
be moved into a method of iplib.InteractiveShell that is called when
InteractiveShell.__init__ is called. There is probably already a method
that instantiates the InteractiveTB instance. The self.shell.xmode attribute
should already be set by the config system.

pylabtools.py:

This should probably be moved to IPython/lib like inputhook.py. The idea of
the lib subpackage is to help us keep core as small as possible. Eventually
core should not depend on the terminal or on anything outside of the stdlib.
The way I think of it is that maybe someday, core would make it into the std
lib itself. So lib is for non-optional things (optional things go into extensions)
that add additional functionality to IPython beyond the core.

1230
====

1231
====

completer.py

In protect_filename maybe a note on why it is needed to protect () on win32.

1232
====

Where did the decorators_trial.py go? I thought we were still using these
to give a single interface for skipping tests in nose/trial. Maybe a later
commit will explain this...

1233
====

Why is _paramtestpy2.py and _paramtestpy3.py named with a leading underscore?
I am guessing that they are imported in a way that is python version
dependent?

Include the standard headers (copyright, etc.) in these files.

1234
====

Yeh, 1, 2, 3, 4!

In test_decorators.py it looks like there is a reference to the twisted
testing decorators that vanished. How are we handling that now?

1235, 1236
==========

1237
====

In prefilter near line 158, why ip.shell._ofind, rather than ip._ofind?

1238, 1239
==========

1240
====

quitter.py, line 36, do we want to print "Bye" on exit. Seems a bit verbose.

1241
====

Great.

1242
====

I think the self.parent.parent can be replaced by self.shell.

1243
====

Can we return an empty string instead of 'Bye'?

1244, 1245
==========

1246
====

OK, this fixes issue with r1242.

1247
====

Great.

1248
====

Should we start using this on all attributes?

1249
====

Can we renamed user_config_ns to something that reflects how it is used by
%who? Like user_ns_exclude? Eventually, when magics are components, the who
magic itself should track this namespace and provide a simple API for adding
things to it.

1250
====

Is this the code that enables you to get rid of the default=NoConfigDefault
all over the place? If so that is great. It is important that most command
line options use NoConfigDefault as the default value.

1251
====

1252
====

Very nice!!!

1253
====

application.py:

Anytime things change in application.py, we need t...

Read more...

review: Needs Fixing (careful)
Revision history for this message
Brian Granger (ellisonbg) wrote :
Download full text (4.8 KiB)

Here are my next 50 commit reviews:

1274, 1275
==========

1276
====

Brian needs to look at this. Create a ticket for this.

1277, 1278
==========

1279
====

I really don't like that we have to do this stuff to run out test suite.

Putting get_ipython in __builtin__ is a bad idea as it suggest that
get_ipython is a global function which is can't be (it always needs to be
attached to a particular InteractiveShell). I worry that with our test suite
having this global, people will start to write their code expecting this (see
above comments on how the pretty extension calls the global get_ipython).

Also, this code is where we are creating an IPythonApp, but we should
probably be creating a raw InteractiveShell.

....the code smell of this is still bad...

1280
====

1281
====

Oooohh, I love this!

1282
====

1283
====

I see you are deleting all the NoConfigDefault keywords arguments. It is
important that the default is set to this. The config system won't work
otherwise. How is this being handled? If it is not handled properly,
the command line options won't be able to override those of the command line.

Also, the parallel computing apps also rely on this particular handling of
NoConfigDefault. Those need to be updated if there is a new way of this
working.

1284
====

The defaults are not set to NoConfigDefaults, so it is not clear the config
system is working properly.

In ipclusterapp.py it looks like this is handled by the argument_default
argument to ArgumentParser.

In ipclusterapp.py 364,365, these should capture the deferreds (d, d2) even
though we are not using them. This is to emphasize that these functions
return deferreds. This code is super subtle to debug and it helps to
show which functions return deferreds. Either that or make a comment about
these functions returning a deferred.

Same issues about NoConfigDefault in ipcontrollerapp/ipengineapp.

1285, 1286, 1287, 1288, 1289
============================

1290
====

Let put new utilities (like teee) in their own modules rather than genutils.
This will at least prevent the genutils situation from getting worse. Also,
new things tend to get lost in genutils.

1291, 1292
==========

1293
====

application.py:

The logic in Application is already extremely complex. I think that adding the
new constructor_config and override_config logic takes the complexity over the
top.

The main goals of an Application are to manage the complex logic of building
a config from the command line and config files. The new constructor_config
and override_config logic seems like it is simply trying to get around
what Application is doing. This basically makes Application *more* complex
in order to get it doing less.

I think this is a perfect case where we should simply directly instantiate
an InteractiveShell class directly. All options can be passed by creating
a single config object and setting its attributes as needed.

This approach will limit the complexity in Application and provide a simpler
way of simply instantiating the object you want.

I think it may be just the old habit of thinking of Application as the
refactored make_ipython function. In reality, the parts of the old
mak...

Read more...

review: Needs Fixing (careful)
Revision history for this message
Brian Granger (ellisonbg) wrote :
Download full text (3.6 KiB)

Final 50 commits...

1319-1320
=========

1321
====

On line 214, "betwee"

1322-1325
=========

1327
====

I don't think that the config/default dir should be added to the default
search path for the cluster config files. The reason is that all of the
cluster apps *require* a cluster directory that can be written to
(the log and security subdirs have to be created.). All of the cluster
apps should automatically handle the copying of the default config files
to the users cluster directory. If this is not happening, it is a bug.

Thus, I think this change should be reverted and tested to make sure it is
behaving properly.

The changes to silence warnings related to Twisted and sets are fine though.

1328
====

tools.py:

I think that these command line options should be made into a config object
when the testing machinery stops creating an IPythonApp and creates an
InteractiveShell instead.

But what is ipexec used for? It might need the command line options.

1329
====

Again, the global get_ipython is scary. Maybe we should just rename it to
something like get_testing_ipython to emphasize the difference between it and
the get_ipython that appears in the user's namespace?

1330-1331
=========

1332
====

This commit allows iptest to be run from the source dir. I am 50/50 on keeping
this feature. The reason is that part of what we want users to test is if
IPython *can* actually be installed.

The top-level ipython.py is great though.

1333
====

Moving the twisted import in error.py inside a function is fine, but why was
this needed? I thought the idea is that things in kernel can depend on
twisted.

1334
====

iptest.py:

I see you removed kernel from the list of packages that nose will test. But
you also removed kernel from the trial_packages. Is it being tested?

OK, later in the diff I see you add kernel to both nose_pkg_names and
trial_pkg_names. I am pretty sure that it *MUST* be removed from the
nose_pkg_names lis (see above comments about testing twisted using code with
nose).

baseutils.py:

baseutils sounds like another genutils disaster waiting to happen. While
the docstring makes it perfectly clear why it is called baseutils, we need
to get into the habit of organizing functions and classes in utils into
logic modules. For example all functions and classes from genutils that handle
external command execution, should be in a single module.

There can be dependencies between such modules in IPython/utils, but *nothing*
in utils should import anything from the rest of IPython (outside of utils).

Summary, let's get rid of baseutils and start to break genutils up into logical
modules that focus on one thing.

1335
====

I am suspicious of this fix. As I recall, the problem was much deeper than
this. Let's make sure we look through the tickets. I think R. Kern helped
us figure out what was going on.

1336-1344
=========

1345
====

iptest.py: This type of thing (INSTALLED) makes me want to get rid of the
ability to run the top-level iptest without installing IPython. It just
makes the test suite more fragile and harder to debug.

tools.py/iptest.py: Same thought about INSTALLED and the top-level iptest.py.

1346
====

I like ha...

Read more...

review: Needs Fixing (careful)
Revision history for this message
Brian Granger (ellisonbg) wrote :

I have submitted my comments in three batches. Overall, this is a fantastic bit of coding!

It might be useful to talk about my comments so I can summarize the main points. After you
make changes I will begin to install, run the test suite and test the parallel computing stuff.

Cheers,

Brian

review: Needs Fixing (overview)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'IPython/__init__.py' (properties changed: -x to +x)
2--- IPython/__init__.py 2009-08-26 20:54:07 +0000
3+++ IPython/__init__.py 2010-01-18 01:17:11 +0000
4@@ -16,18 +16,17 @@
5 #-----------------------------------------------------------------------------
6 # Imports
7 #-----------------------------------------------------------------------------
8+from __future__ import absolute_import
9
10 import os
11 import sys
12-from IPython.core import release
13
14 #-----------------------------------------------------------------------------
15 # Setup everything
16 #-----------------------------------------------------------------------------
17
18-
19-if sys.version[0:3] < '2.4':
20- raise ImportError('Python Version 2.4 or above is required for IPython.')
21+if sys.version[0:3] < '2.5':
22+ raise ImportError('Python Version 2.5 or above is required for IPython.')
23
24
25 # Make it easy to import extensions - they are always directly on pythonpath.
26@@ -39,11 +38,16 @@
27 #-----------------------------------------------------------------------------
28
29 # In some cases, these are causing circular imports.
30-from IPython.core.iplib import InteractiveShell
31-from IPython.core.embed import embed
32-from IPython.core.error import TryNext
33+from .config.loader import Config
34+from .core import release
35+from .core.application import Application
36+from .core.ipapp import IPythonApp
37+from .core.embed import embed
38+from .core.error import TryNext
39+from .core.iplib import InteractiveShell
40+from .testing import test
41
42-from IPython.lib import (
43+from .lib import (
44 enable_wx, disable_wx,
45 enable_gtk, disable_gtk,
46 enable_qt4, disable_qt4,
47@@ -61,4 +65,3 @@
48 __license__ = release.license
49 __version__ = release.version
50 __revision__ = release.revision
51-
52
53=== modified file 'IPython/config/loader.py'
54--- IPython/config/loader.py 2010-01-08 22:28:30 +0000
55+++ IPython/config/loader.py 2010-01-18 01:17:11 +0000
56@@ -1,10 +1,10 @@
57-#!/usr/bin/env python
58-# encoding: utf-8
59+# coding: utf-8
60 """A simple configuration system.
61
62-Authors:
63-
64+Authors
65+-------
66 * Brian Granger
67+* Fernando Perez
68 """
69
70 #-----------------------------------------------------------------------------
71@@ -37,7 +37,25 @@
72 class ConfigLoaderError(ConfigError):
73 pass
74
75+#-----------------------------------------------------------------------------
76+# Argparse fix
77+#-----------------------------------------------------------------------------
78+# Unfortunately argparse by default prints help messages to stderr instead of
79+# stdout. This makes it annoying to capture long help screens at the command
80+# line, since one must know how to pipe stderr, which many users don't know how
81+# to do. So we override the print_help method with one that defaults to
82+# stdout and use our class instead.
83
84+class ArgumentParser(argparse.ArgumentParser):
85+ """Simple argparse subclass that prints help to stdout by default."""
86+
87+ def print_help(self, file=None):
88+ if file is None:
89+ file = sys.stdout
90+ return super(ArgumentParser, self).print_help(file)
91+
92+ print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__
93+
94 #-----------------------------------------------------------------------------
95 # Config class for holding config information
96 #-----------------------------------------------------------------------------
97@@ -279,22 +297,50 @@
98
99
100 class ArgParseConfigLoader(CommandLineConfigLoader):
101+ #: Global default for arguments (see argparse docs for details)
102+ argument_default = NoConfigDefault
103
104- # arguments = [(('-f','--file'),dict(type=str,dest='file'))]
105- arguments = ()
106-
107- def __init__(self, *args, **kw):
108+ def __init__(self, argv=None, arguments=(), *args, **kw):
109 """Create a config loader for use with argparse.
110
111- The args and kwargs arguments here are passed onto the constructor
112- of :class:`argparse.ArgumentParser`.
113+ With the exception of ``argv`` and ``arguments``, other args and kwargs
114+ arguments here are passed onto the constructor of
115+ :class:`argparse.ArgumentParser`.
116+
117+ Parameters
118+ ----------
119+
120+ argv : optional, list
121+ If given, used to read command-line arguments from, otherwise
122+ sys.argv[1:] is used.
123+
124+ arguments : optional, tuple
125+ Description of valid command-line arguments, to be called in sequence
126+ with parser.add_argument() to configure the parser.
127 """
128 super(CommandLineConfigLoader, self).__init__()
129+ if argv == None:
130+ argv = sys.argv[1:]
131+ self.argv = argv
132+ self.arguments = arguments
133 self.args = args
134- self.kw = kw
135+ kwargs = dict(argument_default=self.argument_default)
136+ kwargs.update(kw)
137+ self.kw = kwargs
138
139 def load_config(self, args=None):
140- """Parse command line arguments and return as a Struct."""
141+ """Parse command line arguments and return as a Struct.
142+
143+ Parameters
144+ ----------
145+
146+ args : optional, list
147+ If given, a list with the structure of sys.argv[1:] to parse arguments
148+ from. If not given, the instance's self.argv attribute (given at
149+ construction time) is used."""
150+
151+ if args is None:
152+ args = self.argv
153 self._create_parser()
154 self._parse_args(args)
155 self._convert_to_config()
156@@ -307,25 +353,21 @@
157 return []
158
159 def _create_parser(self):
160- self.parser = argparse.ArgumentParser(*self.args, **self.kw)
161+ self.parser = ArgumentParser(*self.args, **self.kw)
162 self._add_arguments()
163 self._add_other_arguments()
164
165- def _add_other_arguments(self):
166- pass
167-
168 def _add_arguments(self):
169 for argument in self.arguments:
170- if not argument[1].has_key('default'):
171- argument[1]['default'] = NoConfigDefault
172 self.parser.add_argument(*argument[0],**argument[1])
173
174- def _parse_args(self, args=None):
175- """self.parser->self.parsed_data"""
176- if args is None:
177- self.parsed_data, self.extra_args = self.parser.parse_known_args()
178- else:
179- self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
180+ def _add_other_arguments(self):
181+ """Meant for subclasses to add their own arguments."""
182+ pass
183+
184+ def _parse_args(self, args):
185+ """self.parser->self.parsed_data"""
186+ self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
187
188 def _convert_to_config(self):
189 """self.parsed_data->self.config"""
190@@ -333,4 +375,3 @@
191 if v is not NoConfigDefault:
192 exec_str = 'self.config.' + k + '= v'
193 exec exec_str in locals(), globals()
194-
195
196=== modified file 'IPython/config/tests/test_loader.py' (properties changed: -x to +x)
197--- IPython/config/tests/test_loader.py 2009-09-12 15:58:55 +0000
198+++ IPython/config/tests/test_loader.py 2010-01-18 01:17:11 +0000
199@@ -37,17 +37,18 @@
200
201
202 pyfile = """
203-a = 10
204-b = 20
205-Foo.Bar.value = 10
206-Foo.Bam.value = range(10)
207-D.C.value = 'hi there'
208+c = get_config()
209+c.a = 10
210+c.b = 20
211+c.Foo.Bar.value = 10
212+c.Foo.Bam.value = range(10)
213+c.D.C.value = 'hi there'
214 """
215
216 class TestPyFileCL(TestCase):
217
218 def test_basic(self):
219- fd, fname = mkstemp()
220+ fd, fname = mkstemp('.py')
221 f = os.fdopen(fd, 'w')
222 f.write(pyfile)
223 f.close()
224@@ -65,15 +66,13 @@
225
226 def test_basic(self):
227
228- class MyLoader(ArgParseConfigLoader):
229- arguments = (
230+ arguments = (
231 (('-f','--foo'), dict(dest='Global.foo', type=str)),
232 (('-b',), dict(dest='MyClass.bar', type=int)),
233 (('-n',), dict(dest='n', action='store_true')),
234 (('Global.bam',), dict(type=str))
235 )
236-
237- cl = MyLoader()
238+ cl = ArgParseConfigLoader(arguments=arguments)
239 config = cl.load_config('-f hi -b 10 -n wow'.split())
240 self.assertEquals(config.Global.foo, 'hi')
241 self.assertEquals(config.MyClass.bar, 10)
242
243=== modified file 'IPython/core/application.py'
244--- IPython/core/application.py 2009-11-12 00:41:52 +0000
245+++ IPython/core/application.py 2010-01-18 01:17:11 +0000
246@@ -1,4 +1,3 @@
247-#!/usr/bin/env python
248 # encoding: utf-8
249 """
250 An application for IPython.
251@@ -33,64 +32,125 @@
252 import os
253 import sys
254
255-from IPython.core import release
256-from IPython.utils.genutils import get_ipython_dir
257+from IPython.core import release, crashhandler
258+from IPython.utils.genutils import get_ipython_dir, get_ipython_package_dir
259 from IPython.config.loader import (
260 PyFileConfigLoader,
261 ArgParseConfigLoader,
262 Config,
263- NoConfigDefault
264 )
265
266 #-----------------------------------------------------------------------------
267 # Classes and functions
268 #-----------------------------------------------------------------------------
269
270-
271-class BaseAppArgParseConfigLoader(ArgParseConfigLoader):
272- """Default command line options for IPython based applications."""
273-
274- def _add_other_arguments(self):
275- self.parser.add_argument('--ipython-dir',
276- dest='Global.ipython_dir',type=unicode,
277- help='Set to override default location of Global.ipython_dir.',
278- default=NoConfigDefault,
279- metavar='Global.ipython_dir')
280- self.parser.add_argument('-p', '--profile',
281- dest='Global.profile',type=unicode,
282- help='The string name of the ipython profile to be used.',
283- default=NoConfigDefault,
284- metavar='Global.profile')
285- self.parser.add_argument('--log-level',
286- dest="Global.log_level",type=int,
287- help='Set the log level (0,10,20,30,40,50). Default is 30.',
288- default=NoConfigDefault,
289- metavar='Global.log_level')
290- self.parser.add_argument('--config-file',
291- dest='Global.config_file',type=unicode,
292- help='Set the config file name to override default.',
293- default=NoConfigDefault,
294- metavar='Global.config_file')
295-
296-
297 class ApplicationError(Exception):
298 pass
299
300
301+app_cl_args = (
302+ (('--ipython-dir', ), dict(
303+ dest='Global.ipython_dir',type=unicode,
304+ help=
305+ """Set to override default location of the IPython directory
306+ IPYTHON_DIR, stored as Global.ipython_dir. This can also be specified
307+ through the environment variable IPYTHON_DIR.""",
308+ metavar='Global.ipython_dir') ),
309+ (('-p', '--profile',), dict(
310+ dest='Global.profile',type=unicode,
311+ help=
312+ """The string name of the ipython profile to be used. Assume that your
313+ config file is ipython_config-<name>.py (looks in current dir first,
314+ then in IPYTHON_DIR). This is a quick way to keep and load multiple
315+ config files for different tasks, especially if include your basic one
316+ in your more specialized ones. You can keep a basic
317+ IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
318+ include this one and load extra things for particular tasks.""",
319+ metavar='Global.profile') ),
320+ (('--log-level',), dict(
321+ dest="Global.log_level",type=int,
322+ help='Set the log level (0,10,20,30,40,50). Default is 30.',
323+ metavar='Global.log_level')),
324+ (('--config-file',), dict(
325+ dest='Global.config_file',type=unicode,
326+ help=
327+ """Set the config file name to override default. Normally IPython
328+ loads ipython_config.py (from current directory) or
329+ IPYTHON_DIR/ipython_config.py. If the loading of your config file
330+ fails, IPython starts with a bare bones configuration (no modules
331+ loaded at all).""",
332+ metavar='Global.config_file')),
333+ )
334+
335 class Application(object):
336- """Load a config, construct components and set them running."""
337+ """Load a config, construct components and set them running.
338+
339+ The configuration of an application can be done via four different Config
340+ objects, which are loaded and ultimately merged into a single one used from
341+ that point on by the app. These are:
342+
343+ 1. default_config: internal defaults, implemented in code.
344+ 2. file_config: read from the filesystem.
345+ 3. command_line_config: read from the system's command line flags.
346+ 4. constructor_config: passed parametrically to the constructor.
347+
348+ During initialization, 3 is actually read before 2, since at the
349+ command-line one may override the location of the file to be read. But the
350+ above is the order in which the merge is made.
351+
352+ There is a final config object can be created and passed to the
353+ constructor: override_config. If it exists, this completely overrides the
354+ configs 2-4 above (the default is still used to ensure that all needed
355+ fields at least are created). This makes it easier to create
356+ parametrically (e.g. in testing or sphinx plugins) objects with a known
357+ configuration, that are unaffected by whatever arguments may be present in
358+ sys.argv or files in the user's various directories.
359+ """
360
361 name = u'ipython'
362 description = 'IPython: an enhanced interactive Python shell.'
363+ #: usage message printed by argparse. If None, auto-generate
364+ usage = None
365 config_file_name = u'ipython_config.py'
366+ #: Track the default and actual separately because some messages are
367+ #: only printed if we aren't using the default.
368+ default_config_file_name = config_file_name
369 default_log_level = logging.WARN
370-
371- def __init__(self):
372- self._exiting = False
373+ #: Set by --profile option
374+ profile_name = None
375+ #: User's ipython directory, typically ~/.ipython/
376+ ipython_dir = None
377+ #: internal defaults, implemented in code.
378+ default_config = None
379+ #: read from the filesystem
380+ file_config = None
381+ #: read from the system's command line flags
382+ command_line_config = None
383+ #: passed parametrically to the constructor.
384+ constructor_config = None
385+ #: final override, if given supercedes file/command/constructor configs
386+ override_config = None
387+ #: A reference to the argv to be used (typically ends up being sys.argv[1:])
388+ argv = None
389+ #: Default command line arguments. Subclasses should create a new tuple
390+ #: that *includes* these.
391+ cl_arguments = app_cl_args
392+
393+ #: extra arguments computed by the command-line loader
394+ extra_args = None
395+
396+ # Private attributes
397+ _exiting = False
398+ _initialized = False
399+
400+ # Class choices for things that will be instantiated at runtime.
401+ _CrashHandler = crashhandler.CrashHandler
402+
403+ def __init__(self, argv=None, constructor_config=None, override_config=None):
404+ self.argv = sys.argv[1:] if argv is None else argv
405+ self.constructor_config = constructor_config
406+ self.override_config = override_config
407 self.init_logger()
408- # Track the default and actual separately because some messages are
409- # only printed if we aren't using the default.
410- self.default_config_file_name = self.config_file_name
411
412 def init_logger(self):
413 self.log = logging.getLogger(self.__class__.__name__)
414@@ -109,36 +169,80 @@
415
416 log_level = property(_get_log_level, _set_log_level)
417
418- def start(self):
419- """Start the application."""
420- self.attempt(self.create_default_config)
421+ def initialize(self):
422+ """Initialize the application.
423+
424+ Loads all configuration information and sets all application state, but
425+ does not start any relevant processing (typically some kind of event
426+ loop).
427+
428+ Once this method has been called, the application is flagged as
429+ initialized and the method becomes a no-op."""
430+
431+ if self._initialized:
432+ return
433+
434+ # The first part is protected with an 'attempt' wrapper, that will log
435+ # failures with the basic system traceback machinery. Once our crash
436+ # handler is in place, we can let any subsequent exception propagate,
437+ # as our handler will log it with much better detail than the default.
438+ self.attempt(self.create_crash_handler)
439+
440+ # Configuration phase
441+ # Default config (internally hardwired in application code)
442+ self.create_default_config()
443 self.log_default_config()
444 self.set_default_config_log_level()
445- self.attempt(self.pre_load_command_line_config)
446- self.attempt(self.load_command_line_config, action='abort')
447- self.set_command_line_config_log_level()
448- self.attempt(self.post_load_command_line_config)
449- self.log_command_line_config()
450- self.attempt(self.find_ipython_dir)
451- self.attempt(self.find_resources)
452- self.attempt(self.find_config_file_name)
453- self.attempt(self.find_config_file_paths)
454- self.attempt(self.pre_load_file_config)
455- self.attempt(self.load_file_config)
456- self.set_file_config_log_level()
457- self.attempt(self.post_load_file_config)
458- self.log_file_config()
459- self.attempt(self.merge_configs)
460+
461+ if self.override_config is None:
462+ # Command-line config
463+ self.pre_load_command_line_config()
464+ self.load_command_line_config()
465+ self.set_command_line_config_log_level()
466+ self.post_load_command_line_config()
467+ self.log_command_line_config()
468+
469+ # Find resources needed for filesystem access, using information from
470+ # the above two
471+ self.find_ipython_dir()
472+ self.find_resources()
473+ self.find_config_file_name()
474+ self.find_config_file_paths()
475+
476+ if self.override_config is None:
477+ # File-based config
478+ self.pre_load_file_config()
479+ self.load_file_config()
480+ self.set_file_config_log_level()
481+ self.post_load_file_config()
482+ self.log_file_config()
483+
484+ # Merge all config objects into a single one the app can then use
485+ self.merge_configs()
486 self.log_master_config()
487- self.attempt(self.pre_construct)
488- self.attempt(self.construct)
489- self.attempt(self.post_construct)
490- self.attempt(self.start_app)
491+
492+ # Construction phase
493+ self.pre_construct()
494+ self.construct()
495+ self.post_construct()
496+
497+ # Done, flag as such and
498+ self._initialized = True
499+
500+ def start(self):
501+ """Start the application."""
502+ self.initialize()
503+ self.start_app()
504
505 #-------------------------------------------------------------------------
506 # Various stages of Application creation
507 #-------------------------------------------------------------------------
508
509+ def create_crash_handler(self):
510+ """Create a crash handler, typically setting sys.excepthook to it."""
511+ self.crash_handler = self._CrashHandler(self, self.name)
512+ sys.excepthook = self.crash_handler
513+
514 def create_default_config(self):
515 """Create defaults that can't be set elsewhere.
516
517@@ -148,9 +252,10 @@
518 we set them here. The Global section is for variables like this that
519 don't belong to a particular component.
520 """
521- self.default_config = Config()
522- self.default_config.Global.ipython_dir = get_ipython_dir()
523- self.default_config.Global.log_level = self.log_level
524+ c = Config()
525+ c.Global.ipython_dir = get_ipython_dir()
526+ c.Global.log_level = self.log_level
527+ self.default_config = c
528
529 def log_default_config(self):
530 self.log.debug('Default config loaded:')
531@@ -165,10 +270,11 @@
532
533 def create_command_line_config(self):
534 """Create and return a command line config loader."""
535- return BaseAppArgParseConfigLoader(
536- description=self.description,
537- version=release.version
538- )
539+ return ArgParseConfigLoader(self.argv, self.cl_arguments,
540+ description=self.description,
541+ version=release.version,
542+ usage=self.usage,
543+ )
544
545 def pre_load_command_line_config(self):
546 """Do actions just before loading the command line config."""
547@@ -197,10 +303,10 @@
548 def find_ipython_dir(self):
549 """Set the IPython directory.
550
551- This sets ``self.ipython_dir``, but the actual value that is passed
552- to the application is kept in either ``self.default_config`` or
553+ This sets ``self.ipython_dir``, but the actual value that is passed to
554+ the application is kept in either ``self.default_config`` or
555 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
556- ``sys.path`` so config files there can be references by other config
557+ ``sys.path`` so config files there can be referenced by other config
558 files.
559 """
560
561@@ -230,8 +336,7 @@
562 config file are set in :meth:`find_config_file_paths` and then passed
563 to the config file loader where they are resolved to an absolute path.
564
565- If a profile has been set at the command line, this will resolve
566- it.
567+ If a profile has been set at the command line, this will resolve it.
568 """
569
570 try:
571@@ -241,11 +346,12 @@
572
573 try:
574 self.profile_name = self.command_line_config.Global.profile
575+ except AttributeError:
576+ pass
577+ else:
578 name_parts = self.config_file_name.split('.')
579 name_parts.insert(1, u'_' + self.profile_name + u'.')
580 self.config_file_name = ''.join(name_parts)
581- except AttributeError:
582- pass
583
584 def find_config_file_paths(self):
585 """Set the search paths for resolving the config file.
586@@ -253,7 +359,11 @@
587 This must set ``self.config_file_paths`` to a sequence of search
588 paths to pass to the config file loader.
589 """
590- self.config_file_paths = (os.getcwd(), self.ipython_dir)
591+ # Include our own profiles directory last, so that users can still find
592+ # our shipped copies of builtin profiles even if they don't have them
593+ # in their local ipython directory.
594+ prof_dir = os.path.join(get_ipython_package_dir(), 'config', 'profile')
595+ self.config_file_paths = (os.getcwd(), self.ipython_dir, prof_dir)
596
597 def pre_load_file_config(self):
598 """Do actions before the config file is loaded."""
599@@ -266,7 +376,8 @@
600 ``CONFIG_FILE`` config variable is set to the resolved config file
601 location. If not successful, an empty config is used.
602 """
603- self.log.debug("Attempting to load config file: %s" % self.config_file_name)
604+ self.log.debug("Attempting to load config file: %s" %
605+ self.config_file_name)
606 loader = PyFileConfigLoader(self.config_file_name,
607 path=self.config_file_paths)
608 try:
609@@ -275,11 +386,11 @@
610 except IOError:
611 # Only warn if the default config file was NOT being used.
612 if not self.config_file_name==self.default_config_file_name:
613- self.log.warn("Config file not found, skipping: %s" % \
614+ self.log.warn("Config file not found, skipping: %s" %
615 self.config_file_name, exc_info=True)
616 self.file_config = Config()
617 except:
618- self.log.warn("Error loading config file: %s" % \
619+ self.log.warn("Error loading config file: %s" %
620 self.config_file_name, exc_info=True)
621 self.file_config = Config()
622
623@@ -299,16 +410,27 @@
624
625 def log_file_config(self):
626 if hasattr(self.file_config.Global, 'config_file'):
627- self.log.debug("Config file loaded: %s" % self.file_config.Global.config_file)
628+ self.log.debug("Config file loaded: %s" %
629+ self.file_config.Global.config_file)
630 self.log.debug(repr(self.file_config))
631
632 def merge_configs(self):
633 """Merge the default, command line and file config objects."""
634 config = Config()
635 config._merge(self.default_config)
636- config._merge(self.file_config)
637- config._merge(self.command_line_config)
638+ if self.override_config is None:
639+ config._merge(self.file_config)
640+ config._merge(self.command_line_config)
641+ if self.constructor_config is not None:
642+ config._merge(self.constructor_config)
643+ else:
644+ config._merge(self.override_config)
645+ # XXX fperez - propose to Brian we rename master_config to simply
646+ # config, I think this is going to be heavily used in examples and
647+ # application code and the name is shorter/easier to find/remember.
648+ # For now, just alias it...
649 self.master_config = config
650+ self.config = config
651
652 def log_master_config(self):
653 self.log.debug("Master config created:")
654@@ -358,7 +480,10 @@
655 raise
656 except:
657 if action == 'abort':
658+ self.log.critical("Aborting application: %s" % self.name,
659+ exc_info=True)
660 self.abort()
661+ raise
662 elif action == 'exit':
663 self.exit(0)
664
665
666=== modified file 'IPython/core/builtin_trap.py' (properties changed: -x to +x)
667=== modified file 'IPython/core/completer.py'
668--- IPython/core/completer.py 2009-09-12 17:45:35 +0000
669+++ IPython/core/completer.py 2010-01-18 01:17:11 +0000
670@@ -44,7 +44,6 @@
671
672 - When the original stdin is not a tty device, GNU readline is never
673 used, and this module (and the readline module) are silently inactive.
674-
675 """
676
677 #*****************************************************************************
678@@ -54,14 +53,19 @@
679 # proper procedure is to maintain its copyright as belonging to the Python
680 # Software Foundation (in addition to my own, for all new code).
681 #
682+# Copyright (C) 2008-2010 IPython Development Team
683+# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
684 # Copyright (C) 2001 Python Software Foundation, www.python.org
685-# Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
686 #
687 # Distributed under the terms of the BSD License. The full license is in
688 # the file COPYING, distributed as part of this software.
689 #
690 #*****************************************************************************
691
692+#-----------------------------------------------------------------------------
693+# Imports
694+#-----------------------------------------------------------------------------
695+
696 import __builtin__
697 import __main__
698 import glob
699@@ -73,23 +77,57 @@
700 import sys
701 import types
702
703+import IPython.utils.rlineimpl as readline
704 from IPython.core.error import TryNext
705 from IPython.core.prefilter import ESC_MAGIC
706-
707-import IPython.utils.rlineimpl as readline
708-from IPython.utils.ipstruct import Struct
709 from IPython.utils import generics
710-
711-# Python 2.4 offers sets as a builtin
712-try:
713- set()
714-except NameError:
715- from sets import Set as set
716-
717 from IPython.utils.genutils import debugx, dir2
718
719+#-----------------------------------------------------------------------------
720+# Globals
721+#-----------------------------------------------------------------------------
722+
723+# Public API
724 __all__ = ['Completer','IPCompleter']
725
726+if sys.platform == 'win32':
727+ PROTECTABLES = ' '
728+else:
729+ PROTECTABLES = ' ()'
730+
731+#-----------------------------------------------------------------------------
732+# Main functions and classes
733+#-----------------------------------------------------------------------------
734+
735+def protect_filename(s):
736+ """Escape a string to protect certain characters."""
737+
738+ return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
739+ for ch in s])
740+
741+
742+def single_dir_expand(matches):
743+ "Recursively expand match lists containing a single dir."
744+
745+ if len(matches) == 1 and os.path.isdir(matches[0]):
746+ # Takes care of links to directories also. Use '/'
747+ # explicitly, even under Windows, so that name completions
748+ # don't end up escaped.
749+ d = matches[0]
750+ if d[-1] in ['/','\\']:
751+ d = d[:-1]
752+
753+ subdirs = os.listdir(d)
754+ if subdirs:
755+ matches = [ (d + '/' + p) for p in subdirs]
756+ return single_dir_expand(matches)
757+ else:
758+ return matches
759+ else:
760+ return matches
761+
762+class Bunch: pass
763+
764 class Completer:
765 def __init__(self,namespace=None,global_namespace=None):
766 """Create a new completer for the command line.
767@@ -152,6 +190,7 @@
768 defined in self.namespace or self.global_namespace that match.
769
770 """
771+ #print 'Completer->global_matches, txt=%r' % text # dbg
772 matches = []
773 match_append = matches.append
774 n = len(text)
775@@ -179,6 +218,7 @@
776 """
777 import re
778
779+ #print 'Completer->attr_matches, txt=%r' % text # dbg
780 # Another option, seems to work great. Catches things like ''.<tab>
781 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
782
783@@ -205,6 +245,7 @@
784 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
785 return res
786
787+
788 class IPCompleter(Completer):
789 """Extension of the completer class with IPython-specific features"""
790
791@@ -235,7 +276,7 @@
792 to complete. """
793
794 Completer.__init__(self,namespace,global_namespace)
795- self.magic_prefix = shell.name+'.magic_'
796+
797 self.magic_escape = ESC_MAGIC
798 self.readline = readline
799 delims = self.readline.get_completer_delims()
800@@ -244,7 +285,8 @@
801 self.get_line_buffer = self.readline.get_line_buffer
802 self.get_endidx = self.readline.get_endidx
803 self.omit__names = omit__names
804- self.merge_completions = shell.readline_merge_completions
805+ self.merge_completions = shell.readline_merge_completions
806+ self.shell = shell.shell
807 if alias_table is None:
808 alias_table = {}
809 self.alias_table = alias_table
810@@ -263,11 +305,13 @@
811 self.clean_glob = self._clean_glob_win32
812 else:
813 self.clean_glob = self._clean_glob
814+
815+ # All active matcher routines for completion
816 self.matchers = [self.python_matches,
817 self.file_matches,
818+ self.magic_matches,
819 self.alias_matches,
820 self.python_func_kw_matches]
821-
822
823 # Code contributed by Alex Schmolck, for ipython/emacs integration
824 def all_completions(self, text):
825@@ -278,9 +322,8 @@
826 try:
827 for i in xrange(sys.maxint):
828 res = self.complete(text, i)
829-
830- if not res: break
831-
832+ if not res:
833+ break
834 comp_append(res)
835 #XXX workaround for ``notDefined.<tab>``
836 except NameError:
837@@ -316,41 +359,12 @@
838 # don't want to treat as delimiters in filename matching
839 # when escaped with backslash
840
841- if sys.platform == 'win32':
842- protectables = ' '
843- else:
844- protectables = ' ()'
845-
846 if text.startswith('!'):
847 text = text[1:]
848 text_prefix = '!'
849 else:
850 text_prefix = ''
851
852- def protect_filename(s):
853- return "".join([(ch in protectables and '\\' + ch or ch)
854- for ch in s])
855-
856- def single_dir_expand(matches):
857- "Recursively expand match lists containing a single dir."
858-
859- if len(matches) == 1 and os.path.isdir(matches[0]):
860- # Takes care of links to directories also. Use '/'
861- # explicitly, even under Windows, so that name completions
862- # don't end up escaped.
863- d = matches[0]
864- if d[-1] in ['/','\\']:
865- d = d[:-1]
866-
867- subdirs = os.listdir(d)
868- if subdirs:
869- matches = [ (d + '/' + p) for p in subdirs]
870- return single_dir_expand(matches)
871- else:
872- return matches
873- else:
874- return matches
875-
876 lbuf = self.lbuf
877 open_quotes = 0 # track strings with open quotes
878 try:
879@@ -402,13 +416,24 @@
880 #print 'mm',matches # dbg
881 return single_dir_expand(matches)
882
883+ def magic_matches(self, text):
884+ """Match magics"""
885+ #print 'Completer->magic_matches:',text,'lb',self.lbuf # dbg
886+ # Get all shell magics now rather than statically, so magics loaded at
887+ # runtime show up too
888+ magics = self.shell.lsmagic()
889+ pre = self.magic_escape
890+ baretext = text.lstrip(pre)
891+ return [ pre+m for m in magics if m.startswith(baretext)]
892+
893 def alias_matches(self, text):
894 """Match internal system aliases"""
895 #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg
896
897 # if we are not in the first 'item', alias matching
898 # doesn't make sense - unless we are starting with 'sudo' command.
899- if ' ' in self.lbuf.lstrip() and not self.lbuf.lstrip().startswith('sudo'):
900+ if ' ' in self.lbuf.lstrip() and \
901+ not self.lbuf.lstrip().startswith('sudo'):
902 return []
903 text = os.path.expanduser(text)
904 aliases = self.alias_table.keys()
905@@ -420,7 +445,7 @@
906 def python_matches(self,text):
907 """Match attributes or global python names"""
908
909- #print 'Completer->python_matches, txt=<%s>' % text # dbg
910+ #print 'Completer->python_matches, txt=%r' % text # dbg
911 if "." in text:
912 try:
913 matches = self.attr_matches(text)
914@@ -439,11 +464,7 @@
915 matches = []
916 else:
917 matches = self.global_matches(text)
918- # this is so completion finds magics when automagic is on:
919- if (matches == [] and
920- not text.startswith(os.sep) and
921- not ' ' in self.lbuf):
922- matches = self.attr_matches(self.magic_prefix+text)
923+
924 return matches
925
926 def _default_arguments(self, obj):
927@@ -514,9 +535,11 @@
928 callableMatches = self.attr_matches('.'.join(ids[::-1]))
929 argMatches = []
930 for callableMatch in callableMatches:
931- try: namedArgs = self._default_arguments(eval(callableMatch,
932+ try:
933+ namedArgs = self._default_arguments(eval(callableMatch,
934 self.namespace))
935- except: continue
936+ except:
937+ continue
938 for namedArg in namedArgs:
939 if namedArg.startswith(text):
940 argMatches.append("%s=" %namedArg)
941@@ -528,7 +551,7 @@
942 if not line.strip():
943 return None
944
945- event = Struct()
946+ event = Bunch()
947 event.line = line
948 event.symbol = text
949 cmd = line.split(None,1)[0]
950@@ -540,11 +563,9 @@
951 try_magic = self.custom_completers.s_matches(
952 self.magic_escape + cmd)
953 else:
954- try_magic = []
955-
956-
957- for c in itertools.chain(
958- self.custom_completers.s_matches(cmd),
959+ try_magic = []
960+
961+ for c in itertools.chain(self.custom_completers.s_matches(cmd),
962 try_magic,
963 self.custom_completers.flat_matches(self.lbuf)):
964 #print "try",c # dbg
965@@ -555,7 +576,8 @@
966 if withcase:
967 return withcase
968 # if none, then case insensitive ones are ok too
969- return [r for r in res if r.lower().startswith(text.lower())]
970+ text_low = text.lower()
971+ return [r for r in res if r.lower().startswith(text_low)]
972 except TryNext:
973 pass
974
975@@ -598,14 +620,11 @@
976 return None
977
978 magic_escape = self.magic_escape
979- magic_prefix = self.magic_prefix
980
981 self.lbuf = self.full_lbuf[:self.get_endidx()]
982
983 try:
984- if text.startswith(magic_escape):
985- text = text.replace(magic_escape,magic_prefix)
986- elif text.startswith('~'):
987+ if text.startswith('~'):
988 text = os.path.expanduser(text)
989 if state == 0:
990 custom_res = self.dispatch_custom_completer(text)
991@@ -625,13 +644,10 @@
992 self.matches = matcher(text)
993 if self.matches:
994 break
995- def uniq(alist):
996- set = {}
997- return [set.setdefault(e,e) for e in alist if e not in set]
998- self.matches = uniq(self.matches)
999+ self.matches = list(set(self.matches))
1000 try:
1001- ret = self.matches[state].replace(magic_prefix,magic_escape)
1002- return ret
1003+ #print "MATCH: %r" % self.matches[state] # dbg
1004+ return self.matches[state]
1005 except IndexError:
1006 return None
1007 except:
1008
1009=== modified file 'IPython/core/crashhandler.py'
1010--- IPython/core/crashhandler.py 2009-11-09 01:49:24 +0000
1011+++ IPython/core/crashhandler.py 2010-01-18 01:17:11 +0000
1012@@ -26,12 +26,13 @@
1013 # Our own
1014 from IPython.core import release
1015 from IPython.core import ultratb
1016+from IPython.utils.genutils import sys_info
1017+
1018 from IPython.external.Itpl import itpl
1019
1020-from IPython.utils.genutils import *
1021-
1022 #****************************************************************************
1023-class CrashHandler:
1024+
1025+class CrashHandler(object):
1026 """Customizable crash handlers for IPython-based systems.
1027
1028 Instances of this class provide a __call__ method which can be used as a
1029@@ -41,15 +42,15 @@
1030
1031 """
1032
1033- def __init__(self,IP,app_name,contact_name,contact_email,
1034- bug_tracker,crash_report_fname,
1035- show_crash_traceback=True):
1036+ def __init__(self,app, app_name, contact_name=None, contact_email=None,
1037+ bug_tracker=None, crash_report_fname='CrashReport.txt',
1038+ show_crash_traceback=True, call_pdb=False):
1039 """New crash handler.
1040
1041 Inputs:
1042
1043- - IP: a running IPython instance, which will be queried at crash time
1044- for internal information.
1045+ - app: a running application instance, which will be queried at crash
1046+ time for internal information.
1047
1048 - app_name: a string containing the name of your application.
1049
1050@@ -77,13 +78,16 @@
1051 """
1052
1053 # apply args into instance
1054- self.IP = IP # IPython instance
1055+ self.app = app
1056 self.app_name = app_name
1057 self.contact_name = contact_name
1058 self.contact_email = contact_email
1059 self.bug_tracker = bug_tracker
1060 self.crash_report_fname = crash_report_fname
1061 self.show_crash_traceback = show_crash_traceback
1062+ self.section_sep = '\n\n'+'*'*75+'\n\n'
1063+ self.call_pdb = call_pdb
1064+ #self.call_pdb = True # dbg
1065
1066 # Hardcoded defaults, which can be overridden either by subclasses or
1067 # at runtime for the instance.
1068@@ -124,7 +128,7 @@
1069 #color_scheme = 'Linux' # dbg
1070
1071 try:
1072- rptdir = self.IP.ipython_dir
1073+ rptdir = self.app.ipython_dir
1074 except:
1075 rptdir = os.getcwd()
1076 if not os.path.isdir(rptdir):
1077@@ -134,8 +138,14 @@
1078 # properly expanded out in the user message template
1079 self.crash_report_fname = report_name
1080 TBhandler = ultratb.VerboseTB(color_scheme=color_scheme,
1081- long_header=1)
1082- traceback = TBhandler.text(etype,evalue,etb,context=31)
1083+ long_header=1,
1084+ call_pdb=self.call_pdb,
1085+ )
1086+ if self.call_pdb:
1087+ TBhandler(etype,evalue,etb)
1088+ return
1089+ else:
1090+ traceback = TBhandler.text(etype,evalue,etb,context=31)
1091
1092 # print traceback to screen
1093 if self.show_crash_traceback:
1094@@ -155,74 +165,58 @@
1095 # Construct report on disk
1096 report.write(self.make_report(traceback))
1097 report.close()
1098- raw_input("Press enter to exit:")
1099+ raw_input("Hit <Enter> to quit this message (your terminal may close):")
1100
1101 def make_report(self,traceback):
1102 """Return a string containing a crash report."""
1103-
1104- sec_sep = '\n\n'+'*'*75+'\n\n'
1105-
1106- report = []
1107+
1108+ sec_sep = self.section_sep
1109+
1110+ report = ['*'*75+'\n\n'+'IPython post-mortem report\n\n']
1111 rpt_add = report.append
1112+ rpt_add(sys_info())
1113
1114- rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
1115- rpt_add('IPython version: %s \n\n' % release.version)
1116- rpt_add('BZR revision : %s \n\n' % release.revision)
1117- rpt_add('Platform info : os.name -> %s, sys.platform -> %s' %
1118- (os.name,sys.platform) )
1119- rpt_add(sec_sep+'Current user configuration structure:\n\n')
1120- rpt_add(pformat(self.IP.dict()))
1121- rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
1122 try:
1123- rpt_add(sec_sep+"History of session input:")
1124- for line in self.IP.user_ns['_ih']:
1125- rpt_add(line)
1126- rpt_add('\n*** Last line of input (may not be in above history):\n')
1127- rpt_add(self.IP._last_input_line+'\n')
1128+ config = pformat(self.app.config)
1129+ rpt_add(sec_sep+'Current user configuration structure:\n\n')
1130+ rpt_add(config)
1131 except:
1132 pass
1133+ rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
1134
1135 return ''.join(report)
1136
1137+
1138 class IPythonCrashHandler(CrashHandler):
1139 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
1140
1141- def __init__(self,IP):
1142+ def __init__(self, app, app_name='IPython'):
1143
1144 # Set here which of the IPython authors should be listed as contact
1145 AUTHOR_CONTACT = 'Fernando'
1146
1147 # Set argument defaults
1148- app_name = 'IPython'
1149 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
1150 contact_name,contact_email = release.authors[AUTHOR_CONTACT][:2]
1151 crash_report_fname = 'IPython_crash_report.txt'
1152 # Call parent constructor
1153- CrashHandler.__init__(self,IP,app_name,contact_name,contact_email,
1154+ CrashHandler.__init__(self,app,app_name,contact_name,contact_email,
1155 bug_tracker,crash_report_fname)
1156
1157 def make_report(self,traceback):
1158 """Return a string containing a crash report."""
1159
1160- sec_sep = '\n\n'+'*'*75+'\n\n'
1161-
1162- report = []
1163+ sec_sep = self.section_sep
1164+ # Start with parent report
1165+ report = [super(IPythonCrashHandler, self).make_report(traceback)]
1166+ # Add interactive-specific info we may have
1167 rpt_add = report.append
1168-
1169- rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
1170- rpt_add('IPython version: %s \n\n' % release.version)
1171- rpt_add('BZR revision : %s \n\n' % release.revision)
1172- rpt_add('Platform info : os.name -> %s, sys.platform -> %s' %
1173- (os.name,sys.platform) )
1174- rpt_add(sec_sep+'Current user configuration structure:\n\n')
1175- # rpt_add(pformat(self.IP.dict()))
1176- rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
1177 try:
1178 rpt_add(sec_sep+"History of session input:")
1179- for line in self.IP.user_ns['_ih']:
1180+ for line in self.app.shell.user_ns['_ih']:
1181 rpt_add(line)
1182 rpt_add('\n*** Last line of input (may not be in above history):\n')
1183- rpt_add(self.IP._last_input_line+'\n')
1184+ rpt_add(self.app.shell._last_input_line+'\n')
1185 except:
1186 pass
1187
1188
1189=== modified file 'IPython/core/debugger.py'
1190--- IPython/core/debugger.py 2009-10-17 18:14:42 +0000
1191+++ IPython/core/debugger.py 2010-01-18 01:17:11 +0000
1192@@ -477,3 +477,36 @@
1193 namespaces = [('Locals', self.curframe.f_locals),
1194 ('Globals', self.curframe.f_globals)]
1195 self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)
1196+
1197+ def checkline(self, filename, lineno):
1198+ """Check whether specified line seems to be executable.
1199+
1200+ Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
1201+ line or EOF). Warning: testing is not comprehensive.
1202+ """
1203+ #######################################################################
1204+ # XXX Hack! Use python-2.5 compatible code for this call, because with
1205+ # all of our changes, we've drifted from the pdb api in 2.6. For now,
1206+ # changing:
1207+ #
1208+ #line = linecache.getline(filename, lineno, self.curframe.f_globals)
1209+ # to:
1210+ #
1211+ line = linecache.getline(filename, lineno)
1212+ #
1213+ # does the trick. But in reality, we need to fix this by reconciling
1214+ # our updates with the new Pdb APIs in Python 2.6.
1215+ #
1216+ # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
1217+ #######################################################################
1218+
1219+ if not line:
1220+ print >>self.stdout, 'End of file'
1221+ return 0
1222+ line = line.strip()
1223+ # Don't allow setting breakpoint at a blank line
1224+ if (not line or (line[0] == '#') or
1225+ (line[:3] == '"""') or line[:3] == "'''"):
1226+ print >>self.stdout, '*** Blank or comment'
1227+ return 0
1228+ return lineno
1229
1230=== modified file 'IPython/core/history.py'
1231--- IPython/core/history.py 2009-08-19 21:56:41 +0000
1232+++ IPython/core/history.py 2010-01-18 01:17:11 +0000
1233@@ -14,20 +14,25 @@
1234 %history -> print at most 40 inputs (some may be multi-line)\\
1235 %history n -> print at most n inputs\\
1236 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
1237-
1238- Each input's number <n> is shown, and is accessible as the
1239- automatically generated variable _i<n>. Multi-line statements are
1240- printed starting at a new line for easy copy/paste.
1241-
1242+
1243+ By default, input history is printed without line numbers so it can be
1244+ directly pasted into an editor.
1245+
1246+ With -n, each input's number <n> is shown, and is accessible as the
1247+ automatically generated variable _i<n> as well as In[<n>]. Multi-line
1248+ statements are printed starting at a new line for easy copy/paste.
1249
1250 Options:
1251
1252- -n: do NOT print line numbers. This is useful if you want to get a
1253- printout of many lines which can be directly pasted into a text
1254- editor.
1255-
1256+ -n: print line numbers for each input.
1257 This feature is only available if numbered prompts are in use.
1258
1259+ -o: also print outputs for each input.
1260+
1261+ -p: print classic '>>>' python prompts before each input. This is useful
1262+ for making documentation, and in conjunction with -o, for producing
1263+ doctest-ready output.
1264+
1265 -t: (default) print the 'translated' history, as IPython understands it.
1266 IPython filters your input and converts it all into valid Python source
1267 before executing it (things like magics or aliases are turned into
1268@@ -50,7 +55,7 @@
1269 if not self.outputcache.do_full_cache:
1270 print 'This feature is only available if numbered prompts are in use.'
1271 return
1272- opts,args = self.parse_options(parameter_s,'gntsrf:',mode='list')
1273+ opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
1274
1275 # Check if output to specific file was requested.
1276 try:
1277@@ -80,39 +85,43 @@
1278 if 'g' in opts:
1279 init = 1
1280 final = len(input_hist)
1281- parts = parameter_s.split(None,1)
1282+ parts = parameter_s.split(None, 1)
1283 if len(parts) == 1:
1284 parts += '*'
1285 head, pattern = parts
1286 pattern = "*" + pattern + "*"
1287 elif len(args) == 0:
1288- final = len(input_hist)
1289+ final = len(input_hist)-1
1290 init = max(1,final-default_length)
1291 elif len(args) == 1:
1292 final = len(input_hist)
1293- init = max(1,final-int(args[0]))
1294+ init = max(1, final-int(args[0]))
1295 elif len(args) == 2:
1296- init,final = map(int,args)
1297+ init, final = map(int, args)
1298 else:
1299 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
1300- print self.magic_hist.__doc__
1301+ print >> Term.cout, self.magic_hist.__doc__
1302 return
1303+
1304 width = len(str(final))
1305 line_sep = ['','\n']
1306- print_nums = not opts.has_key('n')
1307+ print_nums = 'n' in opts
1308+ print_outputs = 'o' in opts
1309+ pyprompts = 'p' in opts
1310
1311 found = False
1312 if pattern is not None:
1313 sh = self.shadowhist.all()
1314 for idx, s in sh:
1315 if fnmatch.fnmatch(s, pattern):
1316- print "0%d: %s" %(idx, s)
1317+ print >> outfile, "0%d: %s" %(idx, s)
1318 found = True
1319
1320 if found:
1321- print "==="
1322- print "shadow history ends, fetch by %rep <number> (must start with 0)"
1323- print "=== start of normal history ==="
1324+ print >> outfile, "==="
1325+ print >> outfile, \
1326+ "shadow history ends, fetch by %rep <number> (must start with 0)"
1327+ print >> outfile, "=== start of normal history ==="
1328
1329 for in_num in range(init,final):
1330 inline = input_hist[in_num]
1331@@ -122,8 +131,21 @@
1332 multiline = int(inline.count('\n') > 1)
1333 if print_nums:
1334 print >> outfile, \
1335- '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
1336- print >> outfile, inline,
1337+ '%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
1338+ if pyprompts:
1339+ print >> outfile, '>>>',
1340+ if multiline:
1341+ lines = inline.splitlines()
1342+ print >> outfile, '\n... '.join(lines)
1343+ print >> outfile, '... '
1344+ else:
1345+ print >> outfile, inline,
1346+ else:
1347+ print >> outfile, inline,
1348+ if print_outputs:
1349+ output = self.shell.user_ns['Out'].get(in_num)
1350+ if output is not None:
1351+ print >> outfile, repr(output)
1352
1353 if close_at_end:
1354 outfile.close()
1355@@ -245,10 +267,10 @@
1356
1357
1358 def init_ipython(ip):
1359- import ipy_completers
1360-
1361 ip.define_magic("rep",rep_f)
1362 ip.define_magic("hist",magic_hist)
1363 ip.define_magic("history",magic_history)
1364
1365- ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
1366+ # XXX - ipy_completers are in quarantine, need to be updated to new apis
1367+ #import ipy_completers
1368+ #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
1369
1370=== modified file 'IPython/core/hooks.py'
1371--- IPython/core/hooks.py 2009-08-26 20:54:07 +0000
1372+++ IPython/core/hooks.py 2010-01-18 01:17:11 +0000
1373@@ -137,8 +137,7 @@
1374 for prio,cmd in self.chain:
1375 #print "prio",prio,"cmd",cmd #dbg
1376 try:
1377- ret = cmd(*args, **kw)
1378- return ret
1379+ return cmd(*args, **kw)
1380 except TryNext, exc:
1381 if exc.args or exc.kwargs:
1382 args = exc.args
1383
1384=== modified file 'IPython/core/ipapp.py' (properties changed: -x to +x)
1385--- IPython/core/ipapp.py 2009-11-12 00:41:52 +0000
1386+++ IPython/core/ipapp.py 2010-01-18 01:17:11 +0000
1387@@ -4,17 +4,15 @@
1388 The :class:`~IPython.core.application.Application` object for the command
1389 line :command:`ipython` program.
1390
1391-Authors:
1392+Authors
1393+-------
1394
1395 * Brian Granger
1396 * Fernando Perez
1397-
1398-Notes
1399------
1400 """
1401
1402 #-----------------------------------------------------------------------------
1403-# Copyright (C) 2008-2009 The IPython Development Team
1404+# Copyright (C) 2008-2010 The IPython Development Team
1405 #
1406 # Distributed under the terms of the BSD License. The full license is in
1407 # the file COPYING, distributed as part of this software.
1408@@ -23,318 +21,405 @@
1409 #-----------------------------------------------------------------------------
1410 # Imports
1411 #-----------------------------------------------------------------------------
1412+from __future__ import absolute_import
1413
1414 import logging
1415 import os
1416 import sys
1417-import warnings
1418
1419-from IPython.core.application import Application, BaseAppArgParseConfigLoader
1420-from IPython.core import release
1421+from IPython.core import crashhandler
1422+from IPython.core.application import Application
1423 from IPython.core.iplib import InteractiveShell
1424 from IPython.config.loader import (
1425- NoConfigDefault,
1426 Config,
1427- PyFileConfigLoader
1428+ PyFileConfigLoader,
1429+# NoConfigDefault,
1430 )
1431-
1432 from IPython.lib import inputhook
1433-
1434 from IPython.utils.genutils import filefind, get_ipython_dir
1435-
1436-#-----------------------------------------------------------------------------
1437-# Utilities and helpers
1438-#-----------------------------------------------------------------------------
1439-
1440-
1441-ipython_desc = """
1442-A Python shell with automatic history (input and output), dynamic object
1443-introspection, easier configuration, command completion, access to the system
1444-shell and more.
1445-"""
1446-
1447-def pylab_warning():
1448- msg = """
1449-
1450-IPython's -pylab mode has been disabled until matplotlib supports this version
1451-of IPython. This version of IPython has greatly improved GUI integration that
1452-matplotlib will soon be able to take advantage of. This will eventually
1453-result in greater stability and a richer API for matplotlib under IPython.
1454-However during this transition, you will either need to use an older version
1455-of IPython, or do the following to use matplotlib interactively::
1456-
1457- import matplotlib
1458- matplotlib.interactive(True)
1459- matplotlib.use('wxagg') # adjust for your backend
1460- %gui -a wx # adjust for your GUI
1461- from matplotlib import pyplot as plt
1462-
1463-See the %gui magic for information on the new interface.
1464-"""
1465- warnings.warn(msg, category=DeprecationWarning, stacklevel=1)
1466-
1467-
1468-#-----------------------------------------------------------------------------
1469-# Main classes and functions
1470-#-----------------------------------------------------------------------------
1471+from . import usage
1472+
1473+#-----------------------------------------------------------------------------
1474+# Globals, utilities and helpers
1475+#-----------------------------------------------------------------------------
1476+
1477+default_config_file_name = u'ipython_config.py'
1478
1479 cl_args = (
1480 (('--autocall',), dict(
1481- type=int, dest='InteractiveShell.autocall', default=NoConfigDefault,
1482- help='Set the autocall value (0,1,2).',
1483+ type=int, dest='InteractiveShell.autocall',
1484+ help=
1485+ """Make IPython automatically call any callable object even if you
1486+ didn't type explicit parentheses. For example, 'str 43' becomes
1487+ 'str(43)' automatically. The value can be '0' to disable the feature,
1488+ '1' for 'smart' autocall, where it is not applied if there are no more
1489+ arguments on the line, and '2' for 'full' autocall, where all callable
1490+ objects are automatically called (even if no arguments are present).
1491+ The default is '1'.""",
1492 metavar='InteractiveShell.autocall')
1493 ),
1494 (('--autoindent',), dict(
1495- action='store_true', dest='InteractiveShell.autoindent', default=NoConfigDefault,
1496+ action='store_true', dest='InteractiveShell.autoindent',
1497 help='Turn on autoindenting.')
1498 ),
1499 (('--no-autoindent',), dict(
1500- action='store_false', dest='InteractiveShell.autoindent', default=NoConfigDefault,
1501+ action='store_false', dest='InteractiveShell.autoindent',
1502 help='Turn off autoindenting.')
1503 ),
1504 (('--automagic',), dict(
1505- action='store_true', dest='InteractiveShell.automagic', default=NoConfigDefault,
1506- help='Turn on the auto calling of magic commands.')
1507- ),
1508+ action='store_true', dest='InteractiveShell.automagic',
1509+ help='Turn on the auto calling of magic commands.'
1510+ 'Type %%magic at the IPython prompt for more information.')
1511+ ),
1512 (('--no-automagic',), dict(
1513- action='store_false', dest='InteractiveShell.automagic', default=NoConfigDefault,
1514+ action='store_false', dest='InteractiveShell.automagic',
1515 help='Turn off the auto calling of magic commands.')
1516 ),
1517 (('--autoedit-syntax',), dict(
1518- action='store_true', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
1519+ action='store_true', dest='InteractiveShell.autoedit_syntax',
1520 help='Turn on auto editing of files with syntax errors.')
1521 ),
1522 (('--no-autoedit-syntax',), dict(
1523- action='store_false', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
1524+ action='store_false', dest='InteractiveShell.autoedit_syntax',
1525 help='Turn off auto editing of files with syntax errors.')
1526 ),
1527 (('--banner',), dict(
1528- action='store_true', dest='Global.display_banner', default=NoConfigDefault,
1529+ action='store_true', dest='Global.display_banner',
1530 help='Display a banner upon starting IPython.')
1531 ),
1532 (('--no-banner',), dict(
1533- action='store_false', dest='Global.display_banner', default=NoConfigDefault,
1534+ action='store_false', dest='Global.display_banner',
1535 help="Don't display a banner upon starting IPython.")
1536 ),
1537 (('--cache-size',), dict(
1538- type=int, dest='InteractiveShell.cache_size', default=NoConfigDefault,
1539- help="Set the size of the output cache.",
1540+ type=int, dest='InteractiveShell.cache_size',
1541+ help=
1542+ """Set the size of the output cache. The default is 1000, you can
1543+ change it permanently in your config file. Setting it to 0 completely
1544+ disables the caching system, and the minimum value accepted is 20 (if
1545+ you provide a value less than 20, it is reset to 0 and a warning is
1546+ issued). This limit is defined because otherwise you'll spend more
1547+ time re-flushing a too small cache than working.
1548+ """,
1549 metavar='InteractiveShell.cache_size')
1550 ),
1551 (('--classic',), dict(
1552- action='store_true', dest='Global.classic', default=NoConfigDefault,
1553+ action='store_true', dest='Global.classic',
1554 help="Gives IPython a similar feel to the classic Python prompt.")
1555 ),
1556 (('--colors',), dict(
1557- type=str, dest='InteractiveShell.colors', default=NoConfigDefault,
1558+ type=str, dest='InteractiveShell.colors',
1559 help="Set the color scheme (NoColor, Linux, and LightBG).",
1560 metavar='InteractiveShell.colors')
1561 ),
1562 (('--color-info',), dict(
1563- action='store_true', dest='InteractiveShell.color_info', default=NoConfigDefault,
1564- help="Enable using colors for info related things.")
1565+ action='store_true', dest='InteractiveShell.color_info',
1566+ help=
1567+ """IPython can display information about objects via a set of func-
1568+ tions, and optionally can use colors for this, syntax highlighting
1569+ source code and various other elements. However, because this
1570+ information is passed through a pager (like 'less') and many pagers get
1571+ confused with color codes, this option is off by default. You can test
1572+ it and turn it on permanently in your ipython_config.py file if it
1573+ works for you. Test it and turn it on permanently if it works with
1574+ your system. The magic function %%color_info allows you to toggle this
1575+ inter- actively for testing."""
1576+ )
1577 ),
1578 (('--no-color-info',), dict(
1579- action='store_false', dest='InteractiveShell.color_info', default=NoConfigDefault,
1580+ action='store_false', dest='InteractiveShell.color_info',
1581 help="Disable using colors for info related things.")
1582 ),
1583 (('--confirm-exit',), dict(
1584- action='store_true', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
1585- help="Prompt the user when existing.")
1586+ action='store_true', dest='InteractiveShell.confirm_exit',
1587+ help=
1588+ """Set to confirm when you try to exit IPython with an EOF (Control-D
1589+ in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
1590+ '%%Exit', you can force a direct exit without any confirmation.
1591+ """
1592+ )
1593 ),
1594 (('--no-confirm-exit',), dict(
1595- action='store_false', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
1596- help="Don't prompt the user when existing.")
1597+ action='store_false', dest='InteractiveShell.confirm_exit',
1598+ help="Don't prompt the user when exiting.")
1599 ),
1600 (('--deep-reload',), dict(
1601- action='store_true', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
1602- help="Enable deep (recursive) reloading by default.")
1603+ action='store_true', dest='InteractiveShell.deep_reload',
1604+ help=
1605+ """Enable deep (recursive) reloading by default. IPython can use the
1606+ deep_reload module which reloads changes in modules recursively (it
1607+ replaces the reload() function, so you don't need to change anything to
1608+ use it). deep_reload() forces a full reload of modules whose code may
1609+ have changed, which the default reload() function does not. When
1610+ deep_reload is off, IPython will use the normal reload(), but
1611+ deep_reload will still be available as dreload(). This fea- ture is off
1612+ by default [which means that you have both normal reload() and
1613+ dreload()].""")
1614 ),
1615 (('--no-deep-reload',), dict(
1616- action='store_false', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
1617+ action='store_false', dest='InteractiveShell.deep_reload',
1618 help="Disable deep (recursive) reloading by default.")
1619 ),
1620 (('--editor',), dict(
1621- type=str, dest='InteractiveShell.editor', default=NoConfigDefault,
1622+ type=str, dest='InteractiveShell.editor',
1623 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
1624 metavar='InteractiveShell.editor')
1625 ),
1626 (('--log','-l'), dict(
1627- action='store_true', dest='InteractiveShell.logstart', default=NoConfigDefault,
1628- help="Start logging to the default file (./ipython_log.py).")
1629+ action='store_true', dest='InteractiveShell.logstart',
1630+ help="Start logging to the default log file (./ipython_log.py).")
1631 ),
1632 (('--logfile','-lf'), dict(
1633- type=unicode, dest='InteractiveShell.logfile', default=NoConfigDefault,
1634- help="Start logging to logfile.",
1635+ type=unicode, dest='InteractiveShell.logfile',
1636+ help="Start logging to logfile with this name.",
1637 metavar='InteractiveShell.logfile')
1638 ),
1639 (('--log-append','-la'), dict(
1640- type=unicode, dest='InteractiveShell.logappend', default=NoConfigDefault,
1641- help="Start logging to the give file in append mode.",
1642+ type=unicode, dest='InteractiveShell.logappend',
1643+ help="Start logging to the given file in append mode.",
1644 metavar='InteractiveShell.logfile')
1645 ),
1646 (('--pdb',), dict(
1647- action='store_true', dest='InteractiveShell.pdb', default=NoConfigDefault,
1648+ action='store_true', dest='InteractiveShell.pdb',
1649 help="Enable auto calling the pdb debugger after every exception.")
1650 ),
1651 (('--no-pdb',), dict(
1652- action='store_false', dest='InteractiveShell.pdb', default=NoConfigDefault,
1653+ action='store_false', dest='InteractiveShell.pdb',
1654 help="Disable auto calling the pdb debugger after every exception.")
1655 ),
1656 (('--pprint',), dict(
1657- action='store_true', dest='InteractiveShell.pprint', default=NoConfigDefault,
1658+ action='store_true', dest='InteractiveShell.pprint',
1659 help="Enable auto pretty printing of results.")
1660 ),
1661 (('--no-pprint',), dict(
1662- action='store_false', dest='InteractiveShell.pprint', default=NoConfigDefault,
1663+ action='store_false', dest='InteractiveShell.pprint',
1664 help="Disable auto auto pretty printing of results.")
1665 ),
1666 (('--prompt-in1','-pi1'), dict(
1667- type=str, dest='InteractiveShell.prompt_in1', default=NoConfigDefault,
1668- help="Set the main input prompt ('In [\#]: ')",
1669+ type=str, dest='InteractiveShell.prompt_in1',
1670+ help=
1671+ """Set the main input prompt ('In [\#]: '). Note that if you are using
1672+ numbered prompts, the number is represented with a '\#' in the string.
1673+ Don't forget to quote strings with spaces embedded in them. Most
1674+ bash-like escapes can be used to customize IPython's prompts, as well
1675+ as a few additional ones which are IPython-spe- cific. All valid
1676+ prompt escapes are described in detail in the Customization section of
1677+ the IPython manual.""",
1678 metavar='InteractiveShell.prompt_in1')
1679 ),
1680 (('--prompt-in2','-pi2'), dict(
1681- type=str, dest='InteractiveShell.prompt_in2', default=NoConfigDefault,
1682- help="Set the secondary input prompt (' .\D.: ')",
1683+ type=str, dest='InteractiveShell.prompt_in2',
1684+ help=
1685+ """Set the secondary input prompt (' .\D.: '). Similar to the previous
1686+ option, but used for the continuation prompts. The special sequence
1687+ '\D' is similar to '\#', but with all digits replaced by dots (so you
1688+ can have your continuation prompt aligned with your input prompt).
1689+ Default: ' .\D.: ' (note three spaces at the start for alignment with
1690+ 'In [\#]')""",
1691 metavar='InteractiveShell.prompt_in2')
1692 ),
1693 (('--prompt-out','-po'), dict(
1694- type=str, dest='InteractiveShell.prompt_out', default=NoConfigDefault,
1695+ type=str, dest='InteractiveShell.prompt_out',
1696 help="Set the output prompt ('Out[\#]:')",
1697 metavar='InteractiveShell.prompt_out')
1698 ),
1699 (('--quick',), dict(
1700- action='store_true', dest='Global.quick', default=NoConfigDefault,
1701+ action='store_true', dest='Global.quick',
1702 help="Enable quick startup with no config files.")
1703 ),
1704 (('--readline',), dict(
1705- action='store_true', dest='InteractiveShell.readline_use', default=NoConfigDefault,
1706+ action='store_true', dest='InteractiveShell.readline_use',
1707 help="Enable readline for command line usage.")
1708 ),
1709 (('--no-readline',), dict(
1710- action='store_false', dest='InteractiveShell.readline_use', default=NoConfigDefault,
1711+ action='store_false', dest='InteractiveShell.readline_use',
1712 help="Disable readline for command line usage.")
1713 ),
1714 (('--screen-length','-sl'), dict(
1715- type=int, dest='InteractiveShell.screen_length', default=NoConfigDefault,
1716- help='Number of lines on screen, used to control printing of long strings.',
1717+ type=int, dest='InteractiveShell.screen_length',
1718+ help=
1719+ """Number of lines of your screen, used to control printing of very
1720+ long strings. Strings longer than this number of lines will be sent
1721+ through a pager instead of directly printed. The default value for
1722+ this is 0, which means IPython will auto-detect your screen size every
1723+ time it needs to print certain potentially long strings (this doesn't
1724+ change the behavior of the 'print' keyword, it's only triggered
1725+ internally). If for some reason this isn't working well (it needs
1726+ curses support), specify it yourself. Otherwise don't change the
1727+ default.""",
1728 metavar='InteractiveShell.screen_length')
1729 ),
1730 (('--separate-in','-si'), dict(
1731- type=str, dest='InteractiveShell.separate_in', default=NoConfigDefault,
1732- help="Separator before input prompts. Default '\n'.",
1733+ type=str, dest='InteractiveShell.separate_in',
1734+ help="Separator before input prompts. Default '\\n'.",
1735 metavar='InteractiveShell.separate_in')
1736 ),
1737 (('--separate-out','-so'), dict(
1738- type=str, dest='InteractiveShell.separate_out', default=NoConfigDefault,
1739+ type=str, dest='InteractiveShell.separate_out',
1740 help="Separator before output prompts. Default 0 (nothing).",
1741 metavar='InteractiveShell.separate_out')
1742 ),
1743 (('--separate-out2','-so2'), dict(
1744- type=str, dest='InteractiveShell.separate_out2', default=NoConfigDefault,
1745+ type=str, dest='InteractiveShell.separate_out2',
1746 help="Separator after output prompts. Default 0 (nonight).",
1747 metavar='InteractiveShell.separate_out2')
1748 ),
1749 (('-no-sep',), dict(
1750- action='store_true', dest='Global.nosep', default=NoConfigDefault,
1751+ action='store_true', dest='Global.nosep',
1752 help="Eliminate all spacing between prompts.")
1753 ),
1754 (('--term-title',), dict(
1755- action='store_true', dest='InteractiveShell.term_title', default=NoConfigDefault,
1756+ action='store_true', dest='InteractiveShell.term_title',
1757 help="Enable auto setting the terminal title.")
1758 ),
1759 (('--no-term-title',), dict(
1760- action='store_false', dest='InteractiveShell.term_title', default=NoConfigDefault,
1761+ action='store_false', dest='InteractiveShell.term_title',
1762 help="Disable auto setting the terminal title.")
1763 ),
1764 (('--xmode',), dict(
1765- type=str, dest='InteractiveShell.xmode', default=NoConfigDefault,
1766- help="Exception mode ('Plain','Context','Verbose')",
1767+ type=str, dest='InteractiveShell.xmode',
1768+ help=
1769+ """Exception reporting mode ('Plain','Context','Verbose'). Plain:
1770+ similar to python's normal traceback printing. Context: prints 5 lines
1771+ of context source code around each line in the traceback. Verbose:
1772+ similar to Context, but additionally prints the variables currently
1773+ visible where the exception happened (shortening their strings if too
1774+ long). This can potentially be very slow, if you happen to have a huge
1775+ data structure whose string representation is complex to compute.
1776+ Your computer may appear to freeze for a while with cpu usage at 100%%.
1777+ If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
1778+ it more than once).
1779+ """,
1780 metavar='InteractiveShell.xmode')
1781 ),
1782 (('--ext',), dict(
1783- type=str, dest='Global.extra_extension', default=NoConfigDefault,
1784+ type=str, dest='Global.extra_extension',
1785 help="The dotted module name of an IPython extension to load.",
1786 metavar='Global.extra_extension')
1787 ),
1788 (('-c',), dict(
1789- type=str, dest='Global.code_to_run', default=NoConfigDefault,
1790+ type=str, dest='Global.code_to_run',
1791 help="Execute the given command string.",
1792 metavar='Global.code_to_run')
1793 ),
1794 (('-i',), dict(
1795- action='store_true', dest='Global.force_interact', default=NoConfigDefault,
1796- help="If running code from the command line, become interactive afterwards.")
1797- ),
1798- (('--wthread',), dict(
1799- action='store_true', dest='Global.wthread', default=NoConfigDefault,
1800- help="Enable wxPython event loop integration.")
1801- ),
1802- (('--q4thread','--qthread'), dict(
1803- action='store_true', dest='Global.q4thread', default=NoConfigDefault,
1804- help="Enable Qt4 event loop integration. Qt3 is no longer supported.")
1805- ),
1806- (('--gthread',), dict(
1807- action='store_true', dest='Global.gthread', default=NoConfigDefault,
1808- help="Enable GTK event loop integration.")
1809- ),
1810- # # These are only here to get the proper deprecation warnings
1811- (('--pylab',), dict(
1812- action='store_true', dest='Global.pylab', default=NoConfigDefault,
1813- help="Disabled. Pylab has been disabled until matplotlib "
1814- "supports this version of IPython.")
1815- )
1816+ action='store_true', dest='Global.force_interact',
1817+ help=
1818+ "If running code from the command line, become interactive afterwards."
1819+ )
1820+ ),
1821+
1822+ # Options to start with GUI control enabled from the beginning
1823+ (('--gui',), dict(
1824+ type=str, dest='Global.gui',
1825+ help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
1826+ metavar='gui-mode')
1827+ ),
1828+
1829+ (('--pylab','-pylab'), dict(
1830+ type=str, dest='Global.pylab',
1831+ nargs='?', const='auto', metavar='gui-mode',
1832+ help="Pre-load matplotlib and numpy for interactive use. "+
1833+ "If no value is given, the gui backend is matplotlib's, else use "+
1834+ "one of: ['tk', 'qt', 'wx', 'gtk'].")
1835+ ),
1836+
1837+ # Legacy GUI options. Leave them in for backwards compatibility, but the
1838+ # 'thread' names are really a misnomer now.
1839+ (('--wthread','-wthread'), dict(
1840+ action='store_true', dest='Global.wthread',
1841+ help="Enable wxPython event loop integration "+
1842+ "(DEPRECATED, use --gui wx)")
1843+ ),
1844+ (('--q4thread','--qthread','-q4thread','-qthread'), dict(
1845+ action='store_true', dest='Global.q4thread',
1846+ help="Enable Qt4 event loop integration. Qt3 is no longer supported. "+
1847+ "(DEPRECATED, use --gui qt)")
1848+ ),
1849+ (('--gthread','-gthread'), dict(
1850+ action='store_true', dest='Global.gthread',
1851+ help="Enable GTK event loop integration. "+
1852+ "(DEPRECATED, use --gui gtk)")
1853+ ),
1854 )
1855
1856-
1857-class IPythonAppCLConfigLoader(BaseAppArgParseConfigLoader):
1858-
1859- arguments = cl_args
1860-
1861-
1862-default_config_file_name = u'ipython_config.py'
1863-
1864+#-----------------------------------------------------------------------------
1865+# Main classes and functions
1866+#-----------------------------------------------------------------------------
1867
1868 class IPythonApp(Application):
1869 name = u'ipython'
1870- description = 'IPython: an enhanced interactive Python shell.'
1871+ #: argparse formats better the 'usage' than the 'description' field
1872+ description = None
1873+ #: usage message printed by argparse. If None, auto-generate
1874+ usage = usage.cl_usage
1875+
1876 config_file_name = default_config_file_name
1877
1878+ cl_arguments = Application.cl_arguments + cl_args
1879+
1880+ # Private and configuration attributes
1881+ _CrashHandler = crashhandler.IPythonCrashHandler
1882+
1883+ def __init__(self, argv=None,
1884+ constructor_config=None, override_config=None,
1885+ **shell_params):
1886+ """Create a new IPythonApp.
1887+
1888+ See the parent class for details on how configuration is handled.
1889+
1890+ Parameters
1891+ ----------
1892+ argv : optional, list
1893+ If given, used as the command-line argv environment to read arguments
1894+ from.
1895+
1896+ constructor_config : optional, Config
1897+ If given, additional config that is merged last, after internal
1898+ defaults, command-line and file-based configs.
1899+
1900+ override_config : optional, Config
1901+ If given, config that overrides all others unconditionally (except
1902+ for internal defaults, which ensure that all parameters exist).
1903+
1904+ shell_params : optional, dict
1905+ All other keywords are passed to the :class:`iplib.InteractiveShell`
1906+ constructor.
1907+ """
1908+ super(IPythonApp, self).__init__(argv, constructor_config,
1909+ override_config)
1910+ self.shell_params = shell_params
1911+
1912 def create_default_config(self):
1913 super(IPythonApp, self).create_default_config()
1914- self.default_config.Global.display_banner = True
1915+ # Eliminate multiple lookups
1916+ Global = self.default_config.Global
1917+
1918+ # Set all default values
1919+ Global.display_banner = True
1920
1921 # If the -c flag is given or a file is given to run at the cmd line
1922 # like "ipython foo.py", normally we exit without starting the main
1923 # loop. The force_interact config variable allows a user to override
1924 # this and interact. It is also set by the -i cmd line flag, just
1925 # like Python.
1926- self.default_config.Global.force_interact = False
1927+ Global.force_interact = False
1928
1929 # By default always interact by starting the IPython mainloop.
1930- self.default_config.Global.interact = True
1931+ Global.interact = True
1932
1933 # No GUI integration by default
1934- self.default_config.Global.wthread = False
1935- self.default_config.Global.q4thread = False
1936- self.default_config.Global.gthread = False
1937-
1938- def create_command_line_config(self):
1939- """Create and return a command line config loader."""
1940- return IPythonAppCLConfigLoader(
1941- description=self.description,
1942- version=release.version
1943- )
1944-
1945- def post_load_command_line_config(self):
1946- """Do actions after loading cl config."""
1947- clc = self.command_line_config
1948-
1949- # Display the deprecation warnings about threaded shells
1950- if hasattr(clc.Global, 'pylab'):
1951- pylab_warning()
1952- del clc.Global['pylab']
1953+ Global.gui = False
1954+ # Pylab off by default
1955+ Global.pylab = False
1956+
1957+ # Deprecated versions of gui support that used threading, we support
1958+ # them just for bacwards compatibility as an alternate spelling for
1959+ # '--gui X'
1960+ Global.qthread = False
1961+ Global.q4thread = False
1962+ Global.wthread = False
1963+ Global.gthread = False
1964
1965 def load_file_config(self):
1966 if hasattr(self.command_line_config.Global, 'quick'):
1967@@ -377,8 +462,7 @@
1968 # unless the -i flag (Global.force_interact) is true.
1969 code_to_run = config.Global.get('code_to_run','')
1970 file_to_run = False
1971- if len(self.extra_args)>=1:
1972- if self.extra_args[0]:
1973+ if self.extra_args and self.extra_args[0]:
1974 file_to_run = True
1975 if file_to_run or code_to_run:
1976 if not config.Global.force_interact:
1977@@ -390,10 +474,8 @@
1978 sys.path.insert(0, '')
1979
1980 # Create an InteractiveShell instance
1981- self.shell = InteractiveShell(
1982- parent=None,
1983- config=self.master_config
1984- )
1985+ self.shell = InteractiveShell(None, self.master_config,
1986+ **self.shell_params )
1987
1988 def post_construct(self):
1989 """Do actions after construct, but before starting the app."""
1990@@ -412,29 +494,52 @@
1991 if self.log_level <= logging.INFO: print
1992
1993 # Now a variety of things that happen after the banner is printed.
1994- self._enable_gui()
1995+ self._enable_gui_pylab()
1996 self._load_extensions()
1997 self._run_exec_lines()
1998 self._run_exec_files()
1999 self._run_cmd_line_code()
2000-
2001- def _enable_gui(self):
2002- """Enable GUI event loop integration."""
2003- config = self.master_config
2004- try:
2005- # Enable GUI integration
2006- if config.Global.wthread:
2007- self.log.info("Enabling wx GUI event loop integration")
2008- inputhook.enable_wx(app=True)
2009- elif config.Global.q4thread:
2010- self.log.info("Enabling Qt4 GUI event loop integration")
2011- inputhook.enable_qt4(app=True)
2012- elif config.Global.gthread:
2013- self.log.info("Enabling GTK GUI event loop integration")
2014- inputhook.enable_gtk(app=True)
2015- except:
2016- self.log.warn("Error in enabling GUI event loop integration:")
2017- self.shell.showtraceback()
2018+ self._configure_xmode()
2019+
2020+ def _enable_gui_pylab(self):
2021+ """Enable GUI event loop integration, taking pylab into account."""
2022+ Global = self.master_config.Global
2023+
2024+ # Select which gui to use
2025+ if Global.gui:
2026+ gui = Global.gui
2027+ # The following are deprecated, but there's likely to be a lot of use
2028+ # of this form out there, so we might as well support it for now. But
2029+ # the --gui option above takes precedence.
2030+ elif Global.wthread:
2031+ gui = inputhook.GUI_WX
2032+ elif Global.qthread:
2033+ gui = inputhook.GUI_QT
2034+ elif Global.gthread:
2035+ gui = inputhook.GUI_GTK
2036+ else:
2037+ gui = None
2038+
2039+ # Using --pylab will also require gui activation, though which toolkit
2040+ # to use may be chosen automatically based on mpl configuration.
2041+ if Global.pylab:
2042+ activate = self.shell.enable_pylab
2043+ if Global.pylab == 'auto':
2044+ gui = None
2045+ else:
2046+ gui = Global.pylab
2047+ else:
2048+ # Enable only GUI integration, no pylab
2049+ activate = inputhook.enable_gui
2050+
2051+ if gui or Global.pylab:
2052+ try:
2053+ self.log.info("Enabling GUI event loop integration, "
2054+ "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
2055+ activate(gui)
2056+ except:
2057+ self.log.warn("Error in enabling GUI event loop integration:")
2058+ self.shell.showtraceback()
2059
2060 def _load_extensions(self):
2061 """Load all IPython extensions in Global.extensions.
2062@@ -519,10 +624,17 @@
2063 self.log.warn("Error in executing file in user namespace: %s" % fname)
2064 self.shell.showtraceback()
2065
2066+ def _configure_xmode(self):
2067+ # XXX - shouldn't this be read from the config? I'm still a little
2068+ # lost with all the details of handling the new config guys...
2069+ self.shell.InteractiveTB.set_mode(mode=self.shell.xmode)
2070+
2071 def start_app(self):
2072 if self.master_config.Global.interact:
2073 self.log.debug("Starting IPython's mainloop...")
2074 self.shell.mainloop()
2075+ else:
2076+ self.log.debug("IPython not interactive, start_app is no-op...")
2077
2078
2079 def load_default_config(ipython_dir=None):
2080@@ -541,4 +653,3 @@
2081 """Create and run a full blown IPython instance"""
2082 app = IPythonApp()
2083 app.start()
2084-
2085
2086=== modified file 'IPython/core/iplib.py'
2087--- IPython/core/iplib.py 2009-11-09 01:49:24 +0000
2088+++ IPython/core/iplib.py 2010-01-18 01:17:11 +0000
2089@@ -17,6 +17,7 @@
2090 #-----------------------------------------------------------------------------
2091
2092 from __future__ import with_statement
2093+from __future__ import absolute_import
2094
2095 import __builtin__
2096 import StringIO
2097@@ -31,34 +32,37 @@
2098 import tempfile
2099 from contextlib import nested
2100
2101-from IPython.core import ultratb
2102 from IPython.core import debugger, oinspect
2103-from IPython.core import shadowns
2104 from IPython.core import history as ipcorehist
2105 from IPython.core import prefilter
2106+from IPython.core import shadowns
2107+from IPython.core import ultratb
2108 from IPython.core.alias import AliasManager
2109 from IPython.core.builtin_trap import BuiltinTrap
2110+from IPython.core.component import Component
2111 from IPython.core.display_trap import DisplayTrap
2112+from IPython.core.error import TryNext, UsageError
2113 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
2114 from IPython.core.logger import Logger
2115 from IPython.core.magic import Magic
2116+from IPython.core.prefilter import PrefilterManager
2117 from IPython.core.prompts import CachedOutput
2118-from IPython.core.prefilter import PrefilterManager
2119-from IPython.core.component import Component
2120+from IPython.core.pylabtools import pylab_activate
2121 from IPython.core.usage import interactive_usage, default_banner
2122-from IPython.core.error import TryNext, UsageError
2123-
2124-from IPython.utils import pickleshare
2125 from IPython.external.Itpl import ItplNS
2126+from IPython.lib.inputhook import enable_gui
2127 from IPython.lib.backgroundjobs import BackgroundJobManager
2128-from IPython.utils.ipstruct import Struct
2129 from IPython.utils import PyColorize
2130-from IPython.utils.genutils import *
2131+from IPython.utils import pickleshare
2132 from IPython.utils.genutils import get_ipython_dir
2133+from IPython.utils.ipstruct import Struct
2134 from IPython.utils.platutils import toggle_set_term_title, set_term_title
2135 from IPython.utils.strdispatch import StrDispatch
2136 from IPython.utils.syspathcontext import prepended_to_syspath
2137
2138+# XXX - need to clean up this import * line
2139+from IPython.utils.genutils import *
2140+
2141 # from IPython.utils import growl
2142 # growl.start("IPython")
2143
2144@@ -70,7 +74,6 @@
2145 # Globals
2146 #-----------------------------------------------------------------------------
2147
2148-
2149 # store the builtin raw_input globally, and use this always, in case user code
2150 # overwrites it (like wx.py.PyShell does)
2151 raw_input_original = raw_input
2152@@ -78,12 +81,10 @@
2153 # compiled regexps for autoindent management
2154 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
2155
2156-
2157 #-----------------------------------------------------------------------------
2158 # Utilities
2159 #-----------------------------------------------------------------------------
2160
2161-
2162 ini_spaces_re = re.compile(r'^(\s+)')
2163
2164
2165@@ -113,6 +114,8 @@
2166 return oldvalue
2167
2168
2169+def no_op(*a, **kw): pass
2170+
2171 class SpaceInInput(exceptions.Exception): pass
2172
2173 class Bunch: pass
2174@@ -183,6 +186,64 @@
2175 return super(SeparateStr, self).validate(obj, value)
2176
2177
2178+def make_user_namespaces(user_ns=None, user_global_ns=None):
2179+ """Return a valid local and global user interactive namespaces.
2180+
2181+ This builds a dict with the minimal information needed to operate as a
2182+ valid IPython user namespace, which you can pass to the various
2183+ embedding classes in ipython. The default implementation returns the
2184+ same dict for both the locals and the globals to allow functions to
2185+ refer to variables in the namespace. Customized implementations can
2186+ return different dicts. The locals dictionary can actually be anything
2187+ following the basic mapping protocol of a dict, but the globals dict
2188+ must be a true dict, not even a subclass. It is recommended that any
2189+ custom object for the locals namespace synchronize with the globals
2190+ dict somehow.
2191+
2192+ Raises TypeError if the provided globals namespace is not a true dict.
2193+
2194+ Parameters
2195+ ----------
2196+ user_ns : dict-like, optional
2197+ The current user namespace. The items in this namespace should
2198+ be included in the output. If None, an appropriate blank
2199+ namespace should be created.
2200+ user_global_ns : dict, optional
2201+ The current user global namespace. The items in this namespace
2202+ should be included in the output. If None, an appropriate
2203+ blank namespace should be created.
2204+
2205+ Returns
2206+ -------
2207+ A pair of dictionary-like object to be used as the local namespace
2208+ of the interpreter and a dict to be used as the global namespace.
2209+ """
2210+
2211+
2212+ # We must ensure that __builtin__ (without the final 's') is always
2213+ # available and pointing to the __builtin__ *module*. For more details:
2214+ # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
2215+
2216+ if user_ns is None:
2217+ # Set __name__ to __main__ to better match the behavior of the
2218+ # normal interpreter.
2219+ user_ns = {'__name__' :'__main__',
2220+ '__builtin__' : __builtin__,
2221+ '__builtins__' : __builtin__,
2222+ }
2223+ else:
2224+ user_ns.setdefault('__name__','__main__')
2225+ user_ns.setdefault('__builtin__',__builtin__)
2226+ user_ns.setdefault('__builtins__',__builtin__)
2227+
2228+ if user_global_ns is None:
2229+ user_global_ns = user_ns
2230+ if type(user_global_ns) is not dict:
2231+ raise TypeError("user_global_ns must be a true dict; got %r"
2232+ % type(user_global_ns))
2233+
2234+ return user_ns, user_global_ns
2235+
2236 #-----------------------------------------------------------------------------
2237 # Main IPython class
2238 #-----------------------------------------------------------------------------
2239@@ -329,6 +390,7 @@
2240 self.hooks.late_startup_hook()
2241
2242 def get_ipython(self):
2243+ """Return the currently running IPython instance."""
2244 return self
2245
2246 #-------------------------------------------------------------------------
2247@@ -814,8 +876,7 @@
2248 # These routines return properly built dicts as needed by the rest of
2249 # the code, and can also be used by extension writers to generate
2250 # properly initialized namespaces.
2251- user_ns, user_global_ns = self.make_user_namespaces(user_ns,
2252- user_global_ns)
2253+ user_ns, user_global_ns = make_user_namespaces(user_ns, user_global_ns)
2254
2255 # Assign namespaces
2256 # This is the namespace where all normal user variables live
2257@@ -825,7 +886,7 @@
2258 # An auxiliary namespace that checks what parts of the user_ns were
2259 # loaded at startup, so we can list later only variables defined in
2260 # actual interactive use. Since it is always a subset of user_ns, it
2261- # doesn't need to be seaparately tracked in the ns_table
2262+ # doesn't need to be separately tracked in the ns_table.
2263 self.user_config_ns = {}
2264
2265 # A namespace to keep track of internal data structures to prevent
2266@@ -900,55 +961,6 @@
2267 else:
2268 sys.modules[main_name] = FakeModule(self.user_ns)
2269
2270- def make_user_namespaces(self, user_ns=None, user_global_ns=None):
2271- """Return a valid local and global user interactive namespaces.
2272-
2273- This builds a dict with the minimal information needed to operate as a
2274- valid IPython user namespace, which you can pass to the various
2275- embedding classes in ipython. The default implementation returns the
2276- same dict for both the locals and the globals to allow functions to
2277- refer to variables in the namespace. Customized implementations can
2278- return different dicts. The locals dictionary can actually be anything
2279- following the basic mapping protocol of a dict, but the globals dict
2280- must be a true dict, not even a subclass. It is recommended that any
2281- custom object for the locals namespace synchronize with the globals
2282- dict somehow.
2283-
2284- Raises TypeError if the provided globals namespace is not a true dict.
2285-
2286- :Parameters:
2287- user_ns : dict-like, optional
2288- The current user namespace. The items in this namespace should
2289- be included in the output. If None, an appropriate blank
2290- namespace should be created.
2291- user_global_ns : dict, optional
2292- The current user global namespace. The items in this namespace
2293- should be included in the output. If None, an appropriate
2294- blank namespace should be created.
2295-
2296- :Returns:
2297- A tuple pair of dictionary-like object to be used as the local namespace
2298- of the interpreter and a dict to be used as the global namespace.
2299- """
2300-
2301- if user_ns is None:
2302- # Set __name__ to __main__ to better match the behavior of the
2303- # normal interpreter.
2304- user_ns = {'__name__' :'__main__',
2305- '__builtins__' : __builtin__,
2306- }
2307- else:
2308- user_ns.setdefault('__name__','__main__')
2309- user_ns.setdefault('__builtins__',__builtin__)
2310-
2311- if user_global_ns is None:
2312- user_global_ns = user_ns
2313- if type(user_global_ns) is not dict:
2314- raise TypeError("user_global_ns must be a true dict; got %r"
2315- % type(user_global_ns))
2316-
2317- return user_ns, user_global_ns
2318-
2319 def init_user_ns(self):
2320 """Initialize all user-visible namespaces to their minimum defaults.
2321
2322@@ -961,27 +973,59 @@
2323 method. If they were not empty before, data will simply be added to
2324 therm.
2325 """
2326- # Store myself as the public api!!!
2327- self.user_ns['get_ipython'] = self.get_ipython
2328-
2329- # make global variables for user access to the histories
2330- self.user_ns['_ih'] = self.input_hist
2331- self.user_ns['_oh'] = self.output_hist
2332- self.user_ns['_dh'] = self.dir_hist
2333-
2334- # user aliases to input and output histories
2335- self.user_ns['In'] = self.input_hist
2336- self.user_ns['Out'] = self.output_hist
2337-
2338- self.user_ns['_sh'] = shadowns
2339-
2340+ # This function works in two parts: first we put a few things in
2341+ # user_ns, and we sync that contents into user_config_ns so that these
2342+ # initial variables aren't shown by %who. After the sync, we add the
2343+ # rest of what we *do* want the user to see with %who even on a new
2344+ # session (probably nothing, so theye really only see their own stuff)
2345+
2346+ # The user dict must *always* have a __builtin__ reference to the
2347+ # Python standard __builtin__ namespace, which must be imported.
2348+ # This is so that certain operations in prompt evaluation can be
2349+ # reliably executed with builtins. Note that we can NOT use
2350+ # __builtins__ (note the 's'), because that can either be a dict or a
2351+ # module, and can even mutate at runtime, depending on the context
2352+ # (Python makes no guarantees on it). In contrast, __builtin__ is
2353+ # always a module object, though it must be explicitly imported.
2354+
2355+ # For more details:
2356+ # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
2357+ ns = dict(__builtin__ = __builtin__)
2358+
2359 # Put 'help' in the user namespace
2360 try:
2361 from site import _Helper
2362- self.user_ns['help'] = _Helper()
2363+ ns['help'] = _Helper()
2364 except ImportError:
2365 warn('help() not available - check site.py')
2366
2367+ # make global variables for user access to the histories
2368+ ns['_ih'] = self.input_hist
2369+ ns['_oh'] = self.output_hist
2370+ ns['_dh'] = self.dir_hist
2371+
2372+ ns['_sh'] = shadowns
2373+
2374+ # user aliases to input and output histories. These shouldn't show up
2375+ # in %who, as they can have very large reprs.
2376+ ns['In'] = self.input_hist
2377+ ns['Out'] = self.output_hist
2378+
2379+ # Store myself as the public api!!!
2380+ ns['get_ipython'] = self.get_ipython
2381+
2382+ # Sync what we've added so far to user_config_ns so these aren't seen
2383+ # by %who
2384+ self.user_config_ns.update(ns)
2385+
2386+ # Anything put into ns now would show up in %who. Think twice before
2387+ # putting anything here, as we really want %who to show the user their
2388+ # stuff, not our variables.
2389+
2390+ # Finally, update the real user's namespace
2391+ self.user_ns.update(ns)
2392+
2393+
2394 def reset(self):
2395 """Clear all internal namespaces.
2396
2397@@ -1099,9 +1143,6 @@
2398 def savehist(self):
2399 """Save input history to a file (via readline library)."""
2400
2401- if not self.has_readline:
2402- return
2403-
2404 try:
2405 self.readline.write_history_file(self.histfile)
2406 except:
2407@@ -1111,12 +1152,11 @@
2408 def reloadhist(self):
2409 """Reload the input history from disk file."""
2410
2411- if self.has_readline:
2412- try:
2413- self.readline.clear_history()
2414- self.readline.read_history_file(self.shell.histfile)
2415- except AttributeError:
2416- pass
2417+ try:
2418+ self.readline.clear_history()
2419+ self.readline.read_history_file(self.shell.histfile)
2420+ except AttributeError:
2421+ pass
2422
2423 def history_saving_wrapper(self, func):
2424 """ Wrap func for readline history saving
2425@@ -1150,37 +1190,14 @@
2426 color_scheme='NoColor',
2427 tb_offset = 1)
2428
2429- # IPython itself shouldn't crash. This will produce a detailed
2430- # post-mortem if it does. But we only install the crash handler for
2431- # non-threaded shells, the threaded ones use a normal verbose reporter
2432- # and lose the crash handler. This is because exceptions in the main
2433- # thread (such as in GUI code) propagate directly to sys.excepthook,
2434- # and there's no point in printing crash dumps for every user exception.
2435- if self.isthreaded:
2436- ipCrashHandler = ultratb.FormattedTB()
2437- else:
2438- from IPython.core import crashhandler
2439- ipCrashHandler = crashhandler.IPythonCrashHandler(self)
2440- self.set_crash_handler(ipCrashHandler)
2441+ # The instance will store a pointer to the system-wide exception hook,
2442+ # so that runtime code (such as magics) can access it. This is because
2443+ # during the read-eval loop, it may get temporarily overwritten.
2444+ self.sys_excepthook = sys.excepthook
2445
2446 # and add any custom exception handlers the user may have specified
2447 self.set_custom_exc(*custom_exceptions)
2448
2449- def set_crash_handler(self, crashHandler):
2450- """Set the IPython crash handler.
2451-
2452- This must be a callable with a signature suitable for use as
2453- sys.excepthook."""
2454-
2455- # Install the given crash handler as the Python exception hook
2456- sys.excepthook = crashHandler
2457-
2458- # The instance will store a pointer to this, so that runtime code
2459- # (such as magics) can access it. This is because during the
2460- # read-eval loop, it gets temporarily overwritten (to deal with GUI
2461- # frameworks).
2462- self.sys_excepthook = sys.excepthook
2463-
2464 def set_custom_exc(self,exc_tuple,handler):
2465 """set_custom_exc(exc_tuple,handler)
2466
2467@@ -1248,7 +1265,8 @@
2468 """
2469 self.showtraceback((etype,value,tb),tb_offset=0)
2470
2471- def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
2472+ def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
2473+ exception_only=False):
2474 """Display the exception that just occurred.
2475
2476 If nothing is known about the exception, this is the method which
2477@@ -1259,18 +1277,24 @@
2478 care of calling it if needed, so unless you are explicitly catching a
2479 SyntaxError exception, don't try to analyze the stack manually and
2480 simply call this method."""
2481-
2482-
2483- # Though this won't be called by syntax errors in the input line,
2484- # there may be SyntaxError cases whith imported code.
2485
2486 try:
2487 if exc_tuple is None:
2488 etype, value, tb = sys.exc_info()
2489 else:
2490 etype, value, tb = exc_tuple
2491+
2492+ if etype is None:
2493+ if hasattr(sys, 'last_type'):
2494+ etype, value, tb = sys.last_type, sys.last_value, \
2495+ sys.last_traceback
2496+ else:
2497+ self.write('No traceback available to show.\n')
2498+ return
2499
2500 if etype is SyntaxError:
2501+ # Though this won't be called by syntax errors in the input
2502+ # line, there may be SyntaxError cases whith imported code.
2503 self.showsyntaxerror(filename)
2504 elif etype is UsageError:
2505 print "UsageError:", value
2506@@ -1286,12 +1310,20 @@
2507 if etype in self.custom_exceptions:
2508 self.CustomTB(etype,value,tb)
2509 else:
2510- self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
2511- if self.InteractiveTB.call_pdb and self.has_readline:
2512- # pdb mucks up readline, fix it back
2513- self.set_completer()
2514+ if exception_only:
2515+ m = ('An exception has occurred, use %tb to see the '
2516+ 'full traceback.')
2517+ print m
2518+ self.InteractiveTB.show_exception_only(etype, value)
2519+ else:
2520+ self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
2521+ if self.InteractiveTB.call_pdb:
2522+ # pdb mucks up readline, fix it back
2523+ self.set_completer()
2524+
2525 except KeyboardInterrupt:
2526- self.write("\nKeyboardInterrupt\n")
2527+ self.write("\nKeyboardInterrupt\n")
2528+
2529
2530 def showsyntaxerror(self, filename=None):
2531 """Display the syntax error that just occurred.
2532@@ -1304,7 +1336,7 @@
2533 """
2534 etype, value, last_traceback = sys.exc_info()
2535
2536- # See note about these variables in showtraceback() below
2537+ # See note about these variables in showtraceback() above
2538 sys.last_type = etype
2539 sys.last_value = value
2540 sys.last_traceback = last_traceback
2541@@ -1464,20 +1496,25 @@
2542 def init_readline(self):
2543 """Command history completion/saving/reloading."""
2544
2545+ if self.readline_use:
2546+ import IPython.utils.rlineimpl as readline
2547+
2548 self.rl_next_input = None
2549 self.rl_do_indent = False
2550
2551- if not self.readline_use:
2552- return
2553-
2554- import IPython.utils.rlineimpl as readline
2555-
2556- if not readline.have_readline:
2557- self.has_readline = 0
2558+ if not self.readline_use or not readline.have_readline:
2559+ self.has_readline = False
2560 self.readline = None
2561- # no point in bugging windows users with this every time:
2562- warn('Readline services not available on this platform.')
2563+ # Set a number of methods that depend on readline to be no-op
2564+ self.savehist = no_op
2565+ self.reloadhist = no_op
2566+ self.set_completer = no_op
2567+ self.set_custom_completer = no_op
2568+ self.set_completer_frame = no_op
2569+ warn('Readline services not available or not loaded.')
2570 else:
2571+ self.has_readline = True
2572+ self.readline = readline
2573 sys.modules['readline'] = readline
2574 import atexit
2575 from IPython.core.completer import IPCompleter
2576@@ -1512,8 +1549,6 @@
2577 warn('Problems reading readline initialization file <%s>'
2578 % inputrc_name)
2579
2580- self.has_readline = 1
2581- self.readline = readline
2582 # save this in sys so embedded copies can restore it properly
2583 sys.ipcompleter = self.Completer.complete
2584 self.set_completer()
2585@@ -1585,6 +1620,9 @@
2586 # Set user colors (don't do it in the constructor above so that it
2587 # doesn't crash if colors option is invalid)
2588 self.magic_colors(self.colors)
2589+ # History was moved to a separate module
2590+ from . import history
2591+ history.init_ipython(self)
2592
2593 def magic(self,arg_s):
2594 """Call a magic function by name.
2595@@ -1603,7 +1641,6 @@
2596 valid Python code you can type at the interpreter, including loops and
2597 compound statements.
2598 """
2599-
2600 args = arg_s.split(' ',1)
2601 magic_name = args[0]
2602 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2603@@ -1842,7 +1879,8 @@
2604 except EOFError:
2605 if self.autoindent:
2606 self.rl_do_indent = False
2607- self.readline_startup_hook(None)
2608+ if self.has_readline:
2609+ self.readline_startup_hook(None)
2610 self.write('\n')
2611 self.exit()
2612 except bdb.BdbQuit:
2613@@ -1859,10 +1897,13 @@
2614 if (self.SyntaxTB.last_syntax_error and
2615 self.autoedit_syntax):
2616 self.edit_syntax_error()
2617-
2618+
2619 # We are off again...
2620 __builtin__.__dict__['__IPYTHON__active'] -= 1
2621
2622+ # Turn off the exit flag, so the mainloop can be restarted if desired
2623+ self.exit_now = False
2624+
2625 def safe_execfile(self, fname, *where, **kw):
2626 """A safe version of the builtin execfile().
2627
2628@@ -1878,7 +1919,8 @@
2629 One or two namespaces, passed to execfile() as (globals,locals).
2630 If only one is given, it is passed as both.
2631 exit_ignore : bool (False)
2632- If True, then don't print errors for non-zero exit statuses.
2633+ If True, then silence SystemExit for non-zero status (it is always
2634+ silenced for zero status, as it is so common).
2635 """
2636 kw.setdefault('exit_ignore', False)
2637
2638@@ -1903,40 +1945,21 @@
2639
2640 with prepended_to_syspath(dname):
2641 try:
2642- if sys.platform == 'win32' and sys.version_info < (2,5,1):
2643- # Work around a bug in Python for Windows. The bug was
2644- # fixed in in Python 2.5 r54159 and 54158, but that's still
2645- # SVN Python as of March/07. For details, see:
2646- # http://projects.scipy.org/ipython/ipython/ticket/123
2647- try:
2648- globs,locs = where[0:2]
2649- except:
2650- try:
2651- globs = locs = where[0]
2652- except:
2653- globs = locs = globals()
2654- exec file(fname) in globs,locs
2655- else:
2656- execfile(fname,*where)
2657- except SyntaxError:
2658- self.showsyntaxerror()
2659- warn('Failure executing file: <%s>' % fname)
2660+ execfile(fname,*where)
2661 except SystemExit, status:
2662- # Code that correctly sets the exit status flag to success (0)
2663- # shouldn't be bothered with a traceback. Note that a plain
2664- # sys.exit() does NOT set the message to 0 (it's empty) so that
2665- # will still get a traceback. Note that the structure of the
2666- # SystemExit exception changed between Python 2.4 and 2.5, so
2667- # the checks must be done in a version-dependent way.
2668- show = False
2669- if status.args[0]==0 and not kw['exit_ignore']:
2670- show = True
2671- if show:
2672- self.showtraceback()
2673- warn('Failure executing file: <%s>' % fname)
2674+ # If the call was made with 0 or None exit status (sys.exit(0)
2675+ # or sys.exit() ), don't bother showing a traceback, as both of
2676+ # these are considered normal by the OS:
2677+ # > python -c'import sys;sys.exit(0)'; echo $?
2678+ # 0
2679+ # > python -c'import sys;sys.exit()'; echo $?
2680+ # 0
2681+ # For other exit status, we show the exception unless
2682+ # explicitly silenced, but only in short form.
2683+ if status.code not in (0, None) and not kw['exit_ignore']:
2684+ self.showtraceback(exception_only=True)
2685 except:
2686 self.showtraceback()
2687- warn('Failure executing file: <%s>' % fname)
2688
2689 def safe_execfile_ipy(self, fname):
2690 """Like safe_execfile, but for .ipy files with IPython syntax.
2691@@ -2150,9 +2173,8 @@
2692 sys.excepthook = old_excepthook
2693 except SystemExit:
2694 self.resetbuffer()
2695- self.showtraceback()
2696- warn("Type %exit or %quit to exit IPython "
2697- "(%Exit or %Quit do so unconditionally).",level=1)
2698+ self.showtraceback(exception_only=True)
2699+ warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2700 except self.custom_exceptions:
2701 etype,value,tb = sys.exc_info()
2702 self.CustomTB(etype,value,tb)
2703@@ -2336,7 +2358,7 @@
2704 with prepended_to_syspath(self.ipython_extension_dir):
2705 __import__(module_str)
2706 mod = sys.modules[module_str]
2707- self._call_load_ipython_extension(mod)
2708+ return self._call_load_ipython_extension(mod)
2709
2710 def unload_extension(self, module_str):
2711 """Unload an IPython extension by its module name.
2712@@ -2368,11 +2390,11 @@
2713
2714 def _call_load_ipython_extension(self, mod):
2715 if hasattr(mod, 'load_ipython_extension'):
2716- mod.load_ipython_extension(self)
2717+ return mod.load_ipython_extension(self)
2718
2719 def _call_unload_ipython_extension(self, mod):
2720 if hasattr(mod, 'unload_ipython_extension'):
2721- mod.unload_ipython_extension(self)
2722+ return mod.unload_ipython_extension(self)
2723
2724 #-------------------------------------------------------------------------
2725 # Things related to the prefilter
2726@@ -2380,6 +2402,10 @@
2727
2728 def init_prefilter(self):
2729 self.prefilter_manager = PrefilterManager(self, config=self.config)
2730+ # Ultimately this will be refactored in the new interpreter code, but
2731+ # for now, we should expose the main prefilter method (there's legacy
2732+ # code out there that may rely on this).
2733+ self.prefilter = self.prefilter_manager.prefilter_lines
2734
2735 #-------------------------------------------------------------------------
2736 # Utilities
2737@@ -2445,11 +2471,46 @@
2738 return ask_yes_no(prompt,default)
2739
2740 #-------------------------------------------------------------------------
2741+ # Things related to GUI support and pylab
2742+ #-------------------------------------------------------------------------
2743+
2744+ def enable_pylab(self, gui=None):
2745+ """Activate pylab support at runtime.
2746+
2747+ This turns on support for matplotlib, preloads into the interactive
2748+ namespace all of numpy and pylab, and configures IPython to correcdtly
2749+ interact with the GUI event loop. The GUI backend to be used can be
2750+ optionally selected with the optional :param:`gui` argument.
2751+
2752+ Parameters
2753+ ----------
2754+ gui : optional, string
2755+
2756+ If given, dictates the choice of matplotlib GUI backend to use
2757+ (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2758+ 'gtk'), otherwise we use the default chosen by matplotlib (as
2759+ dictated by the matplotlib build-time options plus the user's
2760+ matplotlibrc configuration file).
2761+ """
2762+ # We want to prevent the loading of pylab to pollute the user's
2763+ # namespace as shown by the %who* magics, so we execute the activation
2764+ # code in an empty namespace, and we update *both* user_ns and
2765+ # user_config_ns with this information.
2766+ ns = {}
2767+ gui = pylab_activate(ns, gui)
2768+ self.user_ns.update(ns)
2769+ self.user_config_ns.update(ns)
2770+ # Now we must activate the gui pylab wants to use, and fix %run to take
2771+ # plot updates into account
2772+ enable_gui(gui)
2773+ self.magic_run = self._pylab_magic_run
2774+
2775+ #-------------------------------------------------------------------------
2776 # Things related to IPython exiting
2777 #-------------------------------------------------------------------------
2778
2779 def ask_exit(self):
2780- """ Call for exiting. Can be overiden and used as a callback. """
2781+ """ Ask the shell to exit. Can be overiden and used as a callback. """
2782 self.exit_now = True
2783
2784 def exit(self):
2785
2786=== modified file 'IPython/core/magic.py'
2787--- IPython/core/magic.py 2009-12-30 19:26:16 +0000
2788+++ IPython/core/magic.py 2010-01-18 01:17:11 +0000
2789@@ -44,21 +44,26 @@
2790
2791 # Homebrewed
2792 import IPython
2793-from IPython.utils import wildcard
2794+import IPython.utils.generics
2795+
2796 from IPython.core import debugger, oinspect
2797 from IPython.core.error import TryNext
2798+from IPython.core.error import UsageError
2799 from IPython.core.fakemodule import FakeModule
2800+from IPython.core.macro import Macro
2801+from IPython.core.page import page
2802 from IPython.core.prefilter import ESC_MAGIC
2803+from IPython.core.pylabtools import mpl_runner
2804+from IPython.lib.inputhook import enable_gui
2805 from IPython.external.Itpl import Itpl, itpl, printpl,itplns
2806+from IPython.testing import decorators as testdec
2807+from IPython.utils import platutils
2808+from IPython.utils import wildcard
2809 from IPython.utils.PyColorize import Parser
2810 from IPython.utils.ipstruct import Struct
2811-from IPython.core.macro import Macro
2812+
2813+# XXX - We need to switch to explicit imports here with genutils
2814 from IPython.utils.genutils import *
2815-from IPython.core.page import page
2816-from IPython.utils import platutils
2817-import IPython.utils.generics
2818-from IPython.core.error import UsageError
2819-from IPython.testing import decorators as testdec
2820
2821 #***************************************************************************
2822 # Utility functions
2823@@ -80,10 +85,16 @@
2824 done.add(h)
2825
2826 return newhead + tail
2827-
2828+
2829
2830 #***************************************************************************
2831 # Main class implementing Magic functionality
2832+
2833+# XXX - for some odd reason, if Magic is made a new-style class, we get errors
2834+# on construction of the main InteractiveShell object. Something odd is going
2835+# on with super() calls, Component and the MRO... For now leave it as-is, but
2836+# eventually this needs to be clarified.
2837+
2838 class Magic:
2839 """Magic functions for InteractiveShell.
2840
2841@@ -335,7 +346,7 @@
2842 raise ValueError,'incorrect mode given: %s' % mode
2843 # Get options
2844 list_all = kw.get('list_all',0)
2845- posix = kw.get('posix',True)
2846+ posix = kw.get('posix', os.name == 'posix')
2847
2848 # Check if we have more than one argument to warrant extra processing:
2849 odict = {} # Dictionary with options
2850@@ -864,7 +875,7 @@
2851 show_all=opt('a'),ignore_case=ignore_case)
2852 except:
2853 shell.showtraceback()
2854-
2855+
2856 def magic_who_ls(self, parameter_s=''):
2857 """Return a sorted list of all interactive variables.
2858
2859@@ -874,17 +885,15 @@
2860 user_ns = self.shell.user_ns
2861 internal_ns = self.shell.internal_ns
2862 user_config_ns = self.shell.user_config_ns
2863- out = []
2864+ out = [ i for i in user_ns
2865+ if not i.startswith('_') \
2866+ and not (i in internal_ns or i in user_config_ns) ]
2867+
2868 typelist = parameter_s.split()
2869+ if typelist:
2870+ typeset = set(typelist)
2871+ out = [i for i in out if type(i).__name__ in typeset]
2872
2873- for i in user_ns:
2874- if not (i.startswith('_') or i.startswith('_i')) \
2875- and not (i in internal_ns or i in user_config_ns):
2876- if typelist:
2877- if type(user_ns[i]).__name__ in typelist:
2878- out.append(i)
2879- else:
2880- out.append(i)
2881 out.sort()
2882 return out
2883
2884@@ -1571,7 +1580,7 @@
2885 return
2886
2887 if filename.lower().endswith('.ipy'):
2888- self.safe_execfile_ipy(filename)
2889+ self.shell.safe_execfile_ipy(filename)
2890 return
2891
2892 # Control the response to exit() calls made by the script being run
2893@@ -2522,25 +2531,15 @@
2894 self.shell.pprint = 1 - self.shell.pprint
2895 print 'Pretty printing has been turned', \
2896 ['OFF','ON'][self.shell.pprint]
2897-
2898- def magic_exit(self, parameter_s=''):
2899- """Exit IPython, confirming if configured to do so.
2900-
2901- You can configure whether IPython asks for confirmation upon exit by
2902- setting the confirm_exit flag in the ipythonrc file."""
2903-
2904- self.shell.exit()
2905-
2906- def magic_quit(self, parameter_s=''):
2907- """Exit IPython, confirming if configured to do so (like %exit)"""
2908-
2909- self.shell.exit()
2910-
2911+
2912 def magic_Exit(self, parameter_s=''):
2913 """Exit IPython without confirmation."""
2914
2915 self.shell.ask_exit()
2916
2917+ # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2918+ magic_exit = magic_quit = magic_Quit = magic_Exit
2919+
2920 #......................................................................
2921 # Functions to implement unix shell-type things
2922
2923@@ -2685,11 +2684,12 @@
2924 else:
2925 syscmdlist.append(ff)
2926 else:
2927+ no_alias = self.shell.alias_manager.no_alias
2928 for pdir in path:
2929 os.chdir(pdir)
2930 for ff in os.listdir(pdir):
2931 base, ext = os.path.splitext(ff)
2932- if isexec(ff) and base.lower() not in self.shell.no_alias:
2933+ if isexec(ff) and base.lower() not in no_alias:
2934 if ext.lower() == '.exe':
2935 ff = base
2936 try:
2937@@ -3399,8 +3399,6 @@
2938 your existing IPython session.
2939 """
2940
2941- # XXX - Fix this to have cleaner activate/deactivate calls.
2942- from IPython.extensions import InterpreterPasteInput as ipaste
2943 from IPython.utils.ipstruct import Struct
2944
2945 # Shorthands
2946@@ -3423,8 +3421,6 @@
2947
2948 if mode == False:
2949 # turn on
2950- ipaste.activate_prefilter()
2951-
2952 oc.prompt1.p_template = '>>> '
2953 oc.prompt2.p_template = '... '
2954 oc.prompt_out.p_template = ''
2955@@ -3438,13 +3434,11 @@
2956 oc.prompt_out.pad_left = False
2957
2958 shell.pprint = False
2959-
2960+
2961 shell.magic_xmode('Plain')
2962
2963 else:
2964 # turn off
2965- ipaste.deactivate_prefilter()
2966-
2967 oc.prompt1.p_template = shell.prompt_in1
2968 oc.prompt2.p_template = shell.prompt_in2
2969 oc.prompt_out.p_template = shell.prompt_out
2970@@ -3457,7 +3451,7 @@
2971 oc.prompt1.pad_left = oc.prompt2.pad_left = \
2972 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
2973
2974- rc.pprint = dstore.rc_pprint
2975+ shell.pprint = dstore.rc_pprint
2976
2977 shell.magic_xmode(dstore.xmode)
2978
2979@@ -3475,7 +3469,7 @@
2980 using the (pylab/wthread/etc.) command line flags. GUI toolkits
2981 can now be enabled, disabled and swtiched at runtime and keyboard
2982 interrupts should work without any problems. The following toolkits
2983- are supports: wxPython, PyQt4, PyGTK, and Tk::
2984+ are supported: wxPython, PyQt4, PyGTK, and Tk::
2985
2986 %gui wx # enable wxPython event loop integration
2987 %gui qt4|qt # enable PyQt4 event loop integration
2988@@ -3494,25 +3488,13 @@
2989
2990 This is highly recommended for most users.
2991 """
2992- from IPython.lib import inputhook
2993- if "-a" in parameter_s:
2994- app = True
2995- else:
2996- app = False
2997- if not parameter_s:
2998- inputhook.clear_inputhook()
2999- elif 'wx' in parameter_s:
3000- return inputhook.enable_wx(app)
3001- elif ('qt4' in parameter_s) or ('qt' in parameter_s):
3002- return inputhook.enable_qt4(app)
3003- elif 'gtk' in parameter_s:
3004- return inputhook.enable_gtk(app)
3005- elif 'tk' in parameter_s:
3006- return inputhook.enable_tk(app)
3007+ opts, arg = self.parse_options(parameter_s,'a')
3008+ if arg=='': arg = None
3009+ return enable_gui(arg, 'a' in opts)
3010
3011 def magic_load_ext(self, module_str):
3012 """Load an IPython extension by its module name."""
3013- self.load_extension(module_str)
3014+ return self.load_extension(module_str)
3015
3016 def magic_unload_ext(self, module_str):
3017 """Unload an IPython extension by its module name."""
3018@@ -3522,6 +3504,7 @@
3019 """Reload an IPython extension by its module name."""
3020 self.reload_extension(module_str)
3021
3022+ @testdec.skip_doctest
3023 def magic_install_profiles(self, s):
3024 """Install the default IPython profiles into the .ipython dir.
3025
3026@@ -3576,5 +3559,58 @@
3027 shutil.copy(src, dst)
3028 print "Installing default config file: %s" % dst
3029
3030+ # Pylab support: simple wrappers that activate pylab, load gui input
3031+ # handling and modify slightly %run
3032+
3033+ @testdec.skip_doctest
3034+ def _pylab_magic_run(self, parameter_s=''):
3035+ Magic.magic_run(self, parameter_s,
3036+ runner=mpl_runner(self.shell.safe_execfile))
3037+
3038+ _pylab_magic_run.__doc__ = magic_run.__doc__
3039+
3040+ @testdec.skip_doctest
3041+ def magic_pylab(self, s):
3042+ """Load numpy and matplotlib to work interactively.
3043+
3044+ %pylab [GUINAME]
3045+
3046+ This function lets you activate pylab (matplotlib, numpy and
3047+ interactive support) at any point during an IPython session.
3048+
3049+ It will import at the top level numpy as np, pyplot as plt, matplotlib,
3050+ pylab and mlab, as well as all names from numpy and pylab.
3051+
3052+ Parameters
3053+ ----------
3054+ guiname : optional
3055+ One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or
3056+ 'tk'). If given, the corresponding Matplotlib backend is used,
3057+ otherwise matplotlib's default (which you can override in your
3058+ matplotlib config file) is used.
3059+
3060+ Examples
3061+ --------
3062+ In this case, where the MPL default is TkAgg:
3063+ In [2]: %pylab
3064+
3065+ Welcome to pylab, a matplotlib-based Python environment.
3066+ Backend in use: TkAgg
3067+ For more information, type 'help(pylab)'.
3068+
3069+ But you can explicitly request a different backend:
3070+ In [3]: %pylab qt
3071+
3072+ Welcome to pylab, a matplotlib-based Python environment.
3073+ Backend in use: Qt4Agg
3074+ For more information, type 'help(pylab)'.
3075+ """
3076+ self.shell.enable_pylab(s)
3077+
3078+ def magic_tb(self, s):
3079+ """Print the last traceback with the currently active exception mode.
3080+
3081+ See %xmode for changing exception reporting modes."""
3082+ self.shell.showtraceback()
3083
3084 # end Magic
3085
3086=== modified file 'IPython/core/prefilter.py'
3087--- IPython/core/prefilter.py 2009-11-15 07:37:49 +0000
3088+++ IPython/core/prefilter.py 2010-01-18 01:17:11 +0000
3089@@ -158,7 +158,7 @@
3090 without worrying about *further* damaging state.
3091 """
3092 if not self._oinfo:
3093- self._oinfo = ip._ofind(self.ifun)
3094+ self._oinfo = ip.shell._ofind(self.ifun)
3095 return self._oinfo
3096
3097 def __str__(self):
3098@@ -362,7 +362,7 @@
3099 line = transformer.transform(line, continue_prompt)
3100 return line
3101
3102- def prefilter_line(self, line, continue_prompt):
3103+ def prefilter_line(self, line, continue_prompt=False):
3104 """Prefilter a single input line as text.
3105
3106 This method prefilters a single line of text by calling the
3107@@ -416,7 +416,7 @@
3108 # print "prefiltered line: %r" % prefiltered
3109 return prefiltered
3110
3111- def prefilter_lines(self, lines, continue_prompt):
3112+ def prefilter_lines(self, lines, continue_prompt=False):
3113 """Prefilter multiple input lines of text.
3114
3115 This is the main entry point for prefiltering multiple lines of
3116@@ -427,11 +427,19 @@
3117 which is the case when the user goes back to a multiline history
3118 entry and presses enter.
3119 """
3120- out = []
3121- for line in lines.rstrip('\n').split('\n'):
3122- out.append(self.prefilter_line(line, continue_prompt))
3123- return '\n'.join(out)
3124-
3125+ llines = lines.rstrip('\n').split('\n')
3126+ # We can get multiple lines in one shot, where multiline input 'blends'
3127+ # into one line, in cases like recalling from the readline history
3128+ # buffer. We need to make sure that in such cases, we correctly
3129+ # communicate downstream which line is first and which are continuation
3130+ # ones.
3131+ if len(llines) > 1:
3132+ out = '\n'.join([self.prefilter_line(line, lnum>0)
3133+ for lnum, line in enumerate(llines) ])
3134+ else:
3135+ out = self.prefilter_line(llines[0], continue_prompt)
3136+
3137+ return out
3138
3139 #-----------------------------------------------------------------------------
3140 # Prefilter transformers
3141@@ -508,6 +516,47 @@
3142 return line
3143
3144
3145+_classic_prompt_re = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
3146+
3147+class PyPromptTransformer(PrefilterTransformer):
3148+ """Handle inputs that start with '>>> ' syntax."""
3149+
3150+ priority = Int(50, config=True)
3151+
3152+ def transform(self, line, continue_prompt):
3153+
3154+ if not line or line.isspace() or line.strip() == '...':
3155+ # This allows us to recognize multiple input prompts separated by
3156+ # blank lines and pasted in a single chunk, very common when
3157+ # pasting doctests or long tutorial passages.
3158+ return ''
3159+ m = _classic_prompt_re.match(line)
3160+ if m:
3161+ return line[len(m.group(0)):]
3162+ else:
3163+ return line
3164+
3165+
3166+_ipy_prompt_re = re.compile(r'(^[ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )')
3167+
3168+class IPyPromptTransformer(PrefilterTransformer):
3169+ """Handle inputs that start classic IPython prompt syntax."""
3170+
3171+ priority = Int(50, config=True)
3172+
3173+ def transform(self, line, continue_prompt):
3174+
3175+ if not line or line.isspace() or line.strip() == '...':
3176+ # This allows us to recognize multiple input prompts separated by
3177+ # blank lines and pasted in a single chunk, very common when
3178+ # pasting doctests or long tutorial passages.
3179+ return ''
3180+ m = _ipy_prompt_re.match(line)
3181+ if m:
3182+ return line[len(m.group(0)):]
3183+ else:
3184+ return line
3185+
3186 #-----------------------------------------------------------------------------
3187 # Prefilter checkers
3188 #-----------------------------------------------------------------------------
3189@@ -755,9 +804,17 @@
3190 line = line_info.line
3191 continue_prompt = line_info.continue_prompt
3192
3193- if (continue_prompt and self.shell.autoindent and line.isspace() and
3194- (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2 or
3195- (self.shell.buffer[-1]).isspace() )):
3196+ if (continue_prompt and
3197+ self.shell.autoindent and
3198+ line.isspace() and
3199+
3200+ (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2
3201+ or
3202+ not self.shell.buffer
3203+ or
3204+ (self.shell.buffer[-1]).isspace()
3205+ )
3206+ ):
3207 line = ''
3208
3209 self.shell.log(line, line, continue_prompt)
3210@@ -845,12 +902,11 @@
3211 pre = line_info.pre
3212 continue_prompt = line_info.continue_prompt
3213 obj = line_info.ofind(self)['obj']
3214-
3215 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
3216
3217 # This should only be active for single-line input!
3218 if continue_prompt:
3219- self.log(line,line,continue_prompt)
3220+ self.shell.log(line,line,continue_prompt)
3221 return line
3222
3223 force_auto = isinstance(obj, IPyAutocall)
3224@@ -967,7 +1023,9 @@
3225
3226 _default_transformers = [
3227 AssignSystemTransformer,
3228- AssignMagicTransformer
3229+ AssignMagicTransformer,
3230+ PyPromptTransformer,
3231+ IPyPromptTransformer,
3232 ]
3233
3234 _default_checkers = [
3235@@ -992,4 +1050,3 @@
3236 HelpHandler,
3237 EmacsHandler
3238 ]
3239-
3240
3241=== modified file 'IPython/core/prompts.py'
3242--- IPython/core/prompts.py 2009-09-12 15:58:55 +0000
3243+++ IPython/core/prompts.py 2010-01-18 01:17:11 +0000
3244@@ -131,8 +131,14 @@
3245 # Prompt/history count, with the actual digits replaced by dots. Used
3246 # mainly in continuation prompts (prompt_in2)
3247 #r'\D': '${"."*len(str(self.cache.prompt_count))}',
3248- # More robust form of the above expression, that uses __builtins__
3249- r'\D': '${"."*__builtins__.len(__builtins__.str(self.cache.prompt_count))}',
3250+
3251+ # More robust form of the above expression, that uses the __builtin__
3252+ # module. Note that we can NOT use __builtins__ (note the 's'), because
3253+ # that can either be a dict or a module, and can even mutate at runtime,
3254+ # depending on the context (Python makes no guarantees on it). In
3255+ # contrast, __builtin__ is always a module object, though it must be
3256+ # explicitly imported.
3257+ r'\D': '${"."*__builtin__.len(__builtin__.str(self.cache.prompt_count))}',
3258
3259 # Current working directory
3260 r'\w': '${os.getcwd()}',
3261@@ -215,6 +221,7 @@
3262 out = '<ERROR: %s>' % msg
3263 except Exception,msg:
3264 out = '<ERROR: %s>' % msg
3265+ #raise # dbg
3266 return out
3267
3268 class BasePrompt(object):
3269@@ -549,18 +556,23 @@
3270 # print "Got prompt: ", outprompt
3271 if self.do_full_cache:
3272 cout_write(outprompt)
3273- else:
3274- print "self.do_full_cache = False"
3275
3276- # and now call a possibly user-defined print mechanism
3277- manipulated_val = self.display(arg)
3278+ # and now call a possibly user-defined print mechanism. Note that
3279+ # self.display typically prints as a side-effect, we don't do any
3280+ # printing to stdout here.
3281+ try:
3282+ manipulated_val = self.display(arg)
3283+ except TypeError:
3284+ # If the user's display hook didn't return a string we can
3285+ # print, we're done. Happens commonly if they return None
3286+ cout_write('\n')
3287+ return
3288
3289 # user display hooks can change the variable to be stored in
3290 # output history
3291-
3292 if manipulated_val is not None:
3293 arg = manipulated_val
3294-
3295+
3296 # avoid recursive reference when displaying _oh/Out
3297 if arg is not self.user_ns['_oh']:
3298 self.update(arg)
3299
3300=== added file 'IPython/core/pylabtools.py'
3301--- IPython/core/pylabtools.py 1970-01-01 00:00:00 +0000
3302+++ IPython/core/pylabtools.py 2010-01-18 01:17:11 +0000
3303@@ -0,0 +1,145 @@
3304+# -*- coding: utf-8 -*-
3305+"""Pylab (matplotlib) support utilities.
3306+
3307+Authors
3308+-------
3309+Fernando Perez.
3310+"""
3311+
3312+#-----------------------------------------------------------------------------
3313+# Copyright (C) 2009 The IPython Development Team
3314+#
3315+# Distributed under the terms of the BSD License. The full license is in
3316+# the file COPYING, distributed as part of this software.
3317+#-----------------------------------------------------------------------------
3318+#-----------------------------------------------------------------------------
3319+# Imports
3320+#-----------------------------------------------------------------------------
3321+from IPython.utils.genutils import flag_calls
3322+
3323+#-----------------------------------------------------------------------------
3324+# Main classes and functions
3325+#-----------------------------------------------------------------------------
3326+
3327+def pylab_activate(user_ns, gui=None, import_all=True):
3328+ """Activate pylab mode in the user's namespace.
3329+
3330+ Loads and initializes numpy, matplotlib and friends for interactive use.
3331+
3332+ Parameters
3333+ ----------
3334+ user_ns : dict
3335+ Namespace where the imports will occur.
3336+
3337+ gui : optional, string
3338+ A valid gui name following the conventions of the %gui magic.
3339+
3340+ import_all : optional, boolean
3341+ If true, an 'import *' is done from numpy and pylab.
3342+
3343+ Returns
3344+ -------
3345+ The actual gui used (if not given as input, it was obtained from matplotlib
3346+ itself, and will be needed next to configure IPython's gui integration.
3347+ """
3348+
3349+ # Initialize matplotlib to interactive mode always
3350+ import matplotlib
3351+
3352+ # If user specifies a GUI, that dictates the backend, otherwise we read the
3353+ # user's mpl default from the mpl rc structure
3354+ g2b = {'tk': 'TkAgg',
3355+ 'gtk': 'GTKAgg',
3356+ 'wx': 'WXAgg',
3357+ 'qt': 'Qt4Agg', # qt3 not supported
3358+ 'qt4': 'Qt4Agg' }
3359+
3360+ if gui:
3361+ # select backend based on requested gui
3362+ backend = g2b[gui]
3363+ else:
3364+ backend = matplotlib.rcParams['backend']
3365+ # In this case, we need to find what the appropriate gui selection call
3366+ # should be for IPython, so we can activate inputhook accordingly
3367+ b2g = dict(zip(g2b.values(),g2b.keys()))
3368+ gui = b2g[backend]
3369+
3370+ # We must set the desired backend before importing pylab
3371+ matplotlib.use(backend)
3372+
3373+ # This must be imported last in the matplotlib series, after
3374+ # backend/interactivity choices have been made
3375+ import matplotlib.pylab as pylab
3376+
3377+ # XXX For now leave this commented out, but depending on discussions with
3378+ # mpl-dev, we may be able to allow interactive switching...
3379+ #import matplotlib.pyplot
3380+ #matplotlib.pyplot.switch_backend(backend)
3381+
3382+ pylab.show._needmain = False
3383+ # We need to detect at runtime whether show() is called by the user.
3384+ # For this, we wrap it into a decorator which adds a 'called' flag.
3385+ pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
3386+
3387+ # Import numpy as np/pyplot as plt are conventions we're trying to
3388+ # somewhat standardize on. Making them available to users by default
3389+ # will greatly help this.
3390+ exec ("import numpy\n"
3391+ "import matplotlib\n"
3392+ "from matplotlib import pylab, mlab, pyplot\n"
3393+ "np = numpy\n"
3394+ "plt = pyplot\n"
3395+ ) in user_ns
3396+
3397+ if import_all:
3398+ exec("from matplotlib.pylab import *\n"
3399+ "from numpy import *\n") in user_ns
3400+
3401+ matplotlib.interactive(True)
3402+
3403+ print """
3404+Welcome to pylab, a matplotlib-based Python environment [backend: %s].
3405+For more information, type 'help(pylab)'.""" % backend
3406+
3407+ return gui
3408+
3409+# We need a little factory function here to create the closure where
3410+# safe_execfile can live.
3411+def mpl_runner(safe_execfile):
3412+ """Factory to return a matplotlib-enabled runner for %run.
3413+
3414+ Parameters
3415+ ----------
3416+ safe_execfile : function
3417+ This must be a function with the same interface as the
3418+ :meth:`safe_execfile` method of IPython.
3419+
3420+ Returns
3421+ -------
3422+ A function suitable for use as the ``runner`` argument of the %run magic
3423+ function.
3424+ """
3425+
3426+ def mpl_execfile(fname,*where,**kw):
3427+ """matplotlib-aware wrapper around safe_execfile.
3428+
3429+ Its interface is identical to that of the :func:`execfile` builtin.
3430+
3431+ This is ultimately a call to execfile(), but wrapped in safeties to
3432+ properly handle interactive rendering."""
3433+
3434+ import matplotlib
3435+ import matplotlib.pylab as pylab
3436+
3437+ #print '*** Matplotlib runner ***' # dbg
3438+ # turn off rendering until end of script
3439+ is_interactive = matplotlib.rcParams['interactive']
3440+ matplotlib.interactive(False)
3441+ safe_execfile(fname,*where,**kw)
3442+ matplotlib.interactive(is_interactive)
3443+ # make rendering call now, if the user tried to do it
3444+ if pylab.draw_if_interactive.called:
3445+ pylab.draw()
3446+ pylab.draw_if_interactive.called = False
3447+
3448+ return mpl_execfile
3449
3450=== modified file 'IPython/core/quitter.py' (properties changed: -x to +x)
3451--- IPython/core/quitter.py 2009-08-27 19:20:47 +0000
3452+++ IPython/core/quitter.py 2010-01-18 01:17:11 +0000
3453@@ -1,10 +1,10 @@
3454-#!/usr/bin/env python
3455-# encoding: utf-8
3456+# coding: utf-8
3457 """
3458 A simple class for quitting IPython.
3459
3460-Authors:
3461-
3462+Authors
3463+-------
3464+* Fernando Perez
3465 * Brian Granger
3466 """
3467
3468@@ -19,6 +19,7 @@
3469 # Imports
3470 #-----------------------------------------------------------------------------
3471
3472+import sys
3473
3474 class Quitter(object):
3475 """Simple class to handle exit, similar to Python 2.5's.
3476@@ -30,9 +31,13 @@
3477 self.shell = shell
3478 self.name = name
3479
3480- def __repr__(self):
3481+ def __str__(self):
3482 return 'Type %s() to exit.' % self.name
3483- __str__ = __repr__
3484
3485 def __call__(self):
3486- self.shell.exit()
3487\ No newline at end of file
3488+ self.shell.ask_exit()
3489+
3490+ # Repr MUST return a string, else display like pprint hooks get confused
3491+ def __repr__(self):
3492+ self.shell.ask_exit()
3493+ return 'Bye.'
3494
3495=== modified file 'IPython/core/release.py'
3496--- IPython/core/release.py 2009-12-30 19:36:04 +0000
3497+++ IPython/core/release.py 2010-01-18 01:17:11 +0000
3498@@ -23,7 +23,7 @@
3499 development = True # change this to False to do a release
3500 version_base = '0.11'
3501 branch = 'ipython'
3502-revision = '1219'
3503+revision = '1346'
3504
3505 if development:
3506 if branch == 'ipython':
3507
3508=== removed file 'IPython/core/tests/obj_del.py'
3509--- IPython/core/tests/obj_del.py 2009-09-12 15:58:55 +0000
3510+++ IPython/core/tests/obj_del.py 1970-01-01 00:00:00 +0000
3511@@ -1,35 +0,0 @@
3512-"""Test code for https://bugs.launchpad.net/ipython/+bug/239054
3513-
3514-WARNING: this script exits IPython! It MUST be run in a subprocess.
3515-
3516-When you run the following script from CPython it prints:
3517-__init__ is here
3518-__del__ is here
3519-
3520-and creates the __del__.txt file
3521-
3522-When you run it from IPython it prints:
3523-__init__ is here
3524-
3525-When you exit() or Exit from IPython neothing is printed and no file is created
3526-(the file thing is to make sure __del__ is really never called and not that
3527-just the output is eaten).
3528-
3529-Note that if you call %reset in IPython then everything is Ok.
3530-
3531-IPython should do the equivalent of %reset and release all the references it
3532-holds before exit. This behavior is important when working with binding objects
3533-that rely on __del__. If the current behavior has some use case then I suggest
3534-to add a configuration option to IPython to control it.
3535-"""
3536-import sys
3537-
3538-class A(object):
3539- def __del__(self):
3540- print 'obj_del.py: object A deleted'
3541-
3542-a = A()
3543-
3544-# Now, we force an exit, the caller will check that the del printout was given
3545-_ip = get_ipython()
3546-_ip.ask_exit()
3547
3548=== added file 'IPython/core/tests/simpleerr.py'
3549--- IPython/core/tests/simpleerr.py 1970-01-01 00:00:00 +0000
3550+++ IPython/core/tests/simpleerr.py 2010-01-18 01:17:11 +0000
3551@@ -0,0 +1,32 @@
3552+"""Error script. DO NOT EDIT FURTHER! It will break exception doctests!!!"""
3553+import sys
3554+
3555+def div0():
3556+ "foo"
3557+ x = 1
3558+ y = 0
3559+ x/y
3560+
3561+def sysexit(stat, mode):
3562+ raise SystemExit(stat, 'Mode = %s' % mode)
3563+
3564+def bar(mode):
3565+ "bar"
3566+ if mode=='div':
3567+ div0()
3568+ elif mode=='exit':
3569+ try:
3570+ stat = int(sys.argv[2])
3571+ except:
3572+ stat = 1
3573+ sysexit(stat, mode)
3574+ else:
3575+ raise ValueError('Unknown mode')
3576+
3577+if __name__ == '__main__':
3578+ try:
3579+ mode = sys.argv[1]
3580+ except IndexError:
3581+ mode = 'div'
3582+
3583+ bar(mode)
3584
3585=== modified file 'IPython/core/tests/tclass.py'
3586--- IPython/core/tests/tclass.py 2009-07-02 18:59:10 +0000
3587+++ IPython/core/tests/tclass.py 2010-01-18 01:17:11 +0000
3588@@ -1,22 +1,19 @@
3589-"""Simple script to instantiate a class for testing %run"""
3590+"""Simple script to be run *twice*, to check reference counting bugs.
3591+
3592+See test_run for details."""
3593
3594 import sys
3595
3596-# An external test will check that calls to f() work after %run
3597-class foo: pass
3598-
3599-def f():
3600- return foo()
3601-
3602-# We also want to ensure that while objects remain available for immediate
3603-# access, objects from *previous* runs of the same script get collected, to
3604-# avoid accumulating massive amounts of old references.
3605+# We want to ensure that while objects remain available for immediate access,
3606+# objects from *previous* runs of the same script get collected, to avoid
3607+# accumulating massive amounts of old references.
3608 class C(object):
3609 def __init__(self,name):
3610 self.name = name
3611
3612 def __del__(self):
3613 print 'tclass.py: deleting object:',self.name
3614+ sys.stdout.flush()
3615
3616 try:
3617 name = sys.argv[1]
3618@@ -25,3 +22,10 @@
3619 else:
3620 if name.startswith('C'):
3621 c = C(name)
3622+
3623+#print >> sys.stderr, "ARGV:", sys.argv # dbg
3624+
3625+# This next print statement is NOT debugging, we're making the check on a
3626+# completely separate process so we verify by capturing stdout:
3627+print 'ARGV 1-:', sys.argv[1:]
3628+sys.stdout.flush()
3629
3630=== added file 'IPython/core/tests/test_completer.py'
3631--- IPython/core/tests/test_completer.py 1970-01-01 00:00:00 +0000
3632+++ IPython/core/tests/test_completer.py 2010-01-18 01:17:11 +0000
3633@@ -0,0 +1,35 @@
3634+"""Tests for the IPython tab-completion machinery.
3635+"""
3636+#-----------------------------------------------------------------------------
3637+# Module imports
3638+#-----------------------------------------------------------------------------
3639+
3640+# stdlib
3641+import sys
3642+
3643+# third party
3644+import nose.tools as nt
3645+
3646+# our own packages
3647+from IPython.core import completer
3648+
3649+#-----------------------------------------------------------------------------
3650+# Test functions
3651+#-----------------------------------------------------------------------------
3652+def test_protect_filename():
3653+ pairs = [ ('abc','abc'),
3654+ (' abc',r'\ abc'),
3655+ ('a bc',r'a\ bc'),
3656+ ('a bc',r'a\ \ bc'),
3657+ (' bc',r'\ \ bc'),
3658+ ]
3659+ # On posix, we also protect parens
3660+ if sys.platform != 'win32':
3661+ pairs.extend( [('a(bc',r'a\(bc'),
3662+ ('a)bc',r'a\)bc'),
3663+ ('a( )bc',r'a\(\ \)bc'),
3664+ ] )
3665+ # run the actual tests
3666+ for s1, s2 in pairs:
3667+ s1p = completer.protect_filename(s1)
3668+ nt.assert_equals(s1p, s2)
3669
3670=== modified file 'IPython/core/tests/test_iplib.py'
3671--- IPython/core/tests/test_iplib.py 2009-11-09 01:49:24 +0000
3672+++ IPython/core/tests/test_iplib.py 2010-01-18 01:17:11 +0000
3673@@ -13,45 +13,233 @@
3674 import nose.tools as nt
3675
3676 # our own packages
3677-from IPython.core import iplib
3678-from IPython.core import ipapi
3679-
3680+from IPython.testing import decorators as dec
3681+from IPython.testing.globalipapp import get_ipython
3682
3683 #-----------------------------------------------------------------------------
3684 # Globals
3685 #-----------------------------------------------------------------------------
3686
3687-# Useful global ipapi object and main IPython one. Unfortunately we have a
3688-# long precedent of carrying the 'ipapi' global object which is injected into
3689-# the system namespace as _ip, but that keeps a pointer to the actual IPython
3690-# InteractiveShell instance, which is named IP. Since in testing we do need
3691-# access to the real thing (we want to probe beyond what ipapi exposes), make
3692-# here a global reference to each. In general, things that are exposed by the
3693-# ipapi instance should be read from there, but we also will often need to use
3694-# the actual IPython one.
3695-
3696-# Get the public instance of IPython, and if it's None, make one so we can use
3697-# it for testing
3698-ip = ipapi.get()
3699-if ip is None:
3700- # IPython not running yet, make one from the testing machinery for
3701- # consistency when the test suite is being run via iptest
3702- from IPython.testing.plugin import ipdoctest
3703- ip = ipapi.get()
3704+# Get the public instance of IPython
3705+ip = get_ipython()
3706
3707 #-----------------------------------------------------------------------------
3708 # Test functions
3709 #-----------------------------------------------------------------------------
3710
3711+@dec.parametric
3712 def test_reset():
3713 """reset must clear most namespaces."""
3714- ip.reset() # first, it should run without error
3715- # Then, check that most namespaces end up empty
3716+ # The number of variables in the private user_config_ns is not zero, but it
3717+ # should be constant regardless of what we do
3718+ nvars_config_ns = len(ip.user_config_ns)
3719+
3720+ # Check that reset runs without error
3721+ ip.reset()
3722+
3723+ # Once we've reset it (to clear of any junk that might have been there from
3724+ # other tests, we can count how many variables are in the user's namespace
3725+ nvars_user_ns = len(ip.user_ns)
3726+
3727+ # Now add a few variables to user_ns, and check that reset clears them
3728+ ip.user_ns['x'] = 1
3729+ ip.user_ns['y'] = 1
3730+ ip.reset()
3731+
3732+ # Finally, check that all namespaces have only as many variables as we
3733+ # expect to find in them:
3734 for ns in ip.ns_refs_table:
3735 if ns is ip.user_ns:
3736- # The user namespace is reset with some data, so we can't check for
3737- # it being empty
3738- continue
3739- nt.assert_equals(len(ns),0)
3740-
3741-
3742\ No newline at end of file
3743+ nvars_expected = nvars_user_ns
3744+ elif ns is ip.user_config_ns:
3745+ nvars_expected = nvars_config_ns
3746+ else:
3747+ nvars_expected = 0
3748+
3749+ yield nt.assert_equals(len(ns), nvars_expected)
3750+
3751+
3752+# Tests for reporting of exceptions in various modes, handling of SystemExit,
3753+# and %tb functionality. This is really a mix of testing ultraTB and iplib.
3754+
3755+def doctest_tb_plain():
3756+ """
3757+In [18]: xmode plain
3758+Exception reporting mode: Plain
3759+
3760+In [19]: run simpleerr.py
3761+Traceback (most recent call last):
3762+ ...line 32, in <module>
3763+ bar(mode)
3764+ ...line 16, in bar
3765+ div0()
3766+ ...line 8, in div0
3767+ x/y
3768+ZeroDivisionError: integer division or modulo by zero
3769+ """
3770+
3771+
3772+def doctest_tb_context():
3773+ """
3774+In [3]: xmode context
3775+Exception reporting mode: Context
3776+
3777+In [4]: run simpleerr.py
3778+---------------------------------------------------------------------------
3779+ZeroDivisionError Traceback (most recent call last)
3780+<BLANKLINE>
3781+... in <module>()
3782+ 30 mode = 'div'
3783+ 31
3784+---> 32 bar(mode)
3785+ 33
3786+ 34
3787+<BLANKLINE>
3788+... in bar(mode)
3789+ 14 "bar"
3790+ 15 if mode=='div':
3791+---> 16 div0()
3792+ 17 elif mode=='exit':
3793+ 18 try:
3794+<BLANKLINE>
3795+... in div0()
3796+ 6 x = 1
3797+ 7 y = 0
3798+----> 8 x/y
3799+ 9
3800+ 10 def sysexit(stat, mode):
3801+<BLANKLINE>
3802+ZeroDivisionError: integer division or modulo by zero
3803+"""
3804+
3805+
3806+def doctest_tb_verbose():
3807+ """
3808+In [5]: xmode verbose
3809+Exception reporting mode: Verbose
3810+
3811+In [6]: run simpleerr.py
3812+---------------------------------------------------------------------------
3813+ZeroDivisionError Traceback (most recent call last)
3814+<BLANKLINE>
3815+... in <module>()
3816+ 30 mode = 'div'
3817+ 31
3818+---> 32 bar(mode)
3819+ global bar = <function bar at ...>
3820+ global mode = 'div'
3821+ 33
3822+ 34
3823+<BLANKLINE>
3824+... in bar(mode='div')
3825+ 14 "bar"
3826+ 15 if mode=='div':
3827+---> 16 div0()
3828+ global div0 = <function div0 at ...>
3829+ 17 elif mode=='exit':
3830+ 18 try:
3831+<BLANKLINE>
3832+... in div0()
3833+ 6 x = 1
3834+ 7 y = 0
3835+----> 8 x/y
3836+ x = 1
3837+ y = 0
3838+ 9
3839+ 10 def sysexit(stat, mode):
3840+<BLANKLINE>
3841+ZeroDivisionError: integer division or modulo by zero
3842+ """
3843+
3844+
3845+def doctest_tb_sysexit():
3846+ """
3847+In [17]: %xmode plain
3848+Exception reporting mode: Plain
3849+
3850+In [18]: %run simpleerr.py exit
3851+An exception has occurred, use %tb to see the full traceback.
3852+SystemExit: (1, 'Mode = exit')
3853+
3854+In [19]: %run simpleerr.py exit 2
3855+An exception has occurred, use %tb to see the full traceback.
3856+SystemExit: (2, 'Mode = exit')
3857+
3858+In [20]: %tb
3859+Traceback (most recent call last):
3860+ File ... in <module>
3861+ bar(mode)
3862+ File ... line 22, in bar
3863+ sysexit(stat, mode)
3864+ File ... line 11, in sysexit
3865+ raise SystemExit(stat, 'Mode = %s' % mode)
3866+SystemExit: (2, 'Mode = exit')
3867+
3868+In [21]: %xmode context
3869+Exception reporting mode: Context
3870+
3871+In [22]: %tb
3872+---------------------------------------------------------------------------
3873+SystemExit Traceback (most recent call last)
3874+<BLANKLINE>
3875+...<module>()
3876+ 30 mode = 'div'
3877+ 31
3878+---> 32 bar(mode)
3879+ 33
3880+ 34
3881+<BLANKLINE>
3882+...bar(mode)
3883+ 20 except:
3884+ 21 stat = 1
3885+---> 22 sysexit(stat, mode)
3886+ 23 else:
3887+ 24 raise ValueError('Unknown mode')
3888+<BLANKLINE>
3889+...sysexit(stat, mode)
3890+ 9
3891+ 10 def sysexit(stat, mode):
3892+---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
3893+ 12
3894+ 13 def bar(mode):
3895+<BLANKLINE>
3896+SystemExit: (2, 'Mode = exit')
3897+
3898+In [23]: %xmode verbose
3899+Exception reporting mode: Verbose
3900+
3901+In [24]: %tb
3902+---------------------------------------------------------------------------
3903+SystemExit Traceback (most recent call last)
3904+<BLANKLINE>
3905+... in <module>()
3906+ 30 mode = 'div'
3907+ 31
3908+---> 32 bar(mode)
3909+ global bar = <function bar at ...>
3910+ global mode = 'exit'
3911+ 33
3912+ 34
3913+<BLANKLINE>
3914+... in bar(mode='exit')
3915+ 20 except:
3916+ 21 stat = 1
3917+---> 22 sysexit(stat, mode)
3918+ global sysexit = <function sysexit at ...>
3919+ stat = 2
3920+ mode = 'exit'
3921+ 23 else:
3922+ 24 raise ValueError('Unknown mode')
3923+<BLANKLINE>
3924+... in sysexit(stat=2, mode='exit')
3925+ 9
3926+ 10 def sysexit(stat, mode):
3927+---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
3928+ global SystemExit = undefined
3929+ stat = 2
3930+ mode = 'exit'
3931+ 12
3932+ 13 def bar(mode):
3933+<BLANKLINE>
3934+SystemExit: (2, 'Mode = exit')
3935+ """
3936
3937=== modified file 'IPython/core/tests/test_magic.py'
3938--- IPython/core/tests/test_magic.py 2009-09-18 02:40:20 +0000
3939+++ IPython/core/tests/test_magic.py 2010-01-18 01:17:11 +0000
3940@@ -2,22 +2,31 @@
3941
3942 Needs to be run by nose (to make ipython session available).
3943 """
3944-
3945+from __future__ import absolute_import
3946+
3947+#-----------------------------------------------------------------------------
3948+# Imports
3949+#-----------------------------------------------------------------------------
3950+
3951+# stdlib
3952 import os
3953 import sys
3954 import tempfile
3955 import types
3956 from cStringIO import StringIO
3957
3958+# third-party
3959 import nose.tools as nt
3960
3961+# our own
3962+from IPython.utils import genutils
3963 from IPython.utils.platutils import find_cmd, get_long_path_name
3964 from IPython.testing import decorators as dec
3965 from IPython.testing import tools as tt
3966
3967 #-----------------------------------------------------------------------------
3968 # Test functions begin
3969-
3970+#-----------------------------------------------------------------------------
3971 def test_rehashx():
3972 # clear up everything
3973 _ip = get_ipython()
3974@@ -37,6 +46,19 @@
3975 yield (nt.assert_true, len(scoms) > 10)
3976
3977
3978+def test_magic_parse_options():
3979+ """Test that we don't mangle paths when parsing magic options."""
3980+ ip = get_ipython()
3981+ path = 'c:\\x'
3982+ opts = ip.parse_options('-f %s' % path,'f:')[0]
3983+ # argv splitting is os-dependent
3984+ if os.name == 'posix':
3985+ expected = 'c:x'
3986+ else:
3987+ expected = path
3988+ nt.assert_equals(opts['f'], expected)
3989+
3990+
3991 def doctest_hist_f():
3992 """Test %hist -f with temporary filename.
3993
3994@@ -45,35 +67,93 @@
3995 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
3996
3997 In [11]: %hist -n -f $tfile 3
3998+
3999+ In [13]: import os; os.unlink(tfile)
4000 """
4001
4002
4003 def doctest_hist_r():
4004 """Test %hist -r
4005
4006- XXX - This test is not recording the output correctly. Not sure why...
4007-
4008- In [20]: 'hist' in _ip.lsmagic()
4009- Out[20]: True
4010-
4011- In [6]: x=1
4012-
4013- In [7]: %hist -n -r 2
4014- x=1 # random
4015- hist -n -r 2 # random
4016- """
4017-
4018-# This test is known to fail on win32.
4019-# See ticket https://bugs.launchpad.net/bugs/366334
4020-def test_obj_del():
4021- _ip = get_ipython()
4022- """Test that object's __del__ methods are called on exit."""
4023- test_dir = os.path.dirname(__file__)
4024- del_file = os.path.join(test_dir,'obj_del.py')
4025- ipython_cmd = find_cmd('ipython')
4026- out = _ip.getoutput('%s %s' % (ipython_cmd, del_file))
4027- nt.assert_equals(out,'obj_del.py: object A deleted')
4028-
4029+ XXX - This test is not recording the output correctly. For some reason, in
4030+ testing mode the raw history isn't getting populated. No idea why.
4031+ Disabling the output checking for now, though at least we do run it.
4032+
4033+ In [1]: 'hist' in _ip.lsmagic()
4034+ Out[1]: True
4035+
4036+ In [2]: x=1
4037+
4038+ In [3]: %hist -r 2
4039+ x=1 # random
4040+ %hist -r 2
4041+ """
4042+
4043+def doctest_hist_op():
4044+ """Test %hist -op
4045+
4046+ In [1]: class b:
4047+ ...: pass
4048+ ...:
4049+
4050+ In [2]: class s(b):
4051+ ...: def __str__(self):
4052+ ...: return 's'
4053+ ...:
4054+
4055+ In [3]:
4056+
4057+ In [4]: class r(b):
4058+ ...: def __repr__(self):
4059+ ...: return 'r'
4060+ ...:
4061+
4062+ In [5]: class sr(s,r): pass
4063+ ...:
4064+
4065+ In [6]:
4066+
4067+ In [7]: bb=b()
4068+
4069+ In [8]: ss=s()
4070+
4071+ In [9]: rr=r()
4072+
4073+ In [10]: ssrr=sr()
4074+
4075+ In [11]: bb
4076+ Out[11]: <...b instance at ...>
4077+
4078+ In [12]: ss
4079+ Out[12]: <...s instance at ...>
4080+
4081+ In [13]:
4082+
4083+ In [14]: %hist -op
4084+ >>> class b:
4085+ ... pass
4086+ ...
4087+ >>> class s(b):
4088+ ... def __str__(self):
4089+ ... return 's'
4090+ ...
4091+ >>>
4092+ >>> class r(b):
4093+ ... def __repr__(self):
4094+ ... return 'r'
4095+ ...
4096+ >>> class sr(s,r): pass
4097+ >>>
4098+ >>> bb=b()
4099+ >>> ss=s()
4100+ >>> rr=r()
4101+ >>> ssrr=sr()
4102+ >>> bb
4103+ <...b instance at ...>
4104+ >>> ss
4105+ <...s instance at ...>
4106+ >>>
4107+ """
4108
4109 def test_shist():
4110 # Simple tests of ShadowHist class - test generator.
4111@@ -97,8 +177,12 @@
4112 yield nt.assert_equal,s.get(2),'world'
4113
4114 shutil.rmtree(tfile)
4115+
4116
4117-@dec.skipif_not_numpy
4118+# XXX failing for now, until we get clearcmd out of quarantine. But we should
4119+# fix this and revert the skip to happen only if numpy is not around.
4120+#@dec.skipif_not_numpy
4121+@dec.skipknownfailure
4122 def test_numpy_clear_array_undec():
4123 from IPython.extensions import clearcmd
4124
4125@@ -109,162 +193,8 @@
4126 yield (nt.assert_false, 'a' in _ip.user_ns)
4127
4128
4129-@dec.skip()
4130-def test_fail_dec(*a,**k):
4131- yield nt.assert_true, False
4132-
4133-@dec.skip('This one shouldn not run')
4134-def test_fail_dec2(*a,**k):
4135- yield nt.assert_true, False
4136-
4137-@dec.skipknownfailure
4138-def test_fail_dec3(*a,**k):
4139- yield nt.assert_true, False
4140-
4141-
4142-def doctest_refbug():
4143- """Very nasty problem with references held by multiple runs of a script.
4144- See: https://bugs.launchpad.net/ipython/+bug/269966
4145-
4146- In [1]: _ip.clear_main_mod_cache()
4147-
4148- In [2]: run refbug
4149-
4150- In [3]: call_f()
4151- lowercased: hello
4152-
4153- In [4]: run refbug
4154-
4155- In [5]: call_f()
4156- lowercased: hello
4157- lowercased: hello
4158- """
4159-
4160-#-----------------------------------------------------------------------------
4161-# Tests for %run
4162-#-----------------------------------------------------------------------------
4163-
4164-# %run is critical enough that it's a good idea to have a solid collection of
4165-# tests for it, some as doctests and some as normal tests.
4166-
4167-def doctest_run_ns():
4168- """Classes declared %run scripts must be instantiable afterwards.
4169-
4170- In [11]: run tclass foo
4171-
4172- In [12]: isinstance(f(),foo)
4173- Out[12]: True
4174- """
4175-
4176-
4177-def doctest_run_ns2():
4178- """Classes declared %run scripts must be instantiable afterwards.
4179-
4180- In [4]: run tclass C-first_pass
4181-
4182- In [5]: run tclass C-second_pass
4183- tclass.py: deleting object: C-first_pass
4184- """
4185-
4186-def doctest_run_builtins():
4187- """Check that %run doesn't damage __builtins__ via a doctest.
4188-
4189- This is similar to the test_run_builtins, but I want *both* forms of the
4190- test to catch any possible glitches in our testing machinery, since that
4191- modifies %run somewhat. So for this, we have both a normal test (below)
4192- and a doctest (this one).
4193-
4194- In [1]: import tempfile
4195-
4196- In [2]: bid1 = id(__builtins__)
4197-
4198- In [3]: fname = tempfile.mkstemp()[1]
4199-
4200- In [3]: f = open(fname,'w')
4201-
4202- In [4]: f.write('pass\\n')
4203-
4204- In [5]: f.flush()
4205-
4206- In [6]: print type(__builtins__)
4207- <type 'module'>
4208-
4209- In [7]: %run "$fname"
4210-
4211- In [7]: f.close()
4212-
4213- In [8]: bid2 = id(__builtins__)
4214-
4215- In [9]: print type(__builtins__)
4216- <type 'module'>
4217-
4218- In [10]: bid1 == bid2
4219- Out[10]: True
4220-
4221- In [12]: try:
4222- ....: os.unlink(fname)
4223- ....: except:
4224- ....: pass
4225- ....:
4226- """
4227-
4228-# For some tests, it will be handy to organize them in a class with a common
4229-# setup that makes a temp file
4230-
4231-class TestMagicRun(object):
4232-
4233- def setup(self):
4234- """Make a valid python temp file."""
4235- fname = tempfile.mkstemp()[1]
4236- f = open(fname,'w')
4237- f.write('pass\n')
4238- f.flush()
4239- self.tmpfile = f
4240- self.fname = fname
4241-
4242- def run_tmpfile(self):
4243- _ip = get_ipython()
4244- # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
4245- # See below and ticket https://bugs.launchpad.net/bugs/366353
4246- _ip.magic('run "%s"' % self.fname)
4247-
4248- def test_builtins_id(self):
4249- """Check that %run doesn't damage __builtins__ """
4250- _ip = get_ipython()
4251- # Test that the id of __builtins__ is not modified by %run
4252- bid1 = id(_ip.user_ns['__builtins__'])
4253- self.run_tmpfile()
4254- bid2 = id(_ip.user_ns['__builtins__'])
4255- tt.assert_equals(bid1, bid2)
4256-
4257- def test_builtins_type(self):
4258- """Check that the type of __builtins__ doesn't change with %run.
4259-
4260- However, the above could pass if __builtins__ was already modified to
4261- be a dict (it should be a module) by a previous use of %run. So we
4262- also check explicitly that it really is a module:
4263- """
4264- _ip = get_ipython()
4265- self.run_tmpfile()
4266- tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
4267-
4268- def test_prompts(self):
4269- """Test that prompts correctly generate after %run"""
4270- self.run_tmpfile()
4271- _ip = get_ipython()
4272- p2 = str(_ip.outputcache.prompt2).strip()
4273- nt.assert_equals(p2[:3], '...')
4274-
4275- def teardown(self):
4276- self.tmpfile.close()
4277- try:
4278- os.unlink(self.fname)
4279- except:
4280- # On Windows, even though we close the file, we still can't delete
4281- # it. I have no clue why
4282- pass
4283-
4284 # Multiple tests for clipboard pasting
4285+@dec.parametric
4286 def test_paste():
4287 _ip = get_ipython()
4288 def paste(txt, flags='-q'):
4289@@ -286,11 +216,11 @@
4290 # Run tests with fake clipboard function
4291 user_ns.pop('x', None)
4292 paste('x=1')
4293- yield (nt.assert_equal, user_ns['x'], 1)
4294+ yield nt.assert_equal(user_ns['x'], 1)
4295
4296 user_ns.pop('x', None)
4297 paste('>>> x=2')
4298- yield (nt.assert_equal, user_ns['x'], 2)
4299+ yield nt.assert_equal(user_ns['x'], 2)
4300
4301 paste("""
4302 >>> x = [1,2,3]
4303@@ -299,14 +229,14 @@
4304 ... y.append(i**2)
4305 ...
4306 """)
4307- yield (nt.assert_equal, user_ns['x'], [1,2,3])
4308- yield (nt.assert_equal, user_ns['y'], [1,4,9])
4309+ yield nt.assert_equal(user_ns['x'], [1,2,3])
4310+ yield nt.assert_equal(user_ns['y'], [1,4,9])
4311
4312 # Now, test that paste -r works
4313 user_ns.pop('x', None)
4314- yield (nt.assert_false, 'x' in user_ns)
4315+ yield nt.assert_false('x' in user_ns)
4316 _ip.magic('paste -r')
4317- yield (nt.assert_equal, user_ns['x'], [1,2,3])
4318+ yield nt.assert_equal(user_ns['x'], [1,2,3])
4319
4320 # Also test paste echoing, by temporarily faking the writer
4321 w = StringIO()
4322@@ -320,12 +250,29 @@
4323 out = w.getvalue()
4324 finally:
4325 _ip.write = writer
4326- yield (nt.assert_equal, user_ns['a'], 100)
4327- yield (nt.assert_equal, user_ns['b'], 200)
4328- yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n")
4329+ yield nt.assert_equal(user_ns['a'], 100)
4330+ yield nt.assert_equal(user_ns['b'], 200)
4331+ yield nt.assert_equal(out, code+"\n## -- End pasted text --\n")
4332
4333 finally:
4334 # This should be in a finally clause, instead of the bare except above.
4335 # Restore original hook
4336 hooks.clipboard_get = original_clip
4337
4338+
4339+def test_time():
4340+ _ip.magic('time None')
4341+
4342+
4343+def doctest_time():
4344+ """
4345+ In [10]: %time None
4346+ CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
4347+ Wall time: 0.00 s
4348+ """
4349+
4350+def test_doctest_mode():
4351+ "Toggle doctest_mode twice, it should be a no-op and run without error"
4352+ _ip.magic('doctest_mode')
4353+ _ip.magic('doctest_mode')
4354+
4355
4356=== added file 'IPython/core/tests/test_prefilter.py'
4357--- IPython/core/tests/test_prefilter.py 1970-01-01 00:00:00 +0000
4358+++ IPython/core/tests/test_prefilter.py 2010-01-18 01:17:11 +0000
4359@@ -0,0 +1,34 @@
4360+"""Tests for input manipulation machinery."""
4361+
4362+#-----------------------------------------------------------------------------
4363+# Imports
4364+#-----------------------------------------------------------------------------
4365+import nose.tools as nt
4366+
4367+from IPython.testing import tools as tt, decorators as dec
4368+
4369+#-----------------------------------------------------------------------------
4370+# Tests
4371+#-----------------------------------------------------------------------------
4372+@dec.parametric
4373+def test_prefilter():
4374+ """Test user input conversions"""
4375+
4376+ # pairs of (raw, expected correct) input
4377+ pairs = [ ('2+2','2+2'),
4378+ ('>>> 2+2','2+2'),
4379+ ('>>> # This is a comment\n'
4380+ '... 2+2',
4381+ '# This is a comment\n'
4382+ '2+2'),
4383+ # Some IPython input
4384+ ('In [1]: 1', '1'),
4385+ ('In [2]: for i in range(5):\n'
4386+ ' ...: print i,',
4387+ 'for i in range(5):\n'
4388+ ' print i,'),
4389+ ]
4390+
4391+ ip = get_ipython()
4392+ for raw, correct in pairs:
4393+ yield nt.assert_equals(ip.prefilter(raw), correct)
4394
4395=== added file 'IPython/core/tests/test_run.py'
4396--- IPython/core/tests/test_run.py 1970-01-01 00:00:00 +0000
4397+++ IPython/core/tests/test_run.py 2010-01-18 01:17:11 +0000
4398@@ -0,0 +1,174 @@
4399+"""Tests for code execution (%run and related), which is particularly tricky.
4400+
4401+Because of how %run manages namespaces, and the fact that we are trying here to
4402+verify subtle object deletion and reference counting issues, the %run tests
4403+will be kept in this separate file. This makes it easier to aggregate in one
4404+place the tricks needed to handle it; most other magics are much easier to test
4405+and we do so in a common test_magic file.
4406+"""
4407+from __future__ import absolute_import
4408+
4409+#-----------------------------------------------------------------------------
4410+# Imports
4411+#-----------------------------------------------------------------------------
4412+
4413+# stdlib
4414+import os
4415+import sys
4416+import tempfile
4417+
4418+# third-party
4419+import nose.tools as nt
4420+
4421+# our own
4422+from IPython.utils.platutils import find_cmd
4423+from IPython.utils import genutils
4424+from IPython.testing import decorators as dec
4425+from IPython.testing import tools as tt
4426+
4427+#-----------------------------------------------------------------------------
4428+# Test functions begin
4429+#-----------------------------------------------------------------------------
4430+
4431+def doctest_refbug():
4432+ """Very nasty problem with references held by multiple runs of a script.
4433+ See: https://bugs.launchpad.net/ipython/+bug/269966
4434+
4435+ In [1]: _ip.clear_main_mod_cache()
4436+ # random
4437+
4438+ In [2]: %run refbug
4439+
4440+ In [3]: call_f()
4441+ lowercased: hello
4442+
4443+ In [4]: %run refbug
4444+
4445+ In [5]: call_f()
4446+ lowercased: hello
4447+ lowercased: hello
4448+ """
4449+
4450+
4451+def doctest_run_builtins():
4452+ r"""Check that %run doesn't damage __builtins__.
4453+
4454+ In [1]: import tempfile
4455+
4456+ In [2]: bid1 = id(__builtins__)
4457+
4458+ In [3]: fname = tempfile.mkstemp('.py')[1]
4459+
4460+ In [3]: f = open(fname,'w')
4461+
4462+ In [4]: f.write('pass\n')
4463+
4464+ In [5]: f.flush()
4465+
4466+ In [6]: t1 = type(__builtins__)
4467+
4468+ In [7]: %run $fname
4469+
4470+ In [7]: f.close()
4471+
4472+ In [8]: bid2 = id(__builtins__)
4473+
4474+ In [9]: t2 = type(__builtins__)
4475+
4476+ In [10]: t1 == t2
4477+ Out[10]: True
4478+
4479+ In [10]: bid1 == bid2
4480+ Out[10]: True
4481+
4482+ In [12]: try:
4483+ ....: os.unlink(fname)
4484+ ....: except:
4485+ ....: pass
4486+ ....:
4487+ """
4488+
4489+# For some tests, it will be handy to organize them in a class with a common
4490+# setup that makes a temp file
4491+
4492+class TestMagicRunPass(tt.TempFileMixin):
4493+
4494+ def setup(self):
4495+ """Make a valid python temp file."""
4496+ self.mktmp('pass\n')
4497+
4498+ def run_tmpfile(self):
4499+ _ip = get_ipython()
4500+ # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
4501+ # See below and ticket https://bugs.launchpad.net/bugs/366353
4502+ _ip.magic('run %s' % self.fname)
4503+
4504+ def test_builtins_id(self):
4505+ """Check that %run doesn't damage __builtins__ """
4506+ _ip = get_ipython()
4507+ # Test that the id of __builtins__ is not modified by %run
4508+ bid1 = id(_ip.user_ns['__builtins__'])
4509+ self.run_tmpfile()
4510+ bid2 = id(_ip.user_ns['__builtins__'])
4511+ tt.assert_equals(bid1, bid2)
4512+
4513+ def test_builtins_type(self):
4514+ """Check that the type of __builtins__ doesn't change with %run.
4515+
4516+ However, the above could pass if __builtins__ was already modified to
4517+ be a dict (it should be a module) by a previous use of %run. So we
4518+ also check explicitly that it really is a module:
4519+ """
4520+ _ip = get_ipython()
4521+ self.run_tmpfile()
4522+ tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
4523+
4524+ def test_prompts(self):
4525+ """Test that prompts correctly generate after %run"""
4526+ self.run_tmpfile()
4527+ _ip = get_ipython()
4528+ p2 = str(_ip.outputcache.prompt2).strip()
4529+ nt.assert_equals(p2[:3], '...')
4530+
4531+
4532+class TestMagicRunSimple(tt.TempFileMixin):
4533+
4534+ def test_simpledef(self):
4535+ """Test that simple class definitions work."""
4536+ src = ("class foo: pass\n"
4537+ "def f(): return foo()")
4538+ self.mktmp(src)
4539+ _ip.magic('run %s' % self.fname)
4540+ _ip.runlines('t = isinstance(f(), foo)')
4541+ nt.assert_true(_ip.user_ns['t'])
4542+
4543+ # We have to skip these in win32 because genutils.getoutputerr() crashes,
4544+ # due to the fact that subprocess does not support close_fds when
4545+ # redirecting stdout/err. So unless someone who knows more tells us how to
4546+ # implement genutils.getoutputerr() in win32, we're stuck avoiding these.
4547+ @dec.skip_win32
4548+ def test_obj_del(self):
4549+ """Test that object's __del__ methods are called on exit."""
4550+
4551+ # This test is known to fail on win32.
4552+ # See ticket https://bugs.launchpad.net/bugs/366334
4553+ src = ("class A(object):\n"
4554+ " def __del__(self):\n"
4555+ " print 'object A deleted'\n"
4556+ "a = A()\n")
4557+ self.mktmp(src)
4558+ tt.ipexec_validate(self.fname, 'object A deleted')
4559+
4560+ @dec.skip_win32
4561+ def test_tclass(self):
4562+ mydir = os.path.dirname(__file__)
4563+ tc = os.path.join(mydir, 'tclass')
4564+ src = ("%%run '%s' C-first\n"
4565+ "%%run '%s' C-second\n") % (tc, tc)
4566+ self.mktmp(src, '.ipy')
4567+ out = """\
4568+ARGV 1-: ['C-first']
4569+ARGV 1-: ['C-second']
4570+tclass.py: deleting object: C-first
4571+"""
4572+ tt.ipexec_validate(self.fname, out)
4573
4574=== modified file 'IPython/core/ultratb.py'
4575--- IPython/core/ultratb.py 2009-08-27 22:40:52 +0000
4576+++ IPython/core/ultratb.py 2010-01-18 01:17:11 +0000
4577@@ -88,7 +88,6 @@
4578 from inspect import getsourcefile, getfile, getmodule,\
4579 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
4580
4581-
4582 # IPython's own modules
4583 # Modified pdb which doesn't damage IPython's readline handling
4584 from IPython.utils import PyColorize
4585@@ -263,7 +262,7 @@
4586
4587 _parser = PyColorize.Parser()
4588
4589-def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None):
4590+def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None):
4591 numbers_width = INDENT_SIZE - 1
4592 res = []
4593 i = lnum - index
4594@@ -313,6 +312,15 @@
4595 class TBTools:
4596 """Basic tools used by all traceback printer classes."""
4597
4598+ #: Default output stream, can be overridden at call time. A special value
4599+ #: of 'stdout' *as a string* can be given to force extraction of sys.stdout
4600+ #: at runtime. This allows testing exception printing with doctests, that
4601+ #: swap sys.stdout just at execution time.
4602+ #: Warning: be VERY careful to set this to one of the Term streams, NEVER
4603+ #: directly to sys.stdout/err, because under win32 the Term streams come from
4604+ #: pyreadline and know how to handle color correctly, whie stdout/err don't.
4605+ out_stream = Term.cerr
4606+
4607 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
4608 # Whether to call the interactive pdb debugger after printing
4609 # tracebacks or not
4610@@ -376,16 +384,31 @@
4611
4612 def __call__(self, etype, value, elist):
4613 Term.cout.flush()
4614- print >> Term.cerr, self.text(etype,value,elist)
4615- Term.cerr.flush()
4616-
4617- def text(self,etype, value, elist,context=5):
4618- """Return a color formatted string with the traceback info."""
4619+ Term.cerr.writeln(self.text(etype,value,elist))
4620+
4621+ def text(self, etype, value, elist, context=5):
4622+ """Return a color formatted string with the traceback info.
4623+
4624+ Parameters
4625+ ----------
4626+ etype : exception type
4627+ Type of the exception raised.
4628+
4629+ value : object
4630+ Data stored in the exception
4631+
4632+ elist : list
4633+ List of frames, see class docstring for details.
4634+
4635+ Returns
4636+ -------
4637+ String with formatted exception.
4638+ """
4639
4640 Colors = self.Colors
4641- out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
4642+ out_string = []
4643 if elist:
4644- out_string.append('Traceback %s(most recent call last)%s:' % \
4645+ out_string.append('Traceback %s(most recent call last)%s:' %
4646 (Colors.normalEm, Colors.Normal) + '\n')
4647 out_string.extend(self._format_list(elist))
4648 lines = self._format_exception_only(etype, value)
4649@@ -492,15 +515,29 @@
4650 else:
4651 list.append('%s\n' % str(stype))
4652
4653- # vds:>>
4654+ # sync with user hooks
4655 if have_filedata:
4656 ipinst = ipapi.get()
4657 if ipinst is not None:
4658 ipinst.hooks.synchronize_with_editor(filename, lineno, 0)
4659- # vds:<<
4660
4661 return list
4662
4663+ def show_exception_only(self, etype, value):
4664+ """Only print the exception type and message, without a traceback.
4665+
4666+ Parameters
4667+ ----------
4668+ etype : exception type
4669+ value : exception value
4670+ """
4671+ # This method needs to use __call__ from *this* class, not the one from
4672+ # a subclass whose signature or behavior may be different
4673+ Term.cout.flush()
4674+ ostream = sys.stdout if self.out_stream == 'stdout' else Term.cerr
4675+ ostream.write(ListTB.text(self, etype, value, []))
4676+ ostream.flush()
4677+
4678 def _some_str(self, value):
4679 # Lifted from traceback.py
4680 try:
4681@@ -781,8 +818,8 @@
4682 frames.append(level)
4683 else:
4684 frames.append('%s%s' % (level,''.join(
4685- _formatTracebackLines(lnum,index,lines,Colors,lvals,
4686- col_scheme))))
4687+ _format_traceback_lines(lnum,index,lines,Colors,lvals,
4688+ col_scheme))))
4689
4690 # Get (safely) a string form of the exception info
4691 try:
4692@@ -854,11 +891,11 @@
4693 with display_trap:
4694 self.pdb.reset()
4695 # Find the right frame so we don't pop up inside ipython itself
4696- if hasattr(self,'tb'):
4697+ if hasattr(self,'tb') and self.tb is not None:
4698 etb = self.tb
4699 else:
4700 etb = self.tb = sys.last_traceback
4701- while self.tb.tb_next is not None:
4702+ while self.tb is not None and self.tb.tb_next is not None:
4703 self.tb = self.tb.tb_next
4704 if etb and etb.tb_next:
4705 etb = etb.tb_next
4706@@ -872,8 +909,7 @@
4707 (etype, evalue, etb) = info or sys.exc_info()
4708 self.tb = etb
4709 Term.cout.flush()
4710- print >> Term.cerr, self.text(etype, evalue, etb)
4711- Term.cerr.flush()
4712+ Term.cerr.writeln(self.text(etype, evalue, etb))
4713
4714 # Changed so an instance can just be called as VerboseTB_inst() and print
4715 # out the right info on its own.
4716@@ -980,6 +1016,7 @@
4717 except:
4718 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
4719 """
4720+
4721 def __call__(self,etype=None,evalue=None,etb=None,
4722 out=None,tb_offset=None):
4723 """Print out a formatted exception traceback.
4724@@ -990,16 +1027,18 @@
4725 - tb_offset: the number of frames to skip over in the stack, on a
4726 per-call basis (this overrides temporarily the instance's tb_offset
4727 given at initialization time. """
4728-
4729+
4730 if out is None:
4731- out = Term.cerr
4732+ out = sys.stdout if self.out_stream=='stdout' else self.out_stream
4733 Term.cout.flush()
4734 if tb_offset is not None:
4735 tb_offset, self.tb_offset = self.tb_offset, tb_offset
4736- print >> out, self.text(etype, evalue, etb)
4737+ out.write(self.text(etype, evalue, etb))
4738+ out.write('\n')
4739 self.tb_offset = tb_offset
4740 else:
4741- print >> out, self.text(etype, evalue, etb)
4742+ out.write(self.text(etype, evalue, etb))
4743+ out.write('\n')
4744 out.flush()
4745 try:
4746 self.debugger()
4747
4748=== modified file 'IPython/core/usage.py'
4749--- IPython/core/usage.py 2009-11-09 01:49:24 +0000
4750+++ IPython/core/usage.py 2010-01-18 01:17:11 +0000
4751@@ -1,338 +1,47 @@
4752 # -*- coding: utf-8 -*-
4753-#*****************************************************************************
4754-# Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
4755+"""Usage information for the main IPython applications.
4756+"""
4757+#-----------------------------------------------------------------------------
4758+# Copyright (C) 2008-2010 The IPython Development Team
4759+# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
4760 #
4761 # Distributed under the terms of the BSD License. The full license is in
4762 # the file COPYING, distributed as part of this software.
4763-#*****************************************************************************
4764+#-----------------------------------------------------------------------------
4765
4766 import sys
4767 from IPython.core import release
4768
4769-__doc__ = """
4770-IPython -- An enhanced Interactive Python
4771-=========================================
4772-
4773-A Python shell with automatic history (input and output), dynamic object
4774-introspection, easier configuration, command completion, access to the system
4775-shell and more.
4776-
4777-IPython can also be embedded in running programs. See EMBEDDING below.
4778-
4779-
4780-USAGE
4781- ipython [options] files
4782-
4783- If invoked with no options, it executes all the files listed in
4784- sequence and drops you into the interpreter while still acknowledging
4785- any options you may have set in your ipythonrc file. This behavior is
4786- different from standard Python, which when called as python -i will
4787- only execute one file and will ignore your configuration setup.
4788-
4789- Please note that some of the configuration options are not available at
4790- the command line, simply because they are not practical here. Look into
4791- your ipythonrc configuration file for details on those. This file
4792- typically installed in the $HOME/.ipython directory.
4793-
4794- For Windows users, $HOME resolves to C:\\Documents and
4795- Settings\\YourUserName in most instances, and _ipython is used instead
4796- of .ipython, since some Win32 programs have problems with dotted names
4797- in directories.
4798-
4799- In the rest of this text, we will refer to this directory as
4800- IPYTHON_DIR.
4801-
4802-REGULAR OPTIONS
4803- After the above threading options have been given, regular options can
4804- follow in any order. All options can be abbreviated to their shortest
4805- non-ambiguous form and are case-sensitive. One or two dashes can be
4806- used. Some options have an alternate short form, indicated after a |.
4807-
4808- Most options can also be set from your ipythonrc configuration file.
4809- See the provided examples for assistance. Options given on the comman-
4810- dline override the values set in the ipythonrc file.
4811-
4812- All options with a [no] prepended can be specified in negated form
4813- (using -nooption instead of -option) to turn the feature off.
4814-
4815- -h, --help
4816- Show summary of options.
4817-
4818- -autocall <val>
4819- Make IPython automatically call any callable object even if you
4820- didn't type explicit parentheses. For example, 'str 43' becomes
4821- 'str(43)' automatically. The value can be '0' to disable the
4822- feature, '1' for 'smart' autocall, where it is not applied if
4823- there are no more arguments on the line, and '2' for 'full'
4824- autocall, where all callable objects are automatically called
4825- (even if no arguments are present). The default is '1'.
4826-
4827- -[no]autoindent
4828- Turn automatic indentation on/off.
4829-
4830- -[no]automagic
4831- Make magic commands automatic (without needing their first char-
4832- acter to be %). Type %magic at the IPython prompt for more
4833- information.
4834-
4835- -[no]autoedit_syntax
4836- When a syntax error occurs after editing a file, automatically
4837- open the file to the trouble causing line for convenient fixing.
4838-
4839- -[no]banner
4840- Print the intial information banner (default on).
4841-
4842- -c <command>
4843- Execute the given command string, and set sys.argv to ['c'].
4844- This is similar to the -c option in the normal Python inter-
4845- preter.
4846-
4847- -cache_size|cs <n>
4848- Size of the output cache (maximum number of entries to hold in
4849- memory). The default is 1000, you can change it permanently in
4850- your config file. Setting it to 0 completely disables the
4851- caching system, and the minimum value accepted is 20 (if you
4852- provide a value less than 20, it is reset to 0 and a warning is
4853- issued). This limit is defined because otherwise you'll spend
4854- more time re-flushing a too small cache than working.
4855-
4856- -classic|cl
4857- Gives IPython a similar feel to the classic Python prompt.
4858-
4859- -colors <scheme>
4860- Color scheme for prompts and exception reporting. Currently
4861- implemented: NoColor, Linux, and LightBG.
4862-
4863- -[no]color_info
4864- IPython can display information about objects via a set of func-
4865- tions, and optionally can use colors for this, syntax highlight-
4866- ing source code and various other elements. However, because
4867- this information is passed through a pager (like 'less') and
4868- many pagers get confused with color codes, this option is off by
4869- default. You can test it and turn it on permanently in your
4870- ipythonrc file if it works for you. As a reference, the 'less'
4871- pager supplied with Mandrake 8.2 works ok, but that in RedHat
4872- 7.2 doesn't.
4873-
4874- Test it and turn it on permanently if it works with your system.
4875- The magic function @color_info allows you to toggle this inter-
4876- actively for testing.
4877-
4878- -[no]confirm_exit
4879- Set to confirm when you try to exit IPython with an EOF (Con-
4880- trol-D in Unix, Control-Z/Enter in Windows). Note that using the
4881- magic functions @Exit or @Quit you can force a direct exit,
4882- bypassing any confirmation.
4883-
4884- -[no]debug
4885- Show information about the loading process. Very useful to pin
4886- down problems with your configuration files or to get details
4887- about session restores.
4888-
4889- -[no]deep_reload
4890- IPython can use the deep_reload module which reloads changes in
4891- modules recursively (it replaces the reload() function, so you
4892- don't need to change anything to use it). deep_reload() forces a
4893- full reload of modules whose code may have changed, which the
4894- default reload() function does not.
4895-
4896- When deep_reload is off, IPython will use the normal reload(),
4897- but deep_reload will still be available as dreload(). This fea-
4898- ture is off by default [which means that you have both normal
4899- reload() and dreload()].
4900-
4901- -editor <name>
4902- Which editor to use with the @edit command. By default, IPython
4903- will honor your EDITOR environment variable (if not set, vi is
4904- the Unix default and notepad the Windows one). Since this editor
4905- is invoked on the fly by IPython and is meant for editing small
4906- code snippets, you may want to use a small, lightweight editor
4907- here (in case your default EDITOR is something like Emacs).
4908-
4909- -ipythondir <name>
4910- The name of your IPython configuration directory IPYTHON_DIR.
4911- This can also be specified through the environment variable
4912- IPYTHON_DIR.
4913-
4914- -log|l Generate a log file of all input. The file is named
4915- ipython_log.py in your current directory (which prevents logs
4916- from multiple IPython sessions from trampling each other). You
4917- can use this to later restore a session by loading your logfile
4918- as a file to be executed with option -logplay (see below).
4919-
4920- -logfile|lf
4921- Specify the name of your logfile.
4922-
4923- -logplay|lp
4924- Replay a previous log. For restoring a session as close as pos-
4925- sible to the state you left it in, use this option (don't just
4926- run the logfile). With -logplay, IPython will try to reconstruct
4927- the previous working environment in full, not just execute the
4928- commands in the logfile.
4929- When a session is restored, logging is automatically turned on
4930- again with the name of the logfile it was invoked with (it is
4931- read from the log header). So once you've turned logging on for
4932- a session, you can quit IPython and reload it as many times as
4933- you want and it will continue to log its history and restore
4934- from the beginning every time.
4935-
4936- Caveats: there are limitations in this option. The history vari-
4937- ables _i*,_* and _dh don't get restored properly. In the future
4938- we will try to implement full session saving by writing and
4939- retrieving a failed because of inherent limitations of Python's
4940- Pickle module, so this may have to wait.
4941-
4942- -[no]messages
4943- Print messages which IPython collects about its startup process
4944- (default on).
4945-
4946- -[no]pdb
4947- Automatically call the pdb debugger after every uncaught excep-
4948- tion. If you are used to debugging using pdb, this puts you
4949- automatically inside of it after any call (either in IPython or
4950- in code called by it) which triggers an exception which goes
4951- uncaught.
4952-
4953- -[no]pprint
4954- IPython can optionally use the pprint (pretty printer) module
4955- for displaying results. pprint tends to give a nicer display of
4956- nested data structures. If you like it, you can turn it on per-
4957- manently in your config file (default off).
4958-
4959- -profile|p <name>
4960- Assume that your config file is ipythonrc-<name> (looks in cur-
4961- rent dir first, then in IPYTHON_DIR). This is a quick way to keep
4962- and load multiple config files for different tasks, especially
4963- if you use the include option of config files. You can keep a
4964- basic IPYTHON_DIR/ipythonrc file and then have other 'profiles'
4965- which include this one and load extra things for particular
4966- tasks. For example:
4967-
4968- 1) $HOME/.ipython/ipythonrc : load basic things you always want.
4969- 2) $HOME/.ipython/ipythonrc-math : load (1) and basic math-
4970- related modules.
4971- 3) $HOME/.ipython/ipythonrc-numeric : load (1) and Numeric and
4972- plotting modules.
4973-
4974- Since it is possible to create an endless loop by having circu-
4975- lar file inclusions, IPython will stop if it reaches 15 recur-
4976- sive inclusions.
4977-
4978- -prompt_in1|pi1 <string>
4979- Specify the string used for input prompts. Note that if you are
4980- using numbered prompts, the number is represented with a '\#' in
4981- the string. Don't forget to quote strings with spaces embedded
4982- in them. Default: 'In [\#]: '.
4983-
4984- Most bash-like escapes can be used to customize IPython's
4985- prompts, as well as a few additional ones which are IPython-spe-
4986- cific. All valid prompt escapes are described in detail in the
4987- Customization section of the IPython HTML/PDF manual.
4988-
4989- -prompt_in2|pi2 <string>
4990- Similar to the previous option, but used for the continuation
4991- prompts. The special sequence '\D' is similar to '\#', but with
4992- all digits replaced dots (so you can have your continuation
4993- prompt aligned with your input prompt). Default: ' .\D.: '
4994- (note three spaces at the start for alignment with 'In [\#]').
4995-
4996- -prompt_out|po <string>
4997- String used for output prompts, also uses numbers like
4998- prompt_in1. Default: 'Out[\#]:'.
4999-
5000- -quick Start in bare bones mode (no config file loaded).
The diff has been truncated for viewing.

Subscribers

People subscribed via source and target branches