From 27ef33b36779bc19b89dd77b976e5500cfabc144 Mon Sep 17 00:00:00 2001 From: Dwayne Litzenberger Date: Sun, 14 Jul 2013 17:37:30 -0700 Subject: Py3k cleanup: Always use tp_getattro --- src/_counter.c | 76 ++++++++++++--------------------- src/_fastmath.c | 115 ++++++++++++-------------------------------------- src/block_template.c | 44 ++++++------------- src/hash_template.c | 35 ++++++--------- src/pycrypto_compat.h | 7 ++- src/stream_template.c | 32 +++++--------- src/winrand.c | 24 ++++------- 7 files changed, 102 insertions(+), 231 deletions(-) (limited to 'src') diff --git a/src/_counter.c b/src/_counter.c index 525b5cf..0863539 100644 --- a/src/_counter.c +++ b/src/_counter.c @@ -300,54 +300,46 @@ static PyMethodDef CounterBEObject_methods[] = { * so we hack it here. */ static PyObject * -#ifdef IS_PY3K CounterLEObject_getattro(PyObject *s, PyObject *attr) -#else -CounterLEObject_getattr(PyObject *s, char *name) -#endif { PCT_CounterObject *self = (PCT_CounterObject *)s; -#ifdef IS_PY3K - if (!PyUnicode_Check(attr)) - goto generic; + if (!PyString_Check(attr)) + goto generic; - if (PyUnicode_CompareWithASCIIString(attr, "carry") == 0) { -#else - if (strcmp(name, "carry") == 0) { -#endif + if (PyString_CompareWithASCIIString(attr, "carry") == 0) { return PyInt_FromLong((long)self->carry); } -#ifdef IS_PY3K generic: - return PyObject_GenericGetAttr(s, attr); +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ + return PyObject_GenericGetAttr(s, attr); #else - return Py_FindMethod(CounterLEObject_methods, (PyObject *)self, name); + if (PyString_Check(attr) < 0) { + PyErr_SetObject(PyExc_AttributeError, attr); + return NULL; + } + return Py_FindMethod(CounterLEObject_methods, (PyObject *)self, PyString_AsString(attr)); #endif } static PyObject * -#ifdef IS_PY3K CounterBEObject_getattro(PyObject *s, PyObject *attr) -#else -CounterBEObject_getattr(PyObject *s, char *name) -#endif { PCT_CounterObject *self = (PCT_CounterObject *)s; -#ifdef IS_PY3K - if (!PyUnicode_Check(attr)) - goto generic; + if (!PyString_Check(attr)) + goto generic; - if (PyUnicode_CompareWithASCIIString(attr, "carry") == 0) { -#else - if (strcmp(name, "carry") == 0) { -#endif + if (PyString_CompareWithASCIIString(attr, "carry") == 0) { return PyInt_FromLong((long)self->carry); } -#ifdef IS_PY3K generic: - return PyObject_GenericGetAttr(s, attr); +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ + return PyObject_GenericGetAttr(s, attr); #else - return Py_FindMethod(CounterBEObject_methods, (PyObject *)self, name); + if (PyString_Check(attr) < 0) { + PyErr_SetObject(PyExc_AttributeError, attr); + return NULL; + } + return Py_FindMethod(CounterBEObject_methods, (PyObject *)self, PyString_AsString(attr)); #endif } @@ -360,11 +352,7 @@ PCT_CounterLEType = { /* methods */ (destructor)CounterObject_dealloc, /* tp_dealloc */ 0, /* tp_print */ -#ifdef IS_PY3K - 0, /* tp_getattr */ -#else - CounterLEObject_getattr, /* tp_getattr */ -#endif + 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ 0, /* tp_repr */ @@ -374,20 +362,16 @@ PCT_CounterLEType = { 0, /* tp_hash */ (ternaryfunc)CounterObject_call, /* tp_call */ 0, /* tp_str */ -#ifdef IS_PY3K - CounterLEObject_getattro, /* tp_getattro */ -#else - 0, /* tp_getattro */ -#endif + CounterLEObject_getattro, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ "Counter (little endian)", /* tp_doc */ -#ifdef IS_PY3K 0, /*tp_traverse*/ 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ 0, /*tp_iter*/ 0, /*tp_iternext*/ CounterLEObject_methods, /*tp_methods*/ @@ -402,11 +386,7 @@ PCT_CounterBEType = { 0, /* tp_itemsize */ (destructor)CounterObject_dealloc, /* tp_dealloc */ 0, /* tp_print */ -#ifdef IS_PY3K - 0, /* tp_getattr */ -#else - CounterBEObject_getattr, /* tp_getattr */ -#endif + 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ 0, /* tp_repr */ @@ -416,20 +396,16 @@ PCT_CounterBEType = { 0, /* tp_hash */ (ternaryfunc)CounterObject_call, /* tp_call */ 0, /* tp_str */ -#ifdef IS_PY3K - CounterBEObject_getattro, /* tp_getattro */ -#else - 0, /* tp_getattro */ -#endif + CounterBEObject_getattro, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ "Counter (big endian)", /* tp_doc */ -#ifdef IS_PY3K 0, /*tp_traverse*/ 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ 0, /*tp_iter*/ 0, /*tp_iternext*/ CounterBEObject_methods, /*tp_methods*/ diff --git a/src/_fastmath.c b/src/_fastmath.c index 87d68ec..565d2aa 100644 --- a/src/_fastmath.c +++ b/src/_fastmath.c @@ -139,22 +139,14 @@ static PyObject *rsaKey_new (PyObject *, PyObject *); static PyObject *dsaKey_new (PyObject *, PyObject *); static void dsaKey_dealloc (dsaKey *); -#ifdef IS_PY3K static PyObject *dsaKey_getattro (dsaKey *, PyObject *); -#else -static PyObject *dsaKey_getattr (dsaKey *, char *); -#endif static PyObject *dsaKey__sign (dsaKey *, PyObject *); static PyObject *dsaKey__verify (dsaKey *, PyObject *); static PyObject *dsaKey_size (dsaKey *, PyObject *); static PyObject *dsaKey_has_private (dsaKey *, PyObject *); static void rsaKey_dealloc (rsaKey *); -#ifdef IS_PY3K static PyObject *rsaKey_getattro (rsaKey *, PyObject *); -#else -static PyObject *rsaKey_getattr (rsaKey *, char *); -#endif static PyObject *rsaKey__encrypt (rsaKey *, PyObject *); static PyObject *rsaKey__decrypt (rsaKey *, PyObject *); static PyObject *rsaKey__verify (rsaKey *, PyObject *); @@ -359,11 +351,7 @@ static PyTypeObject dsaKeyType = { 0, (destructor) dsaKey_dealloc, /* dealloc */ 0, /* print */ -#ifdef IS_PY3K 0, /* getattr */ -#else - (getattrfunc) dsaKey_getattr, /* getattr */ -#endif 0, /* setattr */ 0, /* compare */ 0, /* repr */ @@ -372,7 +360,6 @@ static PyTypeObject dsaKeyType = { 0, /* as_mapping */ 0, /* hash */ 0, /* call */ -#ifdef IS_PY3K 0, /*tp_str*/ (getattrofunc) dsaKey_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ @@ -383,6 +370,7 @@ static PyTypeObject dsaKeyType = { 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ 0, /*tp_iter*/ 0, /*tp_iternext*/ dsaKey__methods__, /*tp_methods*/ @@ -397,11 +385,7 @@ static PyTypeObject rsaKeyType = { /* methods */ (destructor) rsaKey_dealloc, /* dealloc */ 0, /* print */ -#ifdef IS_PY3K 0, /* getattr */ -#else - (getattrfunc) rsaKey_getattr, /* getattr */ -#endif 0, /* setattr */ 0, /* compare */ 0, /* repr */ @@ -410,7 +394,6 @@ static PyTypeObject rsaKeyType = { 0, /* as_mapping */ 0, /* hash */ 0, /* call */ -#ifdef IS_PY3K 0, /*tp_str*/ (getattrofunc) rsaKey_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ @@ -421,6 +404,7 @@ static PyTypeObject rsaKeyType = { 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ 0, /*tp_iter*/ 0, /*tp_iternext*/ rsaKey__methods__, /*tp_methods*/ @@ -468,43 +452,19 @@ dsaKey_dealloc (dsaKey * key) } static PyObject * -#ifdef IS_PY3K dsaKey_getattro (dsaKey * key, PyObject *attr) -#else -dsaKey_getattr (dsaKey * key, char *attr) -#endif { -#ifdef IS_PY3K - if (!PyUnicode_Check(attr)) + if (!PyString_Check(attr)) goto generic; - if (PyUnicode_CompareWithASCIIString(attr,"y") == 0) -#else - if (strcmp (attr, "y") == 0) -#endif + if (PyString_CompareWithASCIIString(attr,"y") == 0) return mpzToLongObj (key->y); -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "g") == 0) -#else - else if (strcmp (attr, "g") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "g") == 0) return mpzToLongObj (key->g); -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "p") == 0) -#else - else if (strcmp (attr, "p") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "p") == 0) return mpzToLongObj (key->p); -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "q") == 0) -#else - else if (strcmp (attr, "q") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "q") == 0) return mpzToLongObj (key->q); -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "x") == 0) -#else - else if (strcmp (attr, "x") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "x") == 0) { if (mpz_size (key->x) == 0) { @@ -515,11 +475,15 @@ dsaKey_getattr (dsaKey * key, char *attr) return mpzToLongObj (key->x); } else -#ifdef IS_PY3K generic: +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ return PyObject_GenericGetAttr((PyObject *) key, attr); #else - return Py_FindMethod (dsaKey__methods__, (PyObject *) key, attr); + if (PyString_Check(attr) < 0) { + PyErr_SetObject(PyExc_AttributeError, attr); + return NULL; + } + return Py_FindMethod(dsaKey__methods__, (PyObject *)key, PyString_AsString(attr)); #endif } @@ -734,31 +698,15 @@ rsaKey_dealloc (rsaKey * key) } static PyObject * -#ifdef IS_PY3K rsaKey_getattro (rsaKey * key, PyObject *attr) -#else -rsaKey_getattr (rsaKey * key, char *attr) -#endif { -#ifdef IS_PY3K - if (!PyUnicode_Check(attr)) + if (!PyString_Check(attr)) goto generic; - if (PyUnicode_CompareWithASCIIString(attr, "n") == 0) -#else - if (strcmp (attr, "n") == 0) -#endif + if (PyString_CompareWithASCIIString(attr, "n") == 0) return mpzToLongObj (key->n); -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "e") == 0) -#else - else if (strcmp (attr, "e") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "e") == 0) return mpzToLongObj (key->e); -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "d") == 0) -#else - else if (strcmp (attr, "d") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "d") == 0) { if (mpz_size (key->d) == 0) { @@ -768,11 +716,7 @@ rsaKey_getattr (rsaKey * key, char *attr) } return mpzToLongObj (key->d); } -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "p") == 0) -#else - else if (strcmp (attr, "p") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "p") == 0) { if (mpz_size (key->p) == 0) { @@ -782,11 +726,7 @@ rsaKey_getattr (rsaKey * key, char *attr) } return mpzToLongObj (key->p); } -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "q") == 0) -#else - else if (strcmp (attr, "q") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "q") == 0) { if (mpz_size (key->q) == 0) { @@ -796,11 +736,7 @@ rsaKey_getattr (rsaKey * key, char *attr) } return mpzToLongObj (key->q); } -#ifdef IS_PY3K - else if (PyUnicode_CompareWithASCIIString(attr, "u") == 0) -#else - else if (strcmp (attr, "u") == 0) -#endif + else if (PyString_CompareWithASCIIString(attr, "u") == 0) { if (mpz_size (key->u) == 0) { @@ -811,12 +747,15 @@ rsaKey_getattr (rsaKey * key, char *attr) return mpzToLongObj (key->u); } else -#ifdef IS_PY3K generic: +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ return PyObject_GenericGetAttr((PyObject *) key, attr); #else - return Py_FindMethod (rsaKey__methods__, - (PyObject *) key, attr); + if (PyString_Check(attr) < 0) { + PyErr_SetObject(PyExc_AttributeError, attr); + return NULL; + } + return Py_FindMethod(rsaKey__methods__, (PyObject *)key, PyString_AsString(attr)); #endif } diff --git a/src/block_template.c b/src/block_template.c index fe9f778..e5e9d16 100644 --- a/src/block_template.c +++ b/src/block_template.c @@ -629,54 +629,38 @@ ALGsetattr(PyObject *ptr, char *name, PyObject *v) } static PyObject * -#ifdef IS_PY3K ALGgetattro(PyObject *s, PyObject *attr) -#else -ALGgetattr(PyObject *s, char *name) -#endif { ALGobject *self = (ALGobject*)s; -#ifdef IS_PY3K - if (!PyUnicode_Check(attr)) + if (!PyString_Check(attr)) goto generic; - if (PyUnicode_CompareWithASCIIString(attr, "IV") == 0) -#else - if (strcmp(name, "IV") == 0) -#endif + if (PyString_CompareWithASCIIString(attr, "IV") == 0) { return(PyBytes_FromStringAndSize((char *) self->IV, BLOCK_SIZE)); } -#ifdef IS_PY3K - if (PyUnicode_CompareWithASCIIString(attr, "mode") == 0) -#else - if (strcmp(name, "mode") == 0) -#endif + if (PyString_CompareWithASCIIString(attr, "mode") == 0) { return(PyInt_FromLong((long)(self->mode))); } -#ifdef IS_PY3K - if (PyUnicode_CompareWithASCIIString(attr, "block_size") == 0) -#else - if (strcmp(name, "block_size") == 0) -#endif + if (PyString_CompareWithASCIIString(attr, "block_size") == 0) { return PyInt_FromLong(BLOCK_SIZE); } -#ifdef IS_PY3K - if (PyUnicode_CompareWithASCIIString(attr, "key_size") == 0) -#else - if (strcmp(name, "key_size") == 0) -#endif + if (PyString_CompareWithASCIIString(attr, "key_size") == 0) { return PyInt_FromLong(KEY_SIZE); } -#ifdef IS_PY3K generic: +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ return PyObject_GenericGetAttr(s, attr); #else - return Py_FindMethod(ALGmethods, (PyObject *) self, name); + if (PyString_Check(attr) < 0) { + PyErr_SetObject(PyExc_AttributeError, attr); + return NULL; + } + return Py_FindMethod(ALGmethods, (PyObject *)self, PyString_AsString(attr)); #endif } @@ -697,16 +681,11 @@ static PyTypeObject ALGtype = /* methods */ (destructor) ALGdealloc, /*tp_dealloc*/ 0, /*tp_print*/ -#ifdef IS_PY3K 0, /*tp_getattr*/ -#else - ALGgetattr, /*tp_getattr*/ -#endif ALGsetattr, /*tp_setattr*/ 0, /*tp_compare*/ (reprfunc) 0, /*tp_repr*/ 0, /*tp_as_number*/ -#ifdef IS_PY3K 0, /*tp_as_sequence */ 0, /*tp_as_mapping */ 0, /*tp_hash*/ @@ -721,6 +700,7 @@ static PyTypeObject ALGtype = 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ 0, /*tp_iter*/ 0, /*tp_iternext*/ ALGmethods, /*tp_methods*/ diff --git a/src/hash_template.c b/src/hash_template.c index 2572577..f3f116d 100644 --- a/src/hash_template.c +++ b/src/hash_template.c @@ -184,32 +184,25 @@ static PyMethodDef ALG_methods[] = { }; static PyObject * -#ifdef IS_PY3K ALG_getattro(PyObject *self, PyObject *attr) -#else -ALG_getattr(PyObject *self, char *name) -#endif { -#ifdef IS_PY3K - if (!PyUnicode_Check(attr)) + if (!PyString_Check(attr)) goto generic; - - if (PyUnicode_CompareWithASCIIString(attr, "digest_size")==0) - return PyInt_FromLong(DIGEST_SIZE); - if (PyUnicode_CompareWithASCIIString(attr, "name")==0) - return PyUnicode_FromString(_MODULE_STRING); /* we should try to be compatible with hashlib here */ -#else - if (strcmp(name, "digest_size")==0) + + if (PyString_CompareWithASCIIString(attr, "digest_size")==0) return PyInt_FromLong(DIGEST_SIZE); - if (strcmp(name, "name")==0) + if (PyString_CompareWithASCIIString(attr, "name")==0) return PyString_FromString(_MODULE_STRING); /* we should try to be compatible with hashlib here */ -#endif -#ifdef IS_PY3K generic: +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ return PyObject_GenericGetAttr(self, attr); #else - return Py_FindMethod(ALG_methods, self, name); + if (PyString_Check(attr) < 0) { + PyErr_SetObject(PyExc_AttributeError, attr); + return NULL; + } + return Py_FindMethod(ALG_methods, (PyObject *)self, PyString_AsString(attr)); #endif } @@ -221,16 +214,11 @@ static PyTypeObject ALGtype = { /* methods */ (destructor) ALG_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ -#ifdef IS_PY3K - 0, /*tp_getattr*/ -#else - ALG_getattr, /*tp_getattr*/ -#endif + 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ -#ifdef IS_PY3K 0, /*tp_as_sequence */ 0, /*tp_as_mapping */ 0, /*tp_hash*/ @@ -245,6 +233,7 @@ static PyTypeObject ALGtype = { 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ 0, /*tp_iter*/ 0, /*tp_iternext*/ ALG_methods, /*tp_methods*/ diff --git a/src/pycrypto_compat.h b/src/pycrypto_compat.h index a5a713b..3f890b4 100644 --- a/src/pycrypto_compat.h +++ b/src/pycrypto_compat.h @@ -34,6 +34,9 @@ # define PyInt_AS_LONG PyLong_AS_LONG # define PyInt_CheckExact PyLong_CheckExact # define PyInt_FromLong PyLong_FromLong +# define PyString_Check PyUnicode_Check +# define PyString_CompareWithASCIIString PyUnicode_CompareWithASCIIString +# define PyString_FromString PyUnicode_FromString # define staticforward static #else # define PyBytes_GET_SIZE PyString_GET_SIZE @@ -43,16 +46,18 @@ # define PyBytes_Size PyString_Size # define PyBytes_AsString PyString_AsString # define PyBytesObject PyStringObject +# define PyString_CompareWithASCIIString(o,s) \ + (PyString_Check(o) ? strcmp(PyString_AsString(o),(s)) : -1) /* NB: only compares up to the first NUL byte */ # if PY_MINOR_VERSION <= 5 /* Python 2.5 and earlier */ # define Py_TYPE(v) ((v)->ob_type) # define PyLong_MASK MASK # define PyLong_SHIFT SHIFT -# define PyUnicode_FromString PyString_FromString # define PyVarObject_HEAD_INIT(a,b) PyObject_HEAD_INIT(a) 0, # endif # if PY_MINOR_VERSION <= 1 /* Python 2.1 only */ # define METH_O METH_OLDARGS /* METH_O is a subset of what METH_OLDARGS provides */ # define PyInt_CheckExact PyInt_Check +# define PyString_CheckExact PyString_Check # define PyType_Ready(t) (((t)->ob_type = &PyType_Type) ? 0 : 0) # endif #endif diff --git a/src/stream_template.c b/src/stream_template.c index 5ef1f1e..cb11a0c 100644 --- a/src/stream_template.c +++ b/src/stream_template.c @@ -189,36 +189,28 @@ static PyMethodDef ALGmethods[] = }; static PyObject * -#ifdef IS_PY3K ALGgetattro(PyObject *self, PyObject *attr) -#else -ALGgetattr(PyObject *self, char *name) -#endif { -#ifdef IS_PY3K - if (!PyUnicode_Check(attr)) + if (!PyString_Check(attr)) goto generic; - if (PyUnicode_CompareWithASCIIString(attr, "block_size") == 0) -#else - if (strcmp(name, "block_size") == 0) -#endif + if (PyString_CompareWithASCIIString(attr, "block_size") == 0) { return PyInt_FromLong(BLOCK_SIZE); } -#ifdef IS_PY3K - if (PyUnicode_CompareWithASCIIString(attr, "key_size") == 0) -#else - if (strcmp(name, "key_size") == 0) -#endif + if (PyString_CompareWithASCIIString(attr, "key_size") == 0) { return PyInt_FromLong(KEY_SIZE); } -#ifdef IS_PY3K generic: +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ return PyObject_GenericGetAttr(self, attr); #else - return Py_FindMethod(ALGmethods, self, name); + if (PyString_Check(attr) < 0) { + PyErr_SetObject(PyExc_AttributeError, attr); + return NULL; + } + return Py_FindMethod(ALGmethods, (PyObject *)self, PyString_AsString(attr)); #endif } @@ -240,16 +232,11 @@ static PyTypeObject ALGtype = /* methods */ (destructor) ALGdealloc, /*tp_dealloc*/ 0, /*tp_print*/ -#ifdef IS_PY3K 0, /*tp_getattr*/ -#else - ALGgetattr, /*tp_getattr*/ -#endif 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ -#ifdef IS_PY3K 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash*/ @@ -264,6 +251,7 @@ static PyTypeObject ALGtype = 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ 0, /*tp_iter*/ 0, /*tp_iternext*/ ALGmethods, /*tp_methods*/ diff --git a/src/winrand.c b/src/winrand.c index 060e917..d06b242 100644 --- a/src/winrand.c +++ b/src/winrand.c @@ -191,11 +191,7 @@ static PyMethodDef WR_mod_methods[] = { }; static PyObject * -#ifdef IS_PY3K WRgetattro(PyObject *s, PyObject *attr) -#else -WRgetattr(PyObject *s, char *name) -#endif { WRobject *self = (WRobject*)s; if (! is_WRobject(self)) { @@ -203,19 +199,19 @@ WRgetattr(PyObject *s, char *name) "WinRandom trying to getattr with non-WinRandom object"); return NULL; } -#ifdef IS_PY3K - if (!PyUnicode_Check(attr)) + if (!PyString_Check(attr)) goto generic; - if (PyUnicode_CompareWithASCIIString(attr, "hcp") == 0) -#else - if (strcmp(name, "hcp") == 0) -#endif + if (PyString_CompareWithASCIIString(attr, "hcp") == 0) return PyInt_FromLong((long) self->hcp); -#ifdef IS_PY3K generic: +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ return PyObject_GenericGetAttr(s, attr); #else - return Py_FindMethod(WRmethods, (PyObject *) self, name); + if (PyString_Check(attr) < 0) { + PyErr_SetObject(PyExc_AttributeError, attr); + return NULL; + } + return Py_FindMethod(WRmethods, (PyObject *)self, PyString_AsString(attr)); #endif } @@ -228,9 +224,6 @@ static PyTypeObject WRtype = /* methods */ (destructor) WRdealloc, /*tp_dealloc*/ 0, /*tp_print*/ -#ifndef IS_PY3K - WRgetattr, /*tp_getattr*/ -#else 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ @@ -250,6 +243,7 @@ static PyTypeObject WRtype = 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ +#if PYTHON_API_VERSION >= 1011 /* Python 2.2 and later */ 0, /*tp_iter*/ 0, /*tp_iternext*/ WRmethods, /*tp_methods*/ -- cgit v1.2.1