[Python-Dev] Exposing the Android platform existence to Python modules (original) (raw)
Akira Li 4kir4.1i at gmail.com
Sat Aug 2 03:53:45 CEST 2014
- Previous message: [Python-Dev] Exposing the Android platform existence to Python modules
- Next message: [Python-Dev] Exposing the Android platform existence to Python modules
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Shiz <hi at shiz.me> writes:
Hi folks,
I’m working on porting CPython to the Android platform, and while making decent progress, I’m currently stuck at a higher-level issue than adding #ifdefs for ANDROID to C extension modules. The idea is, not only CPython extension modules have some assumptions that don’t seem to fit Android’s mold, some default Python-written modules do as well. However, whereas CPython extensions can trivially check if we’re building for Android by checking the ANDROID compiler macro, Python modules can do no such check, and are left wondering how to figure out if the platform they are currently running on is an Android one. To my knowledge there is no reliable way to detect if one is using Android as a vehicle for their journey using any other way. Now, the main question is: what would be the best way to ‘expose’ the indication that Android is being ran on to Python-living modules? My own thought was to add sys.getlinuxuserland(), or platform.linuxuserland(), in similar vein to sys.getwindowsversion() and platform.linuxdistribution(), which could return information about the userland of running CPython instance, instead of knowing merely the kernel and the distribution. This way, code could trivially check if it ran on the GNU(+associates) userland, or under a BSD-ish userland, or Android… and adjust its behaviour accordingly. I would be delighted to hear comments on this proposal, or better yet, alternative solutions. :) Kind regards, Shiz P.S.: I am well aware that Android might as well never be officially supported in CPython. In that case, consider this a thought experiment of how it /would/ be handled. :)
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
-- Akira
- Previous message: [Python-Dev] Exposing the Android platform existence to Python modules
- Next message: [Python-Dev] Exposing the Android platform existence to Python modules
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]