git-revise
git-revise copied to clipboard
Just passing by
https://github.com/mystor/git-revise/blob/9f10441a9cfd989c562fc740dbf47b4edaebc284/gitrevise/main.py#L3
I don't believe there is a realistic possibility the name of the module named __main__ will not be equal to "__main__".
/tmp % touch py/__main__.py
/tmp % python
>>> import py
>>> py.__name__
'py'
It's not a module, the module is the directory it's in. This check is for when that file is run as a script.
Well... some explanation of Python modules is due... I think.
Any Python file is also a module. That's just how Python works. Beside other things, modules have a special field __name__, which stores the name of the module. Putting these two together, gives you that file gitrevise/__main__.py contains a module with fully qualified name gitrevise.__main__ and local name __main__, which is what is being tested in the linked code.
It is possible to artificially create a situation, where you implement your own loader, and thus override the name that would be given to a Python module by default. This is stupid and useless, but, that's how Python works. Realistically, nobody does this, since, like I said, it's stupid and useless, and, most Python coders wouldn't know how to do it anyways.
Now, to your example: bad news, it's completely irrelevant. You created modules py and py.__main__, and then imported module py, and are showing me that the module py has local name py. Good for you, I guess, but the question was about the name of the module py.__main__, which, like I said, is __main__. If you want to convince yourself of this, do:
>>> import py.__main__
>>> py.__main__.__name__
Trust me, you'll get __main__ back.
Now, why would anyone want a module named __main__? Well, this is because this is a special module which will be loaded when the containing module is imported from command line, or when Python is instructed to execute a ZIP archive. In particular, in case of gitrevise, if you tried to do:
python -m gitrevise
Python would have loaded module gitrevise.__main__ and execute all top-level code in that module (because that's how imports in Python work). In particular, it would execute the if statement linked in the post below, and would undoubtedly discover that it is true. However, unless you really want to find a contrived example, where you override the default Python loader, it is not possible to change the name of the module. But even then, the blame would be with the author of such loader, and not the author of gitrevise package.