[Interface-dev] Separate zope.interface package

Tim Peters tim at zope.com
Wed Jun 16 22:55:38 EDT 2004


[Tim Peters, about
  http://www.python.org/sf/935117
]
>> Is this really a serious bug?  I find no uses of extend_path in Python,
>> Zope 2, or Zope 3; indeed, it appears to be used only once across all
>> the code Google knows about.

[Fred Drake]
> I removed the only call to this from Zope 3 when we realized this bug
> existed. We would be using it for both the "zope" and "zope.app" packages
> were it not for the bug.

OK, as we discussed, we should be able to address this in pure Python, but
the details are subtle, and anything simpler than the building-block that
follows is probably doomed.  Note that this is aware of a recently reported
Python bug, where the existence of a *directory* named __init__.py screwed
things up royally.

"""
import os

# Check for case-sensitive match.  full_path is the full path to a file
# (or directory).  On a case-insensitive filesystem, we want to ensure
# that the basename matches case-sensitively.  Two cases:
#
# 1. We want a case-sensitive match to the entire basename, like a
#    directory name.  Then pass the entire basename as basename_name,
#    and pass False for require_py_extension.  For example,
#       case_ok('/a/b/c/ZODB', 'ZODB', False)
#    This returns True iff directory /a/b/c/ contains a file or
#    directory with name exactly 'ZODB'.
#
# 2. We're trying to check a Python file.  This is complicated in two
#    ways:  the file may have a number of legitimate extensions; and,
#    while Python does case-sensitive imports even on case-insensitive
#    filesystems, that applies only to the name being imported, not to
#    the extension on the file.  For example, gorp.PY and gorp.pYo both
#    work on Windows if you want to import gorp.  In this case, pass
#    the root of the Python name as basename_name, and True for
#    require_py_extension.  For example,
#        case_ok('/a/b/c/ZODB/__init__.py', '__init__', True)
#    This returns True iff directory /a/b/c/ZODB/ contains a file (not
#    a directory!) with root name exactly '__init__', and an extension
#    that is a case-insensitive match to .py, .pyc, .pyo, or .pyw.

def case_ok(full_path, basename_name, require_py_extension):
    parentdir = os.path.dirname(full_path)
    all_files = os.listdir(parentdir)
    if not require_py_extension:
        return basename_name in all_files

    name_len = len(basename_name)
    for fname in all_files:
        if fname[:name_len] != basename_name:
            continue
        # We have a case-sensitive match to basename_name on a prefix
        # of fname.
        root, ext = os.path.splitext(fname)
        if (len(root) == name_len and
              ext.lower() in ('.py', '.pyc', '.pyo', '.pyw') and
              os.path.isfile(os.path.join(parentdir, fname))):
            return True

    return False
"""





More information about the Interface-dev mailing list