Skip to content
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

gh-71052: Change Android's sys.platform from "linux" to "android" #116215

Merged
merged 3 commits into from
Mar 11, 2024

Conversation

mhsmith
Copy link
Member

@mhsmith mhsmith commented Mar 1, 2024

As discussed in PEP 738, although Android is based on Linux, it differs in enough significant ways that a separate name is justified.

Related changes:

  • I've replaced all instances of hasattr(sys, "getandroidapilevel") with a sys.platform check. This is generally easier to read, and more consistent with how the other platforms are handled.

  • I've checked all the places I could find that compare sys.platform to "linux", and added "android" where appropriate. These are mostly in the tests, but there are also a few in the stdlib.

Copy link
Member

@FFY00 FFY00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, I think this is a backwards incompatible change, which could result in downstream code breaking. While I think we should probably have a better way to identify Android platforms, I don't know exactly how I feel about this approach. I think at least we should get the input from people shipping and packaging Python on Android.

cc @freakboy3742 @misl6

@erlend-aasland

This comment was marked as outdated.

@mhsmith
Copy link
Member Author

mhsmith commented Mar 1, 2024

@freakboy3742 was actually the one who convinced me to go this way – his reasoning is on the PEP's Discourse thread here.

@freakboy3742
Copy link
Contributor

Hum, I think this is a backwards incompatible change, which could result in downstream code breaking. While I think we should probably have a better way to identify Android platforms, I don't know exactly how I feel about this approach. I think at least we should get the input from people shipping and packaging Python on Android.

As I said in the discussion thread on PEP 738 - I'm a hard +1 on sys.platform == "android".

Yes, this is technically backwards incompatible with the current state of Android support in the CPython source tree. However, I'd counter that:

  1. CPython doesn't currently have official support for Android. It has defacto support by way of "not breaking the build", one additional API entry point (sys.getandroidapilevel()), and a handful of other conditional branches in code; but it's not a Tier 3 platform. To the extent that the ecosystem supports Android currently, that support is very ad hoc, and almost entirely performed by the people tagged in this thread (or at most one degree of separation from those people). There are no Android packages on PyPI.

  2. The current state of detecting Android is the source of so many headaches in the rest of the ecosystem. Yes there is code that will "just run" because Android reports as Linux - but that code is almost always so trivial that it would likely also work on iOS - it's really testing for POSIX, not Linux. Any package that does anything even remotely complicated requires patching to run on Android - at the very least in the build system, and often in user-space code as well. The fact that sys.platform reports as Linux means we need to use workarounds like this one everywhere that we do platform detection, because we can't just use a dictionary lookup. The only other platforms that cause this much headache are the ones that include the OS version in sys.platform - which is a pattern that (thankfully) appears to be slowly undergoing deprecation.

Repeating what I said in the discuss thread: Yes, technically, Android is a Linux. However, that is a distinction that isn't helpful in practice, because “Linux” isn’t just a kernel - it’s lots of other device expectations, filesystem expectations, userspace expectations, and more.

The most practical manifestation of this: If Android is indeed "a Linux", why are any patches needed to CPython at all? Shouldn't Android "just work" as a Linux, or fall out of trivial configuration checks? Unless I'm mistaken, that there are less conditional branches in the test suite for FreeBSD support than are required for Android. If that isn't an indication that Android isn't "a Linux" in practice, I don't know what is.

Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for chiming in @freakboy3742.

Looks good to me. I had to look twice at the test_sysconfig change, though 😄

Lib/test/test_os.py Outdated Show resolved Hide resolved
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@mhsmith
Copy link
Member Author

mhsmith commented Mar 4, 2024

@erlend-aasland: I've applied your suggestions, so I think this should be ready to merge now.

Copy link
Member

@FFY00 FFY00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good to me, the only thing missing is updating the documentation.

We should add Android to the "For other systems (...)" list in the sys.platform documentation (Doc/library/sys.rst), and add a versionchanged field noting this change.

After that, I think we are good to merge, thanks @mhsmith!

@bedevere-app
Copy link

bedevere-app bot commented Mar 4, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@mhsmith mhsmith force-pushed the android-sys-platform branch from 4431efc to d49bd1a Compare March 5, 2024 13:46
@mhsmith
Copy link
Member Author

mhsmith commented Mar 5, 2024

I have made the requested changes; please review again.

I've also cleaned up the sys.platform documentation as follows:

  • Removed the reference to "append platform-specific components to sys.path". This has not been true since Python 3.7 (https://bugs.python.org/issue28046).
  • Added iOS as specified in PEP 730.
  • Sorted the table in alphabetical order.
  • Moved the table to the top, since it now contains all supported platforms [EDIT – except FreeBSD].
  • Removed recommendation to use startswith on Linux and AIX. The last Python version to require this for Linux was 3.2, and the last for AIX was 3.7, both of which are now obsolete.

@bedevere-app
Copy link

bedevere-app bot commented Mar 5, 2024

Thanks for making the requested changes!

@erlend-aasland, @FFY00: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested review from erlend-aasland and FFY00 March 5, 2024 13:55
Copy link
Member

@FFY00 FFY00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhsmith, sorry for the delay, the documentation changes look good to me, thank you!

@FFY00 FFY00 merged commit 872c071 into python:main Mar 11, 2024
35 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants