Skip to content

Commit c945837

Browse files
rickeylevhvadehra
authored andcommitted
Remove tests requiring Python 2 support.
This is in preparation for disabling Python 2 functionality. There is more code and tests related to Python 2 support, but these are the ones that fail when `--incompatible_python_disable_py2=true`. This also revealed that rejecting Python 2 settings for `py_runtime` and `py_runtime_pair` will have to wait until after the flag flip. This is because the auto-detecting toolchains define some py2 toolchains, so in order to make tests pass, those have to be removed, but doing so would break Python 2 support prior to the flag flip. The other rules still reject them, though, which is plenty sufficient. Work towards #15684 PiperOrigin-RevId: 504086890 Change-Id: Ib39e3b32076072f5510418241cd0ca4765e6ab44
1 parent aa2a77d commit c945837

16 files changed

+81
-830
lines changed

src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ def _py_runtime_impl(ctx):
6666
else:
6767
python_version = ctx.fragments.py.default_python_version
6868

69-
if ctx.fragments.py.disable_py2 and python_version == "PY2":
70-
fail("Using Python 2 is not supported and disabled; see " +
71-
"https://github.com/bazelbuild/bazel/issues/15684")
69+
# TODO: Uncomment this after --incompatible_python_disable_py2 defaults to true
70+
# if ctx.fragments.py.disable_py2 and python_version == "PY2":
71+
# fail("Using Python 2 is not supported and disabled; see " +
72+
# "https://github.com/bazelbuild/bazel/issues/15684")
7273

