Skip to content

Commit

Permalink
Backward compatible with the old way
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Oct 16, 2024
1 parent 29bd3f8 commit f5b0e8e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ def open_virtual_file(self, family, geometry, direction, data):
return self.open_virtualfile(family, geometry, direction, data)

@contextlib.contextmanager
def virtualfile_from_vectors(self, vectors):
def virtualfile_from_vectors(self, vectors, *args):
"""
Store 1-D arrays as columns of a table inside a virtual file.
Expand Down Expand Up @@ -1379,6 +1379,17 @@ def virtualfile_from_vectors(self, vectors):
... print(fout.read().strip())
<vector memory>: N = 3 <1/3> <4/6> <7/9>
"""
if len(args) > 0:
warnings.warn(
"Passing multiple arguments to Session.virtualfile_fro_vectors is "
"deprecated since v0.14.0 and will be unsupported in v0.16.0. "
"Pass all vectors as a single tuple instead, e.g., "
"Use `with lib.virtualfile_from_vectors((x, y, z)) as vfile` "
"instead of `with lib.virtualfile_from_vectors(x, y, z) as vfile`.",
category=FutureWarning,
stacklevel=3,
)
vectors = (vectors, *args)
# Conversion to a C-contiguous array needs to be done here and not in
# put_vector or put_strings because we need to maintain a reference to
# the copy while it is being used by the C API. Otherwise, the array
Expand Down
23 changes: 23 additions & 0 deletions pygmt/tests/test_clib_virtualfile_from_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,26 @@ def test_virtualfile_from_vectors_arraylike():
bounds = "\t".join([f"<{min(i):.0f}/{max(i):.0f}>" for i in (x, y, z)])
expected = f"<vector memory>: N = {size}\t{bounds}\n"
assert output == expected


def test_virtualfile_from_vectors_args():
"""
Test the backward compatibility of the deprecated syntax for passing multiple
vectors.
This test is the same as test_virtualfile_from_vectors_arraylike, but using the
old syntax.
"""
size = 13
x = list(range(0, size, 1))
y = tuple(range(size, size * 2, 1))
z = range(size * 2, size * 3, 1)
with pytest.warns(FutureWarning, match="virtualfile_from_vectors"):
with clib.Session() as lib:
with lib.virtualfile_from_vectors(x, y, z) as vfile:
with GMTTempFile() as outfile:
lib.call_module("info", [vfile, f"->{outfile.name}"])
output = outfile.read(keep_tabs=True)
bounds = "\t".join([f"<{min(i):.0f}/{max(i):.0f}>" for i in (x, y, z)])
expected = f"<vector memory>: N = {size}\t{bounds}\n"
assert output == expected

0 comments on commit f5b0e8e

Please sign in to comment.