1/********************************************
2copyright 1999 McMillan Enterprises, Inc.
3www.mcmillan-inc.com
4 *********************************************/
5#include "PWOSequence.h"
6#include "PWOMSequence.h"
7#include "PWOMapping.h"
8#include "PWOCallable.h"
9
10// dummy exception singleton
11const PWDException PWDPyExceptionObj;
12const PWDException &PWDPyException = PWDPyExceptionObj;
13
14// incref new owner, and decref old owner, and adjust to new owner
15void PWOBase::GrabRef(PyObject *newObj) {
16  // be careful to incref before decref if old is same as new
17  Py_XINCREF(newObj);
18  Py_XDECREF(_own);
19  _own = _obj = newObj;
20}
21
22PWOTuple::PWOTuple(const PWOList &list): PWOSequence(PyList_AsTuple(list)) {
23  LoseRef(_obj);
24}
25
26PWOListMmbr::PWOListMmbr(PyObject *obj, PWOList &parent, int ndx): PWOBase(obj),
27  _parent(parent), _ndx(ndx){}
28
29PWOListMmbr &PWOListMmbr::operator = (const PWOBase &other) {
30  GrabRef(other);
31  //Py_XINCREF(_obj); // this one is for setItem to steal
32  _parent.setItem(_ndx,  *this);
33  return  *this;
34}
35
36PWOMappingMmbr &PWOMappingMmbr::operator = (const PWOBase &other) {
37  GrabRef(other);
38  _parent.setItem(_key,  *this);
39  return  *this;
40}
41
42PWOBase PWOCallable::call()const {
43  static PWOTuple _empty;
44  PyObject *rslt = PyEval_CallObjectWithKeywords(*this, _empty, NULL);
45  if (rslt == 0)
46    throw PWDPyException;
47  return rslt;
48}
49
50PWOBase PWOCallable::call(PWOTuple &args)const {
51  PyObject *rslt = PyEval_CallObjectWithKeywords(*this, args, NULL);
52  if (rslt == 0)
53    throw PWDPyException;
54  return rslt;
55}
56
57PWOBase PWOCallable::call(PWOTuple &args, PWOMapping &kws)const {
58  PyObject *rslt = PyEval_CallObjectWithKeywords(*this, args, kws);
59  if (rslt == 0)
60    throw PWDPyException;
61  return rslt;
62}
63
64void Fail(PyObject *exc, const char *msg) {
65  PyErr_SetString(exc, msg);
66  throw PWDPyException;
67}
68
69void FailIfPyErr() {
70  PyObject *exc = PyErr_Occurred();
71  if (exc != NULL)
72    throw PWDPyException;
73}
74