summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Doc/library/sys.rst9
-rw-r--r--Lib/test/support/__init__.py9
-rw-r--r--Lib/test/test_sys.py7
-rw-r--r--Misc/NEWS3
-rw-r--r--Python/sysmodule.c18
5 files changed, 44 insertions, 2 deletions
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index 2d14a1d..f8ef83a 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -404,6 +404,15 @@ always available.
.. versionadded:: 3.4
+.. function:: getandroidapilevel()
+
+ Return the build time API version of Android as an integer.
+
+ Availability: Android.
+
+ .. versionadded:: 3.7
+
+
.. function:: getcheckinterval()
Return the interpreter's "check interval"; see :func:`setcheckinterval`.
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 52c908e..eb5a89a 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -766,8 +766,13 @@ requires_lzma = unittest.skipUnless(lzma, 'requires lzma')
is_jython = sys.platform.startswith('java')
-_ANDROID_API_LEVEL = sysconfig.get_config_var('ANDROID_API_LEVEL')
-is_android = (_ANDROID_API_LEVEL is not None and _ANDROID_API_LEVEL > 0)
+try:
+ # constant used by requires_android_level()
+ _ANDROID_API_LEVEL = sys.getandroidapilevel()
+ is_android = True
+except AttributeError:
+ # sys.getandroidapilevel() is only available on Android
+ is_android = False
if sys.platform != 'win32':
unix_shell = '/system/bin/sh' if is_android else '/bin/sh'
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index df9ebd4..828421c 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -826,6 +826,13 @@ class SysModuleTest(unittest.TestCase):
rc, stdout, stderr = assert_python_ok('-c', code)
self.assertEqual(stdout.rstrip(), b'True')
+ @unittest.skipUnless(hasattr(sys, 'getandroidapilevel'),
+ 'need sys.getandroidapilevel()')
+ def test_getandroidapilevel(self):
+ level = sys.getandroidapilevel()
+ self.assertIsInstance(level, int)
+ self.assertGreater(level, 0)
+
@test.support.cpython_only
class SizeofTest(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
index 055bcad..2d5b6bd 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -160,6 +160,9 @@ Core and Builtins
Library
-------
+- Issue #28740: Add sys.getandroidapilevel(): return the build time API version
+ of Android as an integer. Function only available on Android.
+
- Issue #26273: Add new :data:`socket.TCP_CONGESTION` (Linux 2.6.13) and
:data:`socket.TCP_USER_TIMEOUT` (Linux 2.6.37) constants. Patch written by
Omar Sandoval.
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 2a3f36c..19a3850 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1363,6 +1363,20 @@ PyDoc_STRVAR(is_finalizing_doc,
Return True if Python is exiting.");
+#ifdef ANDROID_API_LEVEL
+PyDoc_STRVAR(getandroidapilevel_doc,
+"getandroidapilevel()\n\
+\n\
+Return the build time API version of Android as an integer.");
+
+static PyObject *
+sys_getandroidapilevel(PyObject *self)
+{
+ return PyLong_FromLong(ANDROID_API_LEVEL);
+}
+#endif /* ANDROID_API_LEVEL */
+
+
static PyMethodDef sys_methods[] = {
/* Might as well keep this in alphabetic order */
{"callstats", (PyCFunction)sys_callstats, METH_NOARGS,
@@ -1447,6 +1461,10 @@ static PyMethodDef sys_methods[] = {
METH_VARARGS | METH_KEYWORDS, set_asyncgen_hooks_doc},
{"get_asyncgen_hooks", sys_get_asyncgen_hooks, METH_NOARGS,
get_asyncgen_hooks_doc},
+#ifdef ANDROID_API_LEVEL
+ {"getandroidapilevel", (PyCFunction)sys_getandroidapilevel, METH_NOARGS,
+ getandroidapilevel_doc},
+#endif
{NULL, NULL} /* sentinel */
};