Example #1
0
std::string getPythonTraceback()
{

    // get exception info
    PyObject *type, *value, *traceback;
    PyErr_Fetch(&type, &value, &traceback);
    PyErr_NormalizeException(&type, &value, &traceback);

    std::ostringstream mssg;
    if (traceback)
    {

        PyObject* tracebackModule;
        PyObject* tracebackDictionary;
        PyObject* tracebackFunction;
    
        tracebackModule = PyImport_ImportModule("traceback");
        if (!tracebackModule)
        {
            throw python_error("unable to load traceback module while importing numpy inside PDAL");
        }

        tracebackDictionary = PyModule_GetDict(tracebackModule);

        tracebackFunction = PyDict_GetItemString(tracebackDictionary, "format_exception");
        if (!tracebackFunction)
        {
            throw python_error("unable to find traceback function while importing numpy inside PDAL");
        }

        if (!PyCallable_Check(tracebackFunction))
        {
            throw python_error("invalid traceback function while importing numpy inside PDAL");
        }

        
        // create an argument for "format exception"
        PyObject* args = PyTuple_New(3);
        PyTuple_SetItem(args, 0, type);
        PyTuple_SetItem(args, 1, value);
        PyTuple_SetItem(args, 2, traceback);

        // get a list of string describing what went wrong
        PyObject* output = PyObject_CallObject(tracebackFunction, args);

        // print error message
        int i, n = PyList_Size(output);

#if PY_MAJOR_VERSION >= 3
        for (i=0; i<n; i++) 
        {
            PyObject* u = PyUnicode_AsUTF8String(PyList_GetItem(output, i));
            const char* p = PyBytes_AsString(u);
            
            mssg << p;
        }
        
#else
        for (i=0; i<n; i++) mssg << PyString_AsString(PyList_GetItem(output, i));
#endif
        
        // clean up
        Py_XDECREF(args);
        Py_XDECREF(output);
    }
    else if (value != NULL)
    {
        PyObject *s = PyObject_Str(value);
#if PY_MAJOR_VERSION >= 3
        // const char* text = PyUnicode_AS_DATA(s);
        PyObject* u = PyUnicode_AsUTF8String(s);
        const char* text = PyBytes_AsString(u);        
#else
        const char* text = PyString_AS_STRING(s);
#endif
        Py_DECREF(s);
        mssg << text;
    }
    else
    {
        mssg << "unknown error that we are unable to get a traceback for. Was it already printed/taken?";
    }

    Py_XDECREF(value);
    Py_XDECREF(type);
    Py_XDECREF(traceback);

    return mssg.str();
}
QVariant PythonScript::eval()
{
	if (!isFunction) compiled = notCompiled;
	if (compiled != isCompiled && !compile(true))
		return QVariant();

	PyObject *pyret;
	beginStdoutRedirect();
	if (PyCallable_Check(PyCode)){
		PyObject *empty_tuple = PyTuple_New(0);
		pyret = PyObject_Call(PyCode, empty_tuple, localDict);
		Py_DECREF(empty_tuple);
	} else
		pyret = PyEval_EvalCode((PyCodeObject*)PyCode, env()->globalDict(), localDict);
	endStdoutRedirect();
	if (!pyret){
		if (PyErr_ExceptionMatches(PyExc_ValueError) ||
			PyErr_ExceptionMatches(PyExc_ZeroDivisionError)){
            PyErr_Clear(); // silently ignore errors
			return  QVariant("");
		} else {
			emit_error(env()->errorMsg(), 0);
			return QVariant();
		}
	}

	QVariant qret = QVariant();
	/* None */
	if (pyret == Py_None)
		qret = QVariant("");
	/* numeric types */
	else if (PyFloat_Check(pyret))
		qret = QVariant(PyFloat_AS_DOUBLE(pyret));
	else if (PyInt_Check(pyret))
		qret = QVariant((qlonglong)PyInt_AS_LONG(pyret));
	else if (PyLong_Check(pyret))
		qret = QVariant((qlonglong)PyLong_AsLongLong(pyret));
	else if (PyNumber_Check(pyret)){
		PyObject *number = PyNumber_Float(pyret);
		if (number){
			qret = QVariant(PyFloat_AS_DOUBLE(number));
			Py_DECREF(number);
		}
		/* bool */
	} else if (PyBool_Check(pyret))
		qret = QVariant(pyret==Py_True, 0);
	// could handle advanced types (such as PyList->QValueList) here if needed
	/* fallback: try to convert to (unicode) string */
	if(!qret.isValid()) {
		PyObject *pystring = PyObject_Unicode(pyret);
		if (pystring) {
			PyObject *asUTF8 = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(pystring), PyUnicode_GET_DATA_SIZE(pystring), 0);
			Py_DECREF(pystring);
			if (asUTF8) {
				qret = QVariant(QString::fromUtf8(PyString_AS_STRING(asUTF8)));
				Py_DECREF(asUTF8);
			} else if (pystring = PyObject_Str(pyret)) {
				qret = QVariant(QString(PyString_AS_STRING(pystring)));
				Py_DECREF(pystring);
			}
		}
	}

	Py_DECREF(pyret);

	if (PyErr_Occurred()){
		if (PyErr_ExceptionMatches(PyExc_ValueError) ||
			PyErr_ExceptionMatches(PyExc_ZeroDivisionError)){
            PyErr_Clear(); // silently ignore errors
			return  QVariant("");
		} else {
			emit_error(env()->errorMsg(), 0);
			return QVariant();
		}
	} else
		return qret;
}
Example #3
0
static PyObject * req_readline(requestobject *self, PyObject *args)
{

    int rc, chunk_len, bytes_read;
    char *buffer;
    PyObject *result;
    int copied = 0;
    int len = -1;

    if (! PyArg_ParseTuple(args, "|i", &len)) 
	return NULL;

    if (len == 0) {
	return PyString_FromString("");
    }

    /* is this the first read? */
    if (! self->request_rec->read_length) {

	/* then do some initial setting up */
	rc = ap_setup_client_block(self->request_rec, REQUEST_CHUNKED_ERROR);

	if(rc != OK) {
	    PyObject *val = PyInt_FromLong(rc);
	    if (val == NULL)
		return NULL;
	    PyErr_SetObject(Mp_ServerReturn, val);
	    Py_DECREF(val);
	    return NULL;
	}

	if (! ap_should_client_block(self->request_rec)) {
	    /* client has nothing to send */
	    return PyString_FromString("");
	}
    }

    if (len < 0)
	len = self->request_rec->remaining + 
	    (self->rbuff_len - self->rbuff_pos);

    /* create the result buffer */
    result = PyString_FromStringAndSize(NULL, len);

    /* possibly no more memory */
    if (result == NULL) 
	return NULL;

    buffer = PyString_AS_STRING((PyStringObject *) result);

    /* is there anything left in the rbuff from previous reads? */
    if (self->rbuff_pos < self->rbuff_len) {
	
	/* if yes, process that first */
	while (self->rbuff_pos < self->rbuff_len) {

	    buffer[copied++] = self->rbuff[self->rbuff_pos];
	    if ((self->rbuff[self->rbuff_pos++] == '\n') || 
		(copied == len)) {

		/* our work is done */

		/* resize if necessary */
		if (copied < len) 
		    if(_PyString_Resize(&result, copied))
			return NULL;

		return result;
	    }
	}
    }

    /* if got this far, the buffer should be empty, we need to read more */
	
    /* create a read buffer */
    self->rbuff_len = len > HUGE_STRING_LEN ? len : HUGE_STRING_LEN;
    self->rbuff_pos = self->rbuff_len;
    self->rbuff = ap_palloc(self->request_rec->pool, self->rbuff_len);
    if (! self->rbuff)
	return PyErr_NoMemory();

    /* set timeout */
    ap_soft_timeout("mod_python_read", self->request_rec);

    /* read it in */
    Py_BEGIN_ALLOW_THREADS
	chunk_len = ap_get_client_block(self->request_rec, self->rbuff, 
					self->rbuff_len);
    Py_END_ALLOW_THREADS;
    bytes_read = chunk_len;

    /* if this is a "short read", try reading more */
    while ((chunk_len != 0 ) && (bytes_read + copied < len)) {
	Py_BEGIN_ALLOW_THREADS
	    chunk_len = ap_get_client_block(self->request_rec, 
					    self->rbuff + bytes_read, 
					    self->rbuff_len - bytes_read);
	Py_END_ALLOW_THREADS
	    ap_reset_timeout(self->request_rec);
	if (chunk_len == -1) {
	    ap_kill_timeout(self->request_rec);
	    PyErr_SetObject(PyExc_IOError, 
			    PyString_FromString("Client read error (Timeout?)"));
	    return NULL;
	}
	else
	    bytes_read += chunk_len;
    }
    self->rbuff_len = bytes_read;
    self->rbuff_pos = 0;
    ap_kill_timeout(self->request_rec);

    /* now copy the remaining bytes */
    while (self->rbuff_pos < self->rbuff_len) {

	buffer[copied++] = self->rbuff[self->rbuff_pos];
	if ((self->rbuff[self->rbuff_pos++] == '\n') || 
	    (copied == len)) 
	    /* our work is done */
	    break;
    }

    /* resize if necessary */
    if (copied < len) 
	if(_PyString_Resize(&result, copied))
	    return NULL;

    return result;
}
Example #4
0
void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
{
	painter.clip(eRect(offset, m_itemsize));

	int marked = 0;

	if (m_current_marked && selected)
		marked = 2;
	else if (cursorValid() && isMarked(*m_cursor))
	{
		if (selected)
			marked = 2;
		else
			marked = 1;
	}
	else
		style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);

	eListboxStyle *local_style = 0;

		/* get local listbox style, if present */
	if (m_listbox)
		local_style = m_listbox->getLocalStyle();

	if (marked == 1)  // marked
	{
		style.setStyle(painter, eWindowStyle::styleListboxMarked);
		if (m_color_set[markedForeground])
			painter.setForegroundColor(m_color[markedForeground]);
		if (m_color_set[markedBackground])
			painter.setBackgroundColor(m_color[markedBackground]);
	}
	else if (marked == 2) // marked and selected
	{
		style.setStyle(painter, eWindowStyle::styleListboxMarkedAndSelected);
		if (m_color_set[markedForegroundSelected])
			painter.setForegroundColor(m_color[markedForegroundSelected]);
		if (m_color_set[markedBackgroundSelected])
			painter.setBackgroundColor(m_color[markedBackgroundSelected]);
	}
	else if (local_style)
	{
		if (selected)
		{
			/* if we have a local background color set, use that. */
			if (local_style->m_background_color_selected_set)
				painter.setBackgroundColor(local_style->m_background_color_selected);
			/* same for foreground */
			if (local_style->m_foreground_color_selected_set)
				painter.setForegroundColor(local_style->m_foreground_color_selected);
		}
		else
		{
			/* if we have a local background color set, use that. */
			if (local_style->m_background_color_set)
				painter.setBackgroundColor(local_style->m_background_color);
			/* same for foreground */
			if (local_style->m_foreground_color_set)
				painter.setForegroundColor(local_style->m_foreground_color);
		}
	}

	if (!local_style || !local_style->m_transparent_background)
		/* if we have no transparent background */
	{
		/* blit background picture, if available (otherwise, clear only) */
		if (local_style && local_style->m_background)
			painter.blit(local_style->m_background, offset, eRect(), 0);
		else
			painter.clear();
	} else
	{
		if (local_style->m_background)
			painter.blit(local_style->m_background, offset, eRect(), gPainter::BT_ALPHATEST);
		else if (selected && !local_style->m_selection)
			painter.clear();
	}

	if (cursorValid())
	{
		/* get service information */
		ePtr<iStaticServiceInformation> service_info;
		m_service_center->info(*m_cursor, service_info);
		eServiceReference ref = *m_cursor;
		bool isMarker = ref.flags & eServiceReference::isMarker;
		bool isPlayable = !(ref.flags & eServiceReference::isDirectory || isMarker);
		bool isRecorded = m_record_indicator_mode && isPlayable && checkServiceIsRecorded(ref);
		ePtr<eServiceEvent> evt;
		bool serviceAvail = true;
		bool serviceFallback = false;
		int isplayable_value;

		if (!marked && isPlayable && service_info && m_is_playable_ignore.valid())
		{
			isplayable_value = service_info->isPlayable(*m_cursor, m_is_playable_ignore);

			if (isplayable_value == 0) // service unavailable
			{
				if (m_color_set[serviceNotAvail])
					painter.setForegroundColor(m_color[serviceNotAvail]);
				else
					painter.setForegroundColor(gRGB(0xbbbbbb));
				serviceAvail = false;
			}
			else
			{
				if (isplayable_value == 2) // fallback receiver service
				{
					if (m_color_set[serviceItemFallback])
						painter.setForegroundColor(m_color[serviceItemFallback]);
					serviceFallback = true;
				}
			}
		}
		if (m_record_indicator_mode == 3 && isRecorded)
		{
			if (m_color_set[serviceRecorded])
				painter.setForegroundColor(m_color[serviceRecorded]);
			else
				painter.setForegroundColor(gRGB(0xb40431));
		}

		if (selected && local_style && local_style->m_selection)
			painter.blit(local_style->m_selection, offset, eRect(), gPainter::BT_ALPHATEST);

		int xoffset=0;  // used as offset when painting the folder/marker symbol or the serviceevent progress
		time_t now = time(0);

		for (int e = 0; e != celServiceTypePixmap; ++e)
		{
			if (m_element_font[e])
			{
				int flags=gPainter::RT_VALIGN_CENTER;
				int yoffs = 0;
				eRect area = m_element_position[e];
				std::string text = "<n/a>";
				switch (e)
				{
				case celServiceNumber:
				{
					if (area.width() <= 0)
						continue; // no point in going on if we won't paint anything

					if( m_cursor->getChannelNum() == 0 )
						continue;

					char buffer[15];
					snprintf(buffer, sizeof(buffer), "%d", m_cursor->getChannelNum() );
					text = buffer;
					flags|=gPainter::RT_HALIGN_RIGHT;
					if (isPlayable && serviceFallback && selected && m_color_set[serviceSelectedFallback])
						painter.setForegroundColor(m_color[serviceSelectedFallback]);
					break;
				}
				case celServiceName:
				{
					if (service_info)
						service_info->getName(*m_cursor, text);
					if (!isPlayable)
					{
						area.setWidth(area.width() + m_element_position[celServiceEventProgressbar].width() +  m_nonplayable_margins);
						if (m_element_position[celServiceEventProgressbar].left() == 0)
							area.setLeft(0);
						if (m_element_position[celServiceNumber].width() && m_element_position[celServiceEventProgressbar].left() == m_element_position[celServiceNumber].width() +  m_nonplayable_margins)
							area.setLeft(m_element_position[celServiceNumber].width() +  m_nonplayable_margins);
					}
					if (!(m_record_indicator_mode == 3 && isRecorded) && isPlayable && serviceFallback && selected && m_color_set[serviceSelectedFallback])
						painter.setForegroundColor(m_color[serviceSelectedFallback]);
					break;
				}
				case celServiceInfo:
				{
					if ( isPlayable && service_info && !service_info->getEvent(*m_cursor, evt) )
					{
						std::string name = evt->getEventName();
						if (name.empty())
							continue;
						text = evt->getEventName();
						if (serviceAvail)
						{
							if (!selected && m_color_set[eventForeground])
								painter.setForegroundColor(m_color[eventForeground]);
							else if (selected && m_color_set[eventForegroundSelected])
								painter.setForegroundColor(m_color[eventForegroundSelected]);
							else
								painter.setForegroundColor(gRGB(0xe7b53f));

							if (serviceFallback && !selected && m_color_set[eventForegroundFallback]) // fallback receiver
								painter.setForegroundColor(m_color[eventForegroundFallback]);
							else if (serviceFallback && selected && m_color_set[eventForegroundSelectedFallback])
								painter.setForegroundColor(m_color[eventForegroundSelectedFallback]);

						}
						break;
					}
					continue;
				}
				case celServiceEventProgressbar:
				{
					if (area.width() > 0 && isPlayable && service_info && !service_info->getEvent(*m_cursor, evt))
					{
						char buffer[15];
						snprintf(buffer, sizeof(buffer), "%d %%", (int)(100 * (now - evt->getBeginTime()) / evt->getDuration()));
						text = buffer;
						flags|=gPainter::RT_HALIGN_RIGHT;
						break;
					}
					continue;
				}
				}

				eRect tmp = area;
				int xoffs = 0;
				ePtr<gPixmap> piconPixmap;

				if (e == celServiceName)
				{
					//picon stuff
					if (isPlayable && PyCallable_Check(m_GetPiconNameFunc))
					{
						ePyObject pArgs = PyTuple_New(1);
						PyTuple_SET_ITEM(pArgs, 0, PyString_FromString(ref.toString().c_str()));
						ePyObject pRet = PyObject_CallObject(m_GetPiconNameFunc, pArgs);
						Py_DECREF(pArgs);
						if (pRet)
						{
							if (PyString_Check(pRet))
							{
								std::string piconFilename = PyString_AS_STRING(pRet);
								if (!piconFilename.empty())
									loadPNG(piconPixmap, piconFilename.c_str());
							}
							Py_DECREF(pRet);
						}
					}
					xoffs = xoffset;
					tmp.setWidth(((!isPlayable || m_column_width == -1 || (!piconPixmap && !m_column_width)) ? tmp.width() : m_column_width) - xoffs);
				}

				ePtr<eTextPara> para = new eTextPara(tmp);
				para->setFont(m_element_font[e]);
				para->renderString(text.c_str());

				if (e == celServiceName)
				{
					eRect bbox = para->getBoundBox();

					int servicenameWidth = ((!isPlayable || m_column_width == -1 || (!piconPixmap && !m_column_width)) ? bbox.width() : m_column_width);
					m_element_position[celServiceInfo].setLeft(area.left() + servicenameWidth + m_items_distances + xoffs);
					m_element_position[celServiceInfo].setTop(area.top());
					m_element_position[celServiceInfo].setWidth(area.width() - (servicenameWidth + m_items_distances + xoffs));
					m_element_position[celServiceInfo].setHeight(area.height());

					if (isPlayable)
					{
						//picon stuff
						if (PyCallable_Check(m_GetPiconNameFunc) and (m_column_width || piconPixmap))
						{
							eRect area = m_element_position[celServiceInfo];
							/* PIcons are usually about 100:60. Make it a
							 * bit wider in case the icons are diffently
							 * shaped, and to add a bit of margin between
							 * icon and text. */
							const int iconWidth = area.height() * 9 / 5;
							m_element_position[celServiceInfo].setLeft(area.left() + iconWidth);
							m_element_position[celServiceInfo].setWidth(area.width() - iconWidth);
							area = m_element_position[celServiceName];
							xoffs += iconWidth;
							if (piconPixmap)
							{
								area.moveBy(offset);
								painter.clip(area);
								painter.blitScale(piconPixmap,
									eRect(area.left(), area.top(), iconWidth, area.height()),
									area,
									gPainter::BT_ALPHABLEND | gPainter::BT_KEEP_ASPECT_RATIO);
								painter.clippop();
							}
						}

						//service type marker stuff
						if (m_servicetype_icon_mode)
						{
							int orbpos = m_cursor->getUnsignedData(4) >> 16;
							const char *filename = ref.path.c_str();
							ePtr<gPixmap> &pixmap =
								(m_cursor->flags & eServiceReference::isGroup) ? m_pixmaps[picServiceGroup] :
								(strstr(filename, "://")) ? m_pixmaps[picStream] :
								(orbpos == 0xFFFF) ? m_pixmaps[picDVB_C] :
								(orbpos == 0xEEEE) ? m_pixmaps[picDVB_T] : m_pixmaps[picDVB_S];
							if (pixmap)
							{
								eSize pixmap_size = pixmap->size();
								eRect area = m_element_position[celServiceInfo];
								m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
								m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
								int offs = 0;
								if (m_servicetype_icon_mode == 1)
								{
									area = m_element_position[celServiceName];
									offs = xoffs;
									xoffs += pixmap_size.width() + m_items_distances;
								}
								else if (m_crypto_icon_mode == 1 && m_pixmaps[picCrypto])
									offs = offs + m_pixmaps[picCrypto]->size().width() + m_items_distances;
								int correction = (area.height() - pixmap_size.height()) / 2;
								area.moveBy(offset);
								painter.clip(area);
								painter.blit(pixmap, ePoint(area.left() + offs, offset.y() + correction), area, gPainter::BT_ALPHATEST);
								painter.clippop();
							}
						}

						//crypto icon stuff
						if (m_crypto_icon_mode && m_pixmaps[picCrypto])
						{
							eSize pixmap_size = m_pixmaps[picCrypto]->size();
							eRect area = m_element_position[celServiceInfo];
							int offs = 0;
							if (m_crypto_icon_mode == 1)
							{
								m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
								m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
								area = m_element_position[celServiceName];
								offs = xoffs;
								xoffs += pixmap_size.width() + m_items_distances;
							}
							int correction = (area.height() - pixmap_size.height()) / 2;
							area.moveBy(offset);
							if (service_info && service_info->isCrypted())
							{
								if (m_crypto_icon_mode == 2)
								{
									m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
									m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
								}
								painter.clip(area);
								painter.blit(m_pixmaps[picCrypto], ePoint(area.left() + offs, offset.y() + correction), area, gPainter::BT_ALPHATEST);
								painter.clippop();
							}
						}

						//record icon stuff
						if (isRecorded && m_record_indicator_mode < 3 && m_pixmaps[picRecord])
						{
							eSize pixmap_size = m_pixmaps[picRecord]->size();
							eRect area = m_element_position[celServiceInfo];
							int offs = 0;
							if (m_record_indicator_mode == 1)
							{
								m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
								m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
								area = m_element_position[celServiceName];
								offs = xoffs;
								xoffs += pixmap_size.width() + m_items_distances;
							}
							int correction = (area.height() - pixmap_size.height()) / 2;
							area.moveBy(offset);
							if (m_record_indicator_mode == 2)
							{
								m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
								m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
							}
							painter.clip(area);
							painter.blit(m_pixmaps[picRecord], ePoint(area.left() + offs, offset.y() + correction), area, gPainter::BT_ALPHATEST);
							painter.clippop();
						}
					}
				}

				if (flags & gPainter::RT_HALIGN_RIGHT)
					para->realign(eTextPara::dirRight);
				else if (flags & gPainter::RT_HALIGN_CENTER)
					para->realign(eTextPara::dirCenter);
				else if (flags & gPainter::RT_HALIGN_BLOCK)
					para->realign(eTextPara::dirBlock);

				if (flags & gPainter::RT_VALIGN_CENTER)
				{
					eRect bbox = para->getBoundBox();
					yoffs = (area.height() - bbox.height()) / 2 - bbox.top();
				}

				painter.renderPara(para, offset+ePoint(xoffs, yoffs));
			}
			else if ((e == celFolderPixmap && m_cursor->flags & eServiceReference::isDirectory) ||
				(e == celMarkerPixmap && m_cursor->flags & eServiceReference::isMarker &&
				!(m_cursor->flags & eServiceReference::isNumberedMarker)))
			{
				ePtr<gPixmap> &pixmap =
					(e == celFolderPixmap) ? m_pixmaps[picFolder] : m_pixmaps[picMarker];
				if (pixmap)
				{
					eSize pixmap_size = pixmap->size();
					eRect area;
					if (e == celFolderPixmap || m_element_position[celServiceNumber].width() < pixmap_size.width())
					{
						area = m_element_position[celServiceName];
						if (m_element_position[celServiceEventProgressbar].left() == 0)
							area.setLeft(0);
						xoffset = pixmap_size.width() + m_items_distances;			
					}
					else
						area = m_element_position[celServiceNumber];
					int correction = (area.height() - pixmap_size.height()) / 2;
					area.moveBy(offset);
					painter.clip(area);
					painter.blit(pixmap, ePoint(area.left(), offset.y() + correction), area, gPainter::BT_ALPHATEST);
					painter.clippop();
				}
			}
		}
Example #5
0
/*ARGSUSED*/
static inline PyObject *
_generic_init_common(PyObject *action, PyObject *data, PyObject *attrs)
{
	PyObject *key_aname = NULL;
	PyObject *key_attr = NULL;
	PyObject *path_attr = NULL;
	char *path = NULL;
	char invalid_path = 0;

	/*
	 * Before doing anything else to the action, action attributes must be
	 * set as set_data() relies on it.
	 */
	if (attrs != NULL) {
		if (PyObject_SetAttrString(action, "attrs", attrs) == -1)
			return (NULL);
	} else {
		/* Caller didn't specify any keyword arguments. */
		if ((attrs = PyDict_New()) == NULL)
			return (NULL);
		if (PyObject_SetAttrString(action, "attrs", attrs) == -1) {
			Py_DECREF(attrs);
			return (NULL);
		}
		Py_DECREF(attrs);
	}

	if (data == NULL || data == Py_None) {
		/* No need to call set_data(); this is much faster. */
		if (PyObject_SetAttrString(action, "data", Py_None) == -1)
			return (NULL);
	} else {
		PyObject *res = PyObject_CallMethod(action, "set_data", "(O)",
		    data);
		if (res == NULL)
			return (NULL);
		Py_DECREF(res);
	}

	if ((key_aname = PyObject_GetAttrString(action, "key_attr")) == NULL)
		return (NULL);

	if (key_aname == Py_None) {
		Py_DECREF(key_aname);
		Py_RETURN_NONE;
	}

	if ((key_attr = PyDict_GetItem(attrs, key_aname)) == NULL) {
		PyObject *aname = PyObject_GetAttrString(action, "name");
		char *ns = PyString_AS_STRING(aname);

		/*
		 * set actions allow an alternate value form, so
		 * AttributeAction.__init__ will fill this in later and raise an
		 * exception if appropriate.
		 *
		 * signature actions can't require their key attribute since the
		 * value of a signature may not yet be known.
		 */
		if (strcmp(ns, "set") != 0 && strcmp(ns, "signature") != 0) {
			set_invalid_action_error("MissingKeyAttributeError",
			    action, key_aname);
			Py_DECREF(key_aname);
			return (NULL);
		}

		Py_DECREF(key_aname);
		Py_RETURN_NONE;
	}

	if (PyList_CheckExact(key_attr)) {
		PyObject *aname = PyObject_GetAttrString(action, "name");
		char *ns = PyString_AS_STRING(aname);
		int multi_error = 0;

		if (strcmp(ns, "depend") != 0) {
			/*
			 * Unless this is a dependency action, multiple values
			 * are never allowed for key attribute.
			 */
			multi_error = 1;
		} else {
			PyObject *dt = PyDict_GetItemString(attrs, "type");
			/*
			 * If dependency type is 'require-any', multiple values
			 * are allowed for key attribute.
			 */
			if (dt != NULL) {
				char *ts = PyString_AsString(dt);
				if (ts == NULL) {
					Py_DECREF(key_aname);
					Py_DECREF(aname);
					return (NULL);
				}
				if (strcmp(ts, "require-any") != 0)
					multi_error = 1;
			} else {
				multi_error = 1;
			}
		}

		Py_DECREF(aname);
		if (multi_error == 1) {
			set_invalid_action_error("KeyAttributeMultiValueError",
			    action, key_aname);
			Py_DECREF(key_aname);
			return (NULL);
		}
	}

	if ((path_attr = PyDict_GetItemString(attrs, "path")) == NULL) {
		Py_DECREF(key_aname);
		Py_RETURN_NONE;
	}

	if ((path = PyString_AsString(path_attr)) != NULL) {
		if (path[0] == '/') {
			PyObject *stripped = PyObject_CallMethod(
			    path_attr, "lstrip", "(s)", "/");
			if (stripped == NULL) {
				Py_DECREF(key_aname);
				return (NULL);
			}
			if (PyDict_SetItemString(attrs, "path",
			    stripped) == -1) {
				Py_DECREF(key_aname);
				Py_DECREF(stripped);
				return (NULL);
			}
			if (PyString_GET_SIZE(stripped) == 0)
				invalid_path = 1;
			Py_DECREF(stripped);
		} else {
			if (PyString_GET_SIZE(path_attr) == 0)
				invalid_path = 1;
		}
	} else {
		/* path attribute is not a string. */
		invalid_path = 1;
	}

	if (invalid_path == 1) {
		set_invalid_action_error("InvalidPathAttributeError",
		    action, key_aname);
		Py_DECREF(key_aname);
		return (NULL);
	}

	Py_DECREF(key_aname);
	Py_RETURN_NONE;
}
Example #6
0
int
PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
{
	PyTypeObject *tp = obj->ob_type;
	PyObject *descr;
	descrsetfunc f;
	PyObject **dictptr;
	int res = -1;

	if (!PyString_Check(name)){
#ifdef Py_USING_UNICODE
		/* The Unicode to string conversion is done here because the
		   existing tp_setattro slots expect a string object as name
		   and we wouldn't want to break those. */
		if (PyUnicode_Check(name)) {
			name = PyUnicode_AsEncodedString(name, NULL, NULL);
			if (name == NULL)
				return -1;
		}
		else
#endif
		{
			PyErr_Format(PyExc_TypeError,
				     "attribute name must be string, not '%.200s'",
				     name->ob_type->tp_name);
			return -1;
		}
	}
	else
		Py_INCREF(name);

	if (tp->tp_dict == NULL) {
		if (PyType_Ready(tp) < 0)
			goto done;
	}

	descr = _PyType_Lookup(tp, name);
	f = NULL;
	if (descr != NULL &&
	    PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
		f = descr->ob_type->tp_descr_set;
		if (f != NULL && PyDescr_IsData(descr)) {
			res = f(descr, obj, value);
			goto done;
		}
	}

	dictptr = _PyObject_GetDictPtr(obj);
	if (dictptr != NULL) {
		PyObject *dict = *dictptr;
		if (dict == NULL && value != NULL) {
			dict = PyDict_New();
			if (dict == NULL)
				goto done;
			*dictptr = dict;
		}
		if (dict != NULL) {
			if (value == NULL)
				res = PyDict_DelItem(dict, name);
			else
				res = PyDict_SetItem(dict, name, value);
			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
				PyErr_SetObject(PyExc_AttributeError, name);
			goto done;
		}
	}

	if (f != NULL) {
		res = f(descr, obj, value);
		goto done;
	}

	if (descr == NULL) {
		PyErr_Format(PyExc_AttributeError,
			     "'%.100s' object has no attribute '%.200s'",
			     tp->tp_name, PyString_AS_STRING(name));
		goto done;
	}

	PyErr_Format(PyExc_AttributeError,
		     "'%.50s' object attribute '%.400s' is read-only",
		     tp->tp_name, PyString_AS_STRING(name));
  done:
	Py_DECREF(name);
	return res;
}
Example #7
0
/* This function is a more-or-less straightforward port of the
 * equivalent function in pairwise2.  Please see there for algorithm
 * documentation.
 */
static PyObject *cpairwise2__make_score_matrix_fast(
    PyObject *self, PyObject *args)
{
    int i;
    int row, col;

    PyObject *py_sequenceA, *py_sequenceB, *py_match_fn;
#if PY_MAJOR_VERSION >= 3
    PyObject *py_bytesA, *py_bytesB;
#endif
    char *sequenceA=NULL, *sequenceB=NULL;
    int use_sequence_cstring;
    double open_A, extend_A, open_B, extend_B;
    int penalize_extend_when_opening, penalize_end_gaps_A, penalize_end_gaps_B;
    int align_globally, score_only;

    PyObject *py_match=NULL, *py_mismatch=NULL;
    double first_A_gap, first_B_gap;
    double match, mismatch;
    int use_match_mismatch_scores;
    int lenA, lenB;
    double *score_matrix = NULL;
    struct IndexList *trace_matrix = NULL;
    PyObject *py_score_matrix=NULL, *py_trace_matrix=NULL;

    double *row_cache_score = NULL,
        *col_cache_score = NULL;
    struct IndexList *row_cache_index = NULL,
        *col_cache_index = NULL;

    PyObject *py_retval = NULL;

    if(!PyArg_ParseTuple(args, "OOOddddi(ii)ii", &py_sequenceA, &py_sequenceB,
                         &py_match_fn, &open_A, &extend_A, &open_B, &extend_B,
                         &penalize_extend_when_opening,
                         &penalize_end_gaps_A, &penalize_end_gaps_B,
                         &align_globally, &score_only))
        return NULL;
    if(!PySequence_Check(py_sequenceA) || !PySequence_Check(py_sequenceB)) {
        PyErr_SetString(PyExc_TypeError,
                        "py_sequenceA and py_sequenceB should be sequences.");
        return NULL;
    }

    /* Optimize for the common case.  Check to see if py_sequenceA and
       py_sequenceB are strings.  If they are, use the c string
       representation. */
#if PY_MAJOR_VERSION < 3
    use_sequence_cstring = 0;
    if(PyString_Check(py_sequenceA) && PyString_Check(py_sequenceB)) {
        sequenceA = PyString_AS_STRING(py_sequenceA);
        sequenceB = PyString_AS_STRING(py_sequenceB);
        use_sequence_cstring = 1;
    }
#else
    py_bytesA = _create_bytes_object(py_sequenceA);
    py_bytesB = _create_bytes_object(py_sequenceB);
    if (py_bytesA && py_bytesB) {
        sequenceA = PyBytes_AS_STRING(py_bytesA);
        sequenceB = PyBytes_AS_STRING(py_bytesB);
        use_sequence_cstring = 1;
    }
    else {
        Py_XDECREF(py_bytesA);
        Py_XDECREF(py_bytesB);
        use_sequence_cstring = 0;
    }
#endif

    if(!PyCallable_Check(py_match_fn)) {
        PyErr_SetString(PyExc_TypeError, "py_match_fn must be callable.");
        return NULL;
    }
    /* Optimize for the common case.  Check to see if py_match_fn is
       an identity_match.  If so, pull out the match and mismatch
       member variables and calculate the scores myself. */
    match = mismatch = 0;
    use_match_mismatch_scores = 0;
    if(!(py_match = PyObject_GetAttrString(py_match_fn, "match")))
        goto cleanup_after_py_match_fn;
    match = PyNumber_AsDouble(py_match);
    if(PyErr_Occurred())
        goto cleanup_after_py_match_fn;
    if(!(py_mismatch = PyObject_GetAttrString(py_match_fn, "mismatch")))
        goto cleanup_after_py_match_fn;
    mismatch = PyNumber_AsDouble(py_mismatch);
    if(PyErr_Occurred())
        goto cleanup_after_py_match_fn;
    use_match_mismatch_scores = 1;
cleanup_after_py_match_fn:
    if(PyErr_Occurred())
        PyErr_Clear();
    if(py_match) {
        Py_DECREF(py_match);
    }
    if(py_mismatch) {
        Py_DECREF(py_mismatch);
    }

    /* Cache some commonly used gap penalties */
    first_A_gap = calc_affine_penalty(1, open_A, extend_A,
                                      penalize_extend_when_opening);
    first_B_gap = calc_affine_penalty(1, open_B, extend_B,
                                      penalize_extend_when_opening);

    /* Allocate matrices for storing the results and initialize them. */
    lenA = PySequence_Length(py_sequenceA);
    lenB = PySequence_Length(py_sequenceB);
    score_matrix = malloc(lenA*lenB*sizeof(*score_matrix));
    trace_matrix = malloc(lenA*lenB*sizeof(*trace_matrix));
    if(!score_matrix || !trace_matrix) {
        PyErr_SetString(PyExc_MemoryError, "Out of memory");
        goto _cleanup_make_score_matrix_fast;
    }
    for(i=0; i<lenA*lenB; i++) {
        score_matrix[i] = 0;
        IndexList_init(&trace_matrix[i]);
    }

    /* Initialize the first row and col of the score matrix. */
    for(i=0; i<lenA; i++) {
        double score = _get_match_score(py_sequenceA, py_sequenceB,
                                        py_match_fn, i, 0,
                                        sequenceA, sequenceB,
                                        use_sequence_cstring,
                                        match, mismatch,
                                        use_match_mismatch_scores);
        if(PyErr_Occurred())
            goto _cleanup_make_score_matrix_fast;
        if(penalize_end_gaps_B)
            score += calc_affine_penalty(i, open_B, extend_B,
                                         penalize_extend_when_opening);
        score_matrix[i*lenB] = score;
    }
    for(i=0; i<lenB; i++) {
        double score = _get_match_score(py_sequenceA, py_sequenceB,
                                        py_match_fn, 0, i,
                                        sequenceA, sequenceB,
                                        use_sequence_cstring,
                                        match, mismatch,
                                        use_match_mismatch_scores);
        if(PyErr_Occurred())
            goto _cleanup_make_score_matrix_fast;
        if(penalize_end_gaps_A)
            score += calc_affine_penalty(i, open_A, extend_A,
                                         penalize_extend_when_opening);
        score_matrix[i] = score;
    }

    /* Now initialize the row and col cache. */
    row_cache_score = malloc((lenA-1)*sizeof(*row_cache_score));
    row_cache_index = malloc((lenA-1)* sizeof(*row_cache_index));
    col_cache_score = malloc((lenB-1)*sizeof(*col_cache_score));
    col_cache_index = malloc((lenB-1)* sizeof(*col_cache_index));
    if(!row_cache_score || !row_cache_index ||
       !col_cache_score || !col_cache_index) {
        PyErr_SetString(PyExc_MemoryError, "Out of memory");
        goto _cleanup_make_score_matrix_fast;
    }
    memset((void *)row_cache_score, 0, (lenA-1)*sizeof(*row_cache_score));
    memset((void *)row_cache_index, 0, (lenA-1)*sizeof(*row_cache_index));
    memset((void *)col_cache_score, 0, (lenB-1)*sizeof(*col_cache_score));
    memset((void *)col_cache_index, 0, (lenB-1)*sizeof(*col_cache_index));
    for(i=0; i<lenA-1; i++) {
        row_cache_score[i] = score_matrix[i*lenB] + first_A_gap;
        IndexList_append(&row_cache_index[i], i, 0);
    }
    for(i=0; i<lenB-1; i++) {
        col_cache_score[i] = score_matrix[i] + first_B_gap;
        IndexList_append(&col_cache_index[i], 0, i);
    }

    /* Fill in the score matrix. */
    for(row=1; row<lenA; row++) {
        for(col=1; col<lenB; col++) {
            double nogap_score, row_score, col_score, best_score;
            int best_score_rint;
            struct IndexList *il;

            double score, open_score, extend_score;
            int open_score_rint, extend_score_rint;

            /* Calculate the best score. */
            nogap_score = score_matrix[(row-1)*lenB+col-1];
            if(col > 1) {
                row_score = row_cache_score[row-1];
            } else {
                row_score = nogap_score-1; /* Make sure it's not best score */
            }
            if(row > 1) {
                col_score = col_cache_score[col-1];
            } else {
                col_score = nogap_score-1; /* Make sure it's not best score */
            }

            best_score = (row_score > col_score) ? row_score : col_score;
            if(nogap_score > best_score)
                best_score = nogap_score;
            best_score_rint = rint(best_score);

            /* Set the score and traceback matrices. */
            score = best_score + _get_match_score(py_sequenceA, py_sequenceB,
                                                  py_match_fn, row, col,
                                                  sequenceA, sequenceB,
                                                  use_sequence_cstring,
                                                  match, mismatch,
                                                  use_match_mismatch_scores);
            if(PyErr_Occurred())
                goto _cleanup_make_score_matrix_fast;
            if(!align_globally && score < 0)
                score_matrix[row*lenB+col] = 0;
            else
                score_matrix[row*lenB+col] = score;

            il = &trace_matrix[row*lenB+col];
            if(best_score_rint == rint(nogap_score)) {
                IndexList_append(il, row-1, col-1);
            }
            if(best_score_rint == rint(row_score)) {
                IndexList_extend(il, &row_cache_index[row-1]);
            }
            if(best_score_rint == rint(col_score)) {
                IndexList_extend(il, &col_cache_index[col-1]);
            }

            /* Update the cached column scores. */
            open_score = score_matrix[(row-1)*lenB+col-1] + first_B_gap;
            extend_score = col_cache_score[col-1] + extend_B;
            open_score_rint = rint(open_score);
            extend_score_rint = rint(extend_score);
            if(open_score_rint > extend_score_rint) {
                col_cache_score[col-1] = open_score;
                IndexList_clear(&col_cache_index[col-1]);
                IndexList_append(&col_cache_index[col-1], row-1, col-1);
            } else if(extend_score_rint > open_score_rint) {
                col_cache_score[col-1] = extend_score;
            } else {
                col_cache_score[col-1] = open_score;
                if(!IndexList_contains(&col_cache_index[col-1], row-1, col-1))
                    IndexList_append(&col_cache_index[col-1], row-1, col-1);
            }

            /* Update the cached row scores. */
            open_score = score_matrix[(row-1)*lenB+col-1] + first_A_gap;
            extend_score = row_cache_score[row-1] + extend_A;
            open_score_rint = rint(open_score);
            extend_score_rint = rint(extend_score);
            if(open_score_rint > extend_score_rint) {
                row_cache_score[row-1] = open_score;
                IndexList_clear(&row_cache_index[row-1]);
                IndexList_append(&row_cache_index[row-1], row-1, col-1);
            } else if(extend_score_rint > open_score_rint) {
                row_cache_score[row-1] = extend_score;
            } else {
                row_cache_score[row-1] = open_score;
                if(!IndexList_contains(&row_cache_index[row-1], row-1, col-1))
                    IndexList_append(&row_cache_index[row-1], row-1, col-1);
            }
        }
    }

    /* Save the score and traceback matrices into real python objects. */
    if(!(py_score_matrix = PyList_New(lenA)))
        goto _cleanup_make_score_matrix_fast;
    if(!(py_trace_matrix = PyList_New(lenA)))
        goto _cleanup_make_score_matrix_fast;
    for(row=0; row<lenA; row++) {
        PyObject *py_score_row, *py_trace_row;
        if(!(py_score_row = PyList_New(lenB)))
            goto _cleanup_make_score_matrix_fast;
        PyList_SET_ITEM(py_score_matrix, row, py_score_row);
        if(!(py_trace_row = PyList_New(lenB)))
            goto _cleanup_make_score_matrix_fast;
        PyList_SET_ITEM(py_trace_matrix, row, py_trace_row);

        for(col=0; col<lenB; col++) {
            int i;
            PyObject *py_score, *py_indexlist;
            int offset = row*lenB + col;
            struct IndexList *il = &trace_matrix[offset];

            /* Set py_score_matrix[row][col] to the score. */
            if(!(py_score = PyFloat_FromDouble(score_matrix[offset])))
                goto _cleanup_make_score_matrix_fast;
            PyList_SET_ITEM(py_score_row, col, py_score);

            if(score_only)
                continue;
            /* Set py_trace_matrix[row][col] to a list of indexes.  On
               the edges of the matrix (row or column is 0), the
               matrix should be [None]. */
            if(!row || !col) {
                if(!(py_indexlist = PyList_New(1)))
                    goto _cleanup_make_score_matrix_fast;
                Py_INCREF(Py_None);
                PyList_SET_ITEM(py_indexlist, 0, Py_None);
            }
            else {
                if(!(py_indexlist = PyList_New(il->num_used)))
                    goto _cleanup_make_score_matrix_fast;
                for(i=0; i<il->num_used; i++) {
                    PyObject *py_index=NULL;
                    int row = il->indexes[i*2],
                        col = il->indexes[i*2+1];
                    if(!(py_index = Py_BuildValue("(ii)", row, col)))
                        goto _cleanup_make_score_matrix_fast;
                    PyList_SET_ITEM(py_indexlist, i, py_index);
                }
            }
            PyList_SET_ITEM(py_trace_row, col, py_indexlist);
        }
    }

    py_retval = Py_BuildValue("(OO)", py_score_matrix, py_trace_matrix);


 _cleanup_make_score_matrix_fast:
    if(score_matrix)
        free(score_matrix);
    if(trace_matrix) {
        for(i=0; i<lenA*lenB; i++)
            IndexList_free(&trace_matrix[i]);
        free(trace_matrix);
    }
    if(row_cache_score)
        free(row_cache_score);
    if(col_cache_score)
        free(col_cache_score);
    if(row_cache_index) {
        for(i=0; i<lenA-1; i++)
            IndexList_free(&row_cache_index[i]);
        free(row_cache_index);
    }
    if(col_cache_index) {
        for(i=0; i<lenB-1; i++) {
            IndexList_free(&col_cache_index[i]);
        }
        free(col_cache_index);
    }
    if(py_score_matrix) {
        Py_DECREF(py_score_matrix);
    }
    if(py_trace_matrix) {
        Py_DECREF(py_trace_matrix);
    }
#if PY_MAJOR_VERSION >= 3
    if (py_bytesA != NULL && py_bytesA != py_sequenceA) Py_DECREF(py_bytesA);
    if (py_bytesB != NULL && py_bytesB != py_sequenceB) Py_DECREF(py_bytesB);
#endif

    return py_retval;
}
Example #8
0
char *SortedDict_iterGetName(JSOBJ obj, JSONTypeContext *tc, size_t *outLen)
{
  *outLen = PyString_GET_SIZE(GET_TC(tc)->itemName);
  return PyString_AS_STRING(GET_TC(tc)->itemName);
}
PyObject *_translate_pathtranslate ( PyObject *self, PyObject *args,
                                     PyObject *kwargs )
/*
 * Usage: translate ( path, map, sep = "/." )
 */
{
    char *path = NULL, *sep = "/.";
    PyObject *ret, *dict, *val;
    
    /* Variable length buffer */
    size_t i, j = 0, now = 0, end = BUFSIZ;
    char *out = malloc ( end ), *s, *ourpath;
    char delim;

    /* The argument list */
    static char *kwlist[] = { "path", "map", "sep", NULL };

    if ( !PyArg_ParseTupleAndKeywords ( args, kwargs, "sO!|s", kwlist, 
                                        &path, &PyDict_Type, &dict, &sep ) )
        return NULL;

    /* Path is mutated in process - make a copy */
    ourpath = strdup( path );

    for ( i = strcspn ( ourpath + j, sep ); i + j <= strlen(path); 
          i = strcspn ( ourpath + j, sep ))
    {
        /* Get the separator character */
        if ( (delim = ourpath[i+j]) != '\0' )
            ourpath[i+j] = '\0';

        /* The segment is from path + j to i */
        if ( (val = PyDict_GetItemString ( dict, ourpath + j )) )
        {
            /* Translate and copy */
            if ( !(PyString_Check(val)) )
            {
                PyErr_SetString ( PyExc_ValueError, "mapping should contain "
                                                    "string values only" );
                return NULL;
            }

            s = PyString_AS_STRING(val);
        }
        else
        {
            /* No translation, plain copy */
            s = ourpath + j;
        }

        /* Reallocate if necessary */
        while ( now + strlen(s) + 2 > end )
            out = realloc ( out, (end = end + BUFSIZ) );

        /* Copy the new value */
        strcpy ( out + now, s );

        now += strlen(s);
        out[now++] = delim;           /* Add the separator or '\0'*/

        /* Increment j */
        j += i + 1;
    }

    /* Create the return value */
    ret = PyString_FromString ( out );

    free ( out );
    free ( ourpath );

    return ret;
}
Example #10
0
int Dir_iterNext(JSOBJ _obj, JSONTypeContext *tc)
{
  PyObject *obj = (PyObject *) _obj;
  PyObject *itemValue = GET_TC(tc)->itemValue;
  PyObject *itemName = GET_TC(tc)->itemName;
  PyObject* attr;
  PyObject* attrName;
  char* attrStr;

  if (itemValue)
  {
    Py_DECREF(GET_TC(tc)->itemValue);
    GET_TC(tc)->itemValue = itemValue = NULL;
  }

  if (itemName)
  {
    Py_DECREF(GET_TC(tc)->itemName);
    GET_TC(tc)->itemName = itemName = NULL;
  }

  for (; GET_TC(tc)->index  < GET_TC(tc)->size; GET_TC(tc)->index ++)
  {
    attrName = PyList_GET_ITEM(GET_TC(tc)->attrList, GET_TC(tc)->index);
#if PY_MAJOR_VERSION >= 3
    attr = PyUnicode_AsUTF8String(attrName);
#else
    attr = attrName;
    Py_INCREF(attr);
#endif
    attrStr = PyString_AS_STRING(attr);

    if (attrStr[0] == '_')
    {
      PRINTMARK();
      Py_DECREF(attr);
      continue;
    }

    itemValue = PyObject_GetAttr(obj, attrName);
    if (itemValue == NULL)
    {
      PyErr_Clear();
      Py_DECREF(attr);
      PRINTMARK();
      continue;
    }

    if (PyCallable_Check(itemValue))
    {
      Py_DECREF(itemValue);
      Py_DECREF(attr);
      PRINTMARK();
      continue;
    }

    PRINTMARK();
    itemName = attr;
    break;
  }

  if (itemName == NULL)
  {
    GET_TC(tc)->index = GET_TC(tc)->size;
    GET_TC(tc)->itemValue = NULL;
    return 0;
  }

  GET_TC(tc)->itemName = itemName;
  GET_TC(tc)->itemValue = itemValue;
  GET_TC(tc)->index ++;

  PRINTMARK();
  return 1;
}
Example #11
0
char *Dir_iterGetName(JSOBJ obj, JSONTypeContext *tc, size_t *outLen)
{
  PRINTMARK();
  *outLen = PyString_GET_SIZE(GET_TC(tc)->itemName);
  return PyString_AS_STRING(GET_TC(tc)->itemName);
}
Example #12
0
static void *PyStringToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
{
  PyObject *obj = (PyObject *) _obj;
  *_outLen = PyString_GET_SIZE(obj);
  return PyString_AS_STRING(obj);
}
Example #13
0
static PyObject* __Pyx_PyExec3(PyObject* o, PyObject* globals, PyObject* locals) {
    PyObject* result;
    PyObject* s = 0;
    char *code = 0;

    if (!globals || globals == Py_None) {
        globals = $moddict_cname;
    } else if (!PyDict_Check(globals)) {
        PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.200s",
                     Py_TYPE(globals)->tp_name);
        goto bad;
    }
    if (!locals || locals == Py_None) {
        locals = globals;
    }

    if (PyDict_GetItem(globals, PYIDENT("__builtins__")) == NULL) {
        if (PyDict_SetItem(globals, PYIDENT("__builtins__"), PyEval_GetBuiltins()) < 0)
            goto bad;
    }

    if (PyCode_Check(o)) {
        if (PyCode_GetNumFree((PyCodeObject *)o) > 0) {
            PyErr_SetString(PyExc_TypeError,
                "code object passed to exec() may not contain free variables");
            goto bad;
        }
        #if PY_VERSION_HEX < 0x030200B1
        result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
        #else
        result = PyEval_EvalCode(o, globals, locals);
        #endif
    } else {
        PyCompilerFlags cf;
        cf.cf_flags = 0;
        if (PyUnicode_Check(o)) {
            cf.cf_flags = PyCF_SOURCE_IS_UTF8;
            s = PyUnicode_AsUTF8String(o);
            if (!s) goto bad;
            o = s;
        #if PY_MAJOR_VERSION >= 3
        } else if (!PyBytes_Check(o)) {
        #else
        } else if (!PyString_Check(o)) {
        #endif
            PyErr_Format(PyExc_TypeError,
                "exec: arg 1 must be string, bytes or code object, got %.200s",
                Py_TYPE(o)->tp_name);
            goto bad;
        }
        #if PY_MAJOR_VERSION >= 3
        code = PyBytes_AS_STRING(o);
        #else
        code = PyString_AS_STRING(o);
        #endif
        if (PyEval_MergeCompilerFlags(&cf)) {
            result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
        } else {
            result = PyRun_String(code, Py_file_input, globals, locals);
        }
        Py_XDECREF(s);
    }

    return result;
bad:
    Py_XDECREF(s);
    return 0;
}
Example #14
0
/**
Constructor//Initializer//Destructor.
*/
static PyObject *
Node_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	Node *self;
	PyObject *weights = NULL;

	self = (Node *)type->tp_alloc(type, 0);
	if (self != NULL) {
		// Default values
		self->weights = NULL;
		self->random_range = 1;
		self->activation_function = NULL;
		self->cached_output = 0;

		static char *kwlist[] = {"active", "random_range", "weights", NULL};

		if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SdO", kwlist,
							&self->activation_function,
							&self->random_range,
							&weights))
		{
			PyErr_Format(PyExc_ValueError, "Arguments should be (all optional): string active, double random_range, dict weights");
			return NULL;
		}

		// Weights
		if (weights == NULL)
		{
			self->weights = PyDict_New();
		}
		else if (PyDict_Check(weights)) {
			self->weights = weights;
			Py_INCREF(self->weights);
		}
		else {
			// Incorrect object...
			PyErr_Format(PyExc_ValueError, "Weights was not a dict!");
			return NULL;
		}

		// Set activation function and derivative
		if (self->activation_function != NULL && strcmp (PyString_AS_STRING(self->activation_function), "logsig" ) == 0)
		{
			Py_INCREF(self->activation_function);
			self->function = logsig;
			self->derivative = logsig_derivative;
		}
		else if (self->activation_function != NULL && strcmp (PyString_AS_STRING(self->activation_function), "tanh" ) == 0)
		{
			Py_INCREF(self->activation_function);
			self->function = tanh;
			self->derivative = tanh_derivative;
		}
		else // Linear it is!
		{
		self->activation_function = (PyStringObject*) PyString_FromString("linear");
		self->function = linear;
		self->derivative = linear_derivative;
		}
	} //if !self null

	return (PyObject *)self;
}
Example #15
0
static PyObject * growl_PostDictionary(CFStringRef name, PyObject *self, PyObject *args) {
	int i, j;

	PyObject *inputDict;
	PyObject *pKeys = NULL;
	PyObject *pKey, *pValue;

	CFMutableDictionaryRef note = CFDictionaryCreateMutable(kCFAllocatorDefault,
															/*capacity*/ 0,
															&kCFTypeDictionaryKeyCallBacks,
															&kCFTypeDictionaryValueCallBacks);

	if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &inputDict))
		goto error;

	pKeys = PyDict_Keys(inputDict);
	for (i = 0; i < PyList_Size(pKeys); ++i) {
		CFStringRef convertedKey;

		/* Converting the PyDict key to NSString and used for key in note */
		pKey = PyList_GetItem(pKeys, i);
		if (!pKey)
			// Exception already set
			goto error;
		pValue = PyDict_GetItem(inputDict, pKey);
		if (!pValue) {
			// XXX Neeed a real Error message here.
			PyErr_SetString(PyExc_TypeError," ");
			goto error;
		}
		if (PyUnicode_Check(pKey)) {
			convertedKey = CFStringCreateWithBytes(kCFAllocatorDefault,
												   (const UInt8 *)PyUnicode_AS_DATA(pKey),
												   PyUnicode_GET_DATA_SIZE(pKey),
												   kCFStringEncodingUnicode,
												   false);
		} else if (PyString_Check(pKey)) {
			convertedKey = CFStringCreateWithCString(kCFAllocatorDefault,
													 PyString_AsString(pKey),
													 kCFStringEncodingUTF8);
		} else {
			PyErr_SetString(PyExc_TypeError,"The Dict keys must be strings/unicode");
			goto error;
		}

		/* Converting the PyDict value to NSString or NSData based on class  */
		if (PyString_Check(pValue)) {
			CFStringRef convertedValue = CFStringCreateWithCString(kCFAllocatorDefault,
																   PyString_AS_STRING(pValue),
																   kCFStringEncodingUTF8);
			CFDictionarySetValue(note, convertedKey, convertedValue);
			CFRelease(convertedValue);
		} else if (PyUnicode_Check(pValue)) {
			CFStringRef convertedValue = CFStringCreateWithBytes(kCFAllocatorDefault,
																 (const UInt8 *)PyUnicode_AS_DATA(pValue),
																 PyUnicode_GET_DATA_SIZE(pValue),
																 kCFStringEncodingUnicode,
																 false);
			CFDictionarySetValue(note, convertedKey, convertedValue);
			CFRelease(convertedValue);
		} else if (PyInt_Check(pValue)) {
			long v = PyInt_AS_LONG(pValue);
			CFNumberRef convertedValue = CFNumberCreate(kCFAllocatorDefault,
														kCFNumberLongType,
														&v);
			CFDictionarySetValue(note, convertedKey, convertedValue);
			CFRelease(convertedValue);
		} else if (pValue == Py_None) {
			CFDataRef convertedValue = CFDataCreate(kCFAllocatorDefault, NULL, 0);
			CFDictionarySetValue(note, convertedKey, convertedValue);
			CFRelease(convertedValue);
		} else if (PyList_Check(pValue)) {
			int size = PyList_Size(pValue);
			CFMutableArrayRef listHolder = CFArrayCreateMutable(kCFAllocatorDefault,
																size,
																&kCFTypeArrayCallBacks);
			for (j = 0; j < size; ++j) {
				PyObject *lValue = PyList_GetItem(pValue, j);
				if (PyString_Check(lValue)) {
					CFStringRef convertedValue = CFStringCreateWithCString(kCFAllocatorDefault,
																		   PyString_AS_STRING(lValue),
																		   kCFStringEncodingUTF8);
					CFArrayAppendValue(listHolder, convertedValue);
					CFRelease(convertedValue);
				} else if (PyUnicode_Check(lValue)) {
					CFStringRef convertedValue = CFStringCreateWithBytes(kCFAllocatorDefault,
																		 (const UInt8 *)PyUnicode_AS_DATA(lValue),
																		 PyUnicode_GET_DATA_SIZE(lValue),
																		 kCFStringEncodingUnicode,
																		 false);
					CFArrayAppendValue(listHolder, convertedValue);
					CFRelease(convertedValue);
				} else {
					CFRelease(convertedKey);
					PyErr_SetString(PyExc_TypeError,"The lists must only contain strings");
					goto error;
				}
			}
			CFDictionarySetValue(note, convertedKey, listHolder);
			CFRelease(listHolder);
		} else if (PyObject_HasAttrString(pValue, "rawImageData")) {
			PyObject *lValue = PyObject_GetAttrString(pValue, "rawImageData");
			if (!lValue) {
				goto error;
			} else if (PyString_Check(lValue)) {
				CFDataRef convertedValue = CFDataCreate(kCFAllocatorDefault,
														(const UInt8 *)PyString_AsString(lValue),
														PyString_Size(lValue));
				CFDictionarySetValue(note, convertedKey, convertedValue);
				CFRelease(convertedValue);
			} else {
				CFRelease(convertedKey);
				PyErr_SetString(PyExc_TypeError, "Icon with rawImageData attribute present must ensure it is a string.");
				goto error;
			}
		} else {
			CFRelease(convertedKey);
			PyErr_SetString(PyExc_TypeError, "Value is not of Str/List");
			goto error;
		}
		CFRelease(convertedKey);
	}

	Py_BEGIN_ALLOW_THREADS
	CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(),
										 /*name*/ name,
										 /*object*/ NULL,
										 /*userInfo*/ note,
										 /*deliverImmediately*/ false);
	CFRelease(note);
	Py_END_ALLOW_THREADS

	Py_DECREF(pKeys);

	Py_INCREF(Py_None);
	return Py_None;

