[Patches] [ python-Patches-1035498 ] -m option to run a module as a script

SourceForge.net noreply at sourceforge.net
Mon Oct 4 16:30:54 CEST 2004


Patches item #1035498, was opened at 2004-09-28 00:22
Message generated for change (Comment added) made by ncoghlan
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1035498&group_id=5470

Category: Core (C code)
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Nick Coghlan (ncoghlan)
Assigned to: Barry A. Warsaw (bwarsaw)
Summary: -m option to run a module as a script

Initial Comment:
Implements the '-m' option recently discussed on
python-dev.

Runs a module 'as if' it had been invoked from the
command line.

E.g., for a build directory, the following two commands
are equivalent:
  ./python -m pdb
  ./python Lib/pdb.py

Note that neither of these does the same thing as
"./python -c "import pdb". (In this latter case, any
"if __name__ == "__main__" code would not be executed,
whereas it will be invoked in the first two cases).

Given the vagaries of sys.path, this is quite handy -
if a module can be imported, it can be easily used as a
script too. Not that all modules will necessarily do
anything _interesting_ when used as a script. . .

The current implementation makes changes to main.c
(handle the new argument), pythonrun.[ch] (new flavour
of PyRun_) and import.[ch] (expose a
PyImport_FindModule C API)

Current limitations / to-do if this is pursued:
  - print sensible errors for non-Python files (e.g.
hotshot)
  - allow 'dotted' names
  - decide what to do with packages (e.g. run __init__.py)
  - check usage messages in main.c are all still under
512 characters (I assume that limit was set for a reason)
  - the 1K limit on absolute file paths seems rather
arbitrary.
  - this is code written in the wee hours of the
morning, so it probably has a few other lurking 'features'.

----------------------------------------------------------------------

>Comment By: Nick Coghlan (ncoghlan)
Date: 2004-10-05 00:30

Message:
Logged In: YES 
user_id=1038590

Relevant documentation locations that I can find:
  Tutorial section 2.1 (Invoking the Interpreter)
    Add a very brief description of the '-m' option

  Python/C API section 2 (The Very High Level Layer)
    Document PyRun_SimpleModule

  What's New sections 12 & 13
    I'll do this as a separate patch from the previous two,
as I can't recall if Andrew prefers to write these himself.



----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-10-01 14:13

Message:
Logged In: YES 
user_id=1038590

New version (#4)

Trivial fix - tell PyRun_SimpleFile to close the file when
it's done with it.

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-10-01 12:51

Message:
Logged In: YES 
user_id=1038590

New version (run_module_3.diff)

Takes more account of the file description reported by
PyImport. Explictly restricts itself to PY_SOURCE and
PY_COMPILED scripts.

Behaves correctly when a non-package is encountered while
parsing a dotted name (closes the file descriptor and
reports an error).

I also realised that the '-m' option doesn't currently
comprehend "__path__" variables in packages. So maybe we
*should* be loading the packages as we find them, and then
querying them for their __path__ variable.

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-09-30 19:48

Message:
Logged In: YES 
user_id=1038590

Barry, kicking this in your direction solely because you
were one of the people to comment on it on py-dev. Feel free
to say 'too busy' :)

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-09-30 19:39

Message:
Logged In: YES 
user_id=1038590

New version of patch attached (run_module_2.diff).

This version allows modules within packages to be executed.
It does this *without* loading the packages first (just as
if the script were specified directly on the command line).

It's also a little more responsive when it comes to error
message, and I've confirmed that the usage message
components are now under 512 bytes each.

The FindModule method is now marked as private (i.e. with
leading underscore).

The limit on file paths in pythonrun.c is now taken from
osdefs.h (the same limit that import.c already uses)

The only bug I know of is that it misbehaves if you supply a
name like "a.b.module.d". You will get an ImportError saying
that d was not found, and the file pointer for 'module'
won't get closed properly.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1035498&group_id=5470


More information about the Patches mailing list