Changeset 579


Ignore:
Timestamp:
Nov 21, 2015, 5:37:44 PM (4 years ago)
Author:
cito
Message:

members has been deprecated a long time ago

The members (and methods) attributes are deprecated in Python 2
and not supported in Python 3 any more. Therefore we need to create a
custom dir method to proclaim the members implemented via getattr.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/module/pgmodule.c

    r578 r579  
    898898}
    899899
     900/* get the list of large object attributes */
     901static PyObject *
     902largeDir(largeObject *self) {
     903        PyObject *attrs;
     904
     905        attrs = PyObject_Dir(PyObject_Type((PyObject *)self));
     906        PyObject_CallMethod(attrs, "extend", "[sss]",
     907                "oid", "pgcnx", "error");
     908
     909        return attrs;
     910}
     911
    900912/* large object methods */
    901913static struct PyMethodDef largeMethods[] = {
     914        {"__dir__", (PyCFunction) largeDir,  METH_NOARGS, NULL},
    902915        {"open", (PyCFunction) largeOpen, METH_VARARGS, largeOpen__doc__},
    903916        {"close", (PyCFunction) largeClose, METH_VARARGS, largeClose__doc__},
     
    912925};
    913926
    914 /* get attribute */
     927/* gets large object attributes */
    915928static PyObject *
    916929largeGetAttr(largeObject *self, PyObject *nameobj)
     
    946959        if (!strcmp(name, "error"))
    947960                return PyStr_FromString(PQerrorMessage(self->pgcnx->cnx));
    948 
    949         /* attributes list */
    950         if (!strcmp(name, "__members__"))
    951         {
    952                 PyObject *list = PyList_New(3);
    953 
    954                 if (list)
    955                 {
    956                         PyList_SET_ITEM(list, 0, PyStr_FromString("oid"));
    957                         PyList_SET_ITEM(list, 1, PyStr_FromString("pgcnx"));
    958                         PyList_SET_ITEM(list, 2, PyStr_FromString("error"));
    959                 }
    960 
    961                 return list;
    962         }
    963961
    964962        /* module name */
     
    989987static char large__doc__[] = "PostgreSQL large object";
    990988
    991 /* object type definition */
     989/* large object type definition */
    992990static PyTypeObject largeType = {
    993991        PyVarObject_HEAD_INIT(NULL, 0)
     
    21272125}
    21282126
     2127/* get the list of connection attributes */
     2128static PyObject *
     2129connDir(connObject *self) {
     2130        PyObject *attrs;
     2131
     2132        attrs = PyObject_Dir(PyObject_Type((PyObject *)self));
     2133        PyObject_CallMethod(attrs, "extend", "[ssssssssss]",
     2134                "host", "port", "db", "options", "tty", "error", "status", "user",
     2135                "protocol_version", "server_version");
     2136
     2137        return attrs;
     2138}
     2139
    21292140/* connection object methods */
    21302141static struct PyMethodDef connMethods[] = {
     2142        {"__dir__", (PyCFunction) connDir,  METH_NOARGS, NULL},
     2143
    21312144        {"source", (PyCFunction) connSource, METH_VARARGS, connSource__doc__},
    21322145        {"query", (PyCFunction) connQuery, METH_VARARGS, connQuery__doc__},
     
    21602173
    21612174#ifdef DIRECT_ACCESS
    2162         {"putline", (PyCFunction) connPutLine, 1, connPutLine__doc__},
    2163         {"getline", (PyCFunction) connGetLine, 1, connGetLine__doc__},
    2164         {"endcopy", (PyCFunction) connEndCopy, 1, connEndCopy__doc__},
     2175        {"putline", (PyCFunction) connPutLine, METH_VARARGS, connPutLine__doc__},
     2176        {"getline", (PyCFunction) connGetLine, METH_VARARGS, connGetLine__doc__},
     2177        {"endcopy", (PyCFunction) connEndCopy, METH_VARARGS, connEndCopy__doc__},
    21652178#endif /* DIRECT_ACCESS */
    21662179
    21672180#ifdef LARGE_OBJECTS
    2168         {"locreate", (PyCFunction) connCreateLO, 1, connCreateLO__doc__},
    2169         {"getlo", (PyCFunction) connGetLO, 1, connGetLO__doc__},
    2170         {"loimport", (PyCFunction) connImportLO, 1, connImportLO__doc__},
     2181        {"locreate", (PyCFunction) connCreateLO, METH_VARARGS, connCreateLO__doc__},
     2182        {"getlo", (PyCFunction) connGetLO, METH_VARARGS, connGetLO__doc__},
     2183        {"loimport", (PyCFunction) connImportLO, METH_VARARGS, connImportLO__doc__},
    21712184#endif /* LARGE_OBJECTS */
    21722185
     
    21742187};
    21752188
     2189/* gets connection attributes */
    21762190static PyObject *
    21772191connGetAttr(connObject *self, PyObject *nameobj)
     
    22432257#endif
    22442258
    2245         /* attributes list */
    2246 
    2247         if (!strcmp(name, "__members__"))
    2248         {
    2249                 PyObject *list = PyList_New(10);
    2250 
    2251                 if (list)
    2252                 {
    2253                         PyList_SET_ITEM(list, 0, PyStr_FromString("host"));
    2254                         PyList_SET_ITEM(list, 1, PyStr_FromString("port"));
    2255                         PyList_SET_ITEM(list, 2, PyStr_FromString("db"));
    2256                         PyList_SET_ITEM(list, 3, PyStr_FromString("options"));
    2257                         PyList_SET_ITEM(list, 4, PyStr_FromString("tty"));
    2258                         PyList_SET_ITEM(list, 5, PyStr_FromString("error"));
    2259                         PyList_SET_ITEM(list, 6, PyStr_FromString("status"));
    2260                         PyList_SET_ITEM(list, 7, PyStr_FromString("user"));
    2261                         PyList_SET_ITEM(list, 8, PyStr_FromString("protocol_version"));
    2262                         PyList_SET_ITEM(list, 9, PyStr_FromString("server_version"));
    2263                 }
    2264 
    2265                 return list;
    2266         }
    2267 
    22682259        return PyObject_GenericGetAttr((PyObject *) self, nameobj);
    22692260}
    22702261
    2271 /* object type definition */
     2262/* connection type definition */
    22722263static PyTypeObject connType = {
    22732264        PyVarObject_HEAD_INIT(NULL, 0)
     
    28032794}
    28042795
    2805 /* query object methods */
     2796/* get the list of source object attributes */
     2797static PyObject *
     2798sourceDir(connObject *self) {
     2799        PyObject *attrs;
     2800
     2801        attrs = PyObject_Dir(PyObject_Type((PyObject *)self));
     2802        PyObject_CallMethod(attrs, "extend", "[sssss]",
     2803                "pgcnx", "arraysize", "resulttype", "ntuples", "nfields");
     2804
     2805        return attrs;
     2806}
     2807
     2808/* source object methods */
    28062809static PyMethodDef sourceMethods[] = {
     2810        {"__dir__", (PyCFunction) sourceDir,  METH_NOARGS, NULL},
    28072811        {"close", (PyCFunction) sourceClose, METH_VARARGS,
    28082812                        sourceClose__doc__},
     
    28302834};
    28312835
    2832 /* gets query object attributes */
     2836/* gets source object attributes */
    28332837static PyObject *
    28342838sourceGetAttr(sourceObject *self, PyObject *nameobj)
     
    28632867        if (!strcmp(name, "nfields"))
    28642868                return PyInt_FromLong(self->num_fields);
    2865 
    2866         /* attributes list */
    2867         if (!strcmp(name, "__members__"))
    2868         {
    2869                 PyObject *list = PyList_New(5);
    2870 
    2871                 PyList_SET_ITEM(list, 0, PyStr_FromString("pgcnx"));
    2872                 PyList_SET_ITEM(list, 1, PyStr_FromString("arraysize"));
    2873                 PyList_SET_ITEM(list, 2, PyStr_FromString("resulttype"));
    2874                 PyList_SET_ITEM(list, 3, PyStr_FromString("ntuples"));
    2875                 PyList_SET_ITEM(list, 4, PyStr_FromString("nfields"));
    2876 
    2877                 return list;
    2878         }
    28792869
    28802870        /* module name */
     
    29382928static char source__doc__[] = "PyGreSQL source object";
    29392929
    2940 /* query type definition */
     2930/* source type definition */
    29412931static PyTypeObject sourceType = {
    29422932        PyVarObject_HEAD_INIT(NULL, 0)
     
    34913481}
    34923482
    3493 
    3494 /* get attribute */
     3483/* gets notice object attributes */
    34953484static PyObject *
    34963485noticeGetAttr(noticeObject *self, PyObject *nameobj)
     
    35463535        }
    35473536
    3548         /* attributes list */
    3549         if (!strcmp(name, "__members__"))
    3550         {
    3551                 PyObject *list = PyList_New(6);
    3552                 if (list)
    3553                 {
    3554                         PyList_SET_ITEM(list, 0, PyStr_FromString("pgcnx"));
    3555                         PyList_SET_ITEM(list, 1, PyStr_FromString("severity"));
    3556                         PyList_SET_ITEM(list, 2, PyStr_FromString("message"));
    3557                         PyList_SET_ITEM(list, 3, PyStr_FromString("primary"));
    3558                         PyList_SET_ITEM(list, 4, PyStr_FromString("detail"));
    3559                         PyList_SET_ITEM(list, 5, PyStr_FromString("hint"));
    3560                 }
    3561                 return list;
    3562         }
    3563 
    35643537        return PyObject_GenericGetAttr((PyObject *) self, nameobj);
    35653538}
     
    35713544}
    35723545
    3573 /* object type definition */
     3546/* get the list of notice attributes */
     3547static PyObject *
     3548noticeDir(noticeObject *self) {
     3549        PyObject *attrs;
     3550
     3551        attrs = PyObject_Dir(PyObject_Type((PyObject *)self));
     3552        PyObject_CallMethod(attrs, "extend", "[ssssss]",
     3553                "pgcnx", "severity", "message", "primary", "detail", "hint");
     3554
     3555        return attrs;
     3556}
     3557
     3558/* notice object methods */
     3559static struct PyMethodDef noticeMethods[] = {
     3560        {"__dir__", (PyCFunction) noticeDir,  METH_NOARGS, NULL},
     3561        {NULL, NULL}
     3562};
     3563
     3564/* notice type definition */
    35743565static PyTypeObject noticeType = {
    35753566        PyVarObject_HEAD_INIT(NULL, 0)
     
    35943585        0,                                                              /* tp_as_buffer */
    35953586        Py_TPFLAGS_DEFAULT,                             /* tp_flags */
     3587        0,                                                              /* tp_doc */
     3588        0,                                                              /* tp_traverse */
     3589        0,                                                              /* tp_clear */
     3590        0,                                                              /* tp_richcompare */
     3591        0,                                                              /* tp_weaklistoffset */
     3592        0,                                                              /* tp_iter */
     3593        0,                                                              /* tp_iternext */
     3594        noticeMethods,                                  /* tp_methods */
    35963595};
    35973596
Note: See TracChangeset for help on using the changeset viewer.