-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintobject.c
More file actions
48 lines (38 loc) · 1 KB
/
intobject.c
File metadata and controls
48 lines (38 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "intobject.h"
PyTypeObject PyInt_Type = {
PyObject_HEAD_INIT(&PyType_Type),
"int",
PyInt_Print,
PyInt_Add,
PyInt_Hash,
};
PyObject* PyInt_Create(int value){
PyIntObject* object = (PyIntObject*)malloc(sizeof(PyIntObject));
if (object == NULL){
printf("int malloc is null");
return NULL;
}
object->ob_refcnt = 1;
object->ob_type = &PyInt_Type;
object->ob_ival = value;
return (PyObject*)object;
}
void PyInt_Print(PyObject*num)
{
PyIntObject* i= (PyIntObject*)num;
printf("%d\n", i->ob_ival);
}
PyObject* PyInt_Add(PyObject* num1, PyObject* num2){
PyIntObject* result = (PyIntObject*)PyInt_Create(0);
if(result == NULL){
printf("no enough memory");
return NULL;
}
PyIntObject* n1 = (PyIntObject*)num1;
PyIntObject* n2 = (PyIntObject*)num2;
result->ob_ival = n1->ob_ival + n2->ob_ival;
return (PyObject*)result;
}
long PyInt_Hash(PyObject* o){
return (long)(((PyIntObject*)o)->ob_ival);
}