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
=== modified file 'IPython/__init__.py' (properties changed: -x to +x)
--- IPython/__init__.py 2009-08-26 20:54:07 +0000
+++ IPython/__init__.py 2010-01-18 01:17:11 +0000
@@ -16,18 +16,17 @@
16#-----------------------------------------------------------------------------16#-----------------------------------------------------------------------------
17# Imports17# Imports
18#-----------------------------------------------------------------------------18#-----------------------------------------------------------------------------
19from __future__ import absolute_import
1920
20import os21import os
21import sys22import sys
22from IPython.core import release
2323
24#-----------------------------------------------------------------------------24#-----------------------------------------------------------------------------
25# Setup everything25# Setup everything
26#-----------------------------------------------------------------------------26#-----------------------------------------------------------------------------
2727
2828if sys.version[0:3] < '2.5':
29if sys.version[0:3] < '2.4':29 raise ImportError('Python Version 2.5 or above is required for IPython.')
30 raise ImportError('Python Version 2.4 or above is required for IPython.')
3130
3231
33# Make it easy to import extensions - they are always directly on pythonpath.32# Make it easy to import extensions - they are always directly on pythonpath.
@@ -39,11 +38,16 @@
39#-----------------------------------------------------------------------------38#-----------------------------------------------------------------------------
4039
41# In some cases, these are causing circular imports.40# In some cases, these are causing circular imports.
42from IPython.core.iplib import InteractiveShell41from .config.loader import Config
43from IPython.core.embed import embed42from .core import release
44from IPython.core.error import TryNext43from .core.application import Application
44from .core.ipapp import IPythonApp
45from .core.embed import embed
46from .core.error import TryNext
47from .core.iplib import InteractiveShell
48from .testing import test
4549
46from IPython.lib import (50from .lib import (
47 enable_wx, disable_wx,51 enable_wx, disable_wx,
48 enable_gtk, disable_gtk,52 enable_gtk, disable_gtk,
49 enable_qt4, disable_qt4,53 enable_qt4, disable_qt4,
@@ -61,4 +65,3 @@
61__license__ = release.license65__license__ = release.license
62__version__ = release.version66__version__ = release.version
63__revision__ = release.revision67__revision__ = release.revision
64
6568
=== modified file 'IPython/config/loader.py'
--- IPython/config/loader.py 2010-01-08 22:28:30 +0000
+++ IPython/config/loader.py 2010-01-18 01:17:11 +0000
@@ -1,10 +1,10 @@
1#!/usr/bin/env python1# coding: utf-8
2# encoding: utf-8
3"""A simple configuration system.2"""A simple configuration system.
43
5Authors:4Authors
65-------
7* Brian Granger6* Brian Granger
7* Fernando Perez
8"""8"""
99
10#-----------------------------------------------------------------------------10#-----------------------------------------------------------------------------
@@ -37,7 +37,25 @@
37class ConfigLoaderError(ConfigError):37class ConfigLoaderError(ConfigError):
38 pass38 pass
3939
40#-----------------------------------------------------------------------------
41# Argparse fix
42#-----------------------------------------------------------------------------
43# Unfortunately argparse by default prints help messages to stderr instead of
44# stdout. This makes it annoying to capture long help screens at the command
45# line, since one must know how to pipe stderr, which many users don't know how
46# to do. So we override the print_help method with one that defaults to
47# stdout and use our class instead.
4048
49class ArgumentParser(argparse.ArgumentParser):
50 """Simple argparse subclass that prints help to stdout by default."""
51
52 def print_help(self, file=None):
53 if file is None:
54 file = sys.stdout
55 return super(ArgumentParser, self).print_help(file)
56
57 print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__
58
41#-----------------------------------------------------------------------------59#-----------------------------------------------------------------------------
42# Config class for holding config information60# Config class for holding config information
43#-----------------------------------------------------------------------------61#-----------------------------------------------------------------------------
@@ -279,22 +297,50 @@
279297
280298
281class ArgParseConfigLoader(CommandLineConfigLoader):299class ArgParseConfigLoader(CommandLineConfigLoader):
300 #: Global default for arguments (see argparse docs for details)
301 argument_default = NoConfigDefault
282 302
283 # arguments = [(('-f','--file'),dict(type=str,dest='file'))]303 def __init__(self, argv=None, arguments=(), *args, **kw):
284 arguments = ()
285
286 def __init__(self, *args, **kw):
287 """Create a config loader for use with argparse.304 """Create a config loader for use with argparse.
288305
289 The args and kwargs arguments here are passed onto the constructor306 With the exception of ``argv`` and ``arguments``, other args and kwargs
290 of :class:`argparse.ArgumentParser`.307 arguments here are passed onto the constructor of
308 :class:`argparse.ArgumentParser`.
309
310 Parameters
311 ----------
312
313 argv : optional, list
314 If given, used to read command-line arguments from, otherwise
315 sys.argv[1:] is used.
316
317 arguments : optional, tuple
318 Description of valid command-line arguments, to be called in sequence
319 with parser.add_argument() to configure the parser.
291 """320 """
292 super(CommandLineConfigLoader, self).__init__()321 super(CommandLineConfigLoader, self).__init__()
322 if argv == None:
323 argv = sys.argv[1:]
324 self.argv = argv
325 self.arguments = arguments
293 self.args = args326 self.args = args
294 self.kw = kw327 kwargs = dict(argument_default=self.argument_default)
328 kwargs.update(kw)
329 self.kw = kwargs
295330
296 def load_config(self, args=None):331 def load_config(self, args=None):
297 """Parse command line arguments and return as a Struct."""332 """Parse command line arguments and return as a Struct.
333
334 Parameters
335 ----------
336
337 args : optional, list
338 If given, a list with the structure of sys.argv[1:] to parse arguments
339 from. If not given, the instance's self.argv attribute (given at
340 construction time) is used."""
341
342 if args is None:
343 args = self.argv
298 self._create_parser()344 self._create_parser()
299 self._parse_args(args)345 self._parse_args(args)
300 self._convert_to_config()346 self._convert_to_config()
@@ -307,25 +353,21 @@
307 return []353 return []
308354
309 def _create_parser(self):355 def _create_parser(self):
310 self.parser = argparse.ArgumentParser(*self.args, **self.kw)356 self.parser = ArgumentParser(*self.args, **self.kw)
311 self._add_arguments()357 self._add_arguments()
312 self._add_other_arguments()358 self._add_other_arguments()
313359
314 def _add_other_arguments(self):
315 pass
316
317 def _add_arguments(self):360 def _add_arguments(self):
318 for argument in self.arguments:361 for argument in self.arguments:
319 if not argument[1].has_key('default'):
320 argument[1]['default'] = NoConfigDefault
321 self.parser.add_argument(*argument[0],**argument[1])362 self.parser.add_argument(*argument[0],**argument[1])
322363
323 def _parse_args(self, args=None):364 def _add_other_arguments(self):
324 """self.parser->self.parsed_data"""365 """Meant for subclasses to add their own arguments."""
325 if args is None:366 pass
326 self.parsed_data, self.extra_args = self.parser.parse_known_args()367
327 else:368 def _parse_args(self, args):
328 self.parsed_data, self.extra_args = self.parser.parse_known_args(args)369 """self.parser->self.parsed_data"""
370 self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
329371
330 def _convert_to_config(self):372 def _convert_to_config(self):
331 """self.parsed_data->self.config"""373 """self.parsed_data->self.config"""
@@ -333,4 +375,3 @@
333 if v is not NoConfigDefault:375 if v is not NoConfigDefault:
334 exec_str = 'self.config.' + k + '= v'376 exec_str = 'self.config.' + k + '= v'
335 exec exec_str in locals(), globals()377 exec exec_str in locals(), globals()
336
337378
=== modified file 'IPython/config/tests/test_loader.py' (properties changed: -x to +x)
--- IPython/config/tests/test_loader.py 2009-09-12 15:58:55 +0000
+++ IPython/config/tests/test_loader.py 2010-01-18 01:17:11 +0000
@@ -37,17 +37,18 @@
3737
3838
39pyfile = """39pyfile = """
40a = 1040c = get_config()
41b = 2041c.a = 10
42Foo.Bar.value = 1042c.b = 20
43Foo.Bam.value = range(10)43c.Foo.Bar.value = 10
44D.C.value = 'hi there'44c.Foo.Bam.value = range(10)
45c.D.C.value = 'hi there'
45"""46"""
4647
47class TestPyFileCL(TestCase):48class TestPyFileCL(TestCase):
4849
49 def test_basic(self):50 def test_basic(self):
50 fd, fname = mkstemp()51 fd, fname = mkstemp('.py')
51 f = os.fdopen(fd, 'w')52 f = os.fdopen(fd, 'w')
52 f.write(pyfile)53 f.write(pyfile)
53 f.close()54 f.close()
@@ -65,15 +66,13 @@
6566
66 def test_basic(self):67 def test_basic(self):
6768
68 class MyLoader(ArgParseConfigLoader):69 arguments = (
69 arguments = (
70 (('-f','--foo'), dict(dest='Global.foo', type=str)),70 (('-f','--foo'), dict(dest='Global.foo', type=str)),
71 (('-b',), dict(dest='MyClass.bar', type=int)),71 (('-b',), dict(dest='MyClass.bar', type=int)),
72 (('-n',), dict(dest='n', action='store_true')),72 (('-n',), dict(dest='n', action='store_true')),
73 (('Global.bam',), dict(type=str))73 (('Global.bam',), dict(type=str))
74 )74 )
7575 cl = ArgParseConfigLoader(arguments=arguments)
76 cl = MyLoader()
77 config = cl.load_config('-f hi -b 10 -n wow'.split())76 config = cl.load_config('-f hi -b 10 -n wow'.split())
78 self.assertEquals(config.Global.foo, 'hi')77 self.assertEquals(config.Global.foo, 'hi')
79 self.assertEquals(config.MyClass.bar, 10)78 self.assertEquals(config.MyClass.bar, 10)
8079
=== modified file 'IPython/core/application.py'
--- IPython/core/application.py 2009-11-12 00:41:52 +0000
+++ IPython/core/application.py 2010-01-18 01:17:11 +0000
@@ -1,4 +1,3 @@
1#!/usr/bin/env python
2# encoding: utf-81# encoding: utf-8
3"""2"""
4An application for IPython.3An application for IPython.
@@ -33,64 +32,125 @@
33import os32import os
34import sys33import sys
3534
36from IPython.core import release35from IPython.core import release, crashhandler
37from IPython.utils.genutils import get_ipython_dir36from IPython.utils.genutils import get_ipython_dir, get_ipython_package_dir
38from IPython.config.loader import (37from IPython.config.loader import (
39 PyFileConfigLoader,38 PyFileConfigLoader,
40 ArgParseConfigLoader,39 ArgParseConfigLoader,
41 Config,40 Config,
42 NoConfigDefault
43)41)
4442
45#-----------------------------------------------------------------------------43#-----------------------------------------------------------------------------
46# Classes and functions44# Classes and functions
47#-----------------------------------------------------------------------------45#-----------------------------------------------------------------------------
4846
49
50class BaseAppArgParseConfigLoader(ArgParseConfigLoader):
51 """Default command line options for IPython based applications."""
52
53 def _add_other_arguments(self):
54 self.parser.add_argument('--ipython-dir',
55 dest='Global.ipython_dir',type=unicode,
56 help='Set to override default location of Global.ipython_dir.',
57 default=NoConfigDefault,
58 metavar='Global.ipython_dir')
59 self.parser.add_argument('-p', '--profile',
60 dest='Global.profile',type=unicode,
61 help='The string name of the ipython profile to be used.',
62 default=NoConfigDefault,
63 metavar='Global.profile')
64 self.parser.add_argument('--log-level',
65 dest="Global.log_level",type=int,
66 help='Set the log level (0,10,20,30,40,50). Default is 30.',
67 default=NoConfigDefault,
68 metavar='Global.log_level')
69 self.parser.add_argument('--config-file',
70 dest='Global.config_file',type=unicode,
71 help='Set the config file name to override default.',
72 default=NoConfigDefault,
73 metavar='Global.config_file')
74
75
76class ApplicationError(Exception):47class ApplicationError(Exception):
77 pass48 pass
7849
7950
51app_cl_args = (
52 (('--ipython-dir', ), dict(
53 dest='Global.ipython_dir',type=unicode,
54 help=
55 """Set to override default location of the IPython directory
56 IPYTHON_DIR, stored as Global.ipython_dir. This can also be specified
57 through the environment variable IPYTHON_DIR.""",
58 metavar='Global.ipython_dir') ),
59 (('-p', '--profile',), dict(
60 dest='Global.profile',type=unicode,
61 help=
62 """The string name of the ipython profile to be used. Assume that your
63 config file is ipython_config-<name>.py (looks in current dir first,
64 then in IPYTHON_DIR). This is a quick way to keep and load multiple
65 config files for different tasks, especially if include your basic one
66 in your more specialized ones. You can keep a basic
67 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
68 include this one and load extra things for particular tasks.""",
69 metavar='Global.profile') ),
70 (('--log-level',), dict(
71 dest="Global.log_level",type=int,
72 help='Set the log level (0,10,20,30,40,50). Default is 30.',
73 metavar='Global.log_level')),
74 (('--config-file',), dict(
75 dest='Global.config_file',type=unicode,
76 help=
77 """Set the config file name to override default. Normally IPython
78 loads ipython_config.py (from current directory) or
79 IPYTHON_DIR/ipython_config.py. If the loading of your config file
80 fails, IPython starts with a bare bones configuration (no modules
81 loaded at all).""",
82 metavar='Global.config_file')),
83 )
84
80class Application(object):85class Application(object):
81 """Load a config, construct components and set them running."""86 """Load a config, construct components and set them running.
87
88 The configuration of an application can be done via four different Config
89 objects, which are loaded and ultimately merged into a single one used from
90 that point on by the app. These are:
91
92 1. default_config: internal defaults, implemented in code.
93 2. file_config: read from the filesystem.
94 3. command_line_config: read from the system's command line flags.
95 4. constructor_config: passed parametrically to the constructor.
96
97 During initialization, 3 is actually read before 2, since at the
98 command-line one may override the location of the file to be read. But the
99 above is the order in which the merge is made.
100
101 There is a final config object can be created and passed to the
102 constructor: override_config. If it exists, this completely overrides the
103 configs 2-4 above (the default is still used to ensure that all needed
104 fields at least are created). This makes it easier to create
105 parametrically (e.g. in testing or sphinx plugins) objects with a known
106 configuration, that are unaffected by whatever arguments may be present in
107 sys.argv or files in the user's various directories.
108 """
82109
83 name = u'ipython'110 name = u'ipython'
84 description = 'IPython: an enhanced interactive Python shell.'111 description = 'IPython: an enhanced interactive Python shell.'
112 #: usage message printed by argparse. If None, auto-generate
113 usage = None
85 config_file_name = u'ipython_config.py'114 config_file_name = u'ipython_config.py'
115 #: Track the default and actual separately because some messages are
116 #: only printed if we aren't using the default.
117 default_config_file_name = config_file_name
86 default_log_level = logging.WARN118 default_log_level = logging.WARN
87119 #: Set by --profile option
88 def __init__(self):120 profile_name = None
89 self._exiting = False121 #: User's ipython directory, typically ~/.ipython/
122 ipython_dir = None
123 #: internal defaults, implemented in code.
124 default_config = None
125 #: read from the filesystem
126 file_config = None
127 #: read from the system's command line flags
128 command_line_config = None
129 #: passed parametrically to the constructor.
130 constructor_config = None
131 #: final override, if given supercedes file/command/constructor configs
132 override_config = None
133 #: A reference to the argv to be used (typically ends up being sys.argv[1:])
134 argv = None
135 #: Default command line arguments. Subclasses should create a new tuple
136 #: that *includes* these.
137 cl_arguments = app_cl_args
138
139 #: extra arguments computed by the command-line loader
140 extra_args = None
141
142 # Private attributes
143 _exiting = False
144 _initialized = False
145
146 # Class choices for things that will be instantiated at runtime.
147 _CrashHandler = crashhandler.CrashHandler
148
149 def __init__(self, argv=None, constructor_config=None, override_config=None):
150 self.argv = sys.argv[1:] if argv is None else argv
151 self.constructor_config = constructor_config
152 self.override_config = override_config
90 self.init_logger()153 self.init_logger()
91 # Track the default and actual separately because some messages are
92 # only printed if we aren't using the default.
93 self.default_config_file_name = self.config_file_name
94154
95 def init_logger(self):155 def init_logger(self):
96 self.log = logging.getLogger(self.__class__.__name__)156 self.log = logging.getLogger(self.__class__.__name__)
@@ -109,36 +169,80 @@
109169
110 log_level = property(_get_log_level, _set_log_level)170 log_level = property(_get_log_level, _set_log_level)
111171
112 def start(self):172 def initialize(self):
113 """Start the application."""173 """Initialize the application.
114 self.attempt(self.create_default_config)174
175 Loads all configuration information and sets all application state, but
176 does not start any relevant processing (typically some kind of event
177 loop).
178
179 Once this method has been called, the application is flagged as
180 initialized and the method becomes a no-op."""
181
182 if self._initialized:
183 return
184
185 # The first part is protected with an 'attempt' wrapper, that will log
186 # failures with the basic system traceback machinery. Once our crash
187 # handler is in place, we can let any subsequent exception propagate,
188 # as our handler will log it with much better detail than the default.
189 self.attempt(self.create_crash_handler)
190
191 # Configuration phase
192 # Default config (internally hardwired in application code)
193 self.create_default_config()
115 self.log_default_config()194 self.log_default_config()
116 self.set_default_config_log_level()195 self.set_default_config_log_level()
117 self.attempt(self.pre_load_command_line_config)196
118 self.attempt(self.load_command_line_config, action='abort')197 if self.override_config is None:
119 self.set_command_line_config_log_level()198 # Command-line config
120 self.attempt(self.post_load_command_line_config)199 self.pre_load_command_line_config()
121 self.log_command_line_config()200 self.load_command_line_config()
122 self.attempt(self.find_ipython_dir)201 self.set_command_line_config_log_level()
123 self.attempt(self.find_resources)202 self.post_load_command_line_config()
124 self.attempt(self.find_config_file_name)203 self.log_command_line_config()
125 self.attempt(self.find_config_file_paths)204
126 self.attempt(self.pre_load_file_config)205 # Find resources needed for filesystem access, using information from
127 self.attempt(self.load_file_config)206 # the above two
128 self.set_file_config_log_level()207 self.find_ipython_dir()
129 self.attempt(self.post_load_file_config)208 self.find_resources()
130 self.log_file_config()209 self.find_config_file_name()
131 self.attempt(self.merge_configs)210 self.find_config_file_paths()
211
212 if self.override_config is None:
213 # File-based config
214 self.pre_load_file_config()
215 self.load_file_config()
216 self.set_file_config_log_level()
217 self.post_load_file_config()
218 self.log_file_config()
219
220 # Merge all config objects into a single one the app can then use
221 self.merge_configs()
132 self.log_master_config()222 self.log_master_config()
133 self.attempt(self.pre_construct)223
134 self.attempt(self.construct)224 # Construction phase
135 self.attempt(self.post_construct)225 self.pre_construct()
136 self.attempt(self.start_app)226 self.construct()
227 self.post_construct()
228
229 # Done, flag as such and
230 self._initialized = True
231
232 def start(self):
233 """Start the application."""
234 self.initialize()
235 self.start_app()
137236
138 #-------------------------------------------------------------------------237 #-------------------------------------------------------------------------
139 # Various stages of Application creation238 # Various stages of Application creation
140 #-------------------------------------------------------------------------239 #-------------------------------------------------------------------------
141240
241 def create_crash_handler(self):
242 """Create a crash handler, typically setting sys.excepthook to it."""
243 self.crash_handler = self._CrashHandler(self, self.name)
244 sys.excepthook = self.crash_handler
245
142 def create_default_config(self):246 def create_default_config(self):
143 """Create defaults that can't be set elsewhere.247 """Create defaults that can't be set elsewhere.
144248
@@ -148,9 +252,10 @@
148 we set them here. The Global section is for variables like this that252 we set them here. The Global section is for variables like this that
149 don't belong to a particular component.253 don't belong to a particular component.
150 """254 """
151 self.default_config = Config()255 c = Config()
152 self.default_config.Global.ipython_dir = get_ipython_dir()256 c.Global.ipython_dir = get_ipython_dir()
153 self.default_config.Global.log_level = self.log_level257 c.Global.log_level = self.log_level
258 self.default_config = c
154259
155 def log_default_config(self):260 def log_default_config(self):
156 self.log.debug('Default config loaded:')261 self.log.debug('Default config loaded:')
@@ -165,10 +270,11 @@
165270
166 def create_command_line_config(self):271 def create_command_line_config(self):
167 """Create and return a command line config loader."""272 """Create and return a command line config loader."""
168 return BaseAppArgParseConfigLoader(273 return ArgParseConfigLoader(self.argv, self.cl_arguments,
169 description=self.description, 274 description=self.description,
170 version=release.version275 version=release.version,
171 )276 usage=self.usage,
277 )
172278
173 def pre_load_command_line_config(self):279 def pre_load_command_line_config(self):
174 """Do actions just before loading the command line config."""280 """Do actions just before loading the command line config."""
@@ -197,10 +303,10 @@
197 def find_ipython_dir(self):303 def find_ipython_dir(self):
198 """Set the IPython directory.304 """Set the IPython directory.
199305
200 This sets ``self.ipython_dir``, but the actual value that is passed306 This sets ``self.ipython_dir``, but the actual value that is passed to
201 to the application is kept in either ``self.default_config`` or307 the application is kept in either ``self.default_config`` or
202 ``self.command_line_config``. This also adds ``self.ipython_dir`` to308 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
203 ``sys.path`` so config files there can be references by other config309 ``sys.path`` so config files there can be referenced by other config
204 files.310 files.
205 """311 """
206312
@@ -230,8 +336,7 @@
230 config file are set in :meth:`find_config_file_paths` and then passed336 config file are set in :meth:`find_config_file_paths` and then passed
231 to the config file loader where they are resolved to an absolute path.337 to the config file loader where they are resolved to an absolute path.
232338
233 If a profile has been set at the command line, this will resolve339 If a profile has been set at the command line, this will resolve it.
234 it.
235 """340 """
236341
237 try:342 try:
@@ -241,11 +346,12 @@
241346
242 try:347 try:
243 self.profile_name = self.command_line_config.Global.profile348 self.profile_name = self.command_line_config.Global.profile
349 except AttributeError:
350 pass
351 else:
244 name_parts = self.config_file_name.split('.')352 name_parts = self.config_file_name.split('.')
245 name_parts.insert(1, u'_' + self.profile_name + u'.')353 name_parts.insert(1, u'_' + self.profile_name + u'.')
246 self.config_file_name = ''.join(name_parts)354 self.config_file_name = ''.join(name_parts)
247 except AttributeError:
248 pass
249355
250 def find_config_file_paths(self):356 def find_config_file_paths(self):
251 """Set the search paths for resolving the config file.357 """Set the search paths for resolving the config file.
@@ -253,7 +359,11 @@
253 This must set ``self.config_file_paths`` to a sequence of search359 This must set ``self.config_file_paths`` to a sequence of search
254 paths to pass to the config file loader.360 paths to pass to the config file loader.
255 """361 """
256 self.config_file_paths = (os.getcwd(), self.ipython_dir)362 # Include our own profiles directory last, so that users can still find
363 # our shipped copies of builtin profiles even if they don't have them
364 # in their local ipython directory.
365 prof_dir = os.path.join(get_ipython_package_dir(), 'config', 'profile')
366 self.config_file_paths = (os.getcwd(), self.ipython_dir, prof_dir)
257367
258 def pre_load_file_config(self):368 def pre_load_file_config(self):
259 """Do actions before the config file is loaded."""369 """Do actions before the config file is loaded."""
@@ -266,7 +376,8 @@
266 ``CONFIG_FILE`` config variable is set to the resolved config file376 ``CONFIG_FILE`` config variable is set to the resolved config file
267 location. If not successful, an empty config is used.377 location. If not successful, an empty config is used.
268 """378 """
269 self.log.debug("Attempting to load config file: %s" % self.config_file_name)379 self.log.debug("Attempting to load config file: %s" %
380 self.config_file_name)
270 loader = PyFileConfigLoader(self.config_file_name,381 loader = PyFileConfigLoader(self.config_file_name,
271 path=self.config_file_paths)382 path=self.config_file_paths)
272 try:383 try:
@@ -275,11 +386,11 @@
275 except IOError:386 except IOError:
276 # Only warn if the default config file was NOT being used.387 # Only warn if the default config file was NOT being used.
277 if not self.config_file_name==self.default_config_file_name:388 if not self.config_file_name==self.default_config_file_name:
278 self.log.warn("Config file not found, skipping: %s" % \389 self.log.warn("Config file not found, skipping: %s" %
279 self.config_file_name, exc_info=True)390 self.config_file_name, exc_info=True)
280 self.file_config = Config()391 self.file_config = Config()
281 except:392 except:
282 self.log.warn("Error loading config file: %s" % \393 self.log.warn("Error loading config file: %s" %
283 self.config_file_name, exc_info=True)394 self.config_file_name, exc_info=True)
284 self.file_config = Config()395 self.file_config = Config()
285396
@@ -299,16 +410,27 @@
299410
300 def log_file_config(self):411 def log_file_config(self):
301 if hasattr(self.file_config.Global, 'config_file'):412 if hasattr(self.file_config.Global, 'config_file'):
302 self.log.debug("Config file loaded: %s" % self.file_config.Global.config_file)413 self.log.debug("Config file loaded: %s" %
414 self.file_config.Global.config_file)
303 self.log.debug(repr(self.file_config))415 self.log.debug(repr(self.file_config))
304416
305 def merge_configs(self):417 def merge_configs(self):
306 """Merge the default, command line and file config objects."""418 """Merge the default, command line and file config objects."""
307 config = Config()419 config = Config()
308 config._merge(self.default_config)420 config._merge(self.default_config)
309 config._merge(self.file_config)421 if self.override_config is None:
310 config._merge(self.command_line_config)422 config._merge(self.file_config)
423 config._merge(self.command_line_config)
424 if self.constructor_config is not None:
425 config._merge(self.constructor_config)
426 else:
427 config._merge(self.override_config)
428 # XXX fperez - propose to Brian we rename master_config to simply
429 # config, I think this is going to be heavily used in examples and
430 # application code and the name is shorter/easier to find/remember.
431 # For now, just alias it...
311 self.master_config = config432 self.master_config = config
433 self.config = config
312434
313 def log_master_config(self):435 def log_master_config(self):
314 self.log.debug("Master config created:")436 self.log.debug("Master config created:")
@@ -358,7 +480,10 @@
358 raise480 raise
359 except:481 except:
360 if action == 'abort':482 if action == 'abort':
483 self.log.critical("Aborting application: %s" % self.name,
484 exc_info=True)
361 self.abort()485 self.abort()
486 raise
362 elif action == 'exit':487 elif action == 'exit':
363 self.exit(0)488 self.exit(0)
364489
365490
=== modified file 'IPython/core/builtin_trap.py' (properties changed: -x to +x)
=== modified file 'IPython/core/completer.py'
--- IPython/core/completer.py 2009-09-12 17:45:35 +0000
+++ IPython/core/completer.py 2010-01-18 01:17:11 +0000
@@ -44,7 +44,6 @@
4444
45- When the original stdin is not a tty device, GNU readline is never45- When the original stdin is not a tty device, GNU readline is never
46used, and this module (and the readline module) are silently inactive.46used, and this module (and the readline module) are silently inactive.
47
48"""47"""
4948
50#*****************************************************************************49#*****************************************************************************
@@ -54,14 +53,19 @@
54# proper procedure is to maintain its copyright as belonging to the Python53# proper procedure is to maintain its copyright as belonging to the Python
55# Software Foundation (in addition to my own, for all new code).54# Software Foundation (in addition to my own, for all new code).
56#55#
56# Copyright (C) 2008-2010 IPython Development Team
57# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
57# Copyright (C) 2001 Python Software Foundation, www.python.org58# Copyright (C) 2001 Python Software Foundation, www.python.org
58# Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
59#59#
60# Distributed under the terms of the BSD License. The full license is in60# Distributed under the terms of the BSD License. The full license is in
61# the file COPYING, distributed as part of this software.61# the file COPYING, distributed as part of this software.
62#62#
63#*****************************************************************************63#*****************************************************************************
6464
65#-----------------------------------------------------------------------------
66# Imports
67#-----------------------------------------------------------------------------
68
65import __builtin__69import __builtin__
66import __main__70import __main__
67import glob71import glob
@@ -73,23 +77,57 @@
73import sys77import sys
74import types78import types
7579
80import IPython.utils.rlineimpl as readline
76from IPython.core.error import TryNext81from IPython.core.error import TryNext
77from IPython.core.prefilter import ESC_MAGIC82from IPython.core.prefilter import ESC_MAGIC
78
79import IPython.utils.rlineimpl as readline
80from IPython.utils.ipstruct import Struct
81from IPython.utils import generics83from IPython.utils import generics
82
83# Python 2.4 offers sets as a builtin
84try:
85 set()
86except NameError:
87 from sets import Set as set
88
89from IPython.utils.genutils import debugx, dir284from IPython.utils.genutils import debugx, dir2
9085
86#-----------------------------------------------------------------------------
87# Globals
88#-----------------------------------------------------------------------------
89
90# Public API
91__all__ = ['Completer','IPCompleter']91__all__ = ['Completer','IPCompleter']
9292
93if sys.platform == 'win32':
94 PROTECTABLES = ' '
95else:
96 PROTECTABLES = ' ()'
97
98#-----------------------------------------------------------------------------
99# Main functions and classes
100#-----------------------------------------------------------------------------
101
102def protect_filename(s):
103 """Escape a string to protect certain characters."""
104
105 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
106 for ch in s])
107
108
109def single_dir_expand(matches):
110 "Recursively expand match lists containing a single dir."
111
112 if len(matches) == 1 and os.path.isdir(matches[0]):
113 # Takes care of links to directories also. Use '/'
114 # explicitly, even under Windows, so that name completions
115 # don't end up escaped.
116 d = matches[0]
117 if d[-1] in ['/','\\']:
118 d = d[:-1]
119
120 subdirs = os.listdir(d)
121 if subdirs:
122 matches = [ (d + '/' + p) for p in subdirs]
123 return single_dir_expand(matches)
124 else:
125 return matches
126 else:
127 return matches
128
129class Bunch: pass
130
93class Completer:131class Completer:
94 def __init__(self,namespace=None,global_namespace=None):132 def __init__(self,namespace=None,global_namespace=None):
95 """Create a new completer for the command line.133 """Create a new completer for the command line.
@@ -152,6 +190,7 @@
152 defined in self.namespace or self.global_namespace that match.190 defined in self.namespace or self.global_namespace that match.
153191
154 """192 """
193 #print 'Completer->global_matches, txt=%r' % text # dbg
155 matches = []194 matches = []
156 match_append = matches.append195 match_append = matches.append
157 n = len(text)196 n = len(text)
@@ -179,6 +218,7 @@
179 """218 """
180 import re219 import re
181220
221 #print 'Completer->attr_matches, txt=%r' % text # dbg
182 # Another option, seems to work great. Catches things like ''.<tab>222 # Another option, seems to work great. Catches things like ''.<tab>
183 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)223 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
184224
@@ -205,6 +245,7 @@
205 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]245 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
206 return res246 return res
207247
248
208class IPCompleter(Completer):249class IPCompleter(Completer):
209 """Extension of the completer class with IPython-specific features"""250 """Extension of the completer class with IPython-specific features"""
210251
@@ -235,7 +276,7 @@
235 to complete. """276 to complete. """
236277
237 Completer.__init__(self,namespace,global_namespace)278 Completer.__init__(self,namespace,global_namespace)
238 self.magic_prefix = shell.name+'.magic_'279
239 self.magic_escape = ESC_MAGIC280 self.magic_escape = ESC_MAGIC
240 self.readline = readline281 self.readline = readline
241 delims = self.readline.get_completer_delims()282 delims = self.readline.get_completer_delims()
@@ -244,7 +285,8 @@
244 self.get_line_buffer = self.readline.get_line_buffer285 self.get_line_buffer = self.readline.get_line_buffer
245 self.get_endidx = self.readline.get_endidx286 self.get_endidx = self.readline.get_endidx
246 self.omit__names = omit__names287 self.omit__names = omit__names
247 self.merge_completions = shell.readline_merge_completions 288 self.merge_completions = shell.readline_merge_completions
289 self.shell = shell.shell
248 if alias_table is None:290 if alias_table is None:
249 alias_table = {}291 alias_table = {}
250 self.alias_table = alias_table292 self.alias_table = alias_table
@@ -263,11 +305,13 @@
263 self.clean_glob = self._clean_glob_win32305 self.clean_glob = self._clean_glob_win32
264 else:306 else:
265 self.clean_glob = self._clean_glob307 self.clean_glob = self._clean_glob
308
309 # All active matcher routines for completion
266 self.matchers = [self.python_matches,310 self.matchers = [self.python_matches,
267 self.file_matches,311 self.file_matches,
312 self.magic_matches,
268 self.alias_matches,313 self.alias_matches,
269 self.python_func_kw_matches]314 self.python_func_kw_matches]
270
271 315
272 # Code contributed by Alex Schmolck, for ipython/emacs integration316 # Code contributed by Alex Schmolck, for ipython/emacs integration
273 def all_completions(self, text):317 def all_completions(self, text):
@@ -278,9 +322,8 @@
278 try:322 try:
279 for i in xrange(sys.maxint):323 for i in xrange(sys.maxint):
280 res = self.complete(text, i)324 res = self.complete(text, i)
281325 if not res:
282 if not res: break326 break
283
284 comp_append(res)327 comp_append(res)
285 #XXX workaround for ``notDefined.<tab>``328 #XXX workaround for ``notDefined.<tab>``
286 except NameError:329 except NameError:
@@ -316,41 +359,12 @@
316 # don't want to treat as delimiters in filename matching359 # don't want to treat as delimiters in filename matching
317 # when escaped with backslash360 # when escaped with backslash
318361
319 if sys.platform == 'win32':
320 protectables = ' '
321 else:
322 protectables = ' ()'
323
324 if text.startswith('!'):362 if text.startswith('!'):
325 text = text[1:]363 text = text[1:]
326 text_prefix = '!'364 text_prefix = '!'
327 else:365 else:
328 text_prefix = ''366 text_prefix = ''
329 367
330 def protect_filename(s):
331 return "".join([(ch in protectables and '\\' + ch or ch)
332 for ch in s])
333
334 def single_dir_expand(matches):
335 "Recursively expand match lists containing a single dir."
336
337 if len(matches) == 1 and os.path.isdir(matches[0]):
338 # Takes care of links to directories also. Use '/'
339 # explicitly, even under Windows, so that name completions
340 # don't end up escaped.
341 d = matches[0]
342 if d[-1] in ['/','\\']:
343 d = d[:-1]
344
345 subdirs = os.listdir(d)
346 if subdirs:
347 matches = [ (d + '/' + p) for p in subdirs]
348 return single_dir_expand(matches)
349 else:
350 return matches
351 else:
352 return matches
353
354 lbuf = self.lbuf368 lbuf = self.lbuf
355 open_quotes = 0 # track strings with open quotes369 open_quotes = 0 # track strings with open quotes
356 try:370 try:
@@ -402,13 +416,24 @@
402 #print 'mm',matches # dbg416 #print 'mm',matches # dbg
403 return single_dir_expand(matches)417 return single_dir_expand(matches)
404418
419 def magic_matches(self, text):
420 """Match magics"""
421 #print 'Completer->magic_matches:',text,'lb',self.lbuf # dbg
422 # Get all shell magics now rather than statically, so magics loaded at
423 # runtime show up too
424 magics = self.shell.lsmagic()
425 pre = self.magic_escape
426 baretext = text.lstrip(pre)
427 return [ pre+m for m in magics if m.startswith(baretext)]
428
405 def alias_matches(self, text):429 def alias_matches(self, text):
406 """Match internal system aliases""" 430 """Match internal system aliases"""
407 #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg431 #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg
408 432
409 # if we are not in the first 'item', alias matching 433 # if we are not in the first 'item', alias matching
410 # doesn't make sense - unless we are starting with 'sudo' command.434 # doesn't make sense - unless we are starting with 'sudo' command.
411 if ' ' in self.lbuf.lstrip() and not self.lbuf.lstrip().startswith('sudo'):435 if ' ' in self.lbuf.lstrip() and \
436 not self.lbuf.lstrip().startswith('sudo'):
412 return []437 return []
413 text = os.path.expanduser(text)438 text = os.path.expanduser(text)
414 aliases = self.alias_table.keys()439 aliases = self.alias_table.keys()
@@ -420,7 +445,7 @@
420 def python_matches(self,text):445 def python_matches(self,text):
421 """Match attributes or global python names"""446 """Match attributes or global python names"""
422447
423 #print 'Completer->python_matches, txt=<%s>' % text # dbg448 #print 'Completer->python_matches, txt=%r' % text # dbg
424 if "." in text:449 if "." in text:
425 try:450 try:
426 matches = self.attr_matches(text)451 matches = self.attr_matches(text)
@@ -439,11 +464,7 @@
439 matches = []464 matches = []
440 else:465 else:
441 matches = self.global_matches(text)466 matches = self.global_matches(text)
442 # this is so completion finds magics when automagic is on:467
443 if (matches == [] and
444 not text.startswith(os.sep) and
445 not ' ' in self.lbuf):
446 matches = self.attr_matches(self.magic_prefix+text)
447 return matches468 return matches
448469
449 def _default_arguments(self, obj):470 def _default_arguments(self, obj):
@@ -514,9 +535,11 @@
514 callableMatches = self.attr_matches('.'.join(ids[::-1]))535 callableMatches = self.attr_matches('.'.join(ids[::-1]))
515 argMatches = []536 argMatches = []
516 for callableMatch in callableMatches:537 for callableMatch in callableMatches:
517 try: namedArgs = self._default_arguments(eval(callableMatch,538 try:
539 namedArgs = self._default_arguments(eval(callableMatch,
518 self.namespace))540 self.namespace))
519 except: continue541 except:
542 continue
520 for namedArg in namedArgs:543 for namedArg in namedArgs:
521 if namedArg.startswith(text):544 if namedArg.startswith(text):
522 argMatches.append("%s=" %namedArg)545 argMatches.append("%s=" %namedArg)
@@ -528,7 +551,7 @@
528 if not line.strip():551 if not line.strip():
529 return None552 return None
530553
531 event = Struct()554 event = Bunch()
532 event.line = line555 event.line = line
533 event.symbol = text556 event.symbol = text
534 cmd = line.split(None,1)[0]557 cmd = line.split(None,1)[0]
@@ -540,11 +563,9 @@
540 try_magic = self.custom_completers.s_matches(563 try_magic = self.custom_completers.s_matches(
541 self.magic_escape + cmd) 564 self.magic_escape + cmd)
542 else:565 else:
543 try_magic = []566 try_magic = []
544 567
545 568 for c in itertools.chain(self.custom_completers.s_matches(cmd),
546 for c in itertools.chain(
547 self.custom_completers.s_matches(cmd),
548 try_magic,569 try_magic,
549 self.custom_completers.flat_matches(self.lbuf)):570 self.custom_completers.flat_matches(self.lbuf)):
550 #print "try",c # dbg571 #print "try",c # dbg
@@ -555,7 +576,8 @@
555 if withcase:576 if withcase:
556 return withcase577 return withcase
557 # if none, then case insensitive ones are ok too578 # if none, then case insensitive ones are ok too
558 return [r for r in res if r.lower().startswith(text.lower())]579 text_low = text.lower()
580 return [r for r in res if r.lower().startswith(text_low)]
559 except TryNext:581 except TryNext:
560 pass582 pass
561 583
@@ -598,14 +620,11 @@
598 return None620 return None
599 621
600 magic_escape = self.magic_escape622 magic_escape = self.magic_escape
601 magic_prefix = self.magic_prefix
602623
603 self.lbuf = self.full_lbuf[:self.get_endidx()]624 self.lbuf = self.full_lbuf[:self.get_endidx()]
604625
605 try:626 try:
606 if text.startswith(magic_escape):627 if text.startswith('~'):
607 text = text.replace(magic_escape,magic_prefix)
608 elif text.startswith('~'):
609 text = os.path.expanduser(text)628 text = os.path.expanduser(text)
610 if state == 0:629 if state == 0:
611 custom_res = self.dispatch_custom_completer(text)630 custom_res = self.dispatch_custom_completer(text)
@@ -625,13 +644,10 @@
625 self.matches = matcher(text)644 self.matches = matcher(text)
626 if self.matches:645 if self.matches:
627 break646 break
628 def uniq(alist):647 self.matches = list(set(self.matches))
629 set = {}
630 return [set.setdefault(e,e) for e in alist if e not in set]
631 self.matches = uniq(self.matches)
632 try:648 try:
633 ret = self.matches[state].replace(magic_prefix,magic_escape) 649 #print "MATCH: %r" % self.matches[state] # dbg
634 return ret650 return self.matches[state]
635 except IndexError:651 except IndexError:
636 return None652 return None
637 except:653 except:
638654
=== modified file 'IPython/core/crashhandler.py'
--- IPython/core/crashhandler.py 2009-11-09 01:49:24 +0000
+++ IPython/core/crashhandler.py 2010-01-18 01:17:11 +0000
@@ -26,12 +26,13 @@
26# Our own26# Our own
27from IPython.core import release27from IPython.core import release
28from IPython.core import ultratb28from IPython.core import ultratb
29from IPython.utils.genutils import sys_info
30
29from IPython.external.Itpl import itpl31from IPython.external.Itpl import itpl
3032
31from IPython.utils.genutils import *
32
33#****************************************************************************33#****************************************************************************
34class CrashHandler:34
35class CrashHandler(object):
35 """Customizable crash handlers for IPython-based systems.36 """Customizable crash handlers for IPython-based systems.
3637
37 Instances of this class provide a __call__ method which can be used as a38 Instances of this class provide a __call__ method which can be used as a
@@ -41,15 +42,15 @@
4142
42 """43 """
4344
44 def __init__(self,IP,app_name,contact_name,contact_email,45 def __init__(self,app, app_name, contact_name=None, contact_email=None,
45 bug_tracker,crash_report_fname,46 bug_tracker=None, crash_report_fname='CrashReport.txt',
46 show_crash_traceback=True):47 show_crash_traceback=True, call_pdb=False):
47 """New crash handler.48 """New crash handler.
4849
49 Inputs:50 Inputs:
5051
51 - IP: a running IPython instance, which will be queried at crash time52 - app: a running application instance, which will be queried at crash
52 for internal information.53 time for internal information.
5354
54 - app_name: a string containing the name of your application.55 - app_name: a string containing the name of your application.
5556
@@ -77,13 +78,16 @@
77 """78 """
7879
79 # apply args into instance80 # apply args into instance
80 self.IP = IP # IPython instance81 self.app = app
81 self.app_name = app_name82 self.app_name = app_name
82 self.contact_name = contact_name83 self.contact_name = contact_name
83 self.contact_email = contact_email84 self.contact_email = contact_email
84 self.bug_tracker = bug_tracker85 self.bug_tracker = bug_tracker
85 self.crash_report_fname = crash_report_fname86 self.crash_report_fname = crash_report_fname
86 self.show_crash_traceback = show_crash_traceback87 self.show_crash_traceback = show_crash_traceback
88 self.section_sep = '\n\n'+'*'*75+'\n\n'
89 self.call_pdb = call_pdb
90 #self.call_pdb = True # dbg
87 91
88 # Hardcoded defaults, which can be overridden either by subclasses or92 # Hardcoded defaults, which can be overridden either by subclasses or
89 # at runtime for the instance.93 # at runtime for the instance.
@@ -124,7 +128,7 @@
124 #color_scheme = 'Linux' # dbg128 #color_scheme = 'Linux' # dbg
125 129
126 try:130 try:
127 rptdir = self.IP.ipython_dir131 rptdir = self.app.ipython_dir
128 except:132 except:
129 rptdir = os.getcwd()133 rptdir = os.getcwd()
130 if not os.path.isdir(rptdir):134 if not os.path.isdir(rptdir):
@@ -134,8 +138,14 @@
134 # properly expanded out in the user message template138 # properly expanded out in the user message template
135 self.crash_report_fname = report_name139 self.crash_report_fname = report_name
136 TBhandler = ultratb.VerboseTB(color_scheme=color_scheme,140 TBhandler = ultratb.VerboseTB(color_scheme=color_scheme,
137 long_header=1)141 long_header=1,
138 traceback = TBhandler.text(etype,evalue,etb,context=31)142 call_pdb=self.call_pdb,
143 )
144 if self.call_pdb:
145 TBhandler(etype,evalue,etb)
146 return
147 else:
148 traceback = TBhandler.text(etype,evalue,etb,context=31)
139149
140 # print traceback to screen150 # print traceback to screen
141 if self.show_crash_traceback:151 if self.show_crash_traceback:
@@ -155,74 +165,58 @@
155 # Construct report on disk165 # Construct report on disk
156 report.write(self.make_report(traceback))166 report.write(self.make_report(traceback))
157 report.close()167 report.close()
158 raw_input("Press enter to exit:")168 raw_input("Hit <Enter> to quit this message (your terminal may close):")
159169
160 def make_report(self,traceback):170 def make_report(self,traceback):
161 """Return a string containing a crash report."""171 """Return a string containing a crash report."""
162172
163 sec_sep = '\n\n'+'*'*75+'\n\n'173 sec_sep = self.section_sep
164174
165 report = []175 report = ['*'*75+'\n\n'+'IPython post-mortem report\n\n']
166 rpt_add = report.append176 rpt_add = report.append
177 rpt_add(sys_info())
167 178
168 rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
169 rpt_add('IPython version: %s \n\n' % release.version)
170 rpt_add('BZR revision : %s \n\n' % release.revision)
171 rpt_add('Platform info : os.name -> %s, sys.platform -> %s' %
172 (os.name,sys.platform) )
173 rpt_add(sec_sep+'Current user configuration structure:\n\n')
174 rpt_add(pformat(self.IP.dict()))
175 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
176 try:179 try:
177 rpt_add(sec_sep+"History of session input:")180 config = pformat(self.app.config)
178 for line in self.IP.user_ns['_ih']:181 rpt_add(sec_sep+'Current user configuration structure:\n\n')
179 rpt_add(line)182 rpt_add(config)
180 rpt_add('\n*** Last line of input (may not be in above history):\n')
181 rpt_add(self.IP._last_input_line+'\n')
182 except:183 except:
183 pass184 pass
185 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
184186
185 return ''.join(report)187 return ''.join(report)
186188
189
187class IPythonCrashHandler(CrashHandler):190class IPythonCrashHandler(CrashHandler):
188 """sys.excepthook for IPython itself, leaves a detailed report on disk."""191 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
189 192
190 def __init__(self,IP):193 def __init__(self, app, app_name='IPython'):
191194
192 # Set here which of the IPython authors should be listed as contact195 # Set here which of the IPython authors should be listed as contact
193 AUTHOR_CONTACT = 'Fernando'196 AUTHOR_CONTACT = 'Fernando'
194 197
195 # Set argument defaults198 # Set argument defaults
196 app_name = 'IPython'
197 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'199 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
198 contact_name,contact_email = release.authors[AUTHOR_CONTACT][:2]200 contact_name,contact_email = release.authors[AUTHOR_CONTACT][:2]
199 crash_report_fname = 'IPython_crash_report.txt'201 crash_report_fname = 'IPython_crash_report.txt'
200 # Call parent constructor202 # Call parent constructor
201 CrashHandler.__init__(self,IP,app_name,contact_name,contact_email,203 CrashHandler.__init__(self,app,app_name,contact_name,contact_email,
202 bug_tracker,crash_report_fname)204 bug_tracker,crash_report_fname)
203205
204 def make_report(self,traceback):206 def make_report(self,traceback):
205 """Return a string containing a crash report."""207 """Return a string containing a crash report."""
206208
207 sec_sep = '\n\n'+'*'*75+'\n\n'209 sec_sep = self.section_sep
208210 # Start with parent report
209 report = []211 report = [super(IPythonCrashHandler, self).make_report(traceback)]
212 # Add interactive-specific info we may have
210 rpt_add = report.append213 rpt_add = report.append
211
212 rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
213 rpt_add('IPython version: %s \n\n' % release.version)
214 rpt_add('BZR revision : %s \n\n' % release.revision)
215 rpt_add('Platform info : os.name -> %s, sys.platform -> %s' %
216 (os.name,sys.platform) )
217 rpt_add(sec_sep+'Current user configuration structure:\n\n')
218 # rpt_add(pformat(self.IP.dict()))
219 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
220 try:214 try:
221 rpt_add(sec_sep+"History of session input:")215 rpt_add(sec_sep+"History of session input:")
222 for line in self.IP.user_ns['_ih']:216 for line in self.app.shell.user_ns['_ih']:
223 rpt_add(line)217 rpt_add(line)
224 rpt_add('\n*** Last line of input (may not be in above history):\n')218 rpt_add('\n*** Last line of input (may not be in above history):\n')
225 rpt_add(self.IP._last_input_line+'\n')219 rpt_add(self.app.shell._last_input_line+'\n')
226 except:220 except:
227 pass221 pass
228222
229223
=== modified file 'IPython/core/debugger.py'
--- IPython/core/debugger.py 2009-10-17 18:14:42 +0000
+++ IPython/core/debugger.py 2010-01-18 01:17:11 +0000
@@ -477,3 +477,36 @@
477 namespaces = [('Locals', self.curframe.f_locals),477 namespaces = [('Locals', self.curframe.f_locals),
478 ('Globals', self.curframe.f_globals)]478 ('Globals', self.curframe.f_globals)]
479 self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)479 self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)
480
481 def checkline(self, filename, lineno):
482 """Check whether specified line seems to be executable.
483
484 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
485 line or EOF). Warning: testing is not comprehensive.
486 """
487 #######################################################################
488 # XXX Hack! Use python-2.5 compatible code for this call, because with
489 # all of our changes, we've drifted from the pdb api in 2.6. For now,
490 # changing:
491 #
492 #line = linecache.getline(filename, lineno, self.curframe.f_globals)
493 # to:
494 #
495 line = linecache.getline(filename, lineno)
496 #
497 # does the trick. But in reality, we need to fix this by reconciling
498 # our updates with the new Pdb APIs in Python 2.6.
499 #
500 # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
501 #######################################################################
502
503 if not line:
504 print >>self.stdout, 'End of file'
505 return 0
506 line = line.strip()
507 # Don't allow setting breakpoint at a blank line
508 if (not line or (line[0] == '#') or
509 (line[:3] == '"""') or line[:3] == "'''"):
510 print >>self.stdout, '*** Blank or comment'
511 return 0
512 return lineno
480513
=== modified file 'IPython/core/history.py'
--- IPython/core/history.py 2009-08-19 21:56:41 +0000
+++ IPython/core/history.py 2010-01-18 01:17:11 +0000
@@ -14,20 +14,25 @@
14 %history -> print at most 40 inputs (some may be multi-line)\\14 %history -> print at most 40 inputs (some may be multi-line)\\
15 %history n -> print at most n inputs\\15 %history n -> print at most n inputs\\
16 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\16 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
17 17
18 Each input's number <n> is shown, and is accessible as the18 By default, input history is printed without line numbers so it can be
19 automatically generated variable _i<n>. Multi-line statements are19 directly pasted into an editor.
20 printed starting at a new line for easy copy/paste.20
21 21 With -n, each input's number <n> is shown, and is accessible as the
22 automatically generated variable _i<n> as well as In[<n>]. Multi-line
23 statements are printed starting at a new line for easy copy/paste.
2224
23 Options:25 Options:
2426
25 -n: do NOT print line numbers. This is useful if you want to get a27 -n: print line numbers for each input.
26 printout of many lines which can be directly pasted into a text
27 editor.
28
29 This feature is only available if numbered prompts are in use.28 This feature is only available if numbered prompts are in use.
3029
30 -o: also print outputs for each input.
31
32 -p: print classic '>>>' python prompts before each input. This is useful
33 for making documentation, and in conjunction with -o, for producing
34 doctest-ready output.
35
31 -t: (default) print the 'translated' history, as IPython understands it.36 -t: (default) print the 'translated' history, as IPython understands it.
32 IPython filters your input and converts it all into valid Python source37 IPython filters your input and converts it all into valid Python source
33 before executing it (things like magics or aliases are turned into38 before executing it (things like magics or aliases are turned into
@@ -50,7 +55,7 @@
50 if not self.outputcache.do_full_cache:55 if not self.outputcache.do_full_cache:
51 print 'This feature is only available if numbered prompts are in use.'56 print 'This feature is only available if numbered prompts are in use.'
52 return57 return
53 opts,args = self.parse_options(parameter_s,'gntsrf:',mode='list')58 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
5459
55 # Check if output to specific file was requested.60 # Check if output to specific file was requested.
56 try:61 try:
@@ -80,39 +85,43 @@
80 if 'g' in opts:85 if 'g' in opts:
81 init = 186 init = 1
82 final = len(input_hist)87 final = len(input_hist)
83 parts = parameter_s.split(None,1)88 parts = parameter_s.split(None, 1)
84 if len(parts) == 1:89 if len(parts) == 1:
85 parts += '*'90 parts += '*'
86 head, pattern = parts91 head, pattern = parts
87 pattern = "*" + pattern + "*"92 pattern = "*" + pattern + "*"
88 elif len(args) == 0:93 elif len(args) == 0:
89 final = len(input_hist)94 final = len(input_hist)-1
90 init = max(1,final-default_length)95 init = max(1,final-default_length)
91 elif len(args) == 1:96 elif len(args) == 1:
92 final = len(input_hist)97 final = len(input_hist)
93 init = max(1,final-int(args[0]))98 init = max(1, final-int(args[0]))
94 elif len(args) == 2:99 elif len(args) == 2:
95 init,final = map(int,args)100 init, final = map(int, args)
96 else:101 else:
97 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')102 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
98 print self.magic_hist.__doc__103 print >> Term.cout, self.magic_hist.__doc__
99 return104 return
105
100 width = len(str(final))106 width = len(str(final))
101 line_sep = ['','\n']107 line_sep = ['','\n']
102 print_nums = not opts.has_key('n')108 print_nums = 'n' in opts
109 print_outputs = 'o' in opts
110 pyprompts = 'p' in opts
103 111
104 found = False112 found = False
105 if pattern is not None:113 if pattern is not None:
106 sh = self.shadowhist.all()114 sh = self.shadowhist.all()
107 for idx, s in sh:115 for idx, s in sh:
108 if fnmatch.fnmatch(s, pattern):116 if fnmatch.fnmatch(s, pattern):
109 print "0%d: %s" %(idx, s)117 print >> outfile, "0%d: %s" %(idx, s)
110 found = True118 found = True
111 119
112 if found:120 if found:
113 print "==="121 print >> outfile, "==="
114 print "shadow history ends, fetch by %rep <number> (must start with 0)"122 print >> outfile, \
115 print "=== start of normal history ==="123 "shadow history ends, fetch by %rep <number> (must start with 0)"
124 print >> outfile, "=== start of normal history ==="
116 125
117 for in_num in range(init,final): 126 for in_num in range(init,final):
118 inline = input_hist[in_num]127 inline = input_hist[in_num]
@@ -122,8 +131,21 @@
122 multiline = int(inline.count('\n') > 1)131 multiline = int(inline.count('\n') > 1)
123 if print_nums:132 if print_nums:
124 print >> outfile, \133 print >> outfile, \
125 '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),134 '%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
126 print >> outfile, inline,135 if pyprompts:
136 print >> outfile, '>>>',
137 if multiline:
138 lines = inline.splitlines()
139 print >> outfile, '\n... '.join(lines)
140 print >> outfile, '... '
141 else:
142 print >> outfile, inline,
143 else:
144 print >> outfile, inline,
145 if print_outputs:
146 output = self.shell.user_ns['Out'].get(in_num)
147 if output is not None:
148 print >> outfile, repr(output)
127149
128 if close_at_end:150 if close_at_end:
129 outfile.close()151 outfile.close()
@@ -245,10 +267,10 @@
245267
246268
247def init_ipython(ip):269def init_ipython(ip):
248 import ipy_completers
249
250 ip.define_magic("rep",rep_f) 270 ip.define_magic("rep",rep_f)
251 ip.define_magic("hist",magic_hist) 271 ip.define_magic("hist",magic_hist)
252 ip.define_magic("history",magic_history)272 ip.define_magic("history",magic_history)
253273
254 ipy_completers.quick_completer('%hist' ,'-g -t -r -n')274 # XXX - ipy_completers are in quarantine, need to be updated to new apis
275 #import ipy_completers
276 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
255277
=== modified file 'IPython/core/hooks.py'
--- IPython/core/hooks.py 2009-08-26 20:54:07 +0000
+++ IPython/core/hooks.py 2010-01-18 01:17:11 +0000
@@ -137,8 +137,7 @@
137 for prio,cmd in self.chain:137 for prio,cmd in self.chain:
138 #print "prio",prio,"cmd",cmd #dbg138 #print "prio",prio,"cmd",cmd #dbg
139 try:139 try:
140 ret = cmd(*args, **kw)140 return cmd(*args, **kw)
141 return ret
142 except TryNext, exc:141 except TryNext, exc:
143 if exc.args or exc.kwargs:142 if exc.args or exc.kwargs:
144 args = exc.args143 args = exc.args
145144
=== modified file 'IPython/core/ipapp.py' (properties changed: -x to +x)
--- IPython/core/ipapp.py 2009-11-12 00:41:52 +0000
+++ IPython/core/ipapp.py 2010-01-18 01:17:11 +0000
@@ -4,17 +4,15 @@
4The :class:`~IPython.core.application.Application` object for the command4The :class:`~IPython.core.application.Application` object for the command
5line :command:`ipython` program.5line :command:`ipython` program.
66
7Authors:7Authors
8-------
89
9* Brian Granger10* Brian Granger
10* Fernando Perez11* Fernando Perez
11
12Notes
13-----
14"""12"""
1513
16#-----------------------------------------------------------------------------14#-----------------------------------------------------------------------------
17# Copyright (C) 2008-2009 The IPython Development Team15# Copyright (C) 2008-2010 The IPython Development Team
18#16#
19# Distributed under the terms of the BSD License. The full license is in17# Distributed under the terms of the BSD License. The full license is in
20# the file COPYING, distributed as part of this software.18# the file COPYING, distributed as part of this software.
@@ -23,318 +21,405 @@
23#-----------------------------------------------------------------------------21#-----------------------------------------------------------------------------
24# Imports22# Imports
25#-----------------------------------------------------------------------------23#-----------------------------------------------------------------------------
24from __future__ import absolute_import
2625
27import logging26import logging
28import os27import os
29import sys28import sys
30import warnings
3129
32from IPython.core.application import Application, BaseAppArgParseConfigLoader30from IPython.core import crashhandler
33from IPython.core import release31from IPython.core.application import Application
34from IPython.core.iplib import InteractiveShell32from IPython.core.iplib import InteractiveShell
35from IPython.config.loader import (33from IPython.config.loader import (
36 NoConfigDefault,
37 Config,34 Config,
38 PyFileConfigLoader35 PyFileConfigLoader,
36# NoConfigDefault,
39)37)
40
41from IPython.lib import inputhook38from IPython.lib import inputhook
42
43from IPython.utils.genutils import filefind, get_ipython_dir39from IPython.utils.genutils import filefind, get_ipython_dir
4440from . import usage
45#-----------------------------------------------------------------------------41
46# Utilities and helpers42#-----------------------------------------------------------------------------
47#-----------------------------------------------------------------------------43# Globals, utilities and helpers
4844#-----------------------------------------------------------------------------
4945
50ipython_desc = """46default_config_file_name = u'ipython_config.py'
51A Python shell with automatic history (input and output), dynamic object
52introspection, easier configuration, command completion, access to the system
53shell and more.
54"""
55
56def pylab_warning():
57 msg = """
58
59IPython's -pylab mode has been disabled until matplotlib supports this version
60of IPython. This version of IPython has greatly improved GUI integration that
61matplotlib will soon be able to take advantage of. This will eventually
62result in greater stability and a richer API for matplotlib under IPython.
63However during this transition, you will either need to use an older version
64of IPython, or do the following to use matplotlib interactively::
65
66 import matplotlib
67 matplotlib.interactive(True)
68 matplotlib.use('wxagg') # adjust for your backend
69 %gui -a wx # adjust for your GUI
70 from matplotlib import pyplot as plt
71
72See the %gui magic for information on the new interface.
73"""
74 warnings.warn(msg, category=DeprecationWarning, stacklevel=1)
75
76
77#-----------------------------------------------------------------------------
78# Main classes and functions
79#-----------------------------------------------------------------------------
8047
81cl_args = (48cl_args = (
82 (('--autocall',), dict(49 (('--autocall',), dict(
83 type=int, dest='InteractiveShell.autocall', default=NoConfigDefault,50 type=int, dest='InteractiveShell.autocall',
84 help='Set the autocall value (0,1,2).',51 help=
52 """Make IPython automatically call any callable object even if you
53 didn't type explicit parentheses. For example, 'str 43' becomes
54 'str(43)' automatically. The value can be '0' to disable the feature,
55 '1' for 'smart' autocall, where it is not applied if there are no more
56 arguments on the line, and '2' for 'full' autocall, where all callable
57 objects are automatically called (even if no arguments are present).
58 The default is '1'.""",
85 metavar='InteractiveShell.autocall')59 metavar='InteractiveShell.autocall')
86 ),60 ),
87 (('--autoindent',), dict(61 (('--autoindent',), dict(
88 action='store_true', dest='InteractiveShell.autoindent', default=NoConfigDefault,62 action='store_true', dest='InteractiveShell.autoindent',
89 help='Turn on autoindenting.')63 help='Turn on autoindenting.')
90 ),64 ),
91 (('--no-autoindent',), dict(65 (('--no-autoindent',), dict(
92 action='store_false', dest='InteractiveShell.autoindent', default=NoConfigDefault,66 action='store_false', dest='InteractiveShell.autoindent',
93 help='Turn off autoindenting.')67 help='Turn off autoindenting.')
94 ),68 ),
95 (('--automagic',), dict(69 (('--automagic',), dict(
96 action='store_true', dest='InteractiveShell.automagic', default=NoConfigDefault,70 action='store_true', dest='InteractiveShell.automagic',
97 help='Turn on the auto calling of magic commands.')71 help='Turn on the auto calling of magic commands.'
98 ),72 'Type %%magic at the IPython prompt for more information.')
73 ),
99 (('--no-automagic',), dict(74 (('--no-automagic',), dict(
100 action='store_false', dest='InteractiveShell.automagic', default=NoConfigDefault,75 action='store_false', dest='InteractiveShell.automagic',
101 help='Turn off the auto calling of magic commands.')76 help='Turn off the auto calling of magic commands.')
102 ),77 ),
103 (('--autoedit-syntax',), dict(78 (('--autoedit-syntax',), dict(
104 action='store_true', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,79 action='store_true', dest='InteractiveShell.autoedit_syntax',
105 help='Turn on auto editing of files with syntax errors.')80 help='Turn on auto editing of files with syntax errors.')
106 ),81 ),
107 (('--no-autoedit-syntax',), dict(82 (('--no-autoedit-syntax',), dict(
108 action='store_false', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,83 action='store_false', dest='InteractiveShell.autoedit_syntax',
109 help='Turn off auto editing of files with syntax errors.')84 help='Turn off auto editing of files with syntax errors.')
110 ),85 ),
111 (('--banner',), dict(86 (('--banner',), dict(
112 action='store_true', dest='Global.display_banner', default=NoConfigDefault,87 action='store_true', dest='Global.display_banner',
113 help='Display a banner upon starting IPython.')88 help='Display a banner upon starting IPython.')
114 ),89 ),
115 (('--no-banner',), dict(90 (('--no-banner',), dict(
116 action='store_false', dest='Global.display_banner', default=NoConfigDefault,91 action='store_false', dest='Global.display_banner',
117 help="Don't display a banner upon starting IPython.")92 help="Don't display a banner upon starting IPython.")
118 ),93 ),
119 (('--cache-size',), dict(94 (('--cache-size',), dict(
120 type=int, dest='InteractiveShell.cache_size', default=NoConfigDefault,95 type=int, dest='InteractiveShell.cache_size',
121 help="Set the size of the output cache.",96 help=
97 """Set the size of the output cache. The default is 1000, you can
98 change it permanently in your config file. Setting it to 0 completely
99 disables the caching system, and the minimum value accepted is 20 (if
100 you provide a value less than 20, it is reset to 0 and a warning is
101 issued). This limit is defined because otherwise you'll spend more
102 time re-flushing a too small cache than working.
103 """,
122 metavar='InteractiveShell.cache_size')104 metavar='InteractiveShell.cache_size')
123 ),105 ),
124 (('--classic',), dict(106 (('--classic',), dict(
125 action='store_true', dest='Global.classic', default=NoConfigDefault,107 action='store_true', dest='Global.classic',
126 help="Gives IPython a similar feel to the classic Python prompt.")108 help="Gives IPython a similar feel to the classic Python prompt.")
127 ),109 ),
128 (('--colors',), dict(110 (('--colors',), dict(
129 type=str, dest='InteractiveShell.colors', default=NoConfigDefault,111 type=str, dest='InteractiveShell.colors',
130 help="Set the color scheme (NoColor, Linux, and LightBG).",112 help="Set the color scheme (NoColor, Linux, and LightBG).",
131 metavar='InteractiveShell.colors')113 metavar='InteractiveShell.colors')
132 ),114 ),
133 (('--color-info',), dict(115 (('--color-info',), dict(
134 action='store_true', dest='InteractiveShell.color_info', default=NoConfigDefault,116 action='store_true', dest='InteractiveShell.color_info',
135 help="Enable using colors for info related things.")117 help=
118 """IPython can display information about objects via a set of func-
119 tions, and optionally can use colors for this, syntax highlighting
120 source code and various other elements. However, because this
121 information is passed through a pager (like 'less') and many pagers get
122 confused with color codes, this option is off by default. You can test
123 it and turn it on permanently in your ipython_config.py file if it
124 works for you. Test it and turn it on permanently if it works with
125 your system. The magic function %%color_info allows you to toggle this
126 inter- actively for testing."""
127 )
136 ),128 ),
137 (('--no-color-info',), dict(129 (('--no-color-info',), dict(
138 action='store_false', dest='InteractiveShell.color_info', default=NoConfigDefault,130 action='store_false', dest='InteractiveShell.color_info',
139 help="Disable using colors for info related things.")131 help="Disable using colors for info related things.")
140 ),132 ),
141 (('--confirm-exit',), dict(133 (('--confirm-exit',), dict(
142 action='store_true', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,134 action='store_true', dest='InteractiveShell.confirm_exit',
143 help="Prompt the user when existing.")135 help=
136 """Set to confirm when you try to exit IPython with an EOF (Control-D
137 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
138 '%%Exit', you can force a direct exit without any confirmation.
139 """
140 )
144 ),141 ),
145 (('--no-confirm-exit',), dict(142 (('--no-confirm-exit',), dict(
146 action='store_false', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,143 action='store_false', dest='InteractiveShell.confirm_exit',
147 help="Don't prompt the user when existing.")144 help="Don't prompt the user when exiting.")
148 ),145 ),
149 (('--deep-reload',), dict(146 (('--deep-reload',), dict(
150 action='store_true', dest='InteractiveShell.deep_reload', default=NoConfigDefault,147 action='store_true', dest='InteractiveShell.deep_reload',
151 help="Enable deep (recursive) reloading by default.")148 help=
149 """Enable deep (recursive) reloading by default. IPython can use the
150 deep_reload module which reloads changes in modules recursively (it
151 replaces the reload() function, so you don't need to change anything to
152 use it). deep_reload() forces a full reload of modules whose code may
153 have changed, which the default reload() function does not. When
154 deep_reload is off, IPython will use the normal reload(), but
155 deep_reload will still be available as dreload(). This fea- ture is off
156 by default [which means that you have both normal reload() and
157 dreload()].""")
152 ),158 ),
153 (('--no-deep-reload',), dict(159 (('--no-deep-reload',), dict(
154 action='store_false', dest='InteractiveShell.deep_reload', default=NoConfigDefault,160 action='store_false', dest='InteractiveShell.deep_reload',
155 help="Disable deep (recursive) reloading by default.")161 help="Disable deep (recursive) reloading by default.")
156 ),162 ),
157 (('--editor',), dict(163 (('--editor',), dict(
158 type=str, dest='InteractiveShell.editor', default=NoConfigDefault,164 type=str, dest='InteractiveShell.editor',
159 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",165 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
160 metavar='InteractiveShell.editor')166 metavar='InteractiveShell.editor')
161 ),167 ),
162 (('--log','-l'), dict(168 (('--log','-l'), dict(
163 action='store_true', dest='InteractiveShell.logstart', default=NoConfigDefault,169 action='store_true', dest='InteractiveShell.logstart',
164 help="Start logging to the default file (./ipython_log.py).")170 help="Start logging to the default log file (./ipython_log.py).")
165 ),171 ),
166 (('--logfile','-lf'), dict(172 (('--logfile','-lf'), dict(
167 type=unicode, dest='InteractiveShell.logfile', default=NoConfigDefault,173 type=unicode, dest='InteractiveShell.logfile',
168 help="Start logging to logfile.",174 help="Start logging to logfile with this name.",
169 metavar='InteractiveShell.logfile')175 metavar='InteractiveShell.logfile')
170 ),176 ),
171 (('--log-append','-la'), dict(177 (('--log-append','-la'), dict(
172 type=unicode, dest='InteractiveShell.logappend', default=NoConfigDefault,178 type=unicode, dest='InteractiveShell.logappend',
173 help="Start logging to the give file in append mode.",179 help="Start logging to the given file in append mode.",
174 metavar='InteractiveShell.logfile')180 metavar='InteractiveShell.logfile')
175 ),181 ),
176 (('--pdb',), dict(182 (('--pdb',), dict(
177 action='store_true', dest='InteractiveShell.pdb', default=NoConfigDefault,183 action='store_true', dest='InteractiveShell.pdb',
178 help="Enable auto calling the pdb debugger after every exception.")184 help="Enable auto calling the pdb debugger after every exception.")
179 ),185 ),
180 (('--no-pdb',), dict(186 (('--no-pdb',), dict(
181 action='store_false', dest='InteractiveShell.pdb', default=NoConfigDefault,187 action='store_false', dest='InteractiveShell.pdb',
182 help="Disable auto calling the pdb debugger after every exception.")188 help="Disable auto calling the pdb debugger after every exception.")
183 ),189 ),
184 (('--pprint',), dict(190 (('--pprint',), dict(
185 action='store_true', dest='InteractiveShell.pprint', default=NoConfigDefault,191 action='store_true', dest='InteractiveShell.pprint',
186 help="Enable auto pretty printing of results.")192 help="Enable auto pretty printing of results.")
187 ),193 ),
188 (('--no-pprint',), dict(194 (('--no-pprint',), dict(
189 action='store_false', dest='InteractiveShell.pprint', default=NoConfigDefault,195 action='store_false', dest='InteractiveShell.pprint',
190 help="Disable auto auto pretty printing of results.")196 help="Disable auto auto pretty printing of results.")
191 ),197 ),
192 (('--prompt-in1','-pi1'), dict(198 (('--prompt-in1','-pi1'), dict(
193 type=str, dest='InteractiveShell.prompt_in1', default=NoConfigDefault,199 type=str, dest='InteractiveShell.prompt_in1',
194 help="Set the main input prompt ('In [\#]: ')",200 help=
201 """Set the main input prompt ('In [\#]: '). Note that if you are using
202 numbered prompts, the number is represented with a '\#' in the string.
203 Don't forget to quote strings with spaces embedded in them. Most
204 bash-like escapes can be used to customize IPython's prompts, as well
205 as a few additional ones which are IPython-spe- cific. All valid
206 prompt escapes are described in detail in the Customization section of
207 the IPython manual.""",
195 metavar='InteractiveShell.prompt_in1')208 metavar='InteractiveShell.prompt_in1')
196 ),209 ),
197 (('--prompt-in2','-pi2'), dict(210 (('--prompt-in2','-pi2'), dict(
198 type=str, dest='InteractiveShell.prompt_in2', default=NoConfigDefault,211 type=str, dest='InteractiveShell.prompt_in2',
199 help="Set the secondary input prompt (' .\D.: ')",212 help=
213 """Set the secondary input prompt (' .\D.: '). Similar to the previous
214 option, but used for the continuation prompts. The special sequence
215 '\D' is similar to '\#', but with all digits replaced by dots (so you
216 can have your continuation prompt aligned with your input prompt).
217 Default: ' .\D.: ' (note three spaces at the start for alignment with
218 'In [\#]')""",
200 metavar='InteractiveShell.prompt_in2')219 metavar='InteractiveShell.prompt_in2')
201 ),220 ),
202 (('--prompt-out','-po'), dict(221 (('--prompt-out','-po'), dict(
203 type=str, dest='InteractiveShell.prompt_out', default=NoConfigDefault,222 type=str, dest='InteractiveShell.prompt_out',
204 help="Set the output prompt ('Out[\#]:')",223 help="Set the output prompt ('Out[\#]:')",
205 metavar='InteractiveShell.prompt_out')224 metavar='InteractiveShell.prompt_out')
206 ),225 ),
207 (('--quick',), dict(226 (('--quick',), dict(
208 action='store_true', dest='Global.quick', default=NoConfigDefault,227 action='store_true', dest='Global.quick',
209 help="Enable quick startup with no config files.")228 help="Enable quick startup with no config files.")
210 ),229 ),
211 (('--readline',), dict(230 (('--readline',), dict(
212 action='store_true', dest='InteractiveShell.readline_use', default=NoConfigDefault,231 action='store_true', dest='InteractiveShell.readline_use',
213 help="Enable readline for command line usage.")232 help="Enable readline for command line usage.")
214 ),233 ),
215 (('--no-readline',), dict(234 (('--no-readline',), dict(
216 action='store_false', dest='InteractiveShell.readline_use', default=NoConfigDefault,235 action='store_false', dest='InteractiveShell.readline_use',
217 help="Disable readline for command line usage.")236 help="Disable readline for command line usage.")
218 ),237 ),
219 (('--screen-length','-sl'), dict(238 (('--screen-length','-sl'), dict(
220 type=int, dest='InteractiveShell.screen_length', default=NoConfigDefault,239 type=int, dest='InteractiveShell.screen_length',
221 help='Number of lines on screen, used to control printing of long strings.',240 help=
241 """Number of lines of your screen, used to control printing of very
242 long strings. Strings longer than this number of lines will be sent
243 through a pager instead of directly printed. The default value for
244 this is 0, which means IPython will auto-detect your screen size every
245 time it needs to print certain potentially long strings (this doesn't
246 change the behavior of the 'print' keyword, it's only triggered
247 internally). If for some reason this isn't working well (it needs
248 curses support), specify it yourself. Otherwise don't change the
249 default.""",
222 metavar='InteractiveShell.screen_length')250 metavar='InteractiveShell.screen_length')
223 ),251 ),
224 (('--separate-in','-si'), dict(252 (('--separate-in','-si'), dict(
225 type=str, dest='InteractiveShell.separate_in', default=NoConfigDefault,253 type=str, dest='InteractiveShell.separate_in',
226 help="Separator before input prompts. Default '\n'.",254 help="Separator before input prompts. Default '\\n'.",
227 metavar='InteractiveShell.separate_in')255 metavar='InteractiveShell.separate_in')
228 ),256 ),
229 (('--separate-out','-so'), dict(257 (('--separate-out','-so'), dict(
230 type=str, dest='InteractiveShell.separate_out', default=NoConfigDefault,258 type=str, dest='InteractiveShell.separate_out',
231 help="Separator before output prompts. Default 0 (nothing).",259 help="Separator before output prompts. Default 0 (nothing).",
232 metavar='InteractiveShell.separate_out')260 metavar='InteractiveShell.separate_out')
233 ),261 ),
234 (('--separate-out2','-so2'), dict(262 (('--separate-out2','-so2'), dict(
235 type=str, dest='InteractiveShell.separate_out2', default=NoConfigDefault,263 type=str, dest='InteractiveShell.separate_out2',
236 help="Separator after output prompts. Default 0 (nonight).",264 help="Separator after output prompts. Default 0 (nonight).",
237 metavar='InteractiveShell.separate_out2')265 metavar='InteractiveShell.separate_out2')
238 ),266 ),
239 (('-no-sep',), dict(267 (('-no-sep',), dict(
240 action='store_true', dest='Global.nosep', default=NoConfigDefault,268 action='store_true', dest='Global.nosep',
241 help="Eliminate all spacing between prompts.")269 help="Eliminate all spacing between prompts.")
242 ),270 ),
243 (('--term-title',), dict(271 (('--term-title',), dict(
244 action='store_true', dest='InteractiveShell.term_title', default=NoConfigDefault,272 action='store_true', dest='InteractiveShell.term_title',
245 help="Enable auto setting the terminal title.")273 help="Enable auto setting the terminal title.")
246 ),274 ),
247 (('--no-term-title',), dict(275 (('--no-term-title',), dict(
248 action='store_false', dest='InteractiveShell.term_title', default=NoConfigDefault,276 action='store_false', dest='InteractiveShell.term_title',
249 help="Disable auto setting the terminal title.")277 help="Disable auto setting the terminal title.")
250 ),278 ),
251 (('--xmode',), dict(279 (('--xmode',), dict(
252 type=str, dest='InteractiveShell.xmode', default=NoConfigDefault,280 type=str, dest='InteractiveShell.xmode',
253 help="Exception mode ('Plain','Context','Verbose')",281 help=
282 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
283 similar to python's normal traceback printing. Context: prints 5 lines
284 of context source code around each line in the traceback. Verbose:
285 similar to Context, but additionally prints the variables currently
286 visible where the exception happened (shortening their strings if too
287 long). This can potentially be very slow, if you happen to have a huge
288 data structure whose string representation is complex to compute.
289 Your computer may appear to freeze for a while with cpu usage at 100%%.
290 If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
291 it more than once).
292 """,
254 metavar='InteractiveShell.xmode')293 metavar='InteractiveShell.xmode')
255 ),294 ),
256 (('--ext',), dict(295 (('--ext',), dict(
257 type=str, dest='Global.extra_extension', default=NoConfigDefault,296 type=str, dest='Global.extra_extension',
258 help="The dotted module name of an IPython extension to load.",297 help="The dotted module name of an IPython extension to load.",
259 metavar='Global.extra_extension')298 metavar='Global.extra_extension')
260 ),299 ),
261 (('-c',), dict(300 (('-c',), dict(
262 type=str, dest='Global.code_to_run', default=NoConfigDefault,301 type=str, dest='Global.code_to_run',
263 help="Execute the given command string.",302 help="Execute the given command string.",
264 metavar='Global.code_to_run')303 metavar='Global.code_to_run')
265 ),304 ),
266 (('-i',), dict(305 (('-i',), dict(
267 action='store_true', dest='Global.force_interact', default=NoConfigDefault,306 action='store_true', dest='Global.force_interact',
268 help="If running code from the command line, become interactive afterwards.")307 help=
269 ),308 "If running code from the command line, become interactive afterwards."
270 (('--wthread',), dict(309 )
271 action='store_true', dest='Global.wthread', default=NoConfigDefault,310 ),
272 help="Enable wxPython event loop integration.")311
273 ),312 # Options to start with GUI control enabled from the beginning
274 (('--q4thread','--qthread'), dict(313 (('--gui',), dict(
275 action='store_true', dest='Global.q4thread', default=NoConfigDefault,314 type=str, dest='Global.gui',
276 help="Enable Qt4 event loop integration. Qt3 is no longer supported.")315 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
277 ),316 metavar='gui-mode')
278 (('--gthread',), dict(317 ),
279 action='store_true', dest='Global.gthread', default=NoConfigDefault,318
280 help="Enable GTK event loop integration.")319 (('--pylab','-pylab'), dict(
281 ),320 type=str, dest='Global.pylab',
282 # # These are only here to get the proper deprecation warnings321 nargs='?', const='auto', metavar='gui-mode',
283 (('--pylab',), dict(322 help="Pre-load matplotlib and numpy for interactive use. "+
284 action='store_true', dest='Global.pylab', default=NoConfigDefault,323 "If no value is given, the gui backend is matplotlib's, else use "+
285 help="Disabled. Pylab has been disabled until matplotlib "324 "one of: ['tk', 'qt', 'wx', 'gtk'].")
286 "supports this version of IPython.")325 ),
287 )326
327 # Legacy GUI options. Leave them in for backwards compatibility, but the
328 # 'thread' names are really a misnomer now.
329 (('--wthread','-wthread'), dict(
330 action='store_true', dest='Global.wthread',
331 help="Enable wxPython event loop integration "+
332 "(DEPRECATED, use --gui wx)")
333 ),
334 (('--q4thread','--qthread','-q4thread','-qthread'), dict(
335 action='store_true', dest='Global.q4thread',
336 help="Enable Qt4 event loop integration. Qt3 is no longer supported. "+
337 "(DEPRECATED, use --gui qt)")
338 ),
339 (('--gthread','-gthread'), dict(
340 action='store_true', dest='Global.gthread',
341 help="Enable GTK event loop integration. "+
342 "(DEPRECATED, use --gui gtk)")
343 ),
288)344)
289345
290346#-----------------------------------------------------------------------------
291class IPythonAppCLConfigLoader(BaseAppArgParseConfigLoader):347# Main classes and functions
292348#-----------------------------------------------------------------------------
293 arguments = cl_args
294
295
296default_config_file_name = u'ipython_config.py'
297
298349
299class IPythonApp(Application):350class IPythonApp(Application):
300 name = u'ipython'351 name = u'ipython'
301 description = 'IPython: an enhanced interactive Python shell.'352 #: argparse formats better the 'usage' than the 'description' field
353 description = None
354 #: usage message printed by argparse. If None, auto-generate
355 usage = usage.cl_usage
356
302 config_file_name = default_config_file_name357 config_file_name = default_config_file_name
303358
359 cl_arguments = Application.cl_arguments + cl_args
360
361 # Private and configuration attributes
362 _CrashHandler = crashhandler.IPythonCrashHandler
363
364 def __init__(self, argv=None,
365 constructor_config=None, override_config=None,
366 **shell_params):
367 """Create a new IPythonApp.
368
369 See the parent class for details on how configuration is handled.
370
371 Parameters
372 ----------
373 argv : optional, list
374 If given, used as the command-line argv environment to read arguments
375 from.
376
377 constructor_config : optional, Config
378 If given, additional config that is merged last, after internal
379 defaults, command-line and file-based configs.
380
381 override_config : optional, Config
382 If given, config that overrides all others unconditionally (except
383 for internal defaults, which ensure that all parameters exist).
384
385 shell_params : optional, dict
386 All other keywords are passed to the :class:`iplib.InteractiveShell`
387 constructor.
388 """
389 super(IPythonApp, self).__init__(argv, constructor_config,
390 override_config)
391 self.shell_params = shell_params
392
304 def create_default_config(self):393 def create_default_config(self):
305 super(IPythonApp, self).create_default_config()394 super(IPythonApp, self).create_default_config()
306 self.default_config.Global.display_banner = True395 # Eliminate multiple lookups
396 Global = self.default_config.Global
397
398 # Set all default values
399 Global.display_banner = True
307 400
308 # If the -c flag is given or a file is given to run at the cmd line401 # If the -c flag is given or a file is given to run at the cmd line
309 # like "ipython foo.py", normally we exit without starting the main402 # like "ipython foo.py", normally we exit without starting the main
310 # loop. The force_interact config variable allows a user to override403 # loop. The force_interact config variable allows a user to override
311 # this and interact. It is also set by the -i cmd line flag, just404 # this and interact. It is also set by the -i cmd line flag, just
312 # like Python.405 # like Python.
313 self.default_config.Global.force_interact = False406 Global.force_interact = False
314407
315 # By default always interact by starting the IPython mainloop.408 # By default always interact by starting the IPython mainloop.
316 self.default_config.Global.interact = True409 Global.interact = True
317410
318 # No GUI integration by default411 # No GUI integration by default
319 self.default_config.Global.wthread = False412 Global.gui = False
320 self.default_config.Global.q4thread = False413 # Pylab off by default
321 self.default_config.Global.gthread = False414 Global.pylab = False
322415
323 def create_command_line_config(self):416 # Deprecated versions of gui support that used threading, we support
324 """Create and return a command line config loader."""417 # them just for bacwards compatibility as an alternate spelling for
325 return IPythonAppCLConfigLoader(418 # '--gui X'
326 description=self.description, 419 Global.qthread = False
327 version=release.version420 Global.q4thread = False
328 )421 Global.wthread = False
329422 Global.gthread = False
330 def post_load_command_line_config(self):
331 """Do actions after loading cl config."""
332 clc = self.command_line_config
333
334 # Display the deprecation warnings about threaded shells
335 if hasattr(clc.Global, 'pylab'):
336 pylab_warning()
337 del clc.Global['pylab']
338423
339 def load_file_config(self):424 def load_file_config(self):
340 if hasattr(self.command_line_config.Global, 'quick'):425 if hasattr(self.command_line_config.Global, 'quick'):
@@ -377,8 +462,7 @@
377 # unless the -i flag (Global.force_interact) is true.462 # unless the -i flag (Global.force_interact) is true.
378 code_to_run = config.Global.get('code_to_run','')463 code_to_run = config.Global.get('code_to_run','')
379 file_to_run = False464 file_to_run = False
380 if len(self.extra_args)>=1:465 if self.extra_args and self.extra_args[0]:
381 if self.extra_args[0]:
382 file_to_run = True466 file_to_run = True
383 if file_to_run or code_to_run:467 if file_to_run or code_to_run:
384 if not config.Global.force_interact:468 if not config.Global.force_interact:
@@ -390,10 +474,8 @@
390 sys.path.insert(0, '')474 sys.path.insert(0, '')
391475
392 # Create an InteractiveShell instance476 # Create an InteractiveShell instance
393 self.shell = InteractiveShell(477 self.shell = InteractiveShell(None, self.master_config,
394 parent=None,478 **self.shell_params )
395 config=self.master_config
396 )
397479
398 def post_construct(self):480 def post_construct(self):
399 """Do actions after construct, but before starting the app."""481 """Do actions after construct, but before starting the app."""
@@ -412,29 +494,52 @@
412 if self.log_level <= logging.INFO: print494 if self.log_level <= logging.INFO: print
413495
414 # Now a variety of things that happen after the banner is printed.496 # Now a variety of things that happen after the banner is printed.
415 self._enable_gui()497 self._enable_gui_pylab()
416 self._load_extensions()498 self._load_extensions()
417 self._run_exec_lines()499 self._run_exec_lines()
418 self._run_exec_files()500 self._run_exec_files()
419 self._run_cmd_line_code()501 self._run_cmd_line_code()
420502 self._configure_xmode()
421 def _enable_gui(self):503
422 """Enable GUI event loop integration."""504 def _enable_gui_pylab(self):
423 config = self.master_config505 """Enable GUI event loop integration, taking pylab into account."""
424 try:506 Global = self.master_config.Global
425 # Enable GUI integration507
426 if config.Global.wthread:508 # Select which gui to use
427 self.log.info("Enabling wx GUI event loop integration")509 if Global.gui:
428 inputhook.enable_wx(app=True)510 gui = Global.gui
429 elif config.Global.q4thread:511 # The following are deprecated, but there's likely to be a lot of use
430 self.log.info("Enabling Qt4 GUI event loop integration")512 # of this form out there, so we might as well support it for now. But
431 inputhook.enable_qt4(app=True)513 # the --gui option above takes precedence.
432 elif config.Global.gthread:514 elif Global.wthread:
433 self.log.info("Enabling GTK GUI event loop integration")515 gui = inputhook.GUI_WX
434 inputhook.enable_gtk(app=True)516 elif Global.qthread:
435 except:517 gui = inputhook.GUI_QT
436 self.log.warn("Error in enabling GUI event loop integration:")518 elif Global.gthread:
437 self.shell.showtraceback()519 gui = inputhook.GUI_GTK
520 else:
521 gui = None
522
523 # Using --pylab will also require gui activation, though which toolkit
524 # to use may be chosen automatically based on mpl configuration.
525 if Global.pylab:
526 activate = self.shell.enable_pylab
527 if Global.pylab == 'auto':
528 gui = None
529 else:
530 gui = Global.pylab
531 else:
532 # Enable only GUI integration, no pylab
533 activate = inputhook.enable_gui
534
535 if gui or Global.pylab:
536 try:
537 self.log.info("Enabling GUI event loop integration, "
538 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
539 activate(gui)
540 except:
541 self.log.warn("Error in enabling GUI event loop integration:")
542 self.shell.showtraceback()
438543
439 def _load_extensions(self):544 def _load_extensions(self):
440 """Load all IPython extensions in Global.extensions.545 """Load all IPython extensions in Global.extensions.
@@ -519,10 +624,17 @@
519 self.log.warn("Error in executing file in user namespace: %s" % fname)624 self.log.warn("Error in executing file in user namespace: %s" % fname)
520 self.shell.showtraceback()625 self.shell.showtraceback()
521626
627 def _configure_xmode(self):
628 # XXX - shouldn't this be read from the config? I'm still a little
629 # lost with all the details of handling the new config guys...
630 self.shell.InteractiveTB.set_mode(mode=self.shell.xmode)
631
522 def start_app(self):632 def start_app(self):
523 if self.master_config.Global.interact:633 if self.master_config.Global.interact:
524 self.log.debug("Starting IPython's mainloop...")634 self.log.debug("Starting IPython's mainloop...")
525 self.shell.mainloop()635 self.shell.mainloop()
636 else:
637 self.log.debug("IPython not interactive, start_app is no-op...")
526638
527639
528def load_default_config(ipython_dir=None):640def load_default_config(ipython_dir=None):
@@ -541,4 +653,3 @@
541 """Create and run a full blown IPython instance"""653 """Create and run a full blown IPython instance"""
542 app = IPythonApp()654 app = IPythonApp()
543 app.start()655 app.start()
544
545656
=== modified file 'IPython/core/iplib.py'
--- IPython/core/iplib.py 2009-11-09 01:49:24 +0000
+++ IPython/core/iplib.py 2010-01-18 01:17:11 +0000
@@ -17,6 +17,7 @@
17#-----------------------------------------------------------------------------17#-----------------------------------------------------------------------------
1818
19from __future__ import with_statement19from __future__ import with_statement
20from __future__ import absolute_import
2021
21import __builtin__22import __builtin__
22import StringIO23import StringIO
@@ -31,34 +32,37 @@
31import tempfile32import tempfile
32from contextlib import nested33from contextlib import nested
3334
34from IPython.core import ultratb
35from IPython.core import debugger, oinspect35from IPython.core import debugger, oinspect
36from IPython.core import shadowns
37from IPython.core import history as ipcorehist36from IPython.core import history as ipcorehist
38from IPython.core import prefilter37from IPython.core import prefilter
38from IPython.core import shadowns
39from IPython.core import ultratb
39from IPython.core.alias import AliasManager40from IPython.core.alias import AliasManager
40from IPython.core.builtin_trap import BuiltinTrap41from IPython.core.builtin_trap import BuiltinTrap
42from IPython.core.component import Component
41from IPython.core.display_trap import DisplayTrap43from IPython.core.display_trap import DisplayTrap
44from IPython.core.error import TryNext, UsageError
42from IPython.core.fakemodule import FakeModule, init_fakemod_dict45from IPython.core.fakemodule import FakeModule, init_fakemod_dict
43from IPython.core.logger import Logger46from IPython.core.logger import Logger
44from IPython.core.magic import Magic47from IPython.core.magic import Magic
48from IPython.core.prefilter import PrefilterManager
45from IPython.core.prompts import CachedOutput49from IPython.core.prompts import CachedOutput
46from IPython.core.prefilter import PrefilterManager50from IPython.core.pylabtools import pylab_activate
47from IPython.core.component import Component
48from IPython.core.usage import interactive_usage, default_banner51from IPython.core.usage import interactive_usage, default_banner
49from IPython.core.error import TryNext, UsageError
50
51from IPython.utils import pickleshare
52from IPython.external.Itpl import ItplNS52from IPython.external.Itpl import ItplNS
53from IPython.lib.inputhook import enable_gui
53from IPython.lib.backgroundjobs import BackgroundJobManager54from IPython.lib.backgroundjobs import BackgroundJobManager
54from IPython.utils.ipstruct import Struct
55from IPython.utils import PyColorize55from IPython.utils import PyColorize
56from IPython.utils.genutils import *56from IPython.utils import pickleshare
57from IPython.utils.genutils import get_ipython_dir57from IPython.utils.genutils import get_ipython_dir
58from IPython.utils.ipstruct import Struct
58from IPython.utils.platutils import toggle_set_term_title, set_term_title59from IPython.utils.platutils import toggle_set_term_title, set_term_title
59from IPython.utils.strdispatch import StrDispatch60from IPython.utils.strdispatch import StrDispatch
60from IPython.utils.syspathcontext import prepended_to_syspath61from IPython.utils.syspathcontext import prepended_to_syspath
6162
63# XXX - need to clean up this import * line
64from IPython.utils.genutils import *
65
62# from IPython.utils import growl66# from IPython.utils import growl
63# growl.start("IPython")67# growl.start("IPython")
6468
@@ -70,7 +74,6 @@
70# Globals74# Globals
71#-----------------------------------------------------------------------------75#-----------------------------------------------------------------------------
7276
73
74# store the builtin raw_input globally, and use this always, in case user code77# store the builtin raw_input globally, and use this always, in case user code
75# overwrites it (like wx.py.PyShell does)78# overwrites it (like wx.py.PyShell does)
76raw_input_original = raw_input79raw_input_original = raw_input
@@ -78,12 +81,10 @@
78# compiled regexps for autoindent management81# compiled regexps for autoindent management
79dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')82dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
8083
81
82#-----------------------------------------------------------------------------84#-----------------------------------------------------------------------------
83# Utilities85# Utilities
84#-----------------------------------------------------------------------------86#-----------------------------------------------------------------------------
8587
86
87ini_spaces_re = re.compile(r'^(\s+)')88ini_spaces_re = re.compile(r'^(\s+)')
8889
8990
@@ -113,6 +114,8 @@
113 return oldvalue114 return oldvalue
114115
115116
117def no_op(*a, **kw): pass
118
116class SpaceInInput(exceptions.Exception): pass119class SpaceInInput(exceptions.Exception): pass
117120
118class Bunch: pass121class Bunch: pass
@@ -183,6 +186,64 @@
183 return super(SeparateStr, self).validate(obj, value)186 return super(SeparateStr, self).validate(obj, value)
184187
185188
189def make_user_namespaces(user_ns=None, user_global_ns=None):
190 """Return a valid local and global user interactive namespaces.
191
192 This builds a dict with the minimal information needed to operate as a
193 valid IPython user namespace, which you can pass to the various
194 embedding classes in ipython. The default implementation returns the
195 same dict for both the locals and the globals to allow functions to
196 refer to variables in the namespace. Customized implementations can
197 return different dicts. The locals dictionary can actually be anything
198 following the basic mapping protocol of a dict, but the globals dict
199 must be a true dict, not even a subclass. It is recommended that any
200 custom object for the locals namespace synchronize with the globals
201 dict somehow.
202
203 Raises TypeError if the provided globals namespace is not a true dict.
204
205 Parameters
206 ----------
207 user_ns : dict-like, optional
208 The current user namespace. The items in this namespace should
209 be included in the output. If None, an appropriate blank
210 namespace should be created.
211 user_global_ns : dict, optional
212 The current user global namespace. The items in this namespace
213 should be included in the output. If None, an appropriate
214 blank namespace should be created.
215
216 Returns
217 -------
218 A pair of dictionary-like object to be used as the local namespace
219 of the interpreter and a dict to be used as the global namespace.
220 """
221
222
223 # We must ensure that __builtin__ (without the final 's') is always
224 # available and pointing to the __builtin__ *module*. For more details:
225 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
226
227 if user_ns is None:
228 # Set __name__ to __main__ to better match the behavior of the
229 # normal interpreter.
230 user_ns = {'__name__' :'__main__',
231 '__builtin__' : __builtin__,
232 '__builtins__' : __builtin__,
233 }
234 else:
235 user_ns.setdefault('__name__','__main__')
236 user_ns.setdefault('__builtin__',__builtin__)
237 user_ns.setdefault('__builtins__',__builtin__)
238
239 if user_global_ns is None:
240 user_global_ns = user_ns
241 if type(user_global_ns) is not dict:
242 raise TypeError("user_global_ns must be a true dict; got %r"
243 % type(user_global_ns))
244
245 return user_ns, user_global_ns
246
186#-----------------------------------------------------------------------------247#-----------------------------------------------------------------------------
187# Main IPython class248# Main IPython class
188#-----------------------------------------------------------------------------249#-----------------------------------------------------------------------------
@@ -329,6 +390,7 @@
329 self.hooks.late_startup_hook()390 self.hooks.late_startup_hook()
330391
331 def get_ipython(self):392 def get_ipython(self):
393 """Return the currently running IPython instance."""
332 return self394 return self
333395
334 #-------------------------------------------------------------------------396 #-------------------------------------------------------------------------
@@ -814,8 +876,7 @@
814 # These routines return properly built dicts as needed by the rest of876 # These routines return properly built dicts as needed by the rest of
815 # the code, and can also be used by extension writers to generate877 # the code, and can also be used by extension writers to generate
816 # properly initialized namespaces.878 # properly initialized namespaces.
817 user_ns, user_global_ns = self.make_user_namespaces(user_ns,879 user_ns, user_global_ns = make_user_namespaces(user_ns, user_global_ns)
818 user_global_ns)
819880
820 # Assign namespaces881 # Assign namespaces
821 # This is the namespace where all normal user variables live882 # This is the namespace where all normal user variables live
@@ -825,7 +886,7 @@
825 # An auxiliary namespace that checks what parts of the user_ns were886 # An auxiliary namespace that checks what parts of the user_ns were
826 # loaded at startup, so we can list later only variables defined in887 # loaded at startup, so we can list later only variables defined in
827 # actual interactive use. Since it is always a subset of user_ns, it888 # actual interactive use. Since it is always a subset of user_ns, it
828 # doesn't need to be seaparately tracked in the ns_table889 # doesn't need to be separately tracked in the ns_table.
829 self.user_config_ns = {}890 self.user_config_ns = {}
830891
831 # A namespace to keep track of internal data structures to prevent892 # A namespace to keep track of internal data structures to prevent
@@ -900,55 +961,6 @@
900 else:961 else:
901 sys.modules[main_name] = FakeModule(self.user_ns)962 sys.modules[main_name] = FakeModule(self.user_ns)
902963
903 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
904 """Return a valid local and global user interactive namespaces.
905
906 This builds a dict with the minimal information needed to operate as a
907 valid IPython user namespace, which you can pass to the various
908 embedding classes in ipython. The default implementation returns the
909 same dict for both the locals and the globals to allow functions to
910 refer to variables in the namespace. Customized implementations can
911 return different dicts. The locals dictionary can actually be anything
912 following the basic mapping protocol of a dict, but the globals dict
913 must be a true dict, not even a subclass. It is recommended that any
914 custom object for the locals namespace synchronize with the globals
915 dict somehow.
916
917 Raises TypeError if the provided globals namespace is not a true dict.
918
919 :Parameters:
920 user_ns : dict-like, optional
921 The current user namespace. The items in this namespace should
922 be included in the output. If None, an appropriate blank
923 namespace should be created.
924 user_global_ns : dict, optional
925 The current user global namespace. The items in this namespace
926 should be included in the output. If None, an appropriate
927 blank namespace should be created.
928
929 :Returns:
930 A tuple pair of dictionary-like object to be used as the local namespace
931 of the interpreter and a dict to be used as the global namespace.
932 """
933
934 if user_ns is None:
935 # Set __name__ to __main__ to better match the behavior of the
936 # normal interpreter.
937 user_ns = {'__name__' :'__main__',
938 '__builtins__' : __builtin__,
939 }
940 else:
941 user_ns.setdefault('__name__','__main__')
942 user_ns.setdefault('__builtins__',__builtin__)
943
944 if user_global_ns is None:
945 user_global_ns = user_ns
946 if type(user_global_ns) is not dict:
947 raise TypeError("user_global_ns must be a true dict; got %r"
948 % type(user_global_ns))
949
950 return user_ns, user_global_ns
951
952 def init_user_ns(self):964 def init_user_ns(self):
953 """Initialize all user-visible namespaces to their minimum defaults.965 """Initialize all user-visible namespaces to their minimum defaults.
954966
@@ -961,27 +973,59 @@
961 method. If they were not empty before, data will simply be added to973 method. If they were not empty before, data will simply be added to
962 therm.974 therm.
963 """975 """
964 # Store myself as the public api!!!976 # This function works in two parts: first we put a few things in
965 self.user_ns['get_ipython'] = self.get_ipython977 # user_ns, and we sync that contents into user_config_ns so that these
966978 # initial variables aren't shown by %who. After the sync, we add the
967 # make global variables for user access to the histories979 # rest of what we *do* want the user to see with %who even on a new
968 self.user_ns['_ih'] = self.input_hist980 # session (probably nothing, so theye really only see their own stuff)
969 self.user_ns['_oh'] = self.output_hist981
970 self.user_ns['_dh'] = self.dir_hist982 # The user dict must *always* have a __builtin__ reference to the
971983 # Python standard __builtin__ namespace, which must be imported.
972 # user aliases to input and output histories984 # This is so that certain operations in prompt evaluation can be
973 self.user_ns['In'] = self.input_hist985 # reliably executed with builtins. Note that we can NOT use
974 self.user_ns['Out'] = self.output_hist986 # __builtins__ (note the 's'), because that can either be a dict or a
975987 # module, and can even mutate at runtime, depending on the context
976 self.user_ns['_sh'] = shadowns988 # (Python makes no guarantees on it). In contrast, __builtin__ is
977989 # always a module object, though it must be explicitly imported.
990
991 # For more details:
992 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
993 ns = dict(__builtin__ = __builtin__)
994
978 # Put 'help' in the user namespace995 # Put 'help' in the user namespace
979 try:996 try:
980 from site import _Helper997 from site import _Helper
981 self.user_ns['help'] = _Helper()998 ns['help'] = _Helper()
982 except ImportError:999 except ImportError:
983 warn('help() not available - check site.py')1000 warn('help() not available - check site.py')
9841001
1002 # make global variables for user access to the histories
1003 ns['_ih'] = self.input_hist
1004 ns['_oh'] = self.output_hist
1005 ns['_dh'] = self.dir_hist
1006
1007 ns['_sh'] = shadowns
1008
1009 # user aliases to input and output histories. These shouldn't show up
1010 # in %who, as they can have very large reprs.
1011 ns['In'] = self.input_hist
1012 ns['Out'] = self.output_hist
1013
1014 # Store myself as the public api!!!
1015 ns['get_ipython'] = self.get_ipython
1016
1017 # Sync what we've added so far to user_config_ns so these aren't seen
1018 # by %who
1019 self.user_config_ns.update(ns)
1020
1021 # Anything put into ns now would show up in %who. Think twice before
1022 # putting anything here, as we really want %who to show the user their
1023 # stuff, not our variables.
1024
1025 # Finally, update the real user's namespace
1026 self.user_ns.update(ns)
1027
1028
985 def reset(self):1029 def reset(self):
986 """Clear all internal namespaces.1030 """Clear all internal namespaces.
9871031
@@ -1099,9 +1143,6 @@
1099 def savehist(self):1143 def savehist(self):
1100 """Save input history to a file (via readline library)."""1144 """Save input history to a file (via readline library)."""
11011145
1102 if not self.has_readline:
1103 return
1104
1105 try:1146 try:
1106 self.readline.write_history_file(self.histfile)1147 self.readline.write_history_file(self.histfile)
1107 except:1148 except:
@@ -1111,12 +1152,11 @@
1111 def reloadhist(self):1152 def reloadhist(self):
1112 """Reload the input history from disk file."""1153 """Reload the input history from disk file."""
11131154
1114 if self.has_readline:1155 try:
1115 try:1156 self.readline.clear_history()
1116 self.readline.clear_history()1157 self.readline.read_history_file(self.shell.histfile)
1117 self.readline.read_history_file(self.shell.histfile)1158 except AttributeError:
1118 except AttributeError:1159 pass
1119 pass
11201160
1121 def history_saving_wrapper(self, func):1161 def history_saving_wrapper(self, func):
1122 """ Wrap func for readline history saving1162 """ Wrap func for readline history saving
@@ -1150,37 +1190,14 @@
1150 color_scheme='NoColor',1190 color_scheme='NoColor',
1151 tb_offset = 1)1191 tb_offset = 1)
11521192
1153 # IPython itself shouldn't crash. This will produce a detailed1193 # The instance will store a pointer to the system-wide exception hook,
1154 # post-mortem if it does. But we only install the crash handler for1194 # so that runtime code (such as magics) can access it. This is because
1155 # non-threaded shells, the threaded ones use a normal verbose reporter1195 # during the read-eval loop, it may get temporarily overwritten.
1156 # and lose the crash handler. This is because exceptions in the main1196 self.sys_excepthook = sys.excepthook
1157 # thread (such as in GUI code) propagate directly to sys.excepthook,
1158 # and there's no point in printing crash dumps for every user exception.
1159 if self.isthreaded:
1160 ipCrashHandler = ultratb.FormattedTB()
1161 else:
1162 from IPython.core import crashhandler
1163 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1164 self.set_crash_handler(ipCrashHandler)
11651197
1166 # and add any custom exception handlers the user may have specified1198 # and add any custom exception handlers the user may have specified
1167 self.set_custom_exc(*custom_exceptions)1199 self.set_custom_exc(*custom_exceptions)
11681200
1169 def set_crash_handler(self, crashHandler):
1170 """Set the IPython crash handler.
1171
1172 This must be a callable with a signature suitable for use as
1173 sys.excepthook."""
1174
1175 # Install the given crash handler as the Python exception hook
1176 sys.excepthook = crashHandler
1177
1178 # The instance will store a pointer to this, so that runtime code
1179 # (such as magics) can access it. This is because during the
1180 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1181 # frameworks).
1182 self.sys_excepthook = sys.excepthook
1183
1184 def set_custom_exc(self,exc_tuple,handler):1201 def set_custom_exc(self,exc_tuple,handler):
1185 """set_custom_exc(exc_tuple,handler)1202 """set_custom_exc(exc_tuple,handler)
11861203
@@ -1248,7 +1265,8 @@
1248 """1265 """
1249 self.showtraceback((etype,value,tb),tb_offset=0)1266 self.showtraceback((etype,value,tb),tb_offset=0)
12501267
1251 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):1268 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1269 exception_only=False):
1252 """Display the exception that just occurred.1270 """Display the exception that just occurred.
12531271
1254 If nothing is known about the exception, this is the method which1272 If nothing is known about the exception, this is the method which
@@ -1259,18 +1277,24 @@
1259 care of calling it if needed, so unless you are explicitly catching a1277 care of calling it if needed, so unless you are explicitly catching a
1260 SyntaxError exception, don't try to analyze the stack manually and1278 SyntaxError exception, don't try to analyze the stack manually and
1261 simply call this method."""1279 simply call this method."""
1262
1263
1264 # Though this won't be called by syntax errors in the input line,
1265 # there may be SyntaxError cases whith imported code.
1266 1280
1267 try:1281 try:
1268 if exc_tuple is None:1282 if exc_tuple is None:
1269 etype, value, tb = sys.exc_info()1283 etype, value, tb = sys.exc_info()
1270 else:1284 else:
1271 etype, value, tb = exc_tuple1285 etype, value, tb = exc_tuple
1286
1287 if etype is None:
1288 if hasattr(sys, 'last_type'):
1289 etype, value, tb = sys.last_type, sys.last_value, \
1290 sys.last_traceback
1291 else:
1292 self.write('No traceback available to show.\n')
1293 return
1272 1294
1273 if etype is SyntaxError:1295 if etype is SyntaxError:
1296 # Though this won't be called by syntax errors in the input
1297 # line, there may be SyntaxError cases whith imported code.
1274 self.showsyntaxerror(filename)1298 self.showsyntaxerror(filename)
1275 elif etype is UsageError:1299 elif etype is UsageError:
1276 print "UsageError:", value1300 print "UsageError:", value
@@ -1286,12 +1310,20 @@
1286 if etype in self.custom_exceptions:1310 if etype in self.custom_exceptions:
1287 self.CustomTB(etype,value,tb)1311 self.CustomTB(etype,value,tb)
1288 else:1312 else:
1289 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)1313 if exception_only:
1290 if self.InteractiveTB.call_pdb and self.has_readline:1314 m = ('An exception has occurred, use %tb to see the '
1291 # pdb mucks up readline, fix it back1315 'full traceback.')
1292 self.set_completer()1316 print m
1317 self.InteractiveTB.show_exception_only(etype, value)
1318 else:
1319 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1320 if self.InteractiveTB.call_pdb:
1321 # pdb mucks up readline, fix it back
1322 self.set_completer()
1323
1293 except KeyboardInterrupt:1324 except KeyboardInterrupt:
1294 self.write("\nKeyboardInterrupt\n")1325 self.write("\nKeyboardInterrupt\n")
1326
12951327
1296 def showsyntaxerror(self, filename=None):1328 def showsyntaxerror(self, filename=None):
1297 """Display the syntax error that just occurred.1329 """Display the syntax error that just occurred.
@@ -1304,7 +1336,7 @@
1304 """1336 """
1305 etype, value, last_traceback = sys.exc_info()1337 etype, value, last_traceback = sys.exc_info()
13061338
1307 # See note about these variables in showtraceback() below1339 # See note about these variables in showtraceback() above
1308 sys.last_type = etype1340 sys.last_type = etype
1309 sys.last_value = value1341 sys.last_value = value
1310 sys.last_traceback = last_traceback1342 sys.last_traceback = last_traceback
@@ -1464,20 +1496,25 @@
1464 def init_readline(self):1496 def init_readline(self):
1465 """Command history completion/saving/reloading."""1497 """Command history completion/saving/reloading."""
14661498
1499 if self.readline_use:
1500 import IPython.utils.rlineimpl as readline
1501
1467 self.rl_next_input = None1502 self.rl_next_input = None
1468 self.rl_do_indent = False1503 self.rl_do_indent = False
14691504
1470 if not self.readline_use:1505 if not self.readline_use or not readline.have_readline:
1471 return1506 self.has_readline = False
1472
1473 import IPython.utils.rlineimpl as readline
1474
1475 if not readline.have_readline:
1476 self.has_readline = 0
1477 self.readline = None1507 self.readline = None
1478 # no point in bugging windows users with this every time:1508 # Set a number of methods that depend on readline to be no-op
1479 warn('Readline services not available on this platform.')1509 self.savehist = no_op
1510 self.reloadhist = no_op
1511 self.set_completer = no_op
1512 self.set_custom_completer = no_op
1513 self.set_completer_frame = no_op
1514 warn('Readline services not available or not loaded.')
1480 else:1515 else:
1516 self.has_readline = True
1517 self.readline = readline
1481 sys.modules['readline'] = readline1518 sys.modules['readline'] = readline
1482 import atexit1519 import atexit
1483 from IPython.core.completer import IPCompleter1520 from IPython.core.completer import IPCompleter
@@ -1512,8 +1549,6 @@
1512 warn('Problems reading readline initialization file <%s>'1549 warn('Problems reading readline initialization file <%s>'
1513 % inputrc_name)1550 % inputrc_name)
1514 1551
1515 self.has_readline = 1
1516 self.readline = readline
1517 # save this in sys so embedded copies can restore it properly1552 # save this in sys so embedded copies can restore it properly
1518 sys.ipcompleter = self.Completer.complete1553 sys.ipcompleter = self.Completer.complete
1519 self.set_completer()1554 self.set_completer()
@@ -1585,6 +1620,9 @@
1585 # Set user colors (don't do it in the constructor above so that it1620 # Set user colors (don't do it in the constructor above so that it
1586 # doesn't crash if colors option is invalid)1621 # doesn't crash if colors option is invalid)
1587 self.magic_colors(self.colors)1622 self.magic_colors(self.colors)
1623 # History was moved to a separate module
1624 from . import history
1625 history.init_ipython(self)
15881626
1589 def magic(self,arg_s):1627 def magic(self,arg_s):
1590 """Call a magic function by name.1628 """Call a magic function by name.
@@ -1603,7 +1641,6 @@
1603 valid Python code you can type at the interpreter, including loops and1641 valid Python code you can type at the interpreter, including loops and
1604 compound statements.1642 compound statements.
1605 """1643 """
1606
1607 args = arg_s.split(' ',1)1644 args = arg_s.split(' ',1)
1608 magic_name = args[0]1645 magic_name = args[0]
1609 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)1646 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
@@ -1842,7 +1879,8 @@
1842 except EOFError:1879 except EOFError:
1843 if self.autoindent:1880 if self.autoindent:
1844 self.rl_do_indent = False1881 self.rl_do_indent = False
1845 self.readline_startup_hook(None)1882 if self.has_readline:
1883 self.readline_startup_hook(None)
1846 self.write('\n')1884 self.write('\n')
1847 self.exit()1885 self.exit()
1848 except bdb.BdbQuit:1886 except bdb.BdbQuit:
@@ -1859,10 +1897,13 @@
1859 if (self.SyntaxTB.last_syntax_error and1897 if (self.SyntaxTB.last_syntax_error and
1860 self.autoedit_syntax):1898 self.autoedit_syntax):
1861 self.edit_syntax_error()1899 self.edit_syntax_error()
1862 1900
1863 # We are off again...1901 # We are off again...
1864 __builtin__.__dict__['__IPYTHON__active'] -= 11902 __builtin__.__dict__['__IPYTHON__active'] -= 1
18651903
1904 # Turn off the exit flag, so the mainloop can be restarted if desired
1905 self.exit_now = False
1906
1866 def safe_execfile(self, fname, *where, **kw):1907 def safe_execfile(self, fname, *where, **kw):
1867 """A safe version of the builtin execfile().1908 """A safe version of the builtin execfile().
18681909
@@ -1878,7 +1919,8 @@
1878 One or two namespaces, passed to execfile() as (globals,locals).1919 One or two namespaces, passed to execfile() as (globals,locals).
1879 If only one is given, it is passed as both.1920 If only one is given, it is passed as both.
1880 exit_ignore : bool (False)1921 exit_ignore : bool (False)
1881 If True, then don't print errors for non-zero exit statuses.1922 If True, then silence SystemExit for non-zero status (it is always
1923 silenced for zero status, as it is so common).
1882 """1924 """
1883 kw.setdefault('exit_ignore', False)1925 kw.setdefault('exit_ignore', False)
18841926
@@ -1903,40 +1945,21 @@
19031945
1904 with prepended_to_syspath(dname):1946 with prepended_to_syspath(dname):
1905 try:1947 try:
1906 if sys.platform == 'win32' and sys.version_info < (2,5,1):1948 execfile(fname,*where)
1907 # Work around a bug in Python for Windows. The bug was
1908 # fixed in in Python 2.5 r54159 and 54158, but that's still
1909 # SVN Python as of March/07. For details, see:
1910 # http://projects.scipy.org/ipython/ipython/ticket/123
1911 try:
1912 globs,locs = where[0:2]
1913 except:
1914 try:
1915 globs = locs = where[0]
1916 except:
1917 globs = locs = globals()
1918 exec file(fname) in globs,locs
1919 else:
1920 execfile(fname,*where)
1921 except SyntaxError:
1922 self.showsyntaxerror()
1923 warn('Failure executing file: <%s>' % fname)
1924 except SystemExit, status:1949 except SystemExit, status:
1925 # Code that correctly sets the exit status flag to success (0)1950 # If the call was made with 0 or None exit status (sys.exit(0)
1926 # shouldn't be bothered with a traceback. Note that a plain1951 # or sys.exit() ), don't bother showing a traceback, as both of
1927 # sys.exit() does NOT set the message to 0 (it's empty) so that1952 # these are considered normal by the OS:
1928 # will still get a traceback. Note that the structure of the1953 # > python -c'import sys;sys.exit(0)'; echo $?
1929 # SystemExit exception changed between Python 2.4 and 2.5, so1954 # 0
1930 # the checks must be done in a version-dependent way.1955 # > python -c'import sys;sys.exit()'; echo $?
1931 show = False1956 # 0
1932 if status.args[0]==0 and not kw['exit_ignore']:1957 # For other exit status, we show the exception unless
1933 show = True1958 # explicitly silenced, but only in short form.
1934 if show:1959 if status.code not in (0, None) and not kw['exit_ignore']:
1935 self.showtraceback()1960 self.showtraceback(exception_only=True)
1936 warn('Failure executing file: <%s>' % fname)
1937 except:1961 except:
1938 self.showtraceback()1962 self.showtraceback()
1939 warn('Failure executing file: <%s>' % fname)
19401963
1941 def safe_execfile_ipy(self, fname):1964 def safe_execfile_ipy(self, fname):
1942 """Like safe_execfile, but for .ipy files with IPython syntax.1965 """Like safe_execfile, but for .ipy files with IPython syntax.
@@ -2150,9 +2173,8 @@
2150 sys.excepthook = old_excepthook2173 sys.excepthook = old_excepthook
2151 except SystemExit:2174 except SystemExit:
2152 self.resetbuffer()2175 self.resetbuffer()
2153 self.showtraceback()2176 self.showtraceback(exception_only=True)
2154 warn("Type %exit or %quit to exit IPython "2177 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2155 "(%Exit or %Quit do so unconditionally).",level=1)
2156 except self.custom_exceptions:2178 except self.custom_exceptions:
2157 etype,value,tb = sys.exc_info()2179 etype,value,tb = sys.exc_info()
2158 self.CustomTB(etype,value,tb)2180 self.CustomTB(etype,value,tb)
@@ -2336,7 +2358,7 @@
2336 with prepended_to_syspath(self.ipython_extension_dir):2358 with prepended_to_syspath(self.ipython_extension_dir):
2337 __import__(module_str)2359 __import__(module_str)
2338 mod = sys.modules[module_str]2360 mod = sys.modules[module_str]
2339 self._call_load_ipython_extension(mod)2361 return self._call_load_ipython_extension(mod)
23402362
2341 def unload_extension(self, module_str):2363 def unload_extension(self, module_str):
2342 """Unload an IPython extension by its module name.2364 """Unload an IPython extension by its module name.
@@ -2368,11 +2390,11 @@
23682390
2369 def _call_load_ipython_extension(self, mod):2391 def _call_load_ipython_extension(self, mod):
2370 if hasattr(mod, 'load_ipython_extension'):2392 if hasattr(mod, 'load_ipython_extension'):
2371 mod.load_ipython_extension(self)2393 return mod.load_ipython_extension(self)
23722394
2373 def _call_unload_ipython_extension(self, mod):2395 def _call_unload_ipython_extension(self, mod):
2374 if hasattr(mod, 'unload_ipython_extension'):2396 if hasattr(mod, 'unload_ipython_extension'):
2375 mod.unload_ipython_extension(self)2397 return mod.unload_ipython_extension(self)
23762398
2377 #-------------------------------------------------------------------------2399 #-------------------------------------------------------------------------
2378 # Things related to the prefilter2400 # Things related to the prefilter
@@ -2380,6 +2402,10 @@
23802402
2381 def init_prefilter(self):2403 def init_prefilter(self):
2382 self.prefilter_manager = PrefilterManager(self, config=self.config)2404 self.prefilter_manager = PrefilterManager(self, config=self.config)
2405 # Ultimately this will be refactored in the new interpreter code, but
2406 # for now, we should expose the main prefilter method (there's legacy
2407 # code out there that may rely on this).
2408 self.prefilter = self.prefilter_manager.prefilter_lines
23832409
2384 #-------------------------------------------------------------------------2410 #-------------------------------------------------------------------------
2385 # Utilities2411 # Utilities
@@ -2445,11 +2471,46 @@
2445 return ask_yes_no(prompt,default)2471 return ask_yes_no(prompt,default)
24462472
2447 #-------------------------------------------------------------------------2473 #-------------------------------------------------------------------------
2474 # Things related to GUI support and pylab
2475 #-------------------------------------------------------------------------
2476
2477 def enable_pylab(self, gui=None):
2478 """Activate pylab support at runtime.
2479
2480 This turns on support for matplotlib, preloads into the interactive
2481 namespace all of numpy and pylab, and configures IPython to correcdtly
2482 interact with the GUI event loop. The GUI backend to be used can be
2483 optionally selected with the optional :param:`gui` argument.
2484
2485 Parameters
2486 ----------
2487 gui : optional, string
2488
2489 If given, dictates the choice of matplotlib GUI backend to use
2490 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2491 'gtk'), otherwise we use the default chosen by matplotlib (as
2492 dictated by the matplotlib build-time options plus the user's
2493 matplotlibrc configuration file).
2494 """
2495 # We want to prevent the loading of pylab to pollute the user's
2496 # namespace as shown by the %who* magics, so we execute the activation
2497 # code in an empty namespace, and we update *both* user_ns and
2498 # user_config_ns with this information.
2499 ns = {}
2500 gui = pylab_activate(ns, gui)
2501 self.user_ns.update(ns)
2502 self.user_config_ns.update(ns)
2503 # Now we must activate the gui pylab wants to use, and fix %run to take
2504 # plot updates into account
2505 enable_gui(gui)
2506 self.magic_run = self._pylab_magic_run
2507
2508 #-------------------------------------------------------------------------
2448 # Things related to IPython exiting2509 # Things related to IPython exiting
2449 #-------------------------------------------------------------------------2510 #-------------------------------------------------------------------------
24502511
2451 def ask_exit(self):2512 def ask_exit(self):
2452 """ Call for exiting. Can be overiden and used as a callback. """2513 """ Ask the shell to exit. Can be overiden and used as a callback. """
2453 self.exit_now = True2514 self.exit_now = True
24542515
2455 def exit(self):2516 def exit(self):
24562517
=== modified file 'IPython/core/magic.py'
--- IPython/core/magic.py 2009-12-30 19:26:16 +0000
+++ IPython/core/magic.py 2010-01-18 01:17:11 +0000
@@ -44,21 +44,26 @@
4444
45# Homebrewed45# Homebrewed
46import IPython46import IPython
47from IPython.utils import wildcard47import IPython.utils.generics
48
48from IPython.core import debugger, oinspect49from IPython.core import debugger, oinspect
49from IPython.core.error import TryNext50from IPython.core.error import TryNext
51from IPython.core.error import UsageError
50from IPython.core.fakemodule import FakeModule52from IPython.core.fakemodule import FakeModule
53from IPython.core.macro import Macro
54from IPython.core.page import page
51from IPython.core.prefilter import ESC_MAGIC55from IPython.core.prefilter import ESC_MAGIC
56from IPython.core.pylabtools import mpl_runner
57from IPython.lib.inputhook import enable_gui
52from IPython.external.Itpl import Itpl, itpl, printpl,itplns58from IPython.external.Itpl import Itpl, itpl, printpl,itplns
59from IPython.testing import decorators as testdec
60from IPython.utils import platutils
61from IPython.utils import wildcard
53from IPython.utils.PyColorize import Parser62from IPython.utils.PyColorize import Parser
54from IPython.utils.ipstruct import Struct63from IPython.utils.ipstruct import Struct
55from IPython.core.macro import Macro64
65# XXX - We need to switch to explicit imports here with genutils
56from IPython.utils.genutils import *66from IPython.utils.genutils import *
57from IPython.core.page import page
58from IPython.utils import platutils
59import IPython.utils.generics
60from IPython.core.error import UsageError
61from IPython.testing import decorators as testdec
6267
63#***************************************************************************68#***************************************************************************
64# Utility functions69# Utility functions
@@ -80,10 +85,16 @@
80 done.add(h)85 done.add(h)
8186
82 return newhead + tail 87 return newhead + tail
83 88
8489
85#***************************************************************************90#***************************************************************************
86# Main class implementing Magic functionality91# Main class implementing Magic functionality
92
93# XXX - for some odd reason, if Magic is made a new-style class, we get errors
94# on construction of the main InteractiveShell object. Something odd is going
95# on with super() calls, Component and the MRO... For now leave it as-is, but
96# eventually this needs to be clarified.
97
87class Magic:98class Magic:
88 """Magic functions for InteractiveShell.99 """Magic functions for InteractiveShell.
89100
@@ -335,7 +346,7 @@
335 raise ValueError,'incorrect mode given: %s' % mode346 raise ValueError,'incorrect mode given: %s' % mode
336 # Get options347 # Get options
337 list_all = kw.get('list_all',0)348 list_all = kw.get('list_all',0)
338 posix = kw.get('posix',True)349 posix = kw.get('posix', os.name == 'posix')
339350
340 # Check if we have more than one argument to warrant extra processing:351 # Check if we have more than one argument to warrant extra processing:
341 odict = {} # Dictionary with options352 odict = {} # Dictionary with options
@@ -864,7 +875,7 @@
864 show_all=opt('a'),ignore_case=ignore_case)875 show_all=opt('a'),ignore_case=ignore_case)
865 except:876 except:
866 shell.showtraceback()877 shell.showtraceback()
867878
868 def magic_who_ls(self, parameter_s=''):879 def magic_who_ls(self, parameter_s=''):
869 """Return a sorted list of all interactive variables.880 """Return a sorted list of all interactive variables.
870881
@@ -874,17 +885,15 @@
874 user_ns = self.shell.user_ns885 user_ns = self.shell.user_ns
875 internal_ns = self.shell.internal_ns886 internal_ns = self.shell.internal_ns
876 user_config_ns = self.shell.user_config_ns887 user_config_ns = self.shell.user_config_ns
877 out = []888 out = [ i for i in user_ns
889 if not i.startswith('_') \
890 and not (i in internal_ns or i in user_config_ns) ]
891
878 typelist = parameter_s.split()892 typelist = parameter_s.split()
893 if typelist:
894 typeset = set(typelist)
895 out = [i for i in out if type(i).__name__ in typeset]
879896
880 for i in user_ns:
881 if not (i.startswith('_') or i.startswith('_i')) \
882 and not (i in internal_ns or i in user_config_ns):
883 if typelist:
884 if type(user_ns[i]).__name__ in typelist:
885 out.append(i)
886 else:
887 out.append(i)
888 out.sort()897 out.sort()
889 return out898 return out
890 899
@@ -1571,7 +1580,7 @@
1571 return1580 return
15721581
1573 if filename.lower().endswith('.ipy'):1582 if filename.lower().endswith('.ipy'):
1574 self.safe_execfile_ipy(filename)1583 self.shell.safe_execfile_ipy(filename)
1575 return1584 return
1576 1585
1577 # Control the response to exit() calls made by the script being run1586 # Control the response to exit() calls made by the script being run
@@ -2522,25 +2531,15 @@
2522 self.shell.pprint = 1 - self.shell.pprint2531 self.shell.pprint = 1 - self.shell.pprint
2523 print 'Pretty printing has been turned', \2532 print 'Pretty printing has been turned', \
2524 ['OFF','ON'][self.shell.pprint]2533 ['OFF','ON'][self.shell.pprint]
2525 2534
2526 def magic_exit(self, parameter_s=''):
2527 """Exit IPython, confirming if configured to do so.
2528
2529 You can configure whether IPython asks for confirmation upon exit by
2530 setting the confirm_exit flag in the ipythonrc file."""
2531
2532 self.shell.exit()
2533
2534 def magic_quit(self, parameter_s=''):
2535 """Exit IPython, confirming if configured to do so (like %exit)"""
2536
2537 self.shell.exit()
2538
2539 def magic_Exit(self, parameter_s=''):2535 def magic_Exit(self, parameter_s=''):
2540 """Exit IPython without confirmation."""2536 """Exit IPython without confirmation."""
25412537
2542 self.shell.ask_exit()2538 self.shell.ask_exit()
25432539
2540 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2541 magic_exit = magic_quit = magic_Quit = magic_Exit
2542
2544 #......................................................................2543 #......................................................................
2545 # Functions to implement unix shell-type things2544 # Functions to implement unix shell-type things
25462545
@@ -2685,11 +2684,12 @@
2685 else:2684 else:
2686 syscmdlist.append(ff)2685 syscmdlist.append(ff)
2687 else:2686 else:
2687 no_alias = self.shell.alias_manager.no_alias
2688 for pdir in path:2688 for pdir in path:
2689 os.chdir(pdir)2689 os.chdir(pdir)
2690 for ff in os.listdir(pdir):2690 for ff in os.listdir(pdir):
2691 base, ext = os.path.splitext(ff)2691 base, ext = os.path.splitext(ff)
2692 if isexec(ff) and base.lower() not in self.shell.no_alias:2692 if isexec(ff) and base.lower() not in no_alias:
2693 if ext.lower() == '.exe':2693 if ext.lower() == '.exe':
2694 ff = base2694 ff = base
2695 try:2695 try:
@@ -3399,8 +3399,6 @@
3399 your existing IPython session.3399 your existing IPython session.
3400 """3400 """
34013401
3402 # XXX - Fix this to have cleaner activate/deactivate calls.
3403 from IPython.extensions import InterpreterPasteInput as ipaste
3404 from IPython.utils.ipstruct import Struct3402 from IPython.utils.ipstruct import Struct
34053403
3406 # Shorthands3404 # Shorthands
@@ -3423,8 +3421,6 @@
34233421
3424 if mode == False:3422 if mode == False:
3425 # turn on3423 # turn on
3426 ipaste.activate_prefilter()
3427
3428 oc.prompt1.p_template = '>>> '3424 oc.prompt1.p_template = '>>> '
3429 oc.prompt2.p_template = '... '3425 oc.prompt2.p_template = '... '
3430 oc.prompt_out.p_template = ''3426 oc.prompt_out.p_template = ''
@@ -3438,13 +3434,11 @@
3438 oc.prompt_out.pad_left = False3434 oc.prompt_out.pad_left = False
34393435
3440 shell.pprint = False3436 shell.pprint = False
34413437
3442 shell.magic_xmode('Plain')3438 shell.magic_xmode('Plain')
34433439
3444 else:3440 else:
3445 # turn off3441 # turn off
3446 ipaste.deactivate_prefilter()
3447
3448 oc.prompt1.p_template = shell.prompt_in13442 oc.prompt1.p_template = shell.prompt_in1
3449 oc.prompt2.p_template = shell.prompt_in23443 oc.prompt2.p_template = shell.prompt_in2
3450 oc.prompt_out.p_template = shell.prompt_out3444 oc.prompt_out.p_template = shell.prompt_out
@@ -3457,7 +3451,7 @@
3457 oc.prompt1.pad_left = oc.prompt2.pad_left = \3451 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3458 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left3452 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
34593453
3460 rc.pprint = dstore.rc_pprint3454 shell.pprint = dstore.rc_pprint
34613455
3462 shell.magic_xmode(dstore.xmode)3456 shell.magic_xmode(dstore.xmode)
34633457
@@ -3475,7 +3469,7 @@
3475 using the (pylab/wthread/etc.) command line flags. GUI toolkits3469 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3476 can now be enabled, disabled and swtiched at runtime and keyboard3470 can now be enabled, disabled and swtiched at runtime and keyboard
3477 interrupts should work without any problems. The following toolkits3471 interrupts should work without any problems. The following toolkits
3478 are supports: wxPython, PyQt4, PyGTK, and Tk::3472 are supported: wxPython, PyQt4, PyGTK, and Tk::
34793473
3480 %gui wx # enable wxPython event loop integration3474 %gui wx # enable wxPython event loop integration
3481 %gui qt4|qt # enable PyQt4 event loop integration3475 %gui qt4|qt # enable PyQt4 event loop integration
@@ -3494,25 +3488,13 @@
34943488
3495 This is highly recommended for most users.3489 This is highly recommended for most users.
3496 """3490 """
3497 from IPython.lib import inputhook3491 opts, arg = self.parse_options(parameter_s,'a')
3498 if "-a" in parameter_s:3492 if arg=='': arg = None
3499 app = True3493 return enable_gui(arg, 'a' in opts)
3500 else:
3501 app = False
3502 if not parameter_s:
3503 inputhook.clear_inputhook()
3504 elif 'wx' in parameter_s:
3505 return inputhook.enable_wx(app)
3506 elif ('qt4' in parameter_s) or ('qt' in parameter_s):
3507 return inputhook.enable_qt4(app)
3508 elif 'gtk' in parameter_s:
3509 return inputhook.enable_gtk(app)
3510 elif 'tk' in parameter_s:
3511 return inputhook.enable_tk(app)
35123494
3513 def magic_load_ext(self, module_str):3495 def magic_load_ext(self, module_str):
3514 """Load an IPython extension by its module name."""3496 """Load an IPython extension by its module name."""
3515 self.load_extension(module_str)3497 return self.load_extension(module_str)
35163498
3517 def magic_unload_ext(self, module_str):3499 def magic_unload_ext(self, module_str):
3518 """Unload an IPython extension by its module name."""3500 """Unload an IPython extension by its module name."""
@@ -3522,6 +3504,7 @@
3522 """Reload an IPython extension by its module name."""3504 """Reload an IPython extension by its module name."""
3523 self.reload_extension(module_str)3505 self.reload_extension(module_str)
35243506
3507 @testdec.skip_doctest
3525 def magic_install_profiles(self, s):3508 def magic_install_profiles(self, s):
3526 """Install the default IPython profiles into the .ipython dir.3509 """Install the default IPython profiles into the .ipython dir.
35273510
@@ -3576,5 +3559,58 @@
3576 shutil.copy(src, dst)3559 shutil.copy(src, dst)
3577 print "Installing default config file: %s" % dst3560 print "Installing default config file: %s" % dst
35783561
3562 # Pylab support: simple wrappers that activate pylab, load gui input
3563 # handling and modify slightly %run
3564
3565 @testdec.skip_doctest
3566 def _pylab_magic_run(self, parameter_s=''):
3567 Magic.magic_run(self, parameter_s,
3568 runner=mpl_runner(self.shell.safe_execfile))
3569
3570 _pylab_magic_run.__doc__ = magic_run.__doc__
3571
3572 @testdec.skip_doctest
3573 def magic_pylab(self, s):
3574 """Load numpy and matplotlib to work interactively.
3575
3576 %pylab [GUINAME]
3577
3578 This function lets you activate pylab (matplotlib, numpy and
3579 interactive support) at any point during an IPython session.
3580
3581 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3582 pylab and mlab, as well as all names from numpy and pylab.
3583
3584 Parameters
3585 ----------
3586 guiname : optional
3587 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or
3588 'tk'). If given, the corresponding Matplotlib backend is used,
3589 otherwise matplotlib's default (which you can override in your
3590 matplotlib config file) is used.
3591
3592 Examples
3593 --------
3594 In this case, where the MPL default is TkAgg:
3595 In [2]: %pylab
3596
3597 Welcome to pylab, a matplotlib-based Python environment.
3598 Backend in use: TkAgg
3599 For more information, type 'help(pylab)'.
3600
3601 But you can explicitly request a different backend:
3602 In [3]: %pylab qt
3603
3604 Welcome to pylab, a matplotlib-based Python environment.
3605 Backend in use: Qt4Agg
3606 For more information, type 'help(pylab)'.
3607 """
3608 self.shell.enable_pylab(s)
3609
3610 def magic_tb(self, s):
3611 """Print the last traceback with the currently active exception mode.
3612
3613 See %xmode for changing exception reporting modes."""
3614 self.shell.showtraceback()
35793615
3580# end Magic3616# end Magic
35813617
=== modified file 'IPython/core/prefilter.py'
--- IPython/core/prefilter.py 2009-11-15 07:37:49 +0000
+++ IPython/core/prefilter.py 2010-01-18 01:17:11 +0000
@@ -158,7 +158,7 @@
158 without worrying about *further* damaging state.158 without worrying about *further* damaging state.
159 """159 """
160 if not self._oinfo:160 if not self._oinfo:
161 self._oinfo = ip._ofind(self.ifun)161 self._oinfo = ip.shell._ofind(self.ifun)
162 return self._oinfo162 return self._oinfo
163163
164 def __str__(self): 164 def __str__(self):
@@ -362,7 +362,7 @@
362 line = transformer.transform(line, continue_prompt)362 line = transformer.transform(line, continue_prompt)
363 return line363 return line
364364
365 def prefilter_line(self, line, continue_prompt):365 def prefilter_line(self, line, continue_prompt=False):
366 """Prefilter a single input line as text.366 """Prefilter a single input line as text.
367367
368 This method prefilters a single line of text by calling the368 This method prefilters a single line of text by calling the
@@ -416,7 +416,7 @@
416 # print "prefiltered line: %r" % prefiltered416 # print "prefiltered line: %r" % prefiltered
417 return prefiltered417 return prefiltered
418418
419 def prefilter_lines(self, lines, continue_prompt):419 def prefilter_lines(self, lines, continue_prompt=False):
420 """Prefilter multiple input lines of text.420 """Prefilter multiple input lines of text.
421421
422 This is the main entry point for prefiltering multiple lines of422 This is the main entry point for prefiltering multiple lines of
@@ -427,11 +427,19 @@
427 which is the case when the user goes back to a multiline history427 which is the case when the user goes back to a multiline history
428 entry and presses enter.428 entry and presses enter.
429 """429 """
430 out = []430 llines = lines.rstrip('\n').split('\n')
431 for line in lines.rstrip('\n').split('\n'):431 # We can get multiple lines in one shot, where multiline input 'blends'
432 out.append(self.prefilter_line(line, continue_prompt))432 # into one line, in cases like recalling from the readline history
433 return '\n'.join(out)433 # buffer. We need to make sure that in such cases, we correctly
434434 # communicate downstream which line is first and which are continuation
435 # ones.
436 if len(llines) > 1:
437 out = '\n'.join([self.prefilter_line(line, lnum>0)
438 for lnum, line in enumerate(llines) ])
439 else:
440 out = self.prefilter_line(llines[0], continue_prompt)
441
442 return out
435443
436#-----------------------------------------------------------------------------444#-----------------------------------------------------------------------------
437# Prefilter transformers445# Prefilter transformers
@@ -508,6 +516,47 @@
508 return line516 return line
509517
510518
519_classic_prompt_re = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
520
521class PyPromptTransformer(PrefilterTransformer):
522 """Handle inputs that start with '>>> ' syntax."""
523
524 priority = Int(50, config=True)
525
526 def transform(self, line, continue_prompt):
527
528 if not line or line.isspace() or line.strip() == '...':
529 # This allows us to recognize multiple input prompts separated by
530 # blank lines and pasted in a single chunk, very common when
531 # pasting doctests or long tutorial passages.
532 return ''
533 m = _classic_prompt_re.match(line)
534 if m:
535 return line[len(m.group(0)):]
536 else:
537 return line
538
539
540_ipy_prompt_re = re.compile(r'(^[ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )')
541
542class IPyPromptTransformer(PrefilterTransformer):
543 """Handle inputs that start classic IPython prompt syntax."""
544
545 priority = Int(50, config=True)
546
547 def transform(self, line, continue_prompt):
548
549 if not line or line.isspace() or line.strip() == '...':
550 # This allows us to recognize multiple input prompts separated by
551 # blank lines and pasted in a single chunk, very common when
552 # pasting doctests or long tutorial passages.
553 return ''
554 m = _ipy_prompt_re.match(line)
555 if m:
556 return line[len(m.group(0)):]
557 else:
558 return line
559
511#-----------------------------------------------------------------------------560#-----------------------------------------------------------------------------
512# Prefilter checkers561# Prefilter checkers
513#-----------------------------------------------------------------------------562#-----------------------------------------------------------------------------
@@ -755,9 +804,17 @@
755 line = line_info.line804 line = line_info.line
756 continue_prompt = line_info.continue_prompt805 continue_prompt = line_info.continue_prompt
757806
758 if (continue_prompt and self.shell.autoindent and line.isspace() and807 if (continue_prompt and
759 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2 or808 self.shell.autoindent and
760 (self.shell.buffer[-1]).isspace() )):809 line.isspace() and
810
811 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2
812 or
813 not self.shell.buffer
814 or
815 (self.shell.buffer[-1]).isspace()
816 )
817 ):
761 line = ''818 line = ''
762819
763 self.shell.log(line, line, continue_prompt)820 self.shell.log(line, line, continue_prompt)
@@ -845,12 +902,11 @@
845 pre = line_info.pre902 pre = line_info.pre
846 continue_prompt = line_info.continue_prompt903 continue_prompt = line_info.continue_prompt
847 obj = line_info.ofind(self)['obj']904 obj = line_info.ofind(self)['obj']
848
849 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg905 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
850906
851 # This should only be active for single-line input!907 # This should only be active for single-line input!
852 if continue_prompt:908 if continue_prompt:
853 self.log(line,line,continue_prompt)909 self.shell.log(line,line,continue_prompt)
854 return line910 return line
855911
856 force_auto = isinstance(obj, IPyAutocall)912 force_auto = isinstance(obj, IPyAutocall)
@@ -967,7 +1023,9 @@
9671023
968_default_transformers = [1024_default_transformers = [
969 AssignSystemTransformer,1025 AssignSystemTransformer,
970 AssignMagicTransformer1026 AssignMagicTransformer,
1027 PyPromptTransformer,
1028 IPyPromptTransformer,
971]1029]
9721030
973_default_checkers = [1031_default_checkers = [
@@ -992,4 +1050,3 @@
992 HelpHandler,1050 HelpHandler,
993 EmacsHandler1051 EmacsHandler
994]1052]
995
9961053
=== modified file 'IPython/core/prompts.py'
--- IPython/core/prompts.py 2009-09-12 15:58:55 +0000
+++ IPython/core/prompts.py 2010-01-18 01:17:11 +0000
@@ -131,8 +131,14 @@
131 # Prompt/history count, with the actual digits replaced by dots. Used131 # Prompt/history count, with the actual digits replaced by dots. Used
132 # mainly in continuation prompts (prompt_in2)132 # mainly in continuation prompts (prompt_in2)
133 #r'\D': '${"."*len(str(self.cache.prompt_count))}',133 #r'\D': '${"."*len(str(self.cache.prompt_count))}',
134 # More robust form of the above expression, that uses __builtins__134
135 r'\D': '${"."*__builtins__.len(__builtins__.str(self.cache.prompt_count))}',135 # More robust form of the above expression, that uses the __builtin__
136 # module. Note that we can NOT use __builtins__ (note the 's'), because
137 # that can either be a dict or a module, and can even mutate at runtime,
138 # depending on the context (Python makes no guarantees on it). In
139 # contrast, __builtin__ is always a module object, though it must be
140 # explicitly imported.
141 r'\D': '${"."*__builtin__.len(__builtin__.str(self.cache.prompt_count))}',
136142
137 # Current working directory143 # Current working directory
138 r'\w': '${os.getcwd()}',144 r'\w': '${os.getcwd()}',
@@ -215,6 +221,7 @@
215 out = '<ERROR: %s>' % msg221 out = '<ERROR: %s>' % msg
216 except Exception,msg:222 except Exception,msg:
217 out = '<ERROR: %s>' % msg223 out = '<ERROR: %s>' % msg
224 #raise # dbg
218 return out225 return out
219226
220class BasePrompt(object):227class BasePrompt(object):
@@ -549,18 +556,23 @@
549 # print "Got prompt: ", outprompt556 # print "Got prompt: ", outprompt
550 if self.do_full_cache:557 if self.do_full_cache:
551 cout_write(outprompt)558 cout_write(outprompt)
552 else:
553 print "self.do_full_cache = False"
554559
555 # and now call a possibly user-defined print mechanism560 # and now call a possibly user-defined print mechanism. Note that
556 manipulated_val = self.display(arg)561 # self.display typically prints as a side-effect, we don't do any
562 # printing to stdout here.
563 try:
564 manipulated_val = self.display(arg)
565 except TypeError:
566 # If the user's display hook didn't return a string we can
567 # print, we're done. Happens commonly if they return None
568 cout_write('\n')
569 return
557 570
558 # user display hooks can change the variable to be stored in571 # user display hooks can change the variable to be stored in
559 # output history572 # output history
560
561 if manipulated_val is not None:573 if manipulated_val is not None:
562 arg = manipulated_val574 arg = manipulated_val
563 575
564 # avoid recursive reference when displaying _oh/Out576 # avoid recursive reference when displaying _oh/Out
565 if arg is not self.user_ns['_oh']:577 if arg is not self.user_ns['_oh']:
566 self.update(arg)578 self.update(arg)
567579
=== added file 'IPython/core/pylabtools.py'
--- IPython/core/pylabtools.py 1970-01-01 00:00:00 +0000
+++ IPython/core/pylabtools.py 2010-01-18 01:17:11 +0000
@@ -0,0 +1,145 @@
1# -*- coding: utf-8 -*-
2"""Pylab (matplotlib) support utilities.
3
4Authors
5-------
6Fernando Perez.
7"""
8
9#-----------------------------------------------------------------------------
10# Copyright (C) 2009 The IPython Development Team
11#
12# Distributed under the terms of the BSD License. The full license is in
13# the file COPYING, distributed as part of this software.
14#-----------------------------------------------------------------------------
15#-----------------------------------------------------------------------------
16# Imports
17#-----------------------------------------------------------------------------
18from IPython.utils.genutils import flag_calls
19
20#-----------------------------------------------------------------------------
21# Main classes and functions
22#-----------------------------------------------------------------------------
23
24def pylab_activate(user_ns, gui=None, import_all=True):
25 """Activate pylab mode in the user's namespace.
26
27 Loads and initializes numpy, matplotlib and friends for interactive use.
28
29 Parameters
30 ----------
31 user_ns : dict
32 Namespace where the imports will occur.
33
34 gui : optional, string
35 A valid gui name following the conventions of the %gui magic.
36
37 import_all : optional, boolean
38 If true, an 'import *' is done from numpy and pylab.
39
40 Returns
41 -------
42 The actual gui used (if not given as input, it was obtained from matplotlib
43 itself, and will be needed next to configure IPython's gui integration.
44 """
45
46 # Initialize matplotlib to interactive mode always
47 import matplotlib
48
49 # If user specifies a GUI, that dictates the backend, otherwise we read the
50 # user's mpl default from the mpl rc structure
51 g2b = {'tk': 'TkAgg',
52 'gtk': 'GTKAgg',
53 'wx': 'WXAgg',
54 'qt': 'Qt4Agg', # qt3 not supported
55 'qt4': 'Qt4Agg' }
56
57 if gui:
58 # select backend based on requested gui
59 backend = g2b[gui]
60 else:
61 backend = matplotlib.rcParams['backend']
62 # In this case, we need to find what the appropriate gui selection call
63 # should be for IPython, so we can activate inputhook accordingly
64 b2g = dict(zip(g2b.values(),g2b.keys()))
65 gui = b2g[backend]
66
67 # We must set the desired backend before importing pylab
68 matplotlib.use(backend)
69
70 # This must be imported last in the matplotlib series, after
71 # backend/interactivity choices have been made
72 import matplotlib.pylab as pylab
73
74 # XXX For now leave this commented out, but depending on discussions with
75 # mpl-dev, we may be able to allow interactive switching...
76 #import matplotlib.pyplot
77 #matplotlib.pyplot.switch_backend(backend)
78
79 pylab.show._needmain = False
80 # We need to detect at runtime whether show() is called by the user.
81 # For this, we wrap it into a decorator which adds a 'called' flag.
82 pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
83
84 # Import numpy as np/pyplot as plt are conventions we're trying to
85 # somewhat standardize on. Making them available to users by default
86 # will greatly help this.
87 exec ("import numpy\n"
88 "import matplotlib\n"
89 "from matplotlib import pylab, mlab, pyplot\n"
90 "np = numpy\n"
91 "plt = pyplot\n"
92 ) in user_ns
93
94 if import_all:
95 exec("from matplotlib.pylab import *\n"
96 "from numpy import *\n") in user_ns
97
98 matplotlib.interactive(True)
99
100 print """
101Welcome to pylab, a matplotlib-based Python environment [backend: %s].
102For more information, type 'help(pylab)'.""" % backend
103
104 return gui
105
106# We need a little factory function here to create the closure where
107# safe_execfile can live.
108def mpl_runner(safe_execfile):
109 """Factory to return a matplotlib-enabled runner for %run.
110
111 Parameters
112 ----------
113 safe_execfile : function
114 This must be a function with the same interface as the
115 :meth:`safe_execfile` method of IPython.
116
117 Returns
118 -------
119 A function suitable for use as the ``runner`` argument of the %run magic
120 function.
121 """
122
123 def mpl_execfile(fname,*where,**kw):
124 """matplotlib-aware wrapper around safe_execfile.
125
126 Its interface is identical to that of the :func:`execfile` builtin.
127
128 This is ultimately a call to execfile(), but wrapped in safeties to
129 properly handle interactive rendering."""
130
131 import matplotlib
132 import matplotlib.pylab as pylab
133
134 #print '*** Matplotlib runner ***' # dbg
135 # turn off rendering until end of script
136 is_interactive = matplotlib.rcParams['interactive']
137 matplotlib.interactive(False)
138 safe_execfile(fname,*where,**kw)
139 matplotlib.interactive(is_interactive)
140 # make rendering call now, if the user tried to do it
141 if pylab.draw_if_interactive.called:
142 pylab.draw()
143 pylab.draw_if_interactive.called = False
144
145 return mpl_execfile
0146
=== modified file 'IPython/core/quitter.py' (properties changed: -x to +x)
--- IPython/core/quitter.py 2009-08-27 19:20:47 +0000
+++ IPython/core/quitter.py 2010-01-18 01:17:11 +0000
@@ -1,10 +1,10 @@
1#!/usr/bin/env python1# coding: utf-8
2# encoding: utf-8
3"""2"""
4A simple class for quitting IPython.3A simple class for quitting IPython.
54
6Authors:5Authors
76-------
7* Fernando Perez
8* Brian Granger8* Brian Granger
9"""9"""
1010
@@ -19,6 +19,7 @@
19# Imports19# Imports
20#-----------------------------------------------------------------------------20#-----------------------------------------------------------------------------
2121
22import sys
2223
23class Quitter(object):24class Quitter(object):
24 """Simple class to handle exit, similar to Python 2.5's.25 """Simple class to handle exit, similar to Python 2.5's.
@@ -30,9 +31,13 @@
30 self.shell = shell31 self.shell = shell
31 self.name = name32 self.name = name
32 33
33 def __repr__(self):34 def __str__(self):
34 return 'Type %s() to exit.' % self.name35 return 'Type %s() to exit.' % self.name
35 __str__ = __repr__
3636
37 def __call__(self):37 def __call__(self):
38 self.shell.exit()
39\ No newline at end of file38\ No newline at end of file
39 self.shell.ask_exit()
40
41 # Repr MUST return a string, else display like pprint hooks get confused
42 def __repr__(self):
43 self.shell.ask_exit()
44 return 'Bye.'
4045
=== modified file 'IPython/core/release.py'
--- IPython/core/release.py 2009-12-30 19:36:04 +0000
+++ IPython/core/release.py 2010-01-18 01:17:11 +0000
@@ -23,7 +23,7 @@
23development = True # change this to False to do a release23development = True # change this to False to do a release
24version_base = '0.11'24version_base = '0.11'
25branch = 'ipython'25branch = 'ipython'
26revision = '1219'26revision = '1346'
2727
28if development:28if development:
29 if branch == 'ipython':29 if branch == 'ipython':
3030
=== removed file 'IPython/core/tests/obj_del.py'
--- IPython/core/tests/obj_del.py 2009-09-12 15:58:55 +0000
+++ IPython/core/tests/obj_del.py 1970-01-01 00:00:00 +0000
@@ -1,35 +0,0 @@
1"""Test code for https://bugs.launchpad.net/ipython/+bug/239054
2
3WARNING: this script exits IPython! It MUST be run in a subprocess.
4
5When you run the following script from CPython it prints:
6__init__ is here
7__del__ is here
8
9and creates the __del__.txt file
10
11When you run it from IPython it prints:
12__init__ is here
13
14When you exit() or Exit from IPython neothing is printed and no file is created
15(the file thing is to make sure __del__ is really never called and not that
16just the output is eaten).
17
18Note that if you call %reset in IPython then everything is Ok.
19
20IPython should do the equivalent of %reset and release all the references it
21holds before exit. This behavior is important when working with binding objects
22that rely on __del__. If the current behavior has some use case then I suggest
23to add a configuration option to IPython to control it.
24"""
25import sys
26
27class A(object):
28 def __del__(self):
29 print 'obj_del.py: object A deleted'
30
31a = A()
32
33# Now, we force an exit, the caller will check that the del printout was given
34_ip = get_ipython()
35_ip.ask_exit()
360
=== added file 'IPython/core/tests/simpleerr.py'
--- IPython/core/tests/simpleerr.py 1970-01-01 00:00:00 +0000
+++ IPython/core/tests/simpleerr.py 2010-01-18 01:17:11 +0000
@@ -0,0 +1,32 @@
1"""Error script. DO NOT EDIT FURTHER! It will break exception doctests!!!"""
2import sys
3
4def div0():
5 "foo"
6 x = 1
7 y = 0
8 x/y
9
10def sysexit(stat, mode):
11 raise SystemExit(stat, 'Mode = %s' % mode)
12
13def bar(mode):
14 "bar"
15 if mode=='div':
16 div0()
17 elif mode=='exit':
18 try:
19 stat = int(sys.argv[2])
20 except:
21 stat = 1
22 sysexit(stat, mode)
23 else:
24 raise ValueError('Unknown mode')
25
26if __name__ == '__main__':
27 try:
28 mode = sys.argv[1]
29 except IndexError:
30 mode = 'div'
31
32 bar(mode)
033
=== modified file 'IPython/core/tests/tclass.py'
--- IPython/core/tests/tclass.py 2009-07-02 18:59:10 +0000
+++ IPython/core/tests/tclass.py 2010-01-18 01:17:11 +0000
@@ -1,22 +1,19 @@
1"""Simple script to instantiate a class for testing %run"""1"""Simple script to be run *twice*, to check reference counting bugs.
2
3See test_run for details."""
24
3import sys5import sys
46
5# An external test will check that calls to f() work after %run7# We want to ensure that while objects remain available for immediate access,
6class foo: pass8# objects from *previous* runs of the same script get collected, to avoid
79# accumulating massive amounts of old references.
8def f():
9 return foo()
10
11# We also want to ensure that while objects remain available for immediate
12# access, objects from *previous* runs of the same script get collected, to
13# avoid accumulating massive amounts of old references.
14class C(object):10class C(object):
15 def __init__(self,name):11 def __init__(self,name):
16 self.name = name12 self.name = name
17 13
18 def __del__(self):14 def __del__(self):
19 print 'tclass.py: deleting object:',self.name15 print 'tclass.py: deleting object:',self.name
16 sys.stdout.flush()
2017
21try:18try:
22 name = sys.argv[1]19 name = sys.argv[1]
@@ -25,3 +22,10 @@
25else:22else:
26 if name.startswith('C'):23 if name.startswith('C'):
27 c = C(name)24 c = C(name)
25
26#print >> sys.stderr, "ARGV:", sys.argv # dbg
27
28# This next print statement is NOT debugging, we're making the check on a
29# completely separate process so we verify by capturing stdout:
30print 'ARGV 1-:', sys.argv[1:]
31sys.stdout.flush()
2832
=== added file 'IPython/core/tests/test_completer.py'
--- IPython/core/tests/test_completer.py 1970-01-01 00:00:00 +0000
+++ IPython/core/tests/test_completer.py 2010-01-18 01:17:11 +0000
@@ -0,0 +1,35 @@
1"""Tests for the IPython tab-completion machinery.
2"""
3#-----------------------------------------------------------------------------
4# Module imports
5#-----------------------------------------------------------------------------
6
7# stdlib
8import sys
9
10# third party
11import nose.tools as nt
12
13# our own packages
14from IPython.core import completer
15
16#-----------------------------------------------------------------------------
17# Test functions
18#-----------------------------------------------------------------------------
19def test_protect_filename():
20 pairs = [ ('abc','abc'),
21 (' abc',r'\ abc'),
22 ('a bc',r'a\ bc'),
23 ('a bc',r'a\ \ bc'),
24 (' bc',r'\ \ bc'),
25 ]
26 # On posix, we also protect parens
27 if sys.platform != 'win32':
28 pairs.extend( [('a(bc',r'a\(bc'),
29 ('a)bc',r'a\)bc'),
30 ('a( )bc',r'a\(\ \)bc'),
31 ] )
32 # run the actual tests
33 for s1, s2 in pairs:
34 s1p = completer.protect_filename(s1)
35 nt.assert_equals(s1p, s2)
036
=== modified file 'IPython/core/tests/test_iplib.py'
--- IPython/core/tests/test_iplib.py 2009-11-09 01:49:24 +0000
+++ IPython/core/tests/test_iplib.py 2010-01-18 01:17:11 +0000
@@ -13,45 +13,233 @@
13import nose.tools as nt13import nose.tools as nt
1414
15# our own packages15# our own packages
16from IPython.core import iplib16from IPython.testing import decorators as dec
17from IPython.core import ipapi17from IPython.testing.globalipapp import get_ipython
18
1918
20#-----------------------------------------------------------------------------19#-----------------------------------------------------------------------------
21# Globals20# Globals
22#-----------------------------------------------------------------------------21#-----------------------------------------------------------------------------
2322
24# Useful global ipapi object and main IPython one. Unfortunately we have a23# Get the public instance of IPython
25# long precedent of carrying the 'ipapi' global object which is injected into24ip = get_ipython()
26# the system namespace as _ip, but that keeps a pointer to the actual IPython
27# InteractiveShell instance, which is named IP. Since in testing we do need
28# access to the real thing (we want to probe beyond what ipapi exposes), make
29# here a global reference to each. In general, things that are exposed by the
30# ipapi instance should be read from there, but we also will often need to use
31# the actual IPython one.
32
33# Get the public instance of IPython, and if it's None, make one so we can use
34# it for testing
35ip = ipapi.get()
36if ip is None:
37 # IPython not running yet, make one from the testing machinery for
38 # consistency when the test suite is being run via iptest
39 from IPython.testing.plugin import ipdoctest
40 ip = ipapi.get()
4125
42#-----------------------------------------------------------------------------26#-----------------------------------------------------------------------------
43# Test functions27# Test functions
44#-----------------------------------------------------------------------------28#-----------------------------------------------------------------------------
4529
30@dec.parametric
46def test_reset():31def test_reset():
47 """reset must clear most namespaces."""32 """reset must clear most namespaces."""
48 ip.reset() # first, it should run without error33 # The number of variables in the private user_config_ns is not zero, but it
49 # Then, check that most namespaces end up empty34 # should be constant regardless of what we do
35 nvars_config_ns = len(ip.user_config_ns)
36
37 # Check that reset runs without error
38 ip.reset()
39
40 # Once we've reset it (to clear of any junk that might have been there from
41 # other tests, we can count how many variables are in the user's namespace
42 nvars_user_ns = len(ip.user_ns)
43
44 # Now add a few variables to user_ns, and check that reset clears them
45 ip.user_ns['x'] = 1
46 ip.user_ns['y'] = 1
47 ip.reset()
48
49 # Finally, check that all namespaces have only as many variables as we
50 # expect to find in them:
50 for ns in ip.ns_refs_table:51 for ns in ip.ns_refs_table:
51 if ns is ip.user_ns:52 if ns is ip.user_ns:
52 # The user namespace is reset with some data, so we can't check for
53 # it being empty
54 continue
55 nt.assert_equals(len(ns),0)
56
57
58\ No newline at end of file53\ No newline at end of file
54 nvars_expected = nvars_user_ns
55 elif ns is ip.user_config_ns:
56 nvars_expected = nvars_config_ns
57 else:
58 nvars_expected = 0
59
60 yield nt.assert_equals(len(ns), nvars_expected)
61
62
63# Tests for reporting of exceptions in various modes, handling of SystemExit,
64# and %tb functionality. This is really a mix of testing ultraTB and iplib.
65
66def doctest_tb_plain():
67 """
68In [18]: xmode plain
69Exception reporting mode: Plain
70
71In [19]: run simpleerr.py
72Traceback (most recent call last):
73 ...line 32, in <module>
74 bar(mode)
75 ...line 16, in bar
76 div0()
77 ...line 8, in div0
78 x/y
79ZeroDivisionError: integer division or modulo by zero
80 """
81
82
83def doctest_tb_context():
84 """
85In [3]: xmode context
86Exception reporting mode: Context
87
88In [4]: run simpleerr.py
89---------------------------------------------------------------------------
90ZeroDivisionError Traceback (most recent call last)
91<BLANKLINE>
92... in <module>()
93 30 mode = 'div'
94 31
95---> 32 bar(mode)
96 33
97 34
98<BLANKLINE>
99... in bar(mode)
100 14 "bar"
101 15 if mode=='div':
102---> 16 div0()
103 17 elif mode=='exit':
104 18 try:
105<BLANKLINE>
106... in div0()
107 6 x = 1
108 7 y = 0
109----> 8 x/y
110 9
111 10 def sysexit(stat, mode):
112<BLANKLINE>
113ZeroDivisionError: integer division or modulo by zero
114"""
115
116
117def doctest_tb_verbose():
118 """
119In [5]: xmode verbose
120Exception reporting mode: Verbose
121
122In [6]: run simpleerr.py
123---------------------------------------------------------------------------
124ZeroDivisionError Traceback (most recent call last)
125<BLANKLINE>
126... in <module>()
127 30 mode = 'div'
128 31
129---> 32 bar(mode)
130 global bar = <function bar at ...>
131 global mode = 'div'
132 33
133 34
134<BLANKLINE>
135... in bar(mode='div')
136 14 "bar"
137 15 if mode=='div':
138---> 16 div0()
139 global div0 = <function div0 at ...>
140 17 elif mode=='exit':
141 18 try:
142<BLANKLINE>
143... in div0()
144 6 x = 1
145 7 y = 0
146----> 8 x/y
147 x = 1
148 y = 0
149 9
150 10 def sysexit(stat, mode):
151<BLANKLINE>
152ZeroDivisionError: integer division or modulo by zero
153 """
154
155
156def doctest_tb_sysexit():
157 """
158In [17]: %xmode plain
159Exception reporting mode: Plain
160
161In [18]: %run simpleerr.py exit
162An exception has occurred, use %tb to see the full traceback.
163SystemExit: (1, 'Mode = exit')
164
165In [19]: %run simpleerr.py exit 2
166An exception has occurred, use %tb to see the full traceback.
167SystemExit: (2, 'Mode = exit')
168
169In [20]: %tb
170Traceback (most recent call last):
171 File ... in <module>
172 bar(mode)
173 File ... line 22, in bar
174 sysexit(stat, mode)
175 File ... line 11, in sysexit
176 raise SystemExit(stat, 'Mode = %s' % mode)
177SystemExit: (2, 'Mode = exit')
178
179In [21]: %xmode context
180Exception reporting mode: Context
181
182In [22]: %tb
183---------------------------------------------------------------------------
184SystemExit Traceback (most recent call last)
185<BLANKLINE>
186...<module>()
187 30 mode = 'div'
188 31
189---> 32 bar(mode)
190 33
191 34
192<BLANKLINE>
193...bar(mode)
194 20 except:
195 21 stat = 1
196---> 22 sysexit(stat, mode)
197 23 else:
198 24 raise ValueError('Unknown mode')
199<BLANKLINE>
200...sysexit(stat, mode)
201 9
202 10 def sysexit(stat, mode):
203---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
204 12
205 13 def bar(mode):
206<BLANKLINE>
207SystemExit: (2, 'Mode = exit')
208
209In [23]: %xmode verbose
210Exception reporting mode: Verbose
211
212In [24]: %tb
213---------------------------------------------------------------------------
214SystemExit Traceback (most recent call last)
215<BLANKLINE>
216... in <module>()
217 30 mode = 'div'
218 31
219---> 32 bar(mode)
220 global bar = <function bar at ...>
221 global mode = 'exit'
222 33
223 34
224<BLANKLINE>
225... in bar(mode='exit')
226 20 except:
227 21 stat = 1
228---> 22 sysexit(stat, mode)
229 global sysexit = <function sysexit at ...>
230 stat = 2
231 mode = 'exit'
232 23 else:
233 24 raise ValueError('Unknown mode')
234<BLANKLINE>
235... in sysexit(stat=2, mode='exit')
236 9
237 10 def sysexit(stat, mode):
238---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
239 global SystemExit = undefined
240 stat = 2
241 mode = 'exit'
242 12
243 13 def bar(mode):
244<BLANKLINE>
245SystemExit: (2, 'Mode = exit')
246 """
59247
=== modified file 'IPython/core/tests/test_magic.py'
--- IPython/core/tests/test_magic.py 2009-09-18 02:40:20 +0000
+++ IPython/core/tests/test_magic.py 2010-01-18 01:17:11 +0000
@@ -2,22 +2,31 @@
22
3Needs to be run by nose (to make ipython session available).3Needs to be run by nose (to make ipython session available).
4"""4"""
55from __future__ import absolute_import
6
7#-----------------------------------------------------------------------------
8# Imports
9#-----------------------------------------------------------------------------
10
11# stdlib
6import os12import os
7import sys13import sys
8import tempfile14import tempfile
9import types15import types
10from cStringIO import StringIO16from cStringIO import StringIO
1117
18# third-party
12import nose.tools as nt19import nose.tools as nt
1320
21# our own
22from IPython.utils import genutils
14from IPython.utils.platutils import find_cmd, get_long_path_name23from IPython.utils.platutils import find_cmd, get_long_path_name
15from IPython.testing import decorators as dec24from IPython.testing import decorators as dec
16from IPython.testing import tools as tt25from IPython.testing import tools as tt
1726
18#-----------------------------------------------------------------------------27#-----------------------------------------------------------------------------
19# Test functions begin28# Test functions begin
2029#-----------------------------------------------------------------------------
21def test_rehashx():30def test_rehashx():
22 # clear up everything31 # clear up everything
23 _ip = get_ipython()32 _ip = get_ipython()
@@ -37,6 +46,19 @@
37 yield (nt.assert_true, len(scoms) > 10)46 yield (nt.assert_true, len(scoms) > 10)
3847
3948
49def test_magic_parse_options():
50 """Test that we don't mangle paths when parsing magic options."""
51 ip = get_ipython()
52 path = 'c:\\x'
53 opts = ip.parse_options('-f %s' % path,'f:')[0]
54 # argv splitting is os-dependent
55 if os.name == 'posix':
56 expected = 'c:x'
57 else:
58 expected = path
59 nt.assert_equals(opts['f'], expected)
60
61
40def doctest_hist_f():62def doctest_hist_f():
41 """Test %hist -f with temporary filename.63 """Test %hist -f with temporary filename.
4264
@@ -45,35 +67,93 @@
45 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')67 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
4668
47 In [11]: %hist -n -f $tfile 369 In [11]: %hist -n -f $tfile 3
70
71 In [13]: import os; os.unlink(tfile)
48 """72 """
4973
5074
51def doctest_hist_r():75def doctest_hist_r():
52 """Test %hist -r76 """Test %hist -r
5377
54 XXX - This test is not recording the output correctly. Not sure why...78 XXX - This test is not recording the output correctly. For some reason, in
5579 testing mode the raw history isn't getting populated. No idea why.
56 In [20]: 'hist' in _ip.lsmagic()80 Disabling the output checking for now, though at least we do run it.
57 Out[20]: True81
5882 In [1]: 'hist' in _ip.lsmagic()
59 In [6]: x=183 Out[1]: True
6084
61 In [7]: %hist -n -r 285 In [2]: x=1
62 x=1 # random86
63 hist -n -r 2 # random87 In [3]: %hist -r 2
64 """88 x=1 # random
6589 %hist -r 2
66# This test is known to fail on win32.90 """
67# See ticket https://bugs.launchpad.net/bugs/36633491
68def test_obj_del():92def doctest_hist_op():
69 _ip = get_ipython()93 """Test %hist -op
70 """Test that object's __del__ methods are called on exit."""94
71 test_dir = os.path.dirname(__file__)95 In [1]: class b:
72 del_file = os.path.join(test_dir,'obj_del.py')96 ...: pass
73 ipython_cmd = find_cmd('ipython')97 ...:
74 out = _ip.getoutput('%s %s' % (ipython_cmd, del_file))98
75 nt.assert_equals(out,'obj_del.py: object A deleted')99 In [2]: class s(b):
76100 ...: def __str__(self):
101 ...: return 's'
102 ...:
103
104 In [3]:
105
106 In [4]: class r(b):
107 ...: def __repr__(self):
108 ...: return 'r'
109 ...:
110
111 In [5]: class sr(s,r): pass
112 ...:
113
114 In [6]:
115
116 In [7]: bb=b()
117
118 In [8]: ss=s()
119
120 In [9]: rr=r()
121
122 In [10]: ssrr=sr()
123
124 In [11]: bb
125 Out[11]: <...b instance at ...>
126
127 In [12]: ss
128 Out[12]: <...s instance at ...>
129
130 In [13]:
131
132 In [14]: %hist -op
133 >>> class b:
134 ... pass
135 ...
136 >>> class s(b):
137 ... def __str__(self):
138 ... return 's'
139 ...
140 >>>
141 >>> class r(b):
142 ... def __repr__(self):
143 ... return 'r'
144 ...
145 >>> class sr(s,r): pass
146 >>>
147 >>> bb=b()
148 >>> ss=s()
149 >>> rr=r()
150 >>> ssrr=sr()
151 >>> bb
152 <...b instance at ...>
153 >>> ss
154 <...s instance at ...>
155 >>>
156 """
77157
78def test_shist():158def test_shist():
79 # Simple tests of ShadowHist class - test generator.159 # Simple tests of ShadowHist class - test generator.
@@ -97,8 +177,12 @@
97 yield nt.assert_equal,s.get(2),'world'177 yield nt.assert_equal,s.get(2),'world'
98 178
99 shutil.rmtree(tfile)179 shutil.rmtree(tfile)
180
100 181
101@dec.skipif_not_numpy182# XXX failing for now, until we get clearcmd out of quarantine. But we should
183# fix this and revert the skip to happen only if numpy is not around.
184#@dec.skipif_not_numpy
185@dec.skipknownfailure
102def test_numpy_clear_array_undec():186def test_numpy_clear_array_undec():
103 from IPython.extensions import clearcmd187 from IPython.extensions import clearcmd
104188
@@ -109,162 +193,8 @@
109 yield (nt.assert_false, 'a' in _ip.user_ns)193 yield (nt.assert_false, 'a' in _ip.user_ns)
110 194
111195
112@dec.skip()
113def test_fail_dec(*a,**k):
114 yield nt.assert_true, False
115
116@dec.skip('This one shouldn not run')
117def test_fail_dec2(*a,**k):
118 yield nt.assert_true, False
119
120@dec.skipknownfailure
121def test_fail_dec3(*a,**k):
122 yield nt.assert_true, False
123
124
125def doctest_refbug():
126 """Very nasty problem with references held by multiple runs of a script.
127 See: https://bugs.launchpad.net/ipython/+bug/269966
128
129 In [1]: _ip.clear_main_mod_cache()
130
131 In [2]: run refbug
132
133 In [3]: call_f()
134 lowercased: hello
135
136 In [4]: run refbug
137
138 In [5]: call_f()
139 lowercased: hello
140 lowercased: hello
141 """
142
143#-----------------------------------------------------------------------------
144# Tests for %run
145#-----------------------------------------------------------------------------
146
147# %run is critical enough that it's a good idea to have a solid collection of
148# tests for it, some as doctests and some as normal tests.
149
150def doctest_run_ns():
151 """Classes declared %run scripts must be instantiable afterwards.
152
153 In [11]: run tclass foo
154
155 In [12]: isinstance(f(),foo)
156 Out[12]: True
157 """
158
159
160def doctest_run_ns2():
161 """Classes declared %run scripts must be instantiable afterwards.
162
163 In [4]: run tclass C-first_pass
164
165 In [5]: run tclass C-second_pass
166 tclass.py: deleting object: C-first_pass
167 """
168
169def doctest_run_builtins():
170 """Check that %run doesn't damage __builtins__ via a doctest.
171
172 This is similar to the test_run_builtins, but I want *both* forms of the
173 test to catch any possible glitches in our testing machinery, since that
174 modifies %run somewhat. So for this, we have both a normal test (below)
175 and a doctest (this one).
176
177 In [1]: import tempfile
178
179 In [2]: bid1 = id(__builtins__)
180
181 In [3]: fname = tempfile.mkstemp()[1]
182
183 In [3]: f = open(fname,'w')
184
185 In [4]: f.write('pass\\n')
186
187 In [5]: f.flush()
188
189 In [6]: print type(__builtins__)
190 <type 'module'>
191
192 In [7]: %run "$fname"
193
194 In [7]: f.close()
195
196 In [8]: bid2 = id(__builtins__)
197
198 In [9]: print type(__builtins__)
199 <type 'module'>
200
201 In [10]: bid1 == bid2
202 Out[10]: True
203
204 In [12]: try:
205 ....: os.unlink(fname)
206 ....: except:
207 ....: pass
208 ....:
209 """
210
211# For some tests, it will be handy to organize them in a class with a common
212# setup that makes a temp file
213
214class TestMagicRun(object):
215
216 def setup(self):
217 """Make a valid python temp file."""
218 fname = tempfile.mkstemp()[1]
219 f = open(fname,'w')
220 f.write('pass\n')
221 f.flush()
222 self.tmpfile = f
223 self.fname = fname
224
225 def run_tmpfile(self):
226 _ip = get_ipython()
227 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
228 # See below and ticket https://bugs.launchpad.net/bugs/366353
229 _ip.magic('run "%s"' % self.fname)
230
231 def test_builtins_id(self):
232 """Check that %run doesn't damage __builtins__ """
233 _ip = get_ipython()
234 # Test that the id of __builtins__ is not modified by %run
235 bid1 = id(_ip.user_ns['__builtins__'])
236 self.run_tmpfile()
237 bid2 = id(_ip.user_ns['__builtins__'])
238 tt.assert_equals(bid1, bid2)
239
240 def test_builtins_type(self):
241 """Check that the type of __builtins__ doesn't change with %run.
242
243 However, the above could pass if __builtins__ was already modified to
244 be a dict (it should be a module) by a previous use of %run. So we
245 also check explicitly that it really is a module:
246 """
247 _ip = get_ipython()
248 self.run_tmpfile()
249 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
250
251 def test_prompts(self):
252 """Test that prompts correctly generate after %run"""
253 self.run_tmpfile()
254 _ip = get_ipython()
255 p2 = str(_ip.outputcache.prompt2).strip()
256 nt.assert_equals(p2[:3], '...')
257
258 def teardown(self):
259 self.tmpfile.close()
260 try:
261 os.unlink(self.fname)
262 except:
263 # On Windows, even though we close the file, we still can't delete
264 # it. I have no clue why
265 pass
266
267# Multiple tests for clipboard pasting196# Multiple tests for clipboard pasting
197@dec.parametric
268def test_paste():198def test_paste():
269 _ip = get_ipython()199 _ip = get_ipython()
270 def paste(txt, flags='-q'):200 def paste(txt, flags='-q'):
@@ -286,11 +216,11 @@
286 # Run tests with fake clipboard function216 # Run tests with fake clipboard function
287 user_ns.pop('x', None)217 user_ns.pop('x', None)
288 paste('x=1')218 paste('x=1')
289 yield (nt.assert_equal, user_ns['x'], 1)219 yield nt.assert_equal(user_ns['x'], 1)
290220
291 user_ns.pop('x', None)221 user_ns.pop('x', None)
292 paste('>>> x=2')222 paste('>>> x=2')
293 yield (nt.assert_equal, user_ns['x'], 2)223 yield nt.assert_equal(user_ns['x'], 2)
294224
295 paste("""225 paste("""
296 >>> x = [1,2,3]226 >>> x = [1,2,3]
@@ -299,14 +229,14 @@
299 ... y.append(i**2)229 ... y.append(i**2)
300 ...230 ...
301 """)231 """)
302 yield (nt.assert_equal, user_ns['x'], [1,2,3])232 yield nt.assert_equal(user_ns['x'], [1,2,3])
303 yield (nt.assert_equal, user_ns['y'], [1,4,9])233 yield nt.assert_equal(user_ns['y'], [1,4,9])
304234
305 # Now, test that paste -r works235 # Now, test that paste -r works
306 user_ns.pop('x', None)236 user_ns.pop('x', None)
307 yield (nt.assert_false, 'x' in user_ns)237 yield nt.assert_false('x' in user_ns)
308 _ip.magic('paste -r')238 _ip.magic('paste -r')
309 yield (nt.assert_equal, user_ns['x'], [1,2,3])239 yield nt.assert_equal(user_ns['x'], [1,2,3])
310240
311 # Also test paste echoing, by temporarily faking the writer241 # Also test paste echoing, by temporarily faking the writer
312 w = StringIO()242 w = StringIO()
@@ -320,12 +250,29 @@
320 out = w.getvalue()250 out = w.getvalue()
321 finally:251 finally:
322 _ip.write = writer252 _ip.write = writer
323 yield (nt.assert_equal, user_ns['a'], 100)253 yield nt.assert_equal(user_ns['a'], 100)
324 yield (nt.assert_equal, user_ns['b'], 200)254 yield nt.assert_equal(user_ns['b'], 200)
325 yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n")255 yield nt.assert_equal(out, code+"\n## -- End pasted text --\n")
326 256
327 finally:257 finally:
328 # This should be in a finally clause, instead of the bare except above.258 # This should be in a finally clause, instead of the bare except above.
329 # Restore original hook259 # Restore original hook
330 hooks.clipboard_get = original_clip260 hooks.clipboard_get = original_clip
331261
262
263def test_time():
264 _ip.magic('time None')
265
266
267def doctest_time():
268 """
269 In [10]: %time None
270 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
271 Wall time: 0.00 s
272 """
273
274def test_doctest_mode():
275 "Toggle doctest_mode twice, it should be a no-op and run without error"
276 _ip.magic('doctest_mode')
277 _ip.magic('doctest_mode')
278
332279
=== added file 'IPython/core/tests/test_prefilter.py'
--- IPython/core/tests/test_prefilter.py 1970-01-01 00:00:00 +0000
+++ IPython/core/tests/test_prefilter.py 2010-01-18 01:17:11 +0000
@@ -0,0 +1,34 @@
1"""Tests for input manipulation machinery."""
2
3#-----------------------------------------------------------------------------
4# Imports
5#-----------------------------------------------------------------------------
6import nose.tools as nt
7
8from IPython.testing import tools as tt, decorators as dec
9
10#-----------------------------------------------------------------------------
11# Tests
12#-----------------------------------------------------------------------------
13@dec.parametric
14def test_prefilter():
15 """Test user input conversions"""
16
17 # pairs of (raw, expected correct) input
18 pairs = [ ('2+2','2+2'),
19 ('>>> 2+2','2+2'),
20 ('>>> # This is a comment\n'
21 '... 2+2',
22 '# This is a comment\n'
23 '2+2'),
24 # Some IPython input
25 ('In [1]: 1', '1'),
26 ('In [2]: for i in range(5):\n'
27 ' ...: print i,',
28 'for i in range(5):\n'
29 ' print i,'),
30 ]
31
32 ip = get_ipython()
33 for raw, correct in pairs:
34 yield nt.assert_equals(ip.prefilter(raw), correct)
035
=== added file 'IPython/core/tests/test_run.py'
--- IPython/core/tests/test_run.py 1970-01-01 00:00:00 +0000
+++ IPython/core/tests/test_run.py 2010-01-18 01:17:11 +0000
@@ -0,0 +1,174 @@
1"""Tests for code execution (%run and related), which is particularly tricky.
2
3Because of how %run manages namespaces, and the fact that we are trying here to
4verify subtle object deletion and reference counting issues, the %run tests
5will be kept in this separate file. This makes it easier to aggregate in one
6place the tricks needed to handle it; most other magics are much easier to test
7and we do so in a common test_magic file.
8"""
9from __future__ import absolute_import
10
11#-----------------------------------------------------------------------------
12# Imports
13#-----------------------------------------------------------------------------
14
15# stdlib
16import os
17import sys
18import tempfile
19
20# third-party
21import nose.tools as nt
22
23# our own
24from IPython.utils.platutils import find_cmd
25from IPython.utils import genutils
26from IPython.testing import decorators as dec
27from IPython.testing import tools as tt
28
29#-----------------------------------------------------------------------------
30# Test functions begin
31#-----------------------------------------------------------------------------
32
33def doctest_refbug():
34 """Very nasty problem with references held by multiple runs of a script.
35 See: https://bugs.launchpad.net/ipython/+bug/269966
36
37 In [1]: _ip.clear_main_mod_cache()
38 # random
39
40 In [2]: %run refbug
41
42 In [3]: call_f()
43 lowercased: hello
44
45 In [4]: %run refbug
46
47 In [5]: call_f()
48 lowercased: hello
49 lowercased: hello
50 """
51
52
53def doctest_run_builtins():
54 r"""Check that %run doesn't damage __builtins__.
55
56 In [1]: import tempfile
57
58 In [2]: bid1 = id(__builtins__)
59
60 In [3]: fname = tempfile.mkstemp('.py')[1]
61
62 In [3]: f = open(fname,'w')
63
64 In [4]: f.write('pass\n')
65
66 In [5]: f.flush()
67
68 In [6]: t1 = type(__builtins__)
69
70 In [7]: %run $fname
71
72 In [7]: f.close()
73
74 In [8]: bid2 = id(__builtins__)
75
76 In [9]: t2 = type(__builtins__)
77
78 In [10]: t1 == t2
79 Out[10]: True
80
81 In [10]: bid1 == bid2
82 Out[10]: True
83
84 In [12]: try:
85 ....: os.unlink(fname)
86 ....: except:
87 ....: pass
88 ....:
89 """
90
91# For some tests, it will be handy to organize them in a class with a common
92# setup that makes a temp file
93
94class TestMagicRunPass(tt.TempFileMixin):
95
96 def setup(self):
97 """Make a valid python temp file."""
98 self.mktmp('pass\n')
99
100 def run_tmpfile(self):
101 _ip = get_ipython()
102 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
103 # See below and ticket https://bugs.launchpad.net/bugs/366353
104 _ip.magic('run %s' % self.fname)
105
106 def test_builtins_id(self):
107 """Check that %run doesn't damage __builtins__ """
108 _ip = get_ipython()
109 # Test that the id of __builtins__ is not modified by %run
110 bid1 = id(_ip.user_ns['__builtins__'])
111 self.run_tmpfile()
112 bid2 = id(_ip.user_ns['__builtins__'])
113 tt.assert_equals(bid1, bid2)
114
115 def test_builtins_type(self):
116 """Check that the type of __builtins__ doesn't change with %run.
117
118 However, the above could pass if __builtins__ was already modified to
119 be a dict (it should be a module) by a previous use of %run. So we
120 also check explicitly that it really is a module:
121 """
122 _ip = get_ipython()
123 self.run_tmpfile()
124 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
125
126 def test_prompts(self):
127 """Test that prompts correctly generate after %run"""
128 self.run_tmpfile()
129 _ip = get_ipython()
130 p2 = str(_ip.outputcache.prompt2).strip()
131 nt.assert_equals(p2[:3], '...')
132
133
134class TestMagicRunSimple(tt.TempFileMixin):
135
136 def test_simpledef(self):
137 """Test that simple class definitions work."""
138 src = ("class foo: pass\n"
139 "def f(): return foo()")
140 self.mktmp(src)
141 _ip.magic('run %s' % self.fname)
142 _ip.runlines('t = isinstance(f(), foo)')
143 nt.assert_true(_ip.user_ns['t'])
144
145 # We have to skip these in win32 because genutils.getoutputerr() crashes,
146 # due to the fact that subprocess does not support close_fds when
147 # redirecting stdout/err. So unless someone who knows more tells us how to
148 # implement genutils.getoutputerr() in win32, we're stuck avoiding these.
149 @dec.skip_win32
150 def test_obj_del(self):
151 """Test that object's __del__ methods are called on exit."""
152
153 # This test is known to fail on win32.
154 # See ticket https://bugs.launchpad.net/bugs/366334
155 src = ("class A(object):\n"
156 " def __del__(self):\n"
157 " print 'object A deleted'\n"
158 "a = A()\n")
159 self.mktmp(src)
160 tt.ipexec_validate(self.fname, 'object A deleted')
161
162 @dec.skip_win32
163 def test_tclass(self):
164 mydir = os.path.dirname(__file__)
165 tc = os.path.join(mydir, 'tclass')
166 src = ("%%run '%s' C-first\n"
167 "%%run '%s' C-second\n") % (tc, tc)
168 self.mktmp(src, '.ipy')
169 out = """\
170ARGV 1-: ['C-first']
171ARGV 1-: ['C-second']
172tclass.py: deleting object: C-first
173"""
174 tt.ipexec_validate(self.fname, out)
0175
=== modified file 'IPython/core/ultratb.py'
--- IPython/core/ultratb.py 2009-08-27 22:40:52 +0000
+++ IPython/core/ultratb.py 2010-01-18 01:17:11 +0000
@@ -88,7 +88,6 @@
88from inspect import getsourcefile, getfile, getmodule,\88from inspect import getsourcefile, getfile, getmodule,\
89 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode89 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
9090
91
92# IPython's own modules91# IPython's own modules
93# Modified pdb which doesn't damage IPython's readline handling92# Modified pdb which doesn't damage IPython's readline handling
94from IPython.utils import PyColorize93from IPython.utils import PyColorize
@@ -263,7 +262,7 @@
263262
264_parser = PyColorize.Parser()263_parser = PyColorize.Parser()
265 264
266def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None):265def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None):
267 numbers_width = INDENT_SIZE - 1266 numbers_width = INDENT_SIZE - 1
268 res = []267 res = []
269 i = lnum - index268 i = lnum - index
@@ -313,6 +312,15 @@
313class TBTools:312class TBTools:
314 """Basic tools used by all traceback printer classes."""313 """Basic tools used by all traceback printer classes."""
315314
315 #: Default output stream, can be overridden at call time. A special value
316 #: of 'stdout' *as a string* can be given to force extraction of sys.stdout
317 #: at runtime. This allows testing exception printing with doctests, that
318 #: swap sys.stdout just at execution time.
319 #: Warning: be VERY careful to set this to one of the Term streams, NEVER
320 #: directly to sys.stdout/err, because under win32 the Term streams come from
321 #: pyreadline and know how to handle color correctly, whie stdout/err don't.
322 out_stream = Term.cerr
323
316 def __init__(self,color_scheme = 'NoColor',call_pdb=False):324 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
317 # Whether to call the interactive pdb debugger after printing325 # Whether to call the interactive pdb debugger after printing
318 # tracebacks or not326 # tracebacks or not
@@ -376,16 +384,31 @@
376 384
377 def __call__(self, etype, value, elist):385 def __call__(self, etype, value, elist):
378 Term.cout.flush()386 Term.cout.flush()
379 print >> Term.cerr, self.text(etype,value,elist)387 Term.cerr.writeln(self.text(etype,value,elist))
380 Term.cerr.flush()388
381389 def text(self, etype, value, elist, context=5):
382 def text(self,etype, value, elist,context=5):390 """Return a color formatted string with the traceback info.
383 """Return a color formatted string with the traceback info."""391
392 Parameters
393 ----------
394 etype : exception type
395 Type of the exception raised.
396
397 value : object
398 Data stored in the exception
399
400 elist : list
401 List of frames, see class docstring for details.
402
403 Returns
404 -------
405 String with formatted exception.
406 """
384407
385 Colors = self.Colors408 Colors = self.Colors
386 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]409 out_string = []
387 if elist:410 if elist:
388 out_string.append('Traceback %s(most recent call last)%s:' % \411 out_string.append('Traceback %s(most recent call last)%s:' %
389 (Colors.normalEm, Colors.Normal) + '\n')412 (Colors.normalEm, Colors.Normal) + '\n')
390 out_string.extend(self._format_list(elist))413 out_string.extend(self._format_list(elist))
391 lines = self._format_exception_only(etype, value)414 lines = self._format_exception_only(etype, value)
@@ -492,15 +515,29 @@
492 else:515 else:
493 list.append('%s\n' % str(stype))516 list.append('%s\n' % str(stype))
494517
495 # vds:>>518 # sync with user hooks
496 if have_filedata:519 if have_filedata:
497 ipinst = ipapi.get()520 ipinst = ipapi.get()
498 if ipinst is not None:521 if ipinst is not None:
499 ipinst.hooks.synchronize_with_editor(filename, lineno, 0)522 ipinst.hooks.synchronize_with_editor(filename, lineno, 0)
500 # vds:<<
501523
502 return list524 return list
503525
526 def show_exception_only(self, etype, value):
527 """Only print the exception type and message, without a traceback.
528
529 Parameters
530 ----------
531 etype : exception type
532 value : exception value
533 """
534 # This method needs to use __call__ from *this* class, not the one from
535 # a subclass whose signature or behavior may be different
536 Term.cout.flush()
537 ostream = sys.stdout if self.out_stream == 'stdout' else Term.cerr
538 ostream.write(ListTB.text(self, etype, value, []))
539 ostream.flush()
540
504 def _some_str(self, value):541 def _some_str(self, value):
505 # Lifted from traceback.py542 # Lifted from traceback.py
506 try:543 try:
@@ -781,8 +818,8 @@
781 frames.append(level)818 frames.append(level)
782 else:819 else:
783 frames.append('%s%s' % (level,''.join(820 frames.append('%s%s' % (level,''.join(
784 _formatTracebackLines(lnum,index,lines,Colors,lvals,821 _format_traceback_lines(lnum,index,lines,Colors,lvals,
785 col_scheme))))822 col_scheme))))
786823
787 # Get (safely) a string form of the exception info824 # Get (safely) a string form of the exception info
788 try:825 try:
@@ -854,11 +891,11 @@
854 with display_trap:891 with display_trap:
855 self.pdb.reset()892 self.pdb.reset()
856 # Find the right frame so we don't pop up inside ipython itself893 # Find the right frame so we don't pop up inside ipython itself
857 if hasattr(self,'tb'):894 if hasattr(self,'tb') and self.tb is not None:
858 etb = self.tb895 etb = self.tb
859 else:896 else:
860 etb = self.tb = sys.last_traceback897 etb = self.tb = sys.last_traceback
861 while self.tb.tb_next is not None:898 while self.tb is not None and self.tb.tb_next is not None:
862 self.tb = self.tb.tb_next899 self.tb = self.tb.tb_next
863 if etb and etb.tb_next:900 if etb and etb.tb_next:
864 etb = etb.tb_next901 etb = etb.tb_next
@@ -872,8 +909,7 @@
872 (etype, evalue, etb) = info or sys.exc_info()909 (etype, evalue, etb) = info or sys.exc_info()
873 self.tb = etb910 self.tb = etb
874 Term.cout.flush()911 Term.cout.flush()
875 print >> Term.cerr, self.text(etype, evalue, etb)912 Term.cerr.writeln(self.text(etype, evalue, etb))
876 Term.cerr.flush()
877913
878 # Changed so an instance can just be called as VerboseTB_inst() and print914 # Changed so an instance can just be called as VerboseTB_inst() and print
879 # out the right info on its own.915 # out the right info on its own.
@@ -980,6 +1016,7 @@
980 except:1016 except:
981 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object1017 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
982 """1018 """
1019
983 def __call__(self,etype=None,evalue=None,etb=None,1020 def __call__(self,etype=None,evalue=None,etb=None,
984 out=None,tb_offset=None):1021 out=None,tb_offset=None):
985 """Print out a formatted exception traceback.1022 """Print out a formatted exception traceback.
@@ -990,16 +1027,18 @@
990 - tb_offset: the number of frames to skip over in the stack, on a1027 - tb_offset: the number of frames to skip over in the stack, on a
991 per-call basis (this overrides temporarily the instance's tb_offset1028 per-call basis (this overrides temporarily the instance's tb_offset
992 given at initialization time. """1029 given at initialization time. """
993 1030
994 if out is None:1031 if out is None:
995 out = Term.cerr1032 out = sys.stdout if self.out_stream=='stdout' else self.out_stream
996 Term.cout.flush()1033 Term.cout.flush()
997 if tb_offset is not None:1034 if tb_offset is not None:
998 tb_offset, self.tb_offset = self.tb_offset, tb_offset1035 tb_offset, self.tb_offset = self.tb_offset, tb_offset
999 print >> out, self.text(etype, evalue, etb)1036 out.write(self.text(etype, evalue, etb))
1037 out.write('\n')
1000 self.tb_offset = tb_offset1038 self.tb_offset = tb_offset
1001 else:1039 else:
1002 print >> out, self.text(etype, evalue, etb)1040 out.write(self.text(etype, evalue, etb))
1041 out.write('\n')
1003 out.flush()1042 out.flush()
1004 try:1043 try:
1005 self.debugger()1044 self.debugger()
10061045
=== modified file 'IPython/core/usage.py'
--- IPython/core/usage.py 2009-11-09 01:49:24 +0000
+++ IPython/core/usage.py 2010-01-18 01:17:11 +0000
@@ -1,338 +1,47 @@
1# -*- coding: utf-8 -*-1# -*- coding: utf-8 -*-
2#*****************************************************************************2"""Usage information for the main IPython applications.
3# Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>3"""
4#-----------------------------------------------------------------------------
5# Copyright (C) 2008-2010 The IPython Development Team
6# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
4#7#
5# Distributed under the terms of the BSD License. The full license is in8# Distributed under the terms of the BSD License. The full license is in
6# the file COPYING, distributed as part of this software.9# the file COPYING, distributed as part of this software.
7#*****************************************************************************10#-----------------------------------------------------------------------------
811
9import sys12import sys
10from IPython.core import release13from IPython.core import release
1114
12__doc__ = """
13IPython -- An enhanced Interactive Python
14=========================================
15
16A Python shell with automatic history (input and output), dynamic object
17introspection, easier configuration, command completion, access to the system
18shell and more.
19
20IPython can also be embedded in running programs. See EMBEDDING below.
21
22
23USAGE
24 ipython [options] files
25
26 If invoked with no options, it executes all the files listed in
27 sequence and drops you into the interpreter while still acknowledging
28 any options you may have set in your ipythonrc file. This behavior is
29 different from standard Python, which when called as python -i will
30 only execute one file and will ignore your configuration setup.
31
32 Please note that some of the configuration options are not available at
33 the command line, simply because they are not practical here. Look into
34 your ipythonrc configuration file for details on those. This file
35 typically installed in the $HOME/.ipython directory.
36
37 For Windows users, $HOME resolves to C:\\Documents and
38 Settings\\YourUserName in most instances, and _ipython is used instead
39 of .ipython, since some Win32 programs have problems with dotted names
40 in directories.
41
42 In the rest of this text, we will refer to this directory as
43 IPYTHON_DIR.
44
45REGULAR OPTIONS
46 After the above threading options have been given, regular options can
47 follow in any order. All options can be abbreviated to their shortest
48 non-ambiguous form and are case-sensitive. One or two dashes can be
49 used. Some options have an alternate short form, indicated after a |.
50
51 Most options can also be set from your ipythonrc configuration file.
52 See the provided examples for assistance. Options given on the comman-
53 dline override the values set in the ipythonrc file.
54
55 All options with a [no] prepended can be specified in negated form
56 (using -nooption instead of -option) to turn the feature off.
57
58 -h, --help
59 Show summary of options.
60
61 -autocall <val>
62 Make IPython automatically call any callable object even if you
63 didn't type explicit parentheses. For example, 'str 43' becomes
64 'str(43)' automatically. The value can be '0' to disable the
65 feature, '1' for 'smart' autocall, where it is not applied if
66 there are no more arguments on the line, and '2' for 'full'
67 autocall, where all callable objects are automatically called
68 (even if no arguments are present). The default is '1'.
69
70 -[no]autoindent
71 Turn automatic indentation on/off.
72
73 -[no]automagic
74 Make magic commands automatic (without needing their first char-
75 acter to be %). Type %magic at the IPython prompt for more
76 information.
77
78 -[no]autoedit_syntax
79 When a syntax error occurs after editing a file, automatically
80 open the file to the trouble causing line for convenient fixing.
81
82 -[no]banner
83 Print the intial information banner (default on).
84
85 -c <command>
86 Execute the given command string, and set sys.argv to ['c'].
87 This is similar to the -c option in the normal Python inter-
88 preter.
89
90 -cache_size|cs <n>
91 Size of the output cache (maximum number of entries to hold in
92 memory). The default is 1000, you can change it permanently in
93 your config file. Setting it to 0 completely disables the
94 caching system, and the minimum value accepted is 20 (if you
95 provide a value less than 20, it is reset to 0 and a warning is
96 issued). This limit is defined because otherwise you'll spend
97 more time re-flushing a too small cache than working.
98
99 -classic|cl
100 Gives IPython a similar feel to the classic Python prompt.
101
102 -colors <scheme>
103 Color scheme for prompts and exception reporting. Currently
104 implemented: NoColor, Linux, and LightBG.
105
106 -[no]color_info
107 IPython can display information about objects via a set of func-
108 tions, and optionally can use colors for this, syntax highlight-
109 ing source code and various other elements. However, because
110 this information is passed through a pager (like 'less') and
111 many pagers get confused with color codes, this option is off by
112 default. You can test it and turn it on permanently in your
113 ipythonrc file if it works for you. As a reference, the 'less'
114 pager supplied with Mandrake 8.2 works ok, but that in RedHat
115 7.2 doesn't.
116
117 Test it and turn it on permanently if it works with your system.
118 The magic function @color_info allows you to toggle this inter-
119 actively for testing.
120
121 -[no]confirm_exit
122 Set to confirm when you try to exit IPython with an EOF (Con-
123 trol-D in Unix, Control-Z/Enter in Windows). Note that using the
124 magic functions @Exit or @Quit you can force a direct exit,
125 bypassing any confirmation.
126
127 -[no]debug
128 Show information about the loading process. Very useful to pin
129 down problems with your configuration files or to get details
130 about session restores.
131
132 -[no]deep_reload
133 IPython can use the deep_reload module which reloads changes in
134 modules recursively (it replaces the reload() function, so you
135 don't need to change anything to use it). deep_reload() forces a
136 full reload of modules whose code may have changed, which the
137 default reload() function does not.
138
139 When deep_reload is off, IPython will use the normal reload(),
140 but deep_reload will still be available as dreload(). This fea-
141 ture is off by default [which means that you have both normal
142 reload() and dreload()].
143
144 -editor <name>
145 Which editor to use with the @edit command. By default, IPython
146 will honor your EDITOR environment variable (if not set, vi is
147 the Unix default and notepad the Windows one). Since this editor
148 is invoked on the fly by IPython and is meant for editing small
149 code snippets, you may want to use a small, lightweight editor
150 here (in case your default EDITOR is something like Emacs).
151
152 -ipythondir <name>
153 The name of your IPython configuration directory IPYTHON_DIR.
154 This can also be specified through the environment variable
155 IPYTHON_DIR.
156
157 -log|l Generate a log file of all input. The file is named
158 ipython_log.py in your current directory (which prevents logs
159 from multiple IPython sessions from trampling each other). You
160 can use this to later restore a session by loading your logfile
161 as a file to be executed with option -logplay (see below).
162
163 -logfile|lf
164 Specify the name of your logfile.
165
166 -logplay|lp
167 Replay a previous log. For restoring a session as close as pos-
168 sible to the state you left it in, use this option (don't just
169 run the logfile). With -logplay, IPython will try to reconstruct
170 the previous working environment in full, not just execute the
171 commands in the logfile.
172 When a session is restored, logging is automatically turned on
173 again with the name of the logfile it was invoked with (it is
174 read from the log header). So once you've turned logging on for
175 a session, you can quit IPython and reload it as many times as
176 you want and it will continue to log its history and restore
177 from the beginning every time.
178
179 Caveats: there are limitations in this option. The history vari-
180 ables _i*,_* and _dh don't get restored properly. In the future
181 we will try to implement full session saving by writing and
182 retrieving a failed because of inherent limitations of Python's
183 Pickle module, so this may have to wait.
184
185 -[no]messages
186 Print messages which IPython collects about its startup process
187 (default on).
188
189 -[no]pdb
190 Automatically call the pdb debugger after every uncaught excep-
191 tion. If you are used to debugging using pdb, this puts you
192 automatically inside of it after any call (either in IPython or
193 in code called by it) which triggers an exception which goes
194 uncaught.
195
196 -[no]pprint
197 IPython can optionally use the pprint (pretty printer) module
198 for displaying results. pprint tends to give a nicer display of
199 nested data structures. If you like it, you can turn it on per-
200 manently in your config file (default off).
201
202 -profile|p <name>
203 Assume that your config file is ipythonrc-<name> (looks in cur-
204 rent dir first, then in IPYTHON_DIR). This is a quick way to keep
205 and load multiple config files for different tasks, especially
206 if you use the include option of config files. You can keep a
207 basic IPYTHON_DIR/ipythonrc file and then have other 'profiles'
208 which include this one and load extra things for particular
209 tasks. For example:
210
211 1) $HOME/.ipython/ipythonrc : load basic things you always want.
212 2) $HOME/.ipython/ipythonrc-math : load (1) and basic math-
213 related modules.
214 3) $HOME/.ipython/ipythonrc-numeric : load (1) and Numeric and
215 plotting modules.
216
217 Since it is possible to create an endless loop by having circu-
218 lar file inclusions, IPython will stop if it reaches 15 recur-
219 sive inclusions.
220
221 -prompt_in1|pi1 <string>
222 Specify the string used for input prompts. Note that if you are
223 using numbered prompts, the number is represented with a '\#' in
224 the string. Don't forget to quote strings with spaces embedded
225 in them. Default: 'In [\#]: '.
226
227 Most bash-like escapes can be used to customize IPython's
228 prompts, as well as a few additional ones which are IPython-spe-
229 cific. All valid prompt escapes are described in detail in the
230 Customization section of the IPython HTML/PDF manual.
231
232 -prompt_in2|pi2 <string>
233 Similar to the previous option, but used for the continuation
234 prompts. The special sequence '\D' is similar to '\#', but with
235 all digits replaced dots (so you can have your continuation
236 prompt aligned with your input prompt). Default: ' .\D.: '
237 (note three spaces at the start for alignment with 'In [\#]').
238
239 -prompt_out|po <string>
240 String used for output prompts, also uses numbers like
241 prompt_in1. Default: 'Out[\#]:'.
242
243 -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