GenericMappingTools / pygmt

A Python interface for the Generic Mapping Tools.
https://www.pygmt.org
BSD 3-Clause "New" or "Revised" License
758 stars 220 forks source link

GMT prints incorrect command line string if a list of arguments is passed #3205

Closed seisman closed 6 months ago

seisman commented 6 months ago

This is an upstream GMT bug, which affect us after we refactored wrappers to pass a list of argument strings in PR #3139. To reproduce the bug, run:

>>> import pygmt
>>> pygmt.config(GMT_VERBOSE="d")
gmtset [DEBUG]: gmtlib_get_graphics_item: Fig: 0 Subplot: 2 Panel: () Inset: 0
>>> fig = pygmt.Figure()
pygmt-session [DEBUG]: Exit:  gmt_reload_settings
pygmt-session [DEBUG]: Enter: gmtlib_plot_C_format
pygmt-session [DEBUG]: Exit:  gmtlib_plot_C_format
pygmt-session [DEBUG]: Enter: gmtinit_get_history
pygmt-session [DEBUG]: gmtlib_get_graphics_item: Fig: 0 Subplot: 2 Panel: () Inset: 0
pygmt-session [DEBUG]: Initialize FFTW with 80 threads.
pygmt-session [DEBUG]: GMT_Create_Session initialized GMT structure
pygmt-session [DEBUG]: Shared Library # 0 (core). Path = /home/seisman/opt/miniconda/envs/pygmt/lib/libgmt.so
pygmt-session [DEBUG]: Loading GMT plugins from: /home/seisman/opt/miniconda/envs/pygmt/lib/gmt/plugins
pygmt-session [DEBUG]: Shared Library # 1 (supplements). Path = /home/seisman/opt/miniconda/envs/pygmt/lib/gmt/plugins/supplements.so
pygmt-session [DEBUG]: GMT now running in modern mode [Session ID = 127951]
Exception ignored on calling ctypes callback function: <function Session.create.<locals>.print_func at 0x7fa3da132480>
Traceback (most recent call last):
  File "/home/seisman/OSS/gmt/pygmt/pygmt/clib/session.py", line 387, in print_func
    message = message.decode().strip()
              ^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 59: invalid start byte
pygmt-session [DEBUG]: Revised options: 0540c3cbdfa249479128dd18720134da -
figure [DEBUG]: No figure file /home/seisman/.gmt/sessions/gmt_session.127951/gmt.figures - nothing to do
figure [DEBUG]: New figure: 1   0540c3cbdfa249479128dd18720134da    -

The exception is because GMT incorrectly outputs a string like below to stderr:

pygmt-session [DEBUG]: GMT_Call_Command string: gmt figure \x10l7"\xec\x7f\n

Here, the argument \x10l7"\xec\x7f\n makes no sense.

Upstream PR https://github.com/GenericMappingTools/gmt/pull/8461 should fix the bug.

seisman commented 6 months ago

The upstream PR has been merged. I don't think we can have a workaround in PyGMT, since we definitely don't want to revert all changes in PR #3139. Fortunately, the upstream bug only affects users who care about the GMT_Call_Command string in the debugging message.