Skip to content

parse_ixbrl does not close the file it opens #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cdm-analytics opened this issue Nov 22, 2023 · 4 comments
Open

parse_ixbrl does not close the file it opens #121

cdm-analytics opened this issue Nov 22, 2023 · 4 comments
Assignees

Comments

@cdm-analytics
Copy link

The function, "parse_ixbrl" in xbrl/instance.py opens a file with the id "instance_file" and doesn't close the file.

I am looping through large number of XBRL documents and running into an issue where I am hitting a system limit on open files.

As a work-around, I increased the system limit on open files. That helped but now I am running into memory issues.

I wonder if using "with" or explicitly closing the file would eliminate the issue I'm having.

Great module! Thanks!

@cdm-analytics
Copy link
Author

OK, I think I fixed both problems I was having with the following code change to the "parse_ixbrl" function in xbrl/instance.py:

    instance_file = open(instance_path, "r", encoding=encoding)
    contents = instance_file.read()
    instance_file.close()
    pattern = r'<[ ]*script.*?\/[ ]*script[ ]*>'
    contents = re.sub(pattern, '', contents, flags=(re.IGNORECASE | re.MULTILINE | re.DOTALL))
    with StringIO(contents) as contents_object:
        root: ET.ElementTree = parse_file(contents_object)

The "close" line fixed the issue I had with opening too many files and the "with" code fixed the issue I had with maxing out my system's memory.

@manusimidt
Copy link
Owner

Ah, so you added the with block, right?
Will investigate this for next major release. This is probably also relevant for the parse_xbrl function and not just parse_ixbrl. Thanks for the suggestion.

@manusimidt manusimidt self-assigned this Nov 26, 2023
@cdm-analytics
Copy link
Author

I also added the instance_file.close() line which could alternatively be done using a with block.

Thanks for the follow up! It's a great module that has been really helpful to me.

@s-kust
Copy link

s-kust commented Jan 7, 2024

@cdm-analytics how about also using with instead of instance_file.close() - ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants