/[escript]/branches/arrexp_2137_win_merge/escript/src/WrappedArray.cpp
ViewVC logotype

Contents of /branches/arrexp_2137_win_merge/escript/src/WrappedArray.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2119 - (show annotations)
Tue Dec 2 06:06:04 2008 UTC (10 years, 9 months ago) by jfenwick
Original Path: branches/arrexp_trunk2098/escript/src/WrappedArray.cpp
File size: 2212 byte(s)
Branch commit.
Threading Wrapped Array through the code.
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14 #include "WrappedArray.h"
15 #include "DataException.h"
16
17 #include <iostream>
18
19 using namespace escript;
20 using namespace boost::python;
21
22 namespace
23 {
24
25 void checkFeatures(const boost::python::object& obj)
26 {
27 using namespace std;
28 int len=0;
29 boost::python::object o2;
30 try
31 {
32 len=extract<int>(obj.attr("__len__")());
33 }
34 catch (...)
35 {
36 PyErr_Clear();
37 string s=extract<std::string>(obj.attr("__str__")());
38 cerr << "Failure for object: " << s << endl;
39 s=extract<std::string>(obj.attr("__repr__")());
40 cerr << "Failurr for object: " << s << endl;
41 throw DataException("Object passed to WrappedArray must support __len__");
42 }
43 try
44 {
45 o2=obj.attr("__getitem__");
46 }
47 catch (...)
48 {
49 PyErr_Clear();
50 throw DataException("Object passed to WrappedArray must support __getitem__");
51 }
52 }
53
54 void getObjShape(const boost::python::object& obj, DataTypes::ShapeType& s)
55 {
56 int len=0;
57 try
58 {
59 len=extract<int>(obj.attr("__len__")());
60 }
61 catch(...)
62 {
63 PyErr_Clear(); // tell python the error isn't there anymore
64 return;
65 }
66 if (len<1)
67 {
68 throw DataException("Array filter - no empty components in arrays please.");
69 }
70 s.push_back(len);
71
72 if (s.size()>ESCRIPT_MAX_DATA_RANK)
73 {
74 throw DataException("Array filter - Maximum rank exceeded in array");
75 }
76 getObjShape(obj[0],s);
77 }
78
79 }
80
81 WrappedArray::WrappedArray(const boost::python::object& obj_in)
82 :obj(obj_in)
83 {
84 // First we check for scalars
85 try
86 {
87 double v=extract<double>(obj_in);
88 m_scalar=v;
89 rank=0;
90 return;
91 }
92 catch (...)
93 { // so we
94 PyErr_Clear();
95 }
96 try
97 {
98 double v=extract<double>(obj_in[make_tuple()]);
99 m_scalar=v;
100 rank=0;
101 return;
102 }
103 catch (...)
104 { // so we
105 PyErr_Clear();
106 }
107
108
109 m_scalar=0;
110 checkFeatures(obj_in);
111 getObjShape(obj,shape);
112 rank=shape.size();
113 }
114
115
116

  ViewVC Help
Powered by ViewVC 1.1.26