@@ -9,6 +9,7 @@ PyObject * THPStorage_(newObject)(THStorage *ptr)
9
9
// TODO: error checking
10
10
PyObject *args = PyTuple_New (0 );
11
11
PyObject *kwargs = Py_BuildValue (" {s:N}" , " cdata" , PyLong_FromVoidPtr (ptr));
12
+
12
13
PyObject *instance = PyObject_Call (THPStorageClass, args, kwargs);
13
14
Py_DECREF (args);
14
15
Py_DECREF (kwargs);
@@ -30,17 +31,17 @@ static PyObject * THPStorage_(pynew)(PyTypeObject *type, PyObject *args, PyObjec
30
31
{
31
32
HANDLE_TH_ERRORS
32
33
static const char *keywords[] = {" cdata" , NULL };
33
- PyObject *number_arg = NULL ;
34
- if (!PyArg_ParseTupleAndKeywords (args, kwargs, " |O!" , (char **)keywords, &PyLong_Type, &number_arg))
34
+ void * number_arg = NULL ;
35
+ if (!PyArg_ParseTupleAndKeywords (args, kwargs, " |O&" , (char **)keywords,
36
+ THPUtils_getLong, &number_arg))
35
37
return NULL ;
36
-
37
38
THPStorage *self = (THPStorage *)type->tp_alloc (type, 0 );
38
39
if (self != NULL ) {
39
40
if (kwargs) {
40
- self->cdata = (THStorage*)PyLong_AsVoidPtr ( number_arg) ;
41
+ self->cdata = (THStorage*)number_arg;
41
42
THStorage_ (retain)(self->cdata );
42
43
} else if (/* !kwargs && */ number_arg) {
43
- self->cdata = THStorage_ (newWithSize)(PyLong_AsLong (number_arg) );
44
+ self->cdata = THStorage_ (newWithSize)(( long ) number_arg );
44
45
} else {
45
46
self->cdata = THStorage_ (new )();
46
47
}
@@ -66,8 +67,9 @@ static PyObject * THPStorage_(get)(THPStorage *self, PyObject *index)
66
67
{
67
68
HANDLE_TH_ERRORS
68
69
/* Integer index */
69
- if (PyLong_Check (index )) {
70
- long nindex = PyLong_AsLong (index );
70
+ long nindex;
71
+ if ((PyLong_Check (index ) || PyInt_Check (index ))
72
+ && THPUtils_getLong (index , &nindex) == 1 ) {
71
73
if (nindex < 0 )
72
74
nindex += THStorage_ (size)(self->cdata );
73
75
#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE)
@@ -89,7 +91,11 @@ static PyObject * THPStorage_(get)(THPStorage *self, PyObject *index)
89
91
THStorage *new_storage = THStorage_ (newWithData)(new_data, slicelength);
90
92
return THPStorage_ (newObject)(new_storage);
91
93
}
92
- PyErr_SetString (PyExc_RuntimeError, " Only indexing with integers and slices supported" );
94
+ char err_string[512 ];
95
+ snprintf (err_string, 512 ,
96
+ " %s %s" , " Only indexing with integers and slices supported, but got type: " ,
97
+ index ->ob_type ->tp_name );
98
+ PyErr_SetString (PyExc_RuntimeError, err_string);
93
99
return NULL ;
94
100
END_HANDLE_TH_ERRORS
95
101
}
@@ -101,8 +107,10 @@ static int THPStorage_(set)(THPStorage *self, PyObject *index, PyObject *value)
101
107
if (!THPUtils_ (parseReal)(value, &rvalue))
102
108
return -1 ;
103
109
104
- if (PyLong_Check (index )) {
105
- THStorage_ (set)(self->cdata , PyLong_AsSize_t (index ), rvalue);
110
+ long nindex;
111
+ if ((PyLong_Check (index ) || PyInt_Check (index ))
112
+ && THPUtils_getLong (index , &nindex) == 1 ) {
113
+ THStorage_ (set)(self->cdata , nindex, rvalue);
106
114
return 0 ;
107
115
} else if (PySlice_Check (index )) {
108
116
Py_ssize_t start, stop, len;
@@ -114,7 +122,11 @@ static int THPStorage_(set)(THPStorage *self, PyObject *index, PyObject *value)
114
122
THStorage_ (set)(self->cdata , start, rvalue);
115
123
return 0 ;
116
124
}
117
- PyErr_SetString (PyExc_RuntimeError, " Only indexing with integers and slices supported at the moment" );
125
+ char err_string[512 ];
126
+ snprintf (err_string, 512 , " %s %s" ,
127
+ " Only indexing with integers and slices supported, but got type: " ,
128
+ index ->ob_type ->tp_name );
129
+ PyErr_SetString (PyExc_RuntimeError, err_string);
118
130
return -1 ;
119
131
END_HANDLE_TH_ERRORS_RET (-1 )
120
132
}
0 commit comments