2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-25 06:22:15 +00:00

str, dict, and tuple!

[SVN r14517]
This commit is contained in:
Dave Abrahams
2002-07-18 15:17:08 +00:00
parent 94edc13393
commit dfd85da9d7
18 changed files with 1419 additions and 537 deletions

172
src/dict.cpp Normal file
View File

@@ -0,0 +1,172 @@
#include <boost/python/dict.hpp>
#include <boost/python/extract.hpp>
namespace boost { namespace python {
namespace
{
// When returning list objects from methods, it may turn out that the
// derived class is returning something else, perhaps something not
// even derived from list. Since it is generally harmless for a
// Boost.Python wrapper object to hold an object of a different
// type, and because calling list() with an object may in fact
// perform a conversion, the least-bad alternative is to assume that
// we have a Python list object and stuff it into the list result.
list assume_list(object const& o)
{
return list(detail::borrowed_reference(o.ptr()));
}
// No PyDict_CheckExact; roll our own.
inline bool check_exact(dict const* p)
{
return p->ptr()->ob_type == &PyDict_Type;
}
}
BOOST_PYTHON_DECL detail::new_reference dict::call(object const& arg)
{
return (detail::new_reference)PyObject_CallFunction(
(PyObject*)&PyDict_Type, "(O)",
arg.ptr());
}
BOOST_PYTHON_DECL dict::dict()
: object(detail::new_reference(PyDict_New()))
{}
BOOST_PYTHON_DECL dict::dict(object_cref data)
: object(dict::call(data))
{}
BOOST_PYTHON_DECL void dict::clear()
{
if (check_exact(this))
PyDict_Clear(this->ptr());
else
this->attr("clear")();
}
BOOST_PYTHON_DECL dict dict::copy()
{
if (check_exact(this))
{
return dict(detail::new_reference(
PyDict_Copy(this->ptr())));
}
else
{
return dict(detail::borrowed_reference(
this->attr("copy")().ptr()
));
}
}
BOOST_PYTHON_DECL object dict::get(object_cref k) const
{
if (check_exact(this))
{
return object(detail::borrowed_reference(
PyDict_GetItem(this->ptr(),k.ptr())));
}
else
{
return this->attr("get")(k);
}
}
BOOST_PYTHON_DECL object dict::get(object_cref k, object_cref d) const
{
return this->attr("get")(k,d);
}
BOOST_PYTHON_DECL bool dict::has_key(object_cref k) const
{
return extract<bool>(this->attr("has_key")(k));
}
BOOST_PYTHON_DECL list dict::items() const
{
if (check_exact(this))
{
return list(detail::new_reference(
PyDict_Items(this->ptr())));
}
else
{
return assume_list(this->attr("items")());
}
}
BOOST_PYTHON_DECL object dict::iteritems() const
{
return this->attr("iteritems")();
}
BOOST_PYTHON_DECL object dict::iterkeys() const
{
return this->attr("iterkeys")();
}
BOOST_PYTHON_DECL object dict::itervalues() const
{
return this->attr("itervalues")();
}
BOOST_PYTHON_DECL list dict::keys() const
{
if (check_exact(this))
{
return list(detail::new_reference(
PyDict_Keys(this->ptr())));
}
else
{
return assume_list(this->attr("keys")());
}
}
BOOST_PYTHON_DECL tuple dict::popitem()
{
return tuple(detail::borrowed_reference(
this->attr("popitem")().ptr()
));
}
BOOST_PYTHON_DECL object dict::setdefault(object_cref k)
{
return this->attr("setdefault")(k);
}
BOOST_PYTHON_DECL object dict::setdefault(object_cref k, object_cref d)
{
return this->attr("setdefault")(k,d);
}
BOOST_PYTHON_DECL void dict::update(object_cref other)
{
if (check_exact(this))
{
if (PyDict_Update(this->ptr(),other.ptr()) == -1)
throw_error_already_set();
}
else
{
this->attr("update")(other);
}
}
BOOST_PYTHON_DECL list dict::values() const
{
if (check_exact(this))
{
return list(detail::new_reference(
PyDict_Values(this->ptr())));
}
else
{
return assume_list(this->attr("values")());
}
}
}} // namespace boost::python

View File

@@ -7,7 +7,7 @@
#include <boost/python/object/function.hpp>
#include <numeric>
#include <boost/python/errors.hpp>
#include <boost/python/objects2.hpp>
#include <boost/python/str.hpp>
#include <algorithm>
#include <cstring>
@@ -158,7 +158,7 @@ namespace
void function::add_to_namespace(
handle<> const& name_space, char const* name_, handle<> const& attribute)
{
string const name(name_);
str const name(name_);
PyObject* const ns = name_space.get();
if (attribute->ob_type == &function_type)
@@ -175,7 +175,7 @@ void function::add_to_namespace(
if (dict == 0)
throw_error_already_set();
handle<> existing( allow_null(PyObject_GetItem(dict, name.get())) );
handle<> existing( allow_null(PyObject_GetItem(dict, name.ptr())) );
if (existing.get())
{
@@ -195,7 +195,7 @@ void function::add_to_namespace(
// The PyObject_GetAttrString() call above left an active error
PyErr_Clear();
if (PyObject_SetAttr(ns, name.get(), attribute.get()) < 0)
if (PyObject_SetAttr(ns, name.ptr(), attribute.get()) < 0)
throw_error_already_set();
}

View File

@@ -1,279 +0,0 @@
// (C) Copyright David Abrahams 2000. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
// producing this work.
// TODO: Move inline implementations from objects.cpp here
#ifndef BOOST_PYTHON_SOURCE
# define BOOST_PYTHON_SOURCE
#endif
#include <boost/python/objects.hpp>
#include <boost/python/detail/none.hpp>
namespace boost { namespace python {
objects_base::objects_base(handle<> const& p)
: m_p(borrowed(p.get())) // Do the null check here
{}
// Return a reference to the held object
handle<> objects_base::reference() const
{
return m_p;
}
// Return a raw pointer to the held object
PyObject* objects_base::get() const
{
return m_p.get();
}
}} // namespace boost::python
namespace boost { namespace python {
tuple_base::tuple_base(std::size_t n)
: objects_base(handle<>(PyTuple_New(n)))
{
for (std::size_t i = 0; i < n; ++i)
PyTuple_SET_ITEM(get(), i, detail::none());
}
tuple_base::tuple_base(handle<> p)
: objects_base(p)
{
assert(accepts(p));
if (!accepts(p))
{
PyErr_SetString(PyExc_TypeError, p->ob_type->tp_name);
throw_error_already_set();
}
}
PyTypeObject* tuple_base::type_obj()
{
return &PyTuple_Type;
}
bool tuple_base::accepts(handle<> p)
{
return PyTuple_Check(p.get());
}
std::size_t tuple_base::size() const
{
return PyTuple_Size(get());
}
handle<> tuple_base::operator[](std::size_t pos) const
{
return handle<>(borrowed(PyTuple_GetItem(get(), static_cast<int>(pos))));
}
void tuple_base::set_item(std::size_t pos, const handle<>& rhs)
{
int failed = PyTuple_SetItem(
get()
, static_cast<int>(pos)
, incref(expect_non_null(rhs.get()))
);
(void)failed;
assert(failed == 0);
}
tuple tuple_base::slice(int low, int high) const
{
return tuple(handle<>(PyTuple_GetSlice(get(), low, high)));
}
BOOST_PYTHON_DECL tuple& operator+=(tuple& self, const tuple& rhs)
{
return self = self + rhs;
}
// Construct from an owned PyObject*.
// Precondition: p must point to a python string.
string::string(handle<> p)
: objects_base(p)
{
assert(accepts(p));
if (!accepts(p))
{
PyErr_SetString(PyExc_TypeError, p->ob_type->tp_name);
throw_error_already_set();
}
}
string::string(const char* s)
: objects_base(handle<>(PyString_FromString(s))) {}
string::string(const char* s, std::size_t length)
: objects_base(handle<>(PyString_FromStringAndSize(s, length))) {}
string::string(const char* s, interned_t)
: objects_base(handle<>(PyString_InternFromString(s))) {}
string::string(const string& rhs)
: objects_base(rhs.reference()) {}
// Get the type object for Strings
PyTypeObject* string::type_obj()
{ return &PyString_Type; }
// Return true if the given object is a python string
bool string::accepts(handle<> o)
{ return PyString_Check(o.get()); }
// Return the length of the string.
std::size_t string::size() const
{
int size = PyString_GET_SIZE(get());
assert(size >= 0);
return static_cast<std::size_t>(size);
}
// Returns a null-terminated representation of the contents of string.
// The pointer refers to the internal buffer of string, not a copy.
// The data must not be modified in any way. It must not be de-allocated.
const char* string::c_str() const
{ return PyString_AS_STRING(get()); }
void string::intern()
{ // UNTESTED!!
*this = string(handle<>(borrowed(PyString_InternFromString(c_str()))));
}
string& string::operator*=(unsigned int repeat_count)
{
*this = string(handle<>(PySequence_Repeat(get(), repeat_count)));
return *this;
}
dictionary_base::dictionary_base(handle<> p)
: objects_base(p)
{
assert(accepts(p));
if (!accepts(p))
{
PyErr_SetString(PyExc_TypeError, p->ob_type->tp_name);
throw_error_already_set();
}
}
dictionary_base::dictionary_base()
: objects_base(handle<>(PyDict_New())) {}
PyTypeObject* dictionary_base::type_obj()
{ return &PyDict_Type; }
bool dictionary_base::accepts(handle<> p)
{ return PyDict_Check(p.get()); }
void dictionary_base::clear()
{ PyDict_Clear(get()); }
const handle<>& dictionary_proxy::operator=(const handle<>& rhs)
{
if (PyDict_SetItem(m_dict.get(), m_key.get(), rhs.get()) == -1)
throw_error_already_set();
return rhs;
}
dictionary_proxy::operator handle<>() const
{
return handle<>(
m_dict->ob_type->tp_as_mapping->mp_subscript(m_dict.get(), m_key.get()));
}
dictionary_proxy::dictionary_proxy(const handle<>& dict, const handle<>& key)
: m_dict(dict), m_key(key) {}
dictionary_proxy dictionary_base::operator[](handle<> key)
{ return proxy(reference(), key); }
handle<> dictionary_base::operator[](handle<> key) const {
// An odd MSVC bug causes the ".operator Ptr()" to be needed
return proxy(reference(), key).operator handle<>();
}
handle<> dictionary_base::get_item(const handle<>& key) const
{
return get_item(key, handle<>());
}
handle<> dictionary_base::get_item(const handle<>& key, const handle<>& default_) const
{
PyObject* value_or_null = PyDict_GetItem(get(), key.get());
if (value_or_null == 0 && !PyErr_Occurred())
return default_;
else
return handle<>(borrowed(value_or_null)); // Will throw if there was another error
}
void dictionary_base::set_item(const handle<>& key, const handle<>& value)
{
if (PyDict_SetItem(get(), key.get(), value.get()) == -1)
throw_error_already_set();
}
void dictionary_base::erase(handle<> key) {
if (PyDict_DelItem(get(), key.get()) == -1)
throw_error_already_set();
}
list dictionary_base::items() const { return list(handle<>(PyDict_Items(get()))); }
list dictionary_base::keys() const { return list(handle<>(PyDict_Keys(get()))); }
list dictionary_base::values() const { return list(handle<>(PyDict_Values(get()))); }
std::size_t dictionary_base::size() const { return static_cast<std::size_t>(PyDict_Size(get())); }
string operator+(string x, string y)
{
PyObject* io_string = incref(x.get());
PyString_Concat(&io_string, y.get());
return string(handle<>(io_string));
}
string& string::operator+=(const string& rhs)
{
return *this = *this + rhs;
}
string& string::operator+=(const char* y)
{
return *this += string(y);
}
string operator%(const string& format, const tuple& args)
{
return string(handle<>(PyString_Format(format.get(), args.reference().get())));
}
string operator+(string x, const char* y)
{
return x + string(y);
}
string operator+(const char* x, string y)
{
return string(x) + y;
}
tuple operator+(const tuple& x, const tuple& y)
{
tuple result(x.size() + y.size());
for (std::size_t xi = 0; xi < x.size(); ++xi)
result.set_item(xi, x[xi]);
for (std::size_t yi = 0; yi < y.size(); ++yi)
result.set_item(yi + x.size(), y[yi]);
return result;
}
}} // namespace boost::python

380
src/str.cpp Normal file
View File

@@ -0,0 +1,380 @@
#include <boost/python/str.hpp>
#include <boost/python/extract.hpp>
namespace boost { namespace python {
BOOST_PYTHON_DECL detail::new_reference str::call(object const& arg)
{
return (detail::new_reference)PyObject_CallFunction(
(PyObject*)&PyString_Type, "(O)",
arg.ptr());
}
BOOST_PYTHON_DECL str::str(const char* s)
: object(detail::new_reference(PyString_FromString(s)))
{}
BOOST_PYTHON_DECL str::str(object_cref other)
: object(str::call(other))
{}
namespace
{
// When returning str objects from methods, it may turn out that the
// derived class is returning something else, perhaps something not
// even derived from str. Since it is generally harmless for a
// Boost.Python wrapper object to hold an object of a different
// type, and because calling str() with an object may in fact
// perform a conversion, the least-bad alternative is to assume that
// we have a Python str object and stuff it into the str result.
str assume_str(object const& o)
{
return str(detail::borrowed_reference(o.ptr()));
}
}
BOOST_PYTHON_DECL str str::capitalize() const
{
return assume_str(this->attr("capitalize")());
}
BOOST_PYTHON_DECL str str::center(object_cref width) const
{
return assume_str(
this->attr("center")(width)
);
}
BOOST_PYTHON_DECL long str::count(object_cref sub) const
{
return extract<long>(this->attr("count")(sub));
}
BOOST_PYTHON_DECL long str::count(object_cref sub, object_cref start) const
{
return extract<long>(this->attr("count")(sub,start));
}
BOOST_PYTHON_DECL long str::count(object_cref sub, object_cref start, object_cref end) const
{
return extract<long>(this->attr("count")(sub,start,end));
}
BOOST_PYTHON_DECL str str::decode() const
{
return assume_str(this->attr("decode")());
}
BOOST_PYTHON_DECL str str::decode(object_cref encoding) const
{
return assume_str(this->attr("decode")(encoding));
}
BOOST_PYTHON_DECL str str::decode(object_cref encoding, object_cref errors) const
{
return assume_str(this->attr("decode")(encoding,errors));
}
BOOST_PYTHON_DECL str str::encode() const
{
return assume_str(this->attr("encode")());
}
BOOST_PYTHON_DECL str str::encode(object_cref encoding) const
{
return assume_str(this->attr("encode")(encoding));
}
BOOST_PYTHON_DECL str str::encode(object_cref encoding, object_cref errors) const
{
return assume_str(this->attr("encode")(encoding,errors));
}
BOOST_PYTHON_DECL bool str::endswith(object_cref suffix) const
{
bool result = PyInt_AsLong(this->attr("endswith")(suffix).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL str str::expandtabs() const
{
return assume_str(this->attr("expandtabs")());
}
BOOST_PYTHON_DECL str str::expandtabs(object_cref tabsize) const
{
return assume_str(this->attr("expandtabs")(tabsize));
}
BOOST_PYTHON_DECL long str::find(object_cref sub) const
{
long result = PyInt_AsLong(this->attr("find")(sub).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::find(object_cref sub, object_cref start) const
{
long result = PyInt_AsLong(this->attr("find")(sub,start).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::find(object_cref sub, object_cref start, object_cref end) const
{
long result = PyInt_AsLong(this->attr("find")(sub,start,end).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::index(object_cref sub) const
{
long result = PyInt_AsLong(this->attr("index")(sub).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::index(object_cref sub, object_cref start) const
{
long result = PyInt_AsLong(this->attr("index")(sub,start).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::index(object_cref sub, object_cref start, object_cref end) const
{
long result = PyInt_AsLong(this->attr("index")(sub,start,end).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL bool str::isalnum() const
{
bool result = PyInt_AsLong(this->attr("isalnum")().ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL bool str::isalpha() const
{
bool result = PyInt_AsLong(this->attr("isalpha")().ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL bool str::isdigit() const
{
bool result = PyInt_AsLong(this->attr("isdigit")().ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL bool str::islower() const
{
bool result = PyInt_AsLong(this->attr("islower")().ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL bool str::isspace() const
{
bool result = PyInt_AsLong(this->attr("isspace")().ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL bool str::istitle() const
{
bool result = PyInt_AsLong(this->attr("istitle")().ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL bool str::isupper() const
{
bool result = PyInt_AsLong(this->attr("isupper")().ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL str str::join(object_cref sequence) const
{
return assume_str(this->attr("join")(sequence));
}
BOOST_PYTHON_DECL str str::ljust(object_cref width) const
{
return assume_str(this->attr("ljust")(width));
}
BOOST_PYTHON_DECL str str::lower() const
{
return assume_str(this->attr("lower")());
}
BOOST_PYTHON_DECL str str::lstrip() const
{
return assume_str(this->attr("lstrip")());
}
BOOST_PYTHON_DECL str str::replace(object_cref old, object_cref new_) const
{
return assume_str(this->attr("replace")(old,new_));
}
BOOST_PYTHON_DECL str str::replace(object_cref old, object_cref new_, object_cref maxsplit) const {
return assume_str(this->attr("replace")(old,new_,maxsplit));
}
BOOST_PYTHON_DECL long str::rfind(object_cref sub) const
{
long result = PyInt_AsLong(this->attr("rfind")(sub).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::rfind(object_cref sub, object_cref start) const
{
long result = PyInt_AsLong(this->attr("rfind")(sub,start).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::rfind(object_cref sub, object_cref start, object_cref end) const
{
long result = PyInt_AsLong(this->attr("rfind")(sub,start,end).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::rindex(object_cref sub) const
{
long result = PyInt_AsLong(this->attr("rindex")(sub).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL long str::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;
}
BOOST_PYTHON_DECL long str::rindex(object_cref sub, object_cref start, object_cref end) const
{
long result = PyInt_AsLong(this->attr("rindex")(sub,start,end).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL str str::rjust(object_cref width) const
{
return assume_str(this->attr("rjust")(width));
}
BOOST_PYTHON_DECL str str::rstrip() const
{
return assume_str(this->attr("rstrip")());
}
BOOST_PYTHON_DECL list str::split() const
{
return list(this->attr("split")());
}
BOOST_PYTHON_DECL list str::split(object_cref sep) const
{
return list(this->attr("split")(sep));
}
BOOST_PYTHON_DECL list str::split(object_cref sep, object_cref maxsplit) const
{
return list(this->attr("split")(sep,maxsplit));
}
BOOST_PYTHON_DECL list str::splitlines() const
{
return list(this->attr("splitlines")());
}
BOOST_PYTHON_DECL list str::splitlines(object_cref keepends) const
{
return list(this->attr("splitlines")(keepends));
}
BOOST_PYTHON_DECL bool str::startswith(object_cref prefix) const
{
bool result = PyInt_AsLong(this->attr("startswith")(prefix).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL bool str::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;
}
BOOST_PYTHON_DECL bool str::startswith(object_cref prefix, object_cref start, object_cref end) const
{
bool result = PyInt_AsLong(this->attr("startswith")(prefix,start,end).ptr());
if (PyErr_Occurred())
throw_error_already_set();
return result;
}
BOOST_PYTHON_DECL str str::strip() const
{
return assume_str(this->attr("strip")());
}
BOOST_PYTHON_DECL str str::swapcase() const
{
return assume_str(this->attr("swapcase")());
}
BOOST_PYTHON_DECL str str::title() const
{
return assume_str(this->attr("title")());
}
BOOST_PYTHON_DECL str str::translate(object_cref table) const
{
return assume_str(this->attr("translate")(table));
}
BOOST_PYTHON_DECL str str::translate(object_cref table, object_cref deletechars) const
{
return assume_str(this->attr("translate")(table,deletechars));
}
BOOST_PYTHON_DECL str str::upper() const
{
return assume_str(this->attr("upper")());
}
}} // namespace boost::python

20
src/tuple.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include <boost/python/tuple.hpp>
namespace boost { namespace python {
BOOST_PYTHON_DECL detail::new_reference tuple::call(object const& arg)
{
return (detail::new_reference)PyObject_CallFunction(
(PyObject*)&PyTuple_Type, "(O)",
arg.ptr());
}
BOOST_PYTHON_DECL tuple::tuple()
: object(detail::new_reference(PyTuple_New(0)))
{}
BOOST_PYTHON_DECL tuple::tuple(object_cref sequence)
: object(tuple::call(sequence))
{}
}} // namespace boost::python