11
11
import pathlib
12
12
import sys
13
13
import warnings
14
- from collections .abc import Generator , Sequence
14
+ from collections .abc import Callable , Generator , Sequence
15
15
from typing import Literal
16
16
17
17
import numpy as np
@@ -268,7 +268,9 @@ def get_enum(self, name: str) -> int:
268
268
raise GMTCLibError (f"Constant '{ name } ' doesn't exist in libgmt." )
269
269
return value
270
270
271
- def get_libgmt_func (self , name , argtypes = None , restype = None ):
271
+ def get_libgmt_func (
272
+ self , name : str , argtypes : list | None = None , restype = None
273
+ ) -> Callable :
272
274
"""
273
275
Get a ctypes function from the libgmt shared library.
274
276
@@ -278,14 +280,14 @@ def get_libgmt_func(self, name, argtypes=None, restype=None):
278
280
279
281
Parameters
280
282
----------
281
- name : str
283
+ name
282
284
The name of the GMT API function.
283
- argtypes : list
284
- List of ctypes types used to convert the Python input arguments for
285
- the API function.
285
+ argtypes
286
+ List of ctypes types used to convert the Python input arguments for the API
287
+ function.
286
288
restype : ctypes type
287
- The ctypes type used to convert the input returned by the function
288
- into a Python type.
289
+ The ctypes type used to convert the input returned by the function into a
290
+ Python type.
289
291
290
292
Returns
291
293
-------
@@ -312,43 +314,42 @@ def get_libgmt_func(self, name, argtypes=None, restype=None):
312
314
function .restype = restype
313
315
return function
314
316
315
- def create (self , name ):
317
+ def create (self , name : str ):
316
318
"""
317
319
Create a new GMT C API session.
318
320
319
- This is required before most other methods of
320
- :class:`pygmt.clib.Session` can be called.
321
+ This is required before most other methods of :class:`pygmt.clib.Session` can be
322
+ called.
321
323
322
324
.. warning::
323
325
324
- Usage of :class:`pygmt.clib.Session` as a context manager in a
325
- ``with`` block is preferred over calling
326
- :meth:`pygmt.clib.Session.create` and
326
+ Usage of :class:`pygmt.clib.Session` as a context manager in a ``with``
327
+ block is preferred over calling :meth:`pygmt.clib.Session.create` and
327
328
:meth:`pygmt.clib.Session.destroy` manually.
328
329
329
- Calls ``GMT_Create_Session`` and generates a new ``GMTAPI_CTRL``
330
- struct, which is a :class:`ctypes.c_void_p` pointer. Sets the
331
- ``session_pointer`` attribute to this pointer.
330
+ Calls ``GMT_Create_Session`` and generates a new ``GMTAPI_CTRL`` struct, which
331
+ is a :class:`ctypes.c_void_p` pointer. Sets the ``session_pointer`` attribute to
332
+ this pointer.
332
333
333
334
Remember to terminate the current session using
334
335
:meth:`pygmt.clib.Session.destroy` before creating a new one.
335
336
336
337
Parameters
337
338
----------
338
- name : str
339
+ name
339
340
A name for this session. Doesn't really affect the outcome.
340
341
"""
341
342
try :
342
- # Won't raise an exception if there is a currently open session
343
+ # Won't raise an exception if there is a currently open session.
343
344
_ = self .session_pointer
344
- # In this case, fail to create a new session until the old one is
345
- # destroyed
346
- raise GMTCLibError (
345
+ # In this case, fail to create a new session until the old one is destroyed.
346
+ msg = (
347
347
"Failed to create a GMT API session: There is a currently open session."
348
348
" Must destroy it first."
349
349
)
350
- # If the exception is raised, this means that there is no open session
351
- # and we're free to create a new one.
350
+ raise GMTCLibError (msg )
351
+ # If the exception is raised, this means that there is no open session and we're
352
+ # free to create a new one.
352
353
except GMTCLibNoSessionError :
353
354
pass
354
355
@@ -358,9 +359,9 @@ def create(self, name):
358
359
restype = ctp .c_void_p ,
359
360
)
360
361
361
- # Capture the output printed by GMT into this list. Will use it later
362
- # to generate error messages for the exceptions raised by API calls.
363
- self ._error_log = []
362
+ # Capture the output printed by GMT into this list. Will use it later to
363
+ # generate error messages for the exceptions raised by API calls.
364
+ self ._error_log : list [ str ] = []
364
365
365
366
@ctp .CFUNCTYPE (ctp .c_int , ctp .c_void_p , ctp .c_char_p )
366
367
def print_func (file_pointer , message ): # noqa: ARG001
@@ -382,24 +383,22 @@ def print_func(file_pointer, message): # noqa: ARG001
382
383
print (message , file = sys .stderr , flush = True ) # noqa: T201
383
384
return 0
384
385
385
- # Need to store a copy of the function because ctypes doesn't and it
386
- # will be garbage collected otherwise
386
+ # Need to store a copy of the function because ctypes doesn't and it will be
387
+ # garbage collected otherwise
387
388
self ._print_callback = print_func
388
389
389
390
padding = self ["GMT_PAD_DEFAULT" ]
390
391
session_type = self ["GMT_SESSION_EXTERNAL" ]
391
-
392
392
session = c_create_session (name .encode (), padding , session_type , print_func )
393
393
394
394
if session is None :
395
- raise GMTCLibError (
396
- f"Failed to create a GMT API session:\n { self ._error_message } "
397
- )
395
+ msg = f"Failed to create a GMT API session:\n { self ._error_message } "
396
+ raise GMTCLibError (msg )
398
397
399
398
self .session_pointer = session
400
399
401
400
@property
402
- def _error_message (self ):
401
+ def _error_message (self ) -> str :
403
402
"""
404
403
A string with all error messages emitted by the C API.
405
404
@@ -416,19 +415,17 @@ def destroy(self):
416
415
417
416
.. warning::
418
417
419
- Usage of :class:`pygmt.clib.Session` as a context manager in a
420
- ``with`` block is preferred over calling
421
- :meth:`pygmt.clib.Session.create` and
418
+ Usage of :class:`pygmt.clib.Session` as a context manager in a ``with``
419
+ block is preferred over calling :meth:`pygmt.clib.Session.create` and
422
420
:meth:`pygmt.clib.Session.destroy` manually.
423
421
424
- Calls ``GMT_Destroy_Session`` to terminate and free the memory of a
425
- registered ``GMTAPI_CTRL`` session (the pointer for this struct is
426
- stored in the ``session_pointer`` attribute).
422
+ Calls ``GMT_Destroy_Session`` to terminate and free the memory of a registered
423
+ ``GMTAPI_CTRL`` session (the pointer for this struct is stored in the
424
+ ``session_pointer`` attribute).
427
425
428
- Always use this method after you are done using a C API session. The
429
- session needs to be destroyed before creating a new one. Otherwise,
430
- some of the configuration files might be left behind and can influence
431
- subsequent API calls.
426
+ Always use this method after you are done using a C API session. The session
427
+ needs to be destroyed before creating a new one. Otherwise, some of the
428
+ configuration files might be left behind and can influence subsequent API calls.
432
429
433
430
Sets the ``session_pointer`` attribute to ``None``.
434
431
"""
@@ -438,9 +435,8 @@ def destroy(self):
438
435
439
436
status = c_destroy_session (self .session_pointer )
440
437
if status :
441
- raise GMTCLibError (
442
- f"Failed to destroy GMT API session:\n { self ._error_message } "
443
- )
438
+ msg = f"Failed to destroy GMT API session:\n { self ._error_message } "
439
+ raise GMTCLibError (msg )
444
440
445
441
self .session_pointer = None
446
442
0 commit comments