Exemplo n.º 1
0
//=============================================================================
// METHOD: SPELLvariableMonitor::registerVariable()
//=============================================================================
bool SPELLvariableMonitor::registerVariable( SPELLvarInfo& var )
{
	SPELLsafePythonOperations ops("SPELLvariableMonitor::registerVariable()");

	if ((m_frame->f_globals)&&(var.isGlobal))
	{
		var.varValue = PYSSTR( PyDict_GetItemString( m_frame->f_globals, var.varName.c_str() ));
		if (var.varValue == "") var.varValue = EMPTY_STRING;
		var.varType = PYSSTR( PyObject_Type( PyDict_GetItemString( m_frame->f_globals, var.varName.c_str() )) );
		var.isGlobal = true;
		var.isRegistered = true;
		m_variables.insert( std::make_pair( var.varName, var ) );
		DEBUG("[VM] Registered global variable: " + var.varName + ", current value: " + var.varValue );
		return true;
	}
	else if ((m_frame->f_locals)&&(!var.isGlobal))
	{
		// We need to retrieve the function arguments and other locals, which are only stored in
		// fast locals by default
		PyFrame_FastToLocals(m_frame);
		var.varValue = PYSSTR( PyDict_GetItemString( m_frame->f_locals, var.varName.c_str() ));
		if (var.varValue == "") var.varValue = EMPTY_STRING;
		var.varType = PYSSTR( PyObject_Type( PyDict_GetItemString( m_frame->f_locals, var.varName.c_str() )) );
		var.isGlobal = true;
		var.isRegistered = true;
		m_variables.insert( std::make_pair( var.varName, var ) );
		DEBUG("[VM] Registered local variable: " + var.varName + ", current value: " + var.varValue );
		return true;
	}
	var.varValue = "???";
	var.varType = "???";
	var.isRegistered = false;
	DEBUG("[VM] No such variable: " + var.varName);
	return false;
}
Exemplo n.º 2
0
static PyObject *Polygon_new(PyObject *self, PyObject *args) {
    PyObject *O = NULL, *TMP = NULL;
    int hole = 0;
    Polygon *p = Polygon_NEW(NULL);
    if (! PyArg_ParseTuple(args, "|Oi", &O, &hole))
        Polygon_Raise(ERR_ARG);
    if (O != NULL) {
        if ((PyTypeObject *)PyObject_Type(O) == &Polygon_Type) {
            if (poly_p_clone(((Polygon *)O)->p, p->p) != 0) {
                Polygon_dealloc(p);
                return Polygon_Raise(ERR_MEM);
            }
        } else if (PyString_Check(O)) {
            TMP = Polygon_read(p, args);
        } else if (PySequence_Check(O)) {
            TMP = Polygon_addContour(p, args);
        } else if (PyFile_Check(O)) {
            TMP = Polygon_read(p, args);
        } else return Polygon_Raise(ERR_ARG);
        if (TMP) Py_DECREF(TMP);
        if (PyErr_Occurred()) {
            Polygon_dealloc(p);
            return NULL;
        }
    }
    return (PyObject *)p;
}
Exemplo n.º 3
0
static int is_iplimage(PyObject *o)
{
  PyObject* to = PyObject_Type(o);
  const char* tp_name = ((PyTypeObject*) to)->tp_name;
  //printf("is_iplimage: %s, %d\n", tp_name, strcmp(tp_name, "cv.iplimage") == 0);
  return strcmp(tp_name, "cv.iplimage") >= 0;
}
InstanceVarItem::ObjectMap InstanceVarItem::setObject(_object *obj)
{
    ObjectMap children;
    if(PyObject_HasAttrString(obj, "__dict__"))
    {
        PyObject* dict = PyObject_GetAttrString(obj, "__dict__");
        if(PyDict_Check(dict))
        {
            children = DictVarItem::setObject(dict);
        }
        Py_XDECREF(dict);
    }

    PyObject* type = PyObject_Type(obj);
    PyObject* nameObj = PyObject_GetAttrString(type, "__name__");
    QString name = PythonTypeConverter::toString(nameObj);
    PyObject* modObj = PyObject_GetAttrString(type, "__module__");
    QString module = PythonTypeConverter::toString(modObj);
    if(!module.startsWith("__") && module != "builtins")
        name = module + "." + name;
    mType = name;
    Py_XDECREF(modObj);
    Py_XDECREF(nameObj);
    Py_XDECREF(type);

    PyObject* strObj = PyObject_Str(obj);
    mValue = PythonTypeConverter::toString(strObj);
    Py_XDECREF(strObj);


    return children;
}
Exemplo n.º 5
0
PyObject *
_escape_item(
	PyObject *item,
	PyObject *d)
{
	PyObject *quoted=NULL, *itemtype, *itemconv;
	if (!(itemtype = PyObject_Type(item)))
		goto error;
	itemconv = PyObject_GetItem(d, itemtype);
	Py_DECREF(itemtype);
	if (!itemconv) {
		PyErr_Clear();
		itemconv = PyObject_GetItem(d,
				 (PyObject *) &PyString_Type);
	}
	if (!itemconv) {
		PyErr_SetString(PyExc_TypeError,
				"no default type converter defined");
		goto error;
	}
	quoted = PyObject_CallFunction(itemconv, "OO", item, d);
	Py_DECREF(itemconv);
error:
	return quoted;
}
Exemplo n.º 6
0
int PyPreprocessor_init( PyPreprocessor * self, PyObject * args, PyObject * kwds )
{
    static char * kwlist[] = { "cache", NULL };
    PyObject * pCache = 0;

    if ( !PyArg_ParseTupleAndKeywords( args, kwds, "O", kwlist, &pCache ) )
    {
        PyErr_SetString( PyExc_Exception, "Invalid cache parameter." );
        return -1;
    }

    if ( !pCache || ( pCache == Py_None ) )
    {
        self->pp = new Preprocessor( 0 );
        return 0;
    }

    if ( (PyTypeObject *)PyObject_Type( pCache ) != &PyCacheType )
    {
        PyErr_SetString( PyExc_Exception, "Invalid cache parameter." );
        return -1;
    }

    PyCache const * pyCache( reinterpret_cast<PyCache *>( pCache ) );
    assert( pyCache->cache );

    self->cache = pCache;
    Py_XINCREF( self->cache );
    self->pp = new Preprocessor( pyCache->cache );
    return 0;
}
Exemplo n.º 7
0
static int Polygon_init(Polygon *self, PyObject *args, PyObject *kwds) {
    PyObject *O = NULL, *TMP = NULL;
    int hole;
    static char *kwlist[] = {"contour", "hole", NULL};
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|Oi", kwlist, &O, &hole))
        return -1; 
    if (O != NULL) {
        if ((PyTypeObject *)PyObject_Type(O) == &Polygon_Type) {
            if (poly_p_clone(((Polygon *)O)->gpc_p, self->gpc_p) != 0) {
                Polygon_dealloc(self);
                Polygon_Raise(ERR_MEM);
                return -1;
            }
        } else if (PyString_Check(O)) {
            TMP = Polygon_read(self, args);
        } else if (PySequence_Check(O)) {
            TMP = Polygon_addContour(self, args);
        } else if (PyFile_Check(O)) {
            TMP = Polygon_read(self, args);
        } else {
            Polygon_Raise(ERR_ARG);
            return -1;
        }
        if (TMP) {
            Py_DECREF(TMP);
        }
    }
    return 0;
}
Exemplo n.º 8
0
CString GetType(PyObject* pObj)
{
	PyTypeObject* pType = (PyTypeObject*)PyObject_Type(pObj);
	int nLen = 255;
	char buf[255];
	buf[0] = 0;
	strcpy_s(buf,pType->tp_name);
	return CString(CA2T(buf));
}
static PyObject *Consumer_get_watermark_offsets (Handle *self, PyObject *args,
                                                 PyObject *kwargs) {

        TopicPartition *tp;
        rd_kafka_resp_err_t err;
        double tmout = -1.0f;
        int cached = 0;
        int64_t low = RD_KAFKA_OFFSET_INVALID, high = RD_KAFKA_OFFSET_INVALID;
        static char *kws[] = { "partition", "timeout", "cached", NULL };
        PyObject *rtup;

        if (!self->rk) {
                PyErr_SetString(PyExc_RuntimeError,
                                "Consumer closed");
                return NULL;
        }

        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|db", kws,
                                         (PyObject **)&tp, &tmout, &cached))
                return NULL;


        if (PyObject_Type((PyObject *)tp) != (PyObject *)&TopicPartitionType) {
                PyErr_Format(PyExc_TypeError,
                             "expected %s", TopicPartitionType.tp_name);
                return NULL;
        }

        if (cached) {
                err = rd_kafka_get_watermark_offsets(self->rk,
                                                     tp->topic, tp->partition,
                                                     &low, &high);
        } else {
                Py_BEGIN_ALLOW_THREADS;
                err = rd_kafka_query_watermark_offsets(self->rk,
                                                       tp->topic, tp->partition,
                                                       &low, &high,
                                                       tmout >= 0 ? (int)(tmout * 1000.0f) : -1);
                Py_END_ALLOW_THREADS;
        }

        if (err) {
                cfl_PyErr_Format(err,
                                 "Failed to get watermark offsets: %s",
                                 rd_kafka_err2str(err));
                return NULL;
        }

        rtup = PyTuple_New(2);
        PyTuple_SetItem(rtup, 0, PyLong_FromLongLong(low));
        PyTuple_SetItem(rtup, 1, PyLong_FromLongLong(high));

        return rtup;
}
Exemplo n.º 10
0
inline
CType
CObject::GetType(void) const
{
    // ???
    PyObject* obj = PyObject_Type (Get());
    if ( !obj ) {
        throw CTypeError("Type does not exist");
    }
    return CType(obj, eTakeOwnership);
}
Exemplo n.º 11
0
//=============================================================================
// METHOD: SPELLvariableMonitor::retrieveLocalVariables()
//=============================================================================
void SPELLvariableMonitor::retrieveLocalVariables(std::vector<SPELLvarInfo>& vars)
{
	DEBUG("[VM] Retrieve Locals");

	/*
	 * Bottom stack frame is discarded,
	 * as globals and locals are the same dictionary
	 */
	if (m_frame->f_back == NULL) return;

	/*
	 * Get the names defined in the current code, including arguments
	 */
	std::vector<std::string> varNames = retrieveNames();

	/*
	 * Iterate over the locals dictionary, retrieving the names contained in
	 * varNames
	 */
	PyFrame_FastToLocals(m_frame);
	PyObject* dict = m_frame->f_locals;
	DEBUG("[VM] Frame: " + PYCREPR(m_frame));
	for( unsigned int index = 0; index< varNames.size(); index++)
	{
		std::string varName = varNames[index];
		PyObject* pyVarName = SSTRPY(varName);
		if (PyDict_Contains( dict, pyVarName ))
		{
			PyObject* object = PyDict_GetItem( dict, pyVarName );

			if (!SPELLpythonHelper::instance().isInstance(object, "Database", "spell.lib.adapter.databases.database"))
			{
				if (PyCallable_Check(object)) continue;
				if (PyClass_Check(object)) continue;
				if (PyModule_Check(object)) continue;
				if (PyInstance_Check(object)) continue;
			}
			DEBUG("[VM] Processing " + varName);
			std::string type = PYSSTR( PyObject_Type(object) );
			DEBUG("[VM] Type      : " + type);
			std::string value = PYREPR( object );
			DEBUG("[VM] Value     : " + value);
			DEBUG("[VM] Global    : " + BSTR(false));
			DEBUG("[VM] Registered: " + BSTR(isRegistered(varName)));

			// Mark empty values (empty strings) as "<empty>"
			if (value == "") value = EMPTY_STRING;

			vars.push_back( SPELLvarInfo(varName, type, value, false, isRegistered(varName)) );
		}
	}
	PyFrame_LocalsToFast(m_frame,0);
}
Exemplo n.º 12
0
 /// Check if the passed python object class matches
 static void* check_type(PyObject* py_object) {
   PyObject* obj_type = PyObject_Type(py_object);
   if(PyObject_RichCompareBool(MsgFromPython::py_module_name_, PyObject_GetAttr(obj_type, utils::py_str___module__), Py_EQ) == 1)
   {
     // Module equal (ROS package)
     if(PyObject_RichCompareBool(MsgFromPython::py_class_name_, PyObject_GetAttr(obj_type, utils::py_str___name__), Py_EQ) == 1)
     {
       // Class equal (ROS message)
       return py_object;
     }
   }
   return 0;
 }
