My Project  debian-1:4.1.1-p2+ds-4build1
Data Structures | Public Types | Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes
PythonObject Class Reference

Data Structures

struct  sequence_tag
 

Public Types

typedef PyObject * ptr_type
 

Public Member Functions

 PythonObject ()
 
 PythonObject (ptr_type ptr)
 
ptr_type check_context (ptr_type ptr) const
 
self operator() (int op) const
 Unary operations. More...
 
self operator() (int op, const self &arg) const
 Binary and n-ary operations. More...
 
self operator() (int op, const self &arg1, const self &arg2) const
 Ternary operations. More...
 
self operator[] (const self &idx) const
 Get item. More...
 
self operator[] (long idx) const
 
 operator const ptr_type () const
 Get actual PyObject*. More...
 
char * repr () const
 Get representative as C-style string. More...
 
char * str () const
 Extract C-style string. More...
 
Py_ssize_t size () const
 
BOOLEAN assign_to (leftv result)
 
void import_as (const char *name) const
 
int compare (int op, const self &arg) const
 
self attr (const self &arg) const
 
self del_attr (const self &arg) const
 

Protected Member Functions

self args2list (const self &args) const
 
BOOLEAN handle_exception () const
 
void append_iter (self iterator)
 
int py_opid (int op) const
 

Private Types

typedef PythonObject self
 

Private Member Functions

BOOLEAN none_to (leftv result) const
 
BOOLEAN python_to (leftv result) const
 

Private Attributes

ptr_type m_ptr
 The actual pointer. More...
 

Detailed Description

This class defines an interface for calling PyObject from Singular.

Note
This class does not take care of the memory mangement, this is done in the blackbox routines.

Definition at line 107 of file pyobject.cc.


Data Structure Documentation

◆ PythonObject::sequence_tag

struct PythonObject::sequence_tag

Definition at line 113 of file pyobject.cc.

Member Typedef Documentation

◆ ptr_type

typedef PyObject* PythonObject::ptr_type

Definition at line 112 of file pyobject.cc.

◆ self

Definition at line 109 of file pyobject.cc.

Constructor & Destructor Documentation

◆ PythonObject() [1/2]

PythonObject::PythonObject ( )
inline

Definition at line 115 of file pyobject.cc.

115 : m_ptr(Py_None) { }

◆ PythonObject() [2/2]

PythonObject::PythonObject ( ptr_type  ptr)
inline

Definition at line 116 of file pyobject.cc.

116  : m_ptr(ptr) {
117  if (!ptr && handle_exception()) m_ptr = Py_None;
118  }

Member Function Documentation

◆ append_iter()

void PythonObject::append_iter ( self  iterator)
inlineprotected

Definition at line 252 of file pyobject.cc.

252  {
253  ptr_type item;
254  while ((item = PyIter_Next(iterator))) {
255  PyList_Append(*this, item);
256  Py_DECREF(item);
257  }
258  }

◆ args2list()

self PythonObject::args2list ( const self args) const
inlineprotected

Definition at line 224 of file pyobject.cc.

225  {
226  self pylist(PyList_New(0));
227  PyList_Append(pylist, *this);
228  if(PyTuple_Check(args)) pylist.append_iter(PyObject_GetIter(args));
229  else PyList_Append(pylist, args);
230 
231  return pylist;
232  }

◆ assign_to()

BOOLEAN PythonObject::assign_to ( leftv  result)
inline

Definition at line 189 of file pyobject.cc.

190  {
191  return (m_ptr? (m_ptr == Py_None? none_to(result): python_to(result)): TRUE);
192  }

◆ attr()

self PythonObject::attr ( const self arg) const
inline

Definition at line 211 of file pyobject.cc.

211 { return PyObject_GetAttr(*this, arg); }

◆ check_context()

ptr_type PythonObject::check_context ( ptr_type  ptr) const
inline

Definition at line 120 of file pyobject.cc.

120  {
121  if(ptr) sync_contexts();
122  return ptr;
123  }

◆ compare()

int PythonObject::compare ( int  op,
const self arg 
) const
inline

Definition at line 207 of file pyobject.cc.

208  { return PyObject_RichCompareBool(*this, arg, py_opid(op)); }

◆ del_attr()

self PythonObject::del_attr ( const self arg) const
inline

Definition at line 213 of file pyobject.cc.

214  {
215  if (!PyObject_HasAttr(*this, arg))
216  Werror("Cannot delete attribute %s.", arg.repr());
217  else
218  PyObject_DelAttr(*this, arg);
219 
220  return self();
221  }

◆ handle_exception()

BOOLEAN PythonObject::handle_exception ( ) const
inlineprotected

Definition at line 234 of file pyobject.cc.

234  {
235 
236  if(!PyErr_Occurred()) return FALSE;
237 
238  PyObject *pType, *pMessage, *pTraceback;
239  PyErr_Fetch(&pType, &pMessage, &pTraceback);
240 
241  WerrorS("pyobject error occurred");
242  WerrorS(PyString_AsString(pMessage));
243 
244  Py_XDECREF(pType);
245  Py_XDECREF(pMessage);
246  Py_XDECREF(pTraceback);
247 
248  PyErr_Clear();
249  return TRUE;
250  }

◆ import_as()

void PythonObject::import_as ( const char *  name) const
inline

Definition at line 194 of file pyobject.cc.

194  {
195  idhdl handle = enterid(name, 0, DEF_CMD,
196  &IDROOT, FALSE);
197 
198  if (handle)
199  {
200  IDDATA(handle) = (char*)m_ptr;
201  Py_XINCREF(m_ptr);
202  IDTYP(handle) = PythonInterpreter::id();
203  }
204  else { WerrorS("Importing pyobject to Singular failed"); }
205  }

◆ none_to()

BOOLEAN PythonObject::none_to ( leftv  result) const
inlineprivate

Definition at line 274 of file pyobject.cc.

275  {
276  Py_XDECREF(m_ptr);
277  result->data = NULL;
278  result->rtyp = NONE;
279  return FALSE;
280  }

◆ operator const ptr_type()

PythonObject::operator const ptr_type ( ) const
inline

Get actual PyObject*.

Definition at line 176 of file pyobject.cc.

176 { return m_ptr; }

◆ operator()() [1/3]

self PythonObject::operator() ( int  op) const
inline

Unary operations.

Definition at line 125 of file pyobject.cc.

126  {
127  switch(op)
128  {
129  case '(': return check_context(PyObject_CallObject(*this, NULL));
130  case ATTRIB_CMD: return PyObject_Dir(*this);
131  case PROC_CMD: return *this;
132  }
133 
134  if (op == PythonInterpreter::id())
135  return *this;
136 
137  return self(NULL);
138  }

◆ operator()() [2/3]

self PythonObject::operator() ( int  op,
const self arg 
) const
inline

Binary and n-ary operations.

Definition at line 141 of file pyobject.cc.

141  {
142 
143  switch(op)
144  {
145  case '+': return PyNumber_Add(*this, arg);
146  case '-': return PyNumber_Subtract(*this, arg);
147  case '*': return PyNumber_Multiply(*this, arg);
148  case '/': return PyNumber_Divide(*this, arg);
149  case '^': return PyNumber_Power(*this, arg, Py_None);
150  case '(': return check_context(PyObject_CallObject(*this, arg));
151  case '[': return operator[](arg);
152  case KILLATTR_CMD: return del_attr(arg);
153  case LIST_CMD: return args2list(arg);
154  case '.': case COLONCOLON: case ATTRIB_CMD: return attr(arg);
155  }
156  return self(NULL);
157  }

◆ operator()() [3/3]

self PythonObject::operator() ( int  op,
const self arg1,
const self arg2 
) const
inline

Ternary operations.

Definition at line 160 of file pyobject.cc.

161  {
162  switch(op)
163  {
164  case ATTRIB_CMD:
165  if(PyObject_SetAttr(*this, arg1, arg2) == -1) handle_exception();
166  return self();
167  }
168  return self(NULL);
169  }

◆ operator[]() [1/2]

self PythonObject::operator[] ( const self idx) const
inline

Get item.

Definition at line 172 of file pyobject.cc.

172 { return PyObject_GetItem(*this, idx); }

◆ operator[]() [2/2]

self PythonObject::operator[] ( long  idx) const
inline

Definition at line 173 of file pyobject.cc.

173 { return operator[](PyInt_FromLong(idx)); }

◆ py_opid()

int PythonObject::py_opid ( int  op) const
inlineprotected

Definition at line 260 of file pyobject.cc.

260  {
261  switch(op)
262  {
263  case '<': return Py_LT;
264  case '>': return Py_GT;
265  case EQUAL_EQUAL: return Py_EQ;
266  case NOTEQUAL: return Py_NE;
267  case GE: return Py_GE;
268  case LE: return Py_LE;
269  }
270  return -1;
271  }

◆ python_to()

BOOLEAN PythonObject::python_to ( leftv  result) const
inlineprivate

Definition at line 282 of file pyobject.cc.

283  {
284  result->data = m_ptr;
285  Py_XINCREF(m_ptr);
286  result->rtyp = PythonInterpreter::id();
287  return !m_ptr;
288  }

◆ repr()

char* PythonObject::repr ( ) const
inline

Get representative as C-style string.

Definition at line 179 of file pyobject.cc.

180  {
181  return omStrDup(PyString_AsString(PyObject_Repr(*this)));
182  }

◆ size()

Py_ssize_t PythonObject::size ( ) const
inline

Definition at line 187 of file pyobject.cc.

187 { return PyObject_Size(m_ptr); }

◆ str()

char* PythonObject::str ( ) const
inline

Extract C-style string.

Definition at line 185 of file pyobject.cc.

185 { return omStrDup(PyString_AsString(*this)); }

Field Documentation

◆ m_ptr

ptr_type PythonObject::m_ptr
private

The actual pointer.

Definition at line 291 of file pyobject.cc.


The documentation for this class was generated from the following file:
PythonObject::attr
self attr(const self &arg) const
Definition: pyobject.cc:211
ATTRIB_CMD
Definition: tok.h:35
PythonObject::operator[]
self operator[](const self &idx) const
Get item.
Definition: pyobject.cc:172
FALSE
#define FALSE
Definition: auxiliary.h:94
COLONCOLON
Definition: grammar.cc:275
result
return result
Definition: facAbsBiFact.cc:76
PythonObject::del_attr
self del_attr(const self &arg) const
Definition: pyobject.cc:213
LIST_CMD
Definition: tok.h:117
enterid
idhdl enterid(const char *s, int lev, int t, idhdl *root, BOOLEAN init, BOOLEAN search)
Definition: ipid.cc:256
NONE
#define NONE
Definition: tok.h:217
IDDATA
#define IDDATA(a)
Definition: ipid.h:120
PythonObject::ptr_type
PyObject * ptr_type
Definition: pyobject.cc:112
omStrDup
#define omStrDup(s)
Definition: omAllocDecl.h:261
DEF_CMD
Definition: tok.h:57
PythonObject::py_opid
int py_opid(int op) const
Definition: pyobject.cc:260
PythonObject::handle_exception
BOOLEAN handle_exception() const
Definition: pyobject.cc:234
PythonObject::args2list
self args2list(const self &args) const
Definition: pyobject.cc:224
TRUE
#define TRUE
Definition: auxiliary.h:98
PythonObject::m_ptr
ptr_type m_ptr
The actual pointer.
Definition: pyobject.cc:291
sync_contexts
void sync_contexts()
getting stuff from python to Singular namespace
Definition: pyobject.cc:662
PROC_CMD
Definition: grammar.cc:280
IDROOT
#define IDROOT
Definition: ipid.h:17
PythonObject::python_to
BOOLEAN python_to(leftv result) const
Definition: pyobject.cc:282
EQUAL_EQUAL
Definition: grammar.cc:268
LE
Definition: grammar.cc:270
IDTYP
#define IDTYP(a)
Definition: ipid.h:113
idrec
Definition: idrec.h:33
KILLATTR_CMD
Definition: tok.h:107
PythonObject::none_to
BOOLEAN none_to(leftv result) const
Definition: pyobject.cc:274
Werror
void Werror(const char *fmt,...)
Definition: reporter.cc:188
PythonInterpreter::id
static id_type id()
Get Singular type identitfier.
Definition: pyobject.cc:56
PythonObject::check_context
ptr_type check_context(ptr_type ptr) const
Definition: pyobject.cc:120
name
char name(const Variable &v)
Definition: factory.h:180
WerrorS
void WerrorS(const char *s)
Definition: feFopen.cc:24
NULL
#define NULL
Definition: omList.c:9
GE
Definition: grammar.cc:269
NOTEQUAL
Definition: grammar.cc:273