Пример #1
0
// REVIEW: the poolSize can be pulled from the numeric array
RDKit::INT_VECT MaxMinPicks(MaxMinPicker *picker, python::object distMat,
                            int poolSize, int pickSize,
                            python::object firstPicks, int seed) {
  if (pickSize >= poolSize) {
    throw ValueErrorException("pickSize must be less than poolSize");
  }

  if (!PyArray_Check(distMat.ptr())) {
    throw ValueErrorException("distance mat argument must be a numpy matrix");
  }

  PyArrayObject *copy;
  copy = (PyArrayObject *)PyArray_ContiguousFromObject(distMat.ptr(),
                                                       PyArray_DOUBLE, 1, 1);
  double *dMat = (double *)copy->data;

  RDKit::INT_VECT firstPickVect;
  for (unsigned int i = 0;
       i < python::extract<unsigned int>(firstPicks.attr("__len__")()); ++i) {
    firstPickVect.push_back(python::extract<int>(firstPicks[i]));
  }
  RDKit::INT_VECT res =
      picker->pick(dMat, poolSize, pickSize, firstPickVect, seed);
  Py_DECREF(copy);
  return res;
}
Пример #2
0
TypeGeneral::EdgeData::EdgeData(const py::object& obj)
{
    typedef TypeGeneral::REAL REAL;
    bool isiterable = PyObject_HasAttrString(obj.ptr(), "__iter__");
    
    if(isiterable)
    {
        py::stl_input_iterator<REAL> it(obj), end;
        bool haslen = PyObject_HasAttrString(obj.ptr(), "__len__");
        
        m_type = GENERAL;
        
        if(haslen)
        {
            m_dataGeneral.resize(py::len(obj));
            std::copy(it, end, m_dataGeneral.begin());
        }
        else
        {
            for(; it!=end; ++it)
            {
               m_dataGeneral.push_back(*it);
               std::cout << *it << " ";
            }
        }
    }
    else
    {
        m_type = POTTS;
        m_lambdaPotts = py::extract<REAL>(obj);
    }
}
Пример #3
0
      void SequenceTypeHandler<ContainerType>::set(Kernel::IPropertyManager* alg, const std::string &name,
                                                   const boost::python::object & value) const
      {
        using boost::python::len;
        typedef typename ContainerType::value_type DestElementType;

        // Current workaround for things that still pass back wrapped vectors...
        if(boost::starts_with(value.ptr()->ob_type->tp_name, "std_vector"))
        {
          alg->setProperty(name, StdVectorExtractor<DestElementType>::extract(value));
        }
        // numpy arrays requires special handling to extract their types. Hand-off to a more appropriate handler
        else if( PyArray_Check(value.ptr()) )
        {
          alg->setProperty(name, Converters::NDArrayToVector<DestElementType>(value)());
        }
        else if( PySequence_Check(value.ptr()) )
        {
          alg->setProperty(name, Converters::PySequenceToVector<DestElementType>(value)());
        }
        else // assume it is a scalar and try to convert into a vector of length one
        {
          DestElementType scalar = boost::python::extract<DestElementType>(value.ptr());
          alg->setProperty(name, std::vector<DestElementType>(1, scalar));
        }
      }
Пример #4
0
void OutputHook::call(std::string name, boost::python::object obj)
{
    Hooks::checkName(name);

    auto repr_ = PyObject_Repr(obj.ptr());
    if (PyErr_Occurred())
    {
        PyErr_Clear();
        throw Hooks::Exception("Failed to get __repr__ of argument");
    }
    auto repr = std::string(PyUnicode_AsUTF8(repr_));
    Py_DECREF(repr_);

    PyObject* g = Py_BuildValue(
            "{sO}", "__builtins__", PyEval_GetBuiltins());
    node->parent->loadDatumHooks(g);
    auto out = PyRun_String(repr.c_str(), Py_eval_input, g, g);
    Py_DECREF(g);
    Py_XDECREF(out);
    if (PyErr_Occurred())
    {
        PyErr_Clear();
        throw Hooks::Exception("Could not evaluate __repr__ of output");
    }

    const bool result = node->makeDatum(
            name, obj.ptr()->ob_type, Datum::SIGIL_OUTPUT + repr, true);

    if (!result)
        throw Hooks::Exception("Datum was already defined in this script.");
}
Пример #5
0
	inGtsSurface(py::object _surf, bool _noPad=false): pySurf(_surf), noPad(_noPad), noPadWarned(false) {
		if(!pygts_surface_check(_surf.ptr())) throw std::invalid_argument("Ctor must receive a gts.Surface() instance."); 
		surf=PYGTS_SURFACE_AS_GTS_SURFACE(PYGTS_SURFACE(_surf.ptr()));
	 	if(!gts_surface_is_closed(surf)) throw std::invalid_argument("Surface is not closed.");
		is_open=gts_surface_volume(surf)<0.;
		if((tree=gts_bb_tree_surface(surf))==NULL) throw std::runtime_error("Could not create GTree.");
	}
