示例#1
0
void PythonLoader::getSipAPI()
{
    sip = (const sipAPIDef *)PyCapsule_Import("sip._C_API", 0);

    if (!sip)
        PyErr_Print();
}
示例#2
0
文件: pyev.c 项目: Spencerx/pyev
int
pyev_import_socket(void)
{
    void *api;

#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7
    PyObject *_socket, *_socket_CAPI;
    _socket = PyImport_ImportModule("_socket");
    if (!_socket) {
        return -1;
    }
    _socket_CAPI = PyObject_GetAttrString(_socket, "CAPI");
    if (!_socket_CAPI) {
        Py_DECREF(_socket);
        return -1;
    }
    api = PyCObject_AsVoidPtr(_socket_CAPI);
    Py_DECREF(_socket_CAPI);
    Py_DECREF(_socket);
#else
    api = PyCapsule_Import("_socket.CAPI", 0);
#endif
    if (!api) {
        return -1;
    }
    memcpy(&PySocketModule, api, sizeof(PySocketModule));
    return 0;
}
示例#3
0
init_test_addrxlat (void)
#endif
{
	PyObject *mod;

	addrxlat_API = (struct addrxlat_CAPI*)
		PyCapsule_Import(addrxlat_CAPSULE_NAME, 0);
	if (!addrxlat_API)
		goto err;
	if (addrxlat_API->ver < addrxlat_CAPI_VER) {
		PyErr_Format(PyExc_RuntimeError,
			     "addrxlat CAPI ver >= %lu needed, %lu found",
			     addrxlat_CAPI_VER, addrxlat_API->ver);
		goto err;
	}

#if PY_MAJOR_VERSION >= 3
	mod = PyModule_Create(&addrxlat_moddef);
#else
	mod = Py_InitModule3(MOD_NAME, test_methods, MOD_DOC);
#endif
	if (!mod)
		goto err;

	return MOD_SUCCESS_VAL(mod);

 err:
	return MOD_ERROR_VAL;
}
示例#4
0
PyMODINIT_FUNC
PyInit__undefined(void)
{
    LzExported *lazy_symbols;
    PyObject *strict_meth = NULL;
    PyObject *undefined_inner_type = NULL;
    PyObject *undefined_inner = NULL;
    PyObject *undefined = NULL;
    PyObject *m;
    int err;

    if (!(lazy_symbols =
          PyCapsule_Import("lazy._thunk._exported_symbols", 0))) {
        return NULL;
    }

    if (!(undefined_inner_type = PyErr_NewException(
              "lazy._undefined.undefined", NULL, NULL))) {
            return NULL;
        }


    if (!(undefined_inner = PyObject_CallFunctionObjArgs(
              undefined_inner_type, NULL))) {
        goto error;
    }

    if (!(undefined = lazy_symbols->LzThunk_FromExpr(undefined_inner))) {
        goto error;
    }

    if (!(m = PyModule_Create(&_undefined_module))) {
        goto error;
    }


    if (!(strict_meth = PyCFunction_NewEx(&strict, undefined_inner, m))) {
        goto error;
    }

    err = PyObject_SetAttrString(undefined_inner_type,
                                 "__strict__",
                                 strict_meth);
    Py_DECREF(strict_meth);
    if (err) {
        goto error;
    }

    if (PyObject_SetAttrString(m, "undefined", undefined)) {
        goto error;
    }
    return m;

error:
    Py_XDECREF(strict_meth);
    Py_XDECREF(undefined_inner_type);
    Py_XDECREF(undefined_inner);
    Py_XDECREF(undefined);
    return NULL;
}
示例#5
0
static const sipAPIDef *sip_api()
{
  static const sipAPIDef *sip_API = 0;
  if (sip_API) return sip_API;
#if defined(SIP_USE_PYCAPSULE)
  sip_API = (const sipAPIDef *)PyCapsule_Import("sip._C_API", 0);
#else
  /* Import the SIP module. */
  PyObject * sip_module = PyImport_ImportModule("sip");
  if (sip_module == NULL) return NULL;

  /* Get the module's dictionary. */
  PyObject * sip_module_dict = PyModule_GetDict(sip_module);

  /* Get the "_C_API" attribute. */
  PyObject * c_api = PyDict_GetItemString(sip_module_dict, "_C_API");
  if (c_api == NULL) return NULL;

  /* Sanity check that it is the right type. */
  if (!PyCObject_Check(c_api)) return NULL;

  /* Get the actual pointer from the object. */
  sip_API = (const sipAPIDef *)PyCObject_AsVoidPtr(c_api);
#endif
  return sip_API;
}
// New Python interface using Capsule
int import_quisk_api(void)
{
	Quisk_API = (void **)PyCapsule_Import("_quisk.QUISK_C_API", 0);
	if (Quisk_API == NULL) {
		printf("Failure to import Quisk_API\n");
		return -1;
	}
	pt_quisk_sound_state = (struct sound_conf *)Quisk_API[0];
	return 0;
}
示例#7
0
static PyObject*
module_init(void)
{
    PyObject *module, *mod_dict, *interfaces, *conflicterr;

#ifdef KEY_TYPE_IS_PYOBJECT
    object_ = PyTuple_GetItem(Py_TYPE(Py_None)->tp_bases, 0);
    if (object_ == NULL)
      return NULL;
#endif

    sort_str = INTERN("sort");
    if (!sort_str)
        return NULL;
    reverse_str = INTERN("reverse");
    if (!reverse_str)
        return NULL;
    __setstate___str = INTERN("__setstate__");
    if (!__setstate___str)
        return NULL;
    _bucket_type_str = INTERN("_bucket_type");
    if (!_bucket_type_str)
        return NULL;

    /* Grab the ConflictError class */
    interfaces = PyImport_ImportModule("BTrees.Interfaces");
    if (interfaces != NULL)
    {
        conflicterr = PyObject_GetAttrString(interfaces, "BTreesConflictError");
        if (conflicterr != NULL)
            ConflictError = conflicterr;
        Py_DECREF(interfaces);
    }

    if (ConflictError == NULL)
    {
        Py_INCREF(PyExc_ValueError);
        ConflictError=PyExc_ValueError;
    }

    /* Initialize the PyPersist_C_API and the type objects. */
#ifdef PY3K
    cPersistenceCAPI = (cPersistenceCAPIstruct *)PyCapsule_Import(
                "persistent.cPersistence.CAPI", 0);
#else
    cPersistenceCAPI = (cPersistenceCAPIstruct *)PyCObject_Import(
                "persistent.cPersistence", "CAPI");
#endif
    if (cPersistenceCAPI == NULL)
        return NULL;

#ifdef PY3K
#define _SET_TYPE(typ) ((PyObject*)(&typ))->ob_type = &PyType_Type
#else
#define _SET_TYPE(typ) (typ).ob_type = &PyType_Type
#endif
    _SET_TYPE(BTreeItemsType);
    _SET_TYPE(BTreeIter_Type);
    BTreeIter_Type.tp_getattro = PyObject_GenericGetAttr;
    BucketType.tp_new = PyType_GenericNew;
    SetType.tp_new = PyType_GenericNew;
    BTreeType.tp_new = PyType_GenericNew;
    TreeSetType.tp_new = PyType_GenericNew;
    if (!init_persist_type(&BucketType))
	    return NULL;
    if (!init_persist_type(&BTreeType))
	    return NULL;
    if (!init_persist_type(&SetType))
	    return NULL;
    if (!init_persist_type(&TreeSetType))
	    return NULL;

    if (PyDict_SetItem(BTreeType.tp_dict, _bucket_type_str,
		       (PyObject *)&BucketType) < 0)
    {
        fprintf(stderr, "btree failed\n");
        return NULL;
    }
    if (PyDict_SetItem(TreeSetType.tp_dict, _bucket_type_str,
		       (PyObject *)&SetType) < 0)
    {
        fprintf(stderr, "bucket failed\n");
        return NULL;
    }

    /* Create the module and add the functions */
#ifdef PY3K
    module = PyModule_Create(&moduledef);
#else
    module = Py_InitModule4("_" MOD_NAME_PREFIX "BTree",
		       module_methods, BTree_module_documentation,
		       (PyObject *)NULL, PYTHON_API_VERSION);
#endif

    /* Add some symbolic constants to the module */
    mod_dict = PyModule_GetDict(module);
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "Bucket",
			     (PyObject *)&BucketType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "BTree",
			     (PyObject *)&BTreeType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "Set",
			     (PyObject *)&SetType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "TreeSet",
			     (PyObject *)&TreeSetType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "TreeIterator",
			     (PyObject *)&BTreeIter_Type) < 0)
        return NULL;
	/* We also want to be able to access these constants without the prefix
	 * so that code can more easily exchange modules (particularly the integer
	 * and long modules, but also others).  The TreeIterator is only internal,
	 * so we don't bother to expose that.
     */
    if (PyDict_SetItemString(mod_dict, "Bucket",
			     (PyObject *)&BucketType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, "BTree",
			     (PyObject *)&BTreeType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, "Set",
			     (PyObject *)&SetType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, "TreeSet",
			     (PyObject *)&TreeSetType) < 0)
        return NULL;
