Skip to content

Add support for relative import to %run -m (fixes #2727)#3066

Merged
bfroehle merged 19 commits into
ipython:masterfrom
tkf:run-module
May 3, 2013
Merged

Add support for relative import to %run -m (fixes #2727)#3066
bfroehle merged 19 commits into
ipython:masterfrom
tkf:run-module

Conversation

@tkf

@tkf tkf commented Mar 23, 2013

Copy link
Copy Markdown
Contributor

%run is refactored a little bit by side effect :)

@tkf

tkf commented Mar 23, 2013

Copy link
Copy Markdown
Contributor Author

This is the test failure:
https://travis-ci.org/ipython/ipython/jobs/5746287#L2399

It is from ChangedPyFileTest.test_changing_py_file. In this test, import foo is executed. In my test import <temporary-package>.foo is executed. I guess this failure is due to Python 2.x mixing absolute and relative imports sometime. In Python 3.x, there is no failure.

Comment thread IPython/core/magics/execution.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this line have a comment to explain when it happens? The comments on the if/elif may also need to be updated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thoughts, maybe we should refactor this. Currently prun is used both as a magic command, and as a function to be called by other code, which makes it somewhat confusing. Maybe the core should be pulled out into another function, so that prun is just for the magic command. Going by your other changes, the new function could be called _run_with_profiler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be better to not break backward compatibility. As it is a user facing function (i.e., magic function), someone might be using it for writing their own magic function. But I would refactor and add _run_with_profiler if you think it is OK. I think we can get rid of the else clause as I think it is not used in IPython.

FYI, you can see the parameter spec in the documentation:
http://ipython.org/ipython-doc/dev/api/generated/IPython.core.magics.execution.html#IPython.core.magics.execution.ExecutionMagics.prun

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backwards compatibility would already have been broken when we refactored the magics into separate classes for 0.13, and I don't remember hearing about any problems, so I don't think it's an issue. We can add a note to the "what's new" document in case anyone is using it.

The API docs unfortunately default to documenting every class and function, without us ever having to consider what constitutes our public API. I've added an @undoc decorator to explicitly mark things as private, but I'm reluctant to apply it to magic classes, because at present the API docs are the only fully comprehensive documentation of the magic commands we offer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I can change it that's good. I just played on safer side. The function is clear now, I think.

@takluyver

Copy link
Copy Markdown
Member

Definitely +1 on breaking run up into some smaller pieces like this.

Comment thread IPython/core/magics/execution.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this have a comment explaining that it's filling in the default options for any that aren't specified?

@takluyver

Copy link
Copy Markdown
Member

Thanks, I think this is looking good.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@takluyver This was error(msg) and return. But as _run_with_debugge is a separated function now, return does not finish run. Do you think this is OK? I am not 100% sure that I can replace error(msg) and return with raise UsageError(msg).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think magics are supposed to raise a UsageError now, instead of error()ing and returning. It does mean that this code should only be called by a magic function, but since it's a private method in a Magics class, I think that's reasonable.

@bfroehle

Copy link
Copy Markdown
Contributor

I haven't dug into the details here, but I certainly agree that %run is long overdue for a re-factoring!

@takluyver

Copy link
Copy Markdown
Member

This is OK by me now, but as it touches some core and rather fragile stuff, I'd like to have a second set of eyes look over it before we merge it. I think @fperez said he'd have more time soon, so maybe he'll have a look.

@bfroehle

Copy link
Copy Markdown
Contributor

We should also update the main %run docstring to add the -m option:

%run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] ( -m mod | file ) [args]

@tkf

tkf commented Mar 26, 2013

Copy link
Copy Markdown
Contributor Author

I fixed the usage line. -e was also not mentioned.

@ellisonbg

Copy link
Copy Markdown
Member

Looks like this PR has gotten some good review and updates. There was a mention of having @fperez looking at it because he knows this code well. Do you still feel like that is desired? Other than that, is this ready to go?

@takluyver

Copy link
Copy Markdown
Member

I'd ideally like a second pair of eyes on it, because the %run stuff is quite delicate. I think @fperez knows it best. But we have got reasonable test coverage for it now, so if Fernando's not going to get to it soon, we could push the green button and deal with any fallout that might occur.

Comment thread IPython/core/magics/execution.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it's private, I'd want to see a docstring here... Functions without docstrings are just painful for whomever comes later, as it's very hard to make out the intent of the arguments.

@fperez

fperez commented Apr 26, 2013

Copy link
Copy Markdown
Member

Great job, @tkf! Very minor cosmetic issues there, otherwise it looks solid. Honestly, %run should be refactored even deeper, so that the actual code in magics is absolutely minimal and more of it is a more stateless interface (would be easier to test). But such is life, at least this disentangles the beast somewhat and is a good first step.

Once the small issues are ironed out, I think we can go ahead and merge. Thanks everyone for the earlier review, and sorry for my slowness!

@tkf

tkf commented May 3, 2013

Copy link
Copy Markdown
Contributor Author

@fperez I just fixed the points you mentioned.

bfroehle added a commit that referenced this pull request May 3, 2013
Add support for relative import to %run -m (fixes #2727)
@bfroehle bfroehle merged commit 9087bb9 into ipython:master May 3, 2013
@tkf tkf deleted the run-module branch May 3, 2013 21:35
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Add support for relative import to %run -m (fixes ipython#2727)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants