-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In Bazel pydrake
, unittests may have system modules inadvertently shadowed by submodules
#8041
Comments
Er, nevermind, was just running from But perhaps this is an issue on Mac? |
pydrake
submodules leak in as top-level modules in PYTHONPATH
pydrake
submodules might leak in as top-level modules in PYTHONPATH
on Mac?
Tested on Mac, and it seems like Python on Mac (or just Homebrew?) has different behavior than on Ubuntu: When a Python script is executed, the directory of the script is effectively added to I figured this out by running the script on Russ's
Looking at directory of @RussTedrake For your test, you can work around this issue by placing the test in a separate directory. As an example: EricCousineau-TRI@60c50b1cf @jamiesnape Are you aware if this is intended behavior for Python on Mac? |
You mean when Bazel runs the script, the wrapper it creates adds the directory of the script to PYTHONPATH? |
It does not appear that way. This happens even before any of the Bazel wrapper junk happens. Here's the wrapper script, up to line 5 (where the error occurs):
Update: If I inject a printout of
|
So if I do |
It does appear, but after
Rewording it to run from a file:
However, it seems that this same behavior happens on Ubuntu... so now I have no idea why Russ's branch does not encounter this problem on Ubuntu... ??? Perhaps the Ubuntu binary has some logic to detect this edge case for system libraries, and ignore it? Bottom line, though, is to ensure that tests are not run in a directory that neighbors any modules that might collide with system libraries? TIL: Python prepends a script's directory to |
pydrake
submodules might leak in as top-level modules in PYTHONPATH
on Mac?pydrake
submodules may shadow built-in modules for unittests
Minor point (does not affect the conclusion, I checked it is the same) but all your tests above are using the system Python rather than the Homebrew one. |
I actually never see the working directory in my output for some reason, but I do see the script directory for the second example. |
@EricCousineau-TRI the order in which Python modules are found is here: https://docs.python.org/2/tutorial/modules.html#the-module-search-path Built-ins are always preferred, so if you have a file called However, it's possible to shadow built-ins if you really try. For example, if you have
then you're certainly in trouble when you try to do Relative imports within packages are indeed a source of confusion, (so much so that relative import within a package is removed in Python3). Fortunately, it's easy to do the right thing. If you have:
then from . import math or from .math import sin and be absolutely certain you're getting pydrake.math and not anything else. |
@rdeits Thanks for the info!
This does not seem to be the case for Mac, as
I tested the Bazel |
That would be very strange. Is there a minimal example that demonstrates this? Something like
? |
I'll see if I can whip something up. I couldn't reproduce it with a What I have so far: |
Yup, Mac does not correctly ignore Here is the line that now fails: Note that the other call (where |
Hup, I lied; it fails with Filed the upstream bug: |
Wow, that's super weird. Kudos for figuring this out. |
Yup, just confirmed this for myself too. Having a |
According to Eric V. Smith and Ned Deily on the Python bug tracker, this is expected behavior. Ned posted an example showing So ultimately, Python tests should not have Additionally, the linting tests (which were failing on Mac) should also not be sensitive to the module names - to this end, the Python linting tests should also be placed in a subdirectory. UPDATE: Was sufficiently disappointed in search results that I posted a StackOverflow question + answer: |
pydrake
submodules may shadow built-in modules for unittestspydrake
, unittests may have system modules inadvertently shadowed by submodules
Solution plan:
|
drake_py: Add mechanism for #8041 to prevent submodule shadowing
The following code should not work, but does:SUMMARY (2018/02/19): Because we put tests next to (sub)module code in Bazel, we can inadvertently shadow standard-but-not-builtin modules (e.g.
math
) in our tests / binaries. This happens on Mac more frequently because they adhere to using the correct builtin module list, while Ubuntu has a non-compliant builtin list (e.g. addingmath
).@RussTedrake is running into this when trying to define a
math
submodule, which breaks other things.The text was updated successfully, but these errors were encountered: