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

open() and file.write() without file.close() #82729

Closed
kryptomatrix mannequin opened this issue Oct 21, 2019 · 2 comments
Closed

open() and file.write() without file.close() #82729

kryptomatrix mannequin opened this issue Oct 21, 2019 · 2 comments
Labels
3.7 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@kryptomatrix
Copy link
Mannequin

kryptomatrix mannequin commented Oct 21, 2019

BPO 38548
Nosy @vstinner
Superseder
  • bpo-17852: Built-in module _io can lose data from buffered files in reference cycles
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-10-21.12:20:44.094>
    created_at = <Date 2019-10-21.12:12:20.903>
    labels = ['type-feature', '3.7', 'docs']
    title = 'open() and file.write() without file.close()'
    updated_at = <Date 2019-10-21.12:20:44.092>
    user = 'https://bugs.python.org/kryptomatrix'

    bugs.python.org fields:

    activity = <Date 2019-10-21.12:20:44.092>
    actor = 'vstinner'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-10-21.12:20:44.094>
    closer = 'vstinner'
    components = ['Documentation']
    creation = <Date 2019-10-21.12:12:20.903>
    creator = 'kryptomatrix'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38548
    keywords = []
    message_count = 2.0
    messages = ['355062', '355063']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'docs@python', 'kryptomatrix']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '17852'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue38548'
    versions = ['Python 3.7']

    @kryptomatrix
    Copy link
    Mannequin Author

    kryptomatrix mannequin commented Oct 21, 2019

    Consider the following program:

    f = open("out.txt", "w")
    f.write("abc\n")
    exit(0)

    Please note the absence of f.close().
    The documentation
    https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
    says that you should use f.close() or with f = open(), but is not clear whether the program above without f.close() is guaranteed to write. The tutorial says:
    "If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times."
    For me this sounds like even without f.close() the file is guaranteed to be written. If it is not guaranteed to be written, you should fix the documentation, if it is guaranteed to be written, then I will open another issue because the following program does not write into out.txt on my machine:

    from sympy.core import AtomicExpr
    class MyWeirdClass(AtomicExpr):
    	def __init__(self):
    		pass
    f = open("out.txt", "w")
    f.write("abc\n")
    exit(0)

    Note: sys.version is: "3.7.3 (default, Oct 7 2019, 12:56:13) \n[GCC 8.3.0]"

    @kryptomatrix kryptomatrix mannequin added the 3.7 (EOL) end of life label Oct 21, 2019
    @kryptomatrix kryptomatrix mannequin assigned docspython Oct 21, 2019
    @kryptomatrix kryptomatrix mannequin added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Oct 21, 2019
    @vstinner
    Copy link
    Member

    The issue is a duplicate of bpo-17852.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant