@@ -175,4 +175,88 @@ def check_status(obj, response_code):
175175 raise _error_code_lookup [response_code ](text )
176176
177177
178+ class burble :
179+ """Control diagnostic output, and may be used as a context manager.
180+
181+ Set up and simple usage:
182+
183+ >>> from suitesparse_graphblas import burble, lib, matrix
184+ >>>
185+ >>> A = matrix.new(lib.GrB_BOOL, 3, 3)
186+ >>> burble.is_enabled
187+ False
188+ >>> burble.enable()
189+ >>> burble.is_enabled
190+ True
191+ >>> burble.disable()
192+
193+ Example with explicit enable and disable:
194+
195+ >>> burble.enable()
196+ >>> n = matrix.nvals(A)
197+ [ GrB_Matrix_nvals
198+ 1.91e-06 sec ]
199+ >>> burble.disable()
200+
201+ Example as a context manager:
202+
203+ >>> with burble():
204+ >>> n = matrix.nvals(A)
205+ [ GrB_Matrix_nvals
206+ 1.91e-06 sec ]
207+
208+ """
209+
210+ def __init__ (self ):
211+ self ._states = []
212+
213+ @property
214+ def is_enabled (self ):
215+ """Is burble enabled?"""
216+ val_ptr = ffi .new ("bool*" )
217+ info = lib .GxB_Global_Option_get (lib .GxB_BURBLE , val_ptr )
218+ if info != lib .GrB_SUCCESS :
219+ raise _error_code_lookup [info ](
220+ "Failed to get burble status (has GraphBLAS been initialized?"
221+ )
222+ return val_ptr [0 ]
223+
224+ def enable (self ):
225+ """Enable diagnostic output"""
226+ info = lib .GxB_Global_Option_set (lib .GxB_BURBLE , ffi .cast ("int" , 1 ))
227+ if info != lib .GrB_SUCCESS :
228+ raise _error_code_lookup [info ](
229+ "Failed to enable burble (has GraphBLAS been initialized?"
230+ )
231+
232+ def disable (self ):
233+ """Disable diagnostic output"""
234+ info = lib .GxB_Global_Option_set (lib .GxB_BURBLE , ffi .cast ("int" , 0 ))
235+ if info != lib .GrB_SUCCESS :
236+ raise _error_code_lookup [info ](
237+ "Failed to disable burble (has GraphBLAS been initialized?"
238+ )
239+
240+ def __enter__ (self ):
241+ is_enabled = self .is_enabled
242+ if not is_enabled :
243+ self .enable ()
244+ self ._states .append (is_enabled )
245+ return self
246+
247+ def __exit__ (self , type_ , value , traceback ):
248+ is_enabled = self ._states .pop ()
249+ if not is_enabled :
250+ self .disable ()
251+
252+ def __reduce__ (self ):
253+ return "burble"
254+
255+ def __repr__ (self ):
256+ return f"<burble is_enabled={ self .is_enabled } >"
257+
258+
259+ burble = burble ()
260+
261+
178262__version__ = _version .get_versions ()["version" ]
0 commit comments