Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/venv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ creation according to their needs, the :class:`EnvBuilder` class.
On POSIX systems, if a specific executable ``python3.x`` was used,
symlinks to ``python`` and ``python3`` will be created pointing to that
executable, unless files with those names already exist.
On POSIX systems, a broken symlink at a destination path is removed
before the copy or symlink is created.

.. versionchanged:: next
A broken symlink at a destination path is now removed and replaced.
Previously it was left in place, or it made the copy fail.

.. method:: setup_scripts(context)

Expand Down
7 changes: 6 additions & 1 deletion Lib/_py_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ def _abc_caches_clear(cls):
def __instancecheck__(cls, instance):
"""Override for isinstance(instance, cls)."""
# Inline the cache checking
subclass = instance.__class__
try:
subclass = instance.__class__
except AttributeError:
# Fall back to the type when the instance has no __class__,
# matching the behaviour of the built-in isinstance() (gh-153772).
subclass = type(instance)
if subclass in cls._abc_cache:
return True
subtype = type(instance)
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,25 @@ class C(str): pass
self.assertIsSubclass(C, A)
self.assertIsSubclass(C, (A,))

def test_instancecheck_no_class(self):
# gh-153772: __instancecheck__ must fall back to type(instance)
# when the instance has no __class__, matching isinstance().
class NoClass:
def __getattribute__(self, name):
if name == "__class__":
raise AttributeError(name)
return super().__getattribute__(name)

class A(metaclass=abc_ABCMeta):
pass

obj = NoClass()
# Must return False rather than propagating the AttributeError.
self.assertNotIsInstance(obj, A)
# Registering the actual type makes the fallback report a match.
A.register(NoClass)
self.assertIsInstance(obj, A)

def test_registration_edge_cases(self):
class A(metaclass=abc_ABCMeta):
pass
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,33 @@ def test_failed_symlink(self):
filepath_regex = r"'[A-Z]:\\\\(?:[^\\\\]+\\\\)*[^\\\\]+'"
self.assertRegex(err, rf"Unable to symlink {filepath_regex} to {filepath_regex}")

@requireVenvCreate
@unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
@unittest.skipUnless(can_symlink(), 'Needs symlinks')
def test_broken_symlink_in_existing_venv(self):
"""
Test creating a venv when a stale venv with broken symlinks exists.
"""
bindir = os.path.join(self.env_dir, self.bindir)
os.makedirs(bindir)
python = os.path.join(bindir, 'python3')
os.symlink('/path/to/deleted/env/bin/python3', python)
self.assertTrue(os.path.islink(python))
self.assertFalse(os.path.exists(python))

builder = venv.EnvBuilder(with_pip=False, symlinks=True)
self.run_with_capture(builder.create, self.env_dir)
self.assertTrue(os.path.islink(python))
self.assertTrue(os.path.exists(python))

rmtree(self.env_dir)
os.makedirs(bindir)
os.symlink('/path/to/deleted/env/bin/python3', python)
builder = venv.EnvBuilder(with_pip=False, symlinks=False)
self.run_with_capture(builder.create, self.env_dir)
self.assertFalse(os.path.islink(python))
self.assertTrue(os.path.exists(python))

@requireVenvCreate
def test_multiprocessing(self):
"""
Expand Down
3 changes: 3 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ def _eval_type(t, globalns, localns, type_params, *, recursive_guard=frozenset()
type_params=type_params, owner=owner,
_recursive_guard=recursive_guard, format=format)
if isinstance(t, (_GenericAlias, GenericAlias, Union)):
if isinstance(t, _LiteralGenericAlias):
# Unlike other generic aliases, Literal arguments aren't type expressions
return t
if isinstance(t, GenericAlias):
args = tuple(
_make_forward_ref(arg, parent_fwdref=parent_fwdref) if isinstance(arg, str) else arg
Expand Down
2 changes: 2 additions & 0 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ def symlink_or_copy(self, src, dst, relative_symlinks_ok=False):
switch to a different set of files instead.)
"""
assert os.name != 'nt'
if os.path.islink(dst) and not os.path.exists(dst):
os.unlink(dst)
force_copy = not self.symlinks
if not force_copy:
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`venv`: Replace a dangling interpreter symlink in an existing
virtual environment instead of failing or silently leaving it broken.
Fix by Clay Dugo.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:func:`isinstance` checks against :mod:`collections.abc` classes such as
:class:`~collections.abc.Mapping` no longer raise :exc:`AttributeError`
when the instance has no ``__class__``. The abstract base class machinery
now falls back to the object's type in that case, matching the behaviour of
the built-in :func:`isinstance`.
8 changes: 6 additions & 2 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,15 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
return NULL;
}

subclass = PyObject_GetAttr(instance, &_Py_ID(__class__));
if (subclass == NULL) {
if (PyObject_GetOptionalAttr(instance, &_Py_ID(__class__), &subclass) < 0) {
Py_DECREF(impl);
return NULL;
}
if (subclass == NULL) {
/* Fall back to the type when the instance has no __class__, matching
the behaviour of the built-in isinstance() (gh-153772). */
subclass = Py_NewRef((PyObject *)Py_TYPE(instance));
}
/* Inline the cache checking. */
int incache = _in_weak_set(impl, &impl->_abc_cache, subclass);
if (incache < 0) {
Expand Down
4 changes: 2 additions & 2 deletions Modules/cjkcodecs/cjkcodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ get_module_state(PyObject *mod)

#define OUTCHAR(c) \
do { \
if (_PyUnicodeWriter_WriteChar(writer, (c)) < 0) \
return MBERR_EXCEPTION; \
if (_PyUnicodeWriter_WriteCharInline(writer, (c)) < 0) \
return MBERR_EXCEPTION; \
} while (0)

#define OUTCHAR2(c1, c2) \
Expand Down
Loading