Пример #1
0
// Wrapper function for getting int from a Python object, with 2/3 ifdefs
int as_int(PyObject *target)
{
    int result;
#if PY_MAJOR_VERSION >= 3
    if (!PyLong_Check(target)) {
#else
    if (!PyInt_Check(target)) {
#endif
        PyErr_SetString(PyExc_ValueError, "Non-integer Python object in as_int");
        return -1;
    }

#if PY_MAJOR_VERSION >= 3
    result = (int) PyLong_AsLong(target);
#else
    result = (int) PyInt_AsLong(target);
#endif

    return result;
}

// Wrapper function to check if a python object is an int
int is_pyint(PyObject *target)
{
#if PY_MAJOR_VERSION >= 3
    return PyLong_Check(target);
#else
    return PyInt_Check(target);
#endif
}

// The VMDApp instance will be found in the VMDApp module, under the
// VMDApp dictionary entry.  Got it?  

VMDApp *get_vmdapp() {

  PyObject *module_dict = PyEval_GetBuiltins();
  if (!module_dict)
      return NULL;

// Python 3 uses a "capsule" to store C pointers
#if PY_MAJOR_VERSION >= 3
    PyObject *c_obj = PyDict_GetItemString(module_dict, "-vmdapp-");
    if (!c_obj || PyErr_Occurred())
        return NULL;

    if (PyCapsule_CheckExact(c_obj))
        return (VMDApp *)PyCapsule_GetPointer(c_obj, "-vmdapp-");

// Python 2 instead uses the idea of a "C Object"
#else
    PyObject *c_obj = PyDict_GetItemString(module_dict, (char *)"-vmdapp-");
    if (!c_obj || PyErr_Occurred())
        return NULL;

    if (PyCObject_Check(c_obj))
        return (VMDApp *)PyCObject_AsVoidPtr(c_obj);
#endif
    return NULL;
}

void set_vmdapp(VMDApp *app) {

  PyObject *module_dict = PyEval_GetBuiltins();
  PyObject *cap;

#if PY_MAJOR_VERSION >= 3
  cap = PyCapsule_New(app, "-vmdapp-", NULL);
#else
  cap = PyCObject_FromVoidPtr(app, NULL);
#endif
  PyDict_SetItemString(module_dict, (const char *)"-vmdapp-", cap);
}
Пример #2
0
static PyObject* __Pyx_PyExec3(PyObject* o, PyObject* globals, PyObject* locals) {
    PyObject* result;
    PyObject* s = 0;
    char *code = 0;

    if (!globals || globals == Py_None) {
        globals = PyModule_GetDict($module_cname);
        if (!globals)
            goto bad;
    } else if (!PyDict_Check(globals)) {
        PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.200s",
                     Py_TYPE(globals)->tp_name);
        goto bad;
    }
    if (!locals || locals == Py_None) {
        locals = globals;
    }

    if (PyDict_GetItem(globals, PYIDENT("__builtins__")) == NULL) {
        if (PyDict_SetItem(globals, PYIDENT("__builtins__"), PyEval_GetBuiltins()) < 0)
            goto bad;
    }

    if (PyCode_Check(o)) {
        if (PyCode_GetNumFree((PyCodeObject *)o) > 0) {
            PyErr_SetString(PyExc_TypeError,
                "code object passed to exec() may not contain free variables");
            goto bad;
        }
        #if PY_VERSION_HEX < 0x030200B1
        result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
        #else
        result = PyEval_EvalCode(o, globals, locals);
        #endif
    } else {
        PyCompilerFlags cf;
        cf.cf_flags = 0;
        if (PyUnicode_Check(o)) {
            cf.cf_flags = PyCF_SOURCE_IS_UTF8;
            s = PyUnicode_AsUTF8String(o);
            if (!s) goto bad;
            o = s;
        #if PY_MAJOR_VERSION >= 3
        } else if (!PyBytes_Check(o)) {
        #else
        } else if (!PyString_Check(o)) {
        #endif
            PyErr_Format(PyExc_TypeError,
                "exec: arg 1 must be string, bytes or code object, got %.200s",
                Py_TYPE(o)->tp_name);
            goto bad;
        }
        #if PY_MAJOR_VERSION >= 3
        code = PyBytes_AS_STRING(o);
        #else
        code = PyString_AS_STRING(o);
        #endif
        if (PyEval_MergeCompilerFlags(&cf)) {
            result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
        } else {
            result = PyRun_String(code, Py_file_input, globals, locals);
        }
        Py_XDECREF(s);
    }

    return result;
bad:
    Py_XDECREF(s);
    return 0;
}
Пример #3
0
/* call BPY_context_set first */
void BPY_python_start(int argc, const char **argv)
{
#ifndef WITH_PYTHON_MODULE
	PyThreadState *py_tstate = NULL;
	const char *py_path_bundle = BKE_appdir_folder_id(BLENDER_SYSTEM_PYTHON, NULL);

	/* not essential but nice to set our name */
	static wchar_t program_path_wchar[FILE_MAX]; /* python holds a reference */
	BLI_strncpy_wchar_from_utf8(program_path_wchar, BKE_appdir_program_path(), ARRAY_SIZE(program_path_wchar));
	Py_SetProgramName(program_path_wchar);

	/* must run before python initializes */
	PyImport_ExtendInittab(bpy_internal_modules);

	/* allow to use our own included python */
	PyC_SetHomePath(py_path_bundle);

	/* without this the sys.stdout may be set to 'ascii'
	 * (it is on my system at least), where printing unicode values will raise
	 * an error, this is highly annoying, another stumbling block for devs,
	 * so use a more relaxed error handler and enforce utf-8 since the rest of
	 * blender is utf-8 too - campbell */
	Py_SetStandardStreamEncoding("utf-8", "surrogateescape");

	/* Update, Py3.3 resolves attempting to parse non-existing header */
#if 0
	/* Python 3.2 now looks for '2.xx/python/include/python3.2d/pyconfig.h' to
	 * parse from the 'sysconfig' module which is used by 'site',
	 * so for now disable site. alternatively we could copy the file. */
	if (py_path_bundle) {
		Py_NoSiteFlag = 1;
	}
#endif

	Py_FrozenFlag = 1;

	Py_Initialize();

	// PySys_SetArgv(argc, argv);  /* broken in py3, not a huge deal */
	/* sigh, why do python guys not have a (char **) version anymore? */
	{
		int i;
		PyObject *py_argv = PyList_New(argc);
		for (i = 0; i < argc; i++) {
			/* should fix bug #20021 - utf path name problems, by replacing
			 * PyUnicode_FromString, with this one */
			PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i]));
		}

		PySys_SetObject("argv", py_argv);
		Py_DECREF(py_argv);
	}
	
	/* Initialize thread support (also acquires lock) */
	PyEval_InitThreads();
