You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mac: Local modules can shadow builtins (e.g. a local math.py can shadow math) #77026
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2018-02-15.01:08:51.154>created_at=<Date2018-02-14.23:11:46.841>labels= ['interpreter-core', 'type-bug', 'invalid']
title='Mac: Local modules can shadow builtins (e.g. a local `math.py` can shadow `math`)'updated_at=<Date2018-02-15.17:49:54.435>user='https://github.com/EricCousineau-TRI'
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): RobotLocomotion/drake#8041
This happens for us on HighSierra / Sierra MacOS (for *.py and *.so modules); Ubuntu Linux seems fine.
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 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.
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.
EricCousineau-TRI commented on Feb 15, 2018
EricCousineau-TRImannequin
MannequinAuthor
More actions
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.
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.
Activity
EricCousineau-TRI commentedon Feb 14, 2018
We ran into an issue with our library; we build a submodule named
math.so, and have a testmath_testwhich is ran in the same directory as this module (since this is how we've written it with Bazel's generated Python binaries):RobotLocomotion/drake#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.pyscript. The second execution (where theimport_test.pyscript is moved up one level) succeeds on Mac (s.t. themath.pyis not insys.path).This behavior seems to violate the documentation:
https://docs.python.org/2/tutorial/modules.html#the-module-search-path
ericvsmith commentedon Feb 15, 2018
This is the expected behavior, fortunately or not. "math" is not builtin in the sense that is used in that paragraph.
ericvsmith commentedon Feb 15, 2018
Removing macOS since this isn't Mac specific.
ned-deily commentedon Feb 15, 2018
This had me confused for a while. But eric.smith's comment is the clue to what's going on here:
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.
ned-deily commentedon Feb 15, 2018
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.
EricCousineau-TRI commentedon Feb 15, 2018
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}.pyneighboring 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 namedmathis not frowned upon.ned-deily commentedon Feb 15, 2018
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.