Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 559644 - distutils-r1.eclass makes ${BUILD_DIR}/lib an invalid import path
Summary: distutils-r1.eclass makes ${BUILD_DIR}/lib an invalid import path
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Eclasses (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Python Gentoo Team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-09-05 08:55 UTC by Fabio Rossi
Modified: 2015-09-13 00:02 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments
distutils-r1.eclass.patch (distutils-r1.eclass.patch,766 bytes, patch)
2015-09-05 08:55 UTC, Fabio Rossi
Details | Diff
test.zip (test.zip,1.15 KB, application/zip)
2015-09-07 08:48 UTC, Fabio Rossi
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Fabio Rossi 2015-09-05 08:55:23 UTC
Created attachment 411038 [details, diff]
distutils-r1.eclass.patch

The distutils-r1.eclass sets PYTHONPATH in distutils-r1_run_phase() before running setup.py scripts.

Using PYTHONPATH when the ${BUILD_DIR}/lib is not created yet might lead to problems in setup.py scripts that try to import modules/packages from that path during the build process (Python at startup evaluates PYTHONPATH and associate a NullImporter object to ${BUILD_DIR}/lib in the sys.path_importer_cache).

The fix is really simple, a mkdir is enough, proposed patch is attached.
Comment 1 Mike Gilbert gentoo-dev 2015-09-05 19:52:41 UTC
Can you provide an example of a package that actually requires this?
Comment 2 Fabio Rossi 2015-09-07 08:47:48 UTC
(In reply to Mike Gilbert from comment #1)
> Can you provide an example of a package that actually requires this?

I am writing an ebuild for PyTango which is not in portage. The building system for it is based on distutils and its setup.py builds a PyTango package in ${BUILD_DIR}/lib and later tries to import this freshly built package to write documentation with Sphinx.

The problem arises with an import statement as python doesn't find the package even if it's in the proper place and in the path. After investigating a little bit I have discovered how the import mechanism works and the problem with PYTHONPATH in the distutils-r1.eclass (there was no problem with distutil.eclass because PYTHONPATH is not set during compilation phase).

Basically indicating a not exisisting path in PYTHONPATH has the effect to mark that path as invalid because it's inserted in sys.path_importer_cache as a NullImporter object. This means that PYTHONPATH is useless in distutils-r1.eclass because it masks *always* the ${BUILD_DIR}/lib directory as it's not present when python is executed to run setup.py. Moreover an ebuild wanting to import something freshly built from that path will *always* fail (unless there is something in the site-packages path satisfying the import but this is not the desired result).

I have written a demo program to show what happens, you can run it with

$ PYTHONPATH=dir python test.py

Moreover I'd like to point out that the import mechanism behaves differently if the dir exists when python is executed but there are no modules or packages inside. In this case the import fails only if later, when the effective import statement is executed, there are no required files in place.
Comment 3 Fabio Rossi 2015-09-07 08:48:22 UTC
Created attachment 411234 [details]
test.zip
Comment 4 Fabio Rossi 2015-09-07 09:01:28 UTC
I want to add that with python2 the missing dir is cached as imp.NullImporter object while with python 3 is cached as None, the final effect described above is the same
Comment 5 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2015-09-07 15:13:46 UTC
Thanks for your detailed explanation. If your patch indeed fixes the issue, I'm all for it as a clean solution.
Comment 6 Fabio Rossi 2015-09-11 09:06:49 UTC
(In reply to Michał Górny from comment #5)
> Thanks for your detailed explanation. If your patch indeed fixes the issue,
> I'm all for it as a clean solution.

Yes, it fixes the issue but, most important, makes the PYTHONPATH effective. From the point of view of distutils there are no problems if ${BUILD_DIR}/lib is already in place because the distutils package uses dir_util.mkpath which doesn't fail.
Comment 7 Mike Gilbert gentoo-dev 2015-09-13 00:02:26 UTC
Thanks for the patch.

commit de65dc9655b2a783fd5340a7af2e455ea215c5db
Author: Mike Gilbert <floppym@gentoo.org>
Date:   Sat Sep 12 19:59:55 2015 -0400

    distutils-r1.eclass: mkdir ${BUILD_DIR}/lib
    
    Bug: https://bugs.gentoo.org/559644