diff --git a/gnupg/_util.py b/gnupg/_util.py index 6f5402c..179ecbf 100644 --- a/gnupg/_util.py +++ b/gnupg/_util.py @@ -303,22 +303,31 @@ def _has_readwrite(path): """ return os.access(path, os.R_OK ^ os.W_OK) -def _is_file(input): +def _is_file(filename): """Check that the size of the thing which is supposed to be a filename has size greater than zero, without following symbolic links or using :func:os.path.isfile. - :param input: An object to check. + :param filename: An object to check. :rtype: bool - :returns: True if :param:input is file-like, False otherwise. + :returns: True if **filename** is file-like, False otherwise. """ try: - assert os.lstat(input).st_size > 0, "not a file: %s" % input - except (AssertionError, TypeError, IOError, OSError) as err: - log.error(err.message, exc_info=1) - return False + statinfo = os.lstat(filename) + log.debug("lstat(%r) with type=%s gave us %r" + % (repr(filename), type(filename), repr(statinfo))) + if not (statinfo.st_size > 0): + raise ValueError("'%s' appears to be an empty file!" % filename) + except OSError as oserr: + log.error(oserr) + if filename == '-': + log.debug("Got '-' for filename, assuming sys.stdin...") + return True + except (ValueError, TypeError, IOError) as err: + log.error(err) else: return True + return False def _is_stream(input): """Check that the input is a byte stream.