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

const-ify ClassBase::getattr()

Add repr() function to Class<T>
Add to_python/from_python conversions for PyPtr<T>
Standardize set_item/get_item interfaces (instead of proxies) for Dict and List
Add Reprable<> template to newtypes.h
Fix a bug wherein the __module__ attribute would be lost for classes that have a default virtual function implementation.


[SVN r8091]
This commit is contained in:
Dave Abrahams
2000-11-01 02:30:37 +00:00
parent 7cf495874d
commit 2ba0b22831
7 changed files with 126 additions and 16 deletions

View File

@@ -210,7 +210,8 @@ bool Dict::accepts(Ptr p)
void Dict::clear()
{ PyDict_Clear(get()); }
const Ptr& Dict::Proxy::operator=(const Ptr& rhs) {
const Ptr& Dict::Proxy::operator=(const Ptr& rhs)
{
if (PyDict_SetItem(m_dict.get(), m_key.get(), rhs.get()) == -1)
throw ErrorAlreadySet();
return rhs;
@@ -234,7 +235,7 @@ Ptr Dict::operator[](Ptr key) const {
}
Ptr Dict::get_item(const Ptr& key, const Ptr& default_ /* = Ptr() */)
Ptr Dict::get_item(const Ptr& key, const Ptr& default_ /* = Ptr() */) const
{
PyObject* value_or_null = PyDict_GetItem(get(), key.get());
if (value_or_null == 0 && !PyErr_Occurred())
@@ -243,6 +244,17 @@ Ptr Dict::get_item(const Ptr& key, const Ptr& default_ /* = Ptr() */)
return Ptr(value_or_null, Ptr::borrowed); // Will throw if there was another error
}
void Dict::set_item(const Ptr& key, const Ptr& value)
{
if (PyDict_SetItem(get(), key.get(), value.get()) == -1)
throw ErrorAlreadySet();
}
void Dict::set_item(const Object& key, const Ptr& value)
{
set_item(key.reference(), value);
}
void Dict::erase(Ptr key) {
if (PyDict_DelItem(get(), key.get()) == -1)
throw ErrorAlreadySet();
@@ -264,7 +276,7 @@ Ptr Dict::operator[](const Object& key) const
return this->operator[](key.reference());
}
Ptr Dict::get_item(const Object& key, Ptr default_)
Ptr Dict::get_item(const Object& key, Ptr default_) const
{
return this->get_item(key.reference(), default_);
}
@@ -402,10 +414,7 @@ Tuple List::as_tuple() const
const Ptr& List::Proxy::operator=(const Ptr& rhs)
{
int result = PyList_SetItem(m_list.get(), m_index, rhs.get());
if (result == -1)
throw ErrorAlreadySet();
Py_INCREF(rhs.get());
m_list.set_item(m_index, rhs);
return rhs;
}
@@ -414,6 +423,24 @@ List::Proxy::operator Ptr() const
return Ptr(PyList_GetItem(m_list.get(), m_index), Ptr::borrowed);
}
Ptr List::get_item(std::size_t pos) const
{
return Ptr(PyList_GetItem(this->get(), pos), Ptr::borrowed);
}
void List::set_item(std::size_t pos, Ptr rhs)
{
int result = PyList_SetItem(this->get(), pos, rhs.get());
if (result == -1)
throw ErrorAlreadySet();
Py_INCREF(rhs.get());
}
void List::set_item(std::size_t pos, Object rhs)
{
this->set_item(pos, rhs.reference());
}
List::Proxy::Proxy(const Ptr& list, std::size_t index)
: m_list(list), m_index(index)
{