Exemplo n.º 13
0
static PyObject *Consumer_seek (Handle *self, PyObject *args, PyObject *kwargs) {

        TopicPartition *tp;
        rd_kafka_resp_err_t err;
        static char *kws[] = { "partition", NULL };
        rd_kafka_topic_t *rkt;

        if (!self->rk) {
                PyErr_SetString(PyExc_RuntimeError, "Consumer closed");
                return NULL;
        }

        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kws,
                                         (PyObject **)&tp))
                return NULL;


        if (PyObject_Type((PyObject *)tp) != (PyObject *)&TopicPartitionType) {
                PyErr_Format(PyExc_TypeError,
                             "expected %s", TopicPartitionType.tp_name);
                return NULL;
        }

        rkt = rd_kafka_topic_new(self->rk, tp->topic, NULL);
        if (!rkt) {
                cfl_PyErr_Format(rd_kafka_last_error(),
                                 "Failed to get topic object for "
                                 "topic \"%s\": %s",
                                 tp->topic,
                                 rd_kafka_err2str(rd_kafka_last_error()));
                return NULL;
        }

        Py_BEGIN_ALLOW_THREADS;
        err = rd_kafka_seek(rkt, tp->partition, tp->offset, -1);
        Py_END_ALLOW_THREADS;

        rd_kafka_topic_destroy(rkt);

        if (err) {
                cfl_PyErr_Format(err,
                                 "Failed to seek to offset %"CFL_PRId64": %s",
                                 tp->offset, rd_kafka_err2str(err));
                return NULL;
        }

        Py_RETURN_NONE;
}
Exemplo n.º 14
0
static _pit *
_ccode2pit(void *cco)
{
    PyCFunctionObject *cfn;
    _hitem *it;
    PyObject *name;

    cfn = cco;
    // Issue #15:
    // Hashing cfn to the pits table causes different object methods
    // to be hashed into the same slot. Use cfn->m_ml for hashing the
    // Python C functions.
    it = hfind(current_ctx->pits, (uintptr_t)cfn->m_ml);
    if (!it) {
        _pit *pit = _create_pit();
        if (!pit)
            return NULL;
        if (!hadd(current_ctx->pits, (uintptr_t)cfn->m_ml, (uintptr_t)pit))
            return NULL;

        pit->builtin = 1;
        pit->modname = _pycfunction_module_name(cfn);
        pit->lineno = 0;

        // built-in method?
        if (cfn->m_self != NULL) {
            name = PyStr_FromString(cfn->m_ml->ml_name);
            if (name != NULL) {
                PyObject *obj_type = PyObject_Type(cfn->m_self);
                PyObject *mo = _PyType_Lookup((PyTypeObject *)obj_type, name);
                Py_XINCREF(mo);
                Py_XDECREF(obj_type);
                Py_DECREF(name);
                if (mo != NULL) {
                    pit->name = PyObject_Repr(mo);
                    Py_DECREF(mo);
                    return pit;
                }
            }
            PyErr_Clear();
        }
        pit->name = PyStr_FromString(cfn->m_ml->ml_name);
        return pit;
    }
    return ((_pit *)it->val);
}
Exemplo n.º 15
0
/**
 * Convert unknown python object to empty cell array.
 * Also send a warning with __repr__ of `obj` shown.
 *
 * :param obj: Object to convert [Borrow reference]
 */
