Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileObjectBase raises when accessing the name property on a closed file #1745

Closed
danmilon opened this issue Jan 14, 2021 · 1 comment · Fixed by #1747
Closed

FileObjectBase raises when accessing the name property on a closed file #1745

danmilon opened this issue Jan 14, 2021 · 1 comment · Fixed by #1747
Labels
Type: Bug Identified as a bug; needs a code change to fix

Comments

@danmilon
Copy link

  • gevent version: 20.12.1 from pypi
  • Python version: 2.7.18 from the official Fedora repos.
  • Operating System: Fedora 5.8.10-200.fc32.x86_64

Description:

FileObject variants that inherit from FileObjectBase raise when accessing the name property after the file has been closed. The file object returned by io.open() doesn't have the same behavior, it returns the file path fine. This breaks some libraries (e.g. atomicwrites which rely on this behaviour.

It happens because in FileObjectCommon.close() we drop the reference to the wrapped object.

I can think of two possible fixes.

  1. Don't drop the self._io reference in close(), instead set a self._closed = True flag to be used in __getattr__(), and add name in _delegate_methods().
  2. In FileObjectCommon.__init__() do self.name = self._io.name.

Thoughts?

What I've run:

import gevent.monkey; gevent.monkey.patch_all()
import gevent.fileobject
import io

f = io.open("/tmp/foo", mode="w")
print("f.name by io.open before close: {}".format(f.name))
f.close()
print("f.name by io.open after close: {}".format(f.name))

f = gevent.fileobject.FileObjectThread(
    "/tmp/foo",
    mode="w",
)

print("f.name by gevent.fileobject before close: {}".format(f.name))
f.close()
print("f.name by gevent.fileobject after close: {}".format(f.name))
@jamadden jamadden added the Type: Bug Identified as a bug; needs a code change to fix label Jan 15, 2021
@jamadden
Copy link
Member

Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Identified as a bug; needs a code change to fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants