{"issue":{"state":"open","pull_request":{"diff_url":null,"html_url":null,"patch_url":null},"milestone":null,"user":{"avatar_url":"https://secure.gravatar.com/avatar/7c0fa8fb73fe4c951ea79b476007c77d?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"7c0fa8fb73fe4c951ea79b476007c77d","url":"https://api.github.com/users/victorhooi","login":"victorhooi","id":243499},"assignee":null,"updated_at":"2011-11-15T06:13:57Z","labels":[],"closed_at":null,"comments":0,"url":"https://api.github.com/repos/robey/paramiko/issues/53","body":"Hi,\r\n\r\nI had some Python code with two functions, let's call them alice() and bob().\r\n\r\nalice() would call bob(), and bob() would open up a SSH session, and return a SFTP file handler to alice().\r\n\r\n```\r\n#!/usr/bin/env python2\r\nimport paramiko\r\n\r\ndef get_instances_cfg():\r\n    client = paramiko.SSHClient()\r\n    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\r\n    client.connect('some_server', username='victorhooi')\r\n    sftp = client.open_sftp()\r\n    file = sftp.file('/tmp/instances.cfg')\r\n\r\n    # This would print out the file fine.\r\n    # for row in file:\r\n    #    print(row)\r\n\r\n    return file\r\n    #sftp.close()\r\n    #client.close()\r\n\r\ndef get_root_directory():\r\n    instances_cfg = get_instances_cfg()\r\n\r\n    # The following would print out nothing.\r\n    for row in instances_cfg:\r\n        print(row)\r\n\r\nif __name__ == '__main__':\r\n    get_root_directory()\r\n```\r\n\r\nI could iterate over the file handler fine in bob(), however in alice() it seemed to silently fail, printing nothing.\r\n\r\nAnyhow, somebody pointed out to me that it was probably a scoping issue - when I left bob(), client and sftp would go out of scope, even though I returned file.\r\n\r\nI made client, sftp and sftp global, and this then worked fine - I could iterate over file in alice().\r\n\r\nMy question is - I assume this is by design, right? The SFTP file handler won't work if the client doesn't exist in your current scope?\r\n\r\nShould there be some kind of error message or exception raised if you try to access a SFTP file handler that doesn't have the original SSHClient or SFTP session around?\r\n\r\nIn my case, it seemed to be a valid SFTP file handler, but it just didn't contain any rows.\r\n\r\nCheers,\r\nVictor","html_url":"https://github.com/robey/paramiko/issues/53","title":"SFTP File Handler doesn't raise Exceptions if the SSH Client or SFTP session are out of scope","number":53,"id":2240546,"created_at":"2011-11-15T06:13:57Z"},"action":"opened"}