Skip to content

[Windows] [1.0] [Bug] - NamedTemporaryFile produces a lock in windows that results in PermissionDenied when attempting to write. #38

@ZacharyACoon

Description

@ZacharyACoon

Summary

Windows apparently does locking by default apparently, which trips up the next open instruction with a "Permission Denied"

Bug Location

\jupyterscad\_render.py:50

Solution

Explicitly specify deletion, or just write out opening and writing a file without temporary file, or dive into why this does locking nonsense.

with tempfile.NamedTemporaryFile(suffix=".txt", dir=".") as tf:
with tempfile.NamedTemporaryFile(suffix=".txt", delete=False, dir=".") as tf:

Isolated Example

❌❌❌ Reproduces the failure.

import tempfile

with tempfile.NamedTemporaryFile(suffix=".txt", delete=True, dir=".") as tf:
    with open(tf.name, "w") as f:
        f.write("hi")

✅✅✅ Solution

import tempfile

with tempfile.NamedTemporaryFile(suffix=".txt", delete=False, dir=".") as tf:
    with open(tf.name, "w") as f:
        f.write("hi")

Original Library Error

from jupyterscad import view, render_stl
render_stl(obj, outfile="test.stl",  openscad_exec=r"C:\Program Files\OpenSCAD\openscad.exe")
PermissionError                           Traceback (most recent call last)
Cell In[31], line 3
      1 from jupyterscad import view, render_stl
----> 3 render_stl(obj, outfile="test.stl",  openscad_exec=r"C:\Program Files\OpenSCAD\openscad.exe")
        obj = <solid.objects.difference object at 0x00000280DC31C410>
File ~\Desktop\emdr_bunny-twig\modeling\.venv\Lib\site-packages\jupyterscad\_render.py:51, in render_stl(obj=<solid.objects.difference object>, outfile='test.stl', openscad_exec=r'C:\Program Files\OpenSCAD\openscad.exe')
     36 """Render a stl from an OpenSCAD object.
     37 
     38 Typical usage example:
   (...)     48     exceptions.OpenSCADException: An error occurred running OpenSCAD.
     49 """
     50 with tempfile.NamedTemporaryFile(suffix=".scad", dir=".") as scad_tmp_file:
---> 51     with open(scad_tmp_file.name, "w") as fp:
        scad_tmp_file = <tempfile._TemporaryFileWrapper object at 0x00000280DCB23CB0>
        scad_tmp_file.name = 'C:\\Users\\Zac\\Desktop\\emdr_bunny-twig\\modeling\\tmpc3czr308.scad'     52         fp.write(str(obj))
     54     process(scad_tmp_file.name, outfile, executable=openscad_exec)

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Zac\\Desktop\\emdr_bunny-twig\\modeling\\tmpc3czr308.scad'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions