Potential NameError in copy_file() exception handler #6

Closed
opened 2025-11-15 03:44:10 +00:00 by snegov · 0 comments
Owner

Potential NameError in copy_file() exception handler

Priority: Medium
Component: fs.py
Type: Bug

Description

In the copy_file() function, if the os.open() call for the output file fails, the variable fout will be uninitialized, causing a NameError when the finally block tries to close it.

Location

curateipsum/fs.py:182-198

Current Code

def copy_file(src, dst):
    try:
        fin = os.open(src, READ_FLAGS)
        fstat = os.fstat(fin)
        fout = os.open(dst, WRITE_FLAGS, fstat.st_mode)  # Could fail here
        for x in iter(lambda: os.read(fin, BUFFER_SIZE), b""):
            os.write(fout, x)
    finally:
        try:
            os.close(fout)  # NameError if fout wasn't created
        except (OSError, UnboundLocalError):
            pass

Problem

If line 187 fails, fout is never assigned, leading to UnboundLocalError in the finally block (which is caught but indicates poor code structure).

Proposed Solution

def copy_file(src, dst):
    fin = None
    fout = None
    try:
        fin = os.open(src, READ_FLAGS)
        fstat = os.fstat(fin)
        fout = os.open(dst, WRITE_FLAGS, fstat.st_mode)
        for x in iter(lambda: os.read(fin, BUFFER_SIZE), b""):
            os.write(fout, x)
    finally:
        if fout is not None:
            try:
                os.close(fout)
            except OSError:
                pass
        if fin is not None:
            try:
                os.close(fin)
            except OSError:
                pass

Impact

Medium - Code works but is fragile and harder to maintain.

# Potential NameError in copy_file() exception handler **Priority:** Medium **Component:** fs.py **Type:** Bug ## Description In the `copy_file()` function, if the `os.open()` call for the output file fails, the variable `fout` will be uninitialized, causing a `NameError` when the finally block tries to close it. ## Location `curateipsum/fs.py:182-198` ## Current Code ```python def copy_file(src, dst): try: fin = os.open(src, READ_FLAGS) fstat = os.fstat(fin) fout = os.open(dst, WRITE_FLAGS, fstat.st_mode) # Could fail here for x in iter(lambda: os.read(fin, BUFFER_SIZE), b""): os.write(fout, x) finally: try: os.close(fout) # NameError if fout wasn't created except (OSError, UnboundLocalError): pass ``` ## Problem If line 187 fails, `fout` is never assigned, leading to `UnboundLocalError` in the finally block (which is caught but indicates poor code structure). ## Proposed Solution ```python def copy_file(src, dst): fin = None fout = None try: fin = os.open(src, READ_FLAGS) fstat = os.fstat(fin) fout = os.open(dst, WRITE_FLAGS, fstat.st_mode) for x in iter(lambda: os.read(fin, BUFFER_SIZE), b""): os.write(fout, x) finally: if fout is not None: try: os.close(fout) except OSError: pass if fin is not None: try: os.close(fin) except OSError: pass ``` ## Impact **Medium** - Code works but is fragile and harder to maintain.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: snegov/cura-te-ipsum#6