[Python-Dev] Exposing the Android platform existence to Python modules

Shiz hi at shiz.me
Sat Aug 2 14:00:04 CEST 2014


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Akira Li wrote:
> Python uses os.name, sys.platform, and various functions from
> `platform` module to provide version info:
> 
> - coarse: os.name is 'posix', 'nt', 'ce', 'java' [1]. It is defined
> by availability of some builtin modules ('posix', 'nt' in particular)
> at import time.
> 
> - finer: sys.platform may start with freebsd, linux, win, cygwin,
> darwin (`uname -s`). It is defined at python build time.
> 
> - detailed: `platform` module. It provides as much info as possible 
> e.g., platform.uname(), platform.platform(). It may use runtime
> commands to get it.
> 
> If Android is posixy enough (would `posix` module work on Android?) 
> then os.name could be left 'posix'.
> 
> You could set sys.platform to 'android' (like sys.platform may be 
> 'cygwin' on Windows) if Android is not like *any other* Linux 
> distribution (from the point of view of writing a working Python code
> on it) i.e., if Android is further from other Linux distribution
> than freebsd, linux, darwin from each other then it might deserve 
> sys.platform slot.
> 
> If sys.platform is left 'linux' (like sys.platform is 'darwin' on
> iOS) then platform module could be used to detect Android e.g., 
> platform.linux_distribution() though (it might be removed in Python
> 3.6) it is unpredictable [2] unless you fix it on your python
> distribution, e.g., here's an output on my machine:
> 
>>>> import platform platform.linux_distribution()
> ('Ubuntu', '14.04', 'trusty')
> 
> For example:
> 
> is_android = (platform.linux_distribution()[0] == 'Android')
> 
> You could also define platform.android_version() that can provide
> Android specific version details as much as you need:
> 
> is_android = bool(platform.android_version().release)
> 
> You could provide an alias android_ver (like existing java_ver,
> libc_ver, mac_ver, win32_ver).
> 
> See also, "When to use os.name, sys.platform, or platform.system?"
> [3]
> 
> Unrelated, TIL [4]:
> 
> Android is a Linux distribution according to the Linux Foundation
> 
> [1] https://docs.python.org/3.4/library/os.html#os.name [2]
> http://bugs.python.org/issue1322 [3] 
> http://stackoverflow.com/questions/4553129/when-to-use-os-name-sys-platform-or-platform-system
>
> 
[4] http://en.wikipedia.org/wiki/Android_(operating_system)
> 
> 
> btw, does it help adding os.get_shell_executable() [5] function, to 
> avoid hacking subprocess module, so that os.confstr('CS_PATH') or 
> os.defpath on Android could be defined to include /system/bin
> instead?
> 
> [5] http://bugs.python.org/issue16353

Thanks for the detailed information!

I would consider Android at least POSIX-y enough for os.name to be
considered 'posix'. It doesn't implement a few POSIX-mandated things
like POSIX semaphores, but aside from that I would largely consider it
'compatible enough'.

I guess what is left is deciding whether to add a platform slot for
Android, or to stuff the detection in platform.linux_distribution(). I
feel like it would be a bit hacky for standard modules to rely on a
platform.linux_distribution() return value though, it seems mostly
useful for display purposes.

Phil Thompson's idea of setting sys.platform to 'linux-android' also
occurred to me. Under the premise that we can get users to use
sys.platform.startswith('linux'), this seems like the best solution in
my eyes: it both allows for existing code to continue the assumption
that they are running on a Linux platform, which I believe to be correct
in a lot of places, and Python modules to use a solid value to check if
they need to behave differently when running on Android.

On a sidenote, Kivy and SL4A/Py4A do not address this, no. From what
I've seen from their patches they are mostly there to get Python
compiling and running in the first place, not necessarily about fixing
every compatibility issue. :)

As for the os.get_shell_executable(), that seems like a good solution
for the issue that occurs in the subprocess module indeed. I'd
personally prefer it to manual checking within the module.

Kind regards,
Shiz
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.22 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQQcBAEBCgAGBQJT3NLBAAoJEICfd9ZVuxW+SnIf/jZmnxyMTcFE5IyrmOC5v53M
AklKWhVK/XeK5gYAiglV7+JwIpkEHyiiIyik5QKr/ssQ/D6EjZTmb7guxoX9QZml
pWHukSmEpHTJVUDtSQ9OqgRADisZKV/8Yu1pLRIqe5zDcyZLZZg7Fg01rvqpBHvp
qhVzp2jdLCrcdlVZFKk3Hk04DgbJD9SUYg7ITCqj4qr5wwphCwCYfbGGlzeUXXyG
/zMWB6rI86DaOcy+b8DOK4Q6xdScnwIdFaV8A3lVEBi8b8DIl5ffGe8t6WtnUBOE
XGXh2wLZvnqYr31rGc0nRP16osm1usipq6jL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