[Python-3000] PEP to change how the main module is delineated (original) (raw)
Steven Bethard steven.bethard at gmail.com
Mon Apr 23 06:43:21 CEST 2007
- Previous message: [Python-3000] PEP to change how the main module is delineated
- Next message: [Python-3000] PEP to change how the main module is delineated
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 4/22/07, Brett Cannon <brett at python.org> wrote:
Implementation ==============
When the
-m
option is used,sys.main
will be set to the argument passed in.sys.argv
will be adjusted as it is currently. Then the equivalent of_import_(self.main)
will occur. This differs from current semantics as therunpy
module fetches the code object for the file specified by the module name in order to explicitly set_name_
and other attributes. This is no longer needed as import can perform its normal operation in this situation. If a file name is specified, thensys.main
will be set to"_main_"
. The specified file will then be read and have a code object created and then be executed with_name_
set to"_main_"
. This mirrors current semantics.
So name will still sometimes be "main". That's disappointing.
To clarify, assuming that the foo.bar module contains something like "from . import baz", this PEP only addresses the following situation::
python -mfoo.bar
and all of the following will still raise ImportErrors::
~> python foo/bar
~/grok> python ../foo/bar
~/foo> python bar
Right?
If that's right, I'm -1 on the proposal. It's complicating the standard "am I the main module?" idiom to solve a tiny little problem with "-m". The "am I the main module?" idiom has been around much longer than the "-m" flag, and I'd prefer to see a more compelling reason to change it.
Two reasons that would be compelling for me:
Simplifying the "am I the main module?" idiom, e.g. with the rejected
if __main__
proposal.Getting rid of the "main" value for name entirely. This would require code like the following to determine the name of a module given at the command line::
def get_name(path): sys_path_set = set(sys.path) path, _ = os.path.splitext(path) path = os.path.abspath(path) parts = path.split(os.path.sep) for i in range(1, len(parts)): sub_path = os.path.sep.join(parts[:i]) if sub_path in sys_path_set: return '.'.join(parts[i:]) return parts[-1] Even if we can't do the name resolution perfectly, if the flaws are
documented (e.g. when '..' and symbolic links are combined) I think that would be enough better than the current situation to merit changing the standard "am I the current module?" idiom.
STeVe
I'm not in-sane. Indeed, I am so far out of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy
- Previous message: [Python-3000] PEP to change how the main module is delineated
- Next message: [Python-3000] PEP to change how the main module is delineated
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]