Ejemplo n.º 1
0
/* u - a single wchar_t character */
static PyObject *
u_set(void *ptr, PyObject *value, Py_ssize_t size)
{
    Py_ssize_t len;
    if (PyBytes_Check(value)) {
        value = PyUnicode_FromEncodedObject(value,
                                            _ctypes_conversion_encoding,
                                            _ctypes_conversion_errors);
        if (!value)
            return NULL;
    } else if (!PyUnicode_Check(value)) {
        PyErr_Format(PyExc_TypeError,
                        "unicode string expected instead of %s instance",
                        value->ob_type->tp_name);
        return NULL;
    } else
        Py_INCREF(value);

    len = PyUnicode_GET_SIZE(value);
    if (len != 1) {
        Py_DECREF(value);
        PyErr_SetString(PyExc_TypeError,
                        "one character unicode string expected");
        return NULL;
    }

    *(wchar_t *)ptr = PyUnicode_AS_UNICODE(value)[0];
    Py_DECREF(value);

    _RET(value);
}
Ejemplo n.º 2
0
/* U - a unicode string */
static PyObject *
U_get(void *ptr, Py_ssize_t size)
{
    PyObject *result;
    Py_ssize_t len;
    Py_UNICODE *p;

    size /= sizeof(wchar_t); /* we count character units here, not bytes */

    result = PyUnicode_FromWideChar((wchar_t *)ptr, size);
    if (!result)
        return NULL;
    /* We need 'result' to be able to count the characters with wcslen,
       since ptr may not be NUL terminated.  If the length is smaller (if
       it was actually NUL terminated, we construct a new one and throw
       away the result.
    */
    /* chop off at the first NUL character, if any. */
    p = PyUnicode_AS_UNICODE(result);
    for (len = 0; len < size; ++len)
        if (!p[len])
            break;

    if (len < size) {
        PyObject *ob = PyUnicode_FromWideChar((wchar_t *)ptr, len);
        Py_DECREF(result);
        return ob;
    }
    return result;
}
Ejemplo n.º 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;
    }
}
Ejemplo n.º 4
0
static PyObject*
automaton_search_iter_new(
	Automaton* automaton,
	PyObject* object,
	int start,
	int end
) {
	AutomatonSearchIter* iter;

	iter = (AutomatonSearchIter*)PyObject_New(AutomatonSearchIter, &automaton_search_iter_type);
	if (iter == NULL)
		return NULL;

	iter->automaton = automaton;
	iter->version	= automaton->version;
	iter->object	= object;
#ifdef AHOCORASICK_UNICODE
	iter->data		= PyUnicode_AS_UNICODE(object);
#else
	iter->data		= (uint8_t*)PyBytes_AS_STRING(object);
#endif

	iter->state	= automaton->root;
	iter->output= NULL;
	iter->shift	= 0;
	iter->index	= start - 1;	// -1 because first instruction in next() increments index
	iter->end	= end;

	Py_INCREF(iter->automaton);
	Py_INCREF(iter->object);

	return (PyObject*)iter;
}
Ejemplo n.º 5
0
static PyObject *
int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	PyObject *x = NULL;
	int base = -909;
	static char *kwlist[] = {"x", "base", 0};

	if (type != &PyInt_Type)
		return int_subtype_new(type, args, kwds); /* Wimp out */
	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
					 &x, &base))
		return NULL;
	if (x == NULL)
		return PyInt_FromLong(0L);
	if (base == -909)
		return PyNumber_Int(x);
	if (PyString_Check(x))
		return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
#ifdef Py_USING_UNICODE
	if (PyUnicode_Check(x))
		return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
					 PyUnicode_GET_SIZE(x),
					 base);
#endif
	PyErr_SetString(PyExc_TypeError,
			"int() can't convert non-string with explicit base");
	return NULL;
}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
0
JCharString JPyString::asJCharString(PyObject* obj) 
{	
	PyObject* torelease = NULL;
	TRACE_IN("JPyString::asJCharString");
	
	if (PyString_Check(obj))
	{
		PY_CHECK( obj = PyUnicode_FromObject(obj) );	
		torelease = obj;
	}

	Py_UNICODE* val = PyUnicode_AS_UNICODE(obj);	
	Py_ssize_t length = JPyObject::length(obj);
	JCharString res(length);
	for (int i = 0; val[i] != 0; i++)
	{
		res[i] = (jchar)val[i];
	}

	if (torelease != NULL)
	{
		Py_DECREF(torelease);
	}

	return res;
	TRACE_OUT;
}
Ejemplo n.º 8
0
BlockLocator *
BlockLocator_new(PyUnicodeObject* codestr)
{
	BlockLocator *self;

	#ifdef DEBUG
		fprintf(stderr, "%s\n", __PRETTY_FUNCTION__);
	#endif

	self = PyMem_New(BlockLocator, 1);
	if (self) {
		memset(self, 0, sizeof(BlockLocator));
		Py_INCREF(codestr);
		self->py_codestr = codestr;
		self->codestr = PyUnicode_AS_UNICODE(codestr);
		self->codestr_sz = PyUnicode_GetSize((PyObject*)codestr);
		self->codestr_ptr = self->codestr;
		self->lineno = 1;
		self->par = 0;
		self->instr = 0;
		self->depth = 0;
		self->skip = 0;
		self->init = self->codestr;
		self->lose = self->codestr;
		self->start = NULL;
		self->end = NULL;
		#ifdef DEBUG
			fprintf(stderr, "\tScss BlockLocator object created (%ld units)!\n", self->codestr_sz);
		#endif
	}
	return self;
}
Ejemplo n.º 9
0
static PyObject*
_mktime_u(PyObject* self, PyObject* arg) {
	Py_UNICODE *src = PyUnicode_AS_UNICODE(arg), *s = src, digitBuffer[32], *d = digitBuffer, *dEnd = digitBuffer + 32, *bPtr;
	size_t tbuf[] = {0, 0, 0, 0, 0, 0}, timestamp = 0;

	PROCESS_MKTIME;
}
Ejemplo n.º 10
0
static int __init__(PyObject *self, PyObject *args, PyObject *kwds)
{
	ligolw_Tokenizer *tokenizer = (ligolw_Tokenizer *) self;
	PyObject *arg;

	if(!PyArg_ParseTuple(args, "U", &arg))
		return -1;

	if(PyUnicode_GET_SIZE(arg) != 1) {
		PyErr_SetString(PyExc_ValueError, "len(delimiter) != 1");
		return -1;
	}

	tokenizer->delimiter = *PyUnicode_AS_UNICODE(arg);
	tokenizer->quote_characters = default_quote_characters;
	tokenizer->escape_character = '\\';
	tokenizer->types = malloc(1 * sizeof(*tokenizer->types));
	tokenizer->types_length = &tokenizer->types[1];
	tokenizer->types[0] = (PyObject *) &PyUnicode_Type;
	Py_INCREF(tokenizer->types[0]);
	tokenizer->type = tokenizer->types;
	tokenizer->allocation = 0;
	tokenizer->data = NULL;
	tokenizer->length = tokenizer->data;
	tokenizer->pos = tokenizer->data;

	return 0;
}
Ejemplo n.º 11
0
// Initialize the global decimal character and thousands separator character, used when parsing decimal
// objects.
//
static void init_locale_info()
{
    Object module = PyImport_ImportModule("locale");
    if (!module)
    {
        PyErr_Clear();
        return;
    }

    Object ldict = PyObject_CallMethod(module, "localeconv", 0);
    if (!ldict)
    {
        PyErr_Clear();
        return;
    }

    PyObject* value = PyDict_GetItemString(ldict, "decimal_point");
    if (value)
    {
        if (PyBytes_Check(value) && PyBytes_Size(value) == 1)
            chDecimal = (Py_UNICODE)PyBytes_AS_STRING(value)[0];
        if (PyUnicode_Check(value) && PyUnicode_GET_SIZE(value) == 1)
            chDecimal = PyUnicode_AS_UNICODE(value)[0];
    }
}
Ejemplo n.º 12
0
PyObject* PyCodec_ReplaceErrors(PyObject* exc) noexcept {
    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;
    }
}
Ejemplo n.º 13
0
static bool Connect(PyObject* pConnectString, HDBC hdbc, bool fAnsi)
{
    // This should have been checked by the global connect function.
    I(PyString_Check(pConnectString) || PyUnicode_Check(pConnectString));

    const int cchMax = 600;

    if (PySequence_Length(pConnectString) >= cchMax)
    {
        PyErr_SetString(PyExc_TypeError, "connection string too long");
        return false;
    }

    // The driver manager determines if the app is a Unicode app based on whether we call SQLDriverConnectA or
    // SQLDriverConnectW.  Some drivers, notably Microsoft Access/Jet, change their behavior based on this, so we try
    // the Unicode version first.  (The Access driver only supports Unicode text, but SQLDescribeCol returns SQL_CHAR
    // instead of SQL_WCHAR if we connect with the ANSI version.  Obviously this causes lots of errors since we believe
    // what it tells us (SQL_CHAR).)

    // Python supports only UCS-2 and UCS-4, so we shouldn't need to worry about receiving surrogate pairs.  However,
    // Windows does use UCS-16, so it is possible something would be misinterpreted as one.  We may need to examine
    // this more.

    SQLRETURN ret;

    if (!fAnsi)
    {
        SQLWCHAR szConnectW[cchMax];
        if (PyUnicode_Check(pConnectString))
        {
            Py_UNICODE* p = PyUnicode_AS_UNICODE(pConnectString);
            for (int i = 0, c = PyUnicode_GET_SIZE(pConnectString); i <= c; i++)
                szConnectW[i] = (wchar_t)p[i];
        }
        else
        {
            const char* p = PyString_AS_STRING(pConnectString);
            for (int i = 0, c = PyString_GET_SIZE(pConnectString); i <= c; i++)
                szConnectW[i] = (wchar_t)p[i];
        }

        Py_BEGIN_ALLOW_THREADS
        ret = SQLDriverConnectW(hdbc, 0, szConnectW, SQL_NTS, 0, 0, 0, SQL_DRIVER_NOPROMPT);
        Py_END_ALLOW_THREADS
        if (SQL_SUCCEEDED(ret))
            return true;

        // The Unicode function failed.  If the error is that the driver doesn't have a Unicode version (IM001), continue
        // to the ANSI version.

        PyObject* error = GetErrorFromHandle("SQLDriverConnectW", hdbc, SQL_NULL_HANDLE);
        if (!HasSqlState(error, "IM001"))
        {
            RaiseErrorFromException(error);
            return false;
        }
        Py_XDECREF(error);
    }
Ejemplo n.º 14
0
static NUMBA_INLINE Py_UCS4 __Numba_PyObject_AsPy_UCS4(PyObject* x) {
   long ival;
   if (PyUnicode_Check(x)) {
       Py_ssize_t length;
       #if CYTHON_PEP393_ENABLED
       length = PyUnicode_GET_LENGTH(x);
       if (likely(length == 1)) {
           return PyUnicode_READ_CHAR(x, 0);
       }
       #else
       length = PyUnicode_GET_SIZE(x);
       if (likely(length == 1)) {
           return PyUnicode_AS_UNICODE(x)[0];
       }
       #if Py_UNICODE_SIZE == 2
       else if (PyUnicode_GET_SIZE(x) == 2) {
           Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0];
           if (high_val >= 0xD800 && high_val <= 0xDBFF) {
               Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1];
               if (low_val >= 0xDC00 && low_val <= 0xDFFF) {
                   return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1)));
               }
           }
       }
       #endif
       #endif
       PyErr_Format(PyExc_ValueError,
                    "only single character unicode strings can be converted to Py_UCS4, "
                    "got length %" CYTHON_FORMAT_SSIZE_T "d", length);
       return (Py_UCS4)-1;
   }
   ival = __Numba_PyInt_AsLong(x);
   if (unlikely(ival < 0)) {
       if (!PyErr_Occurred())
           PyErr_SetString(PyExc_OverflowError,
                           "cannot convert negative value to Py_UCS4");
       return (Py_UCS4)-1;
   } else if (unlikely(ival > 1114111)) {
       PyErr_SetString(PyExc_OverflowError,
                       "value too large to convert to Py_UCS4");
       return (Py_UCS4)-1;
   }
   return (Py_UCS4)ival;
}
Ejemplo n.º 15
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;
}
Ejemplo n.º 16
0
static Box* _intNew(Box* val, Box* base) {
    if (isSubclass(val->cls, int_cls)) {
        RELEASE_ASSERT(!base, "");
        BoxedInt* n = static_cast<BoxedInt*>(val);
        if (val->cls == int_cls)
            return n;
        return new BoxedInt(n->n);
    } else if (isSubclass(val->cls, str_cls)) {
        int base_n;
        if (!base)
            base_n = 10;
        else {
            RELEASE_ASSERT(base->cls == int_cls, "");
            base_n = static_cast<BoxedInt*>(base)->n;
        }

        BoxedString* s = static_cast<BoxedString*>(val);

        RELEASE_ASSERT(s->size() == strlen(s->data()), "");
        Box* r = PyInt_FromString(s->data(), NULL, base_n);
        if (!r)
            throwCAPIException();
        return r;
    } else if (isSubclass(val->cls, unicode_cls)) {
        int base_n;
        if (!base)
            base_n = 10;
        else {
            RELEASE_ASSERT(base->cls == int_cls, "");
            base_n = static_cast<BoxedInt*>(base)->n;
        }

        Box* r = PyInt_FromUnicode(PyUnicode_AS_UNICODE(val), PyUnicode_GET_SIZE(val), base_n);
        if (!r)
            throwCAPIException();
        return r;
    } else if (val->cls == float_cls) {
        RELEASE_ASSERT(!base, "");
        double d = static_cast<BoxedFloat*>(val)->d;
        return new BoxedInt(d);
    } else {
        RELEASE_ASSERT(!base, "");
        static const std::string int_str("__int__");
        Box* r = callattr(val, &int_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = true }),
                          ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);

        if (!r) {
            fprintf(stderr, "TypeError: int() argument must be a string or a number, not '%s'\n", getTypeName(val));
            raiseExcHelper(TypeError, "");
        }

        if (!isSubclass(r->cls, int_cls) && !isSubclass(r->cls, long_cls)) {
            raiseExcHelper(TypeError, "__int__ returned non-int (type %s)", r->cls->tp_name);
        }
        return r;
    }
