Add imports attribute to Bazel native Python rules to allow adding directories to PYTHONPATH. Fixes #702 RELNOTES: Add imports attribute to native Python rules. -- MOS_MIGRATED_REVID=114314430
diff --git a/examples/py_native/BUILD b/examples/py_native/BUILD index d6fdfa9..5a82223 100644 --- a/examples/py_native/BUILD +++ b/examples/py_native/BUILD
@@ -7,7 +7,10 @@ py_binary( name = "bin", srcs = ["bin.py"], - deps = [":lib"], + deps = [ + ":lib", + "//examples/py_native/fibonacci", + ], ) py_library( @@ -18,7 +21,10 @@ py_test( name = "test", srcs = ["test.py"], - deps = [":lib"], + deps = [ + ":lib", + "//examples/py_native/fibonacci", + ], ) py_test(
diff --git a/examples/py_native/bin.py b/examples/py_native/bin.py index f79379a..7b65627 100644 --- a/examples/py_native/bin.py +++ b/examples/py_native/bin.py
@@ -1,4 +1,6 @@ """A tiny example binary for the native Python rules of Bazel.""" from examples.py_native.lib import GetNumber +from fib import Fib print "The number is %d" % GetNumber() +print "Fib(5) == %d" % Fib(5)
diff --git a/examples/py_native/fibonacci/BUILD b/examples/py_native/fibonacci/BUILD new file mode 100644 index 0000000..c3ee9b0 --- /dev/null +++ b/examples/py_native/fibonacci/BUILD
@@ -0,0 +1,6 @@ +py_library( + name = "fibonacci", + srcs = ["fib.py"], + imports = ["."], + visibility = ["//examples/py_native:__pkg__"], +)
diff --git a/examples/py_native/fibonacci/fib.py b/examples/py_native/fibonacci/fib.py new file mode 100644 index 0000000..645a937 --- /dev/null +++ b/examples/py_native/fibonacci/fib.py
@@ -0,0 +1,8 @@ +"""An example binary to test the imports attribute of native Python rules.""" + + +def Fib(n): + if n == 0 or n == 1: + return 1 + else: + return Fib(n-1) + Fib(n-2)
diff --git a/examples/py_native/test.py b/examples/py_native/test.py index 811eee1..f9543aa 100644 --- a/examples/py_native/test.py +++ b/examples/py_native/test.py
@@ -1,6 +1,8 @@ """A tiny example binary for the native Python rules of Bazel.""" + import unittest from examples.py_native.lib import GetNumber +from fib import Fib class TestGetNumber(unittest.TestCase): @@ -8,6 +10,8 @@ def test_ok(self): self.assertEquals(GetNumber(), 42) + def test_fib(self): + self.assertEquals(Fib(5), 8) if __name__ == '__main__': unittest.main()