/
AppSandbox.cpp
252 lines (222 loc) · 9.23 KB
/
AppSandbox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/***************************************************************************
* Copyright (c) 2009 Werner Mayer <wmayer@users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <Python.h>
#endif
#include <Base/Console.h>
#include <App/DocumentPy.h>
#include <App/DocumentObjectPy.h>
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>
#include "DocumentThread.h"
#include "DocumentProtector.h"
#include "DocumentProtectorPy.h"
namespace Sandbox {
class PythonBaseClass : public Py::PythonClass< PythonBaseClass >
{
public:
PythonBaseClass( Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds )
: Py::PythonClass< PythonBaseClass >::PythonClass( self, args, kwds )
, m_value( "default value" )
{
std::cout << "PythonBaseClass c'tor Called with " << args.length() << " normal arguments." << std::endl;
Py::List names( kwds.keys() );
std::cout << "and with " << names.length() << " keyword arguments:" << std::endl;
for( Py::List::size_type i=0; i< names.length(); i++ )
{
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()
{
std::cout << "~PythonBaseClass." << std::endl;
}
static void init_type(void)
{
behaviors().name( "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" );
PYCXX_ADD_KEYWORDS_METHOD( func_keyword, PythonBaseClass_func_keyword, "docs for PythonBaseClass_func_keyword" );
PYCXX_ADD_NOARGS_METHOD( func_noargs_raise_exception, PythonBaseClass_func_noargs_raise_exception,
"docs for PythonBaseClass_func_noargs_raise_exception" );
// Call to make the type ready for use
behaviors().readyType();
}
Py::Object PythonBaseClass_func_noargs( void )
{
std::cout << "PythonBaseClass_func_noargs Called." << std::endl;
std::cout << "value ref count " << m_value.reference_count() << std::endl;
return Py::None();
}
PYCXX_NOARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_noargs )
Py::Object PythonBaseClass_func_varargs( const Py::Tuple &args )
{
std::cout << "PythonBaseClass_func_varargs Called with " << args.length() << " normal arguments." << std::endl;
return Py::None();
}
PYCXX_VARARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_varargs )
Py::Object PythonBaseClass_func_keyword( const Py::Tuple &args, const Py::Dict &kwds )
{
std::cout << "PythonBaseClass_func_keyword Called with " << args.length() << " normal arguments." << std::endl;
Py::List names( kwds.keys() );
std::cout << "and with " << names.length() << " keyword arguments:" << std::endl;
for( Py::List::size_type i=0; i< names.length(); i++ )
{
Py::String name( names[i] );
std::cout << " " << name << std::endl;
}
return Py::None();
}
PYCXX_KEYWORDS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_keyword )
Py::Object PythonBaseClass_func_noargs_raise_exception( void )
{
std::cout << "PythonBaseClass_func_noargs_raise_exception Called." << std::endl;
throw Py::RuntimeError( "its an error" );
return Py::None();
}
PYCXX_NOARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_noargs_raise_exception )
Py::Object getattro( const Py::String &name_ )
{
std::string name( name_.as_std_string( "utf-8" ) );
if( name == "value" )
{
return m_value;
}
else
{
return genericGetAttro( name_ );
}
}
int setattro( const Py::String &name_, const Py::Object &value )
{
std::string name( name_.as_std_string( "utf-8" ) );
if( name == "value" )
{
m_value = value;
return 0;
}
else
{
return genericSetAttro( name_, value );
}
}
virtual int sequence_length()
{
// len(x)
return m_array.size();
}
virtual Py::Object sequence_concat(const Py::Object &)
{
// x + y
throw Py::NotImplementedError("not yet implemented");
}
virtual Py::Object sequence_repeat(Py_ssize_t)
{
// 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, Py_ssize_t)
{
// 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, Py_ssize_t, const Py::Object &)
{
// x[0:3] = y
throw Py::NotImplementedError("not yet implemented");
}
Py::String m_value;
std::vector<Py::Object> m_array;
};
/* module functions */
class Module : public Py::ExtensionModule<Module>
{
public:
Module() : Py::ExtensionModule<Module>("Sandbox")
{
Sandbox::PythonBaseClass::init_type();
Sandbox::DocumentProtectorPy::init_type();
add_varargs_method("DocumentProtector",
&Module::new_DocumentProtector,
"DocumentProtector(Document)");
Sandbox::DocumentObjectProtectorPy::init_type();
add_varargs_method("DocumentObjectProtector",
&Module::new_DocumentObjectProtector,
"DocumentObjectProtector(DocumentObject)");
initialize("This module is the Sandbox module"); // register with Python
Py::Dict d( moduleDictionary() );
Py::Object x( Sandbox::PythonBaseClass::type() );
d["PythonBaseClass"] = x;
}
virtual ~Module() {}
private:
Py::Object new_DocumentProtector(const Py::Tuple& args)
{
PyObject* o;
if (!PyArg_ParseTuple(args.ptr(), "O!",&(App::DocumentPy::Type), &o))
throw Py::Exception();
App::DocumentPy* doc = static_cast<App::DocumentPy*>(o);
return Py::asObject(new Sandbox::DocumentProtectorPy(doc));
}
Py::Object new_DocumentObjectProtector(const Py::Tuple& args)
{
PyObject* o;
if (!PyArg_ParseTuple(args.ptr(), "O!",&(App::DocumentObjectPy::Type), &o))
throw Py::Exception();
App::DocumentObjectPy* obj = static_cast<App::DocumentObjectPy*>(o);
return Py::asObject(new Sandbox::DocumentObjectProtectorPy(obj));
}
};
} // namespace Sandbox
/* Python entry */
PyMODINIT_FUNC initSandbox() {
Sandbox::DocumentProtector ::init();
Sandbox::SandboxObject ::init();
// the following constructor call registers our extension module
// with the Python runtime system
(void)new Sandbox::Module;
Base::Console().Log("Loading Sandbox module... done\n");
}