Ejemplo n.º 17
0
    virtual const void *getFontTable(LETag tag) const
    {
#if PY_MAJOR_VERSION >= 3
        PyObject *key = PyUnicode_FromStringAndSize(NULL, 4);
        Py_UNICODE *s = PyUnicode_AS_UNICODE(key);
#else
        PyObject *key = PyString_FromStringAndSize(NULL, 4);
        char *s = PyString_AS_STRING(key);
#endif
        for (int i = 0; i < 4; ++i) {
            s[3 - i] = tag & 0xff;
            tag >>= 8;
        }

        PyObject *result = PyDict_GetItem(tables, key);

        if (result == NULL)
        {
            result = PyObject_CallMethodObjArgs((PyObject *) self,
                                                getFontTable_NAME, key, NULL);
            if (result == NULL)
            {
                if (PyErr_ExceptionMatches(PyExc_KeyError))
                    PyErr_Clear();
                Py_DECREF(key);

                return NULL;
            }

#if PY_MAJOR_VERSION >= 3
            if (!PyBytes_CheckExact(result))
#else
            if (!PyString_CheckExact(result))
#endif
            {
                PyErr_SetObject(PyExc_TypeError, result);
                Py_DECREF(result);
                Py_DECREF(key);

                return NULL;
            }

            PyDict_SetItem(tables, key, result);

            Py_DECREF(result);
            Py_DECREF(key);
        }
        else
            Py_DECREF(key);

#if PY_MAJOR_VERSION >= 3
        return PyBytes_AS_STRING(result);
#else
        return PyString_AS_STRING(result);
#endif
    }
Ejemplo n.º 18
0
static void *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
{
  PyObject *obj = (PyObject *) _obj;
  PyObject *newObj = PyUnicode_EncodeUTF8 (PyUnicode_AS_UNICODE(obj), PyUnicode_GET_SIZE(obj), NULL);

  GET_TC(tc)->newObj = newObj;

  *_outLen = PyString_GET_SIZE(newObj);
  return PyString_AS_STRING(newObj);
}
Ejemplo n.º 19
0
static Tcl_Obj*
AsObj(PyObject *value)
{
	Tcl_Obj *result;

	if (PyString_Check(value))
		return Tcl_NewStringObj(PyString_AS_STRING(value),
					PyString_GET_SIZE(value));
	else if (PyInt_Check(value))
		return Tcl_NewLongObj(PyInt_AS_LONG(value));
	else if (PyFloat_Check(value))
		return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value));
	else if (PyTuple_Check(value)) {
		Tcl_Obj **argv = (Tcl_Obj**)
			ckalloc(PyTuple_Size(value)*sizeof(Tcl_Obj*));
		int i;
		if(!argv)
		  return 0;
		for(i=0;i<PyTuple_Size(value);i++)
		  argv[i] = AsObj(PyTuple_GetItem(value,i));
		result = Tcl_NewListObj(PyTuple_Size(value), argv);
		ckfree(FREECAST argv);
		return result;
	}
	else if (PyUnicode_Check(value)) {
#if TKMAJORMINOR <= 8001
		/* In Tcl 8.1 we must use UTF-8 */
		PyObject* utf8 = PyUnicode_AsUTF8String(value);
		if (!utf8)
			return 0;
		result = Tcl_NewStringObj(PyString_AS_STRING(utf8),
					  PyString_GET_SIZE(utf8));
		Py_DECREF(utf8);
		return result;
#else /* TKMAJORMINOR > 8001 */
		/* In Tcl 8.2 and later, use Tcl_NewUnicodeObj() */
		if (sizeof(Py_UNICODE) != sizeof(Tcl_UniChar)) {
			/* XXX Should really test this at compile time */
			PyErr_SetString(PyExc_SystemError,
				"Py_UNICODE and Tcl_UniChar differ in size");
			return 0;
		}
		return Tcl_NewUnicodeObj(PyUnicode_AS_UNICODE(value),
					 PyUnicode_GET_SIZE(value));
#endif /* TKMAJORMINOR > 8001 */
	}
	else {
		PyObject *v = PyObject_Str(value);
		if (!v)
			return 0;
		result = AsObj(v);
		Py_DECREF(v);
		return result;
	}
}
Ejemplo n.º 20
0
static PyObject *
Z_set(void *ptr, PyObject *value, Py_ssize_t size)
{
    if (value == Py_None) {
        *(wchar_t **)ptr = NULL;
        Py_INCREF(value);
        return value;
    }
    if (PyLong_Check(value) || PyLong_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
        *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongLongMask(value);
#else
        *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongMask(value);
#endif
        Py_INCREF(Py_None);
        return Py_None;
    }
    if (!PyUnicode_Check(value)) {
        PyErr_Format(PyExc_TypeError,
                     "unicode string or integer address expected instead of %s instance",
                     value->ob_type->tp_name);
        return NULL;
    } else
        Py_INCREF(value);
#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
    /* We can copy directly.  Hm, are unicode objects always NUL
       terminated in Python, internally?
     */
    *(wchar_t **)ptr = (wchar_t *) PyUnicode_AS_UNICODE(value);
    return value;
#else
    {
        /* We must create a wchar_t* buffer from the unicode object,
           and keep it alive */
        PyObject *keep;
        wchar_t *buffer;

        buffer = PyUnicode_AsWideCharString(value, NULL);
        if (!buffer) {
            Py_DECREF(value);
            return NULL;
        }
        keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);
        if (!keep) {
            Py_DECREF(value);
            PyMem_Free(buffer);
            return NULL;
        }
        *(wchar_t **)ptr = (wchar_t *)buffer;
        Py_DECREF(value);
        return keep;
    }
