-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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-71042: Add platform.android_ver
#116674
Changes from 3 commits
ead6dcb
8934a4e
4dca7c4
4108dfc
646441b
4f65e8e
9fd7f44
b64f1f5
53e6511
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -542,6 +542,45 @@ def java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', '')): | |
|
||
return release, vendor, vminfo, osinfo | ||
|
||
|
||
AndroidVer = collections.namedtuple( | ||
"AndroidVer", | ||
"release api_level min_api_level manufacturer model device") | ||
|
||
def android_ver(release="", api_level=0, min_api_level=0, | ||
manufacturer="", model="", device=""): | ||
if sys.platform == "android": | ||
min_api_level = sys.getandroidapilevel() | ||
try: | ||
from ctypes import CDLL, c_char_p, create_string_buffer | ||
mhsmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except ImportError: | ||
pass | ||
else: | ||
# An NDK developer confirmed that this is an officially-supported | ||
# API (https://stackoverflow.com/a/28416743). Use `getattr` to avoid | ||
# private name mangling. | ||
system_property_get = getattr(CDLL("libc.so"), "__system_property_get") | ||
system_property_get.argtypes = (c_char_p, c_char_p) | ||
|
||
def getprop(name, default): | ||
PROP_VALUE_MAX = 92 # From sys/system_properties.h | ||
mhsmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
buffer = create_string_buffer(PROP_VALUE_MAX) | ||
length = system_property_get(name.encode("UTF-8"), buffer) | ||
if length == 0: | ||
return default | ||
else: | ||
return buffer.value.decode("UTF-8", "backslashreplace") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't seen such strings, but this function is likely to be used when building error reports, so perfect round-tripping is less important than making sure it doesn't cause any errors itself. For that reason, I'd prefer not to use |
||
|
||
release = getprop("ro.build.version.release", release) | ||
api_level = int(getprop("ro.build.version.sdk", api_level)) | ||
manufacturer = getprop("ro.product.manufacturer", manufacturer) | ||
model = getprop("ro.product.model", model) | ||
device = getprop("ro.product.device", device) | ||
|
||
return AndroidVer( | ||
release, api_level, min_api_level, manufacturer, model, device) | ||
|
||
|
||
### System name aliasing | ||
|
||
def system_alias(system, release, version): | ||
|
@@ -972,6 +1011,11 @@ def uname(): | |
system = 'Windows' | ||
release = 'Vista' | ||
|
||
# On Android, return the name and version of the OS rather than the kernel. | ||
if sys.platform == 'android': | ||
system = 'Android' | ||
release = android_ver().release | ||
|
||
vals = system, node, release, version, machine | ||
# Replace 'unknown' values with the more portable '' | ||
_uname_cache = uname_result(*map(_unknown_as_blank, vals)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add :func:`platform.android_ver`, which provides device and OS information | ||
on Android. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind to repeat here the difference with https://docs.python.org/dev/library/sys.html#sys.getandroidapilevel and so add a reference to the sys function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea; done.