static PyObject * PySfPostFX_LoadFromMemory (PySfPostFX *self, PyObject *args) { char *effect; #ifdef IS_PY3K PyObject *string = PyUnicode_AsUTF8String(args); if (string == NULL) return NULL; effect = PyBytes_AsString(string); #else effect = PyString_AsString(args); #endif bool result = self->obj->LoadFromMemory(effect); #ifdef IS_PY3K Py_DECREF(string); #endif return PyBool_FromLong(result); }
static int calc_attrs_length(PyObject *attributes, int indent, int self_indent) { int attr_length = 0; int i; if (indent == -1) return -1; for (i = 0; i < PyList_Size (attributes); ++i) { PyObject *tuple, *pyvalue; PyObject *s = NULL; char *attr, *value; char *escaped; tuple = PyList_GetItem (attributes, i); if (PyTuple_GetItem(tuple, 1) == Py_None) continue; if (!PyArg_ParseTuple(tuple, "sO", &attr, &pyvalue)) return -1; if (PyUnicode_Check(pyvalue)) { s = PyUnicode_AsUTF8String(pyvalue); if (!s) { return -1; } value = PyString_AsString(s); } else if (PyString_Check(pyvalue)) { value = PyString_AsString(pyvalue); } else { PyErr_SetString(PyExc_TypeError, "value must be string or unicode"); return -1; } escaped = g_markup_escape_text (value, -1); attr_length += 2 + strlen(attr) + strlen(escaped) + 2; g_free(escaped); Py_XDECREF(s); } return attr_length + indent + self_indent; }
int statement_create(Statement* self, Connection* connection, PyObject* sql) { const char* tail; int rc; PyObject* sql_str; char* sql_cstr; self->st = NULL; self->in_use = 0; if (PyString_Check(sql)) { sql_str = sql; Py_INCREF(sql_str); } else if (PyUnicode_Check(sql)) { sql_str = PyUnicode_AsUTF8String(sql); if (!sql_str) { rc = PYSQLITE_SQL_WRONG_TYPE; return rc; } } else { rc = PYSQLITE_SQL_WRONG_TYPE; return rc; } self->in_weakreflist = NULL; self->sql = sql_str; sql_cstr = PyString_AsString(sql_str); rc = sqlite3_prepare(connection->db, sql_cstr, -1, &self->st, &tail); self->db = connection->db; if (rc == SQLITE_OK && check_remaining_sql(tail)) { (void)sqlite3_finalize(self->st); self->st = NULL; rc = PYSQLITE_TOO_MUCH_SQL; } return rc; }
//------------------------------------------------------------------------------------- StringDataDownload::StringDataDownload(PyObjectPtr objptr, const std::string & descr, int16 id): DataDownload(objptr, descr, id) { PyObject* pyobj = PyUnicode_AsUTF8String(objptr.get()); if(pyobj == NULL) { SCRIPT_ERROR_CHECK(); error_ = true; } else { totalBytes_ = (uint32)PyBytes_GET_SIZE(pyobj); stream_ = new char[totalBytes_ + 1]; memcpy(getOutStream(), PyBytes_AS_STRING(pyobj), totalBytes_); Py_DECREF(pyobj); } }
char * weechat_python_unicode_to_string (PyObject *obj) { PyObject *utf8string; char *str; str = NULL; utf8string = PyUnicode_AsUTF8String (obj); if (utf8string) { if (PyBytes_AsString (utf8string)) str = strdup (PyBytes_AsString (utf8string)); Py_XDECREF(utf8string); } return str; }
static inline void pyobj2doc_pair(PyObject *key, PyObject *value, rapidjson::Value& doc, rapidjson::Document& root) { const char *key_string; #ifdef PY3 PyObject *utf8_item; utf8_item = PyUnicode_AsUTF8String(key); key_string = PyBytes_AsString(utf8_item); #else key_string = PyString_AsString(key); #endif rapidjson::Value s; s.SetString(key_string, root.GetAllocator()); rapidjson::Value _v; pyobj2doc(value, _v, root); doc.AddMember(s, _v, root.GetAllocator()); }
static PyObject* Preprocessor_preprocess(Preprocessor* self, PyObject *args) { PyObject *f = NULL; if (!PyArg_ParseTuple(args, "|O:preprocess", &f)) return NULL; try { boost::shared_ptr<FILE> file; long fd; if (!f || f == Py_None) { fd = fileno(stdout); } else if (PyUnicode_Check(f)) { ScopedPyObject utf8_filename(PyUnicode_AsUTF8String(f)); const char *filename; if (utf8_filename && (filename = PyBytes_AsString(utf8_filename))) { file.reset(fopen(filename, "w"), &fclose); fd = fileno(file.get()); } else { return NULL; } } else { // Assume it's a file-like object with a fileno() method. PyObject *pylong = PyObject_CallMethod(f, (char*)"fileno", NULL); if (!pylong || (fd = PyLong_AsLong(pylong)) == -1) return NULL; } self->preprocessor->preprocess(fd); } catch (...) { set_python_exception(); return NULL; } Py_INCREF(Py_None); return Py_None; }
static PyObject * opencc__convert(register openccobject *dp, PyObject *args) { PyObject *str; PyObject *ret; PyObject *tmp; int is_unicode = 0; char *out = NULL; if (!PyArg_ParseTuple(args, "O:convert", &str)) return NULL; if(!dp->opencc){ PyErr_SetString(PyExc_ValueError, "ValueError: I/O operation on closed file."); return NULL; } if (PyString_Check(str)){ tmp = str; }else if (PyUnicode_Check(str)){ is_unicode = 1; tmp = PyUnicode_AsUTF8String(str); }else{ PyErr_SetString(PyExc_TypeError, "TypeError: must be string or buffer."); return NULL; } out = opencc_convert_utf8(dp->opencc, PyString_AsString(tmp), PyString_Size(tmp)); ret = PyString_FromString(out); PyMem_Free(out); if(is_unicode){ Py_DECREF(tmp); tmp = PyString_AsDecodedObject(ret, "utf8", NULL); Py_DECREF(ret); ret = tmp; } return ret; }
int PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString, const char* argumentName, const char* methodname) { // TODO: UTF-8: Does python use UTF-16? // Do we need to convert from the string charset to UTF-8 // for non-unicode data? if (PyUnicode_Check(pObject)) { // Python unicode objects are UCS2 or UCS4 depending on compilation // options, wchar_t is 16-bit or 32-bit depending on platform. // Avoid the complexity by just letting python convert the string. PyObject *utf8_pyString = PyUnicode_AsUTF8String(pObject); if (utf8_pyString) { buf = PyString_AsString(utf8_pyString); Py_DECREF(utf8_pyString); return 1; } } if (PyString_Check(pObject)) { buf = PyString_AsString(pObject); return 1; } // if we got here then we need to coerce the value to a string if (coerceToString) { PyObject* pyStrCast = PyObject_Str(pObject); if (pyStrCast) { int ret = PyXBMCGetUnicodeString(buf,pyStrCast,false,argumentName,methodname); Py_DECREF(pyStrCast); return ret; } } // Object is not a unicode or a normal string. buf = ""; PyErr_Format(PyExc_TypeError, "argument \"%s\" for method \"%s\" must be unicode or str", argumentName, methodname); return 0; }
static gboolean _check_for_unexpected_kwargs (PyGICallableCache *cache, GHashTable *arg_name_hash, PyObject *py_kwargs) { PyObject *dict_key, *dict_value; Py_ssize_t dict_iter_pos = 0; while (PyDict_Next (py_kwargs, &dict_iter_pos, &dict_key, &dict_value)) { PyObject *key; #if PY_VERSION_HEX < 0x03000000 if (PyString_Check (dict_key)) { Py_INCREF (dict_key); key = dict_key; } else #endif { key = PyUnicode_AsUTF8String (dict_key); if (key == NULL) { return FALSE; } } /* Use extended lookup because it returns whether or not the key actually * exists in the hash table. g_hash_table_lookup returns NULL for keys not * found which maps to index 0 for our hash lookup. */ if (!g_hash_table_lookup_extended (arg_name_hash, PyBytes_AsString(key), NULL, NULL)) { char *full_name = pygi_callable_cache_get_full_name (cache); PyErr_Format (PyExc_TypeError, "%.200s() got an unexpected keyword argument '%.400s'", full_name, PyBytes_AsString (key)); Py_DECREF (key); g_free (full_name); return FALSE; } Py_DECREF (key); } return TRUE; }
/* * Append a PEP3118-formatted field name, ":name:", to str */ static int _append_field_name(_tmp_string_t *str, PyObject *name) { int ret = -1; char *p; Py_ssize_t len; PyObject *tmp; #if defined(NPY_PY3K) /* FIXME: XXX -- should it use UTF-8 here? */ tmp = PyUnicode_AsUTF8String(name); #else tmp = name; Py_INCREF(tmp); #endif if (tmp == NULL || PyBytes_AsStringAndSize(tmp, &p, &len) < 0) { PyErr_Clear(); PyErr_SetString(PyExc_ValueError, "invalid field name"); goto fail; } if (_append_char(str, ':') < 0) { goto fail; } while (len > 0) { if (*p == ':') { PyErr_SetString(PyExc_ValueError, "':' is not an allowed character in buffer " "field names"); goto fail; } if (_append_char(str, *p) < 0) { goto fail; } ++p; --len; } if (_append_char(str, ':') < 0) { goto fail; } ret = 0; fail: Py_XDECREF(tmp); return ret; }
/** * Makes a C string from a Python unicode or bytes object. * * If successful, the result is a string that the caller must free(). * Else, returns NULL. */ static char *get_string(PyObject *obj) { if(PyUnicode_Check(obj)) { const char *str; PyObject *pyutf8 = PyUnicode_AsUTF8String(obj); if(pyutf8 == NULL) return NULL; #if PY_MAJOR_VERSION >= 3 str = PyBytes_AsString(pyutf8); #else str = PyString_AsString(pyutf8); #endif if(str == NULL) return NULL; { char *ret = strdup(str); Py_DECREF(pyutf8); return ret; } } else if( #if PY_MAJOR_VERSION >= 3 PyBytes_Check(obj) #else PyString_Check(obj) #endif ) { const char *str; #if PY_MAJOR_VERSION >= 3 str = PyBytes_AsString(obj); #else str = PyString_AsString(obj); #endif if(str == NULL) return NULL; return strdup(str); } else return NULL; }
int PySfDrawable_setattro(PyObject* self, PyObject *attr_name, PyObject *v) { #ifdef IS_PY3K PyObject *string = PyUnicode_AsUTF8String(attr_name); if (string == NULL) return NULL; std::string Name(PyBytes_AsString(string)); #else std::string Name(PyString_AsString(attr_name)); #endif if (Name == "Render") { Py_CLEAR(((PySfDrawable*)self)->obj->RenderFunction); Py_INCREF(v); ((PySfDrawable*)self)->obj->RenderFunction = v; } #ifdef IS_PY3K Py_DECREF(string); #endif return PyObject_GenericSetAttr(self, attr_name, v); }
static int _set_string_attr(PyObject *self, PyObject *value, const struct _alpm_str_getset *closure) { alpm_handle_t *handle = ALPM_HANDLE(self); char *path = NULL; int ret; if (PyBytes_Check(value)) { path = strdup(PyBytes_AS_STRING(value)); } else if (PyUnicode_Check(value)) { PyObject* utf8 = PyUnicode_AsUTF8String(value); path = strdup(PyBytes_AS_STRING(utf8)); Py_DECREF(utf8); } else { PyErr_SetString(PyExc_TypeError, "logfile path must be a string"); return -1; } ret = closure->setter(handle, path); free(path); if (ret == -1) RET_ERR("failed setting option value", alpm_errno(handle), -1); return 0; }
/* #<pydoc> def umsg(text): """ Prints text into IDA's Output window @param text: text to print Can be Unicode, or string in UTF-8 encoding @return: number of bytes printed """ pass #</pydoc> */ static PyObject* py_umsg(PyObject *o) { PyObject* utf8 = NULL; if ( PyUnicode_Check(o) ) { utf8 = PyUnicode_AsUTF8String(o); o = utf8; } else if ( !PyString_Check(o) ) { PyErr_SetString(PyExc_TypeError, "A unicode or UTF-8 string expected"); return NULL; } int rc; Py_BEGIN_ALLOW_THREADS; rc = umsg("%s", PyString_AsString(o)); Py_END_ALLOW_THREADS; Py_XDECREF(utf8); return PyInt_FromLong(rc); }
void populate_javascript_object(JSContext *context, JSObject *obj, PyObject *dict) { char *propname; PyObject *key, *value; Py_ssize_t pos = 0; while (PyDict_Next(dict, &pos, &key, &value)) { PyObject *str = NULL; propname = NULL; if (PyString_Check(key)) { propname = PyString_AsString(key); } else if (PyUnicode_Check(key)) { PyObject *str = PyUnicode_AsUTF8String(key); propname = PyString_AsString(str); Py_DECREF(str); } if (propname != NULL) { jsval item = to_javascript_object(context, value); JS_SetProperty(context, obj, propname, &item); } } }
CString GetPyExceptionStr() { PyObject* ptype; PyObject* pvalue; PyObject* ptraceback; PyErr_Fetch(&ptype, &pvalue, &ptraceback); CString result; if (!pvalue) { Py_INCREF(Py_None); pvalue = Py_None; } if (!ptraceback) { Py_INCREF(Py_None); ptraceback = Py_None; } PyErr_NormalizeException(&ptype, &pvalue, &ptraceback); PyObject* strlist = PyObject_CallFunctionObjArgs(m_PyFormatException, ptype, pvalue, ptraceback, NULL); Py_CLEAR(ptype); Py_CLEAR(pvalue); Py_CLEAR(ptraceback); if (!strlist) { return "Couldn't get exact error message"; } if (PySequence_Check(strlist)) { PyObject* strlist_fast = PySequence_Fast(strlist, "Shouldn't happen (1)"); PyObject** items = PySequence_Fast_ITEMS(strlist_fast); Py_ssize_t L = PySequence_Fast_GET_SIZE(strlist_fast); for (Py_ssize_t i = 0; i < L; ++i) { PyObject* utf8 = PyUnicode_AsUTF8String(items[i]); result += PyBytes_AsString(utf8); Py_CLEAR(utf8); } Py_CLEAR(strlist_fast); } else { result = "Can't get exact error message"; } Py_CLEAR(strlist); return result; }
static gboolean _pygi_marshal_from_py_filename (PyObject *py_arg, GIArgument *arg, gpointer *cleanup_data) { gchar *string_; GError *error = NULL; PyObject *tmp = NULL; if (PyUnicode_Check (py_arg)) { tmp = PyUnicode_AsUTF8String (py_arg); if (!tmp) return FALSE; string_ = PYGLIB_PyBytes_AsString (tmp); } #if PY_VERSION_HEX < 0x03000000 else if (PyString_Check (py_arg)) { string_ = PyString_AsString (py_arg); } #endif else { PyErr_Format (PyExc_TypeError, "Must be string, not %s", py_arg->ob_type->tp_name); return FALSE; } arg->v_string = g_filename_from_utf8 (string_, -1, NULL, NULL, &error); Py_XDECREF (tmp); if (arg->v_string == NULL) { PyErr_SetString (PyExc_Exception, error->message); g_error_free (error); /* TODO: Convert the error to an exception. */ return FALSE; } *cleanup_data = arg->v_string; return TRUE; }
PyObject* SolverMRP::create(PyTypeObject* pytype, PyObject* args, PyObject* kwds) { try { // Create the solver SolverMRP *s = new SolverMRP(); // Iterate over extra keywords, and set attributes. @todo move this responsibility to the readers... PyObject *key, *value; Py_ssize_t pos = 0; while (PyDict_Next(kwds, &pos, &key, &value)) { PythonData field(value); PyObject* key_utf8 = PyUnicode_AsUTF8String(key); DataKeyword attr(PyBytes_AsString(key_utf8)); Py_DECREF(key_utf8); const MetaFieldBase* fmeta = SolverMRP::metadata->findField(attr.getHash()); if (!fmeta) fmeta = Solver::metadata->findField(attr.getHash()); if (fmeta) // Update the attribute fmeta->setField(s, field); else PyErr_Format(PyExc_AttributeError, "attribute '%S' on '%s' can't be updated", key, Py_TYPE(s)->tp_name); }; // Return the object. The reference count doesn't need to be increased // as we do with other objects, because we want this object to be available // for the garbage collector of Python. return static_cast<PyObject*>(s); } catch (...) { PythonType::evalException(); return NULL; } }
void print_dict(PyObject *d) { PyObject *k; Py_ssize_t size, i =0; k = PyDict_Keys(d); size = PyList_Size(k); printf("Size : %d\n", size); for (i = 0; i < size; i++) { PyObject *item = PyList_GET_ITEM(k, i); #ifndef IS_PY3K printf("-- %s -- \n", PyString_AsString(item)); #else printf("-- %s -- \n", PyUnicode_AsUTF8String(item)); #endif } }
/* docstring in numpy.add_newdocs.py */ static PyObject * add_newdoc_ufunc(PyObject *NPY_UNUSED(dummy), PyObject *args) { PyUFuncObject *ufunc; PyObject *str; char *docstr, *newdocstr; #if defined(NPY_PY3K) if (!PyArg_ParseTuple(args, "O!O!", &PyUFunc_Type, &ufunc, &PyUnicode_Type, &str)) { return NULL; } docstr = PyBytes_AS_STRING(PyUnicode_AsUTF8String(str)); #else if (!PyArg_ParseTuple(args, "O!O!", &PyUFunc_Type, &ufunc, &PyString_Type, &str)) { return NULL; } docstr = PyString_AS_STRING(str); #endif if (NULL != ufunc->doc) { PyErr_SetString(PyExc_ValueError, "Cannot change docstring of ufunc with non-NULL docstring"); return NULL; } /* * This introduces a memory leak, as the memory allocated for the doc * will not be freed even if the ufunc itself is deleted. In practice * this should not be a problem since the user would have to * repeatedly create, document, and throw away ufuncs. */ newdocstr = malloc(strlen(docstr) + 1); strcpy(newdocstr, docstr); ufunc->doc = newdocstr; Py_RETURN_NONE; }
PyObject* OperatorDelete::create(PyTypeObject* pytype, PyObject* args, PyObject* kwds) { try { // Create the solver OperatorDelete *s = new OperatorDelete(); // Iterate over extra keywords, and set attributes. @todo move this responsibility to the readers... if (kwds) { PyObject *key, *value; Py_ssize_t pos = 0; while (PyDict_Next(kwds, &pos, &key, &value)) { PythonData field(value); PyObject* key_utf8 = PyUnicode_AsUTF8String(key); DataKeyword attr(PyBytes_AsString(key_utf8)); Py_DECREF(key_utf8); const MetaFieldBase* fmeta = OperatorDelete::metadata->findField(attr.getHash()); if (!fmeta) fmeta = Solver::metadata->findField(attr.getHash()); if (fmeta) // Update the attribute fmeta->setField(s, field); else s->setProperty(attr.getName(), value);; }; } // Return the object //Py_INCREF(s); // XXX TODO SHOULD the ref count be set to one? Or do we prevent the opbject from being garbage collected return static_cast<PyObject*>(s); } catch (...) { PythonType::evalException(); return nullptr; } }
static void construct(PyObject* obj, boost::python::converter::rvalue_from_python_stage1_data* data) { if(PyString_Check(obj)) { const char* value = PyString_AsString(obj); //MY_CHECK(value,translate("Received null string pointer from Python")); void* storage = ((boost::python::converter::rvalue_from_python_storage<std::string>*)data)->storage.bytes; new (storage) std::string(value); data->convertible = storage; } else if(PyUnicode_Check(obj)) { boost::python::handle<> utf8(boost::python::allow_null(PyUnicode_AsUTF8String(obj))); //MY_CHECK(utf8,translate("Could not convert Python unicode object to UTF8 string")); void* storage = ((boost::python::converter::rvalue_from_python_storage<std::string>*)data)->storage.bytes; const char* utf8v = PyString_AsString(utf8.get()); //MY_CHECK(utf8v,translate("Received null string from utf8 string")); new (storage) std::string(utf8v); data->convertible = storage; } else { throw std::logic_error("Unexpected type for string conversion"); } }
char* PyString_CopyAsString(PyObject* string) { PyObject* bytes; char* result; if (PyBytes_Check(string)) { bytes = string; Py_INCREF(bytes); } else { bytes = PyUnicode_AsUTF8String(string); } if (bytes == 0) return 0; result = strdup(PyBytes_AS_STRING(bytes)); Py_DECREF(bytes); if (result == 0) PyErr_NoMemory(); return result; }
static PyObject * asis_getquoted(asisObject *self, PyObject *args) { PyObject *rv; if (self->wrapped == Py_None) { Py_INCREF(psyco_null); rv = psyco_null; } else { rv = PyObject_Str(self->wrapped); #if PY_3 /* unicode to bytes in Py3 */ if (rv) { PyObject *tmp = PyUnicode_AsUTF8String(rv); Py_DECREF(rv); rv = tmp; } #endif } return rv; }
// little utility function to convert a PyObject * that we know is a string to a QString static inline QString ToQStr(PyObject *value) { if(value) { PyObject *repr = PyObject_Str(value); if(repr == NULL) return QString(); PyObject *decoded = PyUnicode_AsUTF8String(repr); if(decoded == NULL) return QString(); QString ret = QString::fromUtf8(PyBytes_AsString(decoded)); Py_DecRef(decoded); Py_DecRef(repr); return ret; } return QString(); }
QString QgsPythonUtilsImpl::getPluginMetadata( QString pluginName, QString function ) { QString command = pluginName + "." + function + "()"; QString retval = "???"; // temporary disable error hook - UI will handle this gracefully uninstallErrorHook(); PyObject* obj = PyRun_String( command.toLocal8Bit().data(), Py_eval_input, mMainDict, mMainDict ); if ( PyErr_Occurred() ) { PyErr_Print(); // just print it to console PyErr_Clear(); retval = "__error__"; } else if ( PyUnicode_Check( obj ) ) { PyObject* utf8 = PyUnicode_AsUTF8String( obj ); if ( utf8 ) retval = QString::fromUtf8( PyString_AS_STRING( utf8 ) ); else retval = "__error__"; Py_XDECREF( utf8 ); } else if ( PyString_Check( obj ) ) { retval = PyString_AS_STRING( obj ); } else { // bad python return value retval = "__error__"; } Py_XDECREF( obj ); installErrorHook(); return retval; }
/* Encode an unicode object into a bytes object in the connection encoding. * * If no connection or encoding is available, default to utf8 */ PyObject * conn_encode(connectionObject *self, PyObject *u) { PyObject *t = NULL; PyObject *rv = NULL; if (!(self && self->pyencoder)) { rv = PyUnicode_AsUTF8String(u); goto exit; } if (!(t = PyObject_CallFunctionObjArgs(self->pyencoder, u, NULL))) { goto exit; } if (!(rv = PyTuple_GetItem(t, 0))) { goto exit; } Py_INCREF(rv); exit: Py_XDECREF(t); return rv; }
const char* toCString(PyObject* str, Py_ssize_t* len) { if (str == Py_None) return NULL; #ifdef IS_PY3K if (PyUnicode_Check(str)) { if (len) { // We need to encode the unicode string into utf8 to know the size of returned char*. Shiboken::AutoDecRef uniStr(PyUnicode_AsUTF8String(str)); *len = PyBytes_GET_SIZE(uniStr.object()); } // Return unicode from str instead of uniStr, because the lifetime of the returned pointer // depends on the lifetime of str. return _PyUnicode_AsString(str); } #endif if (PyBytes_Check(str)) { if (len) *len = PyBytes_GET_SIZE(str); return PyBytes_AS_STRING(str); } return 0; }
static PyObject *PythonQtStdOutRedirect_write(PyObject *self, PyObject *args) { PythonQtStdOutRedirect* s = (PythonQtStdOutRedirect*)self; if (s->_cb) { QString output; if (PyTuple_GET_SIZE(args)>=1) { PyObject* obj = PyTuple_GET_ITEM(args,0); if (PyUnicode_Check(obj)) { #ifdef PY3K output = QString::fromUtf8(PyUnicode_AsUTF8(obj)); #else PyObject *tmp = PyUnicode_AsUTF8String(obj); if(tmp) { output = QString::fromUtf8(PyString_AS_STRING(tmp)); Py_DECREF(tmp); } else { return NULL; } #endif } else { char *string; if (!PyArg_ParseTuple(args, "s", &string)) { return NULL; } output = QString::fromLatin1(string); } } if (s->softspace > 0) { (*s->_cb)(QString("")); s->softspace = 0; } (*s->_cb)(output); } return Py_BuildValue(""); }