#endif
}
Ejemplo n.º 21
0
static PyObject *
complex__format__(PyObject* self, PyObject* args)
{
    PyObject *format_spec;

    if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
    return NULL;
    return _PyComplex_FormatAdvanced(self,
                                     PyUnicode_AS_UNICODE(format_spec),
                                     PyUnicode_GET_SIZE(format_spec));
}
Ejemplo n.º 22
0
EXPORT UnicodeString &PyObject_AsUnicodeString(PyObject *object,
                                               const char *encoding,
                                               const char *mode,
                                               UnicodeString &string)
{
    if (PyUnicode_Check(object))
    {
        if (sizeof(Py_UNICODE) == sizeof(UChar))
            string.setTo((const UChar *) PyUnicode_AS_UNICODE(object),
                         (int32_t) PyUnicode_GET_SIZE(object));
        else
        {
            int32_t len = (int32_t) PyUnicode_GET_SIZE(object);
            Py_UNICODE *pchars = PyUnicode_AS_UNICODE(object);
            UChar *chars = new UChar[len * 3];
            UErrorCode status = U_ZERO_ERROR;
            int32_t dstLen;

            u_strFromUTF32(chars, len * 3, &dstLen,
                           (const UChar32 *) pchars, len, &status);

            if (U_FAILURE(status))
            {
                delete[] chars;
                throw ICUException(status);
            }

            string.setTo((const UChar *) chars, (int32_t) dstLen);
            delete[] chars;
        }
    }
    else if (PyBytes_Check(object))
        PyBytes_AsUnicodeString(object, encoding, mode, string);
    else
    {
        PyErr_SetObject(PyExc_TypeError, object);
        throw ICUException();
    }

    return string;
}
Ejemplo n.º 23
0
static PyObject *
Splitter_split(Splitter *self, PyObject *args)
{
    PyObject *doc;
    char *encoding = "iso-8859-15";

    Py_XDECREF(self->list);
    self->list = PyList_New(0);

    if (! (PyArg_ParseTuple(args,"O|s",&doc, &encoding))) return NULL;

    if (PyBytes_Check(doc)) {
        if (strlen(encoding) == 0 || !strcmp(encoding,"ascii"))
            splitString(self, doc);
        else {
            PyObject *doc1;
            if (! (doc1 = PyUnicode_FromEncodedObject(doc, encoding, "strict"))) {
                PyErr_SetString(PyExc_UnicodeError,"unicode conversion failed (maybe wrong encoding parameter)");
                return NULL;
            }

            splitUnicodeString(self, doc1);
            Py_XDECREF(doc1);
        }
    } else if (PyUnicode_Check(doc)) {
        PyObject *doc1; // create a *real* copy since we need to modify the string
        doc1 = PyUnicode_FromUnicode(NULL, PyUnicode_GET_SIZE(doc));
        Py_UNICODE_COPY(PyUnicode_AS_UNICODE(doc1),
                        PyUnicode_AS_UNICODE(doc),
                        PyUnicode_GET_SIZE(doc));
        splitUnicodeString(self, doc1);
        Py_DECREF(doc1);
    } else {
        PyErr_SetString(PyExc_TypeError, "first argument must be  string or unicode");
        return NULL;
    }

    Py_XINCREF(self->list);

    return self->list;
}
Ejemplo n.º 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;

}
Ejemplo n.º 25
0
/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
   of an error.
