bool BINARY_OPERATION_ADD_UNICODE_UNICODE_INPLACE(PyObject **operand1, PyObject *operand2) {
    assert(operand1);
    CHECK_OBJECT(*operand1);
    CHECK_OBJECT(operand2);
    assert(PyUnicode_CheckExact(*operand1));
    assert(PyUnicode_CheckExact(operand2));

#if PYTHON_VERSION >= 300
    if (Py_REFCNT(*operand1) == 1 && !PyUnicode_CHECK_INTERNED(*operand1)) {
        // We more or less own the operand, so we might re-use its storage and
        // execute stuff in-place.
        return UNICODE_ADD_INCREMENTAL(operand1, operand2);
    }
#endif

    PyObject *result = UNICODE_CONCAT(*operand1, operand2);

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

    Py_DECREF(*operand1);
    *operand1 = result;

    return true;
}
Esempio n. 2
0
File: split.c Progetto: S-YOU/split
static PyObject*
_split_u_list(PyObject* self, PyObject* arg) {
	size_t sLen = 0, splitterLen = 1, maxsplit = 1, index = 0;
	Py_UNICODE *src = NULL, *s, *ss, *sEnd = NULL, *sPrev = NULL;
	Py_UNICODE defaultSplitter = ',', *splitter = &defaultSplitter, *sp, *spEnd = splitter + splitterLen;
	PyObject *list;
	PyTupleObject* argTuple = (PyTupleObject*) arg;

	if (PyUnicode_CheckExact(argTuple->ob_item[0])) {
		src = PyUnicode_AS_UNICODE(argTuple->ob_item[0]);
		sLen = PyUnicode_GET_SIZE(argTuple->ob_item[0]);
	} else {
		return NULL;
	}

	maxsplit = PyInt_AsSsize_t(argTuple->ob_item[1]);
	if (!maxsplit || maxsplit > 100) {
		return NULL;
	}

	if (argTuple->ob_size > 2) {
		if (PyUnicode_CheckExact(argTuple->ob_item[2])) {
			splitter = PyUnicode_AS_UNICODE(argTuple->ob_item[2]);
			splitterLen = PyUnicode_GET_SIZE(argTuple->ob_item[2]);
			spEnd = splitter + splitterLen;
		} else {
			return NULL;
		}
	}

	sPrev = s = src, sEnd = src + sLen;
	list = TYPE_NEW(maxsplit);
	while (s < sEnd) {
		if (*s == *splitter) {
			if (splitterLen > 1) {
				sp = splitter, ss = s;
				while (*++sp == *++ss);
				if (sp >= spEnd) {
					ADD_U;
					if (index >= maxsplit) return list;
					sPrev = s + splitterLen;
					s += splitterLen;
					continue;
				}
			} else {
				ADD_U;
				if (index >= maxsplit) return list;
				sPrev = s + 1;
			}
		}
		s++;
	}
	if (index < maxsplit) {
		ADD_U;
	}
	Py_SIZE(list) = index;
	return list;
}
Esempio n. 3
0
static int
inner_atohl(PyObject *o, unsigned long *ip_ul){
    struct in_addr buf;
    char *ip_addr;

#ifdef IS_PY3K
    if(PyUnicode_CheckExact(o)){
        ip_addr = PyUnicode_AsUTF8(o);
    } else if (PyBytes_CheckExact(o)){
        ip_addr = PyBytes_AsString(o);
    } else if (PyByteArray_CheckExact(o)){
        ip_addr =  PyByteArray_AsString(o);
#else
    if(PyUnicode_CheckExact(o) || PyString_CheckExact(o)){
        ip_addr =  PyString_AsString(o);
    } else if (PyByteArray_CheckExact(o)){
        ip_addr = PyByteArray_AsString(o);
#endif
    } else {
        PyErr_SetString(PyExc_TypeError, "should be built-in string/bytes/byte array");
        return 0;
    }

    if (ip_addr == NULL){
        return 0;
    }

    if (inet_aton(ip_addr, &buf)){
        *ip_ul = ntohl(buf.s_addr);
        return 1;
    }else{
        PyErr_SetString(PyExc_ValueError,
                        "illegal IP address string");
        return 0;
    }
}


static PyObject *
ip_store_atohl(PyObject *self, PyObject *o){

    unsigned long ip_ul;

    if(inner_atohl(o, &ip_ul)){
        return PyLong_FromUnsignedLong(ip_ul);
    }else{
        return NULL;
    }
}
Esempio n. 4
0
PyObject *
PyObject_Unicode(PyObject *v)
{
	PyObject *res;
	PyObject *func;
	PyObject *str;
	static PyObject *unicodestr;

	if (v == NULL) {
		res = PyString_FromString("<NULL>");
		if (res == NULL)
			return NULL;
		str = PyUnicode_FromEncodedObject(res, NULL, "strict");
		Py_DECREF(res);
		return str;
	} else if (PyUnicode_CheckExact(v)) {
		Py_INCREF(v);
		return v;
	}
	/* XXX As soon as we have a tp_unicode slot, we should
	   check this before trying the __unicode__
	   method. */
	if (unicodestr == NULL) {
		unicodestr= PyString_InternFromString("__unicode__");
		if (unicodestr == NULL)
			return NULL;
	}
	func = PyObject_GetAttr(v, unicodestr);
	if (func != NULL) {
		res = PyEval_CallObject(func, (PyObject *)NULL);
		Py_DECREF(func);
	}
	else {
		PyErr_Clear();
		if (PyUnicode_Check(v)) {
			/* For a Unicode subtype that's didn't overwrite __unicode__,
			   return a true Unicode object with the same data. */
			return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v),
			                             PyUnicode_GET_SIZE(v));
		}
		if (PyString_CheckExact(v)) {
			Py_INCREF(v);
			res = v;
		}
		else {
			if (v->ob_type->tp_str != NULL)
				res = (*v->ob_type->tp_str)(v);
			else
				res = PyObject_Repr(v);
		}
	}
	if (res == NULL)
		return NULL;
	if (!PyUnicode_Check(res)) {
		str = PyUnicode_FromEncodedObject(res, NULL, "strict");
		Py_DECREF(res);
		res = str;
	}
	return res;
}
Esempio n. 5
0
RAISES_NEG static int
_psyco_conn_parse_onoff(PyObject *pyval)
{
    int rv = -1;

    Py_INCREF(pyval);   /* for ensure_bytes */

    if (pyval == Py_None) {
        rv = STATE_DEFAULT;
    }
    else if (PyUnicode_CheckExact(pyval) || Bytes_CheckExact(pyval)) {
        if (!(pyval = psycopg_ensure_bytes(pyval))) {
            goto exit;
        }
        if (0 == strcasecmp("default", Bytes_AS_STRING(pyval))) {
            rv = STATE_DEFAULT;
        }
        else {
            PyErr_Format(PyExc_ValueError,
                "the only string accepted is 'default'; got %s",
                Bytes_AS_STRING(pyval));
            goto exit;
        }
    }
    else {
        int istrue;
        if (0 > (istrue = PyObject_IsTrue(pyval))) { goto exit; }
        rv = istrue ? STATE_ON : STATE_OFF;
    }

exit:
    Py_XDECREF(pyval);

    return rv;
}
Esempio n. 6
0
static int
TDI_RawNodeType_setitem(rawnodeobject *self, PyObject *key, PyObject *value)
{
    PyObject *tmp, *normkey;
    tdi_attr_t *item;
    int subresult;

    if (!(key = ENCODE_NAME(self->node, key)))
        return -1;

    if (!PyString_CheckExact(key) && !PyString_Check(key)) {
        PyErr_SetString(TDI_E_ModelError, "attribute key must be a string");
        Py_DECREF(key);
        return -1;
    }
    if (!(normkey = DECODER_NORMALIZE(self->node, key))) {
        Py_DECREF(key);
        return -1;
    }

    if (!value) {
        subresult = PyDict_DelItem(self->node->attr, normkey);
        if (subresult == -1) {
            if (PyErr_ExceptionMatches(PyExc_KeyError)) {
                PyErr_Clear();
                subresult = 0;
            }
        }
        Py_DECREF(normkey);
        Py_DECREF(key);
        return subresult;
    }

    if (PyUnicode_CheckExact(value) || PyUnicode_Check(value)) {
        if (!(value = ENCODE_UNICODE(self->node, value))) {
            Py_DECREF(normkey);
            Py_DECREF(key);
            return -1;
        }
    }
    else if (!(value = PyObject_Str(value))) {
        Py_DECREF(normkey);
        Py_DECREF(key);
        return -1;
    }

    item = (tdi_attr_t *)PyDict_GetItem(self->node->attr, normkey);
    tmp = tdi_attr_new(item ? item->key : key, value);
    Py_DECREF(value);
    Py_DECREF(key);
    if (!tmp) {
        Py_DECREF(normkey);
        return -1;
    }
    subresult = PyDict_SetItem(self->node->attr, normkey, tmp);
    Py_DECREF(tmp);
    Py_DECREF(normkey);

    return subresult;
}
Esempio n. 7
0
static void py_variable_to_json_internal( PyObject *obj,
                                          json_writer_t *writer )
{
  if ( PyString_CheckExact( obj ) ) {
    json_writer_write_str( writer, PyString_AS_STRING( obj ) );
  }
  else if ( PyInt_CheckExact( obj ) ) {
    json_writer_write_integer( writer, PyInt_AS_LONG( obj ) );
  }
  else if ( PyFloat_CheckExact( obj ) ) {
    json_writer_write_number( writer, PyFloat_AS_DOUBLE( obj ) );
  }
  else if ( PyBool_Check( obj ) ) {
    json_writer_write_boolean( writer, ( obj == Py_True ) );
  }
  else if ( PyUnicode_CheckExact( obj ) ) {
    /* Create a new string object that is UTF-8 encoded. */
    Py_UNICODE *unicode = PyUnicode_AS_UNICODE( obj );
    Py_ssize_t size = PyUnicode_GET_SIZE( obj );
    PyObject *str_obj = PyUnicode_EncodeUTF8( unicode, size, NULL );
    py_variable_to_json_internal( str_obj, writer );
    PyObject_Free( str_obj );
  }
  else if ( PyDict_CheckExact( obj ) ) {
    py_dict_to_json( obj, writer );
  }
  else if ( PyList_CheckExact( obj ) ) {
    py_list_to_json( obj, writer );
  }
  else if ( PyTuple_CheckExact( obj ) ) {
    py_tuple_to_json( obj, writer ); 
  }
}
Esempio n. 8
0
static int
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
                 PyObject *name, PyObject *doc)
{
    _Py_IDENTIFIER(__name__);
    _Py_IDENTIFIER(__doc__);
    _Py_IDENTIFIER(__package__);
    _Py_IDENTIFIER(__loader__);
    _Py_IDENTIFIER(__spec__);

    if (md_dict == NULL)
        return -1;
    if (doc == NULL)
        doc = Py_None;

    if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
        return -1;
    if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
        return -1;
    if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
        return -1;
    if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
        return -1;
    if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
        return -1;
    if (PyUnicode_CheckExact(name)) {
        Py_INCREF(name);
        Py_XSETREF(mod->md_name, name);
    }

    return 0;
}
Esempio n. 9
0
static int IsNCName(PyObject *str)
{
  Py_UNICODE *p;

  if (!PyUnicode_CheckExact(str)) {
    PyErr_Format(PyExc_TypeError,
                 "argument must be unicode, %.80s found.",
                 str == Py_None ? "None" : str->ob_type->tp_name);
    return -1;
  }

  p = PyUnicode_AS_UNICODE(str);
  if (*p == 0) {
    return 0;
  }

  /* the first character must match NCNameStart */
  if (!IS_NCNAMESTART(*p)) {
    return 0;
  }

  /* the remaining characters must match NCNameChar */
  for (p++; *p; p++) {
    if (!IS_NCNAMECHAR(*p)) {
      return 0;
    }
  }

  return 1;
}
Esempio n. 10
0
static int
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
                 PyObject *name, PyObject *doc)
{
    if (md_dict == NULL)
        return -1;
    if (doc == NULL)
        doc = Py_None;

    if (PyDict_SetItemString(md_dict, "__name__", name) != 0)
        return -1;
    if (PyDict_SetItemString(md_dict, "__doc__", doc) != 0)
        return -1;
    if (PyDict_SetItemString(md_dict, "__package__", Py_None) != 0)
        return -1;
    if (PyDict_SetItemString(md_dict, "__loader__", Py_None) != 0)
        return -1;
    if (PyDict_SetItemString(md_dict, "__spec__", Py_None) != 0)
        return -1;
    if (PyUnicode_CheckExact(name)) {
        Py_INCREF(name);
        Py_XDECREF(mod->md_name);
        mod->md_name = name;
    }

    return 0;
}
Esempio n. 11
0
static int IsNmtokens(PyObject *str)
{
  Py_UNICODE *p;

  if (!PyUnicode_CheckExact(str)) {
    PyErr_Format(PyExc_TypeError,
                 "argument must be unicode, %.80s found.",
                 str == Py_None ? "None" : str->ob_type->tp_name);
    return -1;
  }

  p = PyUnicode_AS_UNICODE(str);
  if (*p == 0) {
    return 0;
  }

  do {
    /* all characters must match NameChar */
    for (p++; *p && *p != 0x20; p++) {
      if (!IS_NAMECHAR(*p)) {
        return 0;
      }
    }
  } while (*p++);

  return 1;
}
Esempio n. 12
0
static char *
fp_readl(char *s, int size, struct tok_state *tok)
{
    PyObject* bufobj;
    const char *buf;
    Py_ssize_t buflen;

    /* Ask for one less byte so we can terminate it */
    assert(size > 0);
    size--;

    if (tok->decoding_buffer) {
        bufobj = tok->decoding_buffer;
        Py_INCREF(bufobj);
    }
    else
    {
        bufobj = PyObject_CallObject(tok->decoding_readline, NULL);
        if (bufobj == NULL)
            goto error;
    }
    if (PyUnicode_CheckExact(bufobj))
    {
        buf = _PyUnicode_AsStringAndSize(bufobj, &buflen);
        if (buf == NULL) {
            goto error;
        }
    }
    else
    {
        buf = PyByteArray_AsString(bufobj);
        if (buf == NULL) {
            goto error;
        }
        buflen = PyByteArray_GET_SIZE(bufobj);
    }

    Py_XDECREF(tok->decoding_buffer);
    if (buflen > size) {
        /* Too many chars, the rest goes into tok->decoding_buffer */
        tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size,
                                                         buflen-size);
        if (tok->decoding_buffer == NULL)
            goto error;
        buflen = size;
    }
    else
        tok->decoding_buffer = NULL;

    memcpy(s, buf, buflen);
    s[buflen] = '\0';
    if (buflen == 0) /* EOF */
        s = NULL;
    Py_DECREF(bufobj);
    return s;

error:
    Py_XDECREF(bufobj);
    return error_ret(tok);
}
Esempio n. 13
0
int
_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
                                PyObject *obj,
                                PyObject *format_spec,
                                Py_ssize_t start, Py_ssize_t end)
{
    InternalFormatSpec format;

    assert(PyUnicode_Check(obj));

    /* check for the special case of zero length format spec, make
       it equivalent to str(obj) */
    if (start == end) {
        if (PyUnicode_CheckExact(obj))
            return _PyUnicodeWriter_WriteStr(writer, obj);
        else
            return format_obj(obj, writer);
    }

    /* parse the format_spec */
    if (!parse_internal_render_format_spec(format_spec, start, end,
                                           &format, 's', '<'))
        return -1;

    /* type conversion? */
    switch (format.type) {
    case 's':
        /* no type conversion needed, already a string.  do the formatting */
        return format_string_internal(obj, &format, writer);
    default:
        /* unknown */
        unknown_presentation_type(format.type, obj->ob_type->tp_name);
        return -1;
    }
}
Esempio n. 14
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;
}
Esempio n. 15
0
void pb_num_feature::add_feature(
    const std::string& key,
    double value,
    std::vector<std::pair<std::string, double> >& ret_fv) const {
  scoped_gil lk;

  pb_object pkey(pb_unicode_from_string(key));
  PB_CHECK(pkey,
           "cannot convert input key to Python object: " << key);

  pb_object pval(PyFloat_FromDouble(value));
  PB_CHECK(pval,
           "cannot convert input value to Python object for key: " << key);

  pb_object ret(PyObject_CallMethodObjArgs(
      ins_.get(),
      method_.get(),
      pkey.get(),
      pval.get(),
      NULL));
  PB_CHECK(ret,
           name_ << " method cannot be called");
  PB_CHECK(PyList_CheckExact(ret.get()),
           name_ << " method returned non-list type: " << pb_str(ret.get()));

  size_t size = PyList_Size(ret.get());
  for (size_t i = 0; i < size; ++i) {
    PyObject* tpl = PyList_GetItem(ret.get(), i);

    PB_CHECK(tpl,
             "item " << i << " cannot be accessed: "
             << pb_str(ret.get()));
    PB_CHECK(PyTuple_CheckExact(tpl),
             "list must not contain non-tuple: " << pb_str(tpl));
    PB_CHECK(PyTuple_Size(tpl) == 2,
             "tuple length must be 2: " << pb_str(tpl));

    PyObject* f_key = PyTuple_GetItem(tpl, 0);
    PyObject* f_val = PyTuple_GetItem(tpl, 1);

    PB_CHECK(PyUnicode_CheckExact(f_key),
             "feature key must be a unicode string: " << pb_str(tpl));
    PB_CHECK(PyNumber_Check(f_val),
             "feature value must be a number: " << pb_str(tpl));

    pb_object f_key_enc(PyUnicode_AsUTF8String(f_key));
    PB_CHECK(f_key_enc,
             "feature key cannot be encoded as UTF-8: "
             << pb_str(tpl));
    pb_object f_val_float(PyNumber_Float(f_val));
    PB_CHECK(f_val_float,
             "value cannot be converted as float: " << pb_str(tpl));

    ret_fv.push_back(std::make_pair(
        std::string(PyBytes_AsString(f_key_enc.get())),
        PyFloat_AsDouble(f_val_float.get())));
  }
}
bool BINARY_OPERATION_ADD_UNICODE_OBJECT_INPLACE(PyObject **operand1, PyObject *operand2) {
    assert(operand1);
    CHECK_OBJECT(*operand1);
    CHECK_OBJECT(operand2);
    assert(PyUnicode_CheckExact(*operand1));

    if (likely(PyUnicode_CheckExact(operand2))) {
#if PYTHON_VERSION >= 300
        if (Py_REFCNT(*operand1) == 1 && !PyUnicode_CHECK_INTERNED(*operand1)) {
            // We more or less own the operand, so we might re-use its storage and
            // execute stuff in-place.
            return UNICODE_ADD_INCREMENTAL(operand1, operand2);
        }
#endif

        PyObject *result = UNICODE_CONCAT(*operand1, operand2);

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

        Py_DECREF(*operand1);
        *operand1 = result;

        return true;
    }

    PyObject *result = PyNumber_InPlaceAdd(*operand1, operand2);

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

    // We got an object handed, that we have to release.
    Py_DECREF(*operand1);

    // That's our return value then. As we use a dedicated variable, it's
    // OK that way.
    *operand1 = result;

    return true;
}
PyObject *
microprotocol_getquoted(PyObject *obj, connectionObject *conn)
{
    PyObject *res = NULL;
    PyObject *prepare = NULL;
    PyObject *adapted;

    if (!(adapted = microprotocols_adapt(obj, (PyObject*)&isqlquoteType, NULL))) {
       goto exit;
    }

    Dprintf("microprotocol_getquoted: adapted to %s",
        Py_TYPE(adapted)->tp_name);

    /* if requested prepare the object passing it the connection */
    if (conn) {
        if ((prepare = PyObject_GetAttrString(adapted, "prepare"))) {
            res = PyObject_CallFunctionObjArgs(
                prepare, (PyObject *)conn, NULL);
            if (res) {
                Py_DECREF(res);
                res = NULL;
            } else {
                goto exit;
            }
        }
        else {
            /* adapted.prepare not found */
            PyErr_Clear();
        }
    }

    /* call the getquoted method on adapted (that should exist because we
       adapted to the right protocol) */
    res = PyObject_CallMethod(adapted, "getquoted", NULL);

    /* Convert to bytes. */
    if (res && PyUnicode_CheckExact(res)) {
        PyObject *b;
        const char *codec;
        codec = (conn && conn->codec) ? conn->codec : "utf8";
        b = PyUnicode_AsEncodedString(res, codec, NULL);
        Py_DECREF(res);
        res = b;
    }

exit:
    Py_XDECREF(adapted);
    Py_XDECREF(prepare);

    /* we return res with one extra reference, the caller shall free it */
    return res;
}
Esempio n. 18
0
static void
intern_strings(PyObject *tuple)
{
    Py_ssize_t i;

    for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
        PyObject *v = PyTuple_GET_ITEM(tuple, i);
        if (v == NULL || !PyUnicode_CheckExact(v)) {
            Py_FatalError("non-string found in code slot");
        }
        PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
    }
}
Esempio n. 19
0
static PyObject *
unquote_internal_unicode(PyObject *string, int plus)
{
    PyObject *result;
    Py_UNICODE *su, *ru;
    Py_ssize_t j, slen, tlen, sentinel;

    if (!PyUnicode_CheckExact(string)) {
        if (!(string = PyObject_Unicode(string)))
            return NULL;
    }
    else
        Py_INCREF(string);

    su = PyUnicode_AS_UNICODE(string);
    slen = tlen = PyUnicode_GET_SIZE(string);
    
    for (j=0, sentinel=slen-2; j<sentinel; ++j) {
        if (   WTF_IS_LATIN1(su[j]) && (su[j] & 0xFF) == '%'
            && WTF_IS_LATIN1(su[j+1]) && WTF_IS_HEX_DIGIT(su[j+1])
            && WTF_IS_LATIN1(su[j+2]) && WTF_IS_HEX_DIGIT(su[j+2])) {
            tlen -= 2;
            j += 2;
        }
    }
    if (slen == tlen && !plus) /* shortcut: nothing to unquote */
        return string;

    if (!(result = PyUnicode_FromUnicode(NULL, tlen)))
        goto done;
    ru = PyUnicode_AS_UNICODE(result);
    for (j=0, sentinel=slen-2; j<slen; ++j) {
        if (   j < sentinel && WTF_IS_LATIN1(su[j]) && (su[j] & 0xFF) == '%'
            && WTF_IS_LATIN1(su[j+1]) && WTF_IS_HEX_DIGIT(su[j+1])
            && WTF_IS_LATIN1(su[j+2]) && WTF_IS_HEX_DIGIT(su[j+2])) {
            *ru++ =   (WTF_HEX_VALUE(su[j+1]) << 4)
                    + (WTF_HEX_VALUE(su[j+2]));
            j += 2;
        }
        else if (plus && su[j] == (unsigned char)'+') {
            *ru++ = (unsigned char)' ';
        }
        else {
            *ru++ = su[j];
        }
    }

done:
    Py_DECREF(string);
    return result;
}
Esempio n. 20
0
static PyObject *NormalizeSpace(PyObject *str)
{
  Py_UNICODE *start, *end, *p;
  PyObject *result;

  if (!PyUnicode_CheckExact(str)) {
    PyErr_SetString(PyExc_TypeError, "unicode object expected");
    return NULL;
  }

  start = PyUnicode_AS_UNICODE(str);
  end = start + PyUnicode_GET_SIZE(str);
  /* look for runs of contiguous whitespace */
  for (p = start; p < end; p++) {
    if (IS_XMLSPACE(p[0]) && IS_XMLSPACE(p[1])) break;
  }
  if (p == end && !IS_XMLSPACE(start[0]) && !IS_XMLSPACE(end[-1])) {
    /* no whitespace to remove; return original string */
    Py_INCREF(str);
    return str;
  }

  /* skip over leading whitespace */
  while (start < end && IS_XMLSPACE(start[0])) start++;
  /* skip over trailing whitespace */
  while (end > start && IS_XMLSPACE(end[-1])) end--;
  /* create the result string (it may be shrunk later) */
  result = PyUnicode_FromUnicode(start, (Py_ssize_t)(end - start));
  if (result) {
    start = PyUnicode_AS_UNICODE(result);
    end = start + PyUnicode_GET_SIZE(result);
    for (p = start; p < end; p++) {
      if (IS_XMLSPACE(*p)) {
        /* replace contiguous whitespace with a single space */
        *start++ = 0x20;
        p++;
        while (p < end && IS_XMLSPACE(*p)) p++;
      }
      *start++ = *p;
    }
    if (start != end) {
      Py_ssize_t newsize = (Py_ssize_t)(start - PyUnicode_AS_UNICODE(result));
      if (PyUnicode_Resize(&result, newsize) < 0) {
        Py_DECREF(result);
        return NULL;
      }
    }
  }
  return result;
}
Esempio n. 21
0
static int IsSpace(PyObject *str)
{
  Py_UNICODE *p, *e;

  if (!PyUnicode_CheckExact(str))
    return -1;

  p = PyUnicode_AS_UNICODE(str);
  e = p + PyUnicode_GET_SIZE(str);
  while (p < e) {
    if (!IS_XMLSPACE(*p)) return 0;
    p++;
  }
  return 1;
}
Esempio n. 22
0
/*
 * Charset encoder
 */
