Commit fdcfac83f3d2a10478f183c7b598d4a551c4557d

Authored by Jürgen Knödlseder
1 parent 8885b43a

Properly detect boolean application parameter.

I removed the -DSWIG_PYTHON_LEGACY_BOOL compile option for swig and corrected two unit test problems in test_GApplication.py and test_GFits.py that arose from this (see #1494). I then added a __setitem__(const std::string& name, const bool& val) method to GApplication and disallowed setting of a boolean parameter with an integer value. This results in the expected behavior.
pyext/GApplication.i
... ... @@ -87,6 +87,20 @@ public:
87 87 (*self)[name] = val;
88 88 return;
89 89 }
  90 + void __setitem__(const std::string& name, const bool& val) {
  91 + GApplicationPar& par = (*self)[name];
  92 + if (par.type() == "b") {
  93 + par.boolean(val);
  94 + }
  95 + else {
  96 + std::string msg = "Attempt to set \""+par.type()+
  97 + "\" parameter \""+name+"\" with boolean "
  98 + "value \""+gammalib::str(val)+"\".";
  99 + throw GException::invalid_argument("__setitem__(std::string, int)",
  100 + msg);
  101 + }
  102 + return;
  103 + }
90 104 void __setitem__(const std::string& name, const int& val) {
91 105 GApplicationPar& par = (*self)[name];
92 106 if (par.type() == "r") {
... ... @@ -95,9 +109,6 @@ public:
95 109 else if (par.type() == "i") {
96 110 par.integer(val);
97 111 }
98   - else if (par.type() == "b") {
99   - par.boolean(val);
100   - }
101 112 else {
102 113 std::string msg = "Attempt to set \""+par.type()+
103 114 "\" parameter \""+name+"\" with integer "
... ...
pyext/Makefile.am
... ... @@ -131,7 +131,7 @@ endif
131 131 CLEANFILES = $(CLEAN_FILES) $(DEPFILES)
132 132  
133 133 # Set SWIG compile command
134   -SWIGCOMPILE = $(SWIG) -c++ -python -Wall -DSWIG_PYTHON_LEGACY_BOOL
  134 +SWIGCOMPILE = $(SWIG) -c++ -python -Wall
135 135  
136 136 # Things we want to do when 'make' is invoked.
137 137 all-local: build
... ...
test/test_GApplication.py
... ... @@ -99,7 +99,8 @@ class Test(gammalib.GPythonTestSuite):
99 99 # Set reference
100 100 ref = []
101 101 ref.append("Test __call__(std::string)\n")
102   - ref.append("1\n")
  102 + #ref.append("1\n")
  103 + ref.append("true\n") # New swig version
103 104 ref.append("41\n")
104 105 ref.append("3.1415\n")
105 106 ref.append(" This is a parameter .......: \n")
... ... @@ -145,7 +146,7 @@ class Test(gammalib.GPythonTestSuite):
145 146 lines = file.readlines()
146 147 file.close()
147 148 for i, line in enumerate(lines):
148   - self.test_assert(line == ref[i], "Test " + calls[i])
  149 + self.test_assert(line == ref[i], "Test "+calls[i]+": "+line)
149 150  
150 151 # Return
151 152 return
... ...
test/test_GFits.py
1 1 # ==========================================================================
2 2 # This module performs unit tests for the GammaLib FITS module.
3 3 #
4   -# Copyright (C) 2012 Juergen Knoedlseder
  4 +# Copyright (C) 2012-2015 Juergen Knoedlseder
5 5 #
6 6 # This program is free software: you can redistribute it and/or modify
7 7 # it under the terms of the GNU General Public License as published by
... ... @@ -138,8 +138,10 @@ class Test(gammalib.GPythonTestSuite):
138 138 col10 = gammalib.GFitsTableULongCol("ULONG", nrows)
139 139 col11 = gammalib.GFitsTableUShortCol("USHORT", nrows)
140 140 for i in range(nrows):
141   - col1[i] = i % 2
142   - col2[i] = i % 2
  141 + #col1[i] = i % 2
  142 + #col2[i] = i % 2
  143 + col1[i] = bool(i % 2) # New swig version
  144 + col2[i] = bool(i % 2) # New swig version
143 145 col3[i] = i
144 146 col4[i] = i * 0.01
145 147 col5[i] = i * 0.01
... ...