Merged changes in the trunk up to revision 32124.

This commit is contained in:
2010-09-25 21:03:16 +00:00
221 changed files with 2507 additions and 1497 deletions

View File

@@ -522,7 +522,7 @@ def rna2sphinx(BASEPATH):
type_descr = prop.get_type_description(**kwargs)
if prop.name or prop.description:
fw(ident + ":%s%s: %s\n" % (id_name, identifier, ", ".join([val for val in (prop.name, prop.description) if val])))
fw(ident + ":%s%s: %s\n" % (id_name, identifier, ", ".join(val for val in (prop.name, prop.description) if val)))
fw(ident + ":%s%s: %s\n" % (id_type, identifier, type_descr))
def write_struct(struct):
@@ -564,12 +564,12 @@ def rna2sphinx(BASEPATH):
else:
fw("base class --- ")
fw(", ".join([(":class:`%s`" % base_id) for base_id in base_ids]))
fw(", ".join((":class:`%s`" % base_id) for base_id in base_ids))
fw("\n\n")
subclass_ids = [s.identifier for s in structs.values() if s.base is struct if not rna_info.rna_id_ignore(s.identifier)]
if subclass_ids:
fw("subclasses --- \n" + ", ".join([(":class:`%s`" % s) for s in subclass_ids]) + "\n\n")
fw("subclasses --- \n" + ", ".join((":class:`%s`" % s) for s in subclass_ids) + "\n\n")
base_id = getattr(struct.base, "identifier", "")
@@ -607,7 +607,7 @@ def rna2sphinx(BASEPATH):
del py_properties, py_prop
for func in struct.functions:
args_str = ", ".join([prop.get_arg_default(force=False) for prop in func.args])
args_str = ", ".join(prop.get_arg_default(force=False) for prop in func.args)
fw(" .. %s:: %s(%s)\n\n" % ("classmethod" if func.is_classmethod else "method", func.identifier, args_str))
fw(" %s\n\n" % func.description)
@@ -618,7 +618,7 @@ def rna2sphinx(BASEPATH):
if len(func.return_values) == 1:
write_param(" ", fw, func.return_values[0], is_return=True)
elif func.return_values: # multiple return values
fw(" :return (%s):\n" % ", ".join([prop.identifier for prop in func.return_values]))
fw(" :return (%s):\n" % ", ".join(prop.identifier for prop in func.return_values))
for prop in func.return_values:
type_descr = prop.get_type_description(as_ret=True, class_fmt=":class:`%s`")
descr = prop.description
@@ -736,7 +736,7 @@ def rna2sphinx(BASEPATH):
subclass_ids = [s.identifier for s in structs.values() if s.base is None if not rna_info.rna_id_ignore(s.identifier)]
if subclass_ids:
fw("subclasses --- \n" + ", ".join([(":class:`%s`" % s) for s in sorted(subclass_ids)]) + "\n\n")
fw("subclasses --- \n" + ", ".join((":class:`%s`" % s) for s in sorted(subclass_ids)) + "\n\n")
fw(".. class:: %s\n\n" % _BPY_STRUCT_FAKE)
fw(" built-in base class for all classes in bpy.types.\n\n")
@@ -774,7 +774,7 @@ def rna2sphinx(BASEPATH):
fw(".. module:: bpy.ops.%s\n\n" % op.module_name)
last_mod = op.module_name
args_str = ", ".join([prop.get_arg_default(force=True) for prop in op.args])
args_str = ", ".join(prop.get_arg_default(force=True) for prop in op.args)
fw(".. function:: %s(%s)\n\n" % (op.func_name, args_str))
# if the description isn't valid, we output the standard warning

View File