mxArray *mx_from_py_unknown(PyObject* obj)
{
    int dims[1] = { 0 };
    char *buf;

    PyObject *type = PyObject_Type(obj);
    PyObject *repr = PyObject_Repr(type);
    buf = PyString_AsString(repr);
    
    mexWarnMsgTxt("Unknown Python object seen / conversion failed:");
    mexWarnMsgTxt(buf);

    Py_DECREF(repr);
    Py_DECREF(type);
    
    return mxCreateCellArray(1, dims);
}
Exemplo n.º 16
0
crossinterpdatafunc
_PyCrossInterpreterData_Lookup(PyObject *obj)
{
    PyObject *cls = PyObject_Type(obj);
    crossinterpdatafunc getdata = NULL;
    PyThread_acquire_lock(_PyRuntime.xidregistry.mutex, WAIT_LOCK);
    struct _xidregitem *cur = _PyRuntime.xidregistry.head;
    if (cur == NULL) {
        _register_builtins_for_crossinterpreter_data();
        cur = _PyRuntime.xidregistry.head;
    }
    for(; cur != NULL; cur = cur->next) {
        if (cur->cls == (PyTypeObject *)cls) {
            getdata = cur->getdata;
            break;
        }
    }
    Py_DECREF(cls);
    PyThread_release_lock(_PyRuntime.xidregistry.mutex);
    return getdata;
}
Exemplo n.º 17
0
static PyObject *real_encode_io(PyObject *data, PyObject *output)
{
	PyObject *type, *func, *tup, *ret = NULL;
	PyObject *excval;
	if (PyString_Check(data)) {
		/* TODO accept buffer objects */
		return real_encode_string(data, output);
	}
	else if (PyDict_Check(data)) {
		return real_encode_dict(data, output);
	}
	else {
		type = PyObject_Type(data);
		if (type == NULL)
			return NULL;

		func = PyDict_GetItem(encoder_dict, type);   /* borrowed reference to func */
		/* printf("type: %p, func: %p\n", type, func); */
		Py_DECREF(type);
		if (func == NULL) {
			/* TODO include a reference to data in this error */
			excval = Py_BuildValue("(is)", 0, "encoder_dict did not contain an encoding function for data");
			if (excval != NULL) {
				PyErr_SetObject(MencodeError, excval);
				Py_DECREF(excval);
			}
			return NULL;
		}
		tup = PyTuple_New(2);
		if (tup == NULL)
			return NULL;
		Py_INCREF(data);
		Py_INCREF(output);
		PyTuple_SET_ITEM(tup, 0, data);
		PyTuple_SET_ITEM(tup, 1, output);
		ret =  PyObject_CallObject(func, tup);
		Py_DECREF(tup);
		return ret;
	}
}
Exemplo n.º 18
0
static void
_register_builtins_for_crossinterpreter_data(void)
{
    // None
    if (_register_xidata((PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
        Py_FatalError("could not register None for cross-interpreter sharing");
    }

    // int
    if (_register_xidata(&PyLong_Type, _long_shared) != 0) {
        Py_FatalError("could not register int for cross-interpreter sharing");
    }

    // bytes
    if (_register_xidata(&PyBytes_Type, _bytes_shared) != 0) {
        Py_FatalError("could not register bytes for cross-interpreter sharing");
    }

    // str
    if (_register_xidata(&PyUnicode_Type, _str_shared) != 0) {
        Py_FatalError("could not register str for cross-interpreter sharing");
    }
}
Exemplo n.º 19
0
//=============================================================================
// METHOD    : SPELLwsStorage::storeObject
//=============================================================================
void SPELLwsStorage::storeObject( PyObject* object )
{
	if (!m_file.is_open()) return;
    if (object == NULL)
    {
        THROW_EXCEPTION("Unable to store object", "Null reference given", SPELL_ERROR_WSTART);
    }
    if (m_mode == MODE_READ)
    {
        THROW_EXCEPTION("Unable to store object", "Initialized in read mode", SPELL_ERROR_WSTART);
    }

    std::string ptype = PYREPR(PyObject_Type(object));
	DEBUG("Encoding object " + PYREPR(object) + " of type " + ptype);
	// Marshal the object to the persistent storage file
	PyObject* marshal = PyMarshal_WriteObjectToString( object, LATEST_MARSHAL_VERSION );
	// Ensure there is no internal Python error
	SPELLpythonHelper::instance().checkError();
	int marshal_len = PyObject_Length(marshal);
	DEBUG("Marshal length: " + ISTR(marshal_len));
	char* encoded = PyString_AsString(marshal);

    // FORMAT IN FILE:
    // COUNT \1 PTYPE \1 MARSHAL LENGTH
    // MARSHAL

    // If the marshal was OK, continue and store in file: OP,TYPE,MARSHALLED
    m_opCounter++;
    m_file << m_opCounter << "\1" << ptype << "\1" << marshal_len << std::endl;
    m_file.write(encoded,marshal_len); m_file << std::endl;

    std::flush(m_file);

    m_trace << "[" << m_opCounter << "] STORE (" << m_filename << ") " + PYREPR(object) << " [ Type=" + ptype << ", Marhsal length=" + ISTR(marshal_len) + " ]" << std::endl;
    std::flush(m_trace);

}
Teuchos::RCP< Epetra_MultiVector > *
convertPythonToEpetraMultiVector(PyObject * pyobj)
{
  // SWIG initialization
  static swig_type_info * swig_EMV_ptr =
    SWIG_TypeQuery("Teuchos::RCP< Epetra_MultiVector >*");
  static swig_type_info * swig_DMDV_ptr =
    SWIG_TypeQuery("Teuchos::RCP< Domi::MDVector<double> >*");
  //
  // Get the default communicator
  const Teuchos::RCP< const Teuchos::Comm<int> > comm =
    Teuchos::DefaultComm<int>::getComm();
  //
  // Result objects
  void *argp = 0;
  Teuchos::RCP< Epetra_MultiVector > * result = 0;
  Teuchos::RCP< Epetra_MultiVector > emv_rcp;
  Teuchos::RCP< Domi::MDVector<double> > dmdv_rcp;
  int newmem = 0;
  //
  // Check if the Python object is a wrapped Epetra_MultiVector
  int res = SWIG_ConvertPtrAndOwn(pyobj, &argp, swig_EMV_ptr, 0, &newmem);
  if (SWIG_IsOK(res))
  {
    result =
        reinterpret_cast< Teuchos::RCP< Epetra_MultiVector > * >(argp);
    return result;
  }

#ifdef HAVE_DOMI
  //
  // Check if the Python object is a wrapped Domi::MDVector<double>
  newmem = 0;
  res = SWIG_ConvertPtrAndOwn(pyobj, &argp, swig_DMDV_ptr, 0, &newmem);
  if (SWIG_IsOK(res))
  {
    if (newmem & SWIG_CAST_NEW_MEMORY)
    {
      dmdv_rcp =
        *reinterpret_cast< Teuchos::RCP< Domi::MDVector<double> > * >(argp);
      delete reinterpret_cast< Teuchos::RCP< Domi::MDVector<double> > * >(argp);
      //** result = &(dmdv_rcp->getEpetraMultiVectorView());
    }
    else
    {
      dmdv_rcp =
        *reinterpret_cast< Teuchos::RCP< Domi::MDVector<double> > * >(argp);
      //** result = &(dmdv_rcp->getEpetraMultiVectorView());
    }
    return result;
  }
  //
  // Check if the Python object supports the DistArray Protocol
  if (PyObject_HasAttrString(pyobj, "__distarray__"))
  {
    DistArrayProtocol dap(pyobj);
    dmdv_rcp = convertToMDVector<double>(comm, dap);
    //** result = &(dmdv_rcp->getEpetraMultiVectorView());
    return result;
  }
#endif

  //
  // Check if the environment is serial, and if so, check if the
  // Python object is a NumPy array
  if (comm->getSize() == 1)
  {
    if (PyArray_Check(pyobj))
    {
      //** result = new Teuchos::RCP< Epetra_NumPyMultiVector >(
      //**           new Epetra_NumPyMultiVector(pyobj));
      return result;
    }
  }
  //
  // If we get to this point, then none of our known converters will
  // work, so it is time to throw an exception.
  PyErr_Format(PyExc_TypeError, "Could not convert argument of type '%s' to "
               "an Epetra_MultiVector",
               PyString_AsString(PyObject_Str(PyObject_Type(pyobj))));
  throw PythonException();
}
Exemplo n.º 21
0
int Object_npyArrayAddItem(void *prv, JSOBJ obj, JSOBJ value)
{
  PyObject* type;
  PyArray_Descr* dtype;
  npy_intp i;
  char *new_data, *item;
  NpyArrContext* npyarr = (NpyArrContext*) obj;
  PRINTMARK();
  if (!npyarr)
  {
    return 0;
  }

  i = npyarr->i;

  npyarr->shape.ptr[npyarr->dec->curdim-1]++;

  if (PyArray_Check((PyObject*)value))
  {
    // multidimensional array, keep decoding values.
    return 1;
  }

  if (!npyarr->ret)
  {
    // Array not initialised yet.
    // We do it here so we can 'sniff' the data type if none was provided
    if (!npyarr->dec->dtype)
    {
      type = PyObject_Type(value);
      if(!PyArray_DescrConverter(type, &dtype))
      {
        Py_DECREF(type);
        goto fail;
      }
      Py_INCREF(dtype);
      Py_DECREF(type);
    }
    else
    {
      dtype = PyArray_DescrNew(npyarr->dec->dtype);
    }

    // If it's an object or string then fill a Python list and subsequently
    // convert. Otherwise we would need to somehow mess about with
    // reference counts when renewing memory.
    npyarr->elsize = dtype->elsize;
    if (PyDataType_REFCHK(dtype) || npyarr->elsize == 0)
    {
      Py_XDECREF(dtype);

      if (npyarr->dec->curdim > 1)
      {
        PyErr_SetString(PyExc_ValueError, "Cannot decode multidimensional arrays with variable length elements to numpy");
        goto fail;
      }
      npyarr->elcount = 0;
      npyarr->ret = PyList_New(0);
      if (!npyarr->ret)
      {
        goto fail;
      }
      ((JSONObjectDecoder*)npyarr->dec)->newArray = Object_npyNewArrayList;
      ((JSONObjectDecoder*)npyarr->dec)->arrayAddItem = Object_npyArrayListAddItem;
      ((JSONObjectDecoder*)npyarr->dec)->endArray = Object_npyEndArrayList;
      return Object_npyArrayListAddItem(prv, obj, value);
    }

    npyarr->ret = PyArray_NewFromDescr(&PyArray_Type, dtype, 1,
        &npyarr->elcount, NULL,NULL, 0, NULL);

    if (!npyarr->ret)
    {
      goto fail;
    }
  }

  if (i >= npyarr->elcount) {
    // Grow PyArray_DATA(ret):
    // this is similar for the strategy for PyListObject, but we use
    // 50% overallocation => 0, 4, 8, 14, 23, 36, 56, 86 ...
    if (npyarr->elsize == 0)
    {
      PyErr_SetString(PyExc_ValueError, "Cannot decode multidimensional arrays with variable length elements to numpy");
      goto fail;
    }

    npyarr->elcount = (i >> 1) + (i < 4 ? 4 : 2) + i;
    if (npyarr->elcount <= NPY_MAX_INTP/npyarr->elsize) {
      new_data = PyDataMem_RENEW(PyArray_DATA(npyarr->ret), npyarr->elcount * npyarr->elsize);
    }
    else {
      PyErr_NoMemory();
      goto fail;
    }
    ((PyArrayObject*) npyarr->ret)->data = (void*) new_data;

    // PyArray_BYTES(npyarr->ret) = new_data;
  }

  PyArray_DIMS(npyarr->ret)[0] = i + 1;

  if ((item = PyArray_GETPTR1(npyarr->ret, i)) == NULL
      || PyArray_SETITEM(npyarr->ret, item, value) == -1) {
    goto fail;
  }

  Py_DECREF( (PyObject *) value);
  npyarr->i++;
  return 1;

fail:

  Npy_releaseContext(npyarr);
  return 0;
}
Exemplo n.º 22
0
/****************************
 * SV* Py2Pl(PyObject *obj)
 *
 * Converts arbitrary Python data structures to Perl data structures
 * Note on references: does not Py_DECREF(obj).
 *
 * Modifications by Eric Wilhelm 2004-07-11 marked as elw
 *
 ****************************/
SV *Py2Pl(PyObject * const obj) {
    /* elw: see what python says things are */
#if PY_MAJOR_VERSION >= 3
    int const is_string = PyBytes_Check(obj) || PyUnicode_Check(obj);
#else
    int const is_string = PyString_Check(obj) || PyUnicode_Check(obj);
#endif
#ifdef I_PY_DEBUG
    PyObject *this_type = PyObject_Type(obj); /* new reference */
    PyObject *t_string = PyObject_Str(this_type); /* new reference */
#if PY_MAJOR_VERSION >= 3
    PyObject *type_str_bytes = PyUnicode_AsUTF8String(t_string); /* new reference */
    char *type_str = PyBytes_AsString(type_str_bytes);
#else
    char *type_str = PyString_AsString(t_string);
#endif
    Printf(("type is %s\n", type_str));
    printf("Py2Pl object:\n\t");
    PyObject_Print(obj, stdout, Py_PRINT_RAW);
    printf("\ntype:\n\t");
    PyObject_Print(this_type, stdout, Py_PRINT_RAW);
    printf("\n");
    Printf(("String check:   %i\n", is_string));
    Printf(("Number check:   %i\n", PyNumber_Check(obj)));
    Printf(("Int check:      %i\n", PyInt_Check(obj)));
    Printf(("Long check:     %i\n", PyLong_Check(obj)));
    Printf(("Float check:    %i\n", PyFloat_Check(obj)));
    Printf(("Type check:     %i\n", PyType_Check(obj)));
#if PY_MAJOR_VERSION < 3
    Printf(("Class check:    %i\n", PyClass_Check(obj)));
    Printf(("Instance check: %i\n", PyInstance_Check(obj)));
#endif
    Printf(("Dict check:     %i\n", PyDict_Check(obj)));
    Printf(("Mapping check:  %i\n", PyMapping_Check(obj)));
    Printf(("Sequence check: %i\n", PySequence_Check(obj)));
    Printf(("Iter check:     %i\n", PyIter_Check(obj)));
    Printf(("Function check: %i\n", PyFunction_Check(obj)));
    Printf(("Module check:   %i\n", PyModule_Check(obj)));
    Printf(("Method check:   %i\n", PyMethod_Check(obj)));
#if PY_MAJOR_VERSION < 3
    if ((obj->ob_type->tp_flags & Py_TPFLAGS_HEAPTYPE))
        printf("heaptype true\n");
    if ((obj->ob_type->tp_flags & Py_TPFLAGS_HAVE_CLASS))
        printf("has class\n");
#else
    Py_DECREF(type_str_bytes);
#endif
    Py_DECREF(t_string);
    Py_DECREF(this_type);
#endif
    /* elw: this needs to be early */
    /* None (like undef) */
    if (!obj || obj == Py_None) {
        Printf(("Py2Pl: Py_None\n"));
        return &PL_sv_undef;
    }
    else

#ifdef EXPOSE_PERL
    /* unwrap Perl objects */
    if (PerlObjObject_Check(obj)) {
        Printf(("Py2Pl: Obj_object\n"));
        return ((PerlObj_object *) obj)->obj;
    }

    /* unwrap Perl code refs */
    else if (PerlSubObject_Check(obj)) {
        Printf(("Py2Pl: Sub_object\n"));
        SV * ref = ((PerlSub_object *) obj)->ref;
        if (! ref) { /* probably an inherited method */
            if (! ((PerlSub_object *) obj)->obj)
                croak("Error: could not find a code reference or object method for PerlSub");
            SV * const sub_obj = (SV*)SvRV(((PerlSub_object *) obj)->obj);
            HV * const pkg = SvSTASH(sub_obj);
#if PY_MAJOR_VERSION >= 3
            char * const sub = PyBytes_AsString(((PerlSub_object *) obj)->sub);
#else
            PyObject *obj_sub_str = PyObject_Str(((PerlSub_object *) obj)->sub); /* new ref. */
            char * const sub = PyString_AsString(obj_sub_str);
#endif
            GV * const gv = Perl_gv_fetchmethod_autoload(aTHX_ pkg, sub, TRUE);
            if (gv && isGV(gv)) {
                ref = (SV *)GvCV(gv);
            }
#if PY_MAJOR_VERSION < 3
            Py_DECREF(obj_sub_str);
#endif
        }
        return newRV_inc((SV *) ref);
    }

    else
#endif

    /* wrap an instance of a Python class */
    /* elw: here we need to make these look like instances: */
    if ((obj->ob_type->tp_flags & Py_TPFLAGS_HEAPTYPE)
#if PY_MAJOR_VERSION < 3
        || PyInstance_Check(obj)
#endif
    ) {

        /* This is a Python class instance -- bless it into an
         * Inline::Python::Object. If we're being called from an
         * Inline::Python class, it will be re-blessed into whatever
         * class that is.
         */
        SV * const inst_ptr = newSViv(0);
        SV * const inst = newSVrv(inst_ptr, "Inline::Python::Object");;
        _inline_magic priv;

        /* set up magic */
        priv.key = INLINE_MAGIC_KEY;
        sv_magic(inst, inst, PERL_MAGIC_ext, (char *) &priv, sizeof(priv));
        MAGIC * const mg = mg_find(inst, PERL_MAGIC_ext);
        mg->mg_virtual = &inline_mg_vtbl;

        sv_setiv(inst, (IV) obj);
        /*SvREADONLY_on(inst); */ /* to uncomment this means I can't
            re-bless it */
        Py_INCREF(obj);
        Printf(("Py2Pl: Instance. Obj: %p, inst_ptr: %p\n", obj, inst_ptr));

        sv_2mortal(inst_ptr);
        return inst_ptr;
    }

    /* a tuple or a list */
    else if (PySequence_Check(obj) && !is_string) {
        AV * const retval = newAV();
        int i;
        int const sz = PySequence_Length(obj);

        Printf(("sequence (%i)\n", sz));

        for (i = 0; i < sz; i++) {
            PyObject * const tmp = PySequence_GetItem(obj, i);    /* new reference */
            SV * const next = Py2Pl(tmp);
            av_push(retval, next);
            if (sv_isobject(next)) // needed because objects get mortalized in Py2Pl
                SvREFCNT_inc(next);
            Py_DECREF(tmp);
        }

        if (PyTuple_Check(obj)) {
            _inline_magic priv;
            priv.key = TUPLE_MAGIC_KEY;

            sv_magic((SV * const)retval, (SV * const)NULL, PERL_MAGIC_ext, (char *) &priv, sizeof(priv));
        }

        return newRV_noinc((SV *) retval);
    }

    /* a dictionary or fake Mapping object */
    /* elw: PyMapping_Check() now returns true for strings */
    else if (! is_string && PyMapping_Check(obj)) {
        HV * const retval = newHV();
        int i;
        int const sz = PyMapping_Length(obj);
        PyObject * const keys = PyMapping_Keys(obj);   /* new reference */
        PyObject * const vals = PyMapping_Values(obj); /* new reference */

        Printf(("Py2Pl: dict/map\n"));
        Printf(("mapping (%i)\n", sz));

        for (i = 0; i < sz; i++) {
            PyObject * const key = PySequence_GetItem(keys, i), /* new reference */
                                 * const val = PySequence_GetItem(vals, i); /* new reference */
            SV       * const sv_val = Py2Pl(val);
            char     *       key_val;

            if (PyUnicode_Check(key)) {
                PyObject * const utf8_string = PyUnicode_AsUTF8String(key); /* new reference */
#if PY_MAJOR_VERSION >= 3
                key_val = PyBytes_AsString(utf8_string);
                SV * const utf8_key = newSVpv(key_val, PyBytes_Size(utf8_string));
#else
                key_val = PyString_AsString(utf8_string);
                SV * const utf8_key = newSVpv(key_val, PyString_Size(utf8_string));
#endif
                SvUTF8_on(utf8_key);

                hv_store_ent(retval, utf8_key, sv_val, 0);
                Py_DECREF(utf8_string);
            }
            else {
                PyObject * s = NULL;
#if PY_MAJOR_VERSION >= 3
                PyObject * s_bytes = NULL;
                if (PyBytes_Check(key)) {
                    key_val = PyBytes_AsString(key);
#else
                if (PyString_Check(key)) {
                    key_val = PyString_AsString(key);
#endif
                }
                else {
                    /* Warning -- encountered a non-string key value while converting a
                     * Python dictionary into a Perl hash. Perl can only use strings as
                     * key values. Using Python's string representation of the key as
                     * Perl's key value.
                     */
                    s = PyObject_Str(key); /* new reference */
#if PY_MAJOR_VERSION >= 3
                    s_bytes = PyUnicode_AsUTF8String(s); /* new reference */
                    key_val = PyBytes_AsString(s_bytes);
#else
                    key_val = PyString_AsString(s);
#endif
                    Py_DECREF(s);
                    if (PL_dowarn)
                        warn("Stringifying non-string hash key value: '%s'",
                             key_val);
                }

                if (!key_val) {
                    croak("Invalid key on key %i of mapping\n", i);
                }

                hv_store(retval, key_val, strlen(key_val), sv_val, 0);
#if PY_MAJOR_VERSION >= 3
                Py_XDECREF(s_bytes);
#endif
                Py_XDECREF(s);
            }
            if (sv_isobject(sv_val)) // needed because objects get mortalized in Py2Pl
                SvREFCNT_inc(sv_val);
            Py_DECREF(key);
            Py_DECREF(val);
        }
        Py_DECREF(keys);
        Py_DECREF(vals);
        return newRV_noinc((SV *) retval);
    }

    /* a boolean */
    else if (PyBool_Check(obj)) {
Exemplo n.º 23
0
PyObject * PyPreprocessor_scanHeaders( PyPreprocessor * self, PyObject * args, PyObject * kwds )
{
    static char * kwlist[] = { "pp_ctx", "filename", NULL };

    PyObject * pObject = 0;
    PyObject * filename = 0;

    assert( self->pp );

    if ( !PyArg_ParseTupleAndKeywords( args, kwds, "OO", kwlist, &pObject, &filename ) )
        return NULL;

    if ( !pObject || ( (PyTypeObject *)PyObject_Type( pObject ) != &PyPreprocessingContextType ) )
    {
        PyErr_SetString( PyExc_Exception, "Invalid preprocessing context parameter." );
        return NULL;
    }

    PyPreprocessingContext const * ppContext( reinterpret_cast<PyPreprocessingContext *>( pObject ) );

    if ( filename && !PyUnicode_Check( filename ) )
    {
        PyErr_SetString( PyExc_Exception, "Expected a string as 'filename' parameter." );
        return NULL;
    }

    Headers headers;
    HeaderList missing;
    PyThreadState * _save;
    bool result;
    try
    {
        Py_UNBLOCK_THREADS
        result = self->pp->scanHeaders( *ppContext->ppContext, PyUnicode_AsUTF8( filename ), headers, missing );
    }
    catch ( std::runtime_error const & error )
    {
        Py_BLOCK_THREADS
        PyErr_SetString( PyExc_RuntimeError, error.what() );
        return NULL;
    }
    catch ( std::exception const & error )
    {
        Py_BLOCK_THREADS
        PyErr_SetString( PyExc_Exception, error.what() );
        return NULL;
    }
    catch ( ... )
    {
        Py_BLOCK_THREADS
        PyErr_SetString( PyExc_Exception, "Unhandled exception" );
        return NULL;
    }
    
    if ( !result )
    {
        Py_BLOCK_THREADS
        PyErr_SetString( PyExc_Exception, "Failed to preprocess file." );
        return NULL;
    }

    // Group result by dir.
    typedef std::vector<Header const *> HeaderPtrList;
    typedef std::unordered_map<Dir, HeaderPtrList> DirsAndHeaders;
    DirsAndHeaders dirsAndHeaders;
    for ( Header const & header : headers )
        dirsAndHeaders[ header.dir ].push_back( &header );

    Py_BLOCK_THREADS
    PyObject * dirsTuple = PyTuple_New( dirsAndHeaders.size() );
    std::size_t dirIndex( 0 );
    for ( DirsAndHeaders::value_type const & dirAndHeaders : dirsAndHeaders )
    {
        PyObject * headersInDirTuple = PyTuple_New( dirAndHeaders.second.size() );
        std::size_t headersInDirTupleIndex( 0 );
        for ( Header const * header : dirAndHeaders.second )
        {
            PyObject * headerEntry = PyTuple_New( 3 );
            PyTuple_SET_ITEM( headerEntry, 0, PyUnicode_FromStringAndSize( header->name.get().data(), header->name.get().size() ) );

            PyObject * const isRelative( header->relative ? Py_True : Py_False );
            Py_INCREF( isRelative );
            PyTuple_SET_ITEM( headerEntry, 1, isRelative );
        
            PyContentEntry * contentEntry( (PyContentEntry *)_PyObject_New( &PyContentEntryType ) );
            contentEntry->ptr = header->contentEntry.get();
            intrusive_ptr_add_ref( contentEntry->ptr );

            PyTuple_SET_ITEM( headerEntry, 2, (PyObject *)contentEntry );
            PyTuple_SET_ITEM( headersInDirTuple, headersInDirTupleIndex++, headerEntry );
        }
        PyObject * dirTuple = PyTuple_New( 2 );
        llvm::StringRef const dirStr( dirAndHeaders.first.get() );
        PyObject * dir = PyUnicode_FromStringAndSize( dirStr.data(), dirStr.size() );
        PyTuple_SET_ITEM( dirTuple, 0, dir );
        PyTuple_SET_ITEM( dirTuple, 1, headersInDirTuple );
        PyTuple_SET_ITEM( dirsTuple, dirIndex++, dirTuple );
    }

    PyObject * missingHeadersTuple = PyTuple_New( missing.size() );
    std::size_t missingIndex( 0 );
    for ( HeaderList::value_type const & missingHeader : missing )
    {
        PyObject * val = PyUnicode_FromStringAndSize( missingHeader.data(), missingHeader.size() );
        PyTuple_SET_ITEM( missingHeadersTuple, missingIndex++, val );
    }
    
    PyObject * resultTuple = PyTuple_New( 2 );
    PyTuple_SET_ITEM( resultTuple, 0, dirsTuple );
    PyTuple_SET_ITEM( resultTuple, 1, missingHeadersTuple );
    return resultTuple;
}
Exemplo n.º 24
0
//=============================================================================
// METHOD    : SPELLwsDictDataHandler::write()
//=============================================================================
void SPELLwsDictDataHandler::write()
{
	if (getObject() == NULL)
	{
		getStorage()->storeLong( -1 );
		return;
	}

	assert( PyDict_Check(getObject()));

	SPELLpyHandle keys = PyDict_Keys( getObject() );
	unsigned int numItems = PyList_Size( keys.get() );

	DEBUG("[DDH] Storing dictionary items (total " + ISTR(numItems) + ") address " + PSTR(getObject()));

	PyObject* key = NULL;
	PyObject* item = NULL;

	long toStore = 0;
	// Calculate the number of items to store
	// Store each list item
	DEBUG("[DDH] Keys of the dictionary:");
	for( unsigned int index = 0; index < numItems; index++)
	{
		key = PyList_GetItem( keys.get(), index );
		item = PyDict_GetItem( getObject(), key );
		if (SPELLwsWarmStartImpl::shouldFilter(key,item))
		{
			continue;
		}
		else
		{
			DEBUG("     - " + PYSSTR(key));
		}
		toStore++;
	}

	DEBUG("[DDH] Will store " + ISTR(toStore) + " keys");
	// Store the number of items
	getStorage()->storeLong( (long) toStore );

	DEBUG("[DDH] Start checking items");
	if (toStore>0)
	{
		// Store each list item
		for( unsigned int index = 0; index < numItems; index++)
		{
			key = PyList_GetItem( keys.get(), index );
			item = PyDict_GetItem( getObject(), key );

			DEBUG("[DDH] Checking item " + PYCREPR(key));

			// Do not consider filtered values
			if (SPELLwsWarmStartImpl::shouldFilter(key,item)) continue;

			SPELLpythonHelper::instance().checkError();

			DEBUG("		[DDH] Key index " + ISTR(index));
			DEBUG("		[DDH] Key to use" + PYREPR(key));
			DEBUG("		[DDH] Item type:" + PYREPR(PyObject_Type(item)));

			// Handler for the key
			SPELLwsObjectDataHandler keyHandler(key);
			keyHandler.setStorage(getStorage());

			DEBUG("		[DDH] Creating handler");

			// Create a handler for the item
			SPELLwsDataHandler* handler = SPELLwsDataHandlerFactory::createDataHandler(item);
			handler->setStorage(getStorage());

			// Store the key
			DEBUG("		[DDH] Storing key: " + PYREPR(key));
			keyHandler.write();

			// Store the item data code in order to recognise it later
			DEBUG("		[DDH] Storing data code: " + SPELLwsData::codeStr(handler->getCode()));
			handler->storeDataCode();

			// IMPORTANT in the case of lists and dictionaries, we want to be able to continue
			// the storage even if there is a problem in the handler processing at this point.
			// If that is the case, a fake empty object will be replaced by the object being
			// processed by the handler, and the dumping of this collection will continue.
			try
			{
				if (handler->getCode() == SPELLwsData::DATA_CUSTOM_TYPE )
				{
					std::string msg = "WARNING! warm start not supported for custom Python types (" + PYREPR(key) + "=" + PYREPR(item) + ")";
					LOG_WARN(msg);
					SPELLexecutor::instance().getCIF().warning(msg);
					storeFakeObject( SPELLwsData::DATA_NONE );
				}
				else
				{
					// Store the value
					DEBUG("		[DDH] Storing value: " + PYREPR(item));
					handler->write();
					DEBUG("		[DDH] Storing value done");
				}
			}
			catch(SPELLcoreException& ex)
			{
				std::string msg = "WARNING! WS storage of element " + ISTR(index) + " failed: " + ex.what();
				LOG_WARN(msg);
				SPELLexecutor::instance().getCIF().warning(msg);
				storeFakeObject( handler->getCode() );
			}
			delete handler;
		}
	}

	DEBUG("[DDH] Storing dictionary done");
}
Exemplo n.º 25
0
 bool IsPyConfig(PyObject * pyobject)
 {
     if(!pyobject) return false;
     return (PyObject_Type(pyobject) == (PyObject *) (&PyOCIO_ConfigType));
 }
Exemplo n.º 26
0
 bool IsPyLook(PyObject * pyobject)
 {
     if(!pyobject) return false;
     return (PyObject_Type(pyobject) == (PyObject *) (&PyOCIO_LookType));
 }
Exemplo n.º 27
0
//=============================================================================
// METHOD    : SPELLwsStorage::loadObject
//=============================================================================
PyObject* SPELLwsStorage::loadObject()
{
	if (!m_file.is_open()) return NULL;
    if (m_mode == MODE_WRITE)
    {
        THROW_EXCEPTION("Unable to load object", "Initialized in write mode", SPELL_ERROR_WSTART);
    }

    // FORMAT IN FILE:
    // COUNT \1 PTYPE \1 MARSHAL LENGTH
    // MARSHAL

    std::string line = "";
    // Get first the line with the info
    while(!m_file.eof() && (line == ""))
    {
    	std::getline(m_file,line);
        DEBUG("Obtained line [" + line + "]");
    }

    DEBUG("Load object from line [" + line + "]");

    std::vector<std::string> elements = SPELLutils::tokenize(line,"\1");

    PyObject* obj = NULL;
    std::string ptype = elements[1];
	int marshal_len = STRI(elements[2]);
	DEBUG("Decoding object of type " + elements[1] + ", marshal length " + elements[2]);
	// Get the line with the marshal
	char buffer[4512];
	m_file.read(buffer,marshal_len);
	obj = (PyObject*) PyMarshal_ReadObjectFromString( buffer, marshal_len );
	DEBUG("Decoded: " + PYCREPR(obj));
	// Check that the unmarshal was ok
	SPELLpythonHelper::instance().checkError();

    m_opCounter++;
    m_trace << "[" << m_opCounter << "] LOAD (" << m_filename << ") " << PYREPR(obj) << " [ Type=" << PYREPR(PyObject_Type(obj)) << ", Marshal length=" + ISTR(marshal_len) + " ]" << std::endl;
    std::flush(m_trace);


    if (obj != NULL) Py_INCREF(obj);

    return obj;
}
Exemplo n.º 28
0
DistArrayProtocol::DistArrayProtocol(PyObject * distarray)
{
  // Check for a dictionary
  if (!PyDict_Check(distarray))
  {
    PyObject * pyType = PyObject_Type(distarray);
    char * typeStr = PyString_AsString(PyObject_Str(pyType));
    //PyErr_SetString(PyExc_ValueError, "distarray object must be a dictionary");
    PyErr_Format(PyExc_ValueError, "distarray object must be a dictionary, "
                 "given '%s'", typeStr);
    throw PythonException();
  }
  __distarray__ = distarray;
  Py_INCREF(__distarray__);

  // Check for an instance method
  // if (!PyMethod_Check(distarray))
  // {
  //   PyObject * pyType = PyObject_Type(distarray);
  //   char * typeStr = PyString_AsString(PyObject_Str(pyType));
  //   PyErr_Format(PyExc_ValueError, "distarray attribute must be an instance "
  //                "method, given '%s'", typeStr);
  //   throw PythonException();    
  // }
  // std::cout << "a" << std::endl;
  // //PyObject * self = PyMethod_Self(distarray);
  // //std::cout << "b" << std::endl;
  // PyObject * function = PyMethod_Function(distarray);
  // std::cout << "b" << std::endl;
  // if (function == NULL) throw PythonException();
  // std::cout << "d" << std::endl;
  // __distarray__ = PyObject_CallObject(function, NULL);
  // std::cout << "__distarray__ = "
  //           << PyString_AsString(PyObject_Str(__distarray__)) << std::endl;

  //////////////////
  // Get the version
  //////////////////
  PyObject * versionObj = PyDict_GetItemString(distarray, "__version__");
  if (versionObj == NULL)
  {
    PyErr_SetString(PyExc_KeyError, "distarray required key '__version__' not "
                    "present");
    throw PythonException();
  }
  __version__ = PyString_AsString(versionObj);
  if (PyErr_Occurred())
  {
    Py_XDECREF(__distarray__);
    throw PythonException();
  }
#ifdef PYTRILINOS_DAP_VERBOSE
  std::cout << "__version__ = " << __version__ << std::endl;
#endif

  /////////////////
  // Get the buffer
  /////////////////
  __buffer__ = PyDict_GetItemString(distarray, "buffer");
  if (__buffer__ == NULL)
  {
    PyErr_SetString(PyExc_KeyError, "distarray required key 'buffer' not "
                    "present");
    Py_XDECREF(__distarray__);
    throw PythonException();
  }
  // What is the appropriate type check for buffer?
  Py_INCREF(__buffer__);
#ifdef PYTRILINOS_DAP_VERBOSE
  std::cout << "buffer = " << PyString_AsString(PyObject_Str(__buffer__))
            << std::endl;
#endif

  /////////////////////////////////
  // Get the dim_data Python object
  /////////////////////////////////
  PyObject * dimDataObj = PyDict_GetItemString(distarray, "dim_data");
  if (dimDataObj == NULL)
  {
    PyErr_SetString(PyExc_KeyError, "distarray required key 'dim_data' not "
                    "present");
    Py_XDECREF(__distarray__);
    Py_XDECREF(__buffer__);
    throw PythonException();
  }
  if (!PySequence_Check(dimDataObj))
  {
    PyErr_SetString(PyExc_TypeError, "distarray key 'dim_data' must have a "
                    "value that is a sequence");
    Py_XDECREF(__distarray__);
    Py_XDECREF(__buffer__);
    throw PythonException();
  }

  ////////////////////////////////////////////////////////////
  // Loop over dim_data and extract the dimension dictionaries
  ////////////////////////////////////////////////////////////
  dim_data.clear();
  int numDims = PySequence_Size(dimDataObj);
  for (int axis = 0; axis < numDims; ++axis)
  {
    try
    {
      PyObject * dim_dict = PySequence_GetItem(dimDataObj, axis);
#ifdef PYTRILINOS_DAP_VERBOSE
      std::cout << "axis " << axis << ": ";
#endif
      dim_data.push_back(DimensionDictionary(dim_dict));
    }
    catch (PythonException e)
    {
      // One of the dim_dicts was improperly formed.  That messes up
      // this constructor, so de-reference the stored Python objects
      // and re-throw.
      Py_XDECREF(__distarray__);
      Py_XDECREF(__buffer__);
      throw e;
    }
  }
}
Exemplo n.º 29
0
//=============================================================================
// STATIC : SPELLwsDataHandlerFactory::createDataHandler()
//=============================================================================
SPELLwsDataHandler* SPELLwsDataHandlerFactory::createDataHandler( PyObject* object )
{
	assert(object != NULL);

	SPELLwsDataHandler* handler = NULL;

	DEBUG("[DHF] Creating handler for object of type " + PYREPR( PyObject_Type(object) ));

	if (PyDict_Check(object))
	{
		DEBUG("[DHF] Object is a dictionary");
		handler = new SPELLwsDictDataHandler(object);
	}
	else if (PyList_Check(object))
	{
		DEBUG("[DHF] Object is a list");
		handler = new SPELLwsListDataHandler(object);
	}
	else if ( Py_None == object )
	{
		DEBUG("[DHF] Object is None");
		handler = new SPELLwsNoneDataHandler();
	}
	else if (SPELLpythonHelper::instance().isDatabase(object))
	{
		DEBUG("[DHF] Object is database");
		handler = new SPELLwsDbDataHandler(object);
	}
	else if (SPELLpythonHelper::instance().isTime(object))
	{
		DEBUG("[DHF] Object is TIME");
		handler = new SPELLwsTimeDataHandler(object);
	}
	else if (PyClass_Check(object))
	{
		DEBUG("[DHF] Object is a class: " + PYCREPR(object));
		handler = new SPELLwsClassDataHandler(object);
	}
	else if (PyInstance_Check(object))
	{
		DEBUG("[DHF] Object is a instance: " + PYCREPR(object));
		handler = new SPELLwsInstanceDataHandler(object);
	}
	else if (SPELLpythonHelper::instance().isSubclassInstance(object, "TmItemClass", "spell.lib.adapter.tm_item"))
	{
		DEBUG("[DHF] Object is a TM item: " + PYCREPR(object));
		handler = new SPELLwsTmItemDataHandler(object);
	}
	// Look discussion at mail.python.org/pipermail/python-dev/2004-July/046074.html
	else if ((object->ob_type->tp_flags & Py_TPFLAGS_HEAPTYPE)>0)
	{
		DEBUG("[DHF] Object is a custom type: " + PYCREPR(object));
		handler = new SPELLwsCustomTypeDataHandler(object);
	}
	else
	{
		DEBUG("[DHF] Default to object handler: " + PYCREPR(object));
		handler = new SPELLwsObjectDataHandler(object);
	}
	return handler;
}
Exemplo n.º 30
0
//=============================================================================
// METHOD: SPELLvariableMonitor::retrieveGlobalVariables()
//=============================================================================
void SPELLvariableMonitor::retrieveGlobalVariables(std::vector<SPELLvarInfo>& vars,
												   std::set<std::string> locals)
{
	DEBUG("[VM] Retrieve Globals");

	/*
	 * Once we get the bottom stack frame, we have to iterate over all the keys
	 * in the globals dictionary, and filter them agains the m_initialVariables
	 */
	PyObject* dict = m_frame->f_globals;
	PyObject* itemList = PyDict_Keys(dict);
	unsigned int numItems = PyList_Size(itemList);
	for( unsigned int index = 0; index<numItems; index++)
	{
		PyObject* key = PyList_GetItem( itemList, index );
		std::string varName = PYSSTR(key);

		// Do the following check just when the considered variables are not internal databases
		if ( (varName != DatabaseConstants::SCDB) &&
			 (varName != DatabaseConstants::GDB)  &&
			 (varName != DatabaseConstants::PROC) &&
			 (varName != DatabaseConstants::ARGS) &&
			 (varName != DatabaseConstants::IVARS))
		{
			/* If they key is contained in the initial variables set, then ignore it */
			if (m_initialVariables.find(varName) != m_initialVariables.end())
			{
				continue;
			}
		}
		/* If a variable with the same name has been retrieved in the local scope
		 * then it must be ignored */
		if (locals.find(varName) != locals.end())
		{
			continue;
		}

		// Ignore internal flags
		if (varName == "__USERLIB__") continue;

		PyObject* object = PyDict_GetItem( dict, key );

		if (!SPELLpythonHelper::instance().isInstance(object, "Database", "spell.lib.adapter.databases.database"))
		{
			if (PyCallable_Check(object)) continue;
			if (PyClass_Check(object)) continue;
			if (PyModule_Check(object)) continue;
			if (PyInstance_Check(object)) continue;
		}
		DEBUG("[VM] Processing " + varName);

		std::string type = PYSSTR( PyObject_Type(object) );
		std::string value = PYREPR( object );

		DEBUG("[VM] Type      : " + type);
		DEBUG("[VM] Value     : " + value);
		DEBUG("[VM] Global    : " + BSTR(true));
		DEBUG("[VM] Registered: " + BSTR(isRegistered(PYSSTR(key))));

		// Mark empty values (empty strings) as "<empty>"
		if (value == "") value = EMPTY_STRING;

		vars.push_back( SPELLvarInfo(varName, type, value, true, isRegistered(PYSSTR(key))) );
	}
}