@@ -521,9 +521,6 @@ static PyTypeObject pytrap_UnirecTime = {
521521 0 , /* tp_setattro */
522522 0 , /* tp_as_buffer */
523523 Py_TPFLAGS_DEFAULT |
524- #if PY_MAJOR_VERSION < 3
525- Py_TPFLAGS_CHECKTYPES |
526- #endif
527524 Py_TPFLAGS_BASETYPE , /* tp_flags */
528525 "UnirecTime(int(seconds), [int(miliseconds)])\n"
529526 "UnirecTime(double(secs_and_msecs))\n"
@@ -810,11 +807,7 @@ UnirecTemplate_getByName(pytrap_unirectemplate *self, PyObject *args, PyObject *
810807 return NULL ;
811808 }
812809
813- #if PY_MAJOR_VERSION >= 3
814810 if (!PyUnicode_Check (field_name ))
815- #else
816- if (!PyUnicode_Check (field_name ) && !PyString_Check (field_name ))
817- #endif
818811 {
819812 PyErr_SetString (PyExc_TypeError , "Argument field_name must be string." );
820813 return NULL ;
@@ -1424,12 +1417,7 @@ UnirecTemplate_set(pytrap_unirectemplate *self, PyObject *args, PyObject *keywds
14241417 return NULL ;
14251418 }
14261419
1427- #if PY_MAJOR_VERSION >= 3
1428- if (!PyUnicode_Check (field_name ))
1429- #else
1430- if (!PyUnicode_Check (field_name ) && !PyString_Check (field_name ))
1431- #endif
1432- {
1420+ if (!PyUnicode_Check (field_name )) {
14331421 PyErr_SetString (PyExc_TypeError , "Argument field_name must be string." );
14341422 return NULL ;
14351423 }
@@ -1447,26 +1435,27 @@ UnirecTemplate_getFieldsDict_local(pytrap_unirectemplate *self, char byId)
14471435{
14481436 PyObject * key , * num ;
14491437 int i ;
1438+ int result ;
14501439 PyObject * d = PyDict_New ();
14511440 if (d != NULL ) {
14521441 for (i = 0 ; i < self -> urtmplt -> count ; i ++ ) {
1453- #if PY_MAJOR_VERSION >= 3
14541442 key = PyUnicode_FromString (ur_get_name (self -> urtmplt -> ids [i ]));
1455- #else
1456- key = PyString_FromString (ur_get_name (self -> urtmplt -> ids [i ]));
1457- #endif
14581443 num = PyLong_FromLong (self -> urtmplt -> ids [i ]);
14591444 if (byId ) {
1460- PyDict_SetItem (d , num , key );
1445+ result = PyDict_SetItem (d , num , key );
14611446 } else {
1462- PyDict_SetItem (d , key , num );
1447+ result = PyDict_SetItem (d , key , num );
14631448 }
1464- Py_DECREF (num );
14651449 Py_DECREF (key );
1450+ Py_DECREF (num );
1451+ if (result == -1 ) {
1452+ fprintf (stderr , "failed to set item dict.\n" );
1453+ Py_RETURN_NONE ;
1454+ }
14661455 }
14671456 return d ;
14681457 }
1469- Py_DECREF (d );
1458+ Py_XDECREF (d );
14701459 Py_RETURN_NONE ;
14711460}
14721461
@@ -1764,21 +1753,12 @@ UnirecTemplate_getDict(pytrap_unirectemplate *self)
17641753static PyObject *
17651754UnirecTemplate_getFieldType (pytrap_unirectemplate * self , PyObject * args )
17661755{
1767- PyObject * name ;
1756+ PyObject * name , * result = NULL ;
17681757
1769- if (!PyArg_ParseTuple (args , "O" , & name )) {
1758+ if (!PyArg_ParseTuple (args , "O!" , & PyUnicode_Type , & name )) {
17701759 return NULL ;
17711760 }
17721761
1773- #if PY_MAJOR_VERSION >= 3
1774- if (!PyUnicode_Check (name ))
1775- #else
1776- if (!PyUnicode_Check (name ) && !PyString_Check (name ))
1777- #endif
1778- {
1779- PyErr_SetString (PyExc_TypeError , "Argument field_name must be string." );
1780- return NULL ;
1781- }
17821762 int32_t field_id = UnirecTemplate_get_field_id (self , name );
17831763
17841764 switch (ur_get_type (field_id )) {
@@ -1791,27 +1771,26 @@ UnirecTemplate_getFieldType(pytrap_unirectemplate *self, PyObject *args)
17911771 case UR_TYPE_INT32 :
17921772 case UR_TYPE_INT64 :
17931773 case UR_TYPE_CHAR :
1794- return (PyObject * ) & PyLong_Type ;
1774+ result = (PyObject * ) & PyLong_Type ;
17951775 break ;
17961776 case UR_TYPE_FLOAT :
17971777 case UR_TYPE_DOUBLE :
1798- return (PyObject * ) & PyFloat_Type ;
1778+ result = (PyObject * ) & PyFloat_Type ;
17991779 break ;
18001780 case UR_TYPE_IP :
1801- return (PyObject * ) & pytrap_UnirecIPAddr ;
1781+ result = (PyObject * ) & pytrap_UnirecIPAddr ;
1782+ break ;
18021783 case UR_TYPE_MAC :
1803- return (PyObject * ) & pytrap_UnirecMACAddr ;
1784+ result = (PyObject * ) & pytrap_UnirecMACAddr ;
1785+ break ;
18041786 case UR_TYPE_TIME :
1805- return (PyObject * ) & pytrap_UnirecTime ;
1787+ result = (PyObject * ) & pytrap_UnirecTime ;
1788+ break ;
18061789 case UR_TYPE_STRING :
1807- #if PY_MAJOR_VERSION >= 3
1808- return (PyObject * ) & PyUnicode_Type ;
1809- #else
1810- return (PyObject * ) & PyString_Type ;
1811- #endif
1790+ result = (PyObject * ) & PyUnicode_Type ;
18121791 break ;
18131792 case UR_TYPE_BYTES :
1814- return (PyObject * ) & PyByteArray_Type ;
1793+ result = (PyObject * ) & PyByteArray_Type ;
18151794 break ;
18161795 case UR_TYPE_A_UINT8 :
18171796 case UR_TYPE_A_INT8 :
@@ -1826,12 +1805,17 @@ UnirecTemplate_getFieldType(pytrap_unirectemplate *self, PyObject *args)
18261805 case UR_TYPE_A_IP :
18271806 case UR_TYPE_A_MAC :
18281807 case UR_TYPE_A_TIME :
1829- return (PyObject * ) & PyList_Type ;
1808+ result = (PyObject * ) & PyList_Type ;
18301809 break ;
18311810 default :
18321811 PyErr_SetString (PyExc_NotImplementedError , "Unknown UniRec field type." );
18331812 return NULL ;
18341813 } // case (field type)
1814+
1815+ if (result != NULL ) {
1816+ Py_INCREF (result );
1817+ return result ;
1818+ }
18351819 Py_RETURN_NONE ;
18361820}
18371821
@@ -2102,16 +2086,12 @@ UnirecTemplate_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21022086
21032087static void UnirecTemplate_dealloc (pytrap_unirectemplate * self )
21042088{
2105- if (self -> urdict ) {
2106- Py_DECREF (self -> urdict );
2107- }
2108- if (self -> fields_dict ) {
2109- Py_DECREF (self -> fields_dict );
2110- }
2089+ Py_XDECREF (self -> urdict );
2090+ Py_XDECREF (self -> fields_dict );
21112091 if (self -> urtmplt ) {
21122092 ur_free_template (self -> urtmplt );
21132093 }
2114- Py_XDECREF (self -> data_obj ); // Allow to free the original data object
2094+ Py_XDECREF (self -> data_obj );
21152095 Py_TYPE (self )-> tp_free ((PyObject * ) self );
21162096}
21172097
@@ -2120,11 +2100,7 @@ UnirecTemplate_str(pytrap_unirectemplate *self)
21202100{
21212101 char * s = ur_template_string_delimiter (self -> urtmplt , ',' );
21222102 PyObject * result ;
2123- #if PY_MAJOR_VERSION >= 3
21242103 result = PyUnicode_FromFormat ("(%s)" , s );
2125- #else
2126- result = PyString_FromFormat ("(%s)" , s );
2127- #endif
21282104 free (s );
21292105 return result ;
21302106}
@@ -2171,11 +2147,7 @@ UnirecTemplate_next(pytrap_unirectemplate *self)
21712147 PyObject * result ;
21722148
21732149 if (self -> iter_index < self -> field_count ) {
2174- #if PY_MAJOR_VERSION >= 3
21752150 name = PyUnicode_FromString (ur_get_name (self -> urtmplt -> ids [self -> iter_index ]));
2176- #else
2177- name = PyString_FromString (ur_get_name (self -> urtmplt -> ids [self -> iter_index ]));
2178- #endif
21792151 value = UnirecTemplate_get_local (self , self -> data , self -> urtmplt -> ids [self -> iter_index ]);
21802152 self -> iter_index ++ ;
21812153 result = Py_BuildValue ("(OO)" , name , value );
@@ -2268,7 +2240,6 @@ init_unirectemplate(PyObject *m)
22682240 PyModule_AddObject (m , "UnirecTime" , (PyObject * ) & pytrap_UnirecTime );
22692241
22702242 /* Add IPAddr */
2271- //pytrap_UnirecTemplate.tp_new = PyType_GenericNew;
22722243 if (PyType_Ready (& pytrap_UnirecIPAddr ) < 0 ) {
22732244 return EXIT_FAILURE ;
22742245 }
@@ -2283,7 +2254,6 @@ init_unirectemplate(PyObject *m)
22832254 PyModule_AddObject (m , "UnirecIPAddrRange" , (PyObject * ) & pytrap_UnirecIPAddrRange );
22842255
22852256 /* Add MACAddr */
2286- //pytrap_UnirecTemplate.tp_new = PyType_GenericNew;
22872257 if (PyType_Ready (& pytrap_UnirecMACAddr ) < 0 ) {
22882258 return EXIT_FAILURE ;
22892259 }
0 commit comments