This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: package_data chops off first char of default package
Type: Stage:
Components: Distutils Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: calvin, draghuram, hcauwelier, jimjjewett, loewis, pje
Priority: normal Keywords: patch

Created on 2005-04-15 12:34 by calvin, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pytest.tar.gz calvin, 2005-04-15 12:34
package_data_fix.diff calvin, 2005-04-15 12:34
Messages (9)
msg48224 - (view) Author: Bastian Kleineidam (calvin) Date: 2005-04-15 12:34
If the package name is an empty string (ie the default
package), all package_data files have the first
character chopped off.
Attached is a test package pytest.tar.gz where running
python2.4 setup.py build_py
produces this error:
running build_py
creating build
creating build/lib
copying __init__.py -> build/lib
error: can't copy 'ATA': doesn't exist or not a regular
file

Also attached is a fix proposal, though I have tested
this only against the test package.
msg48225 - (view) Author: Hervé Cauwelier (hcauwelier) Date: 2005-10-05 11:03
Logged In: YES 
user_id=1216236

The patch worked well for me, thanks for it!
msg48226 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2006-04-15 08:23
Logged In: YES 
user_id=21627

Why are you using an empty name as the package name? There
is no default package in Python, so this shouldn't work at all.
msg48227 - (view) Author: Bastian Kleineidam (calvin) Date: 2006-05-22 20:13
Logged In: YES 
user_id=9205

I found it in another Python program (don't remember which
though). So I did not think of this as an undocumented
feature. I tried it and it worked (except the data file
stuff :).

The patch should not break any currently working setup.py
installation, since src_dir is only empty when using ''
(empty string) as package name.

Perhaps a cleaner approach would be to forbid an empty
package name instead of silently accepting it? I am not
sure. At least the documentation should mention that empty
package names are not allowed.
msg48228 - (view) Author: Jim Jewett (jimjjewett) Date: 2006-08-31 13:26
Logged In: YES 
user_id=764593

I think the patch is still missing a case or two.  

plen represents the length of the path prefix to ignore.  

Today's code computes this as (len(src_dir) + 1) where the 
+1 is for the "/" added by os.path.join().  As you found, 
src_dir won't add anything (including the "/") if src_dir 
is empty.  

But it also won't add the "/" if src_dir already ends in "/
", and it won't even add the src_dir if the path is already 
absolute.

I'm not certain that either of these two cases can occur, 
but it would be safer to assume they can.

My suggestion is that the stripping be smarter -- change

"""
            # Strip directory from globbed filenames
            filenames = [
                file[plen:] for file in 
self.find_data_files(package, src_dir)
                ]
"""

to 

"""
            # Strip directory from globbed filenames
            filenames = [
                filetail(name, src_dir) for name in 
self.find_data_files(package, src_dir)
                ]
"""
where filetail is a helper function defined as
"""
def filetail(name, strip_path):
    if name.startswith(strip_path):
        kill=len(strip_path)
        if name[kill] == "/":
            kill +=1
        name=name[kill:]
    return name
"""
with tests
"""
>>> filetail("asdf/bdededf", "asdf")
'bdededf'
>>> filetail("asdf/bdededf", "asdf/")
'bdededf'    
"""

msg48229 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2007-02-23 22:07
I concur with Martin: setup() should disallow an empty string as a package, because an empty string is not a valid Python package name.

Regarding Jim's comment that the stripping should be smarter, I'd like to point out that package_dirs must only contain platform-independent directory names, and if anything else is included, it will produce an error when the build_py command is initialized, as 'convert_path' is called on the paths in question.  Thus, they can never contain absolute paths or end in a trailing /.

Thus, the only problem here is that packages is allowed to contain empty strings, which it makes no sense to include.
msg48230 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2007-05-30 15:49

Please check patch 1720897 which fixes slightly related problem.
msg48231 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2007-06-01 14:37
I suggest that this patch be closed as the code change suggested here is committed (with patch 1720897).
msg60261 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2008-01-20 05:07
Closing this as the required code change is committed (as part of #1720897).
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41860
2008-01-20 05:07:43draghuramsetstatus: open -> closed
resolution: fixed
messages: + msg60261
2005-04-15 12:34:21calvincreate