PyObject *
tdi_soup_encode_unicode(PyObject *value, PyObject *encoding)
{
    const char *cencoding;

    if (!PyUnicode_CheckExact(value) && !PyUnicode_Check(value)) {
        PyErr_SetString(TDI_E_TemplateEncodingError,
                        "Charset encoder takes unicode.");
        return NULL;
    }
    if (!(cencoding = PyString_AsString(encoding)))
        return NULL;

    return PyUnicode_AsEncodedString(value, cencoding, "xmlcharrefreplace");
}
Esempio n. 23
0
/*
 * HTML name encoder
 */
PyObject *
tdi_soup_encode_name(PyObject *name, PyObject *encoding)
{
    if (!(PyUnicode_CheckExact(name) || PyUnicode_Check(name))) {
        Py_INCREF(name);
    }
    else {
        const char *cencoding;

        if (!(cencoding = PyString_AsString(encoding)))
            return NULL;
        name = PyUnicode_AsEncodedString(name, cencoding, "strict");
    }
    return name;
}
Esempio n. 24
0
/* Intern selected string constants */
static int
intern_string_constants(PyObject *tuple)
{
    int modified = 0;
    Py_ssize_t i;

    for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
        PyObject *v = PyTuple_GET_ITEM(tuple, i);
        if (PyUnicode_CheckExact(v)) {
            if (PyUnicode_READY(v) == -1) {
                PyErr_Clear();
                continue;
            }
            if (all_name_chars(v)) {
                PyObject *w = v;
                PyUnicode_InternInPlace(&v);
                if (w != v) {
                    PyTuple_SET_ITEM(tuple, i, v);
                    modified = 1;
                }
            }
        }
        else if (PyTuple_CheckExact(v)) {
            intern_string_constants(v);
        }
        else if (PyFrozenSet_CheckExact(v)) {
            PyObject *w = v;
            PyObject *tmp = PySequence_Tuple(v);
            if (tmp == NULL) {
                PyErr_Clear();
                continue;
            }
            if (intern_string_constants(tmp)) {
                v = PyFrozenSet_New(tmp);
                if (v == NULL) {
                    PyErr_Clear();
                }
                else {
                    PyTuple_SET_ITEM(tuple, i, v);
                    Py_DECREF(w);
                    modified = 1;
                }
            }
            Py_DECREF(tmp);
        }
    }
    return modified;
}
Esempio n. 25
0
    PyObject* DetachValue()
    {
        // At this point, Trim should have been called by PostRead.

        if (bytesUsed == SQL_NULL_DATA || buffer == 0)
            Py_RETURN_NONE;

        if (usingStack)
        {
            if (dataType == SQL_C_CHAR || dataType == SQL_C_BINARY)
                return PyString_FromStringAndSize(buffer, bytesUsed);

            if (sizeof(wchar_t) == Py_UNICODE_SIZE)
                return PyUnicode_FromUnicode((const Py_UNICODE*)buffer, bytesUsed / element_size);

            return PyUnicode_FromWideChar((const wchar_t*)buffer, bytesUsed / element_size);
        }

        if (PyString_CheckExact(bufferOwner))
        {
            if (_PyString_Resize(&bufferOwner, bytesUsed) == -1)
                return 0;
            PyObject* tmp = bufferOwner;
            bufferOwner = 0;
            buffer      = 0;
            return tmp;
        }

        if (PyUnicode_CheckExact(bufferOwner))
        {
            if (PyUnicode_Resize(&bufferOwner, bytesUsed / element_size) == -1)
                return 0;
            PyObject* tmp = bufferOwner;
            bufferOwner = 0;
            buffer      = 0;
            return tmp;
        }

        // We have allocated our own wchar_t buffer and must now copy it to a Unicode object.
        PyObject* result = PyUnicode_FromWideChar((const wchar_t*)buffer, bytesUsed / element_size);
        if (result == 0)
            return false;
        free(buffer);
        buffer = 0;
        return result;
    }
