11// python-gphoto2 - Python interface to libgphoto2
22// http://github.com/jim-easterbrook/python-gphoto2
3- // Copyright (C) 2014-24 Jim Easterbrook jim@jim-easterbrook.me.uk
3+ // Copyright (C) 2014-26 Jim Easterbrook jim@jim-easterbrook.me.uk
44//
55// This file is part of python-gphoto2.
66//
3737}
3838%enddef
3939
40- %define GPHOTO2_ERROR (error)
41- PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
42- %enddef
40+ // Get PyExc_GPhoto2Error object
41+ %fragment(" _declare_GPhoto2Error" , " header" ) {
42+ PyObject *PyExc_GPhoto2Error = NULL ;
43+ }
44+ %fragment(" _import_GPhoto2Error" , " init" , fragment=" _declare_GPhoto2Error" ) {
45+ {
46+ PyObject *module = PyImport_ImportModule (" gphoto2" );
47+ if (module ) {
48+ PyExc_GPhoto2Error = PyObject_GetAttrString (module , " GPhoto2Error" );
49+ SWIG_Py_DECREF (module );
50+ }
51+ if (!PyExc_GPhoto2Error)
52+ #if SWIG_VERSION >= 0x040400
53+ return -1 ;
54+ #else
55+ return NULL ;
56+ #endif
57+ }
58+ }
59+
60+ // Set Python exception if result is a failure
61+ %fragment(" gphoto2_error" , " header" , fragment=" _import_GPhoto2Error" ) {
62+ static int gphoto2_error (int error) {
63+ if (error < GP_OK ) {
64+ PyErr_SetObject (PyExc_GPhoto2Error, PyInt_FromLong (error));
65+ return 1 ;
66+ }
67+ return 0 ;
68+ };
69+ }
4370
4471%define PLAIN_ARGOUT (typepattern)
45- %typemap(in, numinputs=0 ) typepattern ($*1_type temp) {
46- temp = NULL ;
72+ %typemap(in, numinputs=0 ) typepattern ($*1_type temp = NULL ) {
4773 $1 = &temp;
4874}
4975%typemap(argout) typepattern {
@@ -55,7 +81,7 @@ PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
5581%define CALLOC_ARGOUT (typepattern)
5682%typemap(in, numinputs=0 ) typepattern () {
5783 $1 = ($1_type)calloc (1 , sizeof ($*1_type));
58- if ($ 1 == NULL ) {
84+ if (!$ 1 ) {
5985 PyErr_SetString (PyExc_MemoryError, " Cannot allocate " " $*1_type" );
6086 SWIG_fail;
6187 }
@@ -71,16 +97,14 @@ PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
7197%enddef
7298
7399%define NEW_ARGOUT (typepattern, alloc_func, free_func)
74- %typemap(in, numinputs=0 ) typepattern () {
75- int error = alloc_func (&$1 );
76- if (error < GP_OK ) {
100+ %typemap(in, numinputs=0 , fragment=" gphoto2_error" ) typepattern () {
101+ if (gphoto2_error (alloc_func (&$1 ))) {
77102 $1 = NULL ;
78- GPHOTO2_ERROR (error)
79103 SWIG_fail;
80104 }
81105}
82106%typemap(freearg) typepattern {
83- if ($1 != NULL ) {
107+ if ($1 ) {
84108 free_func ($1 );
85109 }
86110}
@@ -93,11 +117,10 @@ PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
93117
94118%define DEFAULT_CTOR (type, function)
95119%extend type {
120+ %fragment (" gphoto2_error" );
96121 type () {
97122 struct type *result;
98- int error = function (&result);
99- if (error < GP_OK )
100- GPHOTO2_ERROR (error)
123+ gphoto2_error (function (&result));
101124 return result;
102125 }
103126};
@@ -106,9 +129,9 @@ PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
106129%define DEFAULT_DTOR (name, free_func)
107130%delobject free_func;
108131%extend name {
132+ %fragment (" gphoto2_error" );
109133 ~name () {
110- int error = free_func ($self);
111- if (error < GP_OK ) GPHOTO2_ERROR (error)
134+ gphoto2_error (free_func ($self));
112135 }
113136};
114137%enddef
@@ -117,6 +140,7 @@ PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
117140%define MEMBER_FUNCTION (type, member_rtn, member, member_args,
118141 function, function_args, thread_allow)
119142%extend type {
143+ %fragment (" gphoto2_error" );
120144 member_rtn member member_args {
121145#if #thread_allow != ""
122146 SWIG_PYTHON_THREAD_BEGIN_ALLOW ;
@@ -125,7 +149,7 @@ PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
125149#if #thread_allow != ""
126150 SWIG_PYTHON_THREAD_END_ALLOW ;
127151#endif
128- if (result < GP_OK ) GPHOTO2_ERROR (result)
152+ gphoto2_error (result);
129153#if #member_rtn == "int" || #member_rtn == "static int"
130154 return result;
131155#endif
0 commit comments