//converts a type value string to it's corresponding PyObject*
PyObject* FunctionManager::convert(std::string type_val) {
	PyObject* arg = NULL;
	int delim_pos = type_val.find(':');
	

	if (delim_pos != std::string::npos) {
		std::string type = type_val.substr(0, delim_pos);
		std::string val = type_val.substr(type.length() + 1);
		
		if (type == "string") {
			arg = PyUnicode_FromString(val.c_str());
		}
		else if (type == "long") {
			arg = PyLong_FromLong(atoi(val.c_str()));
		}
		else if (type == "float") {
			arg = PyFloat_FromString(PyUnicode_FromString(val.c_str()));
		}
		else if (type == "double") {
			arg = PyFloat_FromString(PyUnicode_FromString(val.c_str()));
		}
		else if (type == "bool") {
			arg = PyBool_FromLong(atoi(val.c_str()));
		}
	}
	
	return arg;
}
Ejemplo n.º 2
0
NUITKA_MAY_BE_UNUSED static PyObject *TO_FLOAT( PyObject *value )
{
    PyObject *result;

#if PYTHON_VERSION < 300
    if ( PyString_CheckExact( value ) )
    {
        result = PyFloat_FromString( value, NULL );
    }
#else
    if ( PyUnicode_CheckExact( value ) )
    {
        result = PyFloat_FromString( value );
    }
#endif
    else
    {
        result = PyNumber_Float( value );
    }

    if (unlikely( result == NULL ))
    {
        return NULL;
    }

    return result;
}
static PyObject *
typecast_FLOAT_cast(const char *s, Py_ssize_t len, PyObject *curs)
{
    PyObject *str = NULL, *flo = NULL;

    if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
    if (!(str = Text_FromUTF8AndSize(s, len))) { return NULL; }
#if PY_MAJOR_VERSION < 3
    flo = PyFloat_FromString(str, NULL);
#else
    flo = PyFloat_FromString(str);
#endif
    Py_DECREF(str);
    return flo;
}
Ejemplo n.º 4
0
static PyObject *
typecast_FLOAT_cast(const char *s, Py_ssize_t len, PyObject *curs)
{
    PyObject *str = NULL, *flo = NULL;
    char *pend;

    if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
    str = PyString_FromStringAndSize(s, len);
    flo = PyFloat_FromString(str, &pend);
    Py_DECREF(str);
    return flo;
}
Ejemplo n.º 5
0
static int handle_number(void *ctx, const char *value, unsigned int length)
{
    _YajlDecoder *self = (_YajlDecoder *)(ctx);
    PyObject *object;
#ifdef IS_PYTHON3
    PyBytesObject *string;
#else
    PyObject *string;
#endif

    int floaty_char;

    // take a moment here to scan the input string to see if there's
    // any chars which suggest this is a floating point number
    for (floaty_char = 0; floaty_char < length; floaty_char++) {
        switch (value[floaty_char]) {
            case '.': case 'e': case 'E': goto floatin;
        }
    }

  floatin:
#ifdef IS_PYTHON3
    string = (PyBytesObject *)PyBytes_FromStringAndSize(value, length);
    if (floaty_char >= length) {
        object = PyLong_FromString(string->ob_sval, NULL, 10);
    } else {
        object = PyFloat_FromString((PyObject *)string);
    }
#else
    string = PyString_FromStringAndSize(value, length);
    if (floaty_char >= length) {
        object = PyInt_FromString(PyString_AS_STRING(string), NULL, 10);
    } else {
        object = PyFloat_FromString(string, NULL);
    }
#endif
    Py_XDECREF(string);
    return PlaceObject(self, object);
}
Ejemplo n.º 6
0
PyObject *
PyNumber_Float(PyObject *o)
{
	PyNumberMethods *m;

	if (o == NULL)
		return null_error();
	if (PyFloat_Check(o)) {
		Py_INCREF(o);
		return o;
	}
	if (!PyString_Check(o)) {
		m = o->ob_type->tp_as_number;
		if (m && m->nb_float)
			return m->nb_float(o);
	}
	return PyFloat_FromString(o, NULL);
}
Ejemplo n.º 7
0
int API_resultRowValue(void *result, int column, UMTypeInfo *ti, char *value, size_t cbValue)
{
  PyObject *valobj = NULL;

  PRINTMARK();
  //fprintf (stderr, "%s: Got %p (%08x) %08x\n", __FUNCTION__, value, ti->type, cbValue);

  if (value == NULL)
  {
    valobj = Py_None;
    Py_IncRef(valobj);
  }
  else
  {

    switch (ti->type)
    {
      //PyNone:
    case MFTYPE_NULL:
      valobj = Py_None;
      Py_IncRef(valobj);
      break;

      // Use PyLong for "INT UNSIGNED".
    case MFTYPE_LONG:
      if(isUnsigned(ti->flags)){
        // XXX: No overflow detected.
        valobj = PyLong_FromLongLong(parseINT64 (value, ((char *) value) + cbValue));
        break;
      }
      // "INT" only, let it fall through to PyInt.

      //PyInt
    case MFTYPE_TINY:
    case MFTYPE_SHORT:
    case MFTYPE_INT24:
      {
        valobj = PyInt_FromLong(parseINT32 (value, ((char *) value) + cbValue));
        break;
      }

      //PyLong
    case MFTYPE_LONGLONG:
      {
        if(isUnsigned(ti->flags)){
          valobj = PyLong_FromUnsignedLongLong(parseUINT64 (value, ((char *) value) + cbValue));
        }else{
          valobj = PyLong_FromLongLong(parseINT64 (value, ((char *) value) + cbValue));
        }
        break;
      }

      //PyFloat
    case MFTYPE_FLOAT:
    case MFTYPE_DOUBLE:
      {
        //FIXME: Too f*****g slow
        PyObject *sobj = PyString_FromStringAndSize((char *) value, cbValue);
        valobj = PyFloat_FromString (sobj, NULL);
        Py_DECREF(sobj);
        break;
      }

    case MFTYPE_DATE:
      {
        int year;
        int month;
        int day;

        year = parseINT32 (value, value + 4);

        if (year < 1)
        {
          valobj = Py_None;
          Py_IncRef(valobj);
          break;
        }

        value += 5;
        month = parseINT32 (value, value + 2);
        value += 3;
        day = parseINT32 (value, value + 2);
        value += 3;

        valobj = PyDate_FromDate (year, month, day);
        break;
      }

    case MFTYPE_TIMESTAMP:
    case MFTYPE_DATETIME:
      {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;

        //9999-12-31 23:59:59
        char temp[20];
        memcpy (temp, value, cbValue);
        temp[cbValue] = '\0';

        year = parseINT32 (value, value + 4);
        value += 5;
        month = parseINT32 (value, value + 2);
        value += 3;
        day = parseINT32 (value, value + 2);
        value += 3;
        hour = parseINT32 (value, value + 2);
        value += 3;
        minute = parseINT32 (value, value + 2);
        value += 3;
        second = parseINT32 (value, value + 2);
        value += 3;

        if (year < 1)
        {
          valobj = Py_None;
          Py_IncRef(valobj);
          break;
        }


        valobj = PyDateTime_FromDateAndTime (year, month, day, hour, minute, second, 0);
        break;
      }

      // We ignore these

    case MFTYPE_TIME:
    case MFTYPE_YEAR:
    case MFTYPE_NEWDATE:
      // Fall through for string encoding

      //Blob goes as String
    case MFTYPE_TINY_BLOB:
    case MFTYPE_MEDIUM_BLOB:
    case MFTYPE_LONG_BLOB:
    case MFTYPE_BLOB:
      if (ti->flags & MFFLAG_BINARY_FLAG) {
        valobj = PyString_FromStringAndSize( (const char *) value, cbValue);
      } else {
        valobj = DecodeString (ti, value, cbValue);
      }
      break;

      //PyString family
    case MFTYPE_VAR_STRING:
    case MFTYPE_VARCHAR:
    case MFTYPE_STRING:
      valobj = DecodeString (ti, value, cbValue);
      break;

    case MFTYPE_ENUM:
    case MFTYPE_GEOMETRY:
    case MFTYPE_BIT:
    case MFTYPE_NEWDECIMAL:
    case MFTYPE_SET:
    case MFTYPE_DECIMAL:
      // Fall through for string encoding
      valobj = PyString_FromStringAndSize( (const char *) value, cbValue);
      break;

    }
  }

  if (valobj == NULL)
  {
    if (PyErr_Occurred())
    {
      return FALSE;
    }

    PyErr_Format (umysql_Error, "Unable to convert field of type %d", ti->type);
    return FALSE;
  }

  PyTuple_SET_ITEM(((ResultSet *)result)->currRow, column, valobj);
  PRINTMARK();

  return TRUE;
}
Ejemplo n.º 8
0
static PyObject*
decode_number(JSONData *jsondata)
{
    PyObject *object, *str;
    int c, is_float, should_stop;
    char *ptr;

    // check if we got a floating point number
    ptr = jsondata->ptr;
    is_float = should_stop = False;
    while (True) {
        c = *ptr;
        if (c == 0)
            break;
        switch(c) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '-':
            case '+':
                break;
            case '.':
            case 'e':
            case 'E':
                is_float = True;
                break;
            default:
                should_stop = True;
        }
        if (should_stop) {
            break;
        }
        ptr++;
    }

    str = PyString_FromStringAndSize(jsondata->ptr, ptr - jsondata->ptr);
    if (str == NULL)
        return NULL;

    if (is_float) {
        object = PyFloat_FromString(str, NULL);
    } else {
        object = PyInt_FromString(PyString_AS_STRING(str), NULL, 10);
    }

    Py_DECREF(str);

    if (object == NULL) {
        PyErr_Format(JSON_DecodeError, "invalid number starting at position "
                     SSIZE_T_F, (Py_ssize_t)(jsondata->ptr - jsondata->str));
    } else {
        jsondata->ptr = ptr;
    }

    return object;
}
Ejemplo n.º 9
0
static PyObject*
decode_number(JSONData *jsondata)
{
    PyObject *object, *str;
    int is_float;
    char *ptr;

    // validate number and check if it's floating point or not
    ptr = jsondata->ptr;
    is_float = False;

    if (*ptr == '-' || *ptr == '+')
        ptr++;

    if (*ptr == '0') {
        ptr++;
        if (isdigit(*ptr))
            goto number_error;
    } else if (isdigit(*ptr))
        skipDigits(ptr);
    else
        goto number_error;

    if (*ptr == '.') {
       is_float = True;
       ptr++;
       if (!isdigit(*ptr))
           goto number_error;
       skipDigits(ptr);
    }

    if (*ptr == 'e' || *ptr == 'E') {
       is_float = True;
       ptr++;
       if (*ptr == '+' || *ptr == '-')
           ptr++;
       if (!isdigit(*ptr))
           goto number_error;
       skipDigits(ptr);
    }

    str = PyString_FromStringAndSize(jsondata->ptr, ptr - jsondata->ptr);
    if (str == NULL)
        return NULL;

    if (is_float)
        object = PyFloat_FromString(str, NULL);
    else
        object = PyInt_FromString(PyString_AS_STRING(str), NULL, 10);

    Py_DECREF(str);

    if (object == NULL)
        goto number_error;

    jsondata->ptr = ptr;

    return object;

number_error:
    PyErr_Format(JSON_DecodeError, "invalid number starting at position "
                 SSIZE_T_F, (Py_ssize_t)(jsondata->ptr - jsondata->str));
    return NULL;
}
Ejemplo n.º 10
0
/**
 * 将结果集的一项转变为相应类型的对象
 **/