error:
	CFRelease(note);

	Py_XDECREF(pKeys);

	return NULL;
}
Example #16
0
static PyObject *
func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
{
	PyCodeObject *code;
	PyObject *globals;
	PyObject *name = Py_None;
	PyObject *defaults = Py_None;
	PyObject *closure = Py_None;
	PyFunctionObject *newfunc;
	Py_ssize_t nfree, nclosure;
	static char *kwlist[] = {"code", "globals", "name",
							 "argdefs", "closure", 0};

	if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
						  kwlist,
						  &PyCode_Type, &code,
						  &PyDict_Type, &globals,
						  &name, &defaults, &closure))
		return NULL;
	if (name != Py_None && !PyString_Check(name)) {
		PyErr_SetString(PyExc_TypeError,
						"arg 3 (name) must be None or string");
		return NULL;
	}
	if (defaults != Py_None && !PyTuple_Check(defaults)) {
		PyErr_SetString(PyExc_TypeError,
						"arg 4 (defaults) must be None or tuple");
		return NULL;
	}
	nfree = PyTuple_GET_SIZE(code->co_freevars);
	if (!PyTuple_Check(closure)) {
		if (nfree && closure == Py_None) {
			PyErr_SetString(PyExc_TypeError,
							"arg 5 (closure) must be tuple");
			return NULL;
		}
		else if (closure != Py_None) {
			PyErr_SetString(PyExc_TypeError,
				"arg 5 (closure) must be None or tuple");
			return NULL;
		}
	}

	/* check that the closure is well-formed */
	nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
	if (nfree != nclosure)
		return PyErr_Format(PyExc_ValueError,
							"%s requires closure of length %zd, not %zd",
							PyString_AS_STRING(code->co_name),
							nfree, nclosure);
	if (nclosure) {
		Py_ssize_t i;
		for (i = 0; i < nclosure; i++) {
			PyObject *o = PyTuple_GET_ITEM(closure, i);
			if (!PyCell_Check(o)) {
				return PyErr_Format(PyExc_TypeError,
					"arg 5 (closure) expected cell, found %s",
					o->ob_type->tp_name);
			}
		}
	}

	newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, globals);
	if (newfunc == NULL)
		return NULL;

	if (name != Py_None) {
		func_set_name(newfunc, name);
//		Py_INCREF(name);
//		Py_DECREF(newfunc->func_name);
//		newfunc->func_name = name;
	}
	if (defaults != Py_None) {
		Py_INCREF(defaults);
		newfunc->func_defaults  = defaults;
	}
	if (closure != Py_None) {
		Py_INCREF(closure);
		newfunc->func_closure = closure;
	}

	return (PyObject *)newfunc;
}
Example #17
0
PyObject *
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
{
	PyTypeObject *tp = obj->ob_type;
	PyObject *descr = NULL;
	PyObject *res = NULL;
	descrgetfunc f;
	Py_ssize_t dictoffset;
	PyObject **dictptr;

	if (!PyString_Check(name)){
#ifdef Py_USING_UNICODE
		/* The Unicode to string conversion is done here because the
		   existing tp_setattro slots expect a string object as name
		   and we wouldn't want to break those. */
		if (PyUnicode_Check(name)) {
			name = PyUnicode_AsEncodedString(name, NULL, NULL);
			if (name == NULL)
				return NULL;
		}
		else
#endif
		{
			PyErr_Format(PyExc_TypeError,
				     "attribute name must be string, not '%.200s'",
				     name->ob_type->tp_name);
			return NULL;
		}
	}
	else
		Py_INCREF(name);

	if (tp->tp_dict == NULL) {
		if (PyType_Ready(tp) < 0)
			goto done;
	}

	/* Inline _PyType_Lookup */
	{
		Py_ssize_t i, n;
		PyObject *mro, *base, *dict;

		/* Look in tp_dict of types in MRO */
		mro = tp->tp_mro;
		assert(mro != NULL);
		assert(PyTuple_Check(mro));
		n = PyTuple_GET_SIZE(mro);
		for (i = 0; i < n; i++) {
			base = PyTuple_GET_ITEM(mro, i);
			if (PyClass_Check(base))
				dict = ((PyClassObject *)base)->cl_dict;
			else {
				assert(PyType_Check(base));
				dict = ((PyTypeObject *)base)->tp_dict;
			}
			assert(dict && PyDict_Check(dict));
			descr = PyDict_GetItem(dict, name);
			if (descr != NULL)
				break;
		}
	}

	Py_XINCREF(descr);

	f = NULL;
	if (descr != NULL &&
	    PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
		f = descr->ob_type->tp_descr_get;
		if (f != NULL && PyDescr_IsData(descr)) {
			res = f(descr, obj, (PyObject *)obj->ob_type);
			Py_DECREF(descr);
			goto done;
		}
	}

	/* Inline _PyObject_GetDictPtr */
	dictoffset = tp->tp_dictoffset;
	if (dictoffset != 0) {
		PyObject *dict;
		if (dictoffset < 0) {
			Py_ssize_t tsize;
			size_t size;

			tsize = ((PyVarObject *)obj)->ob_size;
			if (tsize < 0)
				tsize = -tsize;
			size = _PyObject_VAR_SIZE(tp, tsize);

			dictoffset += (long)size;
			assert(dictoffset > 0);
			assert(dictoffset % SIZEOF_VOID_P == 0);
		}
		dictptr = (PyObject **) ((char *)obj + dictoffset);
		dict = *dictptr;
		if (dict != NULL) {
			res = PyDict_GetItem(dict, name);
			if (res != NULL) {
				Py_INCREF(res);
				Py_XDECREF(descr);
				goto done;
			}
		}
	}

	if (f != NULL) {
		res = f(descr, obj, (PyObject *)obj->ob_type);
		Py_DECREF(descr);
		goto done;
	}

	if (descr != NULL) {
		res = descr;
		/* descr was already increfed above */
		goto done;
	}

	PyErr_Format(PyExc_AttributeError,
		     "'%.50s' object has no attribute '%.400s'",
		     tp->tp_name, PyString_AS_STRING(name));
  done:
	Py_DECREF(name);
	return res;
}
Example #18
0
/* Generic exact reader */
static PyObject *
generic_read_exact(PyObject *(*reader)(void *, Py_ssize_t), void *ctx,
                   Py_ssize_t size)
{
    PyObject *result;
    bufitem *start, *item, *newitem;
    char *buf;
    Py_ssize_t vlen;

    if (size < 0)
        return reader(ctx, size);

    /* ok then... fetch the stuff */
    start = item = NULL;
    vlen = 0;
    while (vlen < size) {
        result = reader(ctx, size-vlen);
        if (!result) {
            if (PyErr_Occurred())
                goto error;
            break;
        }
        if (PyString_GET_SIZE(result) > 0) {
            if ((vlen + PyString_GET_SIZE(result)) < vlen) {
                Py_DECREF(result);
                PyErr_SetString(PyExc_OverflowError,
                                "Result buffer got too big");
                goto error;
            }
            if (!(newitem = bufitem_new())) {
                Py_DECREF(result);
                goto error;
            }
            vlen += PyString_GET_SIZE(result);
            newitem->next = NULL;
            newitem->load = result;
            if (!item) {
                start = item = newitem;
            }
            else {
                item->next = newitem;
                item = newitem;
            }
        }
    }

    /* ...and assemble the result */
    if (vlen > 0) {
        if (!(result = PyString_FromStringAndSize(NULL, vlen)))
            goto error;
        buf = PyString_AS_STRING(result);
        while (start) {
            (void)memcpy(buf, PyString_AS_STRING(start->load),
                         (size_t)PyString_GET_SIZE(start->load));
            buf += PyString_GET_SIZE(start->load);
            start = bufitem_del(start);
        }
        return result;
    }

error:
    while (start)
        start = bufitem_del(start);
    return NULL;
}
Example #19
0
File: split.c Project: S-YOU/split
static PyObject*
_csv(PyObject* self, PyObject* arg) {
	size_t sLen = 0, splitterLen = 1, maxsplit = 1, index = 0, insideQuote = 0;
	TYPE *src = NULL, *s, *ss, *sEnd = NULL, *sPrev = NULL, *escaped = NULL;
	TYPE quoteChar = '\'';
	TYPE defaultSplitter = ',', *splitter = &defaultSplitter, *sp, *spEnd = splitter + splitterLen;
	PyObject *list;
	PyTupleObject* argTuple = (PyTupleObject*) arg;

	if (Py_SIZE(argTuple) < 2) return NULL;

	if (PyTYPE_CHECK(argTuple->ob_item[0])) {
		src = PyTYPE_AS_TYPE(argTuple->ob_item[0]);
		sLen = PyTYPE_GET_SIZE(argTuple->ob_item[0]);
	} else {
		return NULL;
	}

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

	if (argTuple->ob_size > 2) {
		if (argTuple->ob_item[2] == Py_None) {
			quoteChar = 0;
		} else if (PyString_CheckExact(argTuple->ob_item[2])) {
			quoteChar = *PyString_AS_STRING(argTuple->ob_item[2]);
		} else if (PyUnicode_CheckExact(argTuple->ob_item[2])) {
			quoteChar = *PyUnicode_AS_UNICODE(argTuple->ob_item[2]);
		} else {
			return NULL;
		}
	}

	if (argTuple->ob_size > 3) {
		if (PyTYPE_CHECK(argTuple->ob_item[3])) {
			splitter = PyTYPE_AS_TYPE(argTuple->ob_item[3]);
			splitterLen = PyTYPE_GET_SIZE(argTuple->ob_item[3]);
			spEnd = splitter + splitterLen;
		} else {
			return NULL;
		}
	}

	sPrev = s = src, sEnd = src + sLen;
	list = PyTuple_New(maxsplit);
	while (s < sEnd) {
		if (*s == *splitter && !insideQuote) {
			if (splitterLen > 1) {
				sp = splitter, ss = s;
				while (*++sp == *++ss);
				if (sp >= spEnd) {
					ADD_CSV;
					if (index >= maxsplit) goto done;
					sPrev = s + splitterLen;
					s += splitterLen;
					continue;
				}
			} else {
				ADD_CSV;
				if (index >= maxsplit) goto done;
				sPrev = s + 1;
			}
		} else if (*s == '\\') {
			s++, escaped = s;
		} else if (quoteChar && *s == quoteChar && s != escaped) insideQuote = !insideQuote;
		s++;
	}
	if (index < maxsplit) {
		ADD_CSV;
		if (index < maxsplit) {
			Py_DECREF(list);
			Py_RETURN_NONE;
		}
	}
done:
	return (PyObject*) list;
}
Example #20
0
/* buffer-read a block (return NULL on eof) */
static PyObject *
generic_read(void *self_, Py_ssize_t size)
{
    PyObject *tmp, *result, *sizeobj = NULL;
    bufitem *item;
    genericstreamobject *self=self_;
    char *jptr, *sentinel;
    Py_ssize_t cursize, rsize;

    if (!self->read) {
        PyErr_SetString(PyExc_AttributeError,
            "This stream does not provide a read function"
        );
        return NULL;
    }

    /* return a bufitem */
    if (size == 0) {
        if ((item = self->rbuf_last)) {
            result = item->load;
            Py_INCREF(result);
            if (!(self->rbuf_last = item->next)) {
                self->rbuf = NULL;
                self->rbuf_size = 0;
            }
            else
                self->rbuf_size -= PyString_GET_SIZE(result);
            (void)bufitem_del(item);
            return result;
        }
        else if (self->flags & GENERIC_STREAM_EOF)
            return NULL;
        /* else */
        size = self->chunk_size;
    }

    /* read up to size bytes */
    if (   !(self->flags & GENERIC_STREAM_EOF)
           && (size > 0) && (size > self->rbuf_size)) {
        if (!(sizeobj = optimal_chunk_size(self, size)))
            return NULL;
        if (!(tmp = PyObject_CallObject(self->read, sizeobj)))
            goto error;
        if (PyString_CheckExact(tmp))
            result = tmp;
        else {
            result = PyObject_Str(tmp);
            Py_DECREF(tmp);
            if (!result)
                goto error;
        }

        rsize = PyString_GET_SIZE(result);
        if (rsize > 0) {
            if ((self->rbuf_size + rsize) < self->rbuf_size) {
                PyErr_SetString(PyExc_OverflowError, "Buffer became too big");
                Py_DECREF(result);
                goto error;
            }
            if (!(item = bufitem_new())) {
                Py_DECREF(result);
                goto error;
            }
            item->next = NULL;
            item->load = result;
            if (self->rbuf) {
                self->rbuf->next = item; /* Note to self:         */
                self->rbuf = item;       /* this code is correct. */
                self->rbuf_size += rsize;
            }
            else {
                self->rbuf = self->rbuf_last = item;
                self->rbuf_size = rsize;
            }
        }
        else {
            Py_DECREF(result);
            self->flags |= GENERIC_STREAM_EOF;
        }
    }

    /* slurp it all */
    else if (!(self->flags & GENERIC_STREAM_EOF) && (size < 0)) {
        if (!(sizeobj = optimal_chunk_size(self, size)))
            return NULL;
        while (1) {
            if (!(tmp = PyObject_CallObject(self->read, sizeobj)))
                goto error;
            if (PyString_CheckExact(tmp))
                result = tmp;
            else {
                result = PyObject_Str(tmp);
                Py_DECREF(tmp);
                if (!result)
                    goto error;
            }
            rsize = PyString_GET_SIZE(result);
            if (rsize > 0) {
                if ((self->rbuf_size + rsize) < self->rbuf_size) {
                    PyErr_SetString(PyExc_OverflowError,
                                    "Buffer became too big");
                    Py_DECREF(result);
                    goto error;
                }
                if (!(item = bufitem_new())) {
                    Py_DECREF(result);
                    goto error;
                }
                item->next = NULL;
                item->load = result;
                if (self->rbuf) {
                    self->rbuf->next = item; /* Note to self:         */
                    self->rbuf = item;       /* this code is correct. */
                    self->rbuf_size += rsize;
                }
                else {
                    self->rbuf = self->rbuf_last = item;
                    self->rbuf_size = rsize;
                }
            }
            else {
                Py_DECREF(result);
                self->flags |= GENERIC_STREAM_EOF;
                break;
            }
        }
        size = self->rbuf_size;
    }
    Py_XDECREF(sizeobj);

    if (!self->rbuf_size) {
        self->flags |= GENERIC_STREAM_EOF;
        return NULL;
    }

    /* flatten the bufitems into the result string */
    rsize = size <= self->rbuf_size ? size : self->rbuf_size;
    if (!(result = PyString_FromStringAndSize(NULL, rsize)))
        return NULL;
    jptr = PyString_AS_STRING(result);
    sentinel = jptr + rsize;
    while (jptr < sentinel && (item = self->rbuf_last)) {
        cursize = PyString_GET_SIZE(item->load);
        if (jptr + cursize > sentinel) { /* need to split */
            bufitem *newitem;
            
            if (!(newitem = bufitem_new())) {
                Py_DECREF(result);
                return NULL;
            }
            newitem->next = item->next;
            newitem->load = PyString_FromStringAndSize(
                PyString_AS_STRING(item->load) + (size_t)(sentinel - jptr),
                (cursize - (Py_ssize_t)(sentinel - jptr))
            );
            if (!newitem->load) {
                (void)bufitem_del(newitem);
                Py_DECREF(result);
                return NULL;
            }
            item->next = newitem;
            if (self->rbuf == item)
                self->rbuf = newitem;
            cursize = (Py_ssize_t)(sentinel - jptr);
        }
        (void)memcpy(jptr, PyString_AS_STRING(item->load),
                     (size_t)cursize);
        jptr += cursize;
        self->rbuf_size -= cursize;
        if (self->rbuf == item)
            self->rbuf = NULL;
        self->rbuf_last = bufitem_del(item);
    }

    return result;

error:
    Py_XDECREF(sizeobj);
    return NULL;
}
Example #21
0
static PyObject* pyfsevents_schedule(PyObject* self, PyObject* args,
                                     PyObject *keywds) {
    PyObject* thread;
    PyObject* stream;
    PyObject* paths;
    PyObject* callback;
    PyObject* show_file_events;

    // default latency to be used.
    double latency = 0.01; 
    
    static char *kwlist[] = {"thread", "stream", "callback", "paths", 
                             "show_file_events", "latency", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, keywds, "OOOOO|d:schedule", kwlist,
                                     &thread, &stream, &callback, &paths,
                                     &show_file_events, &latency))
        return NULL;
    
    /* stream must not already have been scheduled */
    if (PyDict_Contains(streams, stream) == 1) {
        return NULL;
    }

    /* create path array */
    CFMutableArrayRef cfArray;
    cfArray = CFArrayCreateMutable(kCFAllocatorDefault, 1,
                                   &kCFTypeArrayCallBacks);
    if (cfArray == NULL)
        return NULL;

    int i;
    Py_ssize_t size = PyList_Size(paths);
    const char* path;
    CFStringRef cfStr;
    for (i=0; i<size; i++) {
        path = PyString_AS_STRING(PyList_GetItem(paths, i));
        cfStr = CFStringCreateWithCString(kCFAllocatorDefault,
                                          path,
                                          kCFStringEncodingUTF8);
        CFArraySetValueAtIndex(cfArray, i, cfStr);
        CFRelease(cfStr);
    }

    /* allocate stream info structure */
    FSEventStreamInfo * info = PyMem_New(FSEventStreamInfo, 1);

    /* create event stream */
    FSEventStreamContext context = {0, (void*) info, NULL, NULL, NULL};
    FSEventStreamRef fsstream = NULL;
    
    UInt32 flags = kFSEventStreamCreateFlagNoDefer;
    if(show_file_events == Py_True){
        flags = flags | kFSEventStreamCreateFlagFileEvents;
    }

    fsstream = FSEventStreamCreate(kCFAllocatorDefault,
                                   (FSEventStreamCallback)&handler,
                                   &context,
                                   cfArray,
                                   kFSEventStreamEventIdSinceNow,
                                   latency,
                                   flags);
    CFRelease(cfArray);

    PyObject* value = PyCObject_FromVoidPtr((void*) fsstream, PyMem_Free);
    PyDict_SetItem(streams, stream, value);

    /* get runloop reference from observer info data or current */
    value = PyDict_GetItem(loops, thread);
    CFRunLoopRef loop;
    if (value == NULL) {
        loop = CFRunLoopGetCurrent();
    } else {
        loop = (CFRunLoopRef) PyCObject_AsVoidPtr(value);
    }

    FSEventStreamScheduleWithRunLoop(fsstream, loop, kCFRunLoopDefaultMode);

    /* set stream info for callback */
    info->callback = callback;
    info->stream = fsstream;
    info->loop = loop;
    info->state = PyThreadState_Get();
    Py_INCREF(callback);

    /* start event streams */
    if (!FSEventStreamStart(fsstream)) {
        FSEventStreamInvalidate(fsstream);
        FSEventStreamRelease(fsstream);
        return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}
Example #22
0
/* read a line (return NULL on eof) */
static PyObject *
generic_readline(genericstreamobject *self, Py_ssize_t size)
{
    bufitem *linebuf, *item;
    PyObject *result;
    char *jptr;
    const char *newline;
    Py_ssize_t cursize, leftsize, readsize;

    if (size < 0)
        size = 0; /* read default chunks */

    if (!(result = generic_read(self, size))) /* maybe just EOF */
        return NULL;
    if (!(linebuf = bufitem_new())) {
        Py_DECREF(result);
        return NULL;
    }
    linebuf->next = NULL;
    linebuf->load = result;
    item = linebuf;
    readsize = 0;
    for (;;) {
        jptr = PyString_AS_STRING(item->load);
        cursize = PyString_GET_SIZE(item->load);
        if ((readsize + cursize) < readsize) {
            PyErr_SetString(PyExc_OverflowError, "Buffer became too big");
            goto error;
        }
        readsize += cursize;
        leftsize = cursize -
            ((size > 0) && (size < readsize) ? readsize - size: 0);
        if (   (leftsize > 0)
            && (newline = memchr(jptr, '\n', (size_t)leftsize))) {
            /* split at newline */
            size = readsize - cursize + ((Py_ssize_t)(newline - jptr) + 1);
            break;
        }
        else if ((size > 0) && (readsize >= size))
            /* cut it here and now */
            break;

        /* read next chunk, if any */
        result = generic_read(self, size - ((size > 0) ? readsize: 0));
        if (!result) {
            if (PyErr_Occurred())
                goto error;
            /* else */
            size = readsize;
            break;
        }
        if (!(item->next = bufitem_new())) {
            Py_DECREF(result);
            goto error;
        }
        item = item->next;
        item->next = NULL;
        item->load = result;
    }

    /* flatten the buffer chain */
    if (size == 0)
        size = readsize; /* > 0 by definition */
    if (!(result = PyString_FromStringAndSize(NULL, size)))
        goto error;
    jptr = PyString_AS_STRING(result);
    newline = jptr + (size_t)size;
    while (linebuf && (jptr < newline)) {
        cursize = PyString_GET_SIZE(linebuf->load);
        if ((jptr + cursize) > newline) { /* need to split */
            if (!(item = bufitem_new())) {
                Py_DECREF(result);
                goto error;
            }
            item->next = linebuf->next;
            item->load = PyString_FromStringAndSize(
                PyString_AS_STRING(linebuf->load) + (size_t)(newline - jptr),
                (cursize - (Py_ssize_t)(newline - jptr))
            );
            if (!item->load) {
                (void)bufitem_del(item);
                Py_DECREF(result);
                goto error;
            }
            linebuf->next = item;
            cursize = (Py_ssize_t)(newline - jptr);
        }
        (void)memcpy(jptr, PyString_AS_STRING(linebuf->load),
                     (size_t)cursize);
        jptr += cursize;
        linebuf = bufitem_del(linebuf);
    }
    /* push back unused data */
    if (linebuf) {
        item = linebuf;
        readsize = PyString_GET_SIZE(item->load);
        while (item->next) {
            item = item->next;
            readsize += PyString_GET_SIZE(item->load);
        }
        if (self->rbuf_last) {
            item->next = self->rbuf_last;
            self->rbuf_last = linebuf;
            self->rbuf_size += readsize; /* just read from it, need no check */
        }
        else {
            self->rbuf = item;
            self->rbuf_last = linebuf;
            self->rbuf_size = readsize;
        }
    }

    return result;

error:
    item = linebuf;
    while ((item = bufitem_del(item)))
        ;
    return NULL;
}
Example #23
0
//-------------------------------------------------------------------------
PyObject *py_appcall(
  ea_t func_ea,
  thid_t tid,
  PyObject *py_type,
  PyObject *py_fields,
  PyObject *arg_list)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();

  if ( !PyList_Check(arg_list) )
    return NULL;

  const char *type   = py_type == Py_None ? NULL : PyString_AS_STRING(py_type);
  const char *fields = py_fields == Py_None ? NULL : PyString_AS_STRING(py_fields);

  // Convert Python arguments into IDC values
  qvector<idc_value_t> idc_args;
  int sn = 0;
  Py_ssize_t nargs = PyList_Size(arg_list);
  idc_args.resize(nargs);
  bool ok = true;
  for ( Py_ssize_t i=0; i<nargs; i++ )
  {
    // Get argument
    borref_t py_item(PyList_GetItem(arg_list, i));
    if ( (debug & IDA_DEBUG_APPCALL) != 0 )
    {
      qstring s;
      PyW_ObjectToString(py_item.o, &s);
      msg("obj[%d]->%s\n", int(i), s.c_str());
    }
    // Convert it
    if ( pyvar_to_idcvar(py_item, &idc_args[i], &sn) < CIP_OK )
    {
      ok = false;
      break;
    }
  }

  // Set exception message
  if ( !ok )
  {
    PyErr_SetString(
        PyExc_ValueError,
        "PyAppCall: Failed to convert Python values to IDC values");
    return NULL;
  }

  error_t ret;
  idc_value_t idc_result;
  Py_BEGIN_ALLOW_THREADS;

  if ( (debug & IDA_DEBUG_APPCALL) != 0 )
  {
    msg("input variables:\n"
        "----------------\n");

    qstring s;
    for ( Py_ssize_t i=0; i<nargs; i++ )
    {
      VarPrint(&s, &idc_args[i]);
      msg("%d]\n%s\n-----------\n", int(i), s.c_str());
      s.qclear();
    }
  }

  // Do Appcall
  ret = appcall(
    func_ea,
    tid,
    (type_t *)type,
    (p_list *)fields,
    idc_args.size(),
    idc_args.begin(),
    &idc_result);

  Py_END_ALLOW_THREADS;

  if ( ret != eOk )
  {
    // An exception was thrown?
    if ( ret == eExecThrow )
    {
      // Convert the result (which is a debug_event) into a Python object
      ref_t py_appcall_exc;
      idcvar_to_pyvar(idc_result, &py_appcall_exc);
      PyErr_SetObject(PyExc_OSError, py_appcall_exc.o);
      return NULL;
    }
    // An error in the Appcall? (or an exception but AppCallOptions/DEBEV is not set)
    else
    {
      char err_str[MAXSTR];
      qstrerror(ret, err_str, sizeof(err_str));
      PyErr_SetString(PyExc_Exception, err_str);
      return NULL;
    }
  }

  if ( (debug & IDA_DEBUG_APPCALL) != 0 )
  {
    msg("return variables:\n"
        "-----------------\n");
    qstring s;
    for ( Py_ssize_t i=0; i<nargs; i++ )
    {
      VarPrint(&s, &idc_args[i]);
      msg("%d]\n%s\n-----------\n", int(i), s.c_str());
      s.qclear();
    }
  }

  // Convert IDC values back to Python values
  for ( Py_ssize_t i=0; i<nargs; i++ )
  {
    // Get argument
    borref_t py_item(PyList_GetItem(arg_list, i));
    // We convert arguments but fail only on fatal errors
    // (we ignore failure because of immutable objects)
    if ( idcvar_to_pyvar(idc_args[i], &py_item) == CIP_FAILED )
    {
      PyErr_SetString(PyExc_ValueError, "PyAppCall: Failed while converting IDC values to Python values");
      return NULL;
    }
  }
  // Convert the result from IDC back to Python
  ref_t py_result;
  if ( idcvar_to_pyvar(idc_result, &py_result) <= CIP_IMMUTABLE )
  {
    PyErr_SetString(PyExc_ValueError, "PyAppCall: Failed while converting IDC return value to Python return value");
    return NULL;
  }
  QASSERT(30413, py_result.o->ob_refcnt == 1);
  if ( (debug & IDA_DEBUG_APPCALL) != 0 )
  {
    msg("return var:\n"
        "-----------\n");
    qstring s;
    VarPrint(&s, &idc_result);
    msg("%s\n-----------\n", s.c_str());
  }
  py_result.incref();
  return py_result.o;
}
Example #24
0
static char *
convertsimple1(PyObject *arg, char **p_format, va_list *p_va)
{
	char *format = *p_format;
	char c = *format++;
	
	switch (c) {
	
	case 'b': /* unsigned byte -- very short int */
		{
			char *p = va_arg(*p_va, char *);
			long ival = PyInt_AsLong(arg);
			if (ival == -1 && PyErr_Occurred())
				return "integer<b>";
			else if (ival < 0) {
				PyErr_SetString(PyExc_OverflowError,
			      "unsigned byte integer is less than minimum");
				return "integer<b>";
			}
			else if (ival > UCHAR_MAX) {
				PyErr_SetString(PyExc_OverflowError,
			      "unsigned byte integer is greater than maximum");
				return "integer<b>";
			}
			else
				*p = (unsigned char) ival;
			break;
		}
	
	case 'B': /* byte sized bitfield - both signed and unsigned values allowed */
		{
			char *p = va_arg(*p_va, char *);
			long ival = PyInt_AsLong(arg);
			if (ival == -1 && PyErr_Occurred())
				return "integer<b>";
			else if (ival < SCHAR_MIN) {
				PyErr_SetString(PyExc_OverflowError,
			      "byte-sized integer bitfield is less than minimum");
				return "integer<B>";
			}
			else if (ival > (int)UCHAR_MAX) {
				PyErr_SetString(PyExc_OverflowError,
			      "byte-sized integer bitfield is greater than maximum");
				return "integer<B>";
			}
			else
				*p = (unsigned char) ival;
			break;
		}
	
	case 'h': /* signed short int */
		{
			short *p = va_arg(*p_va, short *);
			long ival = PyInt_AsLong(arg);
			if (ival == -1 && PyErr_Occurred())
				return "integer<h>";
			else if (ival < SHRT_MIN) {
				PyErr_SetString(PyExc_OverflowError,
			      "signed short integer is less than minimum");
				return "integer<h>";
			}
			else if (ival > SHRT_MAX) {
				PyErr_SetString(PyExc_OverflowError,
			      "signed short integer is greater than maximum");
				return "integer<h>";
			}
			else
				*p = (short) ival;
			break;
		}
	
	case 'H': /* short int sized bitfield, both signed and unsigned allowed */
		{
			unsigned short *p = va_arg(*p_va, unsigned short *);
			long ival = PyInt_AsLong(arg);
			if (ival == -1 && PyErr_Occurred())
				return "integer<H>";
			else if (ival < SHRT_MIN) {
				PyErr_SetString(PyExc_OverflowError,
			      "short integer bitfield is less than minimum");
				return "integer<H>";
			}
			else if (ival > USHRT_MAX) {
				PyErr_SetString(PyExc_OverflowError,
			      "short integer bitfield is greater than maximum");
				return "integer<H>";
			}
			else
				*p = (unsigned short) ival;
			break;
		}
	
	case 'i': /* signed int */
		{
			int *p = va_arg(*p_va, int *);
			long ival = PyInt_AsLong(arg);
			if (ival == -1 && PyErr_Occurred())
				return "integer<i>";
			else if (ival > INT_MAX) {
				PyErr_SetString(PyExc_OverflowError,
				    "signed integer is greater than maximum");
				return "integer<i>";
			}
			else if (ival < INT_MIN) {
				PyErr_SetString(PyExc_OverflowError,
				    "signed integer is less than minimum");
				return "integer<i>";
			}
			else
				*p = ival;
			break;
		}
	case 'l': /* long int */
		{
			long *p = va_arg(*p_va, long *);
			long ival = PyInt_AsLong(arg);
			if (ival == -1 && PyErr_Occurred())
				return "integer<l>";
			else
				*p = ival;
			break;
		}
	
#ifdef HAVE_LONG_LONG
	case 'L': /* LONG_LONG */
		{
			LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
			LONG_LONG ival = PyLong_AsLongLong( arg );
			if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
				return "long<L>";
			} else {
				*p = ival;
			}
			break;
		}
#endif
	
	case 'f': /* float */
		{
			float *p = va_arg(*p_va, float *);
			double dval = PyFloat_AsDouble(arg);
			if (PyErr_Occurred())
				return "float<f>";
			else
				*p = (float) dval;
			break;
		}
	
	case 'd': /* double */
		{
			double *p = va_arg(*p_va, double *);
			double dval = PyFloat_AsDouble(arg);
			if (PyErr_Occurred())
				return "float<d>";
			else
				*p = dval;
			break;
		}
	
#ifndef WITHOUT_COMPLEX
	case 'D': /* complex double */
		{
			Py_complex *p = va_arg(*p_va, Py_complex *);
			Py_complex cval;
			cval = PyComplex_AsCComplex(arg);
			if (PyErr_Occurred())
				return "complex<D>";
			else
				*p = cval;
			break;
		}
#endif /* WITHOUT_COMPLEX */
	
	case 'c': /* char */
		{
			char *p = va_arg(*p_va, char *);
			if (PyString_Check(arg) && PyString_Size(arg) == 1)
				*p = PyString_AsString(arg)[0];
			else
				return "char";
			break;
		}
	
	case 's': /* string */
		{
			if (*format == '#') {
				void **p = (void **)va_arg(*p_va, char **);
				int *q = va_arg(*p_va, int *);

				if (PyString_Check(arg)) {
				    *p = PyString_AS_STRING(arg);
				    *q = PyString_GET_SIZE(arg);
				}
				else if (PyUnicode_Check(arg)) {
				    arg = _PyUnicode_AsDefaultEncodedString(
							            arg, NULL);
				    if (arg == NULL)
					return "unicode conversion error";
				    *p = PyString_AS_STRING(arg);
				    *q = PyString_GET_SIZE(arg);
				}
				else { /* any buffer-like object */
				    PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
				    int count;
				    if ( pb == NULL ||
					 pb->bf_getreadbuffer == NULL ||
					 pb->bf_getsegcount == NULL )
					return "read-only buffer";
				    if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
					return "single-segment read-only buffer";
				    if ( (count =
					  (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
					return "(unspecified)";
				    *q = count;
				}
				format++;
			} else {
				char **p = va_arg(*p_va, char **);
			
				if (PyString_Check(arg))
				    *p = PyString_AS_STRING(arg);
				else if (PyUnicode_Check(arg)) {
				    arg = _PyUnicode_AsDefaultEncodedString(
							            arg, NULL);
				    if (arg == NULL)
					return "unicode conversion error";
				    *p = PyString_AS_STRING(arg);
				}
				else
				  return "string";
				if ((int)strlen(*p) != PyString_Size(arg))
				  return "string without null bytes";
			}
			break;
		}
Example #25
0
static PyObject *searchio_createIndex(PyObject *self, PyObject *args)
{
    /* grab the filename, number of documents, and our index */
    const char *filename = NULL;
    uint32_t numDocuments = 0;
    PyObject *index = NULL;
    
    if (!PyArg_ParseTuple(args, "sIO", &filename, &numDocuments, &index))
        return NULL;
    
    /* open the index file */
    int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC);
    if (fd == -1)
        return PyErr_SetFromErrno(PyExc_IOError);
    
    fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    size_t totalWritten = 0;
    
    /* get the number of terms in the index */
    Py_ssize_t numTermsPy = PyDict_Size(index);
    uint32_t numTerms = 0;
    if (numTermsPy > 0 && numTermsPy < UINT32_MAX)
        numTerms = (uint32_t)numTermsPy;
    else
    {
        PyErr_SetString(PyExc_MemoryError, "the number of terms is greater than UINT32_MAX (or less than 0)");
        return NULL;
    }
    
    /* write a document header */
    searchio_index_header_t header = {htonl(numDocuments), htonl(numTerms), 0};
    totalWritten += write(fd, (void *)&header, sizeof(header));
    
    /* loop once over index to write term entries */
    PyObject *indexKeys = PyDict_Keys(index);
    Py_ssize_t indexKeyLen = PyList_Size(indexKeys);
    
    uint32_t postingsOffset = 0;
    uint32_t keyIdx;
    for (keyIdx = 0; keyIdx < indexKeyLen; keyIdx++)
    {
        PyObject *key = PyList_GetItem(indexKeys, keyIdx);
        PyObject *value = PyDict_GetItem(index, key);
        
        /* create a term entry */
        searchio_index_term_t term;
        term.postingsOffset = htonl(postingsOffset);
        
        /* pull out the term, its df, and a reference to the postings list */
        const char *termStr = PyString_AS_STRING(key);
        PyObject *pydf = PyList_GetItem(value, 0);
        PyObject *postings = PyList_GetItem(value, 1);
        Py_ssize_t postingsLen = PyList_Size(postings);
        
        /* fill in the header */
        size_t termLen = strlen(termStr);
        term.df = htonl(PyInt_AsUnsignedLongMask(pydf));
        term.numDocumentsInPostings = htonl((uint32_t)postingsLen);
        term.termLength = htons((uint16_t)termLen);
        
        /* write out the header, and its term */
        totalWritten += write(fd, (void *)&term, sizeof(term));
        totalWritten += write(fd, termStr, termLen);
        
        /* determine the on-disk size of the posting list */
        uint32_t totalSize = 0;
        Py_ssize_t i;
        for (i = 0; i < postingsLen; i++)
        {
            PyObject *entryList = PyList_GetItem(postings, i);
            PyObject *positions = PyList_GetItem(entryList, 2);
            Py_ssize_t positionLen = PyList_Size(positions);
            
            /* on-disk size is size of a posting struct, plus sizeof(uint32_t) * positions */
            totalSize += (uint32_t)sizeof(searchio_index_posting_t) + (uint32_t)(positionLen * sizeof(uint32_t));
        }
        
        /* increment the postings offset */
        postingsOffset += totalSize;
    }
    
    /* update the postings offset */
    header.postingsStart = htonl(totalWritten);
    
    /* loop again to write postings lists */
    for (keyIdx = 0; keyIdx < indexKeyLen; keyIdx++)
    {
        PyObject *key = PyList_GetItem(indexKeys, keyIdx);
        PyObject *value = PyDict_GetItem(index, key);
        
        /* get the postings list and its length */
        PyObject *postings = PyList_GetItem(value, 1);
        Py_ssize_t postingsLen = PyList_Size(postings);
        
        /* loop over each entry in the postings list and write it out */
        Py_ssize_t i;
        for (i = 0; i < postingsLen; i++)
        {
            /* pull out some values */
            PyObject *entryList = PyList_GetItem(postings, i);
            PyObject *pypageID = PyList_GetItem(entryList, 0);
            PyObject *pywf = PyList_GetItem(entryList, 1);
            PyObject *positions = PyList_GetItem(entryList, 2);
            Py_ssize_t positionsLen = PyList_Size(positions);
            
            /* create a header */
            searchio_index_posting_t posting;
            posting.pageID = htonl((uint32_t)PyInt_AsUnsignedLongMask(pypageID));
            posting.wf = htonl((uint32_t)(PyFloat_AsDouble(pywf) * SEARCHIO_WF_SCALE));
            posting.numPositions = htonl((uint32_t)positionsLen);
            
            /* write the header */
            write(fd, (void *)&posting, sizeof(posting));
            
            /* write each of the positions */
            Py_ssize_t j;
            for (j = 0; j < positionsLen; j++)
            {
                PyObject *item = PyList_GetItem(positions, j);
                uint32_t p = htonl((uint32_t)PyInt_AsUnsignedLongMask(item));
                write(fd, (void *)&p, sizeof(p));
            }
        }
    }
    
    /* rewrite the header with correct offsets */
    lseek(fd, 0, SEEK_SET);
    write(fd, (void *)&header, sizeof(header));
    
    /* close the index */
    close(fd);
    
    /* no meaningful return value here */
    Py_RETURN_NONE;
}
Example #26
0
PyObject* JSONToObj(PyObject* self, PyObject *args, PyObject *kwargs)
{
  PyObject *ret;
  PyObject *sarg;
  PyObject *arg;
  PyObject *opreciseFloat = NULL;
  JSONObjectDecoder *decoder;
  PyObjectDecoder pyDecoder;
  PyArray_Descr *dtype = NULL;
  int numpy = 0, labelled = 0;

  JSONObjectDecoder dec =
  {
    Object_newString,
    Object_objectAddKey,
    Object_arrayAddItem,
    Object_newTrue,
    Object_newFalse,
    Object_newNull,
    Object_newObject,
    Object_endObject,
    Object_newArray,
    Object_endArray,
    Object_newInteger,
    Object_newLong,
    Object_newDouble,
    Object_releaseObject,
    PyObject_Malloc,
    PyObject_Free,
    PyObject_Realloc
  };

  dec.preciseFloat = 0;
  dec.prv = NULL;

  pyDecoder.dec = dec;
  pyDecoder.curdim = 0;
  pyDecoder.npyarr = NULL;
  pyDecoder.npyarr_addr = NULL;

  decoder = (JSONObjectDecoder*) &pyDecoder;

  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OiiO&", g_kwlist, &arg, &opreciseFloat, &numpy, &labelled, PyArray_DescrConverter2, &dtype))
  {
      Npy_releaseContext(pyDecoder.npyarr);
      return NULL;
  }

  if (opreciseFloat && PyObject_IsTrue(opreciseFloat))
  {
      decoder->preciseFloat = 1;
  }

  if (PyString_Check(arg))
  {
      sarg = arg;
  }
  else
  if (PyUnicode_Check(arg))
  {
    sarg = PyUnicode_AsUTF8String(arg);
    if (sarg == NULL)
    {
      //Exception raised above us by codec according to docs
      return NULL;
    }
  }
  else
  {
    PyErr_Format(PyExc_TypeError, "Expected String or Unicode");
    return NULL;
  }

  decoder->errorStr = NULL;
  decoder->errorOffset = NULL;

  if (numpy)
  {
    pyDecoder.dtype = dtype;
    decoder->newArray = Object_npyNewArray;
    decoder->endArray = Object_npyEndArray;
    decoder->arrayAddItem = Object_npyArrayAddItem;

    if (labelled)
    {
      decoder->newObject = Object_npyNewObject;
      decoder->endObject = Object_npyEndObject;
      decoder->objectAddKey = Object_npyObjectAddKey;
    }
  }

  ret = JSON_DecodeObject(decoder, PyString_AS_STRING(sarg), PyString_GET_SIZE(sarg));

  if (sarg != arg)
  {
    Py_DECREF(sarg);
  }

  if (PyErr_Occurred())
  {    
    if (ret)
    {
        Py_DECREF( (PyObject *) ret);
    }
    Npy_releaseContext(pyDecoder.npyarr);   
    return NULL;
  }

  if (decoder->errorStr)
  {
    /*
    FIXME: It's possible to give a much nicer error message here with actual failing element in input etc*/

    PyErr_Format (PyExc_ValueError, "%s", decoder->errorStr);

    if (ret)
    {
        Py_DECREF( (PyObject *) ret);
    }
    Npy_releaseContext(pyDecoder.npyarr);

    return NULL;
  }

  return ret;
}
Example #27
0
static PyObject * req_read(requestobject *self, PyObject *args)
{

    int rc, bytes_read, chunk_len;
    char *buffer;
    PyObject *result;
    int copied = 0;
    int len = -1;

    if (! PyArg_ParseTuple(args, "|i", &len)) 
	return NULL;

    if (len == 0) {
	return PyString_FromString("");
    }

    /* is this the first read? */
    if (! self->request_rec->read_length) {

	/* then do some initial setting up */

	rc = ap_setup_client_block(self->request_rec, REQUEST_CHUNKED_ERROR);
	if(rc != OK) {
	    PyObject *val = PyInt_FromLong(rc);
	    if (val == NULL)
		return NULL;
	    PyErr_SetObject(Mp_ServerReturn, val);
	    Py_DECREF(val);
	    return NULL;
	}

	if (! ap_should_client_block(self->request_rec)) {
	    /* client has nothing to send */
	    return PyString_FromString("");
	}
    }

    if (len < 0)
	len = self->request_rec->remaining +
	    (self->rbuff_len - self->rbuff_pos);

    result = PyString_FromStringAndSize(NULL, len);

    /* possibly no more memory */
    if (result == NULL) 
	return NULL;

    buffer = PyString_AS_STRING((PyStringObject *) result);

    /* if anything left in the readline buffer */
    while ((self->rbuff_pos < self->rbuff_len) && (copied < len))
	buffer[copied++] = self->rbuff[self->rbuff_pos++];
    
    if (copied == len)
	return result; 	/* we're done! */

    /* set timeout */
    ap_soft_timeout("mod_python_read", self->request_rec);

    /* read it in */
    Py_BEGIN_ALLOW_THREADS
    chunk_len = ap_get_client_block(self->request_rec, buffer, len);
    Py_END_ALLOW_THREADS
    bytes_read = chunk_len;

    /* if this is a "short read", try reading more */
    while ((bytes_read < len) && (chunk_len != 0)) {
	Py_BEGIN_ALLOW_THREADS
	chunk_len = ap_get_client_block(self->request_rec, 
					buffer+bytes_read, len-bytes_read);
	Py_END_ALLOW_THREADS
	ap_reset_timeout(self->request_rec);
	if (chunk_len == -1) {
	    ap_kill_timeout(self->request_rec);
	    PyErr_SetObject(PyExc_IOError, 
			    PyString_FromString("Client read error (Timeout?)"));
	    return NULL;
	}
	else
	    bytes_read += chunk_len;
    }

    ap_kill_timeout(self->request_rec);

    /* resize if necessary */
    if (bytes_read < len) 
	if(_PyString_Resize(&result, bytes_read))
	    return NULL;

    return result;
}
Example #28
0
static PyObject *
complex_subtype_from_string(PyTypeObject *type, PyObject *v)
{
	const char *s, *start;
	char *end;
	double x=0.0, y=0.0, z;
	int got_re=0, got_im=0, done=0;
	int digit_or_dot;
	int sw_error=0;
	int sign;
	char buffer[256]; /* For errors */
#ifdef Py_USING_UNICODE
	char s_buffer[256];
#endif
	Py_ssize_t len;

	if (PyString_Check(v)) {
		s = PyString_AS_STRING(v);
		len = PyString_GET_SIZE(v);
	}
#ifdef Py_USING_UNICODE
	else if (PyUnicode_Check(v)) {
		if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
			PyErr_SetString(PyExc_ValueError,
				 "complex() literal too large to convert");
			return NULL;
		}
		if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
					    PyUnicode_GET_SIZE(v),
					    s_buffer,
					    NULL))
			return NULL;
		s = s_buffer;
		len = strlen(s);
	}
#endif
	else if (PyObject_AsCharBuffer(v, &s, &len)) {
		PyErr_SetString(PyExc_TypeError,
				"complex() arg is not a string");
		return NULL;
	}

	/* position on first nonblank */
	start = s;
	while (*s && isspace(Py_CHARMASK(*s)))
		s++;
	if (s[0] == '\0') {
		PyErr_SetString(PyExc_ValueError,
				"complex() arg is an empty string");
		return NULL;
	}

	z = -1.0;
	sign = 1;
	do {

		switch (*s) {

		case '\0':
			if (s-start != len) {
				PyErr_SetString(
					PyExc_ValueError,
					"complex() arg contains a null byte");
				return NULL;
			}
			if(!done) sw_error=1;
			break;

		case '-':
			sign = -1;
				/* Fallthrough */
		case '+':
			if (done)  sw_error=1;
			s++;
			if  (  *s=='\0'||*s=='+'||*s=='-'  ||
			       isspace(Py_CHARMASK(*s))  )  sw_error=1;
			break;

		case 'J':
		case 'j':
			if (got_im || done) {
				sw_error = 1;
				break;
			}
			if  (z<0.0) {
				y=sign;
			}
			else{
				y=sign*z;
			}
			got_im=1;
			s++;
			if  (*s!='+' && *s!='-' )
				done=1;
			break;

		default:
			if (isspace(Py_CHARMASK(*s))) {
				while (*s && isspace(Py_CHARMASK(*s)))
					s++;
				if (s[0] != '\0')
					sw_error=1;
				else
					done = 1;
				break;
			}
			digit_or_dot =
				(*s=='.' || isdigit(Py_CHARMASK(*s)));
			if  (done||!digit_or_dot) {
				sw_error=1;
				break;
			}
			errno = 0;
			PyFPE_START_PROTECT("strtod", return 0)
				z = PyOS_ascii_strtod(s, &end) ;
			PyFPE_END_PROTECT(z)
				if (errno != 0) {
					PyOS_snprintf(buffer, sizeof(buffer),
					  "float() out of range: %.150s", s);
					PyErr_SetString(
						PyExc_ValueError,
						buffer);
					return NULL;
				}
			s=end;
			if  (*s=='J' || *s=='j') {

				break;
			}
			if  (got_re) {
				sw_error=1;
				break;
			}

				/* accept a real part */
			x=sign*z;
			got_re=1;
			if  (got_im)  done=1;
			z = -1.0;
			sign = 1;
			break;

		}  /* end of switch  */

	} while (s - start < len && !sw_error);

	if (sw_error) {
		PyErr_SetString(PyExc_ValueError,
				"complex() arg is a malformed string");
		return NULL;
	}

	return complex_subtype_from_doubles(type, x, y);
}
static int
tok_stdin_decode(struct tok_state *tok, char **inp)
{
    PyObject *enc, *sysstdin, *decoded, *utf8;
    const char *encoding;
    char *converted;

    if (PySys_GetFile((char *)"stdin", NULL) != stdin)
        return 0;
    sysstdin = PySys_GetObject("stdin");
    if (sysstdin == NULL || !PyFile_Check(sysstdin))
        return 0;

    enc = ((PyFileObject *)sysstdin)->f_encoding;
    if (enc == NULL || !PyString_Check(enc))
        return 0;
    Py_INCREF(enc);

    encoding = PyString_AsString(enc);
    decoded = PyUnicode_Decode(*inp, strlen(*inp), encoding, NULL);
    if (decoded == NULL)
        goto error_clear;

    utf8 = PyUnicode_AsEncodedString(decoded, "utf-8", NULL);
    Py_DECREF(decoded);
    if (utf8 == NULL)
        goto error_clear;

    assert(PyString_Check(utf8));
    converted = new_string(PyString_AS_STRING(utf8),
                           PyString_GET_SIZE(utf8));
    Py_DECREF(utf8);
    if (converted == NULL)
        goto error_nomem;

    PyMem_FREE(*inp);
    *inp = converted;
    if (tok->encoding != NULL)
        PyMem_FREE(tok->encoding);
    tok->encoding = new_string(encoding, strlen(encoding));
    if (tok->encoding == NULL)
        goto error_nomem;

    Py_DECREF(enc);
    return 0;

error_nomem:
    Py_DECREF(enc);
    tok->done = E_NOMEM;
    return -1;

error_clear:
    Py_DECREF(enc);
    if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
        tok->done = E_ERROR;
        return -1;
    }
    /* Fallback to iso-8859-1: for backward compatibility */
    PyErr_Clear();
    return 0;
}
Example #30
0
static int
symtable_visit_stmt(struct symtable *st, stmt_ty s)
{
	switch (s->kind) {
        case FunctionDef_kind:
		if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
			return 0;
		if (s->v.FunctionDef.args->defaults)
			VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
		if (s->v.FunctionDef.decorator_list)
			VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
		if (!symtable_enter_block(st, s->v.FunctionDef.name, 
					  FunctionBlock, (void *)s, s->lineno))
			return 0;
		VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
		VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
		if (!symtable_exit_block(st, s))
			return 0;
		break;
        case ClassDef_kind: {
		PyObject *tmp;
		if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
			return 0;
		VISIT_SEQ(st, expr, s->v.ClassDef.bases);
		if (s->v.ClassDef.decorator_list)
			VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
		if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, 
					  (void *)s, s->lineno))
			return 0;
		tmp = st->st_private;
		st->st_private = s->v.ClassDef.name;
		VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
		st->st_private = tmp;
		if (!symtable_exit_block(st, s))
			return 0;
		break;
	}
        case Return_kind:
		if (s->v.Return.value) {
			VISIT(st, expr, s->v.Return.value);
			st->st_cur->ste_returns_value = 1;
			if (st->st_cur->ste_generator) {
				PyErr_SetString(PyExc_SyntaxError,
					RETURN_VAL_IN_GENERATOR);
			        PyErr_SyntaxLocation(st->st_filename,
				             s->lineno);
				return 0;
			}
		}
		break;
        case Delete_kind:
		VISIT_SEQ(st, expr, s->v.Delete.targets);
		break;
        case Assign_kind:
		VISIT_SEQ(st, expr, s->v.Assign.targets);
		VISIT(st, expr, s->v.Assign.value);
		break;
        case AugAssign_kind:
		VISIT(st, expr, s->v.AugAssign.target);
		VISIT(st, expr, s->v.AugAssign.value);
		break;
        case Print_kind:
		if (s->v.Print.dest)
			VISIT(st, expr, s->v.Print.dest);
		VISIT_SEQ(st, expr, s->v.Print.values);
		break;
        case For_kind:
		VISIT(st, expr, s->v.For.target);
		VISIT(st, expr, s->v.For.iter);
		VISIT_SEQ(st, stmt, s->v.For.body);
		if (s->v.For.orelse)
			VISIT_SEQ(st, stmt, s->v.For.orelse);
		break;
        case While_kind:
		VISIT(st, expr, s->v.While.test);
		VISIT_SEQ(st, stmt, s->v.While.body);
		if (s->v.While.orelse)
			VISIT_SEQ(st, stmt, s->v.While.orelse);
		break;
        case If_kind:
		/* XXX if 0: and lookup_yield() hacks */
		VISIT(st, expr, s->v.If.test);
		VISIT_SEQ(st, stmt, s->v.If.body);
		if (s->v.If.orelse)
			VISIT_SEQ(st, stmt, s->v.If.orelse);
		break;
        case Raise_kind:
		if (s->v.Raise.type) {
			VISIT(st, expr, s->v.Raise.type);
			if (s->v.Raise.inst) {
				VISIT(st, expr, s->v.Raise.inst);
				if (s->v.Raise.tback)
					VISIT(st, expr, s->v.Raise.tback);
			}
		}
		break;
        case TryExcept_kind:
		st->st_cur->ste_blockstack = 1;
		VISIT_SEQ(st, stmt, s->v.TryExcept.body);
		VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
		VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
		break;
        case TryFinally_kind:
		st->st_cur->ste_blockstack = 1;
		VISIT_SEQ(st, stmt, s->v.TryFinally.body);
		VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
		break;
        case Assert_kind:
		VISIT(st, expr, s->v.Assert.test);
		if (s->v.Assert.msg)
			VISIT(st, expr, s->v.Assert.msg);
		break;
        case Import_kind:
		VISIT_SEQ(st, alias, s->v.Import.names);
		/* XXX Don't have the lineno available inside
		   visit_alias */
		if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
			st->st_cur->ste_opt_lineno = s->lineno;
		break;
        case ImportFrom_kind:
		VISIT_SEQ(st, alias, s->v.ImportFrom.names);
		/* XXX Don't have the lineno available inside
		   visit_alias */
		if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
			st->st_cur->ste_opt_lineno = s->lineno;
		break;
        case Exec_kind:
		VISIT(st, expr, s->v.Exec.body);
		if (!st->st_cur->ste_opt_lineno)
			st->st_cur->ste_opt_lineno = s->lineno;
		if (s->v.Exec.globals) {
			st->st_cur->ste_unoptimized |= OPT_EXEC;
			VISIT(st, expr, s->v.Exec.globals);
			if (s->v.Exec.locals)
				VISIT(st, expr, s->v.Exec.locals);
		} else {
			st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
		}
		break;
        case Global_kind: {
		int i;
		asdl_seq *seq = s->v.Global.names;
		for (i = 0; i < asdl_seq_LEN(seq); i++) {
			identifier name = (identifier)asdl_seq_GET(seq, i);
			char *c_name = PyString_AS_STRING(name);
			long cur = symtable_lookup(st, name);
			if (cur < 0)
				return 0;
			if (cur & (DEF_LOCAL | USE)) {
				char buf[256];
				if (cur & DEF_LOCAL) 
					PyOS_snprintf(buf, sizeof(buf),
						      GLOBAL_AFTER_ASSIGN,
						      c_name);
				else
					PyOS_snprintf(buf, sizeof(buf),
						      GLOBAL_AFTER_USE,
						      c_name);
				if (!symtable_warn(st, buf, s->lineno))
                                    return 0;
			}
			if (!symtable_add_def(st, name, DEF_GLOBAL))
				return 0;
		}
		break;
	}
        case Expr_kind:
		VISIT(st, expr, s->v.Expr.value);
		break;
        case Pass_kind:
        case Break_kind:
        case Continue_kind:
		/* nothing to do here */
		break;
        case With_kind:
		st->st_cur->ste_blockstack = 1;
		if (!symtable_new_tmpname(st))
			return 0;
                VISIT(st, expr, s->v.With.context_expr);
                if (s->v.With.optional_vars) {
			if (!symtable_new_tmpname(st))
				return 0;
                        VISIT(st, expr, s->v.With.optional_vars);
                }
                VISIT_SEQ(st, stmt, s->v.With.body);
                break;
	}
	return 1;
}