python / cpython

The Python programming language
https://www.python.org
Other
62.5k stars 30k forks source link

PyRun_String not exported in python38.dll #81370

Closed 50363613-ac32-4562-87b1-ef078e34ece0 closed 5 years ago

50363613-ac32-4562-87b1-ef078e34ece0 commented 5 years ago
BPO 37189
Nosy @pfmoore, @vstinner, @tjguk, @benjaminp, @zware, @eryksun, @zooba, @miss-islington
PRs
  • python/cpython#14142
  • python/cpython#14177
  • 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: ```python assignee = None closed_at = created_at = labels = ['3.8', '3.9', 'OS-windows'] title = 'PyRun_String not exported in python38.dll' updated_at = user = 'https://bugs.python.org/cgohlke' ``` bugs.python.org fields: ```python activity = actor = 'vstinner' assignee = 'none' closed = True closed_date = closer = 'vstinner' components = ['Windows'] creation = creator = 'cgohlke' dependencies = [] files = [] hgrepos = [] issue_num = 37189 keywords = ['patch'] message_count = 18.0 messages = ['344905', '344982', '344989', '344993', '345056', '345792', '345809', '345871', '345873', '345915', '345919', '345926', '345928', '345929', '345930', '345931', '345934', '345975'] nosy_count = 10.0 nosy_names = ['paul.moore', 'vstinner', 'tim.golden', 'pyscripter', 'benjamin.peterson', 'cgohlke', 'zach.ware', 'eryksun', 'steve.dower', 'miss-islington'] pr_nums = ['14142', '14177'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue37189' versions = ['Python 3.8', 'Python 3.9'] ```

    50363613-ac32-4562-87b1-ef078e34ece0 commented 5 years ago

    While testing third party packages on Python 3.8.0b1 for Windows, I noticed that the PyRun_String function is no longer exported from python38.dll.

    Is this intentional? I can't see this mentioned at \https://docs.python.org/3.8/whatsnew/3.8.html\ or \https://docs.python.org/3.8/c-api/veryhigh.html#c.PyRun_String\

    This change breaks existing code. But then PyRun_String is easy to replace with PyRun_StringFlags.

    zooba commented 5 years ago

    Guessing Victor may have touched something in this area.

    Was it exported before? Or did we just have a macro for it? Maybe the macro was moved to an internal header by mistake?

    50363613-ac32-4562-87b1-ef078e34ece0 commented 5 years ago

    PyRun_String was exported at least since python23.dll.

    Python.Net relies on it at \https://github.com/pythonnet/pythonnet/blob/master/src/runtime/runtime.cs#L858\

    zware commented 5 years ago

    A look through git log -p looks like bpo-34646 is likely to be related.

    benjaminp commented 5 years ago

    It shouldn't break existing code because PyRun_String has a macro expansion to PyRun_StringFlags. ABI compatibility between major releases is not provded.

    ac970517-7943-4610-bdab-4045a31a9505 commented 5 years ago

    This does break PyScripter Python for Delphi as well. The question whether this change was intentional in which case it would need to be explained and documented, or accidental and will be reversed begs an answer.

    vstinner commented 5 years ago

    Attached PR 14142 fix bpo-34646 regression.

    It shouldn't break existing code because PyRun_String has a macro expansion to PyRun_StringFlags. ABI compatibility between major releases is not provided.

    Many applications don't use Python header files, but access directly libpython. For example, py2app uses dlsym(): https://bitbucket.org/ronaldoussoren/py2app/src/default/py2app/apptemplate/src/main.c

    PyInstaller uses GetProcAddress() on Windows or dlsym() on other platforms:

    That's why PyRun_String() is defined as an alias using a macro *and* as a function in pythonrun.c:

    #undef PyRun_String
    PyObject *
    PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
    {
        return PyRun_StringFlags(str, s, g, l, NULL);
    }
    vstinner commented 5 years ago

    A look through git log -p looks like bpo-34646 is likely to be related.

    New changeset e5024517811ee990b770fca0ba7058742d00e032 by Benjamin Peterson in branch 'master': closes bpo-34646: Remove PyAPI_* macros from declarations. (GH-9218) https://github.com/python/cpython/commit/e5024517811ee990b770fca0ba7058742d00e032

    Extract of this change:

    
     #undef PyRun_String
    -PyAPI_FUNC(PyObject *)
    +PyObject *
     PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
     {
         return PyRun_StringFlags(str, s, g, l, NULL);
     }
    

    On Windows, this change removed dllexport from PyRun_String(). My PR 14142 adds it back, to pythonrun.h.

    zooba commented 5 years ago

    I haven't fully tested this, but a suitable test using ctypes might look like:

    py = ctypes.PyDLL("", handle=sys.dllhandle)
    missing = {name for name in EXPECTED_NAMES if not hasattr(py, name)}
    # assert 'missing' is empty
    vstinner commented 5 years ago

    I tested the following code: ---

    import ctypes, sys 
    names = """
    PyRun_String
    PyRun_AnyFile
    PyRun_AnyFileEx
    PyRun_AnyFileFlags
    PyRun_SimpleString
    PyRun_SimpleFile
    PyRun_SimpleFileEx
    PyRun_InteractiveOne
    PyRun_InteractiveLoop
    PyRun_File
    PyRun_FileEx
    PyRun_FileFlags
    """
    api = ctypes.pythonapi
    api2 = ctypes.PyDLL("", handle=sys.dllhandle)
    for name in names.split():
        if not hasattr(api, name) or not hasattr(api2, name):
            print("MISSING NAME", name)

    Current output:

    Missing names ['PyRun_AnyFile', 'PyRun_AnyFileEx', 'PyRun_File', 'PyRun_FileEx', 'PyRun_FileFlags', 'PyRun_InteractiveLoop', 'PyRun_InteractiveOne', 'PyRun_Simp leFile', 'PyRun_SimpleFileEx', 'PyRun_SimpleString', 'PyRun_String']

    With my PR 14142:

    Missing names []

    eryksun commented 5 years ago

    api = ctypes.pythonapi api2 = ctypes.PyDLL("", handle=sys.dllhandle)

    Those should be the same. In Windows, pythonapi is defined as PyDLL("python dll", None, sys.dllhandle).

    Wouldn't it be better to add a function to _testcapi that checks GetProcAddress(PyWin_DLLhModule, name)?

    vstinner commented 5 years ago

    "Those should be the same."

    Well, I wasn't 100% sure so I tested both. At least, I can now confirm that missing symbols are now exposed in both :-D

    zooba commented 5 years ago

    Ah, that's what ctypes.pythonapi is :) I looked at PyDLL first and figured it out from there.

    Should we add a regression test to avoid this happening in the future?

    vstinner commented 5 years ago

    New changeset 343ed0ffe0d5ddd4f17c31e14a656a04ac7dfc19 by Victor Stinner in branch 'master': bpo-37189: Export old PyRun_XXX() functions (bpo-14142) https://github.com/python/cpython/commit/343ed0ffe0d5ddd4f17c31e14a656a04ac7dfc19

    vstinner commented 5 years ago

    Should we add a regression test to avoid this happening in the future?

    I'm not sure where to add such test, nor which kind of test is needed. I mean, should we only test that the symbol is present? Or should we also test the ABI? Or even write a functional test?

    Since I didn't know, I just merged my fix, to make sure that it lands before 3.8beta2.

    vstinner commented 5 years ago

    My notes on checking an ABI: https://pythoncapi.readthedocs.io/stable_abi.html#check-for-abi-changes

    I didn't check if https://abi-laboratory.pro/tracker/timeline/python/ supports Windows or not.

    miss-islington commented 5 years ago

    New changeset 8cb8d5de4bcc587b35d1b2f4166dad98c202805c by Miss Islington (bot) in branch '3.8': bpo-37189: Export old PyRun_XXX() functions (GH-14142) https://github.com/python/cpython/commit/8cb8d5de4bcc587b35d1b2f4166dad98c202805c

    vstinner commented 5 years ago

    I close the issue. The initial issue has been fixed (PyRun_String is now exported again in the Python DLL).

    Eryk Sun:

    Wouldn't it be better to add a function to _testcapi that checks GetProcAddress(PyWin_DLLhModule, name)?

    I have no idea.

    Steve Dower:

    Should we add a regression test to avoid this happening in the future?

    As I explained, I'm not sure where to put such test, I'm not sure what should be tested. If someone wants to work on that, I suggest to open a separated issue.