Skip to content

Commit d6b31e4

Browse files
committed
py: Change mp_obj_int_is_positive to more general mp_obj_int_sign.
This function returns the sign (-1, 0 or 1) of the integer object.
1 parent 93b3726 commit d6b31e4

5 files changed

Lines changed: 40 additions & 11 deletions

File tree

py/mpprint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char
217217
char prefix_buf[4];
218218
char *prefix = prefix_buf;
219219

220-
if (mp_obj_int_is_positive(x)) {
220+
if (mp_obj_int_sign(x) > 0) {
221221
if (flags & PF_FLAG_SHOW_SIGN) {
222222
*prefix++ = '+';
223223
} else if (flags & PF_FLAG_SPACE_SIGN) {

py/objint.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,15 @@ char *mp_obj_int_formatted(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size,
259259

260260
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
261261

262-
bool mp_obj_int_is_positive(mp_obj_t self_in) {
263-
return mp_obj_get_int(self_in) >= 0;
262+
int mp_obj_int_sign(mp_obj_t self_in) {
263+
mp_int_t val = mp_obj_get_int(self_in);
264+
if (val < 0) {
265+
return -1;
266+
} else if (val > 0) {
267+
return 1;
268+
} else {
269+
return 0;
270+
}
264271
}
265272

266273
// This must handle int and bool types, and must raise a

py/objint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_
5757
int base, const char *prefix, char base_char, char comma);
5858
mp_int_t mp_obj_int_hash(mp_obj_t self_in);
5959
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len, byte *buf);
60-
bool mp_obj_int_is_positive(mp_obj_t self_in);
60+
int mp_obj_int_sign(mp_obj_t self_in);
6161
mp_obj_t mp_obj_int_abs(mp_obj_t self_in);
6262
mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in);
6363
mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in);

py/objint_longlong.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,21 @@ void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len,
7171
}
7272
}
7373

74-
bool mp_obj_int_is_positive(mp_obj_t self_in) {
74+
int mp_obj_int_sign(mp_obj_t self_in) {
75+
mp_longint_impl_t val;
7576
if (MP_OBJ_IS_SMALL_INT(self_in)) {
76-
return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
77+
val = MP_OBJ_SMALL_INT_VALUE(self_in);
78+
} else {
79+
mp_obj_int_t *self = self_in;
80+
val = self->val;
81+
}
82+
if (val < 0) {
83+
return -1;
84+
} else if (val > 0) {
85+
return 1;
86+
} else {
87+
return 0;
7788
}
78-
mp_obj_int_t *self = self_in;
79-
return self->val >= 0;
8089
}
8190

8291
// This must handle int and bool types, and must raise a

py/objint_mpz.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,25 @@ void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len,
113113
mpz_as_bytes(&self->mpz, big_endian, len, buf);
114114
}
115115

116-
bool mp_obj_int_is_positive(mp_obj_t self_in) {
116+
int mp_obj_int_sign(mp_obj_t self_in) {
117117
if (MP_OBJ_IS_SMALL_INT(self_in)) {
118-
return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
118+
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(self_in);
119+
if (val < 0) {
120+
return -1;
121+
} else if (val > 0) {
122+
return 1;
123+
} else {
124+
return 0;
125+
}
119126
}
120127
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
121-
return !self->mpz.neg;
128+
if (self->mpz.len == 0) {
129+
return 0;
130+
} else if (self->mpz.neg == 0) {
131+
return 1;
132+
} else {
133+
return -1;
134+
}
122135
}
123136

124137
// This must handle int and bool types, and must raise a

0 commit comments

Comments
 (0)