Coder Social home page Coder Social logo

Comments (4)

mschwager avatar mschwager commented on June 18, 2024

Hi there!

The tempfile.mktemp function should be avoided because it can introduce race conditions. However, the similarly named tempfile.mkstemp function is okay. The Correct code section above is using the approach recommended by the Python documentation.

Are you seeing a false positive where Dlint is detecting mkstemp as well? It should only be detecting the function without the "s".

from dlint.

hartwork avatar hartwork commented on June 18, 2024

The tempfile.mktemp function should be avoided because it can introduce race conditions.

Agreed, yes!

However, the similarly named tempfile.mkstemp function is okay.

πŸ‘

The Correct code section above is using the approach recommended by the Python documentation.

I think you refer to this block at https://docs.python.org/3.8/library/tempfile.html#deprecated-functions-and-variables:

>>> f = NamedTemporaryFile(delete=False)
>>> f.name
'/tmp/tmptjujjt'
>>> f.write(b"Hello World!\n")
13
>>> f.close()
>>> os.unlink(f.name)
>>> os.path.exists(f.name)
False

I consider their code to be demo code rather than an actual recommendation.
My understanding is that when you need to work with a filename, there are two cases:

a) The file is so short-lived that use of a context manager like β€” this is β€” fine:

with tempfile.NamedTemporaryFile() as f:
    pass  # work with f.file.fileno() and/or f.name here
# f.name deleted now

b) The file lives longer and regular use of a context manager is not an option:

fd, temp_filename = tempfile.mkstemp()                                                                                                                              
os.close(fd)                                                                                                                                                   
# do as much or little with the temp file here as needed
os.remove(filename)

If you want to keep using NamedTemporaryFile(delete=False) for the example in the docs I would suggest to change current…

import tempfile

fd = tempfile.NamedTemporaryFile(delete=False)
temp_filename = fd.name

…to…

import os, tempfile

f = tempfile.NamedTemporaryFile(delete=False)
# .. do work with f.name here ..
os.remove(f.name)

to be more complete and use f (file) rather than mis-leading fd (file descriptor) which is a plain integer most of the time.

What do you think?

Are you seeing a false positive where Dlint is detecting mkstemp as well? It should only be detecting the function without the "s".

I have no report like that, no.

from dlint.

mschwager avatar mschwager commented on June 18, 2024

Thanks for all these descriptive examples!

Regarding short-lived vs. long-lived files, I think the easiest way to cover all these cases is to avoid prescriptive docs for all close and remove scenarios (and context managers). These examples are just trying to show minimal snippets of roughly equivalent code and not fully production-ready code.

Good call with fd vs. f. That recommendation is also consistent with what the Python docs use. I'd be happy to accept a PR or make that change if that works for you πŸ‘

from dlint.

hartwork avatar hartwork commented on June 18, 2024

I see your point about close and remove. I'm curious what you think of the approach taken with #22.

from dlint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.