Issue 32845: Mac: Local modules can shadow builtins (e.g. a local `math.py` can shadow `math`)

Issue32845

Created on 2018-02-14 23:11 by Eric Cousineau, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.sh Eric Cousineau, 2018-02-14 23:11
Messages (7)
msg312183 - (view) Author: Eric Cousineau (Eric Cousineau) * Date: 2018-02-14 23:11
We ran into an issue with our library; we build a submodule named `math.so`, and have a test `math_test` which is ran in the same directory as this module (since this is how we've written it with Bazel's generated Python binaries):
https://github.com/RobotLocomotion/drake/issues/8041

This happens for us on HighSierra / Sierra MacOS (for *.py and *.so modules); Ubuntu Linux seems fine.

I have a reproduction script just for *.py files here:
https://github.com/EricCousineau-TRI/repro/blob/b08bc47/bug/mac_builtin_shadow/test.sh#L38
(attached this as `test.sh`)

The module is exposed on the path given that it neighbors the first execution of the `import_test.py` script. The second execution (where the `import_test.py` script is moved up one level) succeeds on Mac (s.t. the `math.py` is not in `sys.path`).

This behavior seems to violate the documentation:
https://docs.python.org/2/tutorial/modules.html#the-module-search-path
msg312186 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-02-15 00:07
This is the expected behavior, fortunately or not. "math" is not builtin in the sense that is used in that paragraph.
msg312189 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-02-15 00:56
Removing macOS since this isn't Mac specific.
msg312190 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-02-15 01:08
This had me confused for a while.  But eric.smith's comment is the clue to what's going on here:

> This is the expected behavior, fortunately or not. "math" is not builtin in the sense that is used in that paragraph.

The difference in behavior that you are seeing seems to be due to the fact that the Debian/Ubuntu uses a non-standard build to build-in the standard lib "math" module, whereas the Mac version you are using undoubtedly does not.  You can see this behavior yourself if you build your own version of python (either 2.7 or 3.x) on your Ubuntu system.  You should now see the same behavior you see on the Mac.  For example:

# on a current Debian system
$ /usr/bin/python2.7 -c 'import sys,math;print(sys.modules["math"])'
<module 'math' (built-in)>

# on a current Mac system using the python.org 2.7.14
$ /usr/local/bin/python2.7 -c 'import sys,math;print(sys.modules["math"])'
<module 'math' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/math.so'>

So, I don't think this is an issue at all.  Please reopen if you think there is actually a problem here.
msg312210 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-02-15 15:18
P.S. This issue points out once again why it is generally a bad idea to shadow or mix-and-match standard library module names.
msg312211 - (view) Author: Eric Cousineau (Eric Cousineau) * Date: 2018-02-15 15:22
> P.S. This issue points out once again why it is generally a bad idea to shadow or mix-and-match standard library module names.

Duly noted! And thank y'all for the explanations!

Can I ask if it's bad practice to use a standard library module name as a submodule, e.g. `example_module.math`?

TBH, this all arises because we use Bazel and wanted to modularize our tests - we place them under `test/{name}.py` neighboring the target (sub)module, and Bazel presently generates a wrapper script which is executed in the same directory as the (sub)module, which is why this (unintended) shadowing occurs.

I would like still like to have a submodule named `math`, so I can just teach our Bazel rules to not run the test in the same directory -- if having submodules named `math` is not frowned upon.
msg312217 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-02-15 17:49
The example you gave caused problems because of the use of the ambiguous unqualified name "math".  If you are careful to use qualified names, and/or perhaps use alias names ("as") to increase readability, there shouldn't be any problems.
History
Date User Action Args
2022-04-11 14:58:57adminsetgithub: 77026
2018-02-15 17:49:54ned.deilysetmessages: + msg312217
2018-02-15 15:22:39Eric Cousineausetmessages: + msg312211
2018-02-15 15:18:19ned.deilysetmessages: + msg312210
2018-02-15 01:08:51ned.deilysetstatus: open -> closed
resolution: not a bug
messages: + msg312190

stage: resolved

2018-02-15 00:56:24eric.smithsetmessages: + msg312189
components: + Interpreter Core, - macOS
2018-02-15 00:07:38eric.smithsetnosy: + eric.smith
messages: + msg312186
2018-02-14 23:11:46Eric Cousineaucreate