AppSandbox.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <Python.h>
00027 #endif
00028
00029 #include <Base/Console.h>
00030 #include <App/DocumentPy.h>
00031 #include <App/DocumentObjectPy.h>
00032 #include <CXX/Extensions.hxx>
00033 #include <CXX/Objects.hxx>
00034
00035 #include "DocumentThread.h"
00036 #include "DocumentProtector.h"
00037 #include "DocumentProtectorPy.h"
00038
00039 namespace Sandbox {
00040 class PythonBaseClass : public Py::PythonClass< PythonBaseClass >
00041 {
00042 public:
00043 PythonBaseClass( Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds )
00044 : Py::PythonClass< PythonBaseClass >::PythonClass( self, args, kwds )
00045 , m_value( "default value" )
00046 {
00047 std::cout << "PythonBaseClass c'tor Called with " << args.length() << " normal arguments." << std::endl;
00048 Py::List names( kwds.keys() );
00049 std::cout << "and with " << names.length() << " keyword arguments:" << std::endl;
00050 for( Py::List::size_type i=0; i< names.length(); i++ )
00051 {
00052 Py::String name( names[i] );
00053 std::cout << " " << name << std::endl;
00054 }
00055 }
00056
00057 virtual ~PythonBaseClass()
00058 {
00059 std::cout << "~PythonBaseClass." << std::endl;
00060 }
00061
00062 static void init_type(void)
00063 {
00064 behaviors().name( "PythonBaseClass" );
00065 behaviors().doc( "documentation for PythonBaseClass class" );
00066 behaviors().supportGetattro();
00067 behaviors().supportSetattro();
00068
00069 PYCXX_ADD_NOARGS_METHOD( func_noargs, PythonBaseClass_func_noargs, "docs for PythonBaseClass_func_noargs" );
00070 PYCXX_ADD_VARARGS_METHOD( func_varargs, PythonBaseClass_func_varargs, "docs for PythonBaseClass_func_varargs" );
00071 PYCXX_ADD_KEYWORDS_METHOD( func_keyword, PythonBaseClass_func_keyword, "docs for PythonBaseClass_func_keyword" );
00072
00073 PYCXX_ADD_NOARGS_METHOD( func_noargs_raise_exception, PythonBaseClass_func_noargs_raise_exception,
00074 "docs for PythonBaseClass_func_noargs_raise_exception" );
00075
00076
00077 behaviors().readyType();
00078 }
00079
00080 Py::Object PythonBaseClass_func_noargs( void )
00081 {
00082 std::cout << "PythonBaseClass_func_noargs Called." << std::endl;
00083 std::cout << "value ref count " << m_value.reference_count() << std::endl;
00084 return Py::None();
00085 }
00086 PYCXX_NOARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_noargs )
00087
00088 Py::Object PythonBaseClass_func_varargs( const Py::Tuple &args )
00089 {
00090 std::cout << "PythonBaseClass_func_varargs Called with " << args.length() << " normal arguments." << std::endl;
00091 return Py::None();
00092 }
00093 PYCXX_VARARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_varargs )
00094
00095 Py::Object PythonBaseClass_func_keyword( const Py::Tuple &args, const Py::Dict &kwds )
00096 {
00097 std::cout << "PythonBaseClass_func_keyword Called with " << args.length() << " normal arguments." << std::endl;
00098 Py::List names( kwds.keys() );
00099 std::cout << "and with " << names.length() << " keyword arguments:" << std::endl;
00100 for( Py::List::size_type i=0; i< names.length(); i++ )
00101 {
00102 Py::String name( names[i] );
00103 std::cout << " " << name << std::endl;
00104 }
00105 return Py::None();
00106 }
00107 PYCXX_KEYWORDS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_keyword )
00108
00109 Py::Object PythonBaseClass_func_noargs_raise_exception( void )
00110 {
00111 std::cout << "PythonBaseClass_func_noargs_raise_exception Called." << std::endl;
00112 throw Py::RuntimeError( "its an error" );
00113 return Py::None();
00114 }
00115 PYCXX_NOARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_noargs_raise_exception )
00116
00117 Py::Object getattro( const Py::String &name_ )
00118 {
00119 std::string name( name_.as_std_string( "utf-8" ) );
00120
00121 if( name == "value" )
00122 {
00123 return m_value;
00124 }
00125 else
00126 {
00127 return genericGetAttro( name_ );
00128 }
00129 }
00130
00131 int setattro( const Py::String &name_, const Py::Object &value )
00132 {
00133 std::string name( name_.as_std_string( "utf-8" ) );
00134
00135 if( name == "value" )
00136 {
00137 m_value = value;
00138 return 0;
00139 }
00140 else
00141 {
00142 return genericSetAttro( name_, value );
00143 }
00144 }
00145
00146 Py::String m_value;
00147 };
00148
00149 }
00150
00151
00152
00153 class SandboxModule : public Py::ExtensionModule<SandboxModule>
00154 {
00155
00156 public:
00157 SandboxModule() : Py::ExtensionModule<SandboxModule>("Sandbox")
00158 {
00159 Sandbox::PythonBaseClass::init_type();
00160 Sandbox::DocumentProtectorPy::init_type();
00161 add_varargs_method("DocumentProtector",
00162 &SandboxModule::new_DocumentProtector,
00163 "DocumentProtector(Document)");
00164 Sandbox::DocumentObjectProtectorPy::init_type();
00165 add_varargs_method("DocumentObjectProtector",
00166 &SandboxModule::new_DocumentObjectProtector,
00167 "DocumentObjectProtector(DocumentObject)");
00168 initialize("This module is the Sandbox module");
00169
00170 Py::Dict d( moduleDictionary() );
00171 Py::Object x( Sandbox::PythonBaseClass::type() );
00172 d["PythonBaseClass"] = x;
00173 }
00174
00175 virtual ~SandboxModule() {}
00176
00177 private:
00178 Py::Object new_DocumentProtector(const Py::Tuple& args)
00179 {
00180 PyObject* o;
00181 if (!PyArg_ParseTuple(args.ptr(), "O!",&(App::DocumentPy::Type), &o))
00182 throw Py::Exception();
00183 App::DocumentPy* doc = static_cast<App::DocumentPy*>(o);
00184 return Py::asObject(new Sandbox::DocumentProtectorPy(doc));
00185 }
00186 Py::Object new_DocumentObjectProtector(const Py::Tuple& args)
00187 {
00188 PyObject* o;
00189 if (!PyArg_ParseTuple(args.ptr(), "O!",&(App::DocumentObjectPy::Type), &o))
00190 throw Py::Exception();
00191 App::DocumentObjectPy* obj = static_cast<App::DocumentObjectPy*>(o);
00192 return Py::asObject(new Sandbox::DocumentObjectProtectorPy(obj));
00193 }
00194 };
00195
00196
00197
00198 extern "C" {
00199 void SandboxAppExport initSandbox() {
00200
00201 Sandbox::DocumentProtector ::init();
00202 Sandbox::SandboxObject ::init();
00203
00204
00205
00206 (void)new SandboxModule;
00207 Base::Console().Log("Loading Sandbox module... done\n");
00208 }
00209
00210 }