int PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) { PyObject *oldv; if ((l->flags & READONLY) || l->type == T_STRING) { PyErr_SetString(PyExc_AttributeError, "readonly attribute"); return -1; } if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) { PyErr_SetString(PyExc_TypeError, "can't delete numeric/char attribute"); return -1; } addr += l->offset; switch (l->type) { case T_BOOL:{ if (!PyBool_Check(v)) { PyErr_SetString(PyExc_TypeError, "attribute value type must be bool"); return -1; } if (v == Py_True) *(char*)addr = (char) 1; else *(char*)addr = (char) 0; break; } case T_BYTE:{ long long_val = PyLong_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(char*)addr = (char)long_val; /* XXX: For compatibility, only warn about truncations for now. */ if ((long_val > CHAR_MAX) || (long_val < CHAR_MIN)) WARN("Truncation of value to char"); break; } case T_UBYTE:{ long long_val = PyLong_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(unsigned char*)addr = (unsigned char)long_val; if ((long_val > UCHAR_MAX) || (long_val < 0)) WARN("Truncation of value to unsigned char"); break; } case T_SHORT:{ long long_val = PyLong_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(short*)addr = (short)long_val; if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN)) WARN("Truncation of value to short"); break; } case T_USHORT:{ long long_val = PyLong_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(unsigned short*)addr = (unsigned short)long_val; if ((long_val > USHRT_MAX) || (long_val < 0)) WARN("Truncation of value to unsigned short"); break; } case T_INT:{ long long_val = PyLong_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(int *)addr = (int)long_val; if ((long_val > INT_MAX) || (long_val < INT_MIN)) WARN("Truncation of value to int"); break; } case T_UINT:{ unsigned long ulong_val = PyLong_AsUnsignedLong(v); if ((ulong_val == (unsigned int)-1) && PyErr_Occurred()) { /* XXX: For compatibility, accept negative int values as well. */ PyErr_Clear(); ulong_val = PyLong_AsLong(v); if ((ulong_val == (unsigned int)-1) && PyErr_Occurred()) return -1; *(unsigned int *)addr = (unsigned int)ulong_val; WARN("Writing negative value into unsigned field"); } else *(unsigned int *)addr = (unsigned int)ulong_val; if (ulong_val > UINT_MAX) WARN("Truncation of value to unsigned int"); break; } case T_LONG:{ *(long*)addr = PyLong_AsLong(v); if ((*(long*)addr == -1) && PyErr_Occurred()) return -1; break; } case T_ULONG:{ *(unsigned long*)addr = PyLong_AsUnsignedLong(v); if ((*(unsigned long*)addr == (unsigned long)-1) && PyErr_Occurred()) { /* XXX: For compatibility, accept negative int values as well. */ PyErr_Clear(); *(unsigned long*)addr = PyLong_AsLong(v); if ((*(unsigned long*)addr == (unsigned int)-1) && PyErr_Occurred()) return -1; WARN("Writing negative value into unsigned field"); } break; } case T_PYSSIZET:{ *(Py_ssize_t*)addr = PyLong_AsSsize_t(v); if ((*(Py_ssize_t*)addr == (Py_ssize_t)-1) && PyErr_Occurred()) return -1; break; } case T_FLOAT:{ double double_val = PyFloat_AsDouble(v); if ((double_val == -1) && PyErr_Occurred()) return -1; *(float*)addr = (float)double_val; break; } case T_DOUBLE: *(double*)addr = PyFloat_AsDouble(v); if ((*(double*)addr == -1) && PyErr_Occurred()) return -1; break; case T_OBJECT: case T_OBJECT_EX: Py_XINCREF(v); oldv = *(PyObject **)addr; *(PyObject **)addr = v; Py_XDECREF(oldv); break; case T_CHAR: { char *string; Py_ssize_t len; if (!PyUnicode_Check(v)) { PyErr_BadArgument(); return -1; } string = _PyUnicode_AsStringAndSize(v, &len); if (len != 1) { PyErr_BadArgument(); return -1; } *(char*)addr = string[0]; break; } #ifdef HAVE_LONG_LONG case T_LONGLONG:{ PY_LONG_LONG value; *(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v); if ((value == -1) && PyErr_Occurred()) return -1; break; } case T_ULONGLONG:{ unsigned PY_LONG_LONG value; /* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong doesn't ??? */ if (PyLong_Check(v)) *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v); else *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsLong(v); if ((value == (unsigned PY_LONG_LONG)-1) && PyErr_Occurred()) return -1; break; } #endif /* HAVE_LONG_LONG */ default: PyErr_Format(PyExc_SystemError, "bad memberdescr type for %s", l->name); return -1; } return 0; }
static int _lzma_LZMADecompressor___init___impl(Decompressor *self, int format, PyObject *memlimit, PyObject *filters) /*[clinic end generated code: output=9b119f6f2cc2d7a8 input=458ca6132ef29801]*/ { const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; uint64_t memlimit_ = UINT64_MAX; lzma_ret lzret; if (memlimit != Py_None) { if (format == FORMAT_RAW) { PyErr_SetString(PyExc_ValueError, "Cannot specify memory limit with FORMAT_RAW"); return -1; } memlimit_ = PyLong_AsUnsignedLongLong(memlimit); if (PyErr_Occurred()) return -1; } if (format == FORMAT_RAW && filters == Py_None) { PyErr_SetString(PyExc_ValueError, "Must specify filters for FORMAT_RAW"); return -1; } else if (format != FORMAT_RAW && filters != Py_None) { PyErr_SetString(PyExc_ValueError, "Cannot specify filters except with FORMAT_RAW"); return -1; } self->alloc.opaque = NULL; self->alloc.alloc = PyLzma_Malloc; self->alloc.free = PyLzma_Free; self->lzs.allocator = &self->alloc; #ifdef WITH_THREAD self->lock = PyThread_allocate_lock(); if (self->lock == NULL) { PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); return -1; } #endif self->check = LZMA_CHECK_UNKNOWN; self->unused_data = PyBytes_FromStringAndSize(NULL, 0); if (self->unused_data == NULL) goto error; switch (format) { case FORMAT_AUTO: lzret = lzma_auto_decoder(&self->lzs, memlimit_, decoder_flags); if (catch_lzma_error(lzret)) break; return 0; case FORMAT_XZ: lzret = lzma_stream_decoder(&self->lzs, memlimit_, decoder_flags); if (catch_lzma_error(lzret)) break; return 0; case FORMAT_ALONE: self->check = LZMA_CHECK_NONE; lzret = lzma_alone_decoder(&self->lzs, memlimit_); if (catch_lzma_error(lzret)) break; return 0; case FORMAT_RAW: self->check = LZMA_CHECK_NONE; if (Decompressor_init_raw(&self->lzs, filters) == -1) break; return 0; default: PyErr_Format(PyExc_ValueError, "Invalid container format: %d", format); break; } error: Py_CLEAR(self->unused_data); #ifdef WITH_THREAD PyThread_free_lock(self->lock); self->lock = NULL; #endif return -1; }
/* Copies a Python int or long object to an unsigned 64-bit value * Returns 1 if successful or -1 on error */ int pyfshfs_integer_unsigned_copy_to_64bit( PyObject *integer_object, uint64_t *value_64bit, libcerror_error_t **error ) { static char *function = "pyfshfs_integer_unsigned_copy_to_64bit"; int result = 0; #if defined( HAVE_LONG_LONG ) PY_LONG_LONG long_value = 0; #else long long_value = 0; #endif if( integer_object == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid integer object.", function ); return( -1 ); } PyErr_Clear(); result = PyObject_IsInstance( integer_object, (PyObject *) &PyLong_Type ); if( result == -1 ) { pyfshfs_error_fetch( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to determine if integer object is of type long.", function ); return( -1 ); } else if( result != 0 ) { PyErr_Clear(); #if defined( HAVE_LONG_LONG ) long_value = PyLong_AsUnsignedLongLong( integer_object ); #else long_value = PyLong_AsUnsignedLong( integer_object ); #endif } #if PY_MAJOR_VERSION < 3 if( result == 0 ) { PyErr_Clear(); result = PyObject_IsInstance( integer_object, (PyObject *) &PyInt_Type ); if( result == -1 ) { pyfshfs_error_fetch( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to determine if integer object is of type int.", function ); return( -1 ); } else if( result != 0 ) { PyErr_Clear(); #if defined( HAVE_LONG_LONG ) long_value = PyInt_AsUnsignedLongLongMask( integer_object ); #else long_value = PyInt_AsUnsignedLongMask( integer_object ); #endif } } #endif /* PY_MAJOR_VERSION < 3 */ if( result == 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unsupported integer object type.", function ); return( -1 ); } if( PyErr_Occurred() ) { pyfshfs_error_fetch( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to convert integer object to long.", function ); return( -1 ); } #if defined( HAVE_LONG_LONG ) #if ( SIZEOF_LONG_LONG > 8 ) if( ( long_value < (PY_LONG_LONG) 0 ) || ( long_value > (PY_LONG_LONG) UINT64_MAX ) ) #else if( long_value < (PY_LONG_LONG) 0 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, "%s: invalid long value out of bounds.", function ); return( -1 ); } #else #if ( SIZEOF_LONG > 8 ) if( ( long_value < (long) 0 ) || ( long_value > (long) UINT64_MAX ) ) #else if( long_value < (PY_LONG_LONG) 0 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, "%s: invalid long value out of bounds.", function ); return( -1 ); } #endif *value_64bit = (uint64_t) long_value; return( 1 ); }
/** * Set one or more options in a decoder instance. * * Handled options are removed from the hash. * * @param di Decoder instance. * @param options A GHashTable of options to set. * * @return SRD_OK upon success, a (negative) error code otherwise. */ SRD_API int srd_inst_option_set(struct srd_decoder_inst *di, GHashTable *options) { PyObject *py_dec_options, *py_dec_optkeys, *py_di_options, *py_optval; PyObject *py_optlist, *py_classval; Py_UNICODE *py_ustr; unsigned long long int val_ull; int num_optkeys, ret, size, i; char *key, *value; if (!PyObject_HasAttrString(di->decoder->py_dec, "options")) { /* Decoder has no options. */ if (g_hash_table_size(options) == 0) { /* No options provided. */ return SRD_OK; } else { srd_err("Protocol decoder has no options."); return SRD_ERR_ARG; } return SRD_OK; } ret = SRD_ERR_PYTHON; key = NULL; py_dec_options = py_dec_optkeys = py_di_options = py_optval = NULL; py_optlist = py_classval = NULL; py_dec_options = PyObject_GetAttrString(di->decoder->py_dec, "options"); /* All of these are synthesized objects, so they're good. */ py_dec_optkeys = PyDict_Keys(py_dec_options); num_optkeys = PyList_Size(py_dec_optkeys); if (!(py_di_options = PyObject_GetAttrString(di->py_inst, "options"))) goto err_out; for (i = 0; i < num_optkeys; i++) { /* Get the default class value for this option. */ py_str_as_str(PyList_GetItem(py_dec_optkeys, i), &key); if (!(py_optlist = PyDict_GetItemString(py_dec_options, key))) goto err_out; if (!(py_classval = PyList_GetItem(py_optlist, 1))) goto err_out; if (!PyUnicode_Check(py_classval) && !PyLong_Check(py_classval)) { srd_err("Options of type %s are not yet supported.", Py_TYPE(py_classval)->tp_name); goto err_out; } if ((value = g_hash_table_lookup(options, key))) { /* An override for this option was provided. */ if (PyUnicode_Check(py_classval)) { if (!(py_optval = PyUnicode_FromString(value))) { /* Some UTF-8 encoding error. */ PyErr_Clear(); goto err_out; } } else if (PyLong_Check(py_classval)) { if (!(py_optval = PyLong_FromString(value, NULL, 0))) { /* ValueError Exception */ PyErr_Clear(); srd_err("Option %s has invalid value " "%s: expected integer.", key, value); goto err_out; } } g_hash_table_remove(options, key); } else { /* Use the class default for this option. */ if (PyUnicode_Check(py_classval)) { /* Make a brand new copy of the string. */ py_ustr = PyUnicode_AS_UNICODE(py_classval); size = PyUnicode_GET_SIZE(py_classval); py_optval = PyUnicode_FromUnicode(py_ustr, size); } else if (PyLong_Check(py_classval)) { /* Make a brand new copy of the integer. */ val_ull = PyLong_AsUnsignedLongLong(py_classval); if (val_ull == (unsigned long long)-1) { /* OverFlowError exception */ PyErr_Clear(); srd_err("Invalid integer value for %s: " "expected integer.", key); goto err_out; } if (!(py_optval = PyLong_FromUnsignedLongLong(val_ull))) goto err_out; } } /* * If we got here, py_optval holds a known good new reference * to the instance option to set. */ if (PyDict_SetItemString(py_di_options, key, py_optval) == -1) goto err_out; } ret = SRD_OK; err_out: Py_XDECREF(py_optlist); Py_XDECREF(py_di_options); Py_XDECREF(py_dec_optkeys); Py_XDECREF(py_dec_options); g_free(key); if (PyErr_Occurred()) srd_exception_catch("Stray exception in srd_inst_option_set()."); return ret; }
int _attachsql_QueryObject_Initialize(_attachsql_QueryObject *self, PyObject *args, PyObject *kwargs) { bool ret; char *query= NULL; int query_length; int param_count= 0; int i; int64_t *tmp_int; uint64_t *tmp_uint; double *tmp_double; attachsql_query_parameter_st *asql_params= NULL; PyObject *param_list= NULL; PyObject *param_dict= NULL; PyObject *type= NULL; PyObject *value= NULL; PyObject *is_unsigned= NULL; attachsql_error_t *error= NULL; if (!PyArg_ParseTuple(args, "s#|O!", &query, &query_length, &PyList_Type, ¶m_list)) { return -1; } if (!param_list) { ret= attachsql_query(self->pycon->conn, query_length, query, 0, NULL, &error); if (error) { _attachsql_Exception(error); return -1; } return 0; } /* For parameters the user should use: * .query("SELECT * FROM test WHERE a=? and b=?", [{'type': ESCAPE_TYPE_CHAR, 'data': 'hello'}, {'type': ESCAPE_TYPE_INT, 'data': 1}]) */ param_count= PyList_Size(param_list); asql_params= (attachsql_query_parameter_st*)malloc(sizeof(attachsql_query_parameter_st) * param_count); /* This is ugly, need to find a better way */ tmp_int= (int64_t*)malloc(sizeof(int64_t) * param_count); tmp_uint= (uint64_t*)malloc(sizeof(uint64_t) * param_count); tmp_double= (double*)malloc(sizeof(double) * param_count); for (i= 0; i < param_count; i++) { param_dict= PyList_GetItem(param_list, i); if (!PyDict_Check(param_dict)) { PyErr_SetString(PyExc_TypeError, "Dict not found in list"); free(asql_params); return -1; } type= PyDict_GetItemString(param_dict, "type"); value= PyDict_GetItemString(param_dict, "data"); is_unsigned= PyDict_GetItemString(param_dict, "is_unsigned"); if (!type || !value || !PyInt_Check(type)) { PyErr_SetString(PyExc_TypeError, "Bad type or value in dict"); free(asql_params); return -1; } asql_params[i].type= PyInt_AsLong(type); switch (asql_params[i].type) { case ATTACHSQL_ESCAPE_TYPE_NONE: asql_params[i].data= NULL; break; case ATTACHSQL_ESCAPE_TYPE_CHAR: case ATTACHSQL_ESCAPE_TYPE_CHAR_LIKE: asql_params[i].data= PyString_AsString(value); asql_params[i].length= PyString_Size(value); break; case ATTACHSQL_ESCAPE_TYPE_INT: if (is_unsigned && PyInt_AsLong(is_unsigned)) { if (PyInt_Check(value)) { tmp_uint[i]= PyInt_AsUnsignedLongMask(value); } else { tmp_uint[i]= PyLong_AsUnsignedLong(value); } asql_params[i].data= &tmp_uint[i]; asql_params[i].is_unsigned= true; } else { if (PyInt_Check(value)) { tmp_int[i]= PyInt_AsLong(value); } else { tmp_int[i]= PyLong_AsLong(value); } asql_params[i].data= &tmp_int[i]; asql_params[i].is_unsigned= false; } break; case ATTACHSQL_ESCAPE_TYPE_BIGINT: if (is_unsigned && PyInt_AsLong(is_unsigned)) { if (PyInt_Check(value)) { tmp_uint[i]= PyInt_AsUnsignedLongMask(value); } else { tmp_uint[i]= PyLong_AsUnsignedLongLong(value); } asql_params[i].data= &tmp_uint[i]; asql_params[i].is_unsigned= true; } else { if (PyInt_Check(value)) { tmp_int[i]= PyInt_AsLong(value); } else { tmp_int[i]= PyLong_AsLongLong(value); } asql_params[i].data= &tmp_int[i]; asql_params[i].is_unsigned= false; } break; case ATTACHSQL_ESCAPE_TYPE_FLOAT: case ATTACHSQL_ESCAPE_TYPE_DOUBLE: tmp_double[i]= PyFloat_AsDouble(value); asql_params[i].data= &tmp_double[i]; break; } } ret= attachsql_query(self->pycon->conn, query_length, query, param_count, asql_params, &error); free(asql_params); free(tmp_int); free(tmp_uint); free(tmp_double); if (error) { _attachsql_Exception(error); return -1; } return 0; }
static void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc, JSONObjectEncoder *enc) { PyObject *obj, *objRepr, *exc; TypeContext *pc; PRINTMARK(); if (!_obj) { tc->type = JT_INVALID; return; } obj = (PyObject*) _obj; tc->prv = PyObject_Malloc(sizeof(TypeContext)); pc = (TypeContext *) tc->prv; if (!pc) { tc->type = JT_INVALID; PyErr_NoMemory(); return; } pc->newObj = NULL; pc->dictObj = NULL; pc->itemValue = NULL; pc->itemName = NULL; pc->iterator = NULL; pc->attrList = NULL; pc->index = 0; pc->size = 0; pc->longValue = 0; pc->rawJSONValue = NULL; if (PyIter_Check(obj)) { PRINTMARK(); goto ISITERABLE; } if (PyBool_Check(obj)) { PRINTMARK(); tc->type = (obj == Py_True) ? JT_TRUE : JT_FALSE; return; } else if (PyLong_Check(obj)) { PRINTMARK(); pc->PyTypeToJSON = PyLongToINT64; tc->type = JT_LONG; GET_TC(tc)->longValue = PyLong_AsLongLong(obj); exc = PyErr_Occurred(); if (!exc) { return; } if (exc && PyErr_ExceptionMatches(PyExc_OverflowError)) { PyErr_Clear(); pc->PyTypeToJSON = PyLongToUINT64; tc->type = JT_ULONG; GET_TC(tc)->unsignedLongValue = PyLong_AsUnsignedLongLong(obj); exc = PyErr_Occurred(); if (exc && PyErr_ExceptionMatches(PyExc_OverflowError)) { PRINTMARK(); goto INVALID; } } return; } else if (PyInt_Check(obj)) { PRINTMARK(); #ifdef _LP64 pc->PyTypeToJSON = PyIntToINT64; tc->type = JT_LONG; #else pc->PyTypeToJSON = PyIntToINT32; tc->type = JT_INT; #endif return; } else if (PyString_Check(obj)) { PRINTMARK(); pc->PyTypeToJSON = PyStringToUTF8; tc->type = JT_UTF8; return; } else if (PyUnicode_Check(obj)) { PRINTMARK(); pc->PyTypeToJSON = PyUnicodeToUTF8; tc->type = JT_UTF8; return; } else if (PyFloat_Check(obj) || (type_decimal && PyObject_IsInstance(obj, type_decimal))) { PRINTMARK(); pc->PyTypeToJSON = PyFloatToDOUBLE; tc->type = JT_DOUBLE; return; } else if (obj == Py_None) { PRINTMARK(); tc->type = JT_NULL; return; } ISITERABLE: if (PyDict_Check(obj)) { PRINTMARK(); tc->type = JT_OBJECT; SetupDictIter(obj, pc, enc); Py_INCREF(obj); return; } else if (PyList_Check(obj)) { PRINTMARK(); tc->type = JT_ARRAY; pc->iterEnd = List_iterEnd; pc->iterNext = List_iterNext; pc->iterGetValue = List_iterGetValue; pc->iterGetName = List_iterGetName; GET_TC(tc)->index = 0; GET_TC(tc)->size = PyList_GET_SIZE( (PyObject *) obj); return; } else if (PyTuple_Check(obj)) { PRINTMARK(); tc->type = JT_ARRAY; pc->iterEnd = Tuple_iterEnd; pc->iterNext = Tuple_iterNext; pc->iterGetValue = Tuple_iterGetValue; pc->iterGetName = Tuple_iterGetName; GET_TC(tc)->index = 0; GET_TC(tc)->size = PyTuple_GET_SIZE( (PyObject *) obj); GET_TC(tc)->itemValue = NULL; return; } if (UNLIKELY(PyObject_HasAttrString(obj, "toDict"))) { PyObject* toDictFunc = PyObject_GetAttrString(obj, "toDict"); PyObject* tuple = PyTuple_New(0); PyObject* toDictResult = PyObject_Call(toDictFunc, tuple, NULL); Py_DECREF(tuple); Py_DECREF(toDictFunc); if (toDictResult == NULL) { goto INVALID; } if (!PyDict_Check(toDictResult)) { Py_DECREF(toDictResult); tc->type = JT_NULL; return; } PRINTMARK(); tc->type = JT_OBJECT; SetupDictIter(toDictResult, pc, enc); return; } else if (UNLIKELY(PyObject_HasAttrString(obj, "__json__"))) { PyObject* toJSONFunc = PyObject_GetAttrString(obj, "__json__"); PyObject* tuple = PyTuple_New(0); PyObject* toJSONResult = PyObject_Call(toJSONFunc, tuple, NULL); Py_DECREF(tuple); Py_DECREF(toJSONFunc); if (toJSONResult == NULL) { goto INVALID; } if (PyErr_Occurred()) { Py_DECREF(toJSONResult); goto INVALID; } if (!PyString_Check(toJSONResult) && !PyUnicode_Check(toJSONResult)) { Py_DECREF(toJSONResult); PyErr_Format (PyExc_TypeError, "expected string"); goto INVALID; } PRINTMARK(); pc->PyTypeToJSON = PyRawJSONToUTF8; tc->type = JT_RAW; GET_TC(tc)->rawJSONValue = toJSONResult; return; } PRINTMARK(); PyErr_Clear(); objRepr = PyObject_Repr(obj); PyErr_Format (PyExc_TypeError, "%s is not JSON serializable", PyString_AS_STRING(objRepr)); Py_DECREF(objRepr); INVALID: PRINTMARK(); tc->type = JT_INVALID; PyObject_Free(tc->prv); tc->prv = NULL; return; }
std::shared_ptr<MI::MIValue> Py2MI(PyObject* pyValue, MI_Type valueType) { if (pyValue == Py_None) { return std::make_shared<MI::MIValue>(valueType); } if (PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&PyBool_Type))) { return MI::MIValue::FromBoolean(PyObject_IsTrue(pyValue) ? MI_TRUE : MI_FALSE); } else if (PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&PyLong_Type))) { switch (valueType) { case MI_BOOLEAN: return MI::MIValue::FromBoolean(PyLong_AsLong(pyValue) != 0); case MI_UINT8: return MI::MIValue::FromUint8((MI_Uint8)PyLong_AsUnsignedLong(pyValue)); case MI_SINT8: return MI::MIValue::FromSint8((MI_Sint8)PyLong_AsLong(pyValue)); case MI_UINT16: return MI::MIValue::FromUint16((MI_Uint16)PyLong_AsUnsignedLong(pyValue)); case MI_SINT16: return MI::MIValue::FromSint16((MI_Sint16)PyLong_AsLong(pyValue)); case MI_CHAR16: return MI::MIValue::FromChar16((MI_Char16)PyLong_AsLong(pyValue)); case MI_UINT32: return MI::MIValue::FromUint32(PyLong_AsUnsignedLong(pyValue)); case MI_SINT32: return MI::MIValue::FromSint32(PyLong_AsLong(pyValue)); case MI_UINT64: return MI::MIValue::FromUint64(PyLong_AsUnsignedLongLong(pyValue)); case MI_SINT64: return MI::MIValue::FromSint64(PyLong_AsLongLong(pyValue)); case MI_REAL32: return MI::MIValue::FromReal32((MI_Real32)PyLong_AsDouble(pyValue)); case MI_REAL64: return MI::MIValue::FromReal64(PyLong_AsDouble(pyValue)); case MI_STRING: return Py2StrMIValue(pyValue); default: throw MI::TypeConversionException(); } } #ifndef IS_PY3K else if (PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&PyInt_Type))) { switch (valueType) { case MI_BOOLEAN: return MI::MIValue::FromBoolean(PyInt_AsLong(pyValue) != 0); case MI_UINT8: return MI::MIValue::FromUint8((MI_Uint8)PyInt_AsLong(pyValue)); case MI_SINT8: return MI::MIValue::FromSint8((MI_Sint8)PyInt_AsLong(pyValue)); case MI_UINT16: return MI::MIValue::FromUint16((MI_Uint16)PyInt_AsLong(pyValue)); case MI_SINT16: return MI::MIValue::FromSint16((MI_Sint16)PyInt_AsLong(pyValue)); case MI_CHAR16: return MI::MIValue::FromChar16((MI_Char16)PyLong_AsLong(pyValue)); case MI_UINT32: return MI::MIValue::FromUint32((MI_Uint32)PyInt_AsLong(pyValue)); case MI_SINT32: return MI::MIValue::FromSint32((MI_Sint32)PyInt_AsLong(pyValue)); case MI_UINT64: return MI::MIValue::FromUint64((MI_Uint64)PyInt_AsLong(pyValue)); case MI_SINT64: return MI::MIValue::FromSint64((MI_Sint64)PyInt_AsLong(pyValue)); case MI_REAL32: return MI::MIValue::FromReal32((MI_Real32)PyInt_AsLong(pyValue)); case MI_REAL64: return MI::MIValue::FromReal64((MI_Real64)PyInt_AsLong(pyValue)); case MI_STRING: return Py2StrMIValue(pyValue); default: throw MI::TypeConversionException(); } } else if (PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&PyString_Type))) { switch (valueType) { case MI_STRING: return MI::MIValue::FromString(std::string(PyString_AsString(pyValue))); case MI_SINT8: case MI_UINT8: case MI_SINT16: case MI_UINT16: case MI_CHAR16: case MI_SINT32: case MI_UINT32: case MI_SINT64: case MI_UINT64: case MI_REAL32: case MI_REAL64: return Str2PyLong2MI(PyString_AsString(pyValue), valueType); default: throw MI::TypeConversionException(); } } #endif else if (PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&PyUnicode_Type))) { switch (valueType) { case MI_STRING: return MI::MIValue::FromString(Py2WString(pyValue)); case MI_SINT8: case MI_UINT8: case MI_SINT16: case MI_UINT16: case MI_CHAR16: case MI_SINT32: case MI_UINT32: case MI_SINT64: case MI_UINT64: case MI_REAL32: case MI_REAL64: { auto str = Py2WString(pyValue); std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> cv; return Str2PyLong2MI((char*)cv.to_bytes(str).c_str(), valueType); } default: throw MI::TypeConversionException(); } } else if (PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&InstanceType))) { switch (valueType) { case MI_INSTANCE: // TODO: Set the same ScopeContextOwner as the container instance / class return MI::MIValue::FromInstance(*((Instance*)pyValue)->instance); case MI_REFERENCE: // TODO: Set the same ScopeContextOwner as the container instance / class return MI::MIValue::FromReference(*((Instance*)pyValue)->instance); default: throw MI::TypeConversionException(); } } else if (PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&PyTuple_Type)) || PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&PyList_Type))) { bool isTuple = PyObject_IsInstance(pyValue, reinterpret_cast<PyObject*>(&PyTuple_Type)) != 0; Py_ssize_t size = 0; if (isTuple) { size = PyTuple_Size(pyValue); } else { size = PyList_Size(pyValue); } MI_Type itemType = (MI_Type)(valueType ^ MI_ARRAY); auto value = MI::MIValue::CreateArray((unsigned)size, valueType); for (Py_ssize_t i = 0; i < size; i++) { PyObject* pyObj = NULL; if (isTuple) { pyObj = PyTuple_GetItem(pyValue, i); } else { pyObj = PyList_GetItem(pyValue, i); } auto& tmpValue = Py2MI(pyObj, itemType); value->SetArrayItem(*tmpValue, (unsigned)i); } return value; } else { switch (valueType) { case MI_STRING: return Py2StrMIValue(pyValue); default: throw MI::TypeConversionException(); } } }
void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc, JSONObjectEncoder *enc) { PyObject *obj, *exc, *toDictFunc, *iter; TypeContext *pc; PRINTMARK(); if (!_obj) { tc->type = JT_INVALID; return; } obj = (PyObject*) _obj; tc->prv = PyObject_Malloc(sizeof(TypeContext)); pc = (TypeContext *) tc->prv; if (!pc) { tc->type = JT_INVALID; PyErr_NoMemory(); return; } pc->newObj = NULL; pc->dictObj = NULL; pc->itemValue = NULL; pc->itemName = NULL; pc->iterator = NULL; pc->attrList = NULL; pc->index = 0; pc->size = 0; pc->longValue = 0; if (PyIter_Check(obj)) { PRINTMARK(); goto ISITERABLE; } if (PyBool_Check(obj)) { PRINTMARK(); tc->type = (obj == Py_True) ? JT_TRUE : JT_FALSE; return; } else if (PyLong_Check(obj)) { PRINTMARK(); pc->PyTypeToJSON = PyLongToINT64; tc->type = JT_LONG; GET_TC(tc)->longValue = PyLong_AsLongLong(obj); exc = PyErr_Occurred(); if (!exc) { return; } if (exc && PyErr_ExceptionMatches(PyExc_OverflowError)) { PyErr_Clear(); pc->PyTypeToJSON = PyLongToUINT64; tc->type = JT_ULONG; GET_TC(tc)->unsignedLongValue = PyLong_AsUnsignedLongLong(obj); exc = PyErr_Occurred(); if (exc && PyErr_ExceptionMatches(PyExc_OverflowError)) { PRINTMARK(); goto INVALID; } } return; } else if (PyInt_Check(obj)) { PRINTMARK(); #ifdef _LP64 pc->PyTypeToJSON = PyIntToINT64; tc->type = JT_LONG; #else pc->PyTypeToJSON = PyIntToINT32; tc->type = JT_INT; #endif return; } else if (PyString_Check(obj)) { PRINTMARK(); pc->PyTypeToJSON = PyStringToUTF8; tc->type = JT_UTF8; return; } else if (PyUnicode_Check(obj)) { PRINTMARK(); pc->PyTypeToJSON = PyUnicodeToUTF8; tc->type = JT_UTF8; return; } else if (PyFloat_Check(obj) || (type_decimal && PyObject_IsInstance(obj, type_decimal))) { PRINTMARK(); pc->PyTypeToJSON = PyFloatToDOUBLE; tc->type = JT_DOUBLE; return; } else if (PyDateTime_Check(obj)) { PRINTMARK(); pc->PyTypeToJSON = PyDateTimeToINT64; tc->type = JT_LONG; return; } else if (PyDate_Check(obj)) { PRINTMARK(); pc->PyTypeToJSON = PyDateToINT64; tc->type = JT_LONG; return; } else if (obj == Py_None) { PRINTMARK(); tc->type = JT_NULL; return; } ISITERABLE: if (PyDict_Check(obj)) { PRINTMARK(); tc->type = JT_OBJECT; SetupDictIter(obj, pc, enc); Py_INCREF(obj); return; } else if (PyList_Check(obj)) { PRINTMARK(); tc->type = JT_ARRAY; pc->iterEnd = List_iterEnd; pc->iterNext = List_iterNext; pc->iterGetValue = List_iterGetValue; pc->iterGetName = List_iterGetName; GET_TC(tc)->index = 0; GET_TC(tc)->size = PyList_GET_SIZE( (PyObject *) obj); return; } else if (PyTuple_Check(obj)) { PRINTMARK(); tc->type = JT_ARRAY; pc->iterEnd = Tuple_iterEnd; pc->iterNext = Tuple_iterNext; pc->iterGetValue = Tuple_iterGetValue; pc->iterGetName = Tuple_iterGetName; GET_TC(tc)->index = 0; GET_TC(tc)->size = PyTuple_GET_SIZE( (PyObject *) obj); GET_TC(tc)->itemValue = NULL; return; } /* else if (PyAnySet_Check(obj)) { PRINTMARK(); tc->type = JT_ARRAY; pc->iterBegin = NULL; pc->iterEnd = Iter_iterEnd; pc->iterNext = Iter_iterNext; pc->iterGetValue = Iter_iterGetValue; pc->iterGetName = Iter_iterGetName; return; } */ toDictFunc = PyObject_GetAttrString(obj, "toDict"); if (toDictFunc) { PyObject* tuple = PyTuple_New(0); PyObject* toDictResult = PyObject_Call(toDictFunc, tuple, NULL); Py_DECREF(tuple); Py_DECREF(toDictFunc); if (toDictResult == NULL) { PyErr_Clear(); tc->type = JT_NULL; return; } if (!PyDict_Check(toDictResult)) { Py_DECREF(toDictResult); tc->type = JT_NULL; return; } PRINTMARK(); tc->type = JT_OBJECT; SetupDictIter(toDictResult, pc, enc); return; } PRINTMARK(); PyErr_Clear(); iter = PyObject_GetIter(obj); if (iter != NULL) { PRINTMARK(); tc->type = JT_ARRAY; pc->iterator = iter; pc->iterEnd = Iter_iterEnd; pc->iterNext = Iter_iterNext; pc->iterGetValue = Iter_iterGetValue; pc->iterGetName = Iter_iterGetName; return; } PRINTMARK(); PyErr_Clear(); PRINTMARK(); tc->type = JT_OBJECT; GET_TC(tc)->attrList = PyObject_Dir(obj); if (GET_TC(tc)->attrList == NULL) { PyErr_Clear(); goto INVALID; } GET_TC(tc)->index = 0; GET_TC(tc)->size = PyList_GET_SIZE(GET_TC(tc)->attrList); PRINTMARK(); pc->iterEnd = Dir_iterEnd; pc->iterNext = Dir_iterNext; pc->iterGetValue = Dir_iterGetValue; pc->iterGetName = Dir_iterGetName; return; INVALID: PRINTMARK(); tc->type = JT_INVALID; PyObject_Free(tc->prv); tc->prv = NULL; return; }
int PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) { PyObject *oldv; addr += l->offset; if ((l->flags & READONLY)) { PyErr_SetString(PyExc_TypeError, "readonly attribute"); return -1; } if ((l->flags & PY_WRITE_RESTRICTED) && PyEval_GetRestricted()) { PyErr_SetString(PyExc_RuntimeError, "restricted attribute"); return -1; } if (v == NULL) { if (l->type == T_OBJECT_EX) { /* Check if the attribute is set. */ if (*(PyObject **)addr == NULL) { PyErr_SetString(PyExc_AttributeError, l->name); return -1; } } else if (l->type != T_OBJECT) { PyErr_SetString(PyExc_TypeError, "can't delete numeric/char attribute"); return -1; } } switch (l->type) { case T_BOOL:{ if (!PyBool_Check(v)) { PyErr_SetString(PyExc_TypeError, "attribute value type must be bool"); return -1; } if (v == Py_True) *(char*)addr = (char) 1; else *(char*)addr = (char) 0; break; } case T_BYTE:{ long long_val = PyInt_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(char*)addr = (char)long_val; /* XXX: For compatibility, only warn about truncations for now. */ if ((long_val > CHAR_MAX) || (long_val < CHAR_MIN)) WARN("Truncation of value to char"); break; } case T_UBYTE:{ long long_val = PyInt_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(unsigned char*)addr = (unsigned char)long_val; if ((long_val > UCHAR_MAX) || (long_val < 0)) WARN("Truncation of value to unsigned char"); break; } case T_SHORT:{ long long_val = PyInt_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(short*)addr = (short)long_val; if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN)) WARN("Truncation of value to short"); break; } case T_USHORT:{ long long_val = PyInt_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(unsigned short*)addr = (unsigned short)long_val; if ((long_val > USHRT_MAX) || (long_val < 0)) WARN("Truncation of value to unsigned short"); break; } case T_INT:{ long long_val = PyInt_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) return -1; *(int *)addr = (int)long_val; if ((long_val > INT_MAX) || (long_val < INT_MIN)) WARN("Truncation of value to int"); break; } case T_UINT:{ unsigned long ulong_val = PyLong_AsUnsignedLong(v); if ((ulong_val == (unsigned long)-1) && PyErr_Occurred()) { /* XXX: For compatibility, accept negative int values as well. */ PyErr_Clear(); ulong_val = PyLong_AsLong(v); if ((ulong_val == (unsigned long)-1) && PyErr_Occurred()) return -1; *(unsigned int *)addr = (unsigned int)ulong_val; WARN("Writing negative value into unsigned field"); } else *(unsigned int *)addr = (unsigned int)ulong_val; if (ulong_val > UINT_MAX) WARN("Truncation of value to unsigned int"); break; } case T_LONG:{ *(long*)addr = PyLong_AsLong(v); if ((*(long*)addr == -1) && PyErr_Occurred()) return -1; break; } case T_ULONG:{ *(unsigned long*)addr = PyLong_AsUnsignedLong(v); if ((*(unsigned long*)addr == (unsigned long)-1) && PyErr_Occurred()) { /* XXX: For compatibility, accept negative int values as well. */ PyErr_Clear(); *(unsigned long*)addr = PyLong_AsLong(v); if ((*(unsigned long*)addr == (unsigned long)-1) && PyErr_Occurred()) return -1; WARN("Writing negative value into unsigned field"); } break; } case T_PYSSIZET:{ *(Py_ssize_t*)addr = PyInt_AsSsize_t(v); if ((*(Py_ssize_t*)addr == (Py_ssize_t)-1) && PyErr_Occurred()) return -1; break; } case T_FLOAT:{ double double_val = PyFloat_AsDouble(v); if ((double_val == -1) && PyErr_Occurred()) return -1; *(float*)addr = (float)double_val; break; } case T_DOUBLE: *(double*)addr = PyFloat_AsDouble(v); if ((*(double*)addr == -1) && PyErr_Occurred()) return -1; break; case T_OBJECT: case T_OBJECT_EX: Py_XINCREF(v); oldv = *(PyObject **)addr; *(PyObject **)addr = v; // Todo: Maybe here is a smarter update policy possible via index-by-address-lookup updateJyGCHeadLinks((PyObject*) (addr-l->offset), AS_JY(addr-l->offset)); Py_XDECREF(oldv); break; case T_CHAR: if (PyString_Check(v) && PyString_Size(v) == 1) { *(char*)addr = PyString_AsString(v)[0]; } else { PyErr_BadArgument(); return -1; } break; case T_STRING: case T_STRING_INPLACE: PyErr_SetString(PyExc_TypeError, "readonly attribute"); return -1; #ifdef HAVE_LONG_LONG case T_LONGLONG:{ PY_LONG_LONG value; *(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v); if ((value == -1) && PyErr_Occurred()) return -1; break; } case T_ULONGLONG:{ unsigned PY_LONG_LONG value; /* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong doesn't ??? */ if (PyLong_Check(v)) *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v); else *(unsigned PY_LONG_LONG*)addr = value = PyInt_AsLong(v); if ((value == (unsigned PY_LONG_LONG)-1) && PyErr_Occurred()) return -1; break; } #endif /* HAVE_LONG_LONG */ default: PyErr_Format(PyExc_SystemError, "bad memberdescr type for %s", l->name); return -1; } return 0; }
//------------------------------------------------------------------------- // Parses a Python object as a long or long long bool PyW_GetNumber(PyObject *py_var, uint64 *num, bool *is_64 = NULL) { if ( !(PyInt_CheckExact(py_var) || PyLong_CheckExact(py_var)) ) return false; // Can we convert to C long? long l = PyInt_AsLong(py_var); if ( !PyErr_Occurred() ) { if ( num != NULL ) *num = uint64(l); if ( is_64 != NULL ) *is_64 = false; return true; } // Clear last error PyErr_Clear(); // Can be fit into a C unsigned long? unsigned long ul = PyLong_AsUnsignedLong(py_var); if ( !PyErr_Occurred() ) { if ( num != NULL ) *num = uint64(ul); if ( is_64 != NULL ) *is_64 = false; return true; } PyErr_Clear(); // Try to parse as int64 PY_LONG_LONG ll = PyLong_AsLongLong(py_var); if ( !PyErr_Occurred() ) { if ( num != NULL ) *num = uint64(ll); if ( is_64 != NULL ) *is_64 = true; return true; } PyErr_Clear(); // Try to parse as uint64 unsigned PY_LONG_LONG ull = PyLong_AsUnsignedLongLong(py_var); PyObject *err = PyErr_Occurred(); if ( err == NULL ) { if ( num != NULL ) *num = uint64(ull); if ( is_64 != NULL ) *is_64 = true; return true; } // Negative number? _And_ it with uint64(-1) bool ok = false; if ( err == PyExc_TypeError ) { PyObject *py_mask = Py_BuildValue("K", 0xFFFFFFFFFFFFFFFFull); PyObject *py_num = PyNumber_And(py_var, py_mask); if ( py_num != NULL && py_mask != NULL ) { PyErr_Clear(); ull = PyLong_AsUnsignedLongLong(py_num); if ( !PyErr_Occurred() ) { if ( num != NULL ) *num = uint64(ull); if ( is_64 != NULL ) *is_64 = true; ok = true; } } Py_XDECREF(py_num); Py_XDECREF(py_mask); } PyErr_Clear(); return ok; }
static PyObject * object2attr(PyObject *value, kdump_attr_ref_t *ref, kdump_attr_t *attr) { unsigned PY_LONG_LONG num; PyObject *conv; attr->type = value ? kdump_attr_ref_type(ref) : kdump_nil; conv = value; switch (attr->type) { case kdump_nil: /* used for deletions */ break; case kdump_directory: /* TODO: We may want to enforce a specific type or even * a specific value for directory instantiation. */ break; case kdump_number: case kdump_address: if (PyLong_Check(value)) { num = PyLong_AsUnsignedLongLong(value); if (PyErr_Occurred()) return NULL; } else if (PyInt_Check(value)) { num = PyInt_AsLong(value); if (PyErr_Occurred()) return NULL; } else { PyErr_Format(PyExc_TypeError, "need an integer, not %.200s", Py_TYPE(value)->tp_name); return NULL; } if (attr->type == kdump_number) attr->val.number = num; else attr->val.address = num; break; case kdump_string: if (!PyString_Check(value)) { conv = PyObject_Str(value); if (!conv) return NULL; } if (! (attr->val.string = PyString_AsString(conv)) ) return NULL; break; default: PyErr_SetString(PyExc_TypeError, "assignment to an unknown type"); return NULL; } return conv; }
// Convert a Python object to a QVariant. bool Chimera::fromPyObject(PyObject *py, QVariant *var, bool strict) const { // Deal with the simple case of wrapping the Python object rather than // converting it. if (_type != sipType_QVariant && _metatype == PyQt_PyObject::metatype) { // If the type was specified by a Python type (as opposed to // 'PyQt_PyObject') then check the object is an instance of it. if (_py_type && !PyObject_IsInstance(py, _py_type)) return false; *var = keep_as_pyobject(py); return true; } // Let any registered convertors have a go first. for (int i = 0; i < _registered_QVariant_convertors.count(); ++i) { bool ok; if (_registered_QVariant_convertors.at(i)(py, var, &ok)) return ok; } int iserr = 0, value_class_state; void *ptr_class, *value_class = 0; // Temporary storage for different types. union { bool tmp_bool; int tmp_int; unsigned int tmp_unsigned_int; double tmp_double; void *tmp_void_ptr; long tmp_long; qlonglong tmp_qlonglong; short tmp_short; char tmp_char; unsigned long tmp_unsigned_long; qulonglong tmp_qulonglong; unsigned short tmp_unsigned_short; unsigned char tmp_unsigned_char; float tmp_float; } tmp_storage; void *variant_data = &tmp_storage; PyErr_Clear(); QVariant variant; int metatype_used = _metatype; switch (_metatype) { case QMetaType::Bool: tmp_storage.tmp_bool = PyLong_AsLong(py); break; case QMetaType::Int: if (!_inexact || isEnum() || isFlag()) { // It must fit into a C++ int. #if PY_MAJOR_VERSION >= 3 tmp_storage.tmp_int = PyLong_AsLong(py); #else tmp_storage.tmp_int = PyInt_AsLong(py); #endif } else { // Fit it into the smallest C++ type we can. qlonglong qll = PyLong_AsLongLong(py); if (PyErr_Occurred()) { // Try again in case the value is unsigned and will fit with // the extra bit. PyErr_Clear(); qulonglong qull = static_cast<qulonglong>(PyLong_AsUnsignedLongLong(py)); if (PyErr_Occurred()) { // It won't fit into any C++ type so pass it as a Python // object. PyErr_Clear(); *var = keep_as_pyobject(py); metatype_used = QMetaType::Void; } else { tmp_storage.tmp_qulonglong = qull; metatype_used = QMetaType::ULongLong; } } else if ((qlonglong)(int)qll == qll) { // It fits in a C++ int. tmp_storage.tmp_int = qll; } else if ((qulonglong)(unsigned int)qll == (qulonglong)qll) { // The extra bit is enough for it to fit. tmp_storage.tmp_unsigned_int = qll; metatype_used = QMetaType::UInt; } else { // This fits. tmp_storage.tmp_qlonglong = qll; metatype_used = QMetaType::LongLong; } } break; case QMetaType::UInt: tmp_storage.tmp_unsigned_int = sipLong_AsUnsignedLong(py); break; case QMetaType::Double: tmp_storage.tmp_double = PyFloat_AsDouble(py); break; case QMetaType::VoidStar: tmp_storage.tmp_void_ptr = sipConvertToVoidPtr(py); break; case QMetaType::Long: tmp_storage.tmp_long = PyLong_AsLong(py); break; case QMetaType::LongLong: tmp_storage.tmp_qlonglong = PyLong_AsLongLong(py); break; case QMetaType::Short: tmp_storage.tmp_short = PyLong_AsLong(py); break; case QMetaType::Char: if (SIPBytes_Check(py) && SIPBytes_GET_SIZE(py) == 1) tmp_storage.tmp_char = *SIPBytes_AS_STRING(py); else iserr = 1; break; case QMetaType::ULong: tmp_storage.tmp_unsigned_long = sipLong_AsUnsignedLong(py); break; case QMetaType::ULongLong: tmp_storage.tmp_qulonglong = static_cast<qulonglong>(PyLong_AsUnsignedLongLong(py)); break; case QMetaType::UShort: tmp_storage.tmp_unsigned_short = sipLong_AsUnsignedLong(py); break; case QMetaType::UChar: if (SIPBytes_Check(py) && SIPBytes_GET_SIZE(py) == 1) tmp_storage.tmp_unsigned_char = *SIPBytes_AS_STRING(py); else iserr = 1; break; case QMetaType::Float: tmp_storage.tmp_float = PyFloat_AsDouble(py); break; case QMetaType::QObjectStar: tmp_storage.tmp_void_ptr = sipForceConvertToType(py, sipType_QObject, 0, SIP_NO_CONVERTORS, 0, &iserr); break; case QMetaType::QWidgetStar: if (sipType_QWidget) { tmp_storage.tmp_void_ptr = sipForceConvertToType(py, sipType_QWidget, 0, SIP_NO_CONVERTORS, 0, &iserr); } else { iserr = 1; } break; case QMetaType::QVariantList: { QVariantList ql; if (to_QVariantList(py, ql)) { *var = QVariant(ql); metatype_used = QMetaType::Void; } else { iserr = 1; } break; } case QMetaType::QVariantMap: { QVariantMap qm; if (to_QVariantMap(py, qm)) { *var = QVariant(qm); metatype_used = QMetaType::Void; } else if (strict) { iserr = 1; } else { // Assume the failure is because the key was the wrong type. PyErr_Clear(); *var = keep_as_pyobject(py); metatype_used = QMetaType::Void; } break; } #if QT_VERSION >= 0x040500 case QMetaType::QVariantHash: { QVariantHash qh; if (to_QVariantHash(py, qh)) { *var = QVariant(qh); metatype_used = QMetaType::Void; } else { iserr = 1; } break; } #endif case -1: metatype_used = QMetaType::VoidStar; if (SIPBytes_Check(py)) tmp_storage.tmp_void_ptr = SIPBytes_AS_STRING(py); else if (py == Py_None) tmp_storage.tmp_void_ptr = 0; else iserr = 1; break; default: if (_type) { if (_name.endsWith('*')) { ptr_class = sipForceConvertToType(py, _type, 0, SIP_NO_CONVERTORS, 0, &iserr); variant_data = &ptr_class; } else { value_class = sipForceConvertToType(py, _type, 0, SIP_NOT_NONE, &value_class_state, &iserr); variant_data = value_class; } } else { // This is a class we don't recognise. iserr = 1; } } if (iserr || PyErr_Occurred()) { PyErr_Format(PyExc_TypeError, "unable to convert a Python '%s' object to a C++ '%s' instance", Py_TYPE(py)->tp_name, _name.constData()); iserr = 1; } else if (_type == sipType_QVariant) { *var = QVariant(*reinterpret_cast<QVariant *>(variant_data)); } else if (metatype_used != QMetaType::Void) { *var = QVariant(metatype_used, variant_data); } // Release any temporary value-class instance now that QVariant will have // made a copy. if (value_class) sipReleaseType(value_class, _type, value_class_state); return (iserr == 0); }
// Convert a Python object to C++ at a given address. This has a lot in common // with the method that converts to a QVariant. However, unlike that method, // we have no control over the size of the destination storage and so must // convert exactly as requested. bool Chimera::fromPyObject(PyObject *py, void *cpp) const { // Let any registered convertors have a go first. for (int i = 0; i < _registered_QVariant_data_convertors.count(); ++i) { bool ok; if (_registered_QVariant_data_convertors.at(i)(py, cpp, _metatype, &ok)) return ok; } int iserr = 0; PyErr_Clear(); switch (_metatype) { case QMetaType::Bool: *reinterpret_cast<bool *>(cpp) = PyLong_AsLong(py); break; case QMetaType::Int: // Truncate it if necessary to fit into a C++ int. This will // automatically handle enums and flag types as Python knows how to // convert them to ints. #if PY_MAJOR_VERSION >= 3 *reinterpret_cast<int *>(cpp) = PyLong_AsLong(py); #else *reinterpret_cast<int *>(cpp) = PyInt_AsLong(py); #endif break; case QMetaType::UInt: *reinterpret_cast<unsigned int *>(cpp) = sipLong_AsUnsignedLong(py); break; case QMetaType::Double: *reinterpret_cast<double *>(cpp) = PyFloat_AsDouble(py); break; case QMetaType::VoidStar: *reinterpret_cast<void **>(cpp) = sipConvertToVoidPtr(py); break; case QMetaType::Long: *reinterpret_cast<long *>(cpp) = PyLong_AsLong(py); break; case QMetaType::LongLong: *reinterpret_cast<qlonglong *>(cpp) = PyLong_AsLongLong(py); break; case QMetaType::Short: *reinterpret_cast<short *>(cpp) = PyLong_AsLong(py); break; case QMetaType::Char: if (SIPBytes_Check(py) && SIPBytes_GET_SIZE(py) == 1) *reinterpret_cast<char *>(cpp) = *SIPBytes_AS_STRING(py); else iserr = 1; break; case QMetaType::ULong: *reinterpret_cast<unsigned long *>(cpp) = sipLong_AsUnsignedLong(py); break; case QMetaType::ULongLong: *reinterpret_cast<qulonglong *>(cpp) = static_cast<qulonglong>(PyLong_AsUnsignedLongLong(py)); break; case QMetaType::UShort: *reinterpret_cast<unsigned short *>(cpp) = sipLong_AsUnsignedLong(py); break; case QMetaType::UChar: if (SIPBytes_Check(py) && SIPBytes_GET_SIZE(py) == 1) *reinterpret_cast<unsigned char *>(cpp) = *SIPBytes_AS_STRING(py); else iserr = 1; break; case QMetaType::Float: *reinterpret_cast<float *>(cpp) = PyFloat_AsDouble(py); break; case QMetaType::QObjectStar: *reinterpret_cast<void **>(cpp) = sipForceConvertToType(py, sipType_QObject, 0, SIP_NO_CONVERTORS, 0, &iserr); break; case QMetaType::QWidgetStar: if (sipType_QWidget) { *reinterpret_cast<void **>(cpp) = sipForceConvertToType(py, sipType_QWidget, 0, SIP_NO_CONVERTORS, 0, &iserr); } else { iserr = 1; } break; case QMetaType::QVariantList: { QVariantList ql; if (to_QVariantList(py, ql)) *reinterpret_cast<QVariantList *>(cpp) = ql; else iserr = 1; break; } case QMetaType::QVariantMap: { QVariantMap qm; if (to_QVariantMap(py, qm)) *reinterpret_cast<QVariantMap *>(cpp) = qm; else iserr = 1; break; } #if QT_VERSION >= 0x040500 case QMetaType::QVariantHash: { QVariantHash qh; if (to_QVariantHash(py, qh)) *reinterpret_cast<QVariantHash *>(cpp) = qh; else iserr = 1; break; } #endif case -1: { char **ptr = reinterpret_cast<char **>(cpp); if (SIPBytes_Check(py)) *ptr = SIPBytes_AS_STRING(py); else if (py == Py_None) *ptr = 0; else iserr = 1; break; } default: if (_type) { if (_name.endsWith('*')) { // This must be a pointer-type. *reinterpret_cast<void **>(cpp) = sipForceConvertToType(py, _type, 0, SIP_NO_CONVERTORS, 0, &iserr); } else { // This must be a value-type. sipAssignFunc assign = get_assign_helper(); if (assign) { int state; void *value_class; value_class = sipForceConvertToType(py, _type, 0, SIP_NOT_NONE, &state, &iserr); if (!iserr) assign(cpp, 0, value_class); sipReleaseType(value_class, _type, state); } else { iserr = 1; } } } else { iserr = 1; } } if (iserr || PyErr_Occurred()) { PyErr_Format(PyExc_TypeError, "unable to convert a Python '%s' object to a C++ '%s' instance", Py_TYPE(py)->tp_name, _name.constData()); return false; } return true; }
static int __Pyx_ParseOptionalKeywords( PyObject *kwds, PyObject **argnames[], PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, const char* function_name) { PyObject *key = 0, *value = 0; Py_ssize_t pos = 0; PyObject*** name; PyObject*** first_kw_arg = argnames + num_pos_args; while (PyDict_Next(kwds, &pos, &key, &value)) { name = first_kw_arg; while (*name && (**name != key)) name++; if (*name) { values[name-argnames] = value; } else { #if PY_MAJOR_VERSION < 3 if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) { #else if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) { #endif goto invalid_keyword_type; } else { for (name = first_kw_arg; *name; name++) { #if PY_MAJOR_VERSION >= 3 if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) && PyUnicode_Compare(**name, key) == 0) break; #else if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) && _PyString_Eq(**name, key)) break; #endif } if (*name) { values[name-argnames] = value; } else { /* unexpected keyword found */ for (name=argnames; name != first_kw_arg; name++) { if (**name == key) goto arg_passed_twice; #if PY_MAJOR_VERSION >= 3 if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) && PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice; #else if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) && _PyString_Eq(**name, key)) goto arg_passed_twice; #endif } if (kwds2) { if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad; } else { goto invalid_keyword; } } } } } return 0; arg_passed_twice: __Pyx_RaiseDoubleKeywordsError(function_name, **name); goto bad; invalid_keyword_type: PyErr_Format(PyExc_TypeError, "%s() keywords must be strings", function_name); goto bad; invalid_keyword: PyErr_Format(PyExc_TypeError, #if PY_MAJOR_VERSION < 3 "%s() got an unexpected keyword argument '%s'", function_name, PyString_AsString(key)); #else "%s() got an unexpected keyword argument '%U'", function_name, key); #endif bad: return -1; } static CYTHON_INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject* x) { const unsigned char neg_one = (unsigned char)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(unsigned char) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(unsigned char)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to unsigned char" : "value too large to convert to unsigned char"); } return (unsigned char)-1; } return (unsigned char)val; } return (unsigned char)__Pyx_PyInt_AsUnsignedLong(x); } static CYTHON_INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject* x) { const unsigned short neg_one = (unsigned short)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(unsigned short) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(unsigned short)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to unsigned short" : "value too large to convert to unsigned short"); } return (unsigned short)-1; } return (unsigned short)val; } return (unsigned short)__Pyx_PyInt_AsUnsignedLong(x); } static CYTHON_INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject* x) { const unsigned int neg_one = (unsigned int)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(unsigned int) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(unsigned int)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to unsigned int" : "value too large to convert to unsigned int"); } return (unsigned int)-1; } return (unsigned int)val; } return (unsigned int)__Pyx_PyInt_AsUnsignedLong(x); } static CYTHON_INLINE char __Pyx_PyInt_AsChar(PyObject* x) { const char neg_one = (char)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(char) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(char)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to char" : "value too large to convert to char"); } return (char)-1; } return (char)val; } return (char)__Pyx_PyInt_AsLong(x); } static CYTHON_INLINE short __Pyx_PyInt_AsShort(PyObject* x) { const short neg_one = (short)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(short) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(short)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to short" : "value too large to convert to short"); } return (short)-1; } return (short)val; } return (short)__Pyx_PyInt_AsLong(x); } static CYTHON_INLINE int __Pyx_PyInt_AsInt(PyObject* x) { const int neg_one = (int)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(int) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(int)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to int" : "value too large to convert to int"); } return (int)-1; } return (int)val; } return (int)__Pyx_PyInt_AsLong(x); } static CYTHON_INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject* x) { const signed char neg_one = (signed char)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(signed char) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(signed char)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to signed char" : "value too large to convert to signed char"); } return (signed char)-1; } return (signed char)val; } return (signed char)__Pyx_PyInt_AsSignedLong(x); } static CYTHON_INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject* x) { const signed short neg_one = (signed short)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(signed short) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(signed short)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to signed short" : "value too large to convert to signed short"); } return (signed short)-1; } return (signed short)val; } return (signed short)__Pyx_PyInt_AsSignedLong(x); } static CYTHON_INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject* x) { const signed int neg_one = (signed int)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; if (sizeof(signed int) < sizeof(long)) { long val = __Pyx_PyInt_AsLong(x); if (unlikely(val != (long)(signed int)val)) { if (!unlikely(val == -1 && PyErr_Occurred())) { PyErr_SetString(PyExc_OverflowError, (is_unsigned && unlikely(val < 0)) ? "can't convert negative value to signed int" : "value too large to convert to signed int"); } return (signed int)-1; } return (signed int)val; } return (signed int)__Pyx_PyInt_AsSignedLong(x); } static CYTHON_INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject* x) { const unsigned long neg_one = (unsigned long)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; #if PY_VERSION_HEX < 0x03000000 if (likely(PyInt_Check(x))) { long val = PyInt_AS_LONG(x); if (is_unsigned && unlikely(val < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to unsigned long"); return (unsigned long)-1; } return (unsigned long)val; } else #endif if (likely(PyLong_Check(x))) { if (is_unsigned) { if (unlikely(Py_SIZE(x) < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to unsigned long"); return (unsigned long)-1; } return PyLong_AsUnsignedLong(x); } else { return PyLong_AsLong(x); } } else { unsigned long val; PyObject *tmp = __Pyx_PyNumber_Int(x); if (!tmp) return (unsigned long)-1; val = __Pyx_PyInt_AsUnsignedLong(tmp); Py_DECREF(tmp); return val; } } static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject* x) { const unsigned PY_LONG_LONG neg_one = (unsigned PY_LONG_LONG)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; #if PY_VERSION_HEX < 0x03000000 if (likely(PyInt_Check(x))) { long val = PyInt_AS_LONG(x); if (is_unsigned && unlikely(val < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to unsigned PY_LONG_LONG"); return (unsigned PY_LONG_LONG)-1; } return (unsigned PY_LONG_LONG)val; } else #endif if (likely(PyLong_Check(x))) { if (is_unsigned) { if (unlikely(Py_SIZE(x) < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to unsigned PY_LONG_LONG"); return (unsigned PY_LONG_LONG)-1; } return PyLong_AsUnsignedLongLong(x); } else { return PyLong_AsLongLong(x); } } else { unsigned PY_LONG_LONG val; PyObject *tmp = __Pyx_PyNumber_Int(x); if (!tmp) return (unsigned PY_LONG_LONG)-1; val = __Pyx_PyInt_AsUnsignedLongLong(tmp); Py_DECREF(tmp); return val; } } static CYTHON_INLINE long __Pyx_PyInt_AsLong(PyObject* x) { const long neg_one = (long)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; #if PY_VERSION_HEX < 0x03000000 if (likely(PyInt_Check(x))) { long val = PyInt_AS_LONG(x); if (is_unsigned && unlikely(val < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to long"); return (long)-1; } return (long)val; } else #endif if (likely(PyLong_Check(x))) { if (is_unsigned) { if (unlikely(Py_SIZE(x) < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to long"); return (long)-1; } return PyLong_AsUnsignedLong(x); } else { return PyLong_AsLong(x); } } else { long val; PyObject *tmp = __Pyx_PyNumber_Int(x); if (!tmp) return (long)-1; val = __Pyx_PyInt_AsLong(tmp); Py_DECREF(tmp); return val; } } static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(PyObject* x) { const PY_LONG_LONG neg_one = (PY_LONG_LONG)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; #if PY_VERSION_HEX < 0x03000000 if (likely(PyInt_Check(x))) { long val = PyInt_AS_LONG(x); if (is_unsigned && unlikely(val < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to PY_LONG_LONG"); return (PY_LONG_LONG)-1; } return (PY_LONG_LONG)val; } else #endif if (likely(PyLong_Check(x))) { if (is_unsigned) { if (unlikely(Py_SIZE(x) < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to PY_LONG_LONG"); return (PY_LONG_LONG)-1; } return PyLong_AsUnsignedLongLong(x); } else { return PyLong_AsLongLong(x); } } else { PY_LONG_LONG val; PyObject *tmp = __Pyx_PyNumber_Int(x); if (!tmp) return (PY_LONG_LONG)-1; val = __Pyx_PyInt_AsLongLong(tmp); Py_DECREF(tmp); return val; } } static CYTHON_INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject* x) { const signed long neg_one = (signed long)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; #if PY_VERSION_HEX < 0x03000000 if (likely(PyInt_Check(x))) { long val = PyInt_AS_LONG(x); if (is_unsigned && unlikely(val < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to signed long"); return (signed long)-1; } return (signed long)val; } else #endif if (likely(PyLong_Check(x))) { if (is_unsigned) { if (unlikely(Py_SIZE(x) < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to signed long"); return (signed long)-1; } return PyLong_AsUnsignedLong(x); } else { return PyLong_AsLong(x); } } else { signed long val; PyObject *tmp = __Pyx_PyNumber_Int(x); if (!tmp) return (signed long)-1; val = __Pyx_PyInt_AsSignedLong(tmp); Py_DECREF(tmp); return val; } } static CYTHON_INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject* x) { const signed PY_LONG_LONG neg_one = (signed PY_LONG_LONG)-1, const_zero = 0; const int is_unsigned = neg_one > const_zero; #if PY_VERSION_HEX < 0x03000000 if (likely(PyInt_Check(x))) { long val = PyInt_AS_LONG(x); if (is_unsigned && unlikely(val < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to signed PY_LONG_LONG"); return (signed PY_LONG_LONG)-1; } return (signed PY_LONG_LONG)val; } else #endif if (likely(PyLong_Check(x))) { if (is_unsigned) { if (unlikely(Py_SIZE(x) < 0)) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to signed PY_LONG_LONG"); return (signed PY_LONG_LONG)-1; } return PyLong_AsUnsignedLongLong(x); } else { return PyLong_AsLongLong(x); } } else { signed PY_LONG_LONG val; PyObject *tmp = __Pyx_PyNumber_Int(x); if (!tmp) return (signed PY_LONG_LONG)-1; val = __Pyx_PyInt_AsSignedLongLong(tmp); Py_DECREF(tmp); return val; } } #include "compile.h" #include "frameobject.h" #include "traceback.h" static void __Pyx_AddTraceback(const char *funcname) { PyObject *py_srcfile = 0; PyObject *py_funcname = 0; PyObject *py_globals = 0; PyCodeObject *py_code = 0; PyFrameObject *py_frame = 0; #if PY_MAJOR_VERSION < 3 py_srcfile = PyString_FromString(__pyx_filename); #else py_srcfile = PyUnicode_FromString(__pyx_filename); #endif if (!py_srcfile) goto bad; if (__pyx_clineno) { #if PY_MAJOR_VERSION < 3 py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno); #else py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno); #endif } else { #if PY_MAJOR_VERSION < 3 py_funcname = PyString_FromString(funcname); #else py_funcname = PyUnicode_FromString(funcname); #endif } if (!py_funcname) goto bad; py_globals = PyModule_GetDict(__pyx_m); if (!py_globals) goto bad; py_code = PyCode_New( 0, /*int argcount,*/ #if PY_MAJOR_VERSION >= 3 0, /*int kwonlyargcount,*/ #endif 0, /*int nlocals,*/ 0, /*int stacksize,*/ 0, /*int flags,*/ __pyx_empty_bytes, /*PyObject *code,*/ __pyx_empty_tuple, /*PyObject *consts,*/ __pyx_empty_tuple, /*PyObject *names,*/ __pyx_empty_tuple, /*PyObject *varnames,*/ __pyx_empty_tuple, /*PyObject *freevars,*/ __pyx_empty_tuple, /*PyObject *cellvars,*/ py_srcfile, /*PyObject *filename,*/ py_funcname, /*PyObject *name,*/ __pyx_lineno, /*int firstlineno,*/ __pyx_empty_bytes /*PyObject *lnotab*/ ); if (!py_code) goto bad; py_frame = PyFrame_New( PyThreadState_GET(), /*PyThreadState *tstate,*/ py_code, /*PyCodeObject *code,*/ py_globals, /*PyObject *globals,*/ 0 /*PyObject *locals*/ ); if (!py_frame) goto bad; py_frame->f_lineno = __pyx_lineno; PyTraceBack_Here(py_frame); bad: Py_XDECREF(py_srcfile); Py_XDECREF(py_funcname); Py_XDECREF(py_code); Py_XDECREF(py_frame); }
static PyObject * archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw) { static char *keywords[] = { "start_pc", "end_pc", "count", NULL }; CORE_ADDR start, end = 0; CORE_ADDR pc; gdb_py_ulongest start_temp; long count = 0, i; PyObject *result_list, *end_obj = NULL, *count_obj = NULL; struct gdbarch *gdbarch = NULL; ARCHPY_REQUIRE_VALID (self, gdbarch); if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords, &start_temp, &end_obj, &count_obj)) return NULL; start = start_temp; if (end_obj) { if (PyLong_Check (end_obj)) end = PyLong_AsUnsignedLongLong (end_obj); else if (PyInt_Check (end_obj)) /* If the end_pc value is specified without a trailing 'L', end_obj will be an integer and not a long integer. */ end = PyInt_AsLong (end_obj); else { Py_DECREF (end_obj); Py_XDECREF (count_obj); PyErr_SetString (PyExc_TypeError, _("Argument 'end_pc' should be a (long) integer.")); return NULL; } if (end < start) { Py_DECREF (end_obj); Py_XDECREF (count_obj); PyErr_SetString (PyExc_ValueError, _("Argument 'end_pc' should be greater than or " "equal to the argument 'start_pc'.")); return NULL; } } if (count_obj) { count = PyInt_AsLong (count_obj); if (PyErr_Occurred () || count < 0) { Py_DECREF (count_obj); Py_XDECREF (end_obj); PyErr_SetString (PyExc_TypeError, _("Argument 'count' should be an non-negative " "integer.")); return NULL; } } result_list = PyList_New (0); if (result_list == NULL) return NULL; for (pc = start, i = 0; /* All args are specified. */ (end_obj && count_obj && pc <= end && i < count) /* end_pc is specified, but no count. */ || (end_obj && count_obj == NULL && pc <= end) /* end_pc is not specified, but a count is. */ || (end_obj == NULL && count_obj && i < count) /* Both end_pc and count are not specified. */ || (end_obj == NULL && count_obj == NULL && pc == start);) { int insn_len = 0; char *as = NULL; struct ui_file *memfile = mem_fileopen (); PyObject *insn_dict = PyDict_New (); volatile struct gdb_exception except; if (insn_dict == NULL) { Py_DECREF (result_list); ui_file_delete (memfile); return NULL; } if (PyList_Append (result_list, insn_dict)) { Py_DECREF (result_list); Py_DECREF (insn_dict); ui_file_delete (memfile); return NULL; /* PyList_Append Sets the exception. */ } TRY_CATCH (except, RETURN_MASK_ALL) { insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL); } if (except.reason < 0) { Py_DECREF (result_list); ui_file_delete (memfile); gdbpy_convert_exception (except); return NULL; } as = ui_file_xstrdup (memfile, NULL); if (PyDict_SetItemString (insn_dict, "addr", gdb_py_long_from_ulongest (pc)) || PyDict_SetItemString (insn_dict, "asm", PyString_FromString (*as ? as : "<unknown>")) || PyDict_SetItemString (insn_dict, "length", PyInt_FromLong (insn_len))) { Py_DECREF (result_list); ui_file_delete (memfile); xfree (as); return NULL; } pc += insn_len; i++; ui_file_delete (memfile); xfree (as); }
/** * pyg_value_from_pyobject: * @value: the GValue object to store the converted value in. * @obj: the Python object to convert. * * This function converts a Python object and stores the result in a * GValue. The GValue must be initialised in advance with * g_value_init(). If the Python object can't be converted to the * type of the GValue, then an error is returned. * * Returns: 0 on success, -1 on error. */ int pyg_value_from_pyobject(GValue *value, PyObject *obj) { PyObject *tmp; switch (G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value))) { case G_TYPE_INTERFACE: /* we only handle interface types that have a GObject prereq */ if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT)) { if (obj == Py_None) g_value_set_object(value, NULL); else { if (!PyObject_TypeCheck(obj, &PyGObject_Type)) { return -1; } if (!G_TYPE_CHECK_INSTANCE_TYPE(pygobject_get(obj), G_VALUE_TYPE(value))) { return -1; } g_value_set_object(value, pygobject_get(obj)); } } else { return -1; } break; case G_TYPE_CHAR: #if PY_VERSION_HEX < 0x03000000 if (PyString_Check(obj)) { g_value_set_char(value, PyString_AsString(obj)[0]); } else #endif if (PyUnicode_Check(obj)) { tmp = PyUnicode_AsUTF8String(obj); g_value_set_char(value, PYGLIB_PyBytes_AsString(tmp)[0]); Py_DECREF(tmp); } else { PyErr_Clear(); return -1; } break; case G_TYPE_UCHAR: if (PYGLIB_PyLong_Check(obj)) { glong val; val = PYGLIB_PyLong_AsLong(obj); if (val >= 0 && val <= 255) g_value_set_uchar(value, (guchar)PYGLIB_PyLong_AsLong (obj)); else return -1; #if PY_VERSION_HEX < 0x03000000 } else if (PyString_Check(obj)) { g_value_set_uchar(value, PyString_AsString(obj)[0]); #endif } else if (PyUnicode_Check(obj)) { tmp = PyUnicode_AsUTF8String(obj); g_value_set_uchar(value, PYGLIB_PyBytes_AsString(tmp)[0]); Py_DECREF(tmp); } else { PyErr_Clear(); return -1; } break; case G_TYPE_BOOLEAN: g_value_set_boolean(value, PyObject_IsTrue(obj)); break; case G_TYPE_INT: g_value_set_int(value, PYGLIB_PyLong_AsLong(obj)); break; case G_TYPE_UINT: { if (PYGLIB_PyLong_Check(obj)) { glong val; val = PYGLIB_PyLong_AsLong(obj); if (val >= 0 && val <= G_MAXUINT) g_value_set_uint(value, (guint)val); else return -1; } else { g_value_set_uint(value, PyLong_AsUnsignedLong(obj)); } } break; case G_TYPE_LONG: g_value_set_long(value, PYGLIB_PyLong_AsLong(obj)); break; case G_TYPE_ULONG: #if PY_VERSION_HEX < 0x03000000 if (PyInt_Check(obj)) { long val; val = PYGLIB_PyLong_AsLong(obj); if (val < 0) { PyErr_SetString(PyExc_OverflowError, "negative value not allowed for uint64 property"); return -1; } g_value_set_ulong(value, (gulong)val); } else #endif if (PyLong_Check(obj)) g_value_set_ulong(value, PyLong_AsUnsignedLong(obj)); else return -1; break; case G_TYPE_INT64: g_value_set_int64(value, PyLong_AsLongLong(obj)); break; case G_TYPE_UINT64: #if PY_VERSION_HEX < 0x03000000 if (PyInt_Check(obj)) { long v = PyInt_AsLong(obj); if (v < 0) { PyErr_SetString(PyExc_OverflowError, "negative value not allowed for uint64 property"); return -1; } g_value_set_uint64(value, v); } else #endif if (PyLong_Check(obj)) g_value_set_uint64(value, PyLong_AsUnsignedLongLong(obj)); else return -1; break; case G_TYPE_ENUM: { gint val = 0; if (pyg_enum_get_value(G_VALUE_TYPE(value), obj, &val) < 0) { PyErr_Clear(); return -1; } g_value_set_enum(value, val); } break; case G_TYPE_FLAGS: { gint val = 0; if (pyg_flags_get_value(G_VALUE_TYPE(value), obj, &val) < 0) { PyErr_Clear(); return -1; } g_value_set_flags(value, val); } break; case G_TYPE_FLOAT: g_value_set_float(value, PyFloat_AsDouble(obj)); break; case G_TYPE_DOUBLE: g_value_set_double(value, PyFloat_AsDouble(obj)); break; case G_TYPE_STRING: if (obj == Py_None) { g_value_set_string(value, NULL); #if PY_VERSION_HEX < 0x03000000 } else if (PyString_Check(obj)) { g_value_set_string(value, PyString_AsString(obj)); #endif } else if (PyUnicode_Check(obj)) { tmp = PyUnicode_AsUTF8String(obj); g_value_set_string(value, PYGLIB_PyBytes_AsString(tmp)); Py_DECREF(tmp); } else { PyErr_Clear(); return -1; } break; case G_TYPE_POINTER: if (obj == Py_None) g_value_set_pointer(value, NULL); else if (PyObject_TypeCheck(obj, &PyGPointer_Type) && G_VALUE_HOLDS(value, ((PyGPointer *)obj)->gtype)) g_value_set_pointer(value, pyg_pointer_get(obj, gpointer)); else if (PYGLIB_CPointer_Check(obj)) g_value_set_pointer(value, PYGLIB_CPointer_GetPointer(obj, NULL)); else return -1; break; case G_TYPE_BOXED: { PyGTypeMarshal *bm; if (obj == Py_None) g_value_set_boxed(value, NULL); else if (G_VALUE_HOLDS(value, PY_TYPE_OBJECT)) g_value_set_boxed(value, obj); else if (PyObject_TypeCheck(obj, &PyGBoxed_Type) && G_VALUE_HOLDS(value, ((PyGBoxed *)obj)->gtype)) g_value_set_boxed(value, pyg_boxed_get(obj, gpointer)); else if (G_VALUE_HOLDS(value, G_TYPE_VALUE)) { GType type; GValue *n_value; type = pyg_type_from_object((PyObject*)Py_TYPE(obj)); if (G_UNLIKELY (! type)) { PyErr_Clear(); return -1; } n_value = g_new0 (GValue, 1); g_value_init (n_value, type); g_value_take_boxed (value, n_value); return pyg_value_from_pyobject (n_value, obj); } else if (PySequence_Check(obj) && G_VALUE_HOLDS(value, G_TYPE_VALUE_ARRAY)) return pyg_value_array_from_pyobject(value, obj, NULL); else if (PYGLIB_PyUnicode_Check(obj) && G_VALUE_HOLDS(value, G_TYPE_GSTRING)) { GString *string; char *buffer; Py_ssize_t len; if (PYGLIB_PyUnicode_AsStringAndSize(obj, &buffer, &len)) return -1; string = g_string_new_len(buffer, len); g_value_set_boxed(value, string); g_string_free (string, TRUE); break; } else if ((bm = pyg_type_lookup(G_VALUE_TYPE(value))) != NULL) return bm->tovalue(value, obj); else if (PYGLIB_CPointer_Check(obj)) g_value_set_boxed(value, PYGLIB_CPointer_GetPointer(obj, NULL)); else return -1; break; } case G_TYPE_PARAM: if (PyGParamSpec_Check(obj)) g_value_set_param(value, PYGLIB_CPointer_GetPointer(obj, NULL)); else return -1; break; case G_TYPE_OBJECT: if (obj == Py_None) { g_value_set_object(value, NULL); } else if (PyObject_TypeCheck(obj, &PyGObject_Type) && G_TYPE_CHECK_INSTANCE_TYPE(pygobject_get(obj), G_VALUE_TYPE(value))) { g_value_set_object(value, pygobject_get(obj)); } else return -1; break; default: { PyGTypeMarshal *bm; if ((bm = pyg_type_lookup(G_VALUE_TYPE(value))) != NULL) return bm->tovalue(value, obj); break; } } if (PyErr_Occurred()) { g_value_unset(value); PyErr_Clear(); return -1; } return 0; }
static PyObject * py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size, Py_buffer *key, Py_buffer *salt, Py_buffer *person, int fanout, int depth, PyObject *leaf_size_obj, PyObject *node_offset_obj, int node_depth, int inner_size, int last_node) /*[clinic end generated code: output=fe060b258a8cbfc6 input=458cfdcb3d0d47ff]*/ { BLAKE2sObject *self = NULL; Py_buffer buf; unsigned long leaf_size = 0; unsigned long long node_offset = 0; self = new_BLAKE2sObject(type); if (self == NULL) { goto error; } /* Zero parameter block. */ memset(&self->param, 0, sizeof(self->param)); /* Set digest size. */ if (digest_size <= 0 || digest_size > BLAKE2S_OUTBYTES) { PyErr_Format(PyExc_ValueError, "digest_size must be between 1 and %d bytes", BLAKE2S_OUTBYTES); goto error; } self->param.digest_length = digest_size; /* Set salt parameter. */ if ((salt->obj != NULL) && salt->len) { if (salt->len > BLAKE2S_SALTBYTES) { PyErr_Format(PyExc_ValueError, "maximum salt length is %d bytes", BLAKE2S_SALTBYTES); goto error; } memcpy(self->param.salt, salt->buf, salt->len); } /* Set personalization parameter. */ if ((person->obj != NULL) && person->len) { if (person->len > BLAKE2S_PERSONALBYTES) { PyErr_Format(PyExc_ValueError, "maximum person length is %d bytes", BLAKE2S_PERSONALBYTES); goto error; } memcpy(self->param.personal, person->buf, person->len); } /* Set tree parameters. */ if (fanout < 0 || fanout > 255) { PyErr_SetString(PyExc_ValueError, "fanout must be between 0 and 255"); goto error; } self->param.fanout = (uint8_t)fanout; if (depth <= 0 || depth > 255) { PyErr_SetString(PyExc_ValueError, "depth must be between 1 and 255"); goto error; } self->param.depth = (uint8_t)depth; if (leaf_size_obj != NULL) { leaf_size = PyLong_AsUnsignedLong(leaf_size_obj); if (leaf_size == (unsigned long) -1 && PyErr_Occurred()) { goto error; } if (leaf_size > 0xFFFFFFFFU) { PyErr_SetString(PyExc_OverflowError, "leaf_size is too large"); goto error; } } // NB: Simple assignment here would be incorrect on big endian platforms. store32(&(self->param.leaf_length), leaf_size); if (node_offset_obj != NULL) { node_offset = PyLong_AsUnsignedLongLong(node_offset_obj); if (node_offset == (unsigned long long) -1 && PyErr_Occurred()) { goto error; } } #ifdef HAVE_BLAKE2S if (node_offset > 0xFFFFFFFFFFFFULL) { /* maximum 2**48 - 1 */ PyErr_SetString(PyExc_OverflowError, "node_offset is too large"); goto error; } store48(&(self->param.node_offset), node_offset); #else // NB: Simple assignment here would be incorrect on big endian platforms. store64(&(self->param.node_offset), node_offset); #endif if (node_depth < 0 || node_depth > 255) { PyErr_SetString(PyExc_ValueError, "node_depth must be between 0 and 255"); goto error; } self->param.node_depth = node_depth; if (inner_size < 0 || inner_size > BLAKE2S_OUTBYTES) { PyErr_Format(PyExc_ValueError, "inner_size must be between 0 and is %d", BLAKE2S_OUTBYTES); goto error; } self->param.inner_length = inner_size; /* Set key length. */ if ((key->obj != NULL) && key->len) { if (key->len > BLAKE2S_KEYBYTES) { PyErr_Format(PyExc_ValueError, "maximum key length is %d bytes", BLAKE2S_KEYBYTES); goto error; } self->param.key_length = (uint8_t)key->len; } /* Initialize hash state. */ if (blake2s_init_param(&self->state, &self->param) < 0) { PyErr_SetString(PyExc_RuntimeError, "error initializing hash state"); goto error; } /* Set last node flag (must come after initialization). */ self->state.last_node = last_node; /* Process key block if any. */ if (self->param.key_length) { uint8_t block[BLAKE2S_BLOCKBYTES]; memset(block, 0, sizeof(block)); memcpy(block, key->buf, key->len); blake2s_update(&self->state, block, sizeof(block)); secure_zero_memory(block, sizeof(block)); } /* Process initial data if any. */ if (data != NULL) { GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error); if (buf.len >= HASHLIB_GIL_MINSIZE) { Py_BEGIN_ALLOW_THREADS blake2s_update(&self->state, buf.buf, buf.len); Py_END_ALLOW_THREADS } else {
static int pygvfinfo_setattr(PyGnomeVFSFileInfo *self, const gchar *attr, PyObject *value) { GnomeVFSFileInfo *finfo; if (!self->finfo) self->finfo = gnome_vfs_file_info_new(); finfo = self->finfo; if (!strcmp(attr, "__members__")) { PyErr_SetString(PyExc_TypeError, "readonly attribute"); return -1; } else if (!strcmp(attr, "name")) { if (!PyString_Check(value)) { PyErr_SetString(PyExc_TypeError, "'name' attribute must be a string"); return -1; } if (finfo->name) g_free(finfo->name); finfo->name = g_strdup(PyString_AsString(value)); return 0; } else if (!strcmp(attr, "valid_fields")) { if (!PyInt_Check(value)) { PyErr_SetString(PyExc_TypeError, "'valid_fields' attribute must be an int"); return -1; } finfo->valid_fields = PyInt_AsLong(value); return 0; } else if (!strcmp(attr, "type")) { if (!PyInt_Check(value)) { PyErr_SetString(PyExc_TypeError, "'type' attribute must be an int"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_TYPE; finfo->type = PyInt_AsLong(value); return 0; } else if (!strcmp(attr, "permissions")) { if (!PyInt_Check(value)) { PyErr_SetString(PyExc_TypeError, "'permissions' attribute must be an int"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS; finfo->permissions = PyInt_AsLong(value); return 0; } else if (!strcmp(attr, "access")) { if (!PyInt_Check(value)) { PyErr_SetString(PyExc_TypeError, "'access' attribute must be an int"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_ACCESS; finfo->permissions |= PyInt_AsLong(value); return 0; } else if (!strcmp(attr, "flags")) { if (!PyInt_Check(value)) { PyErr_SetString(PyExc_TypeError, "'flags' attribute must be an int"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_FLAGS; finfo->flags = PyInt_AsLong(value); return 0; } else if (!strcmp(attr, "device")) { if (!(PyInt_Check(value) || PyLong_Check(value))) { PyErr_SetString(PyExc_TypeError, "'device' attribute must be an int or long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_DEVICE; if (PyInt_Check(value)) finfo->device = PyInt_AsLong(value); else finfo->device = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "inode")) { if (!(PyInt_Check(value) || PyLong_Check(value))) { PyErr_SetString(PyExc_TypeError, "'inode' attribute must be an int or long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_INODE; if (PyInt_Check(value)) finfo->inode = PyInt_AsLong(value); else finfo->inode = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "link_count")) { if (!(PyInt_Check(value) || PyLong_Check(value))) { PyErr_SetString(PyExc_TypeError, "'link_count' attribute must be an int or long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_LINK_COUNT; if (PyInt_Check(value)) finfo->link_count = PyInt_AsLong(value); else finfo->link_count = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "uid")) { if (!(PyInt_Check(value) || PyLong_Check(value))) { PyErr_SetString(PyExc_TypeError, "'uid' attribute must be an int or long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_IDS; if (PyInt_Check(value)) finfo->uid = PyInt_AsLong(value); else finfo->uid = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "gid")) { if (!(PyInt_Check(value) || PyLong_Check(value))) { PyErr_SetString(PyExc_TypeError, "'gid' attribute must be an int or long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_IDS; if (PyInt_Check(value)) finfo->gid = PyInt_AsLong(value); else finfo->gid = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "size")) { if (!(PyInt_Check(value) || PyLong_Check(value))) { PyErr_SetString(PyExc_TypeError, "'size' attribute must be an int or long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE; if (PyInt_Check(value)) finfo->size = PyInt_AsLong(value); else finfo->size = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "block_count")) { if (!(PyInt_Check(value) || PyLong_Check(value))) { PyErr_SetString(PyExc_TypeError, "'block_count' attribute must be an int or long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT; if (PyInt_Check(value)) finfo->block_count = PyInt_AsLong(value); else finfo->block_count = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "io_block_size")) { if (!(PyInt_Check(value) || PyLong_Check(value))) { PyErr_SetString(PyExc_TypeError, "'io_block_size' attribute must be an int or long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE; if (PyInt_Check(value)) finfo->io_block_size = PyInt_AsLong(value); else finfo->io_block_size = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "atime")) { if (!PyLong_Check(value)) { PyErr_SetString(PyExc_TypeError, "'atime' attribute must be a long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_ATIME; finfo->atime = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "mtime")) { if (!PyLong_Check(value)) { PyErr_SetString(PyExc_TypeError, "'mtime' attribute must be a long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MTIME; finfo->mtime = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "ctime")) { if (!PyLong_Check(value)) { PyErr_SetString(PyExc_TypeError, "'ctime' attribute must be a long"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_CTIME; finfo->ctime = PyLong_AsUnsignedLongLong(value); return 0; } else if (!strcmp(attr, "symlink_name")) { if (!PyString_Check(value)) { PyErr_SetString(PyExc_TypeError, "'symlink_name' attribute must be a string"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SYMLINK_NAME; if (finfo->symlink_name) g_free(finfo->symlink_name); finfo->symlink_name = g_strdup(PyString_AsString(value)); return 0; } else if (!strcmp(attr, "mime_type")) { if (!PyString_Check(value)) { PyErr_SetString(PyExc_TypeError, "'mime_type' attribute must be a string"); return -1; } finfo->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE; if (finfo->mime_type) g_free(finfo->mime_type); finfo->mime_type = g_strdup(PyString_AsString(value)); return 0; } else { PyObject *name = PyString_FromString(attr); int ret = PyObject_GenericSetAttr((PyObject *)self, name, value); Py_DECREF(name); return ret; } }