Skip to content

Copy libs/pythonXY.lib to support building of C extensions without distutils - #1023

Closed
jcfr wants to merge 1 commit into
pypa:masterfrom
jcfr:windows-copy-import-library
Closed

jcfr wants to merge 1 commit into
pypa:masterfrom
jcfr:windows-copy-import-library

Conversation

@jcfr

@jcfr jcfr commented Feb 23, 2017 •

Copy link
Copy Markdown

This issue was discovered while using virtualenv to create an
isolated environment and test that a source distribution would compile
without using the knowledge hardcoded in distutils.sysconfig and
virtualenv/virtualenv_embedded/distutils-init.py

In other word, without relying on the fact virtualenv monkey patch distutils
to ensure the value associated with LIBDIR variable is correct.

This is relevant because the library location is implicitly specified
in PC/pyconfig.h

[...]

/* For an MSVC DLL, we can nominate the .lib files used by extensions */
                        /* So MSVC users need not specify the .lib file in
                        their Makefile (other compilers are generally
                        taken care of by distutils.) */

[...]

References:

@macisamuele

Copy link
Copy Markdown

Is there any interest on merging this PR?
I'm currently working on a small project with rust bindings on Windows and I'm not able to build it within a virtual environment. Applying those changes on top of the latest version of virtualenv I can finally build them.

Pinging @dstufft as one of the project moderator recently active.
If you think that this PR could be "mergeable" but it needs testing etc. let me know and I'll try to give some support

@jcfr

jcfr commented Aug 10, 2018 •

Copy link
Copy Markdown
Author

@macisamuele Here is a rebased topic, if it works well, I will push force this topic. EDIT: This topic has been updated.

Thanks for your help

It would indeed be nice to get this integrated.

@macisamuele

Copy link
Copy Markdown

I've spent the last few days experimenting in building rust from a venv with no success, I've applied the patch from this PR on the current master and it worked on the first try 🎉

@jcfr

jcfr commented Aug 10, 2018 •

Copy link
Copy Markdown
Author

Great 👍 Could you review this topic then: master...jcfr:windows-copy-import-library-rebased ? A sanity check to make sure I properly rebased it. EDIT: This topic has been updated.

@macisamuele

Copy link
Copy Markdown

@jcfr I can confirm. The build with your new branch worked as expected

@jcfr
jcfr force-pushed the windows-copy-import-library branch from c96b157 to 895742d Compare August 10, 2018 18:27
@jcfr

jcfr commented Aug 10, 2018

Copy link
Copy Markdown
Author

Thanks @macisamuele , topic updated 😄

@gaborbernat gaborbernat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require a rebase on master, and some test to validate the changes. Thanks!

@gaborbernat

Copy link
Copy Markdown
Contributor

Closing due to inactivity.

@jcfr

jcfr commented Jan 14, 2019

Copy link
Copy Markdown
Author

@gaborbernat I would appreciate if you could give this a little more time. Thanks for your patience.

@gaborbernat gaborbernat reopened this Jan 14, 2019
@jcfr
jcfr force-pushed the windows-copy-import-library branch 3 times, most recently from 9354332 to 121cc1e Compare January 14, 2019 21:45
@jcfr

jcfr commented Jan 14, 2019

Copy link
Copy Markdown
Author

Topic has been rebased and tested locally.

I confirm that the pythonXY.lib file is copied for all version of python:

$ find . | grep "\.lib$"
./venv-test-27-x64/libs/python27.lib
./venv-test-27-x86/libs/python27.lib
./venv-test-34-x64/libs/python34.lib
./venv-test-34-x86/libs/python34.lib
./venv-test-35-x64/libs/python35.lib
./venv-test-35-x86/libs/python35.lib
./venv-test-36-x64/libs/python36.lib
./venv-test-36-x86/libs/python36.lib
./venv-test-37-x64/libs/python37.lib
./venv-test-37-x86/libs/python37.lib

@gaborbernat I was thinking to update the test test_install_python_bin ? What do you think ?

…stutils

This issue was discovered while using virtualenv to create an
isolated environment and test that a source distribution would compile
without using the knowledge hardcoded in distutils.sysconfig and
virtualenv/virtualenv_embedded/distutils-init.py