*/
static PyObject *
test_u_code(PyObject *self)
{
	PyObject *tuple, *obj;
	Py_UNICODE *value;
	int len;

	/* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
	/* Just use the macro and check that it compiles */
	int x = Py_UNICODE_ISSPACE(25);

        tuple = PyTuple_New(1);
        if (tuple == NULL)
        	return NULL;

        obj = PyUnicode_Decode("test", strlen("test"),
			       "ascii", NULL);
        if (obj == NULL)
        	return NULL;

        PyTuple_SET_ITEM(tuple, 0, obj);

        value = 0;
        if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
        	return NULL;
        if (value != PyUnicode_AS_UNICODE(obj))
        	return raiseTestError("test_u_code",
			"u code returned wrong value for u'test'");
        value = 0;
        if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
        	return NULL;
        if (value != PyUnicode_AS_UNICODE(obj) ||
	    len != PyUnicode_GET_SIZE(obj))
        	return raiseTestError("test_u_code",
			"u# code returned wrong values for u'test'");

	Py_DECREF(tuple);
	Py_INCREF(Py_None);
	return Py_None;
}
Ejemplo n.º 26
0
static PyObject *
astrcmp(PyObject *self, PyObject *args)
{
    PyObject *s1, *s2;
	float d;
	const Py_UNICODE *us1, *us2;
	int len1, len2;
    PyThreadState *_save;

    if (!PyArg_ParseTuple(args, "UU", &s1, &s2))
        return NULL;

	us1 = PyUnicode_AS_UNICODE(s1);
	us2 = PyUnicode_AS_UNICODE(s2);
	len1 = PyUnicode_GetSize(s1);
	len2 = PyUnicode_GetSize(s2);

    Py_UNBLOCK_THREADS
	d = LevenshteinDistance(us1, len1, us2, len2);
    Py_BLOCK_THREADS
    return Py_BuildValue("f", d);
}
Ejemplo n.º 27
0
BOOL CVirtualHelper::do_call(PyObject *args)
{
	USES_CONVERSION;
	XDODECREF(retVal);	// our old one.
	retVal = NULL;
	ASSERT(handler);	// caller must trap this.
	ASSERT(args);
	PyObject *result = gui_call_object(handler,args);
	DODECREF(args);
	if (result==NULL) {
		if (vehErrorHandling==VEH_PRINT_ERROR) {
			char msg[256];
			TRACE("CallVirtual : callback failed with exception\n");
			gui_print_error();
			// this will probably fail if we are already inside the exception handler
			PyObject *obRepr = PyObject_Repr(handler);
			char *szRepr = "<no representation (PyObject_Repr failed)>";
			if (obRepr){
				if (PyString_Check(obRepr))
					szRepr = PyString_AS_STRING(obRepr);
				else if (PyUnicode_Check(obRepr))
					szRepr=W2A(PyUnicode_AS_UNICODE(obRepr));
				}
			else
				PyErr_Clear();

			LPTSTR HandlerName=csHandlerName.GetBuffer(csHandlerName.GetLength());
			snprintf(msg, sizeof(msg)/sizeof(msg[0]),  "%s() virtual handler (%s) raised an exception", 
				T2A(HandlerName),
				szRepr);
			csHandlerName.ReleaseBuffer();
			Py_XDECREF(obRepr);
			PyErr_SetString(ui_module_error, msg);
			// send to the debugger
			TRACE(msg);
			TRACE("\n");
			// send to the app.
			gui_print_error();
		} else {
			// Error dialog.
			CString csAddnMsg = " when executing ";
			csAddnMsg += csHandlerName;
			csAddnMsg += " handler";

			ExceptionHandler(EHA_DISPLAY_DIALOG, NULL, csAddnMsg);
		}
		return FALSE;
	}
	retVal = result;
	return TRUE;
}
Ejemplo n.º 28
0
Archivo: html.c Proyecto: AEliu/calibre
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);
            }
        }
    }