Esempio n. 26
0
File: myson.c Progetto: S-YOU/myson
void encode_object(PyObject *arg) {
	Py_ssize_t size, i;
	char *p;

	if (PyString_CheckExact(arg)) {
		uint8_t *s, *sEnd;
		*d++ = '"';
		ENCODE_STR(arg, s, sEnd)
		*d++ = '"';
	} else if (PyUnicode_CheckExact(arg)) {
		*d++ = '"';
		Py_UNICODE *u, *uEnd;
		ENCODE_UNICODE(arg, u, uEnd)
		*d++ = '"';
	} else if (PyInt_CheckExact(arg) || PyLong_CheckExact(arg)) {
		i = PyInt_AsSsize_t(arg);
		uint64_t val = i < 0 ? *d++ = '-', (uint64_t)(~i + 1) : i;
		ITOA(val)
	} else if (PyList_CheckExact(arg)) {
Esempio n. 27
0
static PyObject*
_mktime_ymd(PyObject* self, PyObject* arg) {
	if (!PyUnicode_CheckExact(arg)) return NULL;

	Py_UNICODE *src = PyUnicode_AS_UNICODE(arg), *bPtr;
	size_t year = 0, month = 0, day = 0, timestamp = 0, multiplier, size = PyUnicode_GET_SIZE(arg);

	if (size >= 4) { ATOI(src, src + 4, year); }
	if (size >= 6) { ATOI(src + 4, src + 6, month); }
	if (size >= 8) { ATOI(src + 6, src + 8, day); }

	if (year < 1970 || year > 2038 || month > 13 || day > 32) {
		Py_RETURN_NONE;
	}

	timestamp = YEARS[year - 1970] + (IS_LEAP(year) ? MONTHS2[month] : MONTHS1[month]) + DAYS[day];

	return PyInt_FromSize_t(timestamp);
}
Esempio n. 28
0
static PyObject*
convert_nested(PyObject *ob, convert_func convert_string)
{
    /* dict. */
    if (PyDict_CheckExact(ob)) {
        return convert_dict(ob, convert_string);
    }

    /* sequence. */
    if (PyTuple_CheckExact(ob) || PyList_CheckExact(ob)) {
        return convert_seq(ob, convert_string);
    }

    /* numbers. */
    if (PyInt_CheckExact(ob) || PyLong_CheckExact(ob) || PyFloat_CheckExact(ob)) {
        Py_INCREF(ob);
        return ob;
    }

    /* bool. */
    if (PyBool_Check(ob)) {
        Py_INCREF(ob);
        return ob;
    }

    /* none. */
    if (ob == Py_None) {
        Py_INCREF(ob);
        return ob;
    }

    if (PyString_CheckExact(ob) || PyUnicode_CheckExact(ob)) {
        return convert_string(ob);
    }

    return PyErr_Format(
        PyExc_TypeError,
        "Got wrong type: %s",
        ob->ob_type->tp_name);
}
Esempio n. 29
0
/* Helper for code_new: return a shallow copy of a tuple that is
   guaranteed to contain exact strings, by converting string subclasses
   to exact strings and complaining if a non-string is found. */
static PyObject*
validate_and_copy_tuple(PyObject *tup)
{
    PyObject *newtuple;
    PyObject *item;
    Py_ssize_t i, len;

    len = PyTuple_GET_SIZE(tup);
    newtuple = PyTuple_New(len);
    if (newtuple == NULL)
        return NULL;

    for (i = 0; i < len; i++) {
        item = PyTuple_GET_ITEM(tup, i);
        if (PyUnicode_CheckExact(item)) {
            Py_INCREF(item);
        }
        else if (!PyUnicode_Check(item)) {
            PyErr_Format(
                PyExc_TypeError,
                "name tuples must contain only "
                "strings, not '%.500s'",
                item->ob_type->tp_name);
            Py_DECREF(newtuple);
            return NULL;
        }
        else {
            item = PyUnicode_FromUnicode(
                PyUnicode_AS_UNICODE(item),
                PyUnicode_GET_SIZE(item));
            if (item == NULL) {
                Py_DECREF(newtuple);
                return NULL;
            }
        }
        PyTuple_SET_ITEM(newtuple, i, item);
    }

    return newtuple;
}
Esempio n. 30
0
static int
TDI_RawNodeType_setcontent(rawnodeobject *self, PyObject *value,
                           void *closure)
{
    tdi_content_t *tmp;

    if (!value) {
        PyErr_SetString(PyExc_TypeError,
                        "Cannot delete the content attribute");
        return -1;
    }

    if (PyUnicode_CheckExact(value) || PyUnicode_Check(value)) {
        if (!(value = ENCODE_UNICODE(self->node, value)))
            return -1;
    }
    else if (!(value = PyObject_Str(value))) {
        return -1;
    }

    Py_INCREF(tdi_g_empty_dict);
    Py_CLEAR(self->node->namedict);
    self->node->namedict = tdi_g_empty_dict;
    tmp = self->node->content;
    self->node->content = NULL;
    if (!tmp) {
        tmp = tdi_content_new();
    }
    else {
        Py_CLEAR(tmp->clean);
        Py_CLEAR(tmp->with_escapes);
    }
    tmp->clean = value;
    Py_INCREF(value);
    tmp->with_escapes = value;
    self->node->content = tmp;

    return 0;
}