Skip to content

Checking the existence of GMTTempFile using "os.path.exists" is invalid #1912

Closed

Description

@seisman

Description of the problem

In our tests, we usually save the output to a GMTTempFile object and then check if the output temporary file exists using the os.path.exists function.

Here is an example test for grd2xyz:

def test_grd2xyz_file_output(grid):
"""
Test that grd2xyz returns a file output when it is specified.
"""
with GMTTempFile(suffix=".xyz") as tmpfile:
result = grd2xyz(grid=grid, outfile=tmpfile.name, output_type="file")
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outfile exists

The problem is, GMTTempFile always creates the 0-byte temporary file when GMTTempFile is called, so the temporary file always exists. It means that we can't use os.path.exists to check if functions like grd2xyz write output to a file or not.

Here is a minimal script to verify it:

import os
from pygmt.helpers import GMTTempFile
with GMTTempFile(suffix="txt") as fp:
    print(fp.name, os.path.exists(fp.name), os.path.getsize(fp.name))

the output is: /tmp/pygmt-mhtfhdmltxt True 0.


Possible solutions:

  • Improve the GMTTempFile class to make sure that the 0-byte temporary file is not created by GMTTempFile
  • Use os.path.getsize to check if the file size is non-zero, rather than using os.path.exists

Activity

  1. added
    bug
    on May 2, 2022
  2. added this to the 0.7.0 milestone on May 3, 2022
  3. changed the title [-]Checking the existence of GMTTempFile using "os.path.exists" are invalid[/-] [+]Checking the existence of GMTTempFile using "os.path.exists" is invalid[/+] on May 6, 2022
  4. maxrjones commented on May 12, 2022

    @maxrjones
    Member

    A third possible solution is to use info/grdinfo to check that the contents are generally as expected.

  5. seisman commented on May 13, 2022

    @seisman
    MemberAuthor

    A third possible solution is to use info/grdinfo to check that the contents are generally as expected.

    We decided to use xarray.testing rather than pygmt.grdinfo for testing grids.

  6. modified the milestones: 0.7.0, 0.8.0 on Jun 23, 2022
  7. seisman commented on Dec 4, 2022

    @seisman
    MemberAuthor

    Possible solutions:

    • Improve the GMTTempFile class to make sure that the 0-byte temporary file is not created by GMTTempFile

    The GMTTempFile class is implemented based on the NamedTempoaryFile class. I think it's not a good idea to change the default behavior.

    • Use os.path.getsize to check if the file size is non-zero, rather than using os.path.exists.

    I think this is the way to go, and Path.stat().st_size is a better choice.

  8. self-assigned this
    on Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions