forked from davisp/python-spidermonkey
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhashcobj.c
More file actions
143 lines (122 loc) · 3.59 KB
/
Copy pathhashcobj.c
File metadata and controls
143 lines (122 loc) · 3.59 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2009 Paul J. Davis <paul.joseph.davis@gmail.com>
*
* This file is part of the python-spidermonkey package released
* under the MIT license.
*
*/
#include "spidermonkey.h"
PyObject*
HashCObj_FromVoidPtr(void *cobj)
{
HashCObj* self = NULL;
self = PyObject_NEW(HashCObj, HashCObjType);
if(self == NULL) goto error;
self->cobj = cobj;
goto success;
error:
success:
return (PyObject*) self;
}
void*
HashCObj_AsVoidPtr(PyObject* self)
{
return ((HashCObj*)self)->cobj;
}
int
HashCObj_cmp(PyObject* self, PyObject* other)
{
int ret = -1;
if(!PyObject_TypeCheck(self, HashCObjType))
{
PyErr_SetString(PyExc_ValueError, "Invalid comparison object.");
goto error;
}
if(!PyObject_TypeCheck(other, HashCObjType))
{
PyErr_SetString(PyExc_ValueError, "Invalid comparison object 2.");
goto error;
}
if(((HashCObj*)self)->cobj == ((HashCObj*)other)->cobj)
{
ret = 0;
}
else
{
ret = 1;
}
goto success;
error:
success:
return ret;
}
PyObject*
HashCObj_rich_cmp(PyObject* self, PyObject* other, int op)
{
PyObject* ret = NULL;
if(op != Py_EQ && op != Py_NE) return Py_NotImplemented;
if(!PyObject_TypeCheck(self, HashCObjType))
{
PyErr_SetString(PyExc_ValueError, "Invalid comparison object.");
goto error;
}
if(!PyObject_TypeCheck(other, HashCObjType))
{
PyErr_SetString(PyExc_ValueError, "Invalid comparison object 2.");
goto error;
}
if(((HashCObj*)self)->cobj == ((HashCObj*)other)->cobj)
{
ret = (op == Py_EQ)? Py_True : Py_False;
}
else
{
ret = (op == Py_EQ)? Py_False : Py_True;
}
error:
success:
Py_XINCREF(ret);
return ret;
}
PyObject*
HashCObj_repr(PyObject* self)
{
return PyBytes_FromFormat("<%s Ptr: %p>",
Py_TYPE(self)->tp_name,
((HashCObj*)self)->cobj);
}
long
HashCObj_hash(HashCObj* self)
{
return _Py_HashPointer(self->cobj);
}
PyTypeObject _HashCObjType = {
PyVarObject_HEAD_INIT(NULL, 0)
"spidermonkey._HashCObj", /*tp_name*/
sizeof(HashCObj), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
#if PY_MAJOR_VERSION >= 3
0, /*tp_compare*/
#else
(cmpfunc)HashCObj_cmp, /*tp_compare*/
#endif
(reprfunc)HashCObj_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)HashCObj_hash, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
0, /*tp_flags*/
"Internal hashing object.", /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
(richcmpfunc)HashCObj_rich_cmp, /*tp_richcompare*/
};