Ejemplo n.º 29
0
// Convert a QString to a Python Unicode object.
PyObject *qpycore_PyObject_FromQString(const QString &qstr)
{
    PyObject *obj;

#if defined(PYQT_PEP_393)
    obj = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, qstr.constData(),
            qstr.length());
#elif defined(Py_UNICODE_WIDE)
#if QT_VERSION >= 0x040200
    QVector<uint> ucs4 = qstr.toUcs4();

    if ((obj = PyUnicode_FromUnicode(NULL, ucs4.size())) == NULL)
        return NULL;

    memcpy(PyUnicode_AS_UNICODE(obj), ucs4.constData(),
            ucs4.size() * sizeof (Py_UNICODE));
#else
    // Note that this code doesn't handle code points greater than 0xffff very
    // well.

    if ((obj = PyUnicode_FromUnicode(NULL, qstr.length())) == NULL)
        return NULL;

    Py_UNICODE *pyu = PyUnicode_AS_UNICODE(obj);

    for (int i = 0; i < qstr.length(); ++i)
        *pyu++ = (qstr.at(i)).unicode();
#endif
#else
    if ((obj = PyUnicode_FromUnicode(NULL, qstr.length())) == NULL)
        return NULL;

    memcpy(PyUnicode_AS_UNICODE(obj), qstr.utf16(),
            qstr.length() * sizeof (Py_UNICODE));
#endif

    return obj;
}
Ejemplo n.º 30
0
bool PythonUtil::PyStringToString( const PyObject * pystr, std::string * str )
{
	if( PyUnicode_Check(pystr) )
	{
		*str = StringUtil::WideCharToMultiByte( (const wchar_t*)PyUnicode_AS_UNICODE(pystr), PyUnicode_GET_SIZE(pystr) );
		return true;
	}
	else
	{
		PyErr_SetString( PyExc_TypeError, "must be unicode." );
		*str = "";
		return false;
	}
}