#if defined(ZODB_64BIT_INTS) && defined(NEED_LONG_LONG_SUPPORT)
    if (PyDict_SetItemString(mod_dict, "using64bits", Py_True) < 0)
        return NULL;
#else
    if (PyDict_SetItemString(mod_dict, "using64bits", Py_False) < 0)
        return NULL;
#endif
    return module;
}
PyMODINIT_FUNC SIP_MODULE_ENTRY()
#endif
{
    static PyMethodDef sip_methods[] = {
        {0, 0, 0, 0}
    };

#if PY_MAJOR_VERSION >= 3
    static PyModuleDef sip_module_def = {
        PyModuleDef_HEAD_INIT,
        "PyQt4.QtSvg",
        NULL,
        -1,
        sip_methods,
        NULL,
        NULL,
        NULL,
        NULL
    };
#endif

    PyObject *sipModule, *sipModuleDict;
#if !defined(SIP_USE_PYCAPSULE)
    PyObject *sip_sipmod, *sip_capiobj;
#endif

    /* Initialise the module and get it's dictionary. */
#if PY_MAJOR_VERSION >= 3
    sipModule = PyModule_Create(&sip_module_def);
#elif PY_VERSION_HEX >= 0x02050000
    sipModule = Py_InitModule(sipName_PyQt4_QtSvg, sip_methods);
#else
    sipModule = Py_InitModule(const_cast<char *>(sipName_PyQt4_QtSvg), sip_methods);
#endif

    if (sipModule == NULL)
        SIP_MODULE_RETURN(NULL);

    sipModuleDict = PyModule_GetDict(sipModule);

    /* Get the SIP module's API. */
#if defined(SIP_USE_PYCAPSULE)

    sipAPI_QtSvg = reinterpret_cast<const sipAPIDef *>(PyCapsule_Import("sip._C_API", 0));

    if (sipAPI_QtSvg == NULL)
    {
        SIP_MODULE_DISCARD(sipModule);
        SIP_MODULE_RETURN(NULL);
    }

#else

#if PY_VERSION_HEX >= 0x02050000
    sip_sipmod = PyImport_ImportModule("sip");
#else
    sip_sipmod = PyImport_ImportModule(const_cast<char *>("sip"));
#endif

    if (sip_sipmod == NULL)
    {
        SIP_MODULE_DISCARD(sipModule);
        SIP_MODULE_RETURN(NULL);
    }

    sip_capiobj = PyDict_GetItemString(PyModule_GetDict(sip_sipmod), "_C_API");

    if (sip_capiobj == NULL || !PyCObject_Check(sip_capiobj))
    {
        Py_DECREF(sip_sipmod);
        SIP_MODULE_DISCARD(sipModule);
        SIP_MODULE_RETURN(NULL);
    }

    sipAPI_QtSvg = reinterpret_cast<const sipAPIDef *>(PyCObject_AsVoidPtr(sip_capiobj));

#endif

    /* Export the module and publish it's API. */
    if (sipExportModule(&sipModuleAPI_QtSvg,SIP_API_MAJOR_NR,SIP_API_MINOR_NR,0) < 0)
    {
#if !defined(SIP_USE_PYCAPSULE)
        Py_DECREF(sip_sipmod);
#endif
        SIP_MODULE_DISCARD(sipModule);
        SIP_MODULE_RETURN(0);
    }

    sip_QtSvg_qt_metaobject = (sip_qt_metaobject_func)sipImportSymbol("qtcore_qt_metaobject");
    sip_QtSvg_qt_metacall = (sip_qt_metacall_func)sipImportSymbol("qtcore_qt_metacall");
    sip_QtSvg_qt_metacast = (sip_qt_metacast_func)sipImportSymbol("qtcore_qt_metacast");

    /* Initialise the module now all its dependencies have been set up. */
    if (sipInitModule(&sipModuleAPI_QtSvg,sipModuleDict) < 0)
    {
#if !defined(SIP_USE_PYCAPSULE)
        Py_DECREF(sip_sipmod);
#endif
        SIP_MODULE_DISCARD(sipModule);
        SIP_MODULE_RETURN(0);
    }

    /* Get the APIs of the modules that this one is dependent on. */
    sipModuleAPI_QtSvg_QtCore = sipModuleAPI_QtSvg.em_imports[0].im_module;
    sipModuleAPI_QtSvg_QtGui = sipModuleAPI_QtSvg.em_imports[1].im_module;

    SIP_MODULE_RETURN(sipModule);
}