Skip to content

On windows, "import nul" always succeed #47349

Closed

Description

@amauryfa
BPO 3099
Nosy @birkenfeld, @terryjreedy, @amauryfa, @tjguk, @merwok

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/tjguk'
closed_at = <Date 2013-07-31.15:09:34.597>
created_at = <Date 2008-06-13.01:18:47.570>
labels = ['OS-windows']
title = 'On windows, "import nul" always succeed'
updated_at = <Date 2013-07-31.15:09:34.596>
user = 'https://github.com/amauryfa'

bugs.python.org fields:

activity = <Date 2013-07-31.15:09:34.596>
actor = 'tim.golden'
assignee = 'tim.golden'
closed = True
closed_date = <Date 2013-07-31.15:09:34.597>
closer = 'tim.golden'
components = ['Windows']
creation = <Date 2008-06-13.01:18:47.570>
creator = 'amaury.forgeotdarc'
dependencies = []
files = []
hgrepos = []
issue_num = 3099
keywords = []
message_count = 12.0
messages = ['68115', '68163', '68164', '68169', '68170', '69039', '83451', '83468', '112924', '112927', '137135', '194001']
nosy_count = 7.0
nosy_names = ['georg.brandl', 'terry.reedy', 'amaury.forgeotdarc', 'ocean-city', 'tim.golden', 'eric.araujo', 'markm']
pr_nums = []
priority = 'low'
resolution = 'works for me'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue3099'
versions = ['Python 2.7']

Activity

  1. amauryfa commented on Jun 13, 2008

    @amauryfa
    ContributorAuthor

    on Windows, the stat() function always returns True for some special
    device names (nul, con, com1, lpt1...), even when followed by an extension.

    Thus "import con" manages to find that "con.py" exists, and tries to
    read from it... fun.

    A solution is to use GetFileAttributes() instead.

    Note that on python2.5.2, the error message suggest that we have such a
    problem, but fortunately the error is still an ImportError::
      >>> import nul
      ImportError: DLL load failed: Le module spécifié est introuvable.
  2. birkenfeld commented on Jun 13, 2008

    @birkenfeld
    Member

    The title is misleading -- if what you show is correct then "import nul"
    doesn't succeed :)

  3. amauryfa commented on Jun 13, 2008

    @amauryfa
    ContributorAuthor

    Sorry, I was not clear.
    With python 2.5.2, "import nul" correctly raises ImportError, even if
    the error message is slightly misleading.

    With a recent release25-maint (and all other branches), "import nul"
    does succeed, and creates an empty module.
    "import con" seems to block, it actually waits for the user to enter
    text and type ^Z. Then it prints to the console some bizarre text that
    looks like the content of a .pyc file:

    Python 2.5.3a0 (release25-maint, Jun 11 2008, 13:17:36) [MSC v.1200 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import con
    ^Z
    a=1
    b=2
    c=3
    ^Z
    │≥
        c        ☺   @   s►   d  Z  d☺ Z☺ d☻ S(♥   i☻   i♥   N(☻   t☺   bt☺
      c(    (    (    s♠   con.py   <module>☺   s☻   ♠☺    [27520 refs]
    >>> dir(con)
    ['__builtins__', '__doc__', '__file__', '__name__', 'b', 'c']
    [27533 refs]
  4. amauryfa commented on Jun 13, 2008

    @amauryfa
    ContributorAuthor

    I was wrong. It seems that an installed python works correctly, but a
    python running from its build directory has the problem.

  5. amauryfa commented on Jun 13, 2008

    @amauryfa
    ContributorAuthor

    OK, I think I got is now. The difference is between debug and release
    builds.

    Explanation:

    • in release build, "import nul" calls stat("nul.pyd") which succeeds.
      It then tries LoadLibrary("nul.pyd"), which fails with a DLL error.
    • in debug builds, "import nul" first tries stat("nul_d.pyd") which
      fails. It then tries stat("nul.py"), which succeeds to return an empty file!

    Whaaaa.

  6. ocean-city commented on Jul 1, 2008

    ocean-citymannequin
    Mannequin

    GetFileAttributes() succeeds for "nul", but GetFileAttributesEx() fails.
    Maybe is it good idea to use GetFileAttributesEx()?

    # test code

    import ctypes
    import ctypes.wintypes as type
    
    dll = ctypes.windll.kernel32

    print dll.GetFileAttributesA("nul") # 32

    class WIN32_FILE_ATTRIBUTE_DATA(ctypes.Structure):
    	_fields_ = [("dwFileAttributes", type.DWORD),
    	            ("ftCreationTime", type.FILETIME),
    	            ("ftLastAccessTime", type.FILETIME),
    	            ("ftLastWriteTime", type.FILETIME),
    	            ("nFileSizeHigh", type.DWORD),
    	            ("nFileSizeLow", type.DWORD)]
    
    GetFileExInfoStandard = 0
    
    wfad = WIN32_FILE_ATTRIBUTE_DATA()

    print dll.GetFileAttributesExA("nul", GetFileExInfoStandard,
    ctypes.byref(wfad)) # 0

  7. brettcannon commented on Mar 11, 2009

    @brettcannon
    Member

    Since I don't have access to Windows I am unassigning. Amaury, if this
    is still a problem should we go ahead and switch to
    GetFileAttributesEx()? Or just realize this is a problem for people
    developing Python?

  8. removed their assignment
    on Mar 11, 2009
  9. amauryfa commented on Mar 11, 2009

    @amauryfa
    ContributorAuthor

    It's a problem with debug builds on Windows.
    Lowering priority.

    Also, it is likely that the new import library will correct the problem.

  10. terryjreedy commented on Aug 4, 2010

    @terryjreedy
    Member

    it is likely that the new import library will correct the problem.

    Did it?

    With standard 3.1.2,
    >>> import nul
    ...
    ImportError: DLL load failed: The specified module could not be found.
    >>> import con
    ...
    ImportError: No module named con

    So is there any problem with 2.7, or should we close this?

  11. merwok commented on Aug 4, 2010

    @merwok
    Member

    3.1 does not use importlib for imports.

  12. self-assigned this
    on Aug 13, 2010
  13. markm commented on May 28, 2011

    markmmannequin
    Mannequin

    I am not sure that I fully understand the issue - but it seems that trunk still has this issue.
    As stated by Amaury - this is on DEBUG builds only.

    c:\>pcbuild\python_d.exe
    Python 3.3a0 (default, May 28 2011, 20:22:11) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import nul
    [60967 refs]
    >>> import con
    ^Z
    [60986 refs]
    
    c:\>PCbuild\python.exe
    Python 3.3a0 (default, May 28 2011, 20:25:13) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import nul
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: DLL load failed: The parameter is incorrect.
    >>> import con
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: DLL load failed: The specified module could not be found.
    >>>
  14. tjguk commented on Jul 31, 2013

    @tjguk
    Member

    This one seems to have been fixed by the importlib rebuild. I haven't bothered to trace the code path, but certainly "import nul" returns the expected "ImportError: No module named 'nul'" in both Debug & Release builds.

  15. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions