Skip to content

Unix-only crypt should not be present on Windows. #69359

Closed

Description

@terryjreedy
BPO 25172
Nosy @terryjreedy, @pfmoore, @tjguk, @zware, @zooba, @miss-islington, @shireenrao
PRs
  • bpo-25172: Raise appropriate ImportError msg when crypt module used on Windows #15149
  • [3.8] bpo-25172: Raise appropriate ImportError msg when crypt module used on Windows (GH-15149) #15182
  • bpo-25172: Add test for crypt ImportError msg for Windows #15252
  • [3.8] bpo-25172: Add test for crypt ImportError on Windows (GH-15252) #15263
  • bpo-25172: Reduce scope of crypt import tests #17881
  • [3.8] bpo-25172: Reduce scope of crypt import tests (GH-17881) #17922
  • 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 = None
    closed_at = <Date 2020-01-09.17:40:12.788>
    created_at = <Date 2015-09-19.00:49:31.196>
    labels = ['easy', 'type-bug', '3.8', '3.9', 'library', 'OS-windows']
    title = 'Unix-only crypt should not be present on Windows.'
    updated_at = <Date 2020-01-09.17:40:12.788>
    user = 'https://github.com/terryjreedy'

    bugs.python.org fields:

    activity = <Date 2020-01-09.17:40:12.788>
    actor = 'steve.dower'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-01-09.17:40:12.788>
    closer = 'steve.dower'
    components = ['Library (Lib)', 'Windows']
    creation = <Date 2015-09-19.00:49:31.196>
    creator = 'terry.reedy'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 25172
    keywords = ['patch', 'easy', 'newcomer friendly']
    message_count = 40.0
    messages = ['251046', '348935', '349097', '349111', '349114', '349115', '349116', '349117', '349118', '349125', '349129', '349130', '349138', '349143', '349251', '349252', '349253', '349254', '349256', '349257', '349258', '349259', '349262', '349265', '349275', '349566', '349568', '349569', '349615', '349619', '349620', '349621', '349623', '349624', '356914', '358170', '359467', '359469', '359686', '359688']
    nosy_count = 8.0
    nosy_names = ['terry.reedy', 'paul.moore', 'jafo', 'tim.golden', 'zach.ware', 'steve.dower', 'miss-islington', 'shireenrao']
    pr_nums = ['15149', '15182', '15252', '15263', '17881', '17922']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue25172'
    versions = ['Python 3.8', 'Python 3.9']

    Activity

    1. terryjreedy commented on Sep 19, 2015

      @terryjreedy
      MemberAuthor

      Except for crypt, all the modules labeled 'Unix' or 'Linux' on the module index https://docs.python.org/3/py-modindex.html are absent from /Lib on Windows. 'import xyz' fails with "ImportError: no module named 'zyz'". (I presume the same is true on unix for Windows-only modules.)

      However, crypt is present, and 'import crypt' fails with "...'_crypt'", leading one to think that the C accelerator should be present but is not. Assuming that _crypt should (cannot) not be present, please add crypt to the list of modules omitted from the Windows installer, however this is done. And if 'unix-only' is obsolete and _crypt should be present, please fix this instead. ;-)

      This is 3.x issue only, as crypt is not present in my Windows 2.7.10 /Lib. The fact that is was somehow added to some 3.x prompted me to look and see if there is anything useful without _crypt present. I think not.

    2. zooba commented on Aug 2, 2019

      @zooba
      Member

      Marking this easy/newcomer friendly.

      This should catch the ModuleNotFoundError raised when _crypt is missing and raise a more informative ImportError saying that crypt is unsupported.

      All the other modules that are missing are native extension modules that are not built - this is the only one that is a .py file. We don't exclude any other Lib\*.py files from the distribution.

    3. shireenrao commented on Aug 6, 2019

      shireenraomannequin
      Mannequin

      Hello
      I would like to work on this issue. First time contributor here. Would putting the import _crypt statement on line 3 in crypt.py inside a try/except block and checking to see if platform is windows then give an appropriate message be a good fix?

      Thank you
      -Srinivas

    4. zooba commented on Aug 6, 2019

      @zooba
      Member

      That sounds good to me, though you may want to propose the error message here first so we can get the wording right - probably not everyone will be watching github PR.

      It probably makes sense to raise a different error message on platforms where we do expect it to have been built - the same confusion could arise there.

      Some starting points (that will likely need improvement):

      win32: raise ImportError("The crypt module is not supported on Windows")

      not win32: raise ImportError("The required _crypt module was not built as part of CPython")

    5. shireenrao commented on Aug 6, 2019

      shireenraomannequin
      Mannequin

      What is the recommended way to check for platform in cpython? Is it using the sys module or os module? As for the error messages I couldn't think of anything better then what you suggested :).

      win32: raise ImportError("The crypt module is not supported on Windows")

      not win32: raise ImportError("The required _crypt module was not built as part of CPython")

    6. zooba commented on Aug 6, 2019

      @zooba
      Member

      sys.platform is the value that reflects how Python was compiled - sys.platform == 'win32' means compiled for Windows. And since this issue is related to compilation, it makes the most sense to use sys.

      (os.name is also based on compilation, but it really only reflects the POSIX implementation in use, which is why sys is better.)

    7. 23 remaining items

    8. miss-islington commented on Aug 13, 2019

      @miss-islington
      Contributor

      New changeset 7f7f747 by Miss Islington (bot) in branch '3.8':
      bpo-25172: Raise appropriate ImportError msg when crypt module used on Windows (GH-15149)
      7f7f747

    9. zooba commented on Aug 13, 2019

      @zooba
      Member

      New changeset 243a73d by Steve Dower (shireenrao) in branch 'master':
      bpo-25172: Add test for crypt ImportError on Windows (GH-15252)
      243a73d

    10. zooba commented on Aug 13, 2019

      @zooba
      Member

      Thanks for the patches, Srinivas!

    11. miss-islington commented on Aug 13, 2019

      @miss-islington
      Contributor

      New changeset e7ec9e0 by Miss Islington (bot) in branch '3.8':
      bpo-25172: Add test for crypt ImportError on Windows (GH-15252)
      e7ec9e0

    12. shireenrao commented on Aug 13, 2019

      shireenraomannequin
      Mannequin

      Thank you Steve for accepting my pull requests.

      I was surprised to see the methods in the class and its decorators getting evaluated and causing the failures initially, but I then realized that the code is parsed before execution and that's when I was seeing the failure.

    13. xdegaye commented on Nov 18, 2019

      xdegayemannequin
      Mannequin

      test_crypt fails on android following last changes made at 243a73d.
      The android libc does not have a crypt() function and the _crypt module is not built.

      generic_x86_64:/data/local/tmp/python $ python
      Python 3.9.0a0 (heads/abifa-dirty:cf805c25e6, Nov 18 2019, 16:40:26)
      [Clang 8.0.2 (https://android.googlesource.com/toolchain/clang 40173bab62ec7462 on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import crypt
      Traceback (most recent call last):
        File "/data/local/tmp/python/lib/python3.9/crypt.py", line 6, in <module>
          import _crypt
      ModuleNotFoundError: No module named '_crypt'
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/data/local/tmp/python/lib/python3.9/crypt.py", line 11, in <module>
          raise ImportError("The required _crypt module was not built as part of CPython")
      ImportError: The required _crypt module was not built as part of CPython
      >>>

      generic_x86_64:/data/local/tmp/python $ python -m test -v test_crypt
      == CPython 3.9.0a0 (heads/abifa-dirty:cf805c25e6, Nov 18 2019, 16:40:26) [Clang 8.0.2 (https://andro
      id.googlesource.com/toolchain/clang 40173bab62ec7462
      == Linux-3.10.0+-x86_64-with-libc little-endian
      == cwd: /data/local/tmp/python/tmp/test_python_3523
      == CPU count: 2
      == encodings: locale=UTF-8, FS=utf-8
      0:00:00 Run tests sequentially
      0:00:00 [1/1] test_crypt
      test_blowfish_rounds (test.test_crypt.CryptTestCase) ... skipped 'Not supported on Windows'
      test_crypt (test.test_crypt.CryptTestCase) ... skipped 'Not supported on Windows'
      test_invalid_rounds (test.test_crypt.CryptTestCase) ... skipped 'Not supported on Windows'
      test_methods (test.test_crypt.CryptTestCase) ... skipped 'Not supported on Windows'
      test_salt (test.test_crypt.CryptTestCase) ... skipped 'Not supported on Windows'
      test_saltedcrypt (test.test_crypt.CryptTestCase) ... skipped 'Not supported on Windows'
      test_sha2_rounds (test.test_crypt.CryptTestCase) ... skipped 'Not supported on Windows'
      test_failure_only_for_windows (test.test_crypt.TestWhyCryptDidNotImport) ... FAIL
      test_import_failure_message (test.test_crypt.TestWhyCryptDidNotImport) ... FAIL

      ======================================================================
      FAIL: test_failure_only_for_windows (test.test_crypt.TestWhyCryptDidNotImport)
      ----------------------------------------------------------------------

      Traceback (most recent call last):
        File "/data/local/tmp/python/lib/python3.9/test/test_crypt.py", line 16, in test_failure_only_for_
      windows
          self.assertEqual(sys.platform, 'win32')
      AssertionError: 'linux' != 'win32'
      - linux
      + win32

      ======================================================================
      FAIL: test_import_failure_message (test.test_crypt.TestWhyCryptDidNotImport)
      ----------------------------------------------------------------------

      Traceback (most recent call last):
        File "/data/local/tmp/python/lib/python3.9/test/test_crypt.py", line 19, in test_import_failure_message
          self.assertIn('not supported', IMPORT_ERROR)
      AssertionError: 'not supported' not found in 'The required _crypt module was not built as part of CPython'

      Ran 9 tests in 0.008s

      FAILED (failures=2, skipped=7)
      test test_crypt failed
      test_crypt failed

      == Tests result: FAILURE ==

      1 test failed:
      test_crypt

      Total duration: 165 ms
      Tests result: FAILURE

    14. xdegaye commented on Dec 10, 2019

      xdegayemannequin
      Mannequin

      Not interested anymore in android stuff.

    15. shireenrao commented on Jan 6, 2020

      shireenraomannequin
      Mannequin

      @steve.dower - Does this fix need more work for android? @xdegaye says he does not need this for android anymore.

    16. zooba commented on Jan 6, 2020

      @zooba
      Member

      I started creating an example of improving the skip cases, and ended up just making a PR. It should:

      • expect a helpful error on Windows
      • work fine on other platforms with crypt
      • skip all tests on other platforms without crypt
    17. zooba commented on Jan 9, 2020

      @zooba
      Member

      New changeset ed36781 by Steve Dower in branch 'master':
      bpo-25172: Reduce scope of crypt import tests (GH-17881)
      ed36781

    18. miss-islington commented on Jan 9, 2020

      @miss-islington
      Contributor

      New changeset 8c08518 by Miss Islington (bot) in branch '3.8':
      bpo-25172: Reduce scope of crypt import tests (GH-17881)
      8c08518

    19. 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

    No one assigned

      Labels

      3.8 (EOL)end of life3.9 (EOL)end of lifeOS-windowseasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions