changeset: 105409:be70d64bbf88 parent: 105406:a21a8943c59e user: Victor Stinner date: Fri Dec 02 01:13:46 2016 +0100 files: Doc/library/sys.rst Lib/test/support/__init__.py Lib/test/test_sys.py Misc/NEWS Python/sysmodule.c description: Add sys.getandroidapilevel() Issue #28740: Add sys.getandroidapilevel(): return the build time API version of Android as an integer. Function only available on Android. diff -r a21a8943c59e -r be70d64bbf88 Doc/library/sys.rst --- a/Doc/library/sys.rst Thu Dec 01 11:37:47 2016 -0500 +++ b/Doc/library/sys.rst Fri Dec 02 01:13:46 2016 +0100 @@ -404,6 +404,15 @@ .. 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 -r a21a8943c59e -r be70d64bbf88 Lib/test/support/__init__.py --- a/Lib/test/support/__init__.py Thu Dec 01 11:37:47 2016 -0500 +++ b/Lib/test/support/__init__.py Fri Dec 02 01:13:46 2016 +0100 @@ -766,8 +766,13 @@ 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 -r a21a8943c59e -r be70d64bbf88 Lib/test/test_sys.py --- a/Lib/test/test_sys.py Thu Dec 01 11:37:47 2016 -0500 +++ b/Lib/test/test_sys.py Fri Dec 02 01:13:46 2016 +0100 @@ -826,6 +826,13 @@ 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 -r a21a8943c59e -r be70d64bbf88 Misc/NEWS --- a/Misc/NEWS Thu Dec 01 11:37:47 2016 -0500 +++ b/Misc/NEWS Fri Dec 02 01:13:46 2016 +0100 @@ -160,6 +160,9 @@ 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 -r a21a8943c59e -r be70d64bbf88 Python/sysmodule.c --- a/Python/sysmodule.c Thu Dec 01 11:37:47 2016 -0500 +++ b/Python/sysmodule.c Fri Dec 02 01:13:46 2016 +0100 @@ -1363,6 +1363,20 @@ 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 @@ 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 */ };