Skip to content

Local file existence checking requires URL modules to be loaded in _datasource.py #12536

Closed
@EelcoPeacs

Description

@EelcoPeacs

While loading a local file with the loadtxt function I received an exception that was related to importing the urlopen module. Since I tried to open a local file I was a bit surprised by this error. Going through the stack-trace I ended up in the _datasource.py file.

Here on line 551, the module is tried to be loaded:

    # We import this here because importing urllib2 is slow and
    # a significant fraction of numpy's total import time.
    if sys.version_info[0] >= 3:
        from urllib.request import urlopen
        from urllib.error import URLError
    else:
        from urllib2 import urlopen
        from urllib2 import URLError

    # Test local path
    if os.path.exists(path):
        return True

I noticed that on line 558 the file is actually being checked for being a local file, which being true in my case should not require the urlopen module to be loaded.

I would like to propose to change the code at line 550 to:

    # Test local path
    if os.path.exists(path):
        return True

    # We import this here because importing urllib2 is slow and
    # a significant fraction of numpy's total import time.
    if sys.version_info[0] >= 3:
        from urllib.request import urlopen
        from urllib.error import URLError
    else:
        from urllib2 import urlopen
        from urllib2 import URLError

This will result in the same behavior but will be quicker when a file is a local file and, in my case prevent the exception.

Reproducing code example:

In my case the exception was cause by the missing _socket object/module which is being imported deep down when importing the urlopen module

Numpy/Python version information:

The proposed change is based on the current code, revision #12388

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions