Example #1
0
// TODO: It might be possible to simplify these further by making the
// conversion function (eg. PyString_AsString) an argument to a macro or
// something, but I'm not sure yet how easy it is to generalize the error
// handling
int get_header_string(PyObject* header, char* keyword, char** val, char* def) {
    PyObject* keystr;
    PyObject* keyval;
#ifdef IS_PY3K
    PyObject* tmp;  // Temp pointer to decoded bytes object
#endif
    int retval;

    keystr = PyString_FromString(keyword);
    keyval = PyObject_GetItem(header, keystr);

    if (keyval != NULL) {
#ifdef IS_PY3K
        // FITS header values should always be ASCII, but Latin1 is on the
        // safe side
        tmp = PyUnicode_AsLatin1String(keyval);
        *val = PyBytes_AsString(tmp);
        Py_DECREF(tmp);
#else
        *val = PyString_AsString(keyval);
#endif
        retval = 0;
    }
    else {
        PyErr_Clear();
        *val = def;
        retval = 1;
    }

    Py_DECREF(keystr);
    Py_XDECREF(keyval);
    return retval;
}
Example #2
0
PyObject *wsgi_convert_string_to_bytes(PyObject *value)
{
    PyObject *result = NULL;

#if PY_MAJOR_VERSION >= 3
    if (!PyUnicode_Check(value)) {
        PyErr_Format(PyExc_TypeError, "expected unicode object, value "
                     "of type %.200s found", value->ob_type->tp_name);
        return NULL;
    }

    result = PyUnicode_AsLatin1String(value);

    if (!result) {
        PyErr_SetString(PyExc_ValueError, "unicode object contains non "
                        "latin-1 characters");
        return NULL;
    }
#else
    if (!PyBytes_Check(value)) {
        PyErr_Format(PyExc_TypeError, "expected byte string object, "
                     "value of type %.200s found", value->ob_type->tp_name);
        return NULL;
    }

    Py_INCREF(value);
    result = value;
#endif

    return result;
}
Example #3
0
template<> const char* fromPy<const char*>(PyObject *obj) {
    if (PyUnicode_Check(obj))
        return PyBytes_AsString(PyUnicode_AsLatin1String(obj));
#if PY_MAJOR_VERSION <= 2
    else if (PyString_Check(obj))
        return PyString_AsString(obj);
#endif
    else errMsg("argument is not a string");
}
Example #4
0
File: lib.cpp Project: biotty/rmg
std::string
parse_string(PyObject * s)
{
    if (PyObject * o = PyUnicode_AsLatin1String(s)) {
        std::string r = PyBytes_AsString(o);
        Py_DECREF(o);
        return r;
    } else {
        return "";
    }
}
Example #5
0
// convert a python object to an erlang term, return the atom 'unknown' if
// the type can't be converted
static ERL_NIF_TERM pynerl_obj_to_term(ErlNifEnv* env, PyObject* obj) {
	ERL_NIF_TERM term;

	if (obj == Py_False) {
		term = enif_make_atom(env, "false");
	}
	else if (obj == Py_True) {
		term = enif_make_atom(env, "true");
	}
	else if (PyLong_Check(obj)) {
		// TODO: make ints when the size allows to.
		term = enif_make_long(env, PyLong_AsLong(obj));
	}
	else if (PyFloat_Check(obj)) {
		term = enif_make_double(env, PyFloat_AsDouble(obj));
	}
	else if (PyTuple_Check(obj)) {
		Py_ssize_t i, arity = PyTuple_Size(obj);
		ERL_NIF_TERM *terms = (ERL_NIF_TERM*) malloc(sizeof(ERL_NIF_TERM) * (int)arity);

		for (i = 0; i < arity; i++) {
			terms[(int)i] = pynerl_obj_to_term(env, PyTuple_GetItem(obj, i));
		}

		term = enif_make_tuple_from_array(env, terms, (unsigned int)arity);
	}
	else if (PyBytes_Check(obj)) {
		// XXX: the encoding must be latin1
		term = enif_make_string(env, PyBytes_AsString(obj), ERL_NIF_LATIN1);
	}
	else if (PyUnicode_Check(obj)) {
		// XXX: the encoding must be latin1
		term = enif_make_string(env, PyBytes_AsString(PyUnicode_AsLatin1String(obj)), ERL_NIF_LATIN1);
	}
	else if (PyList_Check(obj)) {
		Py_ssize_t i, arity = PyList_Size(obj);
		ERL_NIF_TERM *terms = (ERL_NIF_TERM*) malloc(sizeof(ERL_NIF_TERM) * (int)arity);

		for (i = 0; i < arity; i++) {
			terms[(int)i] = pynerl_obj_to_term(env, PyList_GetItem(obj, i));
		}

		term = enif_make_list_from_array(env, terms, (unsigned int)arity);
	}
	else if (obj == Py_None) {
		term = enif_make_atom(env, "none");
    }
	else {
		term = enif_make_atom(env, "unknown");
    }

	return term;
}
Example #6
0
 static void from_str_to_char(PyObject* in, CORBA::String_member& out)
 {
     if (PyUnicode_Check(in))
     {
         PyObject *bytes_in = PyUnicode_AsLatin1String(in);
         out = CORBA::string_dup(PyBytes_AsString(bytes_in));
         Py_DECREF(bytes_in);
     }
     else 
     {
         out = CORBA::string_dup(PyBytes_AsString(in));
     }
 }
Example #7
0
static PyObject * _psp_module_parsestring(PyObject *self, PyObject *argv)
{
    PyObject *code;
    PyObject *str;
    PyObject *latin = NULL;
    char *c_str = NULL;
    yyscan_t scanner;
    psp_parser_t  *parser;
    YY_BUFFER_STATE bs;

    if (!PyArg_ParseTuple(argv, "S", &str)) {
        return NULL;
    }

    Py_BEGIN_ALLOW_THREADS
    parser = psp_parser_init();
    yylex_init(&scanner);
    yyset_extra(parser, scanner);

    if (PyUnicode_Check(str)) {
        latin = PyUnicode_AsLatin1String(str);
        if (latin)
            c_str = PyBytes_AsString(latin);
    } else if (PyBytes_Check(str))
        c_str = PyBytes_AsString(str);

    if (!c_str) c_str = "UNICODE ERROR";

    bs = yy_scan_string(c_str, scanner);
    yylex(scanner);

    Py_XDECREF(latin);

    /* yy_delete_buffer(bs, scanner); */
    yylex_destroy(scanner);

    psp_string_0(&parser->pycode);
    Py_END_ALLOW_THREADS

    if (parser->pycode.blob) {
        code = MpBytesOrUnicode_FromString(parser->pycode.blob);
    }
    else {
        code = MpBytesOrUnicode_FromString("");
    }

    psp_parser_cleanup(parser);

    return code;
}
Example #8
0
int get_header_string(PyObject* header, const char* keyword, char* val,
                      const char* def, HeaderGetFlags flags) {
    /* nonnegative doesn't make sense for strings*/
    assert(!(flags & HDR_FAIL_VAL_NEGATIVE));
    PyObject* keyval = get_header_value(header, keyword, flags);

    if (keyval == NULL) {
        strncpy(val, def, 72);
        return PyErr_Occurred() ? GET_HEADER_FAILED : GET_HEADER_DEFAULT_USED;
    }
    PyObject* tmp = PyUnicode_AsLatin1String(keyval);
    // FITS header values should always be ASCII, but Latin1 is on the
    // safe side
    Py_DECREF(keyval);
    if (tmp == NULL) {
        /* could always fail to allocate the memory or such like. */
        return GET_HEADER_FAILED;
    }
    strncpy(val, PyBytes_AsString(tmp), 72);
    Py_DECREF(tmp);
    return GET_HEADER_SUCCESS;
}
Example #9
0
PyObject *_a85_decode(PyObject *module, PyObject *args)
{
	unsigned char	*inData, *p, *q, *tmp, *buf;
	unsigned int	length, blocks, extra, k, num, c1, c2, c3, c4, c5;
	static unsigned pad[] = {0,0,0xffffff,0xffff,0xff};
	PyObject		*retVal=NULL, *inObj, *_o1=NULL;
	if(!PyArg_ParseTuple(args, "O", &inObj)) return NULL;
	if(PyUnicode_Check(inObj)){
		_o1 = PyUnicode_AsLatin1String(inObj);
		if(!_o1){
			PyErr_SetString(PyExc_ValueError,"argument not decodable as latin1");
			ERROR_EXIT();
			}
		inData = PyBytes_AsString(_o1);
		inObj = _o1;
		if(!inData){
			PyErr_SetString(PyExc_ValueError,"argument not converted to internal char string");
			ERROR_EXIT();
			}
		}
	else if(!PyBytes_Check(inObj)){
		PyErr_SetString(PyExc_ValueError,"argument should be " BYTESNAME " or latin1 decodable " STRNAME);
		ERROR_EXIT();
		}
	inData = PyBytes_AsString(inObj);
	length = PyBytes_GET_SIZE(inObj);
	for(k=0,q=inData, p=q+length;q<p && (q=(unsigned char*)strchr((const char*)q,'z'));k++, q++);	/*count 'z'*/
	length += k*4;
	tmp = q = (unsigned char*)malloc(length+1);
	while(inData<p && (k = *inData++)){
		if(isspace(k)) continue;
		if(k=='z'){
			/*turn 'z' into '!!!!!'*/
			memcpy(q,"!!!!!",5);
			q += 5;
			}
		else
			*q++ = k;
		}
	inData = tmp;
	length = q - inData;
	buf = inData+length-2;
	if(buf[0]!='~' || buf[1]!='>'){
		PyErr_SetString(PyExc_ValueError, "Invalid terminator for Ascii Base 85 Stream");
		free(inData);
		ERROR_EXIT();
		}
	length -= 2;
	buf[0] = 0;

	blocks = length / 5;
	extra = length % 5;

	buf = (unsigned char*)malloc((blocks+1)*4);
	q = inData+blocks*5;
	for(k=0;inData<q;inData+=5){
		c1 = inData[0]-33;
		c2 = inData[1]-33;
		c3 = inData[2]-33;
		c4 = inData[3]-33;
		c5 = inData[4]-33;
		num = (((c1*85+c2)*85+c3)*85+c4)*85+c5;
		buf[k++] = num>>24;
		buf[k++] = num>>16;
		buf[k++] = num>>8;
		buf[k++] = num;
		}
	if(extra>1){
		c1 = inData[0]-33;
		c2 = extra>=2 ? inData[1]-33: 0;
		c3 = extra>=3 ? inData[2]-33: 0;
		c4 = extra>=4 ? inData[3]-33: 0;
		c5 = 0;
		num = (((c1*85+c2)*85+c3)*85+c4)*85+c5 + pad[extra];
		if(extra>1){
			buf[k++] = num>>24;
			if(extra>2){
				buf[k++] = num>>16;
				if(extra>3){
					buf[k++] = num>>8;
					}
Example #10
0
PyObject *_a85_encode(PyObject *module, PyObject *args)
{
	unsigned char	*inData;
	int				length, blocks, extra, i, k, lim;
	unsigned long	block, res;
	char			*buf;
	PyObject		*retVal=NULL, *inObj, *_o1=NULL;
	if(!PyArg_ParseTuple(args, "O", &inObj)) return NULL;
	if(PyUnicode_Check(inObj)){
		_o1 = PyUnicode_AsLatin1String(inObj);
		if(!_o1){
			PyErr_SetString(PyExc_ValueError,"argument not decodable as latin1");
			ERROR_EXIT();
			}
		inData = PyBytes_AsString(_o1);
		inObj = _o1;
		if(!inData){
			PyErr_SetString(PyExc_ValueError,"argument not converted to internal char string");
			ERROR_EXIT();
			}
		}
	else if(!PyBytes_Check(inObj)){
		PyErr_SetString(PyExc_ValueError,"argument should be " BYTESNAME " or latin1 decodable " STRNAME);
		ERROR_EXIT();
		}
	inData = PyBytes_AsString(inObj);
	length = PyBytes_GET_SIZE(inObj);

	blocks = length / 4;
	extra = length % 4;

	buf = (char*)malloc((blocks+1)*5+3);
	lim = 4*blocks;

	for(k=i=0; i<lim; i += 4){
		/*
		 * If you evere have trouble with this consider using masking to ensure
		 * that the shifted quantity is only 8 bits long
		 */
		block = ((unsigned long)inData[i]<<24)|((unsigned long)inData[i+1]<<16)
				|((unsigned long)inData[i+2]<<8)|((unsigned long)inData[i+3]);
		if (block == 0) buf[k++] = 'z';
		else {
			res = block/a85_4;
			buf[k++] = (char)(res+33);
			block -= res*a85_4;

			res = block/a85_3;
			buf[k++] = (char)(res+33);
			block -= res*a85_3;

			res = block/a85_2;
			buf[k++] = (char)(res+33);
			block -= res*a85_2;

			res = block / a85_1;
			buf[k++] = (char)(res+33);

			buf[k++] = (char)(block-res*a85_1+33);
			}
		}

	if(extra>0){
		block = 0L;

		for (i=0; i<extra; i++)
			block += (unsigned long)inData[length-extra+i] << (24-8*i);

		res = block/a85_4;
		buf[k++] = (char)(res+33);
		if(extra>=1){
			block -= res*a85_4;

			res = block/a85_3;
			buf[k++] = (char)(res+33);
			if(extra>=2){
				block -= res*a85_3;

				res = block/a85_2;
				buf[k++] = (char)(res+33);
				if(extra>=3) buf[k++] = (char)((block-res*a85_2)/a85_1+33);
				}
			}
		}

	buf[k++] = '~';
	buf[k++] = '>';
	retVal = PyUnicode_FromStringAndSize(buf, k);
	free(buf);
	if(!retVal){
		PyErr_SetString(PyExc_ValueError,"failed to create return " STRNAME " value" );
		ERROR_EXIT();
		}
L_exit:
	Py_XDECREF(_o1);
	return retVal;
L_ERR:
	ADD_TB(module,"asciiBase85Encode");
	goto L_exit;
}
Example #11
0
static PyObject*
Sexp_do_slot(PyObject *self, PyObject *name)
{
  SEXP sexp = RPY_SEXP(((PySexpObject*)self));
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;
  }
#if (PY_VERSION_HEX < 0x03010000)
  if (! PyString_Check(name)) {
#else
    if (! PyUnicode_Check(name)) {
#endif
    PyErr_SetString(PyExc_TypeError, "The name must be a string.");
    return NULL;
  }
#if (PY_VERSION_HEX < 0x03010000)
  char *name_str = PyString_AS_STRING(name);
#else
  PyObject *pybytes = PyUnicode_AsLatin1String(name);
  char *name_str = PyBytes_AsString(pybytes);
#endif
  if (! R_has_slot(sexp, install(name_str))) {
    PyErr_SetString(PyExc_LookupError, "The object has no such attribute.");
#if (PY_VERSION_HEX >= 0x03010000)
    Py_DECREF(pybytes);
#endif
    return NULL;
  }
  SEXP res_R = GET_SLOT(sexp, install(name_str));
#if (PY_VERSION_HEX >= 0x03010000)
    Py_DECREF(pybytes);
#endif
  PyObject *res = (PyObject *)newPySexpObject(res_R, 1);
  return res;
}
PyDoc_STRVAR(Sexp_do_slot_doc,
             "Returns the attribute/slot for an R object.\n"
             " The name of the slot (a string) is the only parameter for\n"
             "the method.\n"
             ":param name: string\n"
             ":rtype: instance of type or subtype :class:`rpy2.rinterface.Sexp`");

static PyObject*
Sexp_do_slot_assign(PyObject *self, PyObject *args)
{

  SEXP sexp = RPY_SEXP(((PySexpObject*)self));
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }

  char *name_str;
  PyObject *value;
  if (! PyArg_ParseTuple(args, "sO", 
                         &name_str,
                         &value)) {
    return NULL;
  }

  if (! PyObject_IsInstance(value, 
                          (PyObject*)&Sexp_Type)) {
      PyErr_Format(PyExc_ValueError, "Value must be an instance of Sexp.");
      return NULL;
  }

  SEXP value_sexp = RPY_SEXP((PySexpObject *)value);
  if (! value_sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }

  SET_SLOT(sexp, install(name_str), value_sexp);
  Py_INCREF(Py_None);
  return Py_None;
}
// Convert a Python unicode/string/bytes object to a character string encoded
// according to the given encoding.  Update the object with a new reference to
// the object that owns the data.
const char *qpycore_encode(PyObject **s, QCoreApplication::Encoding encoding)
{
    PyObject *obj = *s;
    const char *es = 0;
    SIP_SSIZE_T sz;

    if (PyUnicode_Check(obj))
    {
        if (encoding == QCoreApplication::UnicodeUTF8)
        {
            obj = PyUnicode_AsUTF8String(obj);
        }
        else
        {
            QTextCodec *codec = QTextCodec::codecForTr();

            if (codec)
            {
                // Use the Qt codec to get to a byte string, and then to a
                // Python object.
                QString qs = qpycore_PyObject_AsQString(obj);
                QByteArray ba = codec->fromUnicode(qs);

#if PY_MAJOR_VERSION >= 3
                obj = PyBytes_FromStringAndSize(ba.constData(), ba.size());
#else
                obj = PyString_FromStringAndSize(ba.constData(), ba.size());
#endif
            }
            else
            {
                obj = PyUnicode_AsLatin1String(obj);
            }
        }

        if (obj)
        {
#if PY_MAJOR_VERSION >= 3
            es = PyBytes_AS_STRING(obj);
#else
            es = PyString_AS_STRING(obj);
#endif
        }
    }
#if PY_MAJOR_VERSION >= 3
    else if (PyBytes_Check(obj))
    {
        es = PyBytes_AS_STRING(obj);
        Py_INCREF(obj);
    }
#else
    else if (PyString_Check(obj))
    {
        es = PyString_AS_STRING(obj);
        Py_INCREF(obj);
    }
#endif
    else if (PyObject_AsCharBuffer(obj, &es, &sz) >= 0)
    {
        Py_INCREF(obj);
    }

    if (es)
    {
        *s = obj;
    }
    else
    {
        PyErr_Format(PyExc_UnicodeEncodeError,
                "unable to convert '%s' to requested encoding",
                Py_TYPE(*s)->tp_name);
    }

    return es;
}