Ejemplo n.º 1
0
PyMODINIT_FUNC
initpyodbc()
{
#ifdef _DEBUG
    #ifndef Py_REF_DEBUG
    #error Py_REF_DEBUG not set!
    #endif

    int grfDebugFlags = _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF;
    _CrtSetDbgFlag(grfDebugFlags);
#endif

    ErrorInit();

    // Make sure that this was built correctly.  Unfortunately, the lack of good static assertions leads to compiler
    // warnings...
    int n1 = sizeof(SQLWCHAR);
    int n2 = SQLWCHAR_SIZE;
    if (n1 != n2)
    {
        PyErr_Format(PyExc_RuntimeError, "The pyodbc module was built with incorrect SQLWCHAR_SIZE: actual-size=%d  compiled-size=%d  version=%s",
                     n1, n2, TOSTRING(PYODBC_VERSION));
        return;
    }

    if (PyType_Ready(&ConnectionType) < 0 || PyType_Ready(&CursorType) < 0 || PyType_Ready(&RowType) < 0 || PyType_Ready(&CnxnInfoType) < 0)
        return;

    pModule = Py_InitModule4("pyodbc", pyodbc_methods, module_doc, NULL, PYTHON_API_VERSION);

    if (!import_types())
        return;

    init_locale_info();

    if (!CreateExceptions())
        return;

    const char* szVersion = TOSTRING(PYODBC_VERSION);
    PyModule_AddStringConstant(pModule, "version", (char*)szVersion);

    PyModule_AddIntConstant(pModule, "threadsafety", 1);
    PyModule_AddStringConstant(pModule, "apilevel", "2.0");
    PyModule_AddStringConstant(pModule, "paramstyle", "qmark");
    PyModule_AddObject(pModule, "pooling", Py_True);
    Py_INCREF(Py_True);
    PyModule_AddObject(pModule, "lowercase", Py_False);
    Py_INCREF(Py_False);
                       
    PyModule_AddObject(pModule, "Connection", (PyObject*)&ConnectionType);
    Py_INCREF((PyObject*)&ConnectionType);
    PyModule_AddObject(pModule, "Cursor", (PyObject*)&CursorType);
    Py_INCREF((PyObject*)&CursorType);
    PyModule_AddObject(pModule, "Row", (PyObject*)&RowType);
    Py_INCREF((PyObject*)&RowType);

    // Add the SQL_XXX defines from ODBC.
    for (unsigned int i = 0; i < _countof(aConstants); i++)
        PyModule_AddIntConstant(pModule, (char*)aConstants[i].szName, aConstants[i].value);

    PyModule_AddObject(pModule, "Date", (PyObject*)PyDateTimeAPI->DateType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateType);
    PyModule_AddObject(pModule, "Time", (PyObject*)PyDateTimeAPI->TimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->TimeType);
    PyModule_AddObject(pModule, "Timestamp", (PyObject*)PyDateTimeAPI->DateTimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateTimeType);
    PyModule_AddObject(pModule, "DATETIME", (PyObject*)PyDateTimeAPI->DateTimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateTimeType);
    PyModule_AddObject(pModule, "STRING", (PyObject*)&PyString_Type);
    Py_INCREF((PyObject*)&PyString_Type);
    PyModule_AddObject(pModule, "NUMBER", (PyObject*)&PyFloat_Type);
    Py_INCREF((PyObject*)&PyFloat_Type);
    PyModule_AddObject(pModule, "ROWID", (PyObject*)&PyInt_Type);
    Py_INCREF((PyObject*)&PyInt_Type);
    PyModule_AddObject(pModule, "BINARY", (PyObject*)&PyBuffer_Type);
    Py_INCREF((PyObject*)&PyBuffer_Type);
    PyModule_AddObject(pModule, "Binary", (PyObject*)&PyBuffer_Type);
    Py_INCREF((PyObject*)&PyBuffer_Type);
    
    PyModule_AddIntConstant(pModule, "UNICODE_SIZE", sizeof(Py_UNICODE));
    PyModule_AddIntConstant(pModule, "SQLWCHAR_SIZE", sizeof(SQLWCHAR));

    if (PyErr_Occurred())
        ErrorCleanup();
}
Ejemplo n.º 2
0
initpyodbc(void)
#endif
{
    ErrorInit();

    if (PyType_Ready(&ConnectionType) < 0 || PyType_Ready(&CursorType) < 0 || PyType_Ready(&RowType) < 0 || PyType_Ready(&CnxnInfoType) < 0)
        return MODRETURN(0);

    Object module;

#if PY_MAJOR_VERSION >= 3
    module.Attach(PyModule_Create(&moduledef));
#else
    module.Attach(Py_InitModule4("pyodbc", pyodbc_methods, module_doc, NULL, PYTHON_API_VERSION));
#endif

    pModule = module.Get();

    if (!module || !import_types() || !CreateExceptions())
        return MODRETURN(0);

    init_locale_info();

    const char* szVersion = TOSTRING(PYODBC_VERSION);
    PyModule_AddStringConstant(module, "version", (char*)szVersion);

    PyModule_AddIntConstant(module, "threadsafety", 1);
    PyModule_AddStringConstant(module, "apilevel", "2.0");
    PyModule_AddStringConstant(module, "paramstyle", "qmark");
    PyModule_AddObject(module, "pooling", Py_True);
    Py_INCREF(Py_True);
    PyModule_AddObject(module, "lowercase", Py_False);
    Py_INCREF(Py_False);
    PyModule_AddObject(module, "native_uuid", Py_False);
    Py_INCREF(Py_False);

    PyModule_AddObject(module, "Connection", (PyObject*)&ConnectionType);
    Py_INCREF((PyObject*)&ConnectionType);
    PyModule_AddObject(module, "Cursor", (PyObject*)&CursorType);
    Py_INCREF((PyObject*)&CursorType);
    PyModule_AddObject(module, "Row", (PyObject*)&RowType);
    Py_INCREF((PyObject*)&RowType);

    // Add the SQL_XXX defines from ODBC.
    for (unsigned int i = 0; i < _countof(aConstants); i++)
        PyModule_AddIntConstant(module, (char*)aConstants[i].szName, aConstants[i].value);

    PyModule_AddObject(module, "Date", (PyObject*)PyDateTimeAPI->DateType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateType);
    PyModule_AddObject(module, "Time", (PyObject*)PyDateTimeAPI->TimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->TimeType);
    PyModule_AddObject(module, "Timestamp", (PyObject*)PyDateTimeAPI->DateTimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateTimeType);
    PyModule_AddObject(module, "DATETIME", (PyObject*)PyDateTimeAPI->DateTimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateTimeType);
    PyModule_AddObject(module, "STRING", (PyObject*)&PyString_Type);
    Py_INCREF((PyObject*)&PyString_Type);
    PyModule_AddObject(module, "NUMBER", (PyObject*)&PyFloat_Type);
    Py_INCREF((PyObject*)&PyFloat_Type);
    PyModule_AddObject(module, "ROWID", (PyObject*)&PyInt_Type);
    Py_INCREF((PyObject*)&PyInt_Type);

    PyObject* binary_type;
#if PY_VERSION_HEX >= 0x02060000
    binary_type = (PyObject*)&PyByteArray_Type;
#else
    binary_type = (PyObject*)&PyBuffer_Type;
#endif
    PyModule_AddObject(module, "BINARY", binary_type);
    Py_INCREF(binary_type);
    PyModule_AddObject(module, "Binary", binary_type);
    Py_INCREF(binary_type);

    I(null_binary != 0);        // must be initialized first
    PyModule_AddObject(module, "BinaryNull", null_binary);

    PyModule_AddIntConstant(module, "UNICODE_SIZE", sizeof(Py_UNICODE));
    PyModule_AddIntConstant(module, "SQLWCHAR_SIZE", sizeof(SQLWCHAR));

    if (!PyErr_Occurred())
    {
        module.Detach();
    }
    else
    {
        ErrorCleanup();
    }

    return MODRETURN(pModule);
}
Ejemplo n.º 3
0
PyMODINIT_FUNC
initpyodbc()
{
#ifdef _DEBUG
    #ifndef Py_REF_DEBUG
    #error Py_REF_DEBUG not set!
    #endif

    int grfDebugFlags = _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF;
    _CrtSetDbgFlag(grfDebugFlags);
#endif

    ErrorInit();

    if (PyType_Ready(&ConnectionType) < 0 || PyType_Ready(&CursorType) < 0 || PyType_Ready(&RowType) < 0 || PyType_Ready(&CnxnInfoType) < 0)
        return;

    pModule = Py_InitModule4("pyodbc", pyodbc_methods, module_doc, NULL, PYTHON_API_VERSION);

    if (!import_types())
        return;

    init_locale_info();

    if (!CreateExceptions())
        return;

    // The 'build' version number is a beta identifier.  For example, if it is 7, then we are on beta7 of the
    // (major,minor.micro) version.  On Windows, we poke these values into the DLL's version resource, so when we make
    // an official build (which come *after* the betas), we set the BUILD to 9999 so installers will know that it
    // should replace any installed betas.  However, we obviously don't want to see these.

    PyObject* pVersion;
    if (PYODBC_BUILD == 9999)
        pVersion = PyString_FromFormat("%d.%d.%d", PYODBC_MAJOR, PYODBC_MINOR, PYODBC_MICRO);
    else
        pVersion = PyString_FromFormat("%d.%d.%d-beta%d", PYODBC_MAJOR, PYODBC_MINOR, PYODBC_MICRO, PYODBC_BUILD);
    PyModule_AddObject(pModule, "version", pVersion);

    PyModule_AddIntConstant(pModule, "threadsafety", 1);
    PyModule_AddStringConstant(pModule, "apilevel", "2.0");
    PyModule_AddStringConstant(pModule, "paramstyle", "qmark");
    PyModule_AddObject(pModule, "pooling", Py_True);
    Py_INCREF(Py_True);
    PyModule_AddObject(pModule, "lowercase", Py_False);
    Py_INCREF(Py_False);
                       
    PyModule_AddObject(pModule, "Connection", (PyObject*)&ConnectionType);
    Py_INCREF((PyObject*)&ConnectionType);
    PyModule_AddObject(pModule, "Cursor", (PyObject*)&CursorType);
    Py_INCREF((PyObject*)&CursorType);
    PyModule_AddObject(pModule, "Row", (PyObject*)&RowType);
    Py_INCREF((PyObject*)&RowType);

    // Add the SQL_XXX defines from ODBC.
    for (unsigned int i = 0; i < _countof(aConstants); i++)
        PyModule_AddIntConstant(pModule, (char*)aConstants[i].szName, aConstants[i].value);

    PyModule_AddObject(pModule, "Date", (PyObject*)PyDateTimeAPI->DateType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateType);
    PyModule_AddObject(pModule, "Time", (PyObject*)PyDateTimeAPI->TimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->TimeType);
    PyModule_AddObject(pModule, "Timestamp", (PyObject*)PyDateTimeAPI->DateTimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateTimeType);
    PyModule_AddObject(pModule, "DATETIME", (PyObject*)PyDateTimeAPI->DateTimeType);
    Py_INCREF((PyObject*)PyDateTimeAPI->DateTimeType);
    PyModule_AddObject(pModule, "STRING", (PyObject*)&PyString_Type);
    Py_INCREF((PyObject*)&PyString_Type);
    PyModule_AddObject(pModule, "NUMBER", (PyObject*)&PyFloat_Type);
    Py_INCREF((PyObject*)&PyFloat_Type);
    PyModule_AddObject(pModule, "ROWID", (PyObject*)&PyInt_Type);
    Py_INCREF((PyObject*)&PyInt_Type);
    PyModule_AddObject(pModule, "BINARY", (PyObject*)&PyBuffer_Type);
    Py_INCREF((PyObject*)&PyBuffer_Type);
    PyModule_AddObject(pModule, "Binary", (PyObject*)&PyBuffer_Type);
    Py_INCREF((PyObject*)&PyBuffer_Type);
    
    if (PyErr_Occurred())
        ErrorCleanup();
}