In other word, without relying on the fact virtualenv monkey patch distutils
to ensure the value associated with LIBDIR variable is correct.

This is relevant because the library location is implicitly specified
in PC/pyconfig.h

8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---
[...]

/* For an MSVC DLL, we can nominate the .lib files used by extensions */
#ifdef MS_COREDLL
#       ifndef Py_BUILD_CORE /* not building the core - must be an ext */
#               if defined(_MSC_VER)
                        /* So MSVC users need not specify the .lib file in
                        their Makefile (other compilers are generally
                        taken care of by distutils.) */
#                       if defined(_DEBUG)
#                               pragma comment(lib,"python35_d.lib")
#                       elif defined(Py_LIMITED_API)
#                               pragma comment(lib,"python3.lib")
#                       else
#                               pragma comment(lib,"python35.lib")
#                       endif /* _DEBUG */
#               endif /* _MSC_VER */
#       endif /* Py_BUILD_CORE */
#endif /* MS_COREDLL */

[...]
8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---

References:
 * https://docs.python.org/2/extending/windows.html#using-dlls-in-practice
 * https://docs.python.org/3.6/extending/windows.html#using-dlls-in-practice

Tested-by: Samuele Maci <[email protected]>
@jcfr
jcfr force-pushed the windows-copy-import-library branch from 121cc1e to 508919a Compare January 14, 2019 21:52

@gaborbernat gaborbernat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as modifying that test to assert against it, I'm good with it.

Comment thread virtualenv.py
# considering that on windows, python import libraries are located in
# the "<root>/libs" directory, the following code will look for and
# copy "pythonXY.lib".
pythonlib_name = "python{}{}.lib".format(sys.version_info[0], sys.version_info[1])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slight name tweak, python_lib_name (the logic applies to all other names too)

Comment thread virtualenv.py
pythonlib_dest_dir = os.path.join(lib_dir, "..", "libs")
pythonlib_dest = os.path.join(pythonlib_dest_dir, pythonlib_name)
if os.path.exists(pythonlib):
logger.info("Also created %s" % pythonlib_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not materialize the string yet, leave it extra arg to logger.

Comment thread virtualenv.py
logger.info("Removed %s as the source does not exist", python_dll_d_dest)
os.unlink(python_dll_d_dest)

if not IS_PYPY:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why just pypy, what about jython? We should also mention why this is so special

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Jython is java-based and doesn't provide import libraries like CPython does.
  • Pypy indeed provides a libs folder with the corresponding pythonXY.lib file, I will update the patch to not exclude it.

Comment thread virtualenv.py
# considering that on windows, python import libraries are located in
# the "<root>/libs" directory, the following code will look for and
# copy "pythonXY.lib".
pythonlib_name = "python{}{}.lib".format(sys.version_info[0], sys.version_info[1])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it lib on Windows too?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.lib is indeed the extension on Windows. Test above was performed on windows and the directory were searched running find and grep from Git Bash.

Comment thread virtualenv.py
# copy "pythonXY.lib".
pythonlib_name = "python{}{}.lib".format(sys.version_info[0], sys.version_info[1])
pythonlib = os.path.join(os.path.dirname(sys.executable), "libs", pythonlib_name)
pythonlib_dest_dir = os.path.join(lib_dir, "..", "libs")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this take the lib_dir value from

lib_dir = join(home_dir, "Lib")
?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .lib file indeed lives in the .libs folder.

image

@stale

stale Bot commented Apr 15, 2019

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.

@stale stale Bot added the wontfix label Apr 15, 2019
@gaborbernat

Copy link
Copy Markdown
Contributor

Closing due to no action on the PR for a long time, if anyone has the bandwidth to pick this up, let me know, and we can have a talk what would be a good solution here.

@jcfr

jcfr commented Nov 18, 2019 •

Copy link
Copy Markdown
Author

Hi @gaborbernat ,

Thanks for the update.

If I find some time, I will rebase, test locally and answer follow-up questions.

Best,

@gaborbernat

Copy link
Copy Markdown
Contributor

I'd like to validate this on the new rewrite branch to be honest, though not sure is advanced enough to add this.

@gaborbernat

Copy link
Copy Markdown
Contributor

created #1448 to track this

@mattip mattip mentioned this pull request Oct 4, 2021
3 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants