Skip to content

Commit

Permalink
+ example of sequence protocol of Python type
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Aug 2, 2016
1 parent a539cec commit dc25c73
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Mod/Sandbox/App/AppSandbox.cpp
Expand Up @@ -52,6 +52,9 @@ class PythonBaseClass : public Py::PythonClass< PythonBaseClass >
Py::String name( names[i] );
std::cout << " " << name << std::endl;
}
m_array.push_back(Py::Long(2));
m_array.push_back(Py::Float(3.0));
m_array.push_back(Py::String("4.0"));
}

virtual ~PythonBaseClass()
Expand All @@ -65,6 +68,7 @@ class PythonBaseClass : public Py::PythonClass< PythonBaseClass >
behaviors().doc( "documentation for PythonBaseClass class" );
behaviors().supportGetattro();
behaviors().supportSetattro();
behaviors().supportSequenceType();

PYCXX_ADD_NOARGS_METHOD( func_noargs, PythonBaseClass_func_noargs, "docs for PythonBaseClass_func_noargs" );
PYCXX_ADD_VARARGS_METHOD( func_varargs, PythonBaseClass_func_varargs, "docs for PythonBaseClass_func_varargs" );
Expand Down Expand Up @@ -142,8 +146,49 @@ class PythonBaseClass : public Py::PythonClass< PythonBaseClass >
return genericSetAttro( name_, value );
}
}
virtual int sequence_length()
{
// len(x)
return m_array.size();
}
virtual Py::Object sequence_concat(const Py::Object & i)
{
// x + y
throw Py::NotImplementedError("not yet implemented");
}
virtual Py::Object sequence_repeat(Py_ssize_t i)
{
// x * 3
throw Py::NotImplementedError("not yet implemented");
}
virtual Py::Object sequence_item(Py_ssize_t i)
{
// x[0]
if (i >= static_cast<Py_ssize_t>(m_array.size()))
throw Py::IndexError("index out of range");
return m_array[i];
}
virtual Py::Object sequence_slice(Py_ssize_t i, Py_ssize_t j)
{
// x[0:3]
throw Py::NotImplementedError("not yet implemented");
}
virtual int sequence_ass_item(Py_ssize_t i, const Py::Object & o)
{
// x[0] = y
if (i >= static_cast<Py_ssize_t>(m_array.size()))
throw Py::IndexError("index out of range");
m_array[i] = o;
return 0;
}
virtual int sequence_ass_slice(Py_ssize_t i, Py_ssize_t j, const Py::Object & o)
{
// x[0:3] = y
throw Py::NotImplementedError("not yet implemented");
}

Py::String m_value;
std::vector<Py::Object> m_array;
};

/* module functions */
Expand Down

0 comments on commit dc25c73

Please sign in to comment.