bool QPython::importModule_sync(QString name) { // Lesson learned: name.toUtf8().constData() doesn't work, as the // temporary QByteArray will be destroyed after constData() has // returned, so we need to save the toUtf8() result in a local // variable that doesn't get destroyed until the function returns. QByteArray utf8bytes = name.toUtf8(); const char *moduleName = utf8bytes.constData(); ENSURE_GIL_STATE; bool use_api_10 = (api_version_major == 1 && api_version_minor == 0); PyObjectRef module; if (use_api_10) { // PyOtherSide API 1.0 behavior (star import) module = PyObjectRef(PyImport_ImportModule(moduleName), true); } else { // PyOtherSide API 1.2 behavior: "import x.y.z" PyObjectRef fromList(PyList_New(0), true); module = PyObjectRef(PyImport_ImportModuleEx(const_cast<char *>(moduleName), NULL, NULL, fromList.borrow()), true); } if (!module) { emitError(QString("Cannot import module: %1 (%2)").arg(name).arg(priv->formatExc())); return false; } if (!use_api_10) { // PyOtherSide API 1.2 behavior: "import x.y.z" // If "x.y.z" is imported, we need to set "x" in globals if (name.indexOf('.') != -1) { name = name.mid(0, name.indexOf('.')); utf8bytes = name.toUtf8(); moduleName = utf8bytes.constData(); } } PyDict_SetItemString(priv->globals.borrow(), moduleName, module.borrow()); return true; }
QDBusData QDBusData::fromQValueList(const QValueList<QDBusData>& list) { return fromList(QDBusDataList(list)); }
Buffer Readable::read(int n /*= -1*/) { auto& state = _readableState; auto nOrig = n; if (n != 0) state.emittedReadable = false; if (n == 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { if (state.length == 0 && state.ended) { endReadable(); } else { emitReadable(); } return Buffer(); } n = howMuchToRead(n, state); // if we've ended, and we're now clear, then finish it up. if (n == 0 && state.ended) { if (state.length == 0) endReadable(); return Buffer(); } // if we need a readable event, then we need to do some reading. bool doRead = state.needReadable; // if we currently have less than the highWaterMark, then also read some if (state.length == 0 || state.length - n < state.highWaterMark) { doRead = true; } // however, if we've ended, then there's no point, and if we're already // reading, then it's unnecessary. if (state.ended || state.reading) { doRead = false; } else if (doRead) { state.reading = true; state.sync = true; // if the length is currently zero, then we *need* a readable event. if (state.length == 0) state.needReadable = true; // call internal read method // this._read(state.highWaterMark); state.sync = false; // If _read pushed data synchronously, then `reading` will be false, // and we need to re-evaluate how much data we can return to the user. if (!state.reading) n = howMuchToRead(nOrig, state); } Buffer ret; if (n > 0) ret = fromList(n, state); if (ret.size() == 0) { state.needReadable = true; n = 0; } else { state.length -= n; } if (state.length == 0) { // If we have nothing in the buffer, then we want to know // as soon as we *do* get something into the buffer. if (!state.ended) state.needReadable = true; // If we tried to read() past the EOF, then emit end on the next tick. if (nOrig != n && state.ended) endReadable(); } if (ret.size() != 0) emit<const Buffer&>("data", ret); return ret; }