static PyObject * pboolean_getquoted(pbooleanObject *self, PyObject *args) { if (PyObject_IsTrue(self->wrapped)) { return Bytes_FromString("true"); } else { return Bytes_FromString("false"); } }
static PyObject * pfloat_getquoted(pfloatObject *self, PyObject *args) { PyObject *rv; double n = PyFloat_AsDouble(self->wrapped); if (isnan(n)) rv = Bytes_FromString("'NaN'::float"); else if (isinf(n)) { if (n > 0) rv = Bytes_FromString("'Infinity'::float"); else rv = Bytes_FromString("'-Infinity'::float"); } else { if (!(rv = PyObject_Repr(self->wrapped))) { goto exit; } #if PY_MAJOR_VERSION > 2 /* unicode to bytes in Py3 */ { PyObject *tmp = PyUnicode_AsUTF8String(rv); Py_DECREF(rv); if (!(rv = tmp)) { goto exit; } } #endif if ('-' == Bytes_AS_STRING(rv)[0]) { /* Prepend a space in front of negative numbers (ticket #57) */ PyObject *tmp; if (!(tmp = Bytes_FromString(" "))) { Py_DECREF(rv); rv = NULL; goto exit; } Bytes_ConcatAndDel(&tmp, rv); if (!(rv = tmp)) { goto exit; } } } exit: return rv; }
static PyObject * pboolean_getquoted(pbooleanObject *self, PyObject *args) { #ifdef PSYCOPG_NEW_BOOLEAN if (PyObject_IsTrue(self->wrapped)) { return Bytes_FromString("true"); } else { return Bytes_FromString("false"); } #else if (PyObject_IsTrue(self->wrapped)) { return Bytes_FromString("'t'"); } else { return Bytes_FromString("'f'"); } #endif }
static PyObject * pint_getquoted(pintObject *self, PyObject *args) { PyObject *res = NULL; /* Convert subclass to int to handle IntEnum and other subclasses * whose str() is not the number. */ if (PyLong_CheckExact(self->wrapped) #if PY_MAJOR_VERSION < 2 || PyInt_CheckExact(self->wrapped) #endif ) { res = PyObject_Str(self->wrapped); } else { PyObject *tmp; if (!(tmp = PyObject_CallFunctionObjArgs( (PyObject *)&PyLong_Type, self->wrapped, NULL))) { goto exit; } res = PyObject_Str(tmp); Py_DECREF(tmp); } if (!res) { goto exit; } #if PY_MAJOR_VERSION > 2 /* unicode to bytes in Py3 */ { PyObject *tmp = PyUnicode_AsUTF8String(res); Py_DECREF(res); if (!(res = tmp)) { goto exit; } } #endif if ('-' == Bytes_AS_STRING(res)[0]) { /* Prepend a space in front of negative numbers (ticket #57) */ PyObject *tmp; if (!(tmp = Bytes_FromString(" "))) { Py_DECREF(res); res = NULL; goto exit; } Bytes_ConcatAndDel(&tmp, res); if (!(res = tmp)) { goto exit; } } exit: return res; }
static PyObject * binary_quote(binaryObject *self) { char *to = NULL; const char *buffer = NULL; Py_ssize_t buffer_len; size_t len = 0; PyObject *rv = NULL; #if HAS_MEMORYVIEW Py_buffer view; int got_view = 0; #endif /* if we got a plain string or a buffer we escape it and save the buffer */ #if HAS_MEMORYVIEW if (PyObject_CheckBuffer(self->wrapped)) { if (0 > PyObject_GetBuffer(self->wrapped, &view, PyBUF_CONTIG_RO)) { goto exit; } got_view = 1; buffer = (const char *)(view.buf); buffer_len = view.len; } #endif #if HAS_BUFFER if (!buffer && (Bytes_Check(self->wrapped) || PyBuffer_Check(self->wrapped))) { if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer, &buffer_len) < 0) { goto exit; } } #endif if (!buffer) { goto exit; } /* escape and build quoted buffer */ to = (char *)binary_escape((unsigned char*)buffer, (size_t) buffer_len, &len, self->conn ? ((connectionObject*)self->conn)->pgconn : NULL); if (to == NULL) { PyErr_NoMemory(); goto exit; } if (len > 0) rv = Bytes_FromFormat( (self->conn && ((connectionObject*)self->conn)->equote) ? "E'%s'::bytea" : "'%s'::bytea" , to); else rv = Bytes_FromString("''::bytea"); exit: if (to) { PQfreemem(to); } #if HAS_MEMORYVIEW if (got_view) { PyBuffer_Release(&view); } #endif /* Allow Binary(None) to work */ if (self->wrapped == Py_None) { Py_INCREF(psyco_null); rv = psyco_null; } /* if the wrapped object is not bytes or a buffer, this is an error */ if (!rv && !PyErr_Occurred()) { PyErr_Format(PyExc_TypeError, "can't escape %s to binary", Py_TYPE(self->wrapped)->tp_name); } return rv; }
PyMODINIT_FUNC INIT_MODULE(_psycopg)(void) { #if PY_VERSION_HEX < 0x03020000 static void *PSYCOPG_API[PSYCOPG_API_pointers]; PyObject *c_api_object; #endif PyObject *module = NULL, *dict; #ifdef PSYCOPG_DEBUG if (getenv("PSYCOPG_DEBUG")) psycopg_debug_enabled = 1; #endif Dprintf("initpsycopg: initializing psycopg %s", PSYCOPG_VERSION); /* initialize all the new types and then the module */ Py_TYPE(&connectionType) = &PyType_Type; if (PyType_Ready(&connectionType) == -1) goto exit; Py_TYPE(&cursorType) = &PyType_Type; if (PyType_Ready(&cursorType) == -1) goto exit; Py_TYPE(&typecastType) = &PyType_Type; if (PyType_Ready(&typecastType) == -1) goto exit; Py_TYPE(&qstringType) = &PyType_Type; if (PyType_Ready(&qstringType) == -1) goto exit; Py_TYPE(&binaryType) = &PyType_Type; if (PyType_Ready(&binaryType) == -1) goto exit; Py_TYPE(&isqlquoteType) = &PyType_Type; if (PyType_Ready(&isqlquoteType) == -1) goto exit; Py_TYPE(&pbooleanType) = &PyType_Type; if (PyType_Ready(&pbooleanType) == -1) goto exit; Py_TYPE(&pintType) = &PyType_Type; if (PyType_Ready(&pintType) == -1) goto exit; Py_TYPE(&pfloatType) = &PyType_Type; if (PyType_Ready(&pfloatType) == -1) goto exit; Py_TYPE(&pdecimalType) = &PyType_Type; if (PyType_Ready(&pdecimalType) == -1) goto exit; Py_TYPE(&asisType) = &PyType_Type; if (PyType_Ready(&asisType) == -1) goto exit; Py_TYPE(&listType) = &PyType_Type; if (PyType_Ready(&listType) == -1) goto exit; Py_TYPE(&chunkType) = &PyType_Type; if (PyType_Ready(&chunkType) == -1) goto exit; Py_TYPE(¬ifyType) = &PyType_Type; if (PyType_Ready(¬ifyType) == -1) goto exit; Py_TYPE(&xidType) = &PyType_Type; if (PyType_Ready(&xidType) == -1) goto exit; Py_TYPE(&errorType) = &PyType_Type; errorType.tp_base = (PyTypeObject *)PyExc_StandardError; if (PyType_Ready(&errorType) == -1) goto exit; Py_TYPE(&diagnosticsType) = &PyType_Type; if (PyType_Ready(&diagnosticsType) == -1) goto exit; Py_TYPE(&lobjectType) = &PyType_Type; if (PyType_Ready(&lobjectType) == -1) goto exit; /* initialize libcrypto threading callbacks */ psyco_libcrypto_threads_init(); /* import mx.DateTime module, if necessary */ #ifdef HAVE_MXDATETIME Py_TYPE(&mxdatetimeType) = &PyType_Type; if (PyType_Ready(&mxdatetimeType) == -1) goto exit; if (0 != mxDateTime_ImportModuleAndAPI()) { PyErr_Clear(); /* only fail if the mx typacaster should have been the default */ #ifdef PSYCOPG_DEFAULT_MXDATETIME PyErr_SetString(PyExc_ImportError, "can't import mx.DateTime module (requested as default adapter)"); goto exit; #endif } #endif /* import python builtin datetime module, if available */ pyDateTimeModuleP = PyImport_ImportModule("datetime"); if (pyDateTimeModuleP == NULL) { Dprintf("initpsycopg: can't import datetime module"); PyErr_SetString(PyExc_ImportError, "can't import datetime module"); goto exit; } /* Initialize the PyDateTimeAPI everywhere is used */ PyDateTime_IMPORT; if (psyco_adapter_datetime_init()) { goto exit; } Py_TYPE(&pydatetimeType) = &PyType_Type; if (PyType_Ready(&pydatetimeType) == -1) goto exit; /* initialize the module and grab module's dictionary */ #if PY_MAJOR_VERSION < 3 module = Py_InitModule("_psycopg", psycopgMethods); #else module = PyModule_Create(&psycopgmodule); #endif if (!module) { goto exit; } dict = PyModule_GetDict(module); /* initialize all the module's exported functions */ /* PyBoxer_API[PyBoxer_Fake_NUM] = (void *)PyBoxer_Fake; */ /* Create a CObject containing the API pointer array's address */ /* If anybody asks for a PyCapsule we'll deal with it. */ #if PY_VERSION_HEX < 0x03020000 c_api_object = PyCObject_FromVoidPtr((void *)PSYCOPG_API, NULL); if (c_api_object != NULL) PyModule_AddObject(module, "_C_API", c_api_object); #endif /* other mixed initializations of module-level variables */ if (!(psycoEncodings = PyDict_New())) { goto exit; } if (0 != psyco_encodings_fill(psycoEncodings)) { goto exit; } psyco_null = Bytes_FromString("NULL"); if (!(psyco_DescriptionType = psyco_make_description_type())) { goto exit; } /* set some module's parameters */ PyModule_AddStringConstant(module, "__version__", PSYCOPG_VERSION); PyModule_AddStringConstant(module, "__doc__", "psycopg PostgreSQL driver"); PyModule_AddIntConstant(module, "__libpq_version__", PG_VERSION_NUM); PyModule_AddObject(module, "apilevel", Text_FromUTF8(APILEVEL)); PyModule_AddObject(module, "threadsafety", PyInt_FromLong(THREADSAFETY)); PyModule_AddObject(module, "paramstyle", Text_FromUTF8(PARAMSTYLE)); /* put new types in module dictionary */ PyModule_AddObject(module, "connection", (PyObject*)&connectionType); PyModule_AddObject(module, "cursor", (PyObject*)&cursorType); PyModule_AddObject(module, "ISQLQuote", (PyObject*)&isqlquoteType); PyModule_AddObject(module, "Notify", (PyObject*)¬ifyType); PyModule_AddObject(module, "Xid", (PyObject*)&xidType); PyModule_AddObject(module, "Diagnostics", (PyObject*)&diagnosticsType); PyModule_AddObject(module, "AsIs", (PyObject*)&asisType); PyModule_AddObject(module, "Binary", (PyObject*)&binaryType); PyModule_AddObject(module, "Boolean", (PyObject*)&pbooleanType); PyModule_AddObject(module, "Decimal", (PyObject*)&pdecimalType); PyModule_AddObject(module, "Int", (PyObject*)&pintType); PyModule_AddObject(module, "Float", (PyObject*)&pfloatType); PyModule_AddObject(module, "List", (PyObject*)&listType); PyModule_AddObject(module, "QuotedString", (PyObject*)&qstringType); PyModule_AddObject(module, "lobject", (PyObject*)&lobjectType); PyModule_AddObject(module, "Column", psyco_DescriptionType); /* encodings dictionary in module dictionary */ PyModule_AddObject(module, "encodings", psycoEncodings); #ifdef HAVE_MXDATETIME /* If we can't find mx.DateTime objects at runtime, * remove them from the module (and, as consequence, from the adapters). */ if (0 != psyco_adapter_mxdatetime_init()) { PyDict_DelItemString(dict, "DateFromMx"); PyDict_DelItemString(dict, "TimeFromMx"); PyDict_DelItemString(dict, "TimestampFromMx"); PyDict_DelItemString(dict, "IntervalFromMx"); } #endif /* initialize default set of typecasters */ if (0 != typecast_init(dict)) { goto exit; } /* initialize microprotocols layer */ microprotocols_init(dict); if (0 != psyco_adapters_init(dict)) { goto exit; } /* create a standard set of exceptions and add them to the module's dict */ if (0 != psyco_errors_init()) { goto exit; } psyco_errors_fill(dict); Dprintf("initpsycopg: module initialization complete"); exit: #if PY_MAJOR_VERSION > 2 return module; #else return; #endif }