blob: 2953ffad3c7500166719b6d6d5124ba1bb2deab6 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossume5372401993-03-16 12:15:04 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Integer object implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossume5372401993-03-16 12:15:04 +000028#include "modsupport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029
Guido van Rossum72481a31993-10-26 15:21:51 +000030#ifdef __STDC__
31#include <limits.h>
32#endif
33
34#ifndef LONG_MAX
35#define LONG_MAX 0X7FFFFFFFL
36#endif
37
38#ifndef LONG_MIN
39#define LONG_MIN (-LONG_MAX-1)
40#endif
41
42#ifndef CHAR_BIT
43#define CHAR_BIT 8
44#endif
45
46#define LONG_BIT (CHAR_BIT * sizeof(long))
47
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000049
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050intobject FalseObject = {
51 OB_HEAD_INIT(&Inttype)
52 0
53};
Guido van Rossum3f5da241990-12-20 15:06:42 +000054
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055intobject TrueObject = {
56 OB_HEAD_INIT(&Inttype)
57 1
58};
59
Guido van Rossum165e67e1990-10-14 20:02:26 +000060static object *
Guido van Rossum3a628451991-12-10 13:57:36 +000061err_ovf(msg)
62 char *msg;
Guido van Rossum165e67e1990-10-14 20:02:26 +000063{
Guido van Rossum3a628451991-12-10 13:57:36 +000064 err_setstr(OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000065 return NULL;
66}
67
Guido van Rossum3f5da241990-12-20 15:06:42 +000068/* Integers are quite normal objects, to make object handling uniform.
69 (Using odd pointers to represent integers would save much space
70 but require extra checks for this special case throughout the code.)
71 Since, a typical Python program spends much of its time allocating
72 and deallocating integers, these operations should be very fast.
73 Therefore we use a dedicated allocation scheme with a much lower
74 overhead (in space and time) than straight malloc(): a simple
75 dedicated free list, filled when necessary with memory from malloc().
76*/
77
78#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
79#define N_INTOBJECTS (BLOCK_SIZE / sizeof(intobject))
80
81static intobject *
82fill_free_list()
83{
84 intobject *p, *q;
85 p = NEW(intobject, N_INTOBJECTS);
86 if (p == NULL)
87 return (intobject *)err_nomem();
88 q = p + N_INTOBJECTS;
89 while (--q > p)
90 *(intobject **)q = q-1;
91 *(intobject **)q = NULL;
92 return p + N_INTOBJECTS - 1;
93}
94
95static intobject *free_list = NULL;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000096#ifndef NSMALLPOSINTS
97#define NSMALLPOSINTS 100
98#endif
99#ifndef NSMALLNEGINTS
100#define NSMALLNEGINTS 1
101#endif
102#if NSMALLNEGINTS + NSMALLPOSINTS > 0
103/* References to small integers are saved in this array so that they
104 can be shared.
105 The integers that are saved are those in the range
106 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
107*/
108static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
109#endif
110#ifdef COUNT_ALLOCS
111int quick_int_allocs, quick_neg_int_allocs;
112#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114object *
115newintobject(ival)
116 long ival;
117{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000118 register intobject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000119#if NSMALLNEGINTS + NSMALLPOSINTS > 0
120 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
121 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
122 INCREF(v);
123#ifdef COUNT_ALLOCS
124 if (ival >= 0)
125 quick_int_allocs++;
126 else
127 quick_neg_int_allocs++;
128#endif
129 return (object *) v;
130 }
131#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132 if (free_list == NULL) {
133 if ((free_list = fill_free_list()) == NULL)
134 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000136 v = free_list;
137 free_list = *(intobject **)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000138 v->ob_type = &Inttype;
139 v->ob_ival = ival;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000140 NEWREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000141#if NSMALLNEGINTS + NSMALLPOSINTS > 0
142 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
143 /* save this one for a following allocation */
144 INCREF(v);
145 small_ints[ival + NSMALLNEGINTS] = v;
146 }
147#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000148 return (object *) v;
149}
150
151static void
152int_dealloc(v)
153 intobject *v;
154{
155 *(intobject **)v = free_list;
156 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157}
158
159long
160getintvalue(op)
161 register object *op;
162{
163 if (!is_intobject(op)) {
Guido van Rossum5c52b6a1990-10-21 22:11:03 +0000164 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 return -1;
166 }
167 else
168 return ((intobject *)op) -> ob_ival;
169}
170
171/* Methods */
172
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000173/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000174static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000175int_print(v, fp, flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176 intobject *v;
177 FILE *fp;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000178 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179{
180 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000181 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
184static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000185int_repr(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186 intobject *v;
187{
188 char buf[20];
189 sprintf(buf, "%ld", v->ob_ival);
190 return newstringobject(buf);
191}
192
193static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000194int_compare(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195 intobject *v, *w;
196{
197 register long i = v->ob_ival;
198 register long j = w->ob_ival;
199 return (i < j) ? -1 : (i > j) ? 1 : 0;
200}
201
Guido van Rossum9bfef441993-03-29 10:43:31 +0000202static long
203int_hash(v)
204 intobject *v;
205{
206 long x = v -> ob_ival;
207 if (x == -1)
208 x = -2;
209 return x;
210}
211
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000213int_add(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000215 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216{
217 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000219 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000221 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000222 return err_ovf("integer addition");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223 return newintobject(x);
224}
225
226static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000227int_sub(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000229 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230{
231 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000233 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000235 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000236 return err_ovf("integer subtraction");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237 return newintobject(x);
238}
239
240static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000241int_mul(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000243 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244{
245 register long a, b;
246 double x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000248 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249 x = (double)a * (double)b;
Guido van Rossum72481a31993-10-26 15:21:51 +0000250 if (x > LONG_MAX || x < (double) (long) (LONG_MIN))
Guido van Rossum3a628451991-12-10 13:57:36 +0000251 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252 return newintobject(a * b);
253}
254
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000255static int
256i_divmod(x, y, p_xdivy, p_xmody)
257 register intobject *x, *y;
258 long *p_xdivy, *p_xmody;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000260 long xi = x->ob_ival;
261 long yi = y->ob_ival;
262 long xdivy, xmody;
263
264 if (yi == 0) {
265 err_setstr(ZeroDivisionError, "integer division or modulo");
266 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267 }
Guido van Rossum00466951991-05-05 20:08:27 +0000268 if (yi < 0) {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000269 if (xi < 0)
270 xdivy = -xi / -yi;
271 else
272 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000273 }
274 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000275 if (xi < 0)
276 xdivy = - (-xi / yi);
277 else
278 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000279 }
280 xmody = xi - xdivy*yi;
281 if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) {
282 xmody += yi;
283 xdivy -= 1;
284 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000285 *p_xdivy = xdivy;
286 *p_xmody = xmody;
287 return 0;
288}
289
290static object *
291int_div(x, y)
292 intobject *x;
293 intobject *y;
294{
295 long d, m;
296 if (i_divmod(x, y, &d, &m) < 0)
297 return NULL;
298 return newintobject(d);
299}
300
301static object *
302int_mod(x, y)
303 intobject *x;
304 intobject *y;
305{
306 long d, m;
307 if (i_divmod(x, y, &d, &m) < 0)
308 return NULL;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000309 return newintobject(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000310}
311
312static object *
313int_divmod(x, y)
314 intobject *x;
315 intobject *y;
316{
317 object *v, *v0, *v1;
318 long d, m;
319 if (i_divmod(x, y, &d, &m) < 0)
320 return NULL;
Guido van Rossume5372401993-03-16 12:15:04 +0000321 return mkvalue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000322}
323
324static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000325int_pow(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000327 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328{
329 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000331 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000332 if (iw < 0) {
Guido van Rossum3a628451991-12-10 13:57:36 +0000333 err_setstr(ValueError, "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000334 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335 }
Guido van Rossum00466951991-05-05 20:08:27 +0000336 ix = 1;
337 while (--iw >= 0) {
338 long prev = ix;
339 ix = ix * iv;
340 if (iv == 0)
341 break; /* 0 to some power -- avoid ix / 0 */
342 if (ix / iv != prev)
Guido van Rossum3a628451991-12-10 13:57:36 +0000343 return err_ovf("integer pow()");
Guido van Rossum00466951991-05-05 20:08:27 +0000344 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 return newintobject(ix);
346}
347
348static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000349int_neg(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350 intobject *v;
351{
352 register long a, x;
353 a = v->ob_ival;
354 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000355 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000356 return err_ovf("integer negation");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 return newintobject(x);
358}
359
360static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000361int_pos(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362 intobject *v;
363{
364 INCREF(v);
365 return (object *)v;
366}
367
Guido van Rossum00466951991-05-05 20:08:27 +0000368static object *
369int_abs(v)
370 intobject *v;
371{
372 if (v->ob_ival >= 0)
373 return int_pos(v);
374 else
375 return int_neg(v);
376}
377
Guido van Rossum0bff0151991-05-14 12:05:32 +0000378static int
379int_nonzero(v)
380 intobject *v;
381{
382 return v->ob_ival != 0;
383}
384
Guido van Rossum7928cd71991-10-24 14:59:31 +0000385static object *
386int_invert(v)
387 intobject *v;
388{
389 return newintobject(~v->ob_ival);
390}
391
392static object *
393int_lshift(v, w)
394 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000395 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000396{
397 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000398 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000399 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000400 if (b < 0) {
401 err_setstr(ValueError, "negative shift count");
402 return NULL;
403 }
404 if (a == 0 || b == 0) {
405 INCREF(v);
406 return (object *) v;
407 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000408 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000409 return newintobject(0L);
410 }
411 a = (unsigned long)a << b;
412 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000413}
414
415static object *
416int_rshift(v, w)
417 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000418 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000419{
420 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000421 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000422 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000423 if (b < 0) {
424 err_setstr(ValueError, "negative shift count");
425 return NULL;
426 }
427 if (a == 0 || b == 0) {
428 INCREF(v);
429 return (object *) v;
430 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000431 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000432 if (a < 0)
433 a = -1;
434 else
435 a = 0;
436 }
437 else {
438 if (a < 0)
439 a = ~( ~(unsigned long)a >> b );
440 else
441 a = (unsigned long)a >> b;
442 }
443 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000444}
445
446static object *
447int_and(v, w)
448 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000449 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000450{
451 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000452 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000453 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000454 return newintobject(a & b);
455}
456
457static object *
458int_xor(v, w)
459 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000460 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000461{
462 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000463 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000464 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000465 return newintobject(a ^ b);
466}
467
468static object *
469int_or(v, w)
470 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000471 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000472{
473 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000474 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000475 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000476 return newintobject(a | b);
477}
478
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000479static object *
480int_int(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000481 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000482{
483 INCREF(v);
Guido van Rossum9575a441993-04-07 14:06:14 +0000484 return (object *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000485}
486
487static object *
488int_long(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000489 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000490{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000491 return newlongobject((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000492}
493
494static object *
495int_float(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000496 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000497{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000498 return newfloatobject((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000499}
500
501static object *
502int_oct(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000503 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000504{
505 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000506 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000507 if (x == 0)
508 strcpy(buf, "0");
509 else if (x > 0)
510 sprintf(buf, "0%lo", x);
511 else
512 sprintf(buf, "-0%lo", -x);
513 return newstringobject(buf);
514}
515
516static object *
517int_hex(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000518 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000519{
520 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000521 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000522 if (x >= 0)
523 sprintf(buf, "0x%lx", x);
524 else
525 sprintf(buf, "-0x%lx", -x);
526 return newstringobject(buf);
527}
528
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000529static number_methods int_as_number = {
Guido van Rossum00466951991-05-05 20:08:27 +0000530 int_add, /*nb_add*/
531 int_sub, /*nb_subtract*/
532 int_mul, /*nb_multiply*/
533 int_div, /*nb_divide*/
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000534 int_mod, /*nb_remainder*/
Guido van Rossum00466951991-05-05 20:08:27 +0000535 int_divmod, /*nb_divmod*/
536 int_pow, /*nb_power*/
537 int_neg, /*nb_negative*/
538 int_pos, /*nb_positive*/
539 int_abs, /*nb_absolute*/
Guido van Rossum0bff0151991-05-14 12:05:32 +0000540 int_nonzero, /*nb_nonzero*/
Guido van Rossum7928cd71991-10-24 14:59:31 +0000541 int_invert, /*nb_invert*/
542 int_lshift, /*nb_lshift*/
543 int_rshift, /*nb_rshift*/
544 int_and, /*nb_and*/
545 int_xor, /*nb_xor*/
546 int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000547 0, /*nb_coerce*/
548 int_int, /*nb_int*/
549 int_long, /*nb_long*/
550 int_float, /*nb_float*/
551 int_oct, /*nb_oct*/
552 int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553};
554
555typeobject Inttype = {
556 OB_HEAD_INIT(&Typetype)
557 0,
558 "int",
559 sizeof(intobject),
560 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000561 int_dealloc, /*tp_dealloc*/
562 int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563 0, /*tp_getattr*/
564 0, /*tp_setattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000565 int_compare, /*tp_compare*/
566 int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567 &int_as_number, /*tp_as_number*/
568 0, /*tp_as_sequence*/
569 0, /*tp_as_mapping*/
Guido van Rossum9575a441993-04-07 14:06:14 +0000570 int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571};