Skip to content

Commit 4e0eeeb

Browse files
committed
py: Implement sys.maxsize, standard way to check platform "bitness".
Implementing it as a static constant is a bit peculiar and require cooperation from long int implementation.
1 parent 3816182 commit 4e0eeeb

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

py/modsys.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdint.h>
28+
#include <limits.h>
2729
#include "mpconfig.h"
2830
#include "misc.h"
2931
#include "qstr.h"
@@ -33,6 +35,8 @@
3335
#include "objlist.h"
3436
#include "objtuple.h"
3537
#include "objstr.h"
38+
#include "mpz.h"
39+
#include "objint.h"
3640

3741
#if MICROPY_PY_SYS
3842

@@ -44,6 +48,8 @@ extern struct _dummy_t mp_sys_stdin_obj;
4448
extern struct _dummy_t mp_sys_stdout_obj;
4549
extern struct _dummy_t mp_sys_stderr_obj;
4650

51+
extern mp_obj_int_t mp_maxsize_obj;
52+
4753
mp_obj_list_t mp_sys_path_obj;
4854
mp_obj_list_t mp_sys_argv_obj;
4955
#define I(n) MP_OBJ_NEW_SMALL_INT(n)
@@ -70,6 +76,19 @@ STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
7076
#else
7177
{ MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_big) },
7278
#endif
79+
#if MICROPY_PY_SYS_MAXSIZE
80+
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
81+
// INT_MAX is not representable as small int, as we know that small int
82+
// takes one bit for tag. So, we have little choice but to provide this
83+
// value. Apps also should be careful to not try to compare sys.maxsize
84+
// with some number (which may not fit in available int size), but instead
85+
// count number of significant bits in sys.maxsize.
86+
{ MP_OBJ_NEW_QSTR(MP_QSTR_maxsize), MP_OBJ_NEW_SMALL_INT(INT_MAX >> 1) },
87+
#else
88+
{ MP_OBJ_NEW_QSTR(MP_QSTR_maxsize), (mp_obj_t)&mp_maxsize_obj },
89+
#endif
90+
#endif
91+
7392

7493
#if MICROPY_PY_SYS_EXIT
7594
{ MP_OBJ_NEW_QSTR(MP_QSTR_exit), (mp_obj_t)&mp_sys_exit_obj },

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ typedef double mp_float_t;
336336
#define MICROPY_PY_SYS (1)
337337
#endif
338338

339+
// Whether to provide "sys.maxsize" constant
340+
#ifndef MICROPY_PY_SYS_MAXSIZE
341+
#define MICROPY_PY_SYS_MAXSIZE (0)
342+
#endif
343+
339344
// Whether to provide "sys.exit" function
340345
#ifndef MICROPY_PY_SYS_EXIT
341346
#define MICROPY_PY_SYS_EXIT (0)

py/objint_longlong.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
#define SUFFIX ""
5151
#endif
5252

53+
#if MICROPY_PY_SYS_MAXSIZE
54+
// Export value for sys.maxsize
55+
const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, INT_MAX};
56+
#endif
57+
5358
bool mp_obj_int_is_positive(mp_obj_t self_in) {
5459
if (MP_OBJ_IS_SMALL_INT(self_in)) {
5560
return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;

py/objint_mpz.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@
4343

4444
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
4545

46+
#if MICROPY_PY_SYS_MAXSIZE
47+
// Export value for sys.maxsize
48+
#define DIG_MASK ((1 << MPZ_DIG_SIZE) - 1)
49+
STATIC const mpz_dig_t maxsize_dig[MPZ_NUM_DIG_FOR_INT] = {
50+
(INT_MAX >> MPZ_DIG_SIZE * 0) & DIG_MASK,
51+
(INT_MAX >> MPZ_DIG_SIZE * 1) & DIG_MASK,
52+
(INT_MAX >> MPZ_DIG_SIZE * 2) & DIG_MASK,
53+
#if (INT_MAX >> MPZ_DIG_SIZE * 2) > DIG_MASK
54+
(INT_MAX >> MPZ_DIG_SIZE * 3) & DIG_MASK,
55+
(INT_MAX >> MPZ_DIG_SIZE * 4) & DIG_MASK,
56+
// (INT_MAX >> MPZ_DIG_SIZE * 5) & DIG_MASK,
57+
#endif
58+
};
59+
const mp_obj_int_t mp_maxsize_obj = {
60+
{&mp_type_int},
61+
{.fixed_dig = 1, .len = MPZ_NUM_DIG_FOR_INT, .alloc = MPZ_NUM_DIG_FOR_INT, .dig = (mpz_dig_t*)maxsize_dig}
62+
};
63+
#undef DIG_MASK
64+
#endif
65+
4666
STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
4767
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
4868
o->base.type = &mp_type_int;

py/qstrdefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ Q(stdout)
352352
Q(stderr)
353353
Q(version)
354354
Q(version_info)
355+
#if MICROPY_PY_SYS_MAXSIZE
356+
Q(maxsize)
357+
#endif
355358
#endif
356359

357360
#if MICROPY_PY_STRUCT

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#define MICROPY_PY_BUILTINS_FROZENSET (1)
4646
#define MICROPY_PY_SYS_EXIT (1)
4747
#define MICROPY_PY_SYS_PLATFORM "linux"
48+
#define MICROPY_PY_SYS_MAXSIZE (1)
4849
#define MICROPY_PY_SYS_STDFILES (1)
4950
#define MICROPY_PY_CMATH (1)
5051
#define MICROPY_PY_IO_FILEIO (1)

0 commit comments

Comments
 (0)