static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kwds) { static const char *kwlist[] = { "filepath", "datablocks", /* optional */ "relative_remap", "fake_user", "compress", NULL, }; /* args */ const char *filepath; char filepath_abs[FILE_MAX]; PyObject *datablocks = NULL; bool use_relative_remap = false, use_fake_user = false, use_compress = false; if (!PyArg_ParseTupleAndKeywords( args, kwds, "sO!|$O&O&O&:write", (char **)kwlist, &filepath, &PySet_Type, &datablocks, PyC_ParseBool, &use_relative_remap, PyC_ParseBool, &use_fake_user, PyC_ParseBool, &use_compress)) { return NULL; } Main *bmain_src = G.main; int write_flags = 0; if (use_relative_remap) { write_flags |= G_FILE_RELATIVE_REMAP; } if (use_compress) { write_flags |= G_FILE_COMPRESS; } BLI_strncpy(filepath_abs, filepath, FILE_MAX); BLI_path_abs(filepath_abs, G.main->name); BKE_blendfile_write_partial_begin(bmain_src); /* array of ID's and backup any data we modify */ struct { ID *id; /* original values */ short id_flag; short id_us; } *id_store_array, *id_store; int id_store_len = 0; PyObject *ret; /* collect all id data from the set and store in 'id_store_array' */ { Py_ssize_t pos, hash; PyObject *key; id_store_array = MEM_mallocN(sizeof(*id_store_array) * PySet_Size(datablocks), __func__); id_store = id_store_array; pos = hash = 0; while (_PySet_NextEntry(datablocks, &pos, &key, &hash)) { if (!pyrna_id_FromPyObject(key, &id_store->id)) { PyErr_Format(PyExc_TypeError, "Expected and ID type, not %.200s", Py_TYPE(key)->tp_name); ret = NULL; goto finally; } else { id_store->id_flag = id_store->id->flag; id_store->id_us = id_store->id->us; if (use_fake_user) { id_store->id->flag |= LIB_FAKEUSER; } id_store->id->us = 1; BKE_blendfile_write_partial_tag_ID(id_store->id, true); id_store_len += 1; id_store++; } } } /* write blend */ int retval = 0; ReportList reports; BKE_reports_init(&reports, RPT_STORE); retval = BKE_blendfile_write_partial(bmain_src, filepath_abs, write_flags, &reports); /* cleanup state */ BKE_blendfile_write_partial_end(bmain_src); if (retval) { BKE_reports_print(&reports, RPT_ERROR_ALL); BKE_reports_clear(&reports); ret = Py_None; Py_INCREF(ret); } else { if (BPy_reports_to_error(&reports, PyExc_IOError, true) == 0) { PyErr_SetString(PyExc_IOError, "Unknown error writing library data"); } ret = NULL; } finally: /* clear all flags for ID's added to the store (may run on error too) */ id_store = id_store_array; for (int i = 0; i < id_store_len; id_store++, i++) { if (use_fake_user) { if ((id_store->id_flag & LIB_FAKEUSER) == 0) { id_store->id->flag &= ~LIB_FAKEUSER; } } id_store->id->us = id_store->id_us; BKE_blendfile_write_partial_tag_ID(id_store->id, false); } MEM_freeN(id_store_array); return ret; }
PyObject* _PyCode_ConstantKey(PyObject *op) { PyObject *key; /* Py_None and Py_Ellipsis are singleton */ if (op == Py_None || op == Py_Ellipsis || PyLong_CheckExact(op) || PyBool_Check(op) || PyBytes_CheckExact(op) || PyUnicode_CheckExact(op) /* code_richcompare() uses _PyCode_ConstantKey() internally */ || PyCode_Check(op)) { key = PyTuple_Pack(2, Py_TYPE(op), op); } else if (PyFloat_CheckExact(op)) { double d = PyFloat_AS_DOUBLE(op); /* all we need is to make the tuple different in either the 0.0 * or -0.0 case from all others, just to avoid the "coercion". */ if (d == 0.0 && copysign(1.0, d) < 0.0) key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); else key = PyTuple_Pack(2, Py_TYPE(op), op); } else if (PyComplex_CheckExact(op)) { Py_complex z; int real_negzero, imag_negzero; /* For the complex case we must make complex(x, 0.) different from complex(x, -0.) and complex(0., y) different from complex(-0., y), for any x and y. All four complex zeros must be distinguished.*/ z = PyComplex_AsCComplex(op); real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; /* use True, False and None singleton as tags for the real and imag * sign, to make tuples different */ if (real_negzero && imag_negzero) { key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True); } else if (imag_negzero) { key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False); } else if (real_negzero) { key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); } else { key = PyTuple_Pack(2, Py_TYPE(op), op); } } else if (PyTuple_CheckExact(op)) { Py_ssize_t i, len; PyObject *tuple; len = PyTuple_GET_SIZE(op); tuple = PyTuple_New(len); if (tuple == NULL) return NULL; for (i=0; i < len; i++) { PyObject *item, *item_key; item = PyTuple_GET_ITEM(op, i); item_key = _PyCode_ConstantKey(item); if (item_key == NULL) { Py_DECREF(tuple); return NULL; } PyTuple_SET_ITEM(tuple, i, item_key); } key = PyTuple_Pack(3, Py_TYPE(op), op, tuple); Py_DECREF(tuple); } else if (PyFrozenSet_CheckExact(op)) { Py_ssize_t pos = 0; PyObject *item; Py_hash_t hash; Py_ssize_t i, len; PyObject *tuple, *set; len = PySet_GET_SIZE(op); tuple = PyTuple_New(len); if (tuple == NULL) return NULL; i = 0; while (_PySet_NextEntry(op, &pos, &item, &hash)) { PyObject *item_key; item_key = _PyCode_ConstantKey(item); if (item_key == NULL) { Py_DECREF(tuple); return NULL; } assert(i < len); PyTuple_SET_ITEM(tuple, i, item_key); i++; } set = PyFrozenSet_New(tuple); Py_DECREF(tuple); if (set == NULL) return NULL; key = PyTuple_Pack(3, Py_TYPE(op), op, set); Py_DECREF(set); return key; } else { /* for other types, use the object identifier as a unique identifier * to ensure that they are seen as unequal. */ PyObject *obj_id = PyLong_FromVoidPtr(op); if (obj_id == NULL) return NULL; key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id); Py_DECREF(obj_id); } return key; }