Пример #6
0
 std::valarray<T> convert2valarray(boost::python::object arr)
 {
   import();                 // ### WARNING: forgetting this will end up in segmentation fault!
   
   std::size_t vec_size = PyArray_Size(arr.ptr());
   T * data = (T *) PyArray_DATA(arr.ptr());
   std::valarray<T> vec(vec_size);
   memcpy(&vec[0],data, PyArray_ITEMSIZE((PyArrayObject*) arr.ptr()) * vec_size);
   return vec;
 }
Пример #7
0
void send_alive(boost::python::object ad_obj=boost::python::object(), boost::python::object pid_obj=boost::python::object(), boost::python::object timeout_obj=boost::python::object())
{
    std::string addr;
    if (ad_obj.ptr() == Py_None)
    {
        char *inherit_var = getenv("CONDOR_INHERIT");
        if (!inherit_var) {
            THROW_EX(RuntimeError, "No location specified and $CONDOR_INHERIT not in Unix environment.");
        }
        std::string inherit(inherit_var);
        boost::python::object inherit_obj(inherit);
        boost::python::object inherit_split = inherit_obj.attr("split")();
        if (py_len(inherit_split) < 2) {
            THROW_EX(RuntimeError, "$CONDOR_INHERIT Unix environment variable malformed.");
        }
        addr = boost::python::extract<std::string>(inherit_split[1]);
    }
    else
    {
        const ClassAdWrapper ad = boost::python::extract<ClassAdWrapper>(ad_obj);
        if (!ad.EvaluateAttrString(ATTR_MY_ADDRESS, addr))
        {
            THROW_EX(ValueError, "Address not available in location ClassAd.");
        }
    }
    int pid = getpid();
    if (pid_obj.ptr() != Py_None)
    {
        pid = boost::python::extract<int>(pid_obj);
    }
    int timeout;
    if (timeout_obj.ptr() == Py_None)
    {
        timeout = param_integer("NOT_RESPONDING_TIMEOUT");
    }
    else
    {
        timeout = boost::python::extract<int>(timeout_obj);
    }
    if (timeout < 1) {
        timeout = 1;
    }

    classy_counted_ptr<Daemon> daemon = new Daemon(DT_ANY, addr.c_str());
    classy_counted_ptr<ChildAliveMsg> msg = new ChildAliveMsg(pid, timeout, 0, 0, true);

    {
        condor::ModuleLock ml;
        daemon->sendBlockingMsg(msg.get());
    }
    if (msg->deliveryStatus() != DCMsg::DELIVERY_SUCCEEDED)
    {
        THROW_EX(RuntimeError, "Failed to deliver keepalive message.");
    }
}
inline index_type sequence_len(boost::python::object const& obj){
    if( !PySequence_Check( obj.ptr() ) ){
        raise_error( PyExc_TypeError, "Sequence expected" );
    }

    index_type result = PyObject_Length( obj.ptr() );
    if( PyErr_Occurred() ){
        boost::python::throw_error_already_set();
    }
    return result;
}
Пример #9
0
 // Used by multiple profile classes to ensure at most one radius is given.
 static void checkRadii(const bp::object & r1, const bp::object & r2, const bp::object & r3) 
 {
     int nRad = (r1.ptr() != Py_None) + (r2.ptr() != Py_None) + (r3.ptr() != Py_None);
     if (nRad > 1) {
         PyErr_SetString(PyExc_TypeError, "Multiple radius parameters given");
         bp::throw_error_already_set();
     }
     if (nRad == 0) {
         PyErr_SetString(PyExc_TypeError, "No radius parameter given");
         bp::throw_error_already_set();
     }
 }
Пример #10
0
    void
    delegateGSI(boost::python::object fname)
    {
        if (m_claim.empty()) {THROW_EX(ValueError, "No claim set for object.");}

        std::string proxy_file;
        if (fname.ptr() == Py_None)
        {
            proxy_file = get_x509_proxy_filename();
        }
        else
        {
            proxy_file = boost::python::extract<std::string>(fname);
        }

        DCStartd startd(m_addr.c_str());
        startd.setClaimId(m_claim);
        compat_classad::ClassAd reply;
        int irval;
        {
            condor::ModuleLock ml;
            irval = startd.delegateX509Proxy(proxy_file.c_str(), 0, NULL);
        }
        if (irval != OK) {THROW_EX(RuntimeError, "Startd failed to delegate GSI proxy.");}
    }
Пример #11
0
boost::python::object test4( boost::python::object  M, range const & R1, int j) { 
 cerr<<" entring test4 "<<endl;
 array_view<long,2> A(M.ptr());
 array_view<long,1> V(A(R1,j));
 cerr<<" array python "<<A<<endl<<" return view  "<<V<<endl;
 return boost::python::object(V);
}
Пример #12
0
boost::python::object test2( boost::python::object  M, range const & R1, range const & R2) { 
 cerr<<" entring test2 "<<endl;
 array_view<long,2> A (M.ptr());
 array_view<long,2> V(A(R1,R2));
 cerr<<" array python "<<A<<endl<<R1<<" return view  "<<V<<endl;
 return boost::python::object(V);
}
Пример #13
0
double infoGain(python::object resArr) {
  PyObject *matObj = resArr.ptr();
  if (!PyArray_Check(matObj)) {
    throw_value_error("Expecting a Numeric array object");
  }
  PyArrayObject *copy;
  copy = (PyArrayObject *)PyArray_ContiguousFromObject(
      matObj, PyArray_DESCR((PyArrayObject *)matObj)->type_num, 2, 2);
  long int rows = (long int)PyArray_DIM((PyArrayObject *)matObj, 0);
  long int cols = (long int)PyArray_DIM((PyArrayObject *)matObj, 1);
  double res = 0.0;
  if (PyArray_DESCR((PyArrayObject *)matObj)->type_num == NPY_DOUBLE) {
    double *data = (double *)PyArray_DATA(copy);
    res = InfoEntropyGain(data, rows, cols);
  } else if (PyArray_DESCR((PyArrayObject *)matObj)->type_num == NPY_FLOAT) {
    float *data = (float *)PyArray_DATA(copy);
    res = InfoEntropyGain(data, rows, cols);
  } else if (PyArray_DESCR((PyArrayObject *)matObj)->type_num == NPY_INT) {
    int *data = (int *)PyArray_DATA(copy);
    res = InfoEntropyGain(data, rows, cols);
  } else if (PyArray_DESCR((PyArrayObject *)matObj)->type_num == NPY_LONG) {
    long int *data = (long int *)PyArray_DATA(copy);
    res = InfoEntropyGain(data, rows, cols);
  } else {
    throw_value_error(
        "Numeric array object of type int or long or float or double");
  }
  Py_DECREF(copy);
  return res;
}
Пример #14
0
double infoEntropy(python::object resArr) {
  PyObject *matObj = resArr.ptr();
  if (!PyArray_Check(matObj)) {
    throw_value_error("Expecting a Numeric array object");
  }
  PyArrayObject *copy;
  copy = (PyArrayObject *)PyArray_ContiguousFromObject(
      matObj, ((PyArrayObject *)matObj)->descr->type_num, 1, 1);
  double res = 0.0;
  // we are expecting a 1 dimensional array
  long int ncols = (long int)((PyArrayObject *)matObj)->dimensions[0];
  CHECK_INVARIANT(ncols > 0, "");
  if (((PyArrayObject *)matObj)->descr->type_num == PyArray_DOUBLE) {
    double *data = (double *)copy->data;
    res = InfoEntropy(data, ncols);
  } else if (((PyArrayObject *)matObj)->descr->type_num == PyArray_FLOAT) {
    float *data = (float *)copy->data;
    res = InfoEntropy(data, ncols);
  } else if (((PyArrayObject *)matObj)->descr->type_num == PyArray_INT) {
    int *data = (int *)copy->data;
    res = InfoEntropy(data, ncols);
  } else if (((PyArrayObject *)matObj)->descr->type_num == PyArray_LONG) {
    long int *data = (long int *)copy->data;
    res = InfoEntropy(data, ncols);
  }
  Py_DECREF(copy);
  return res;
}
Пример #15
0
double chiSquare(python::object resArr) {
  PyObject *matObj = resArr.ptr();
  if (!PyArray_Check(matObj)) {
    throw_value_error("Expecting a Numeric array object");
  }
  PyArrayObject *copy;
  copy = (PyArrayObject *)PyArray_ContiguousFromObject(
      matObj, ((PyArrayObject *)matObj)->descr->type_num, 2, 2);
  long int rows = (long int)((PyArrayObject *)matObj)->dimensions[0];
  long int cols = (long int)((PyArrayObject *)matObj)->dimensions[1];
  double res = 0.0;
  if (((PyArrayObject *)matObj)->descr->type_num == PyArray_DOUBLE) {
    double *data = (double *)copy->data;
    res = ChiSquare(data, rows, cols);
  } else if (((PyArrayObject *)matObj)->descr->type_num == PyArray_FLOAT) {
    float *data = (float *)copy->data;
    res = ChiSquare(data, rows, cols);
  } else if (((PyArrayObject *)matObj)->descr->type_num == PyArray_INT) {
    int *data = (int *)copy->data;
    res = ChiSquare(data, rows, cols);
  } else if (((PyArrayObject *)matObj)->descr->type_num == PyArray_LONG) {
    long int *data = (long int *)copy->data;
    res = ChiSquare(data, rows, cols);
  } else {
    throw_value_error(
        "Numeric array object of type int or long or float or double");
  }
  Py_DECREF(copy);
  return res;
}
Пример #16
0
static py::object unpack( py::object bytes )
{
    auto p = bytes.ptr();

    if( !p )
    {
        throw std::runtime_error( "can't unpack: no object" );
    }

    const void * data = nullptr;
    Py_ssize_t size = 0;

    if( PyObject_AsReadBuffer( p, &data, &size ) )
    {
        py::throw_error_already_set();
    }

    if( !data || !size )
    {
        throw std::runtime_error( "can't unpack: no data" );
    }

    Unpacker unpacker( reinterpret_cast< char const * >( data ), size );

    return unpacker.unpack();
}
Пример #17
0
void sgs_lvm_simulation(
		boost::python::tuple output_array,
		const py_grid_t & grid,
		const py_sgs_params_t & param,
		boost::python::object mean_data,		
		PyObject * cdf_property,
		boost::python::object mask_data)
{
	using namespace boost::python;	
	sp_double_property_array_t out_prop = cont_prop_from_tuple(output_array);

	boost::python::extract<tuple> ext(cdf_property);
	
	unsigned char * mask =
			mask_data.ptr() == Py_None ? NULL 
			: get_buffer_from_ndarray<unsigned char,'u'>(mask_data, out_prop->size(), "sgs_lvm_simulation");

	sp_double_property_array_t cdf_prop;
	if (ext.check())
	{
		cdf_prop = cont_prop_from_tuple(ext);
	}
	else
	{
		cdf_prop = out_prop;
	}

	mean_t * lvm_data = get_buffer_from_ndarray<mean_t, 'f'>(mean_data, out_prop->size(), "sgs_lvm_simulation");
	hpgl::sequential_gaussian_simulation_lvm(*grid.m_sugarbox_geometry, param.m_sgs_params, lvm_data, *out_prop, *cdf_prop, mask);	
	
}
Пример #18
0
  bool doTriangleSmoothing(python::object boundsMatArg,double tol){
    PyObject *boundsMatObj = boundsMatArg.ptr();
    if(!PyArray_Check(boundsMatObj))
      throw_value_error("Argument isn't an array");
    
    PyArrayObject *boundsMat=reinterpret_cast<PyArrayObject *>(boundsMatObj);
    // get the dimensions of the array
    int nrows = boundsMat->dimensions[0];
    int ncols = boundsMat->dimensions[1];
    if(nrows!=ncols)
      throw_value_error("The array has to be square");
    if(nrows<=0)
      throw_value_error("The array has to have a nonzero size");
    if (boundsMat->descr->type_num != PyArray_DOUBLE)
      throw_value_error("Only double arrays are currently supported");

    unsigned int dSize = nrows*nrows;
    double *cData = new double[dSize];
    double *inData = reinterpret_cast<double *>(boundsMat->data);
    memcpy(static_cast<void *>(cData), 
           static_cast<const void *>(inData),
           dSize*sizeof(double));
    DistGeom::BoundsMatrix::DATA_SPTR sdata(cData);
    DistGeom::BoundsMatrix bm(nrows,sdata);

    bool res=DistGeom::triangleSmoothBounds(&bm,tol);
    memcpy(static_cast<void *>(inData), 
           static_cast<const void *>(cData),
           dSize*sizeof(double));
    return res;
  }
Пример #19
0
const message::ptr messageW::get_message_ptr(const boost::python::object& obj){
	boost::python::extract<bool> b(obj);
	if(PyBool_Check(obj.ptr())){
	    return bool_message::create(b());
	}
	boost::python::extract<int64_t> i(obj);
	if (i.check()) {
	    return int_message::create(i());
	}
	boost::python::extract<double> d(obj);
	if (d.check()) {
	    return double_message::create(d());
	}
	boost::python::extract<std::string> s(obj);
	if (s.check()) {
	    return string_message::create(s());
	}
	boost::python::extract<boost::python::list> l(obj);
	if (l.check()) {
		boost::python::list list=l();
		message::ptr ret=array_message::create();
		int len = boost::python::len(list);
		for(int i=0; i<len; i++) {
			ret->get_vector().push_back(messageW::get_message_ptr(list[i]));
		}
	    return ret;
	}
	boost::python::extract<boost::python::dict> m(obj);
	if (m.check()) {
		boost::python::dict dict=m();
		message::ptr ret=object_message::create();
		int len = boost::python::len(dict);
		boost::python::list keys = dict.keys();
		for(int i=0; i<len; i++) {
			std::string k = boost::python::extract<std::string>(keys[i]);
			ret->get_map()[k]=messageW::get_message_ptr(dict[k]);
		}
		return ret;
	}
	if (obj.ptr() == Py_None){
		return null_message::create();
	}
	PyErr_SetString(PyExc_TypeError,"Incompatible type of message.");
	//std::cout<<"No type hit."<<std::endl;
	boost::python::throw_error_already_set();
	return null_message::create();
}
Пример #20
0
int PyHandle::Cmp( bp::object other )
{
	// The thing to which we compare is NULL
	PyObject *pPyObject = other.ptr();
	if( pPyObject == Py_None ) {
		return Get() != NULL;
	}

	// We are NULL
	if( Get() == NULL )
	{
		return pPyObject != NULL;
	}

	// Check if it is directly a pointer to an entity
#ifdef CLIENT_DLL
	if( PyObject_IsInstance(pPyObject, bp::object(_entities.attr("C_BaseEntity")).ptr()) )
#else
	if( PyObject_IsInstance(pPyObject, bp::object(_entities.attr("CBaseEntity")).ptr()) )
#endif // CLIENT_DLL
	{
		CBaseEntity *pSelf = Get();
		CBaseEntity *pOther = boost::python::extract< CBaseEntity * >(other);
		if( pOther == pSelf )
		{
			return 0;
		}
		else if( pOther->entindex() > pSelf->entindex() )
		{
			return 1;
		}
		else
		{
			return -1;
		}
	}

	try
	{
		// Must be a handle
		CBaseHandle *pHandle = bp::extract< CBaseHandle * >( other );
		if( pHandle )
		{
			if( pHandle->ToInt() == ToInt() )
				return 0;
			else if( pHandle->GetEntryIndex() > GetEntryIndex() )
				return 1;
			else
				return -1;
		}
	}
	catch( bp::error_already_set & )
	{
		// Not a handle, just clear error and return -1
		PyErr_Clear();	
	}

	return -1;
}
 //bravais_lattice::bravais_lattice( tqa::array_view<double,2> const & units__, boost::python::object orbitals__) : 
 bravais_lattice::bravais_lattice( boost::python::object units__, boost::python::object orbitals__) : 
  units_(3,3), dim_(boost::python::len(units__)), orbitals_() { 
  //units_(3,3), dim_(units__.len(0)), orbitals_() { 
   cons_deleg(array<double,2>(units__.ptr()));
   //if (orbitals__) 
    orbitals_ = python_tools::converter<orbital_type>::Py2C(orbitals__);
   //else { R_type z(3); z()=0; orbitals_.insert(std::make_pair("",z));}
  }
Пример #22
0
std::string Serialisation::modulePath( boost::python::object &o )
{
	if( !PyObject_HasAttrString( o.ptr(), "__module__" ) )
	{
		return "";
	}
	std::string modulePath = extract<std::string>( o.attr( "__module__" ) );
	std::string objectName;
	if( PyType_Check( o.ptr() ) )
	{
		objectName = extract<std::string>( o.attr( "__name__" ) );
	}
	else
	{
		objectName = extract<std::string>( o.attr( "__class__" ).attr( "__name__" ) );
	}

	typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
	std::string sanitisedModulePath;
	Tokenizer tokens( modulePath, boost::char_separator<char>( "." ) );

	for( Tokenizer::iterator tIt=tokens.begin(); tIt!=tokens.end(); tIt++ )
	{
		if( tIt->compare( 0, 1, "_" )==0 )
		{
			// assume that module path components starting with _ are bogus, and are used only to bring
			// binary components into a namespace.
			continue;
		}
		Tokenizer::iterator next = tIt; next++;
		if( next==tokens.end() && *tIt == objectName )
		{
			// if the last module name is the same as the class name then assume this is just the file the
			// class has been implemented in.
			continue;
		}
		if( sanitisedModulePath.size() )
		{
			sanitisedModulePath += ".";
		}
		sanitisedModulePath += *tIt;
	}

	return sanitisedModulePath;
}
Пример #23
0
 void addCallback(const boost::python::object &callable) {
   PyThreadSafeObject obj(callable);
   if (!PyCallable_Check(callable.ptr()))
     throw std::runtime_error("Not a callable");
   {
     GILScopedUnlock _unlock;
     _obj.connect(_sigid, AnyFunction::from(boost::bind<void>(&pyPropertyCb, _1, obj)));
   }
 }
Пример #24
0
 static void* convertible(PyObject* pyo)
 {
     if (!PyObject_TypeCheck(pyo, reinterpret_cast<PyTypeObject*>(
             species_info_class.ptr())))
     {
         return 0;
     }
     return pyo;
 }
Пример #25
0
// REVIEW: the poolSize can be pulled from the numeric array
RDKit::VECT_INT_VECT HierarchicalClusters(HierarchicalClusterPicker *picker,
                                          python::object &distMat, int poolSize,
                                          int pickSize) {
  if (!PyArray_Check(distMat.ptr())) {
    throw ValueErrorException("distance mat argument must be a numpy matrix");
  }

  // REVIEW: check pickSize < poolSize, otherwise throw_value_error()
  PyArrayObject *copy;
  // it's painful to have to copy the input matrix, but the
  // picker itself will step on the distance matrix, so use
  // CopyFromObject here instead of ContiguousFromObject
  copy =
      (PyArrayObject *)PyArray_CopyFromObject(distMat.ptr(), NPY_DOUBLE, 1, 1);
  double *dMat = (double *)PyArray_DATA(copy);

  RDKit::VECT_INT_VECT res = picker->cluster(dMat, poolSize, pickSize);
  Py_DECREF(copy);
  return res;
}
Пример #26
0
  void InfoManager::setPyEventScheduler(boost::python::object evt_scheduler) {
#ifdef DEBUG
    Logger::debug2() << "InfoManager setting Python event scheduler" << endl;
#endif
    fPyEventScheduler  = evt_scheduler;
    //cerr << "pyeventscheduler location is " << evt_scheduler.ptr() << endl;
  
    // infomanager destructor seg-faults if we do not increment
    // reference to python object. - ST
    Py_INCREF(evt_scheduler.ptr());

  }
Пример #27
0
int get_bytes_or_bytearray_ln(bp::object buf)
{
    PyObject *py_ba;
    py_ba = buf.ptr();
    if (PyByteArray_Check(py_ba))
        return PyByteArray_Size(py_ba);
    else if (PyBytes_Check(py_ba))
        return PyBytes_Size(py_ba);
    else
        throw_ba_exception();
    return 0;
}
Пример #28
0
void Socket::send(py::object py_buf) const throw()
{
    // pointer to buffer
    char* buf = nullptr;
    // true buffer length
    int buf_len = 0;
    // passed in length size
    int pref_len = 0;

    // TODO: find a way to use Boost.Python instead
    if (!PyArg_ParseTuple(py_buf.ptr(), "s#ii", &buf, &buf_len, &pref_len))
    {
        Exception e("Wrong arguments: Socket::send((char*)buf, (int)buf_len)", "");
        translateException(e);
        throw e;
    }
    else
    {
        if ((pref_len - buf_len) > buf_len)
        {
            Exception e("Buffer length must not double real buffer length", "");
            translateException(e);
            throw e;

            return;
        }
    }

    if (buf == nullptr)
    {
        Exception e("Null buffer provided during Socket::send", "");
        translateException(e);
        throw e;
    }

    int res;

    // FIXME: useful python macros?
    Py_BEGIN_ALLOW_THREADS;
    res = UDT::send(descriptor_, buf, buf_len, 0);
    Py_END_ALLOW_THREADS;

    if (res == UDT::ERROR)
    {
        PYUDT_LOG_ERROR("Could not send data through socket " << descriptor_);
        translateUDTError();
        return;
    }

    PYUDT_LOG_TRACE("Sent " << buf_len << " byte(s) through socket "
                    << descriptor_ << " that were stored in "
                    << static_cast<void *>(&buf));
}
Пример #29
0
/** Must be called with GIL locked
 * */
std::string getStrFromPy(bp::object o) {
    PyObject* ptr = o.ptr();
    if (PyBytes_Check(ptr)) {
        return std::string(PyBytes_AsString(ptr),
                PyBytes_Size(ptr));
    }

    bp::object bytes = o.attr("encode")("utf-8");
    ptr = bytes.ptr();
    return std::string(PyBytes_AsString(ptr),
            PyBytes_Size(ptr));
}
Пример #30
0
  py::handle<> host_pool_allocate(
      boost::shared_ptr<pycuda::memory_pool<host_allocator> > pool,
      py::object shape, py::object dtype, py::object order_py)
  {
    PyArray_Descr *tp_descr;
    if (PyArray_DescrConverter(dtype.ptr(), &tp_descr) != NPY_SUCCEED)
      throw py::error_already_set();

    std::vector<npy_intp> dims;
    std::copy(
        py::stl_input_iterator<npy_intp>(shape),
        py::stl_input_iterator<npy_intp>(),
        back_inserter(dims));

    std::auto_ptr<pooled_host_allocation> alloc(
        new pooled_host_allocation( 
          pool, tp_descr->elsize*pycuda::size_from_dims(dims.size(), &dims.front())));

    NPY_ORDER order = PyArray_CORDER;
    PyArray_OrderConverter(order_py.ptr(), &order);

    int flags = 0;
    if (order == PyArray_FORTRANORDER)
      flags |= NPY_FARRAY;
    else if (order == PyArray_CORDER)
      flags |= NPY_CARRAY;
    else
      throw std::runtime_error("unrecognized order specifier");

    py::handle<> result = py::handle<>(PyArray_NewFromDescr(
        &PyArray_Type, tp_descr,
        int(dims.size()), &dims.front(), /*strides*/ NULL,
        alloc->ptr(), flags, /*obj*/NULL));

    py::handle<> alloc_py(handle_from_new_ptr(alloc.release()));
    PyArray_BASE(result.get()) = alloc_py.get();
    Py_INCREF(alloc_py.get());

    return result;
  }