Python tempfile.TemporaryFile hangs on Windows when no write privilege

JDM

My environment is Python 3.7.2, running on Windows 10. I'm working on a directory-selection widget, and I'm looking for the cleanest+surest method to test whether the selected directory path allows write privilege.

Previously I'd been opening a named file by the usual open() method, writing a few bytes to it, then deleting it -- putting the whole thing in a try-except block. This was OK but it ran the risk of leaving behind an unwanted file. Recently I came across the documentation for tempfile.TemporaryFile(), and this seemed cleaner way to get the same result, with no risk of leaving junk files on the system.

The problem is, tempfile.TemporaryFile() hangs on my system when it's given a dir parameter that's a read-only folder. I've googled around and found this very old bug, but it was written against Python 2.4 and was fixed long ago.

Here's a test script I put together to illustrate the issue. (Note that I've omitted the file-delete that my actual app performs, as it's not relevant for the illustration.)

import os, tempfile

def normOpen(checkPath):
    try:
        with open(os.path.join(checkPath,'x.txt'),'wb') as tf:
            tf.write(b'ABC')
    except Exception as e:
        print('Write disabled for '+checkPath)
        print(str(e))
    else:
        print('Write enabled  for '+checkPath)

def tfOpen(checkPath):
    try:
        with tempfile.TemporaryFile(dir=checkPath) as tf:
            tf.write(b'ABC')
    except Exception as e:
        print('Write disabled for '+checkPath)
        print(str(e))
    else:
        print('Write enabled  for '+checkPath)

tryPath1 = 'C:\\JDM\\Dev_Python\\TMPV\\canwrite'  #Full control path
tryPath2 = 'C:\\JDM\\Dev_Python\\TMPV\\nowrite'   #Read-only path

print('First method - normal file-open')
normOpen(tryPath1)
normOpen(tryPath2)

print('Second method - TemporaryFile')
tfOpen(tryPath1)
tfOpen(tryPath2)

When I run this script, it hangs on the last line and just sits there (Task Manager shows Python consuming about 10-15% CPU).

Windows CMD

Does anyone know what the problem might be? Particularly is this a Python bug, or is there something wrong with my usage of TemporaryFile?

In case it helps, below is the specific privileges Windows shows for each of these folders:

Permissions

JDM

A deeper dive than I'd initially done, turned up the answer. This is indeed a Python bug, reported some time ago but which remains to be addressed.

The comments from eryksun describe the details -- and it's what prompted me to take a closer look at the Python bug database -- so ultimately that's where credit is due. I'm just filling it in here to get the question answered and closed out.

The bug affects only Windows environments, but unfortunately it has the result of rendering tempfile.TemporaryFile unusable on Windows for this common use case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to specify text mode in Python's tempfile.TemporaryFile()?

Python windows privilege escalation

Doesn't python's ffmpy works with temporary files made using tempfile.TemporaryFile?

NotImplementedError: Only tempfile.TemporaryFile is available for use

tempfile.TemporaryFile vs. StringIO

How to write to a tempfile then execute it as a subprocess in Python?

Why is tempfile.TemporaryFile showing invalid path on AWS ECS/docker?

How to run python script with elevated privilege on windows

How to run python script with elevated privilege on windows

Ruby how to write to Tempfile

Trying to write a string to a tempfile

How to write a Tempfile as binary

Python hangs silently on large file write

Python tempfile with a context manager on Windows 10 leads to PermissionError: [Errno 13]

gitlab privilege issue when running go mod tidy on Windows 11

Windows PermissionError & tempfile module

conda install -c conda-forge python-pdal Solving environment: | hangs when running windows 10

tempfile.TemporaryFile() does not work with sqlite3 after being assigned to a variable

Read from url and write to Tempfile

Invoking python under CygWin on Windows hangs

Invoking python under CygWin on Windows hangs

Bokeh Server hangs on Startup (Windows, Python 3.8)

Privilege elevation in Windows with ansible

Save svg to tempfile Python

PHP: socket_write hangs when client disconnects while writing

How can I get the size of a TemporaryFile in python?

Python subprocess hangs as Popen when piping output

python script hangs when writing to /dev/xconsole

Python SpeechRecognition ignores timeout when listening, and hangs