Skip to content
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

ctypes.util.find_msvcrt() function #46126

Closed
theller opened this issue Jan 11, 2008 · 7 comments
Closed

ctypes.util.find_msvcrt() function #46126

theller opened this issue Jan 11, 2008 · 7 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@theller
Copy link

theller commented Jan 11, 2008

BPO 1793
Nosy @theller, @amauryfa, @tiran
Files
  • ctypes-util.patch: Patch for Lib\ctypes\util.py
  • 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/theller'
    closed_at = <Date 2008-05-16.20:13:58.011>
    created_at = <Date 2008-01-11.14:59:37.850>
    labels = ['extension-modules', 'type-feature']
    title = 'ctypes.util.find_msvcrt() function'
    updated_at = <Date 2008-05-16.20:13:57.973>
    user = 'https://github.com/theller'

    bugs.python.org fields:

    activity = <Date 2008-05-16.20:13:57.973>
    actor = 'theller'
    assignee = 'theller'
    closed = True
    closed_date = <Date 2008-05-16.20:13:58.011>
    closer = 'theller'
    components = ['Extension Modules']
    creation = <Date 2008-01-11.14:59:37.850>
    creator = 'theller'
    dependencies = []
    files = ['9123']
    hgrepos = []
    issue_num = 1793
    keywords = ['patch']
    message_count = 7.0
    messages = ['59713', '59728', '59732', '59963', '60007', '60023', '66966']
    nosy_count = 3.0
    nosy_names = ['theller', 'amaury.forgeotdarc', 'christian.heimes']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue1793'
    versions = ['Python 2.6']

    @theller
    Copy link
    Author

    theller commented Jan 11, 2008

    I'm uploading this patch for discussion, in case someone cares.

    This code (for Windows) adds a function ctypes.util.find_msvcrt(). This
    function returns the filename of the MSVC runtime library that the
    current Python executable uses. If calling functions from the C runtime
    library, it is very important to use the SAME dll that Python uses.

    Further, this patch changes ctypes.util.find_library(name) so that the
    MSVC runtime library name is returned when searching for "c" or "m".

    @theller theller self-assigned this Jan 11, 2008
    @theller theller added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Jan 11, 2008
    @tiran
    Copy link
    Member

    tiran commented Jan 11, 2008

    In general I like the idea. But wouldn't it be better to have a cross
    platform function which returns the c runtime library? msvcrt?? on
    Windows, libc on Linux and whatever Mac and BSD are using.

    @theller
    Copy link
    Author

    theller commented Jan 11, 2008

    The cross-platform function is ctypes.util.find_library, which is
    currently not very useful on Windows.

    This patch changes it so that, on Windows, find_library("c") or
    find_library("m") finds the MS C runtime lib which exposes functions the
    are typically in libc and libm.

    The runtime lib in Windows is special anyway; the 'open' function, for
    example, is exported as '_open'.

    However, the easiest way on Linux (don't know about other platforms) to
    load the C runtime lib is to use ctypes.CDLL(None), which translates to
    dlopen(NULL) in C. Linux exposes all symbols from all loaded libraries
    to the returned handle.

    @amauryfa
    Copy link
    Member

    I found the code I wrote some time ago for the same purpose:
    (in pypy, which uses ctypes a lot)

    import sys
    if sys.platform == 'win32':
        # Parses sys.version and deduces the version of the compiler
        import distutils.msvccompiler
        version = distutils.msvccompiler.get_build_version()
        if version is None:
            # This logic works with official builds of Python.
            if sys.version_info < (2, 4):
                clibname = 'msvcrt'
            else:
                clibname = 'msvcr71'
        else:
            if version <= 6:
                clibname = 'msvcrt'
            else:
                clibname = 'msvcr%d' % (version * 10)
    
        # If python was built with in debug mode
        import imp
        if imp.get_suffixes()[0][0] == '_d.pyd':
            clibname += 'd'
    
        standard_c_lib = ctypes.cdll.LoadLibrary(clibname+'.dll')

    This code works on all pythons I have on my machine: official builds,
    custom builds (relase/debug) with several MS compilers...
    I did not test it with other compiled vendors (mingw32...).

    But to me this seems more robust than a text search in the executable.

    @theller
    Copy link
    Author

    theller commented Jan 16, 2008

    Amaury Forgeot d'Arc schrieb:

    This code works on all pythons I have on my machine: official builds,
    custom builds (relase/debug) with several MS compilers...
    I did not test it with other compiled vendors (mingw32...).

    What I do not like about your code is that it imports distutils.
    On the other hand, the code in distutils.msvccompiler.get_build_version
    is so small and simple that it could easily be duplicated in ctypes.util;
    it could even be simplified more because ctypes doesn't work in Python < 2.3.

    What do you think?

    @amauryfa
    Copy link
    Member

    Fine with me.

    @theller
    Copy link
    Author

    theller commented May 16, 2008

    Committed in trunk as rev. 63395. I've changed the code that Amaury
    suggested so that None is returned when get_build_version() returns
    None. Thanks.

    @theller theller closed this as completed May 16, 2008
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants