Loading...
Searching...
No Matches
PythonObject Class Reference

This class defines an interface for calling PyObject from Singular. More...

## 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.
self | operator() (int op, const self &arg) const
| Binary and n-ary operations.
self | operator() (int op, const self &arg1, const self &arg2) const
| Ternary operations.
self | operator[] (const self &idx) const
| Get item.
self | operator[] (long idx) const
| operator const ptr_type () const
| Get actual PyObject*.
char * | repr () const
| Get representative as C-style string.
char * | str () const
| Extract C-style string.
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.

## 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

typedef PythonObject PythonObject::self

private

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.

: m_ptr(Py_None) { }
PythonObject::m_ptr
ptr_type m_ptr
The actual pointer.
Definition pyobject.cc:291

## PythonObject() [2/2]

PythonObject::PythonObject | ( | ptr_type | ptr | ) |

inline

Definition at line 116 of file pyobject.cc.

: m_ptr(ptr) {
if (!ptr && handle_exception()) m_ptr = Py_None;
}
PythonObject::handle_exception
BOOLEAN handle_exception() const
Definition pyobject.cc:234

## Member Function Documentation

## append_iter()

void PythonObject::append_iter | ( | self | iterator | ) |

inline protected

Definition at line 252 of file pyobject.cc.

{
ptr_type item;
while ((item = PyIter_Next(iterator))) {
PyList_Append(*this, item);
Py_DECREF(item);
}
}
PythonObject::ptr_type
PyObject * ptr_type
Definition pyobject.cc:112

## args2list()

self PythonObject::args2list | ( | const self & | args | ) | const

inline protected

Definition at line 224 of file pyobject.cc.

{
self pylist(PyList_New(0));
PyList_Append(pylist, *this);
if(PyTuple_Check(args)) pylist.append_iter(PyObject_GetIter(args));
else PyList_Append(pylist, args);
return pylist;
}
PythonObject::self
PythonObject self
Definition pyobject.cc:109

## assign_to()

BOOLEAN PythonObject::assign_to | ( | leftv | result | ) |

inline

Definition at line 189 of file pyobject.cc.

{
return (m_ptr? (m_ptr == Py_None? none_to(result): python_to(result)): TRUE);
}
TRUE
#define TRUE
Definition auxiliary.h:101
PythonObject::none_to
BOOLEAN none_to(leftv result) const
Definition pyobject.cc:274
PythonObject::python_to
BOOLEAN python_to(leftv result) const
Definition pyobject.cc:282
result
return result
Definition facAbsBiFact.cc:76

## attr()

self PythonObject::attr | ( | const self & | arg | ) | const

inline

Definition at line 211 of file pyobject.cc.

{ 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.

{
if(ptr) sync_contexts();
return ptr;
}
sync_contexts
void sync_contexts()
getting stuff from python to Singular namespace
Definition pyobject.cc:662

## compare()

int PythonObject::compare | ( | int | op,
|  | const self & | arg ) const

inline

Definition at line 207 of file pyobject.cc.

{ return PyObject_RichCompareBool(*this, arg, py_opid(op)); }
PythonObject::py_opid
int py_opid(int op) const
Definition pyobject.cc:260

## del_attr()

self PythonObject::del_attr | ( | const self & | arg | ) | const

inline

Definition at line 213 of file pyobject.cc.

{
if (!PyObject_HasAttr(*this, arg))
Werror("Cannot delete attribute %s.", arg.repr());
else
PyObject_DelAttr(*this, arg);
return self();
}
Werror
void Werror(const char *fmt,...)
Definition reporter.cc:189

## handle_exception()

BOOLEAN PythonObject::handle_exception | ( |  | ) | const

inline protected

Definition at line 234 of file pyobject.cc.

{
if(!PyErr_Occurred()) return FALSE;
PyObject *pType, *pMessage, *pTraceback;
PyErr_Fetch(&pType, &pMessage, &pTraceback);
WerrorS("pyobject error occurred");
WerrorS(PyString_AsString(pMessage));
Py_XDECREF(pType);
Py_XDECREF(pMessage);
Py_XDECREF(pTraceback);
PyErr_Clear();
return TRUE;
}
FALSE
#define FALSE
Definition auxiliary.h:97
WerrorS
void WerrorS(const char *s)
Definition feFopen.cc:24

## import_as()

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

inline

Definition at line 194 of file pyobject.cc.

{
idhdl handle = enterid(name, 0, DEF_CMD,
&IDROOT, FALSE);
if (handle)
{
IDDATA(handle) = (char*)m_ptr;
Py_XINCREF(m_ptr);
IDTYP(handle) = PythonInterpreter::id();
}
else { WerrorS("Importing pyobject to Singular failed"); }
}
PythonInterpreter::id
static id_type id()
Get Singular type identitfier.
Definition pyobject.cc:56
name
char name(const Variable &v)
Definition factory.h:189
enterid
idhdl enterid(const char *s, int lev, int t, idhdl *root, BOOLEAN init, BOOLEAN search)
Definition ipid.cc:279
IDDATA
#define IDDATA(a)
Definition ipid.h:126
IDROOT
#define IDROOT
Definition ipid.h:19
IDTYP
#define IDTYP(a)
Definition ipid.h:119
idhdl
idrec * idhdl
Definition ring.h:22
DEF_CMD
@ DEF_CMD
Definition tok.h:58

## none_to()

BOOLEAN PythonObject::none_to | ( | leftv | result | ) | const

inline private

Definition at line 274 of file pyobject.cc.

{
Py_XDECREF(m_ptr);
result->data = NULL;
result->rtyp = NONE;
return FALSE;
}
NULL
#define NULL
Definition omList.c:12
NONE
#define NONE
Definition tok.h:223

## operator const ptr_type()

PythonObject::operator const ptr_type | ( |  | ) | const

inline

Get actual PyObject*.

Definition at line 176 of file pyobject.cc.

{ return m_ptr; }

## operator()() [1/3]

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

inline

Unary operations.

Definition at line 125 of file pyobject.cc.

{
switch(op)
{
case '(': return check_context(PyObject_CallObject(*this, NULL));
case ATTRIB_CMD: return PyObject_Dir(*this);
case PROC_CMD: return *this;
}
if (op == PythonInterpreter::id())
return *this;
return self(NULL);
}
PythonObject::check_context
ptr_type check_context(ptr_type ptr) const
Definition pyobject.cc:120
PROC_CMD
@ PROC_CMD
Definition grammar.cc:281
ATTRIB_CMD
@ ATTRIB_CMD
Definition tok.h:36

## 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.

{
switch(op)
{
case '+': return PyNumber_Add(*this, arg);
case '-': return PyNumber_Subtract(*this, arg);
case '*': return PyNumber_Multiply(*this, arg);
case '/': return PyNumber_Divide(*this, arg);
case '^': return PyNumber_Power(*this, arg, Py_None);
case '(': return check_context(PyObject_CallObject(*this, arg));
case '[': return operator[](arg);
case KILLATTR_CMD: return del_attr(arg);
case LIST_CMD: return args2list(arg);
case '.': case COLONCOLON: case ATTRIB_CMD: return attr(arg);
}
return self(NULL);
}
attr
sattr * attr
Definition attrib.h:16
PythonObject::args2list
self args2list(const self &args) const
Definition pyobject.cc:224
PythonObject::del_attr
self del_attr(const self &arg) const
Definition pyobject.cc:213
PythonObject::operator[]
self operator[](const self &idx) const
Get item.
Definition pyobject.cc:172
COLONCOLON
@ COLONCOLON
Definition grammar.cc:275
LIST_CMD
@ LIST_CMD
Definition tok.h:118
KILLATTR_CMD
@ KILLATTR_CMD
Definition tok.h:108

## 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.

{
switch(op)
{
case ATTRIB_CMD:
if(PyObject_SetAttr(*this, arg1, arg2) == -1) handle_exception();
return self();
}
return self(NULL);
}

## operator[]() [1/2]

self PythonObject::operator[] | ( | const self & | idx | ) | const

inline

Get item.

Definition at line 172 of file pyobject.cc.

{ return PyObject_GetItem(*this, idx); }

## operator[]() [2/2]

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

inline

Definition at line 173 of file pyobject.cc.

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

## py_opid()

int PythonObject::py_opid | ( | int | op | ) | const

inline protected

Definition at line 260 of file pyobject.cc.

{
switch(op)
{
case '<': return Py_LT;
case '>': return Py_GT;
case EQUAL_EQUAL: return Py_EQ;
case NOTEQUAL: return Py_NE;
case GE: return Py_GE;
case LE: return Py_LE;
}
return -1;
}
GE
@ GE
Definition grammar.cc:269
EQUAL_EQUAL
@ EQUAL_EQUAL
Definition grammar.cc:268
LE
@ LE
Definition grammar.cc:270
NOTEQUAL
@ NOTEQUAL
Definition grammar.cc:273

## python_to()

BOOLEAN PythonObject::python_to | ( | leftv | result | ) | const

inline private

Definition at line 282 of file pyobject.cc.

{
result->data = m_ptr;
Py_XINCREF(m_ptr);
result->rtyp = PythonInterpreter::id();
return !m_ptr;
}

## repr()

char * PythonObject::repr | ( |  | ) | const

inline

Get representative as C-style string.

Definition at line 179 of file pyobject.cc.

{
return omStrDup(PyString_AsString(PyObject_Repr(*this)));
}
omStrDup
#define omStrDup(s)
Definition omAllocDecl.h:263

## size()

Py_ssize_t PythonObject::size | ( |  | ) | const

inline

Definition at line 187 of file pyobject.cc.

{ return PyObject_Size(m_ptr); }

## str()

char * PythonObject::str | ( |  | ) | const

inline

Extract C-style string.

Definition at line 185 of file pyobject.cc.

{ 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:

- Singular/dyn_modules/pyobject/pyobject.cc