BOOST_PYTHON_DECL void* rvalue_from_python_stage2( PyObject* source, rvalue_from_python_stage1_data& data, registration const& converters) { if (!data.convertible) { handle<> msg( #if PY_VERSION_HEX >= 0x03000000 ::PyUnicode_FromFormat #else ::PyString_FromFormat #endif ( "No registered converter was able to produce a C++ rvalue of type %s from this Python object of type %s" , converters.target_type.name() , source->ob_type->tp_name )); PyErr_SetObject(PyExc_TypeError, msg.get()); throw_error_already_set(); } // If a construct function was registered (i.e. we found an // rvalue conversion), call it now. if (data.construct != 0) data.construct(source, &data); // Return the address of the resulting C++ object return data.convertible; }
bool str_base::startswith(object_cref prefix, object_cref start) const { bool result = PyInt_AsLong(this->attr("startswith")(prefix,start).ptr()); if (PyErr_Occurred()) throw_error_already_set(); return result; }
long str_base::rindex(object_cref sub, object_cref start) const { long result = PyInt_AsLong(this->attr("rindex")(sub,start).ptr()); if (PyErr_Occurred()) throw_error_already_set(); return result; }
bool str_base::isupper() const { bool result = PyInt_AsLong(this->attr("isupper")().ptr()); if (PyErr_Occurred()) throw_error_already_set(); return result; }
static list attributes(AttributeCachePtr cache, const AttributeCache::ObjectHandle &obj, object regex) { list attributes; AttributeHandleVector a; if ( regex != object() ) { extract< std::string >elem( regex ); if ( elem.check() ) { cache->attributes(obj, elem(), a); } else { PyErr_SetString(PyExc_TypeError, "Regex parameter must be a string or None."); throw_error_already_set(); } } else { cache->attributes(obj, a); } for (AttributeHandleVector::const_iterator it = a.begin(); it != a.end(); ++it) { attributes.append<std::string>(*it); } return attributes; }
static void base_append(Container& container, object v) { extract<data_type&> elem(v); // try if elem is an exact Data if (elem.check()) { DerivedPolicies::append(container, elem()); } else { // try to convert elem to data_type extract<data_type> elem(v); if (elem.check()) { DerivedPolicies::append(container, elem()); } else { PyErr_SetString(PyExc_TypeError, "Attempting to append an invalid type"); throw_error_already_set(); } } }
bool str_base::startswith(object_cref prefix, object_cref start, object_cref end) const { bool result = _BOOST_PYTHON_ASLONG(this->attr("startswith")(prefix,start,end).ptr()); if (PyErr_Occurred()) throw_error_already_set(); return result; }
/// \todo I think we need some sort of standard string /// matching behaviour throughout Gaffer. In various places /// in Python we're accepting either a regex or a string we /// translate with fnmatch.translate(), and in C++ PathMatcher /// uses glob style matching and various registries use regexes. /// Ideally I think we'd settle on a glob style behaviour everywhere /// and have some simple utility class to make it easy to use. static boost::regex objectToRegex( object o ) { extract<std::string> stringExtractor( o ); if( stringExtractor.check() ) { std::string glob = stringExtractor(); std::string regex; for( const char *c = glob.c_str(); *c; ++c ) { switch( *c ) { case '*' : regex += ".*"; break; case '?' : regex += "."; break; case '.' : regex += "\\."; break; default : regex += *c; } } return boost::regex( regex ); } PyErr_SetString( PyExc_TypeError, "Expected a string" ); throw_error_already_set(); return boost::regex(); // we can't get here but we need to keep the compiler happy }
// Execute python source code from file filename. // global and local are the global and local scopes respectively, // used during execution. object BOOST_PYTHON_DECL exec_file(str filename, object global, object local) { // Set suitable default values for global and local dicts. if (global.is_none()) { if (PyObject *g = PyEval_GetGlobals()) global = object(detail::borrowed_reference(g)); else global = dict(); } if (local.is_none()) local = global; // should be 'char const *' but older python versions don't use 'const' yet. char *f = python::extract<char *>(filename); #if PY_VERSION_HEX >= 0x03000000 // TODO(bhy) temporary workaround for Python 3. // should figure out a way to avoid binary incompatibilities as the Python 2 // version did. FILE *fs = fopen(f, "r"); #else // Let python open the file to avoid potential binary incompatibilities. PyObject *pyfile = PyFile_FromString(f, const_cast<char*>("r")); if (!pyfile) throw std::invalid_argument(std::string(f) + " : no such file"); python::handle<> file(pyfile); FILE *fs = PyFile_AsFile(file.get()); #endif PyObject* result = PyRun_File(fs, f, Py_file_input, global.ptr(), local.ptr()); if (!result) throw_error_already_set(); return object(detail::new_reference(result)); }
long str_base::rindex(object_cref sub, object_cref start, object_cref end) const { long result = _BOOST_PYTHON_ASLONG(this->attr("rindex")(sub,start,end).ptr()); if (PyErr_Occurred()) throw_error_already_set(); return result; }
static T extract(PyObject* intermediate) { long x = PyInt_AsLong(intermediate); if (PyErr_Occurred()) throw_error_already_set(); return numeric_cast<T>(x); }
long str_base::rfind(object_cref sub) const { long result = _BOOST_PYTHON_ASLONG(this->attr("rfind")(sub).ptr()); if (PyErr_Occurred()) throw_error_already_set(); return result; }
bool str_base::isupper() const { bool result = _BOOST_PYTHON_ASLONG(this->attr("isupper")().ptr()); if (PyErr_Occurred()) throw_error_already_set(); return result; }
void list_base::insert(object const& index, object_cref x) { long index_ = PyInt_AsLong(index.ptr()); if (index_ == -1 && PyErr_Occurred()) throw_error_already_set(); this->insert(index_, x); }
const object CDebugHelper::ToObject(const DEBUG_VALUE& value) { switch (value.Type) { case DEBUG_VALUE_INT8: return object(value.I8); case DEBUG_VALUE_INT16: return object(value.I16); case DEBUG_VALUE_INT32: return object(value.I32); case DEBUG_VALUE_INT64: return object(value.I64); case DEBUG_VALUE_FLOAT32: return object(value.F32); case DEBUG_VALUE_FLOAT64: return object(value.F64); } size_t size = GetSize(value); object buffer(handle<>(::PyBuffer_New(size))); LPVOID data = NULL; Py_ssize_t len = 0; if (0 != ::PyObject_AsWriteBuffer(buffer.ptr(), &data, &len)) throw_error_already_set(); memcpy(data, &value, size); return buffer; }
// Execute python source code from file filename. // global and local are the global and local scopes respectively, // used during execution. object BOOST_PYTHON_DECL exec_file(str filename, object global, object local) { // Set suitable default values for global and local dicts. object none; if (global.ptr() == none.ptr()) { if (PyObject *g = PyEval_GetGlobals()) global = object(detail::borrowed_reference(g)); else global = dict(); } if (local.ptr() == none.ptr()) local = global; // should be 'char const *' but older python versions don't use 'const' yet. char *f = python::extract<char *>(filename); // Let python open the file to avoid potential binary incompatibilities. PyObject *pyfile = PyFile_FromString(f, const_cast<char*>("r")); if (!pyfile) throw std::invalid_argument(std::string(f) + " : no such file"); python::handle<> file(pyfile); PyObject* result = PyRun_File(PyFile_AsFile(file.get()), f, Py_file_input, global.ptr(), local.ptr()); if (!result) throw_error_already_set(); return object(detail::new_reference(result)); }
BOOST_PYTHON_DECL PyObject* registration::to_python(void const volatile* source) const { if (this->m_to_python == 0) { handle<> msg( #if PY_VERSION_HEX >= 0x3000000 ::PyUnicode_FromFormat #else ::PyString_FromFormat #endif ( "No to_python (by-value) converter found for C++ type: %s" , this->target_type.name() ) ); PyErr_SetObject(PyExc_TypeError, msg.get()); throw_error_already_set(); } return source == 0 ? incref(Py_None) : this->m_to_python(const_cast<void*>(source)); }
void extend_container(Container& container, object l) { typedef typename Container::value_type data_type; // l must be iterable BOOST_FOREACH(object elem, std::make_pair( boost::python::stl_input_iterator<object>(l), boost::python::stl_input_iterator<object>() )) { extract<data_type const&> x(elem); // try if elem is an exact data_type type if (x.check()) { container.push_back(x()); } else { // try to convert elem to data_type type extract<data_type> x(elem); if (x.check()) { container.push_back(x()); } else { PyErr_SetString(PyExc_TypeError, "Incompatible Data Type"); throw_error_already_set(); } } }
inline bool object_operators<U>::operator!() const { object_cref2 x = *static_cast<U const*>(this); int is_true = PyObject_IsTrue(x.ptr()); if (is_true < 0) throw_error_already_set(); return !is_true; }
object BOOST_PYTHON_DECL exec(str string, object global, object local) { // should be 'char const *' but older python versions don't use 'const' yet. char *s = python::extract<char *>(string); PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr()); if (!result) throw_error_already_set(); return object(detail::new_reference(result)); }
long list_base::index(object_cref value) const { object result_obj(this->attr("index")(value)); long result = PyInt_AsLong(result_obj.ptr()); if (result == -1) throw_error_already_set(); return result; }
inline object_operators<U>::operator bool_type() const { object_cref2 x = *static_cast<U const*>(this); int is_true = PyObject_IsTrue(x.ptr()); if (is_true < 0) throw_error_already_set(); return is_true ? &object::ptr : 0; }
BOOST_PYTHON_DECL void setattr(object const& target, char const* key, object const& value) { if (PyObject_SetAttrString( target.ptr(), const_cast<char*>(key), value.ptr()) == -1 ) { throw_error_already_set(); } }
BOOST_PYTHON_DECL void delslice(object const& target, handle<> const& begin, handle<> const& end) { if (assign_slice( target.ptr(), begin.get(), end.get(), 0) == -1 ) { throw_error_already_set(); } }
BOOST_PYTHON_DECL void delattr(object const& target, char const* key) { if (PyObject_DelAttrString( target.ptr(), const_cast<char*>(key)) == -1 ) { throw_error_already_set(); } }
static index_type convert_index(Container& container, PyObject* i_) { extract<long> i(i_); if (i.check()) { long index = i(); if (index < 0) index += DerivedPolicies::size(container); if (index >= long(container.size()) || index < 0) { PyErr_SetString(PyExc_IndexError, "Index out of range"); throw_error_already_set(); } return index; } PyErr_SetString(PyExc_TypeError, "Invalid index type"); throw_error_already_set(); return index_type(); }
//[] access is by reference, so nucleon_collection[0].x += 5 works Nucleon& GetNucleon(NucleonCollection& nucleon_collection, int index) { if(index >= int(nucleon_collection.NucleonCount())) { PyErr_SetString(PyExc_StopIteration, "No more data."); throw_error_already_set(); } if(index < 0) { index = index % nucleon_collection.NucleonCount(); } return nucleon_collection[index]; }
double checkGreaterThanZero(double value) { if (value <= 0) { PyErr_SetString(PyExc_ValueError, "Expected resolution > 0"); throw_error_already_set(); } return value; }
static data_type& get_item(Container& container, index_type i_) { typename Container::iterator i = container.find(i_); if (i == container.end()) { PyErr_SetString(PyExc_KeyError, "Invalid key"); throw_error_already_set(); } return i->second; }
object MakeFile() { FILE* fp = pcap_dump_file( mFileHandle ); if( fp == NULL ) { PyErr_SetString( PyExc_Exception, "Can't make file." ); throw_error_already_set(); } PyObject* o = PyFile_FromFile( fp, "savefile", "wb", fclose ); return object(borrowed(o)); }