Ejemplo n.º 1
0
boost::python::str ScintillaWrapper::getWord(boost::python::object position, boost::python::object useOnlyWordChars /* = true */)
{
	int pos;
	if (position.is_none())
	{
		pos = callScintilla(SCI_GETCURRENTPOS);
	}
	else
	{
		pos = boost::python::extract<int>(position);
	}

	bool wordChars;
	if (useOnlyWordChars.is_none())
	{
		wordChars = true;
	}
	else
	{
		wordChars = boost::python::extract<bool>(useOnlyWordChars);
	}

	int startPos = callScintilla(SCI_WORDSTARTPOSITION, pos, wordChars);
	int endPos = callScintilla(SCI_WORDENDPOSITION, pos, wordChars);
	Sci_TextRange tr;
	tr.chrg.cpMin = startPos;
	tr.chrg.cpMax = endPos;
	tr.lpstrText = new char[size_t((endPos - startPos) + 1)];
	callScintilla(SCI_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&tr));
	boost::python::str retVal(const_cast<const char *>(tr.lpstrText));
	delete[] tr.lpstrText;
	return retVal;
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
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.");
	}
Ejemplo n.º 4
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));
        }
      }
Ejemplo n.º 5
0
    void update(boost::python::object source)
    {
        // See if we have a dictionary-like object.
        if (py_hasattr(source, "items"))
        {
            return this->update(source.attr("items")());
        }
        if (!py_hasattr(source, "__iter__")) {
            THROW_EX(ValueError, "Must provide a dictionary-like object to update()");
        }

        object iter = source.attr("__iter__")();
        while (true) {
            PyObject *pyobj = PyIter_Next(iter.ptr());
            if (!pyobj) break;
            if (PyErr_Occurred()) {
                throw_error_already_set();
            }

            object obj = object(handle<>(pyobj));

            tuple tup = extract<tuple>(obj);
            std::string attr = extract<std::string>(tup[0]);
            std::string value = extract<std::string>(tup[1]);
            setitem(attr, value);
        }
    }
Ejemplo n.º 6
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.");
}
Ejemplo n.º 7
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);
    }
}
Ejemplo n.º 8
0
/** Converts a one-dimensional numpy vector to eigen VectorXd. */
Eigen::VectorXd fromNumpyVector(py::object vec) {
	assert(("Input numpy matrix is not one dimensional.", py::extract<int>(vec.attr("ndim"))==1));
	vec = np_mod.attr("array")(vec, "float64");
	int size = py::extract<int>(vec.attr("size"));

	double* vec_data = getPointer<double>(vec);
	return Eigen::Map<Eigen::VectorXd>(vec_data, size);
}
Ejemplo n.º 9
0
 static void from2dArray(PointCloudT* cloud, py::object arr) {
   arr = np_mod.attr("array")(arr, "float32");
   int npoints = py::extract<int>(arr.attr("shape")[0]);
   int floatfields = py::extract<int>(arr.attr("shape")[1]);
   FAIL_IF_FALSE(floatfields == sizeof(T)/4);
   cloud->resize(npoints);
   float* p = getPointer<float>(arr);
   memcpy(&cloud->points[0], p,  npoints*floatfields*sizeof(float));
 }
Ejemplo n.º 10
0
/** Converts a two-dimensional numpy matrix to eigen MatrixXd. */
Eigen::MatrixXd fromNumpy(py::object mat) {
	assert(("Input numpy matrix is not two dimensional.", py::extract<int>(mat.attr("ndim"))==2));
	mat = np_mod.attr("array")(mat, "float64");
	int rows = py::extract<int>(mat.attr("shape")[0]);
	int cols = py::extract<int>(mat.attr("shape")[1]);

	double* mat_data = getPointer<double>(mat);
	return Eigen::Map<Eigen::MatrixXd>(mat_data, rows, cols);
}
Ejemplo n.º 11
0
static Eigen::MatrixXd fromNP(py::object arr) {
  arr = np_mod.attr("array")(arr, "float64");
  int rows = py::extract<int>(arr.attr("shape")[0]);
  int cols = py::extract<int>(arr.attr("shape")[1]);
  Eigen::MatrixXd mat(rows, cols);
  double* p = getPointer<double>(arr);
  memcpy(&mat(0,0), p,  rows*cols*sizeof(double));
  return mat;
}
Ejemplo n.º 12
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;
 }
Ejemplo n.º 13
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;
}
Ejemplo n.º 15
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();
     }
 }
Ejemplo n.º 16
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));
}
Ejemplo n.º 17
0
 static void from3dArray(PointCloudT* cloud, py::object arr) {
   arr = np_mod.attr("array")(arr, "float32");
   int height = py::extract<int>(arr.attr("shape")[0]);
   int width = py::extract<int>(arr.attr("shape")[1]);
   int floatfields = py::extract<int>(arr.attr("shape")[2]);
   FAIL_IF_FALSE(floatfields==sizeof(T)/4);
   cloud->resize(width*height);
   cloud->height = height;
   cloud->width = width;
   float* p = getPointer<float>(arr);
   memcpy(&cloud->points[0], p,  width*height*floatfields*sizeof(float));
 }
Ejemplo n.º 18
0
	/** Constructor. dtype_name determines type of matrices created. */
	numpy_converter( const char * dtype_name = "float64" )
	{
		PyObject* module = ::PyImport_Import( boost::python::object( "numpy" ).ptr() );
		if( ! module  )
		{
			throw std::logic_error( "Could not import numpy" );
		}
		numpy = boost::python::object( boost::python::handle<>( module ) );
		array_type = numpy.attr( "ndarray" );
		array_function = numpy.attr( "array" );
		set_dtype( dtype_name );
	}
Ejemplo n.º 19
0
    static void set_errors(Tango::EventData &event_data, boost::python::object &error)
    {
        PyObject* error_ptr = error.ptr();
        if (PyObject_IsInstance(error_ptr, PyTango_DevFailed.ptr()))
        {
            Tango::DevFailed df;
	    boost::python::object error_list = error.attr("args");
	    sequencePyDevError_2_DevErrorList(error_list.ptr(), event_data.errors);
        }
        else
        {
            sequencePyDevError_2_DevErrorList(error_ptr, event_data.errors);
        }
    }
Ejemplo n.º 20
0
void ScintillaWrapper::replace(boost::python::object searchStr, boost::python::object replaceStr, boost::python::object flags)
{
	int start = 0;
	int end = GetLength();
	int iFlags = 0;


	if (!flags.is_none())
	{
		iFlags |= boost::python::extract<int>(flags);
	}


	const char *replaceChars = boost::python::extract<const char*>(replaceStr.attr("__str__")());
	
	size_t replaceLength = strlen(replaceChars);

	Sci_TextToFind src;

	src.lpstrText = const_cast<char*>((const char *)boost::python::extract<const char *>(searchStr.attr("__str__")()));
	
	BeginUndoAction();
	int result = 0;

	while(result != -1)
	{
		src.chrg.cpMin = start;
		src.chrg.cpMax = end;
		result = callScintilla(SCI_FINDTEXT, iFlags, reinterpret_cast<LPARAM>(&src));
		
		// If nothing found, then just finish
		if (-1 == result)
		{
			return;
		}
		else
		{
			// Replace the location found with the replacement text
			SetTargetStart(src.chrgText.cpMin);
			SetTargetEnd(src.chrgText.cpMax);
			callScintilla(SCI_REPLACETARGET, replaceLength, reinterpret_cast<LPARAM>(replaceChars));
			start = src.chrgText.cpMin + (int)replaceLength;
			end = end + ((int)replaceLength - (src.chrgText.cpMax - src.chrgText.cpMin));
		}

	}
	EndUndoAction();
}
Ejemplo n.º 21
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);	
	
}
Ejemplo n.º 22
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();
}
Ejemplo n.º 23
0
 boost::python::object call_(const char* name,Args... args){
     using namespace boost::python;
     try {
         return context_.attr(name)(args...);
     }
     catch(error_already_set const &){
         PyObject *type,*value,*traceback;
         PyErr_Fetch(&type,&value,&traceback);
         // Construct a message
         std::string message;
         if(value){
             extract<std::string> value_string(value);
             // Check a string can be extracted from the PyObject
             if(value_string.check()){
                 message += value_string() +":\n";
             }
         }
         // There may not be a traceback (e.g. if a syntax error)
         if(value and traceback){
             handle<> type_handle(type);
             handle<> value_handle(allow_null(value));
             handle<> traceback_handle(allow_null(traceback));
             object formatted_list = import("traceback").attr("format_exception")(type_handle,value_handle,traceback_handle);
             for(int i=0;i<len(formatted_list);i++){
                 extract<std::string> line_string(formatted_list[i]);
                 // Check a string can be extracted from the PyObject
                 if(line_string.check()){
                     message += line_string();
                 }
             }
         }
         throw PythonException(message);
     }
 }
Ejemplo n.º 24
0
void fill_alpha3D_complex(bp::object points, bp::object complex)
{
    typedef     std::map<AlphaSimplex3D::Vertex, unsigned>      ASPointMap;

    Delaunay3D  Dt;
    ASPointMap  point_map;
    unsigned i = 0;
    for (bp::stl_input_iterator<bp::list> pt = points; pt != bp::stl_input_iterator<bp::list>(); ++pt)
    {
        double x = bp::extract<double>((*pt)[0]);
        double y = bp::extract<double>((*pt)[1]);
        double z = bp::extract<double>((*pt)[2]);
        point_map[Dt.insert(Point(x,y,z))] = i++;
    }

    AlphaSimplex3D::SimplexSet simplices;
    fill_simplex_set(Dt, simplices);

    for (AlphaSimplex3D::SimplexSet::const_iterator cur = simplices.begin(); cur != simplices.end(); ++cur)
    {
        
        dp::SimplexVD s;
        for (AlphaSimplex3D::VertexContainer::const_iterator vcur  = cur->vertices().begin(); 
                                                             vcur != cur->vertices().end(); ++vcur)
            s.add(point_map[*vcur]);
        
        s.data() = bp::object(std::make_pair(cur->value(), !cur->attached()));      // regular/critical rather than attached
        complex.attr("append")(s);
    }
}
Ejemplo n.º 25
0
void SystemMatrix::setToSolution(escript::Data& out, escript::Data& in,
                                 boost::python::object& options) const
{
    if (in.isComplex() || out.isComplex())
    {
        throw PasoException("SystemMatrix::setToSolution: complex arguments not supported.");
    }
    options.attr("resetDiagnostics")();
    Options paso_options(options);
    if (out.getDataPointSize() != getColumnBlockSize()) {
        throw PasoException("solve: column block size does not match the number of components of solution.");
    } else if (in.getDataPointSize() != getRowBlockSize()) {
        throw PasoException("solve: row block size does not match the number of components of  right hand side.");
    } else if (out.getFunctionSpace() != getColumnFunctionSpace()) {
        throw PasoException("solve: column function space and function space of solution don't match.");
    } else if (in.getFunctionSpace() != getRowFunctionSpace()) {
        throw PasoException("solve: row function space and function space of right hand side don't match.");
    }
    out.expand();
    in.expand();
    out.requireWrite();
    in.requireWrite();
    double* out_dp = out.getExpandedVectorReference(static_cast<escript::DataTypes::real_t>(0)).data();        
    double* in_dp = in.getExpandedVectorReference(static_cast<escript::DataTypes::real_t>(0)).data();                
    solve(out_dp, in_dp, &paso_options);
    paso_options.updateEscriptDiagnostics(options);
}
Ejemplo n.º 26
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);
}
Ejemplo n.º 27
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);
}
Ejemplo n.º 28
0
 static
 void
 setstate(boost::python::object w_obj, boost::python::tuple state)
 {
     using namespace boost::python;
     world& w = extract<world&>(w_obj)();
     
     if (len(state) != 2)
     {
       PyErr_SetObject(PyExc_ValueError,
                       ("expected 2-item tuple in call to __setstate__; got %s"
                        % state).ptr()
           );
       throw_error_already_set();
     }
     
     // restore the object's __dict__
     dict d = extract<dict>(w_obj.attr("__dict__"))();
     d.update(state[0]);
     
     // restore the internal state of the C++ object
     long number = extract<long>(state[1]);
     if (number != 42)
         w.set_secret_number(number);
 }
Ejemplo n.º 29
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;
  }
Ejemplo n.º 30
0
    static void set_errors(Tango::DataReadyEventData &event_data, 
	                   boost::python::object &dev_failed)
    {
        Tango::DevFailed df;
        boost::python::object errors = dev_failed.attr("args");
        sequencePyDevError_2_DevErrorList(errors.ptr(), event_data.errors);
    }