Add insn.py demo.
[pycomedi.git] / pycomedi / _error.pyx
index 900af07e9123fcb9d49d95719d5b8671ccdd1019..4cd4ceae2ac077aad6a8a894054b509a1edc9792 100644 (file)
@@ -2,18 +2,17 @@
 #
 # This file is part of pycomedi.
 #
-# pycomedi is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the
-# Free Software Foundation, either version 2 of the License, or (at your
-# option) any later version.
+# pycomedi is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 2 of the License, or (at your option) any later
+# version.
 #
-# pycomedi is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# General Public License for more details.
+# pycomedi is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License
-# along with pycomedi.  If not, see <http://www.gnu.org/licenses/>.
+# You should have received a copy of the GNU General Public License along with
+# pycomedi.  If not, see <http://www.gnu.org/licenses/>.
 
 "Useful error checking wrappers around Comedilib function calls"
 
@@ -25,9 +24,10 @@ from pycomedi import PyComediError as _PyComediError
 cimport _comedilib_h
 
 
-def raise_error(function_name='', ret=None, error_msg=None):
+def raise_error(function_name=None, ret=None, error_msg=None):
     """Report an error while executing a comedilib function
 
+    >>> from pycomedi import PyComediError
     >>> raise_error(function_name='myfn', ret=-1)
     Traceback (most recent call last):
       ...
@@ -36,29 +36,33 @@ def raise_error(function_name='', ret=None, error_msg=None):
     Traceback (most recent call last):
       ...
     PyComediError: myfn (some error): Success (-1)
+    >>> try:
+    ...     raise_error(function_name='myfn', ret=-1)
+    ... except PyComediError, e:
+    ...     print(e.function_name)
+    ...     print(e.ret)
+    myfn
+    -1
     """
     errno = _comedilib_h.comedi_errno()
     comedi_msg = _comedilib_h.comedi_strerror(errno)
-    #_comedilib_h.comedi_perror(function_name)
-    if error_msg:
-        msg = '%s (%s): %s (%s)' % (function_name, error_msg, comedi_msg, ret)
-    else:
-        msg = '%s: %s (%s)' % (function_name, comedi_msg, ret)
-    raise _PyComediError(msg)
+    raise _PyComediError(
+        function_name=function_name, ret=ret, comedi_msg=comedi_msg,
+        error_msg=error_msg)
 
 
 def _comedi_getter(name, is_invalid):
     # Hmm, cannot get function by name, or pass differing function pointers...
     #def comedi_get(function_name, *args, **kwargs):
     def comedi_get(function, *args, **kwargs):
+        #fn = getattr(_comedilib_h, function_name)
+        fn = function  # workaround until I get getattr() working
+        function_name = function.__name__
         if 'error_msg' in kwargs:
             error_msg = kwargs.pop('error_msg')
         else:
             error_msg = 'error while running %s with %s and %s' % (
                 function_name, args, kwargs)
-        #fn = getattr(_comedilib_h, function_name)
-        fn = function  # workaround until I get getattr() working
-        function_name = function.__name__
 
         _LOG.debug('calling %s with %s %s' % (function_name, args, kwargs))