7374
return [
7475
_PyRuntimeInfo(

src/test/java/com/google/devtools/build/lib/bazel/rules/python/BazelPyBinaryConfiguredTargetTest.java

+35-77
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public void runtimeSetByPythonTop() throws Exception {
103103
"pkg/BUILD",
104104
"py_runtime(",
105105
" name = 'my_py_runtime',",
106-
" interpreter_path = '/system/python2',",
107-
" python_version = 'PY2',",
106+
" interpreter_path = '/system/python3',",
107+
" python_version = 'PY3',",
108108
")",
109109
"py_binary(",
110110
" name = 'pybin',",
@@ -114,7 +114,7 @@ public void runtimeSetByPythonTop() throws Exception {
114114
analysisMock.pySupport().createPythonTopEntryPoint(mockToolsConfig, "//pkg:my_py_runtime");
115115
useConfiguration("--incompatible_use_python_toolchains=false", "--python_top=" + pythonTop);
116116
String path = getInterpreterPathFromStub(getConfiguredTarget("//pkg:pybin"));
117-
assertThat(path).isEqualTo("/system/python2");
117+
assertThat(path).isEqualTo("/system/python3");
118118
}
119119

120120
@Test
@@ -149,8 +149,8 @@ public void pythonTopTakesPrecedenceOverPythonPath() throws Exception {
149149
"pkg/BUILD",
150150
"py_runtime(",
151151
" name = 'my_py_runtime',",
152-
" interpreter_path = '/system/python2',",
153-
" python_version = 'PY2',",
152+
" interpreter_path = '/system/python3',",
153+
" python_version = 'PY3',",
154154
")",
155155
"py_binary(",
156156
" name = 'pybin',",
@@ -163,7 +163,7 @@ public void pythonTopTakesPrecedenceOverPythonPath() throws Exception {
163163
"--python_top=" + pythonTop,
164164
"--python_path=/better/not/be/this/one");
165165
String path = getInterpreterPathFromStub(getConfiguredTarget("//pkg:pybin"));
166-
assertThat(path).isEqualTo("/system/python2");
166+
assertThat(path).isEqualTo("/system/python3");
167167
}
168168

169169
// TODO(brandjon): Move generic toolchain tests that don't access legacy behavior to
@@ -177,20 +177,13 @@ private void defineToolchains() throws Exception {
177177
"toolchains/BUILD",
178178
"load('" + TOOLCHAIN_BZL + "', 'py_runtime_pair')",
179179
"py_runtime(",
180-
" name = 'py2_runtime',",
181-
" interpreter_path = '/system/python2',",
182-
" python_version = 'PY2',",
183-
" stub_shebang = '#!/usr/bin/env python',",
184-
")",
185-
"py_runtime(",
186180
" name = 'py3_runtime',",
187181
" interpreter_path = '/system/python3',",
188182
" python_version = 'PY3',",
189183
" stub_shebang = '#!/usr/bin/env python3',",
190184
")",
191185
"py_runtime_pair(",
192186
" name = 'py_runtime_pair',",
193-
" py2_runtime = ':py2_runtime',",
194187
" py3_runtime = ':py3_runtime',",
195188
")",
196189
"toolchain(",
@@ -199,12 +192,12 @@ private void defineToolchains() throws Exception {
199192
" toolchain_type = '" + TOOLCHAIN_TYPE + "',",
200193
")",
201194
"py_runtime_pair(",
202-
" name = 'py_runtime_pair_for_py2_only',",
203-
" py2_runtime = ':py2_runtime',",
195+
" name = 'py_runtime_pair_for_py3_only',",
196+
" py3_runtime = ':py3_runtime',",
204197
")",
205198
"toolchain(",
206-
" name = 'py_toolchain_for_py2_only',",
207-
" toolchain = ':py_runtime_pair_for_py2_only',",
199+
" name = 'py_toolchain_for_py3_only',",
200+
" toolchain = ':py_runtime_pair_for_py3_only',",
208201
" toolchain_type = '" + TOOLCHAIN_TYPE + "',",
209202
")");
210203
}
@@ -215,11 +208,6 @@ public void runtimeObtainedFromToolchain() throws Exception {
215208
scratch.file(
216209
"pkg/BUILD",
217210
"py_binary(",
218-
" name = 'py2_bin',",
219-
" srcs = ['py2_bin.py'],",
220-
" python_version = 'PY2',",
221-
")",
222-
"py_binary(",
223211
" name = 'py3_bin',",
224212
" srcs = ['py3_bin.py'],",
225213
" python_version = 'PY3',",
@@ -228,17 +216,12 @@ public void runtimeObtainedFromToolchain() throws Exception {
228216
"--incompatible_use_python_toolchains=true",
229217
"--extra_toolchains=//toolchains:py_toolchain");
230218

231-
ConfiguredTarget py2 = getConfiguredTarget("//pkg:py2_bin");
232219
ConfiguredTarget py3 = getConfiguredTarget("//pkg:py3_bin");
233220

234-
String py2Path = getInterpreterPathFromStub(py2);
235221
String py3Path = getInterpreterPathFromStub(py3);
236-
assertThat(py2Path).isEqualTo("/system/python2");
237222
assertThat(py3Path).isEqualTo("/system/python3");
238223

239-
String py2Shebang = getShebangFromStub(py2);
240224
String py3Shebang = getShebangFromStub(py3);
241-
assertThat(py2Shebang).isEqualTo("#!/usr/bin/env python");
242225
assertThat(py3Shebang).isEqualTo("#!/usr/bin/env python3");
243226
}
244227

@@ -248,41 +231,21 @@ public void toolchainCanOmitUnusedRuntimeVersion() throws Exception {
248231
scratch.file(
249232
"pkg/BUILD",
250233
"py_binary(",
251-
" name = 'py2_bin',",
252-
" srcs = ['py2_bin.py'],",
253-
" python_version = 'PY2',",
234+
" name = 'py3_bin',",
235+
" srcs = ['py3_bin.py'],",
236+
" python_version = 'PY3',",
254237
")");
255238
useConfiguration(
256239
"--incompatible_use_python_toolchains=true",
257-
"--extra_toolchains=//toolchains:py_toolchain_for_py2_only");
240+
"--extra_toolchains=//toolchains:py_toolchain_for_py3_only");
258241

259-
String path = getInterpreterPathFromStub(getConfiguredTarget("//pkg:py2_bin"));
260-
assertThat(path).isEqualTo("/system/python2");
242+
String path = getInterpreterPathFromStub(getConfiguredTarget("//pkg:py3_bin"));
243+
assertThat(path).isEqualTo("/system/python3");
261244
}
262245

263246
@Test
264247
public void toolchainTakesPrecedenceOverLegacyFlags() throws Exception {
265248
defineToolchains();
266-
scratch.file(
267-
"pkg/BUILD",
268-
"py_binary(",
269-
" name = 'py2_bin',",
270-
" srcs = ['py2_bin.py'],",
271-
" python_version = 'PY2',",
272-
")");
273-
useConfiguration(
274-
"--incompatible_use_python_toolchains=true",
275-
"--extra_toolchains=//toolchains:py_toolchain",
276-
"--python_path=/better/not/be/this/one");
277-
278-
String path = getInterpreterPathFromStub(getConfiguredTarget("//pkg:py2_bin"));
279-
assertThat(path).isEqualTo("/system/python2");
280-
}
281-
282-
@Test
283-
public void toolchainIsMissingNeededRuntime() throws Exception {
284-
defineToolchains();
285-
reporter.removeHandler(failFastHandler);
286249
scratch.file(
287250
"pkg/BUILD",
288251
"py_binary(",
@@ -292,10 +255,11 @@ public void toolchainIsMissingNeededRuntime() throws Exception {
292255
")");
293256
useConfiguration(
294257
"--incompatible_use_python_toolchains=true",
295-
"--extra_toolchains=//toolchains:py_toolchain_for_py2_only");
258+
"--extra_toolchains=//toolchains:py_toolchain",
259+
"--python_path=/better/not/be/this/one");
296260

297-
getConfiguredTarget("//pkg:py3_bin");
298-
assertContainsEvent("The Python toolchain does not provide a runtime for Python version PY3");
261+
String path = getInterpreterPathFromStub(getConfiguredTarget("//pkg:py3_bin"));
262+
assertThat(path).isEqualTo("/system/python3");
299263
}
300264

301265
/**
@@ -330,16 +294,16 @@ private void defineCustomToolchain(String... lines) throws Exception {
330294
}
331295

332296
/**
333-
* Defines a PY2 py_binary target at //pkg:pybin, configures it to use the custom toolchain
297+
* Defines a py_binary target at //pkg:pybin, configures it to use the custom toolchain
334298
* //toolchains:custom, and attempts to retrieve it with {@link #getConfiguredTarget}.
335299
*/
336-
private void analyzePy2BinaryTargetUsingCustomToolchain() throws Exception {
300+
private void analyzePyBinaryTargetUsingCustomToolchain() throws Exception {
337301
scratch.file(
338302
"pkg/BUILD",
339303
"py_binary(",
340304
" name = 'pybin',",
341305
" srcs = ['pybin.py'],",
342-
" python_version = 'PY2',",
306+
" python_version = 'PY3',",
343307
")");
344308
useConfiguration(
345309
"--incompatible_use_python_toolchains=true",
@@ -352,48 +316,42 @@ public void toolchainInfoFieldIsMissing() throws Exception {
352316
reporter.removeHandler(failFastHandler);
353317
defineCustomToolchain(
354318
"return platform_common.ToolchainInfo(",
355-
" py2_runtime = PyRuntimeInfo(",
356-
" interpreter_path = '/system/python2',",
357-
" python_version = 'PY2')",
319+
" py3_runtime = PyRuntimeInfo(",
320+
" interpreter_path = '/system/python3',",
321+
" python_version = 'PY3')",
358322
")");
359-
// Use PY2 binary to test that we still validate the PY3 field even when it's not needed.
360-
analyzePy2BinaryTargetUsingCustomToolchain();
323+
analyzePyBinaryTargetUsingCustomToolchain();
361324
assertContainsEvent(
362-
"Error parsing the Python toolchain's ToolchainInfo: field 'py3_runtime' is missing");
325+
"Error parsing the Python toolchain's ToolchainInfo: field 'py2_runtime' is missing");
363326
}
364327

365328
@Test
366329
public void toolchainInfoFieldHasBadType() throws Exception {
367330
reporter.removeHandler(failFastHandler);
368331
defineCustomToolchain(
369332
"return platform_common.ToolchainInfo(",
370-
" py2_runtime = PyRuntimeInfo(",
371-
" interpreter_path = '/system/python2',",
372-
" python_version = 'PY2'),",
373-
" py3_runtime = 'abc',",
333+
" py3_runtime = PyRuntimeInfo(",
334+
" interpreter_path = '/system/python3',",
335+
" python_version = 'PY3'),",
336+
" py2_runtime = 'abc',",
374337
")");
375-
// Use PY2 binary to test that we still validate the PY3 field even when it's not needed.
376-
analyzePy2BinaryTargetUsingCustomToolchain();
338+
analyzePyBinaryTargetUsingCustomToolchain();
377339
assertContainsEvent(
378340
"Error parsing the Python toolchain's ToolchainInfo: Expected a PyRuntimeInfo in field "
379-
+ "'py3_runtime', but got 'string'");
341+
+ "'py2_runtime', but got 'string'");
380342
}
381343

382344
@Test
383345
public void toolchainInfoFieldHasBadVersion() throws Exception {
384346
reporter.removeHandler(failFastHandler);
385347
defineCustomToolchain(
386348
"return platform_common.ToolchainInfo(",
387-
" py2_runtime = PyRuntimeInfo(",
388-
" interpreter_path = '/system/python2',",
389-
" python_version = 'PY2'),",
390349
" py3_runtime = PyRuntimeInfo(",
391350
" interpreter_path = '/system/python3',",
392351
// python_version is erroneously set to PY2 for the PY3 field.
393352
" python_version = 'PY2'),",
394353
")");
395-
// Use PY2 binary to test that we still validate the PY3 field even when it's not needed.
396-
analyzePy2BinaryTargetUsingCustomToolchain();
354+
analyzePyBinaryTargetUsingCustomToolchain();
397355
assertContainsEvent(
398356
"Error retrieving the Python runtime from the toolchain: Expected field 'py3_runtime' to "
399357
+ "have a runtime with python_version = 'PY3', but got python_version = 'PY2'");

src/test/java/com/google/devtools/build/lib/packages/util/BazelMockPythonSupport.java

-6
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,12 @@ public void setup(MockToolsConfig config) throws IOException {
6161
"constraint_setting(name = 'py2_interpreter_path')",
6262
"constraint_setting(name = 'py3_interpreter_path')",
6363
"py_runtime(",
64-
" name = 'py2_interpreter',",
65-
" interpreter_path = '/usr/bin/mockpython2',",
66-
" python_version = 'PY2',",
67-
")",
68-
"py_runtime(",
6964
" name = 'py3_interpreter',",
7065
" interpreter_path = '/usr/bin/mockpython3',",
7166
" python_version = 'PY3',",
7267
")",
7368
"py_runtime_pair(",
7469
" name = 'default_py_runtime_pair',",
75-
" py2_runtime = ':py2_interpreter',",
7670
" py3_runtime = ':py3_interpreter',",
7771
")",
7872
"toolchain(",

src/test/java/com/google/devtools/build/lib/rules/python/PyBaseConfiguredTargetTestBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void goodSrcsVersionValue() throws Exception {
6161
"pkg/BUILD",
6262
ruleName + "(",
6363
" name = 'foo',",
64-
" srcs_version = 'PY2',",
64+
" srcs_version = 'PY3',",
6565
" srcs = ['foo.py'])");
6666
getConfiguredTarget("//pkg:foo");
6767
assertNoEvents();

src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java

-49
Original file line numberDiff line numberDiff line change
@@ -32,55 +32,6 @@ public PyBinaryConfiguredTargetTest() {
3232
super("py_binary");
3333
}
3434

35-
/**
36-
* Creates a target //pkg:bin with the given version attr and that depends on a target //pkg:lib
37-
* having the given sources version attr.
38-
*/
39-
private void declareBinDependingOnLibWithVersions(String binVersion, String libSrcsVersion)
40-
throws Exception {
41-
scratch.file(
42-
"pkg/BUILD",
43-
"py_library(name = 'lib',",
44-
" srcs = [],",
45-
" srcs_version = '" + libSrcsVersion + "')",
46-
"py_binary(name = 'bin',",
47-
" srcs = ['bin.py'],",
48-
" deps = [':lib'],",
49-
" python_version = '" + binVersion + "')");
50-
}
51-
52-
@Test
53-
public void python2WithPy3SrcsVersionDependency() throws Exception {
54-
PythonTestUtils.assumeIsBazel(); // Google has py2 disabled
55-
setBuildLanguageOptions(
56-
"--experimental_builtins_injection_override=-py_test,-py_binary,-py_library");
57-
declareBinDependingOnLibWithVersions("PY2", "PY3");
58-
assertThat(getPyExecutableDeferredError("//pkg:bin"))
59-
.startsWith(
60-
"//pkg:bin: This target is being built for Python 2 but (transitively) "
61-
+ "includes Python 3-only sources");
62-
}
63-
64-
@Test
65-
public void python2WithPy3OnlySrcsVersionDependency() throws Exception {
66-
PythonTestUtils.assumeIsBazel(); // Google has py2 disabled
67-
setBuildLanguageOptions(
68-
"--experimental_builtins_injection_override=-py_test,-py_binary,-py_library");
69-
declareBinDependingOnLibWithVersions("PY2", "PY3ONLY");
70-
assertThat(getPyExecutableDeferredError("//pkg:bin"))
71-
.contains("being built for Python 2 but (transitively) includes Python 3-only sources");
72-
}
73-
74-
@Test
75-
public void python3WithPy2OnlySrcsVersionDependency_NewSemantics() throws Exception {
76-
PythonTestUtils.assumeIsBazel(); // Google has py2 disabled
77-
setBuildLanguageOptions(
78-
"--experimental_builtins_injection_override=-py_test,-py_binary,-py_library");
79-
declareBinDependingOnLibWithVersions("PY3", "PY2ONLY");
80-
assertThat(getPyExecutableDeferredError("//pkg:bin"))
81-
.contains("being built for Python 3 but (transitively) includes Python 2-only sources");
82-
}
83-
8435
@Test
8536
public void filesToBuild() throws Exception {
8637
scratch.file("pkg/BUILD",

0 commit comments

Comments
 (0)