static PyObject*
wrap_to_Object(char* cell, int type, int len)
{
    PyObject *res;
    PyObject *v;
    switch(type)
    {
    //文本或者字符串
    case FIELD_TYPE_BLOB:
    case FIELD_TYPE_LONG_BLOB:
    case FIELD_TYPE_MEDIUM_BLOB:
    case FIELD_TYPE_TINY_BLOB:
    case FIELD_TYPE_STRING:
    case FIELD_TYPE_VAR_STRING:
        res = PyString_FromStringAndSize(cell, len);
        break;
    //整数对象
    case FIELD_TYPE_ENUM:
    case FIELD_TYPE_INT24:
    case FIELD_TYPE_LONG:
    case FIELD_TYPE_LONGLONG:
    case FIELD_TYPE_SHORT:
    case FIELD_TYPE_TINY:
        res = PyInt_FromString(cell, NULL, 10);
        break;
    //浮点数
    case FIELD_TYPE_DECIMAL:
    case FIELD_TYPE_DOUBLE:
    case FIELD_TYPE_FLOAT:
        v = PyString_FromStringAndSize(cell, len);
        res = PyFloat_FromString(v, NULL);
        Py_XDECREF(v);
        break;
    //时间类型
    case FIELD_TYPE_DATE:
    case FIELD_TYPE_DATETIME:
    case FIELD_TYPE_NEWDATE:
    case FIELD_TYPE_TIME:
    case FIELD_TYPE_TIMESTAMP:
        res = PyString_FromStringAndSize(cell, len);
        break;
    //空值
    case FIELD_TYPE_NULL:
        res = Py_None;
        Py_INCREF(res);
        break;
    //set类型
    case FIELD_TYPE_SET:
        res = Py_None;
        Py_INCREF(res);
        break;
    case FIELD_TYPE_YEAR:
        res = Py_None;
        Py_INCREF(res);
        break;
    default:
        Py_INCREF(Py_None);
        return Py_None;
    }

    return res;
}