ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TSetItemHolder.cxx
Go to the documentation of this file.
1 // Author: Wim Lavrijsen, Oct 2005
2 
3 // Bindings
4 #include "PyROOT.h"
5 #include "TSetItemHolder.h"
6 #include "Executors.h"
7 
8 
9 //- protected members --------------------------------------------------------
11 {
12 // basic call will do
13  if ( ! TMethodHolder::InitExecutor_( executor ) )
14  return kFALSE;
15 
16 // check to make sure we're dealing with a RefExecutor
17  if ( ! dynamic_cast< TRefExecutor* >( executor ) ) {
18  PyErr_Format( PyExc_NotImplementedError,
19  "no __setitem__ handler for return type (%s)",
20  this->GetReturnTypeName().c_str() );
21  return kFALSE;
22  }
23 
24  return kTRUE;
25 }
26 
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// Prepare executor with a buffer for the return value.
30 
32  ObjectProxy*& self, PyObject* args, PyObject* kwds )
33 {
34  int nArgs = PyTuple_GET_SIZE( args );
35  if ( nArgs <= 1 ) {
36  PyErr_SetString( PyExc_TypeError, "insufficient arguments to __setitem__" );
37  return 0;
38  }
39 
40 // strip the last element of args to be used on return
41  ((TRefExecutor*)this->GetExecutor())->SetAssignable( PyTuple_GET_ITEM( args, nArgs - 1 ) );
42  PyObject* subset = PyTuple_GetSlice( args, 0, nArgs - 1 );
43 
44 // see whether any of the arguments is a tuple itself
45  Py_ssize_t realsize = 0;
46  for ( int i = 0; i < nArgs - 1; ++i ) {
47  PyObject* item = PyTuple_GetItem( subset, i );
48  realsize += PyTuple_Check( item ) ? PyTuple_GET_SIZE( item ) : 1;
49  }
50 
51 // unroll any tuples, if present in the arguments
52  PyObject* unrolled = 0;
53  if ( realsize != nArgs - 1 ) {
54  unrolled = PyTuple_New( realsize );
55 
56  int current = 0;
57  for ( int i = 0; i < nArgs - 1; ++i, ++current ) {
58  PyObject* item = PyTuple_GetItem( subset, i );
59  if ( PyTuple_Check( item ) ) {
60  for ( int j = 0; j < PyTuple_GET_SIZE( item ); ++j, ++current ) {
61  PyObject* subitem = PyTuple_GetItem( item, j );
62  Py_INCREF( subitem );
63  PyTuple_SetItem( unrolled, current, subitem );
64  }
65  } else {
66  Py_INCREF( item );
67  PyTuple_SetItem( unrolled, current, item );
68  }
69  }
70  }
71 
72 // actual call into C++
73  PyObject* result = TMethodHolder::PreProcessArgs( self, unrolled ? unrolled : subset, kwds );
74  Py_XDECREF( unrolled );
75  Py_DECREF( subset );
76  return result;
77 }
const char * current
Definition: demos.C:12
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual PyObject * PreProcessArgs(ObjectProxy *&self, PyObject *args, PyObject *kwds)
Prepare executor with a buffer for the return value.
std::string GetReturnTypeName()
virtual Bool_t InitExecutor_(TExecutor *&, TCallContext *ctxt=0)
install executor conform to the return type
virtual Bool_t InitExecutor_(TExecutor *&, TCallContext *ctxt=0)
install executor conform to the return type
virtual PyObject * PreProcessArgs(ObjectProxy *&self, PyObject *args, PyObject *kwds)
verify existence of self, return if ok
int Py_ssize_t
Definition: PyROOT.h:154
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
_object PyObject
Definition: TPyArg.h:22