Exemple #1
0
static int SplitQName(PyObject *qualifiedName, PyObject **prefix,
                      PyObject **localName)
{
  int i;
  Py_UNICODE colon = ':';
  Py_ssize_t len = PyUnicode_GET_SIZE(qualifiedName);
  const Py_UNICODE *p = PyUnicode_AS_UNICODE(qualifiedName);

  for (i = 0; i < len; i++) {
    if (p[i] == colon) {
      PyObject *u, *v;
      u = PyUnicode_FromUnicode(p, (Py_ssize_t)i);
      if (u == NULL) {
        return 0;
      }
      /* skip over the colon */
      i++;
      v = PyUnicode_FromUnicode((p + i), (Py_ssize_t)(len - i));
      if (v == NULL) {
        Py_DECREF(u);
        return 0;
      }
      *prefix = u;
      *localName = v;
      return 1;
    }
  }

  /* No prefix */
  *prefix = Py_None;
  Py_INCREF(Py_None);
  *localName = qualifiedName;
  Py_INCREF(qualifiedName);
  return 1;
}
PyObject *
PyIMEngine::py_get_surrounding_text (PyIMEngineObject *self, PyObject *args)
{
	PyObject *tuple;

	int maxlen_before = -1;
	int maxlen_after = -1;

	if (!PyArg_ParseTuple (args, "|ii:get_surrounding_text", &maxlen_before, &maxlen_after))
		return NULL;

	WideString text;
	int cursor;
	int provided = self->engine.get_surrounding_text(text, cursor, maxlen_before, maxlen_after);
	
	tuple = PyTuple_New (2);

	if (!provided) {
		text = L"";
		cursor = 0;
	}

#if Py_UNICODE_SIZE == 4
	PyTuple_SET_ITEM (tuple, 0, PyUnicode_FromUnicode ((Py_UNICODE *)text.c_str(), text.length()));
#else
	gunichar2 *utf16_str = g_ucs4_to_utf16 (text.c_str(), -1, NULL, NULL, NULL);
	PyTuple_SET_ITEM (tuple, 0, PyUnicode_FromUnicode ((Py_UNICODE *)utf16_str, text.length()));
#endif
	PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong ((long) cursor));
	
	return tuple;
}
Exemple #3
0
EXPORT PyObject *PyUnicode_FromUnicodeString(const UnicodeString *string)
{
    if (!string)
    {
        Py_INCREF(Py_None);
        return Py_None;
    }
    else if (sizeof(Py_UNICODE) == sizeof(UChar))
        return PyUnicode_FromUnicode((const Py_UNICODE *) string->getBuffer(),
                                     (int) string->length());
    else
    {
        int len = string->length();
        PyObject *u = PyUnicode_FromUnicode(NULL, len);

        if (u)
        {
            Py_UNICODE *pchars = PyUnicode_AS_UNICODE(u);
            const UChar *chars = string->getBuffer();

            for (int i = 0; i < len; i++)
                pchars[i] = chars[i];
        }        

        return u;
    }
}
Exemple #4
0
PyObject *PyCodec_ReplaceErrors(PyObject *exc)
{
    PyObject *restuple;
    Py_ssize_t start;
    Py_ssize_t end;
    Py_ssize_t i;

    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
        PyObject *res;
        Py_UNICODE *p;
        if (PyUnicodeEncodeError_GetStart(exc, &start))
            return NULL;
        if (PyUnicodeEncodeError_GetEnd(exc, &end))
            return NULL;
        res = PyUnicode_FromUnicode(NULL, end-start);
        if (res == NULL)
            return NULL;
        for (p = PyUnicode_AS_UNICODE(res), i = start;
            i<end; ++p, ++i)
            *p = '?';
        restuple = Py_BuildValue("(On)", res, end);
        Py_DECREF(res);
        return restuple;
    }
    else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
        Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
        if (PyUnicodeDecodeError_GetEnd(exc, &end))
            return NULL;
        return Py_BuildValue("(u#n)", &res, (Py_ssize_t)1, end);
    }
    else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
        PyObject *res;
        Py_UNICODE *p;
        if (PyUnicodeTranslateError_GetStart(exc, &start))
            return NULL;
        if (PyUnicodeTranslateError_GetEnd(exc, &end))
            return NULL;
        res = PyUnicode_FromUnicode(NULL, end-start);
        if (res == NULL)
            return NULL;
        for (p = PyUnicode_AS_UNICODE(res), i = start;
            i<end; ++p, ++i)
            *p = Py_UNICODE_REPLACEMENT_CHARACTER;
        restuple = Py_BuildValue("(On)", res, end);
        Py_DECREF(res);
        return restuple;
    }
    else {
        wrong_exception_type(exc);
        return NULL;
    }
}
/*
 * READER
 */
