Skip to content

Redundant Code in FileString __cinit__ #19464

@JettHiggins

Description

@JettHiggins

Description

I have been looking through the low level functions of astropy for GsoC 2026.
Looking at the FileString class in io/ascii/cparser.pyx

def __cinit__(self, fname):
        self.fhandle = open(fname, 'r')
        if not self.fhandle:
            raise OSError('File "{0}" could not be opened'.format(fname))
        self.mmap = mmap.mmap(self.fhandle.fileno(), 0, access=mmap.ACCESS_READ)
        cdef Py_ssize_t buf_len = len(self.mmap)
        PyObject_GetBuffer(self.mmap, &self.buf, PyBUF_SIMPLE)
        self.mmap_ptr = self.buf.buf     

I was thinking wouldnt the check if not self.fhanlde be redundant because if the open failed it would immediately raise the error and if it was successful that code would be skipped, if so that error would never be raised.

Expected behavior

No response

How to Reproduce

Versions

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions