Merge lp:~cwilson/spud/pythonnewkeywarning into lp:spud

Proposed by Cian Wilson
Status: Merged
Merged at revision: 539
Proposed branch: lp:~cwilson/spud/pythonnewkeywarning
Merge into: lp:spud
Diff against target: 113 lines (+29/-18)
2 files modified
python/libspud.c (+22/-18)
python/test_libspud.py (+7/-0)
To merge this branch: bzr merge lp:~cwilson/spud/pythonnewkeywarning
Reviewer Review Type Date Requested Status
Stephan Kramer Approve
Review via email: mp+318869@code.launchpad.net

Description of the change

This merge implements a fix for exception handling in set_option through the python interface- specifically handling SpudNewKeyWarnings.

Previously it would raise an exception but not return the right state (NULL) for it to be caught. Now SpudNewKeyWarning can be caught in python.

Additionally it was not possible to set a new option with type double through python. This appears to be because the PyArg_Parse function is broken (and its use discouraged in the documentation). Switching this to PyFloat_AS_DOUBLE, which works.

Modifying the python tests to reflect these changes.

Also tidying up messages associated with python exceptions.

To post a comment you must log in.
Revision history for this message
Stephan Kramer (s-kramer) wrote :

Looks glorious!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'python/libspud.c'
--- python/libspud.c 2012-07-17 20:14:59 +0000
+++ python/libspud.c 2017-03-03 05:26:25 +0000
@@ -22,44 +22,44 @@
22 char errormessage [MAXLENGTH];22 char errormessage [MAXLENGTH];
2323
24 if (outcome == SPUD_KEY_ERROR){24 if (outcome == SPUD_KEY_ERROR){
25 snprintf(errormessage, MAXLENGTH, "Error: The specified option is not present \25 snprintf(errormessage, MAXLENGTH, "Error: The specified option is not present "
26 in the dictionary in %s", functionname);26 "in the dictionary in %s", functionname);
27 PyErr_SetString(SpudKeyError, errormessage);27 PyErr_SetString(SpudKeyError, errormessage);
28 return NULL;28 return NULL;
29 }29 }
30 if (outcome == SPUD_TYPE_ERROR){30 if (outcome == SPUD_TYPE_ERROR){
31 snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different \31 snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different "
32 type from that of the option argument provided in %s", functionname);32 "type from that of the option argument provided in (%s).", functionname);
33 PyErr_SetString(SpudTypeError, errormessage);33 PyErr_SetString(SpudTypeError, errormessage);
34 return NULL;34 return NULL;
35 }35 }
36 if (outcome == SPUD_NEW_KEY_WARNING){36 if (outcome == SPUD_NEW_KEY_WARNING){
37 snprintf(errormessage, MAXLENGTH, "Warning: The option being inserted is not ] \37 snprintf(errormessage, MAXLENGTH, "Warning: The option being inserted is not "
38 already in the dictionary %s", functionname);38 "already in the dictionary (%s).", functionname);
39 PyErr_SetString(SpudNewKeyWarning, errormessage);39 PyErr_SetString(SpudNewKeyWarning, errormessage);
40 return NULL;40 return NULL;
41 }41 }
42 if (outcome == SPUD_FILE_ERROR){42 if (outcome == SPUD_FILE_ERROR){
43 snprintf(errormessage, MAXLENGTH, "Error: The specified options file cannot be \43 snprintf(errormessage, MAXLENGTH, "Error: The specified options file cannot be "
44 read or written to as the routine requires in %s", functionname);44 "read or written to as the routine requires in (%s).", functionname);
45 PyErr_SetString(SpudFileError, errormessage);45 PyErr_SetString(SpudFileError, errormessage);
46 return NULL;46 return NULL;
47 }47 }
48 if (outcome == SPUD_RANK_ERROR){48 if (outcome == SPUD_RANK_ERROR){
49 snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different rank from \49 snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different rank from "
50 that of the option argument provided %s", functionname);50 "that of the option argument provided (%s).", functionname);
51 PyErr_SetString(SpudRankError, errormessage);51 PyErr_SetString(SpudRankError, errormessage);
52 return NULL;52 return NULL;
53 }53 }
54 if (outcome == SPUD_SHAPE_ERROR){54 if (outcome == SPUD_SHAPE_ERROR){
55 snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different shape from \55 snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different shape from "
56 that of the option argument provided in %s", functionname);56 "that of the option argument provided in (%s).", functionname);
57 PyErr_SetString(SpudShapeError, errormessage);57 PyErr_SetString(SpudShapeError, errormessage);
58 return NULL;58 return NULL;
59 }59 }
60 if (outcome == SPUD_ATTR_SET_FAILED_WARNING){60 if (outcome == SPUD_ATTR_SET_FAILED_WARNING){
61 snprintf(errormessage, MAXLENGTH, "Warning: The option being set as an attribute can not be \61 snprintf(errormessage, MAXLENGTH, "Warning: The option being set as an attribute can not be "
62 set as an attribute in %s", functionname);62 "set as an attribute in (%s).", functionname);
63 PyErr_SetString(SpudAttrSetFailedWarning, errormessage);63 PyErr_SetString(SpudAttrSetFailedWarning, errormessage);
64 return NULL;64 return NULL;
65 }65 }
@@ -655,8 +655,7 @@
655 int outcomeSetOption = SPUD_NO_ERROR;655 int outcomeSetOption = SPUD_NO_ERROR;
656656
657 if (type == SPUD_DOUBLE){ //scalar is double657 if (type == SPUD_DOUBLE){ //scalar is double
658 double val;658 double val = PyFloat_AS_DOUBLE(pyscalar);
659 PyArg_Parse(pyscalar, "d", &val);
660 outcomeSetOption = spud_set_option(key, key_len, &val, type, rank, shape);659 outcomeSetOption = spud_set_option(key, key_len, &val, type, rank, shape);
661 }660 }
662 else if (type == SPUD_INT){661 else if (type == SPUD_INT){
@@ -767,7 +766,12 @@
767 }766 }
768 }767 }
769768
770 Py_RETURN_NONE;769 if (PyErr_Occurred()){
770 return NULL;
771 }
772 else {
773 Py_RETURN_NONE;
774 }
771}775}
772776
773static PyObject*777static PyObject*
@@ -836,7 +840,7 @@
836 SpudNewKeyWarning = PyErr_NewException("SpudNewKey.warning", NULL, NULL);840 SpudNewKeyWarning = PyErr_NewException("SpudNewKey.warning", NULL, NULL);
837 SpudKeyError = PyErr_NewException("SpudKey.error", NULL, NULL);841 SpudKeyError = PyErr_NewException("SpudKey.error", NULL, NULL);
838 SpudTypeError = PyErr_NewException("SpudType.error", NULL, NULL);842 SpudTypeError = PyErr_NewException("SpudType.error", NULL, NULL);
839 SpudFileError = PyErr_NewException("SpudFile.warning", NULL, NULL);843 SpudFileError = PyErr_NewException("SpudFile.error", NULL, NULL);
840 SpudAttrSetFailedWarning = PyErr_NewException("SpudAttrSetFailed.warning", NULL, NULL);844 SpudAttrSetFailedWarning = PyErr_NewException("SpudAttrSetFailed.warning", NULL, NULL);
841 SpudShapeError = PyErr_NewException("SpudShape.error", NULL, NULL);845 SpudShapeError = PyErr_NewException("SpudShape.error", NULL, NULL);
842 SpudRankError = PyErr_NewException("SpudRank.error", NULL, NULL);846 SpudRankError = PyErr_NewException("SpudRank.error", NULL, NULL);
843847
=== modified file 'python/test_libspud.py'
--- python/test_libspud.py 2011-11-04 13:55:59 +0000
+++ python/test_libspud.py 2017-03-03 05:26:25 +0000
@@ -87,6 +87,13 @@
8787
88libspud.write_options('test_out.flml')88libspud.write_options('test_out.flml')
8989
90try:
91 libspud.set_option('/test', 4.3)
92except libspud.SpudNewKeyWarning, e:
93 pass
94
95assert libspud.get_option('/test') == 4.3
96
90libspud.set_option('/test',4)97libspud.set_option('/test',4)
9198
92assert libspud.get_option('/test') == 499assert libspud.get_option('/test') == 4

Subscribers

People subscribed via source and target branches