#else
	(void)argc;
	(void)argv;

	/* must run before python initializes */
	/* broken in py3.3, load explicitly below */
	// PyImport_ExtendInittab(bpy_internal_modules);
#endif

	bpy_intern_string_init();


#ifdef WITH_PYTHON_MODULE
	{
		/* Manually load all modules */
		struct _inittab *inittab_item;
		PyObject *sys_modules = PyImport_GetModuleDict();

		for (inittab_item = bpy_internal_modules; inittab_item->name; inittab_item++) {
			PyObject *mod = inittab_item->initfunc();
			if (mod) {
				PyDict_SetItemString(sys_modules, inittab_item->name, mod);
			}
			else {
				PyErr_Print();
				PyErr_Clear();
			}
			// Py_DECREF(mod); /* ideally would decref, but in this case we never want to free */
		}
	}
#endif

	/* bpy.* and lets us import it */
	BPy_init_modules();

	bpy_import_init(PyEval_GetBuiltins());
	
	pyrna_alloc_types();

#ifndef WITH_PYTHON_MODULE
	/* py module runs atexit when bpy is freed */
	BPY_atexit_register(); /* this can init any time */

	py_tstate = PyGILState_GetThisThreadState();
	PyEval_ReleaseThread(py_tstate);
#endif
}