Add sys.getandroidapilevel()

Issue #28740: Add sys.getandroidapilevel(): return the build time
API version of Android as an integer.

Function only available on Android.
This commit is contained in:
Victor Stinner 2016-12-02 01:13:46 +01:00
parent edfe8869c8
commit d6958ac6c0
5 changed files with 44 additions and 2 deletions

View File

@ -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`.

View File

@ -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'

View File

@ -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):

View File

@ -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.

View File

@ -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 */
};