@@ -357,26 +357,3 @@ void bpy_text_clear_modules(int clear_all)
Py_DECREF(list); /* removes all references from append */
}
#endif
/*****************************************************************************
* Description: This function creates a new Python dictionary object.
* note: dict is owned by sys.modules["__main__"] module, reference is borrowed
* note: important we use the dict from __main__, this is what python expects
for 'pickle' to work as well as strings like this...
>> foo = 10
>> print(__import__("__main__").foo)
*****************************************************************************/
PyObject *bpy_namespace_dict_new(const char *filename)
{
PyInterpreterState *interp= PyThreadState_GET()->interp;
PyObject *mod_main= PyModule_New("__main__");
PyDict_SetItemString(interp->modules, "__main__", mod_main);
Py_DECREF(mod_main); /* sys.modules owns now */
PyModule_AddStringConstant(mod_main, "__name__", "__main__");
if(filename)
PyModule_AddStringConstant(mod_main, "__file__", filename); /* __file__ only for nice UI'ness */
PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
Py_INCREF(interp->builtins); /* AddObject steals a reference */
return PyModule_GetDict(mod_main);
}

View File

@@ -60,7 +60,4 @@ extern PyMethodDef bpy_reload_meth[];
struct Main *bpy_import_main_get(void);
void bpy_import_main_set(struct Main *maggie);
/* name namespace function for bpy & bge */
PyObject *bpy_namespace_dict_new(const char *filename);
#endif /* EXPP_bpy_import_h */

View File

@@ -269,3 +269,170 @@ PyObject *PyC_UnicodeFromByte(const char *str)
return result;
}
}
/*****************************************************************************
* Description: This function creates a new Python dictionary object.
* note: dict is owned by sys.modules["__main__"] module, reference is borrowed
* note: important we use the dict from __main__, this is what python expects
for 'pickle' to work as well as strings like this...
>> foo = 10
>> print(__import__("__main__").foo)
*****************************************************************************/
PyObject *PyC_DefaultNameSpace(const char *filename)
{
PyInterpreterState *interp= PyThreadState_GET()->interp;
PyObject *mod_main= PyModule_New("__main__");
PyDict_SetItemString(interp->modules, "__main__", mod_main);
Py_DECREF(mod_main); /* sys.modules owns now */
PyModule_AddStringConstant(mod_main, "__name__", "__main__");
if(filename)
PyModule_AddStringConstant(mod_main, "__file__", filename); /* __file__ only for nice UI'ness */
PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
Py_INCREF(interp->builtins); /* AddObject steals a reference */
return PyModule_GetDict(mod_main);
}
/* Would be nice if python had this built in */
void PyC_RunQuicky(const char *filepath, int n, ...)
{
FILE *fp= fopen(filepath, "r");
if(fp) {
PyGILState_STATE gilstate= PyGILState_Ensure();
va_list vargs;
int *sizes= PyMem_MALLOC(sizeof(int) * (n / 2));
int i;
PyObject *py_dict = PyC_DefaultNameSpace(filepath);
PyObject *values= PyList_New(n / 2); /* namespace owns this, dont free */
PyObject *py_result, *ret;
PyObject *struct_mod= PyImport_ImportModule("struct");
PyObject *calcsize= PyObject_GetAttrString(struct_mod, "calcsize"); /* struct.calcsize */
PyObject *pack= PyObject_GetAttrString(struct_mod, "pack"); /* struct.pack */
PyObject *unpack= PyObject_GetAttrString(struct_mod, "unpack"); /* struct.unpack */
Py_DECREF(struct_mod);
va_start(vargs, n);
for (i=0; i * 2<n; i++) {
char *format = va_arg(vargs, char *);
void *ptr = va_arg(vargs, void *);
ret= PyObject_CallFunction(calcsize, "s", format);
if(ret) {
sizes[i]= PyLong_AsSsize_t(ret);
Py_DECREF(ret);
ret = PyObject_CallFunction(unpack, "sy#", format, (char *)ptr, sizes[i]);
}
if(ret == NULL) {
printf("PyC_InlineRun error, line:%d\n", __LINE__);
PyErr_Print();
PyErr_Clear();
PyList_SET_ITEM(values, i, Py_None); /* hold user */
Py_INCREF(Py_None);
sizes[i]= 0;
}
else {
if(PyTuple_GET_SIZE(ret) == 1) {
/* convenience, convert single tuples into single values */
PyObject *tmp= PyTuple_GET_ITEM(ret, 0);
Py_INCREF(tmp);
Py_DECREF(ret);
ret = tmp;
}
PyList_SET_ITEM(values, i, ret); /* hold user */
}
}
va_end(vargs);
/* set the value so we can access it */
PyDict_SetItemString(py_dict, "values", values);
py_result = PyRun_File(fp, filepath, Py_file_input, py_dict, py_dict);
fclose(fp);
if(py_result) {
/* we could skip this but then only slice assignment would work
* better not be so strict */
values= PyDict_GetItemString(py_dict, "values");
if(values && PyList_Check(values)) {
/* dont use the result */
Py_DECREF(py_result);
py_result= NULL;
/* now get the values back */
va_start(vargs, n);
for (i=0; i*2 <n; i++) {
char *format = va_arg(vargs, char *);
void *ptr = va_arg(vargs, void *);
PyObject *item;
PyObject *item_new;
/* prepend the string formatting and remake the tuple */
item= PyList_GET_ITEM(values, i);
if(PyTuple_CheckExact(item)) {
int ofs= PyTuple_GET_SIZE(item);
item_new= PyTuple_New(ofs + 1);
while(ofs--) {
PyObject *member= PyTuple_GET_ITEM(item, ofs);
PyTuple_SET_ITEM(item_new, ofs + 1, member);
Py_INCREF(member);
}
PyTuple_SET_ITEM(item_new, 0, PyUnicode_FromString(format));
}
else {
item_new= Py_BuildValue("sO", format, item);
}
ret = PyObject_Call(pack, item_new, NULL);
if(ret) {
/* copy the bytes back into memory */
memcpy(ptr, PyBytes_AS_STRING(ret), sizes[i]);
Py_DECREF(ret);
}
else {
printf("PyC_InlineRun error on arg '%d', line:%d\n", i, __LINE__);
PyC_ObSpit("failed converting:", item_new);
PyErr_Print();
PyErr_Clear();
}
Py_DECREF(item_new);
}
va_end(vargs);
}
else {
printf("PyC_InlineRun error, 'values' not a list, line:%d\n", __LINE__);
}
}
else {
printf("PyC_InlineRun error line:%d\n", __LINE__);
PyErr_Print();
PyErr_Clear();
}
Py_DECREF(calcsize);
Py_DECREF(pack);
Py_DECREF(unpack);
PyMem_FREE(sizes);
PyGILState_Release(gilstate);
}
}