static int
parse_save_field(ReaderObj *self)
{
	PyObject *field;

	field = PyUnicode_FromUnicode(self->field, self->field_len);
	if (field == NULL)
		return -1;
	self->field_len = 0;
	if (self->numeric_field) {
		PyObject *tmp;

		self->numeric_field = 0;
		tmp = PyNumber_Float(field);
		if (tmp == NULL) {
			Py_DECREF(field);
			return -1;
		}
		Py_DECREF(field);
		field = tmp;
	}
	PyList_Append(self->fields, field);
	Py_DECREF(field);
	return 0;
}
Exemple #6
0
static PyObject*
record_getstring(msiobj* record, PyObject* args)
{
    unsigned int field;
    unsigned int status;
    WCHAR buf[2000];
    WCHAR *res = buf;
    DWORD size = sizeof(buf);
    PyObject* string;
    
    if (!PyArg_ParseTuple(args, "I:GetString", &field))
        return NULL;
    status = MsiRecordGetStringW(record->h, field, res, &size);
    if (status == ERROR_MORE_DATA) {
        res = (WCHAR*) malloc((size + 1)*sizeof(WCHAR));
        if (res == NULL)
            return PyErr_NoMemory();
        status = MsiRecordGetStringW(record->h, field, res, &size);
    }
    if (status != ERROR_SUCCESS)
        return msierror((int) status);
    string = PyUnicode_FromUnicode(res, size);
    if (buf != res)
        free(res);
    return string;
}
Exemple #7
0
static PyObject*
uuidcreate(PyObject* obj, PyObject*args)
{
    UUID result;
    unsigned short *cresult;
    PyObject *oresult;
    
    /* May return ok, local only, and no address.
       For local only, the documentation says we still get a uuid.
       For RPC_S_UUID_NO_ADDRESS, it's not clear whether we can
       use the result. */
    if (UuidCreate(&result) == RPC_S_UUID_NO_ADDRESS) {
	PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result");
	return NULL;
    }

    if (UuidToStringW(&result, &cresult) == RPC_S_OUT_OF_MEMORY) {
	PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen");
	return NULL;
    }

    oresult = PyUnicode_FromUnicode(cresult, wcslen(cresult));
    RpcStringFreeW(&cresult);
    return oresult;

}
Exemple #8
0
static int xns_init(PyXPathNamespaceObject *self,
                    PyElementObject *parentNode,
                    PyObject *prefix,
                    PyObject *namespaceURI)
{
  if ((self == NULL || !PyXPathNamespace_Check(self)) ||
      (parentNode == NULL || !PyElement_Check(parentNode)) ||
      (prefix == NULL || !DOMString_NullCheck(prefix)) ||
      (namespaceURI == NULL || !DOMString_Check(namespaceURI))) {
    PyErr_BadInternalCall();
    return -1;
  }

  if (prefix == Py_None) {
    prefix = PyUnicode_FromUnicode(NULL, (Py_ssize_t)0);
    if (prefix == NULL) return -1;
  } else {
    Py_INCREF(prefix);
  }
  self->nodeName = prefix;

  Py_INCREF(namespaceURI);
  self->nodeValue = namespaceURI;

  Node_SET_PARENT(self, (PyNodeObject *) parentNode);

  return 0;
}
static int __init__(PyObject *self, PyObject *args, PyObject *kwds)
{
	ligolw_RowDumper *rowdumper = (ligolw_RowDumper *) self;
	Py_UNICODE default_delimiter = ',';

	rowdumper->delimiter = NULL;
	if(!PyArg_ParseTuple(args, "OO|U", &rowdumper->attributes, &rowdumper->formats, &rowdumper->delimiter))
		return -1;

	if(rowdumper->delimiter)
		Py_INCREF(rowdumper->delimiter);
	else
		rowdumper->delimiter = PyUnicode_FromUnicode(&default_delimiter, 1);
	rowdumper->attributes = llwtokenizer_build_attributes(rowdumper->attributes);
	rowdumper->formats = llwtokenizer_build_formats(rowdumper->formats);
	if(!rowdumper->delimiter || !rowdumper->attributes || !rowdumper->formats)
		/* memory clean-up happens in __del__() */
		return -1;

	if(PyTuple_GET_SIZE(rowdumper->attributes) != PyTuple_GET_SIZE(rowdumper->formats)) {
		/* memory clean-up happens in __del__() */
		PyErr_SetString(PyExc_ValueError, "len(attributes) != len(formats)");
		return -1;
	}

	rowdumper->rows_converted = 0;
	rowdumper->iter = Py_None;
	Py_INCREF(rowdumper->iter);
	rowdumper->tokens = Py_None;
	Py_INCREF(rowdumper->tokens);

	return 0;
}
void grid2utf(T const& grid_type,
                     boost::python::list& l,
                     std::vector<typename T::lookup_type>& key_order)
{
    using keys_type = std::map< typename T::lookup_type, typename T::value_type>;
    using keys_iterator = typename keys_type::iterator;

    typename T::data_type const& data = grid_type.data();
    typename T::feature_key_type const& feature_keys = grid_type.get_feature_keys();
    typename T::feature_key_type::const_iterator feature_pos;

    keys_type keys;
    // start counting at utf8 codepoint 32, aka space character
    std::uint16_t codepoint = 32;

    std::size_t array_size = data.width();
    for (std::size_t y = 0; y < data.height(); ++y)
    {
        std::uint16_t idx = 0;
        const std::unique_ptr<Py_UNICODE[]> line(new Py_UNICODE[array_size]);
        typename T::value_type const* row = data.get_row(y);
        for (std::size_t x = 0; x < data.width(); ++x)
        {
            typename T::value_type feature_id = row[x];
            feature_pos = feature_keys.find(feature_id);
            if (feature_pos != feature_keys.end())
            {
                mapnik::grid::lookup_type val = feature_pos->second;
                keys_iterator key_pos = keys.find(val);
                if (key_pos == keys.end())
                {
                    // Create a new entry for this key. Skip the codepoints that
                    // can't be encoded directly in JSON.
                    if (codepoint == 34) ++codepoint;      // Skip "
                    else if (codepoint == 92) ++codepoint; // Skip backslash
                    if (feature_id == mapnik::grid::base_mask)
                    {
                        keys[""] = codepoint;
                        key_order.push_back("");
                    }
                    else
                    {
                        keys[val] = codepoint;
                        key_order.push_back(val);
                    }
                    line[idx++] = static_cast<Py_UNICODE>(codepoint);
                    ++codepoint;
                }
                else
                {
                    line[idx++] = static_cast<Py_UNICODE>(key_pos->second);
                }
            }
            // else, shouldn't get here...
        }
        l.append(boost::python::object(
                     boost::python::handle<>(
                         PyUnicode_FromUnicode(line.get(), array_size))));
    }
}
Exemple #11
0
static PyObject*
escape_unicode(PyUnicodeObject *in)
{
	PyUnicodeObject *out;
	Py_UNICODE *inp = in->str;
	const Py_UNICODE *inp_end = in->str + in->length;
	Py_UNICODE *next_escp;
	Py_UNICODE *outp;
	Py_ssize_t delta=0, erepl=0, delta_len=0;

	/* First we need to figure out how long the escaped string will be */
	while (*(inp) || inp < inp_end) {
		if (*inp < ESCAPED_CHARS_TABLE_SIZE && escaped_chars_delta_len[*inp]) {
			delta += escaped_chars_delta_len[*inp];
			++erepl;
		}
		++inp;
	}

	/* Do we need to escape anything at all? */
	if (!erepl) {
		Py_INCREF(in);
		return (PyObject*)in;
	}

	out = (PyUnicodeObject*)PyUnicode_FromUnicode(NULL, in->length + delta);
	if (!out)
		return NULL;

	outp = out->str;
	inp = in->str;
	while (erepl-- > 0) {
		/* look for the next substitution */
		next_escp = inp;
		while (next_escp < inp_end) {
			if (*next_escp < ESCAPED_CHARS_TABLE_SIZE &&
			    (delta_len = escaped_chars_delta_len[*next_escp])) {
				++delta_len;
				break;
			}
			++next_escp;
		}
		
		if (next_escp > inp) {
			/* copy unescaped chars between inp and next_escp */
			Py_UNICODE_COPY(outp, inp, next_escp-inp);
			outp += next_escp - inp;
		}

		/* escape 'next_escp' */
		Py_UNICODE_COPY(outp, escaped_chars_repl[*next_escp], delta_len);
		outp += delta_len;

		inp = next_escp + 1;
	}
	if (inp < inp_end)
		Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str));

	return (PyObject*)out;
}
Exemple #12
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;
}
Exemple #13
0
PyObject * fudgepyc_convertStringToPython ( FudgeString source )
{
    PyObject * target = 0;
    fudge_byte * buffer;
    size_t written, bufsize;

    bufsize = FudgeString_getSize ( source ) * sizeof ( Py_UNICODE );
    if ( ! ( buffer = ( fudge_byte * ) PyMem_Malloc ( bufsize ) ) )
        return 0;

    /* See comment in fudgepyc_convertPythonToString for explanation */
    switch ( sizeof ( Py_UNICODE ) )
    {
        case 2:
            written = FudgeString_copyToUTF16 ( buffer, bufsize, source );
            break;

        case 4:
            written = FudgeString_copyToUTF32 ( buffer, bufsize, source );
            break;

        default:
            exception_raise_any ( PyExc_RuntimeError,
                                  "Cannot decode Fudge string; Python "
                                  "interpreter not using UCS2 or UCS4 for"
                                  "internal unicode encoding" );
            return 0;
    }

    target = PyUnicode_FromUnicode ( ( Py_UNICODE * ) buffer,
                                     written / sizeof ( Py_UNICODE ) );
    PyMem_Free ( buffer );
    return target;
}
Exemple #14
0
static PyObject *PyLoadModule(PyObject *self, PyObject *args)
{
#ifdef UNICODE
	static char *fmt="u";
#else
	static char *fmt="s";
#endif
    TCHAR *modName=NULL;
    if (!PyArg_ParseTuple(args, fmt, &modName))
        return NULL;
    HINSTANCE hinst = LoadLibrary(modName);

    if (hinst == NULL) {
        Py_INCREF(Py_None);
        return Py_None;
    }
    TCHAR buf[_MAX_PATH];
    if (GetModuleFileName(hinst, buf, sizeof(buf)/sizeof(buf[0]))==0) {
        Py_INCREF(Py_None);
        return Py_None;
    }
#ifdef UNICODE
    return PyUnicode_FromUnicode(buf, wcslen(buf));
#else
	return PyString_FromString(buf);
#endif
}
Exemple #15
0
PyObject* PyUnicode_FromSQLWCHAR(const SQLWCHAR* sz, Py_ssize_t cch)
{
    // Create a Python Unicode object from a zero-terminated SQLWCHAR.

    if (SQLWCHAR_SIZE == Py_UNICODE_SIZE)
    {
        // The ODBC Unicode and Python Unicode types are the same size.  Cast the ODBC type to the Python type and use
        // a fast function.
        return PyUnicode_FromUnicode((const Py_UNICODE*)sz, cch);
    }
    
#ifdef HAVE_WCHAR_H
    if (WCHAR_T_SIZE == SQLWCHAR_SIZE)
    {
        // The ODBC Unicode is the same as wchar_t.  Python provides a function for that.
        return PyUnicode_FromWideChar((const wchar_t*)sz, cch);
    }
#endif

    // There is no conversion, so we will copy it ourselves with a simple cast.

    if (Py_UNICODE_SIZE < SQLWCHAR_SIZE)
    {
        // We are casting from a larger size to a smaller one, so we'll make sure they all fit.

        for (Py_ssize_t i = 0; i < cch; i++)
        {
            if (((Py_ssize_t)sz[i]) > MAX_PY_UNICODE)
            {
                PyErr_Format(PyExc_ValueError, "Cannot convert from SQLWCHAR %zd to Unicode.  Value is too large.", (Py_ssize_t)sz[i]);
                return 0;
            }
        }
        
    }
    
    Object result(PyUnicode_FromUnicode(0, cch));
    if (!result)
        return 0;

    Py_UNICODE* pch = PyUnicode_AS_UNICODE(result.Get());
    for (Py_ssize_t i = 0; i < cch; i++)
        pch[i] = (Py_UNICODE)sz[i];
    
    return result.Detach();
}
void grid2utf2(T const& grid_type,
               boost::python::list& l,
               std::vector<typename T::lookup_type>& key_order,
               unsigned int resolution)
{
    using keys_type = std::map< typename T::lookup_type, typename T::value_type>;
    using keys_iterator = typename keys_type::const_iterator;

    typename T::data_type const& data = grid_type.data();
    typename T::feature_key_type const& feature_keys = grid_type.get_feature_keys();
    typename T::feature_key_type::const_iterator feature_pos;

    keys_type keys;
    // start counting at utf8 codepoint 32, aka space character
    uint16_t codepoint = 32;

    mapnik::grid::data_type target(data.width()/resolution,data.height()/resolution);
    mapnik::scale_grid(target,grid_type.data(),0.0,0.0);

    unsigned array_size = target.width();
    for (unsigned y = 0; y < target.height(); ++y)
    {
        uint16_t idx = 0;
        const std::unique_ptr<Py_UNICODE[]> line(new Py_UNICODE[array_size]);
        mapnik::grid::value_type * row = target.getRow(y);
        unsigned x;
        for (x = 0; x < target.width(); ++x)
        {
            feature_pos = feature_keys.find(row[x]);
            if (feature_pos != feature_keys.end())
            {
                mapnik::grid::lookup_type val = feature_pos->second;
                keys_iterator key_pos = keys.find(val);
                if (key_pos == keys.end())
                {
                    // Create a new entry for this key. Skip the codepoints that
                    // can't be encoded directly in JSON.
                    if (codepoint == 34) ++codepoint;      // Skip "
                    else if (codepoint == 92) ++codepoint; // Skip backslash
                    keys[val] = codepoint;
                    key_order.push_back(val);
                    line[idx++] = static_cast<Py_UNICODE>(codepoint);
                    ++codepoint;
                }
                else
                {
                    line[idx++] = static_cast<Py_UNICODE>(key_pos->second);
                }
            }
            // else, shouldn't get here...
        }
        l.append(boost::python::object(
                     boost::python::handle<>(
                         PyUnicode_FromUnicode(line.get(), array_size))));
    }
}
Exemple #17
0
static PyObject* MakeConnectionString(PyObject* existing, PyObject* parts)
{
    // Creates a connection string from an optional existing connection string plus a dictionary of keyword value
    // pairs.
    //
    // existing
    //   Optional Unicode connection string we will be appending to.  Used when a partial connection string is passed
    //   in, followed by keyword parameters:
    //
    //   connect("driver={x};database={y}", user='******')
    //
    // parts
    //   A dictionary of text keywords and text values that will be appended.

    I(PyUnicode_Check(existing));

    Py_ssize_t length = 0;      // length in *characters*
    if (existing)
        length = Text_Size(existing) + 1; // + 1 to add a trailing semicolon

    Py_ssize_t pos = 0;
    PyObject* key = 0;
    PyObject* value = 0;

    while (PyDict_Next(parts, &pos, &key, &value))
    {
        length += Text_Size(key) + 1 + Text_Size(value) + 1; // key=value;
    }

    PyObject* result = PyUnicode_FromUnicode(0, length);
    if (!result)
        return 0;

    Py_UNICODE* buffer = PyUnicode_AS_UNICODE(result);
    Py_ssize_t offset = 0;

    if (existing)
    {
        offset += TextCopyToUnicode(&buffer[offset], existing);
        buffer[offset++] = (Py_UNICODE)';';
    }

    pos = 0;
    while (PyDict_Next(parts, &pos, &key, &value))
    {
        offset += TextCopyToUnicode(&buffer[offset], key);
        buffer[offset++] = (Py_UNICODE)'=';

        offset += TextCopyToUnicode(&buffer[offset], value);
        buffer[offset++] = (Py_UNICODE)';';
    }

    I(offset == length);

    return result;
}
static PyObject *wpDbResult_getstring(wpDbResult *self, PyObject *args) {
	unsigned int pos;
	if (!PyArg_ParseTuple(args, "I:dbresult.getstring(position)", &pos)) {
		return 0;
	}

	QString value = self->result->getString(pos);

	return PyUnicode_FromUnicode((Py_UNICODE*)value.ucs2(), value.length());
}
Exemple #19
0
PyObject *
PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
{
	PyObject *name = filename ?
	                 PyUnicode_FromUnicode(filename, wcslen(filename)) :
	                 NULL;
	PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
	Py_XDECREF(name);
	return result;
}
static PyObject *
get_nullchar_as_None(Py_UNICODE c)
{
        if (c == '\0') {
                Py_INCREF(Py_None);
                return Py_None;
        }
        else
                return PyUnicode_FromUnicode((Py_UNICODE *)&c, 1);
}
Exemple #21
0
PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
{
    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
        PyObject *restuple;
        PyObject *object;
        Py_ssize_t start;
        Py_ssize_t end;
        PyObject *res;
        Py_UNICODE *p;
        Py_UNICODE *startp;
        Py_UNICODE *outp;
        int ressize;
        if (PyUnicodeEncodeError_GetStart(exc, &start))
            return NULL;
        if (PyUnicodeEncodeError_GetEnd(exc, &end))
            return NULL;
        if (!(object = PyUnicodeEncodeError_GetObject(exc)))
            return NULL;
        startp = PyUnicode_AS_UNICODE(object);
        for (p = startp+start, ressize = 0; p < startp+end; ++p) {
#ifdef Py_UNICODE_WIDE
            if (*p >= 0x00010000)
                ressize += 1+1+8;
            else
#endif
            if (*p >= 0x100) {
                ressize += 1+1+4;
            }
            else
                ressize += 1+1+2;
        }
        res = PyUnicode_FromUnicode(NULL, ressize);
        if (res==NULL)
            return NULL;
        for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
            p < startp+end; ++p) {
            Py_UNICODE c = *p;
            *outp++ = '\\';
#ifdef Py_UNICODE_WIDE
            if (c >= 0x00010000) {
                *outp++ = 'U';
                *outp++ = hexdigits[(c>>28)&0xf];
                *outp++ = hexdigits[(c>>24)&0xf];
                *outp++ = hexdigits[(c>>20)&0xf];
                *outp++ = hexdigits[(c>>16)&0xf];
                *outp++ = hexdigits[(c>>12)&0xf];
                *outp++ = hexdigits[(c>>8)&0xf];
            }
            else
#endif
            if (c >= 0x100) {
                *outp++ = 'u';
                *outp++ = hexdigits[(c>>12)&0xf];
                *outp++ = hexdigits[(c>>8)&0xf];
            }
Exemple #22
0
static PyObject *_pyhangulic_flush(PY_HANGULIC *self, PyObject *args)
{
#ifndef Py_UNICODE_WIDE
    int i;
    Py_UNICODE *buf;
#endif /* !Py_UNICODE_WIDE */
    int len;
    const ucschar *str;

    str = hangul_ic_flush(self->hic);
    len = ucscharlen(str);

#ifdef Py_UNICODE_WIDE
    return PyUnicode_FromUnicode((Py_UNICODE*)str, len);
#else  /* Py_UNICODE_WIDE */
    buf = alloca(sizeof(Py_UNICODE) * len);
    for (i = 0; i < len; i++)
	buf[i] = str[i];
    return PyUnicode_FromUnicode(buf, len);
#endif /* Py_UNICODE_WIDE */
}
Exemple #23
0
PyObject* JPyString::fromUnicode(const jchar* str, int len) 
{
	Py_UNICODE* value = new Py_UNICODE[len+1];
	value[len] = 0;
	for (int i = 0; i < len; i++)
	{
		value[i] = (Py_UNICODE)str[i];
	}
	PY_CHECK( PyObject* obj = PyUnicode_FromUnicode(value, len) );
	delete[] value;
	return obj;
}
Exemple #24
0
static PyObject *
filters_uwebsafe(PyObject * self, PyObject *args) 
{
  PyObject * com;
  Py_UNICODE * input_buffer;
  Py_UNICODE *buffer;
  PyObject * res;
  int ic=0, ib=0;
  int len;
  Py_UNICODE c;
  if (!(com = unicode_arg(args))) return NULL;
  input_buffer = PyUnicode_AS_UNICODE(com);
  len = PyUnicode_GetSize(com);

  buffer = (Py_UNICODE*)malloc(6*len*sizeof(Py_UNICODE));
  for(ic = 0, ib = 0; ic < len; ic++, ib++) {
    c = input_buffer[ic];
    if (c == '&') {
      buffer[ib++] = (Py_UNICODE)'&';
      buffer[ib++] = (Py_UNICODE)'a';
      buffer[ib++] = (Py_UNICODE)'m';
      buffer[ib++] = (Py_UNICODE)'p';
      buffer[ib]   = (Py_UNICODE)';';
    }
    else if(c == (Py_UNICODE)'<') {
      buffer[ib++] = (Py_UNICODE)'&';
      buffer[ib++] = (Py_UNICODE)'l';
      buffer[ib++] = (Py_UNICODE)'t';
      buffer[ib]   = (Py_UNICODE)';';
    }
    else if(c == (Py_UNICODE)'>') {
      buffer[ib++] = (Py_UNICODE)'&';
      buffer[ib++] = (Py_UNICODE)'g';
      buffer[ib++] = (Py_UNICODE)'t';
      buffer[ib]   = (Py_UNICODE)';';
    }
    else if(c == (Py_UNICODE)'"') {
      buffer[ib++] = (Py_UNICODE)'&';
      buffer[ib++] = (Py_UNICODE)'q';
      buffer[ib++] = (Py_UNICODE)'u';
      buffer[ib++] = (Py_UNICODE)'o';
      buffer[ib++] = (Py_UNICODE)'t';
      buffer[ib]   = (Py_UNICODE)';';      
    }
    else {
      buffer[ib] = input_buffer[ic];
    }
  }
  res = PyUnicode_FromUnicode(buffer, ib);
  free(buffer);
  return res;

}
static
PyUnicodeObject *prepareString(Splitter *self,PyUnicodeObject *o)

{
    PyUnicodeObject *u;

    u = (PyUnicodeObject*) PyUnicode_FromUnicode(o->str, o->length);
    if (u != NULL){
        if (self->casefolding)
          fixlower(u);
    }
    return  u;
}
Exemple #26
0
static PyObject *node_to_string(PyObject *node)
{
  PyObject *nodeType, *result, *descendants;

  nodeType = PyObject_GetAttrString(node, "nodeType");
  if (nodeType == NULL) {
    PyErr_Clear();
    return PyUnicode_FromUnicode(NULL, 0);
  }
  
  /* convert DOM node to string */
  switch (PyInt_AsLong(nodeType)) {
  case DOCUMENT_NODE:
  case ELEMENT_NODE:
    /* The concatenation of all text descendants in document order */
    descendants = PyList_New(0);
    if (node_descendants(node, descendants) == NULL) {
      Py_DECREF(descendants);
      return NULL;
    }
    result = PyUnicode_Join(PyUnicode_FromUnicode(NULL, 0), descendants);
    Py_DECREF(descendants);
    break;
  case ATTRIBUTE_NODE:
  case XPATH_NAMESPACE_NODE:
    result = PyObject_GetAttrString(node, "value");
    break;
  case PROCESSING_INSTRUCTION_NODE:
  case COMMENT_NODE:
  case TEXT_NODE:
    result = PyObject_GetAttrString(node, "data");
    break;
  default:
    result = PyUnicode_FromUnicode(NULL, 0);
  }

  Py_DECREF(nodeType);
  return result;
}
Exemple #27
0
PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
	int ierr,
	const Py_UNICODE *filename)
{
	PyObject *name = filename ?
	                 PyUnicode_FromUnicode(filename, wcslen(filename)) :
	                 NULL;
	PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
						      PyExc_WindowsError,
						      ierr, name);
	Py_XDECREF(name);
	return result;
}
Exemple #28
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;
}
static PyObject* MakeConnectionString(PyObject* existing, PyObject* parts)
{
    // Creates a connection string from an optional existing connection string plus a dictionary of keyword value
    // pairs.  The keywords must be String objects and the values must be Unicode objects.

    Py_ssize_t length = 0;
    if (existing)
        length = PyUnicode_GET_SIZE(existing) + 1; // + 1 to add a trailing 

    Py_ssize_t pos = 0;
    PyObject* key = 0;
    PyObject* value = 0;

    while (PyDict_Next(parts, &pos, &key, &value))
    {
        length += PyString_GET_SIZE(key) + 1 + PyUnicode_GET_SIZE(value) + 1; // key=value;
    }
    
    PyObject* result = PyUnicode_FromUnicode(0, length);
    if (!result)
        return 0;

    Py_UNICODE* buffer = PyUnicode_AS_UNICODE(result);
    Py_ssize_t offset = 0;

    if (existing)
    {
        memcpy(&buffer[offset], PyUnicode_AS_UNICODE(existing), PyUnicode_GET_SIZE(existing) * sizeof(Py_UNICODE));
        offset += PyUnicode_GET_SIZE(existing);
        buffer[offset++] = (Py_UNICODE)';';
    }

    pos = 0;
    while (PyDict_Next(parts, &pos, &key, &value))
    {
        const char* szKey = PyString_AS_STRING(key);
        for (int i = 0; i < PyString_GET_SIZE(key); i++)
            buffer[offset++] = (Py_UNICODE)szKey[i];

        buffer[offset++] = (Py_UNICODE)'=';

        memcpy(&buffer[offset], PyUnicode_AS_UNICODE(value), PyUnicode_GET_SIZE(value) * sizeof(Py_UNICODE));
        offset += PyUnicode_GET_SIZE(value);
        
        buffer[offset++] = (Py_UNICODE)';';
    }

    I(offset == length);

    return result;
}
Exemple #30
0
static PyObject*
html_check_spelling(PyObject *self, PyObject *args) {
#if PY_VERSION_HEX >= 0x03030000 
#error Not implemented for python >= 3.3
#endif
    PyObject *ans = NULL, *temp = NULL, *items = NULL, *text = NULL, *fmt = NULL, *locale = NULL, *sfmt = NULL, *_store_locale = NULL, *t = NULL, *utmp = NULL;
    long text_len = 0, start = 0, length = 0, ppos = 0; 
    int store_locale = 0, ok = 0;
    Py_ssize_t i = 0, j = 0;

    if (!PyArg_ParseTuple(args, "OlOOOO", &text, &text_len, &fmt, &locale, &sfmt, &_store_locale)) return NULL;
    store_locale = PyObject_IsTrue(_store_locale);
    temp = PyObject_GetAttrString(locale, "langcode");
    if (temp == NULL) goto error;
    items = PyObject_CallFunctionObjArgs(split, text, temp, NULL); 
    Py_DECREF(temp); temp = NULL;
    if (items == NULL) goto error;
    ans = PyTuple_New((2 * PyList_GET_SIZE(items)) + 1);
    if (ans == NULL) { PyErr_NoMemory(); goto error; }

#define APPEND(x, y) t = Py_BuildValue("lO", (x), y); if (t == NULL) goto error; PyTuple_SET_ITEM(ans, j, t); j += 1;

    for (i = 0, j = 0; i < PyList_GET_SIZE(items); i++) {
        temp = PyList_GET_ITEM(items, i);
        start = PyInt_AS_LONG(PyTuple_GET_ITEM(temp, 0)); length = PyInt_AS_LONG(PyTuple_GET_ITEM(temp, 1));
        temp = NULL;

        if (start > ppos) { APPEND(start - ppos, fmt) }
        ppos = start + length;

        utmp = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(text) + start, length);
        if (utmp == NULL) { PyErr_NoMemory(); goto error; }
        temp = PyObject_CallFunctionObjArgs(recognized, utmp, locale, NULL);
        Py_DECREF(utmp); utmp = NULL;
        if (temp == NULL) goto error;
        ok = PyObject_IsTrue(temp);
        Py_DECREF(temp); temp = NULL;

        if (ok) {
            APPEND(length, fmt)
        } else {
            if (store_locale) {
                temp = PyObject_CallFunctionObjArgs(spell_property, sfmt, locale, NULL);
                if (temp == NULL) goto error;
                APPEND(length, temp);
                Py_DECREF(temp); temp = NULL;
            } else {
                APPEND(length, sfmt);
            }
        }
    }