View File

@@ -25,8 +25,6 @@
#ifndef PY_CAPI_UTILS_H
#define PY_CAPI_UTILS_H
struct PyObject;
void PyC_ObSpit(char *name, PyObject *var);
void PyC_LineSpit(void);
PyObject * PyC_ExceptionBuffer(void);
@@ -38,4 +36,7 @@ int PyC_AsArray(void *array, PyObject *value, int length, PyTypeObject *type,
PyObject * PyC_UnicodeFromByte(const char *str);
const char * PuC_UnicodeAsByte(PyObject *py_str, PyObject **coerce); /* coerce must be NULL */
/* name namespace function for bpy & bge */
PyObject * PyC_DefaultNameSpace(const char *filename);
void PyC_RunQuicky(const char *filepath, int n, ...);
#endif // PY_CAPI_UTILS_H

View File

@@ -400,7 +400,7 @@ int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, in
return ret;
}
static PyObject *pyrna_array_item(PointerRNA *ptr, PropertyRNA *prop, int index)
PyObject *pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index)
{
PyObject *item;
@@ -441,7 +441,7 @@ static PyObject *pyrna_py_from_array_internal(PointerRNA *ptr, PropertyRNA *prop
if (dim + 1 < totdim)
item= pyrna_py_from_array_internal(ptr, prop, dim + 1, index);
else {
item= pyrna_array_item(ptr, prop, *index);
item= pyrna_array_index(ptr, prop, *index);
*index= *index + 1;
}
@@ -496,7 +496,7 @@ PyObject *pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr,
}
else {
index = arrayoffset + index;
ret= (BPy_PropertyArrayRNA *)pyrna_array_item(ptr, prop, index);
ret= (BPy_PropertyArrayRNA *)pyrna_array_index(ptr, prop, index);
}
return (PyObject*)ret;

View File

@@ -52,6 +52,7 @@
#include "BPY_extern.h"
#include "../generic/bpy_internal_import.h" // our own imports
#include "../generic/py_capi_utils.h"
/* for internal use, when starting and ending python scripts */
@@ -306,7 +307,6 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc
if (text) {
char fn_dummy[FILE_MAXDIR];
bpy_text_filename_get(fn_dummy, text);
py_dict = bpy_namespace_dict_new(fn_dummy);
if( !text->compiled ) { /* if it wasn't already compiled, do it now */
char *buf = txt_to_buf( text );
@@ -320,31 +320,36 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc
BPY_free_compiled_text( text );
}
}
if(text->compiled)
py_result = PyEval_EvalCode( text->compiled, py_dict, py_dict );
if(text->compiled) {
py_dict = PyC_DefaultNameSpace(fn_dummy);
py_result = PyEval_EvalCode(text->compiled, py_dict, py_dict);
}
}
else {
FILE *fp= fopen(fn, "r");
py_dict = bpy_namespace_dict_new(fn);
if(fp) {
py_dict = PyC_DefaultNameSpace(fn);
#ifdef _WIN32
/* Previously we used PyRun_File to run directly the code on a FILE
* object, but as written in the Python/C API Ref Manual, chapter 2,
* 'FILE structs for different C libraries can be different and
* incompatible'.
* So now we load the script file data to a buffer */
char *pystring;
{
char *pystring;
fclose(fp);
fclose(fp);
pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
pystring[0]= '\0';
sprintf(pystring, "exec(open(r'%s').read())", fn);
py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
MEM_freeN(pystring);
pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
pystring[0]= '\0';
sprintf(pystring, "exec(open(r'%s').read())", fn);
py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
MEM_freeN(pystring);
}
#else
py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
fclose(fp);
@@ -471,7 +476,7 @@ int BPY_run_python_script_space(const char *modulename, const char *func)
gilstate = PyGILState_Ensure();
py_dict = bpy_namespace_dict_new("<dummy>");
py_dict = PyC_DefaultNameSpace("<dummy>");
PyObject *module = PyImport_ImportModule(scpt->script.filename);
if (module==NULL) {
@@ -523,7 +528,7 @@ int BPY_eval_button(bContext *C, const char *expr, double *value)
bpy_context_set(C, &gilstate);
py_dict= bpy_namespace_dict_new("<blender button>");
py_dict= PyC_DefaultNameSpace("<blender button>");
mod = PyImport_ImportModule("math");
if (mod) {
@@ -594,7 +599,7 @@ int BPY_eval_string(bContext *C, const char *expr)
bpy_context_set(C, &gilstate);
py_dict= bpy_namespace_dict_new("<blender string>");
py_dict= PyC_DefaultNameSpace("<blender string>");
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);

View File

@@ -1065,6 +1065,26 @@ static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, ParameterList *p
StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
int flag = RNA_property_flag(prop);
/* this is really nasty!, so we can fake the operator having direct properties eg:
* layout.prop(self, "filepath")
* ... which infact should be
* layout.prop(self.properties, "filepath")
*
* we need to do this trick.
* if the prop is not an operator type and the pyobject is an operator, use its properties in place of its self.
*
* this is so bad that its almost a good reason to do away with fake 'self.properties -> self' class mixing
* if this causes problems in the future it should be removed.
*/
if( (ptype == &RNA_AnyType) &&
(BPy_StructRNA_Check(value)) &&
(RNA_struct_is_a(((BPy_StructRNA *)value)->ptr.type, &RNA_Operator))
) {
value= PyObject_GetAttrString(value, "properties");
value_new= value;
}
/* if property is an OperatorProperties pointer and value is a map, forward back to pyrna_pydict_to_props */
if (RNA_struct_is_a(ptype, &RNA_OperatorProperties) && PyDict_Check(value)) {
PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
@@ -1140,6 +1160,8 @@ static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, ParameterList *p
Py_XDECREF(value_new); return -1;
}
}
Py_XDECREF(value_new);
break;
}
@@ -1957,26 +1979,46 @@ static PyObject *pyrna_struct_values(BPy_PropertyRNA *self)
static int pyrna_struct_anim_args_parse(PointerRNA *ptr, const char *error_prefix, const char *path,
char **path_full, int *index)
{
const int is_idbase= RNA_struct_is_ID(ptr->type);
PropertyRNA *prop;
PointerRNA r_ptr;
if (ptr->data==NULL) {
PyErr_Format(PyExc_TypeError, "%.200s this struct has no data, can't be animated", error_prefix);
return -1;
}
prop = RNA_struct_find_property(ptr, path);
/* full paths can only be given from ID base */
if(is_idbase) {
int r_index= -1;
if(RNA_path_resolve_full(ptr, path, &r_ptr, &prop, &r_index)==0) {
prop= NULL;
}
else if(r_index != -1) {
PyErr_Format(PyExc_ValueError, "%.200s path includes index, must be a separate argument", error_prefix, path);
return -1;
}
else if(ptr->id.data != r_ptr.id.data) {
PyErr_Format(PyExc_ValueError, "%.200s path spans ID blocks", error_prefix, path);
return -1;
}
}
else {
prop = RNA_struct_find_property(ptr, path);
r_ptr= *ptr;
}
if (prop==NULL) {
PyErr_Format( PyExc_TypeError, "%.200s property \"%s\" not found", error_prefix, path);
return -1;
}
if (!RNA_property_animateable(ptr, prop)) {
if (!RNA_property_animateable(&r_ptr, prop)) {
PyErr_Format(PyExc_TypeError, "%.200s property \"%s\" not animatable", error_prefix, path);
return -1;
}
if(RNA_property_array_check(ptr, prop) == 0) {
if(RNA_property_array_check(&r_ptr, prop) == 0) {
if((*index) == -1) {
*index= 0;
}
@@ -1986,18 +2028,23 @@ static int pyrna_struct_anim_args_parse(PointerRNA *ptr, const char *error_prefi
}
}
else {
int array_len= RNA_property_array_length(ptr, prop);
int array_len= RNA_property_array_length(&r_ptr, prop);
if((*index) < -1 || (*index) >= array_len) {
PyErr_Format( PyExc_TypeError, "%.200s index out of range \"%s\", given %d, array length is %d", error_prefix, path, *index, array_len);
return -1;
}
}
*path_full= RNA_path_from_ID_to_property(ptr, prop);
if (*path_full==NULL) {
PyErr_Format( PyExc_TypeError, "%.200s could not make path to \"%s\"", error_prefix, path);
return -1;
if(is_idbase) {
*path_full= BLI_strdup(path);
}
else {
*path_full= RNA_path_from_ID_to_property(&r_ptr, prop);
if (*path_full==NULL) {
PyErr_Format( PyExc_TypeError, "%.200s could not make path to \"%s\"", error_prefix, path);
return -1;
}
}
return 0;
@@ -2262,17 +2309,29 @@ static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *args)
PyObject *coerce= Py_True;
PointerRNA r_ptr;
PropertyRNA *r_prop;
int index= -1;
if (!PyArg_ParseTuple(args, "s|O!:path_resolve", &path, &PyBool_Type, &coerce))
return NULL;
if (RNA_path_resolve(&self->ptr, path, &r_ptr, &r_prop)) {
if (RNA_path_resolve_full(&self->ptr, path, &r_ptr, &r_prop, &index)) {
if(r_prop) {
if(coerce == Py_False) {
return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
if(index != -1) {
if(index >= RNA_property_array_length(&r_ptr, r_prop) || index < 0) {
PyErr_Format(PyExc_TypeError, "%.200s.path_resolve(\"%.200s\") index out of range", RNA_struct_identifier(self->ptr.type), path);
return NULL;
}
else {
return pyrna_array_index(&r_ptr, r_prop, index);
}
}
else {
return pyrna_prop_to_py(&r_ptr, r_prop);
if(coerce == Py_False) {
return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
}
else {
return pyrna_prop_to_py(&r_ptr, r_prop);
}
}
}
else {

View File

@@ -101,6 +101,7 @@ void pyrna_free_types(void);
/* primitive type conversion */
int pyrna_py_to_array(PointerRNA *ptr, PropertyRNA *prop, ParameterList *parms, char *param_data, PyObject *py, const char *error_prefix);
int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, int arrayoffset, int index, PyObject *py, const char *error_prefix);
PyObject *pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index);
PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop);
PyObject *pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index);