Skip to content
Open
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
67 changes: 49 additions & 18 deletions mypyc/codegen/emitmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@
from mypyc.irbuild.main import build_ir
from mypyc.irbuild.mapper import Mapper
from mypyc.irbuild.prepare import load_type_map
from mypyc.irbuild.util import is_decorated_class, is_extension_class, is_trait
from mypyc.irbuild.util import (
MypycAttrs,
get_mypyc_attrs,
is_decorated_class,
is_extension_class,
is_trait,
)
from mypyc.namegen import NameGenerator, exported_name
from mypyc.options import CompilerOptions
from mypyc.transform.borrow_generator_attrs import borrow_generator_attrs
Expand Down Expand Up @@ -118,6 +124,9 @@
# A list of (file name, file contents) pairs.
FileContents = list[tuple[str, str]]

# The kind of a class that other modules depend on (see class_kinds).
ClassKind = tuple[str, bool, bool, bool, bool, bool, bool]


class MarkedDeclaration:
"""Add a mark, useful for topological sort."""
Expand All @@ -141,8 +150,11 @@ class MypycPlugin(Plugin):
other modules use a class: @mypyc_attr(native_class=False) and
@trait decide what kind of class mypyc generates, and a decorator
that can replace the class changes what its name refers to (see
ClassIR.is_decorated). So we make the kinds of a module's classes
part of its interface.
ClassIR.is_decorated). @mypyc_attr(weakrefs=True) changes the object
layout of subclasses, @mypyc_attr(serializable=True) is inherited by
subclasses and changes how they are constructed, and
@mypyc_attr(acyclic=True) decides whether subclasses can be acyclic.
So we make the kinds of a module's classes part of its interface.
"""

def __init__(
Expand All @@ -160,7 +172,7 @@ def __init__(

def report_config_data(
self, ctx: ReportConfigContext
) -> tuple[str | None, list[str], list[tuple[str, bool, bool, bool]]] | None:
) -> tuple[str | None, list[str], list[ClassKind]] | None:
# The config data we report is the group map entry for the module,
# followed by the kinds of its classes (see class_kinds).
# If the data is being used to check validity, we do additional checks
Expand Down Expand Up @@ -212,7 +224,15 @@ def report_config_data(
# The module hasn't been parsed yet, so take the class kinds from the
# cached IR. It was built from the source the cache is for.
kinds = sorted(
(c["name"], c["is_ext_class"], c["is_trait"], c["is_decorated"])
(
c["name"],
c["is_ext_class"],
c["is_trait"],
c["is_decorated"],
c["_weakrefs"],
c["_serializable"],
c.get("is_acyclic", False),
)
for c in ir_data["ir"]["classes"]
if not c["is_generated"]
)
Expand Down Expand Up @@ -245,23 +265,34 @@ def get_additional_indirect_deps(self, file: MypyFile) -> set[str]:
return mods


def class_kinds(tree: MypyFile, options: Options) -> list[tuple[str, bool, bool, bool]]:
"""Return (name, is_ext_class, is_trait, is_decorated) for each class in a module.
def class_kinds(tree: MypyFile, options: Options) -> list[ClassKind]:
"""Return the kind of each class in a module.

These match the ClassIR flags that build_type_map sets.
The kind of a class is (name, is_ext_class, is_trait, is_decorated,
weakrefs, serializable, acyclic). These match the ClassIR flags that
build_type_map sets, and the ClassIR flags _weakrefs, _serializable and
is_acyclic, which prepare_class_def sets for native classes.
"""
# Any errors are reported when the module is compiled.
errors = Errors(options)
return sorted(
(
node.name,
is_extension_class(tree.path, node, errors),
is_trait(node),
is_decorated_class(node),
)
for node in tree.defs
if isinstance(node, ClassDef)
)
kinds = []
for node in tree.defs:
if isinstance(node, ClassDef):
ext_class = is_extension_class(tree.path, node, errors)
trait = is_trait(node)
attrs: MypycAttrs = get_mypyc_attrs(node, tree.path, errors)[0] if ext_class else {}
kinds.append(
(
node.name,
ext_class,
trait,
is_decorated_class(node),
not trait and attrs.get("weakrefs") is True,
attrs.get("serializable") is True,
attrs.get("acyclic") is True,
)
)
return sorted(kinds)


def parse_and_typecheck(
Expand Down
203 changes: 203 additions & 0 deletions mypyc/test-data/run-multimodule.test
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,209 @@ print(call(D()))
[rechecked2 native]
[rechecked3 native, other]

[case testIncrementalCompilationTogglingWeakrefs]
# Mypy also ignores @mypyc_attr(weakrefs=True). It adds a weak reference list
# to the object struct of A, which moves the attributes of subclasses, so
# native.py must be recompiled to give B a layout that matches.
# In step 3 other.py is unchanged, so it must stay fresh.
from other import A

class B(A):
def __init__(self) -> None:
super().__init__()
self.y = 2
self.t = "b"

[file other.py]
from mypy_extensions import mypyc_attr

class A:
def __init__(self) -> None:
self.x = 1
self.s = "a"

def get_x(a: A) -> int:
return a.x

[file other.py.2]
from mypy_extensions import mypyc_attr

@mypyc_attr(weakrefs=True)
class A:
def __init__(self) -> None:
self.x = 1
self.s = "a"

def get_x(a: A) -> int:
return a.x

[file native.py.3]
from other import A

class B(A):
def __init__(self) -> None:
super().__init__()
self.y = 3
self.t = "b"

[file other.py.4]
from mypy_extensions import mypyc_attr

class A:
def __init__(self) -> None:
self.x = 1
self.s = "a"

def get_x(a: A) -> int:
return a.x

[file driver.py]
import weakref
from native import B
from other import get_x

b = B()
try:
r = weakref.ref(b)
except TypeError:
weak = False
else:
weak = r() is b
print(get_x(b), b.x, b.s, b.y, b.t, weak)
[out]
1 1 a 2 b False
[out2]
1 1 a 2 b True
[out3]
1 1 a 3 b True
[out4]
1 1 a 3 b False

[rechecked native, other]
[rechecked2 native]
[rechecked3 native, other]

[case testIncrementalCompilationTogglingSerializable]
# Mypy also ignores @mypyc_attr(serializable=True). B inherits the flag from
# A, and then B.__new__ must not call __init__, since copy.copy() calls it
# without arguments. So native.py must be recompiled.
# In step 3 other.py is unchanged, so it must stay fresh.
from other import A

class B(A):
def __init__(self, x: int, y: int) -> None:
super().__init__(x)
self.y = y

[file other.py]
from mypy_extensions import mypyc_attr

class A:
def __init__(self, x: int) -> None:
self.x = x

[file other.py.2]
from mypy_extensions import mypyc_attr

@mypyc_attr(serializable=True)
class A:
def __init__(self, x: int) -> None:
self.x = x

[file native.py.3]
from other import A

class B(A):
def __init__(self, x: int, y: int) -> None:
super().__init__(x)
self.y = y + 1

[file other.py.4]
from mypy_extensions import mypyc_attr

class A:
def __init__(self, x: int) -> None:
self.x = x

[file driver.py]
import copy
from native import B

b = B(1, 2)
try:
c = copy.copy(b)
except TypeError:
print(b.x, b.y, "TypeError")
else:
print(c.x, c.y, c is not b)
[out]
1 2 TypeError
[out2]
1 2 True
[out3]
1 3 True
[out4]
1 3 TypeError

[rechecked native, other]
[rechecked2 native]
[rechecked3 native, other]

[case testIncrementalCompilationTogglingAcyclic]
# Mypy also ignores @mypyc_attr(acyclic=True). A class can only be acyclic if
# its base classes are, so native.py must be rechecked when A changes.
# Otherwise, making A non-acyclic again after step 3 wouldn't be reported as
# an error, and freeing a B could crash, since CPython would make B a GC type
# that the tp_dealloc of B doesn't untrack.
# In step 3 only native.py changes, so other.py must stay fresh.
from other import A

class B(A):
def __init__(self) -> None:
super().__init__()
self.y: object = None

[file other.py]
from mypy_extensions import mypyc_attr

class A:
def __init__(self) -> None:
self.x: object = None

[file other.py.2]
from mypy_extensions import mypyc_attr

@mypyc_attr(acyclic=True)
class A:
def __init__(self) -> None:
self.x: object = None

[file native.py.3]
from mypy_extensions import mypyc_attr
from other import A

@mypyc_attr(acyclic=True)
class B(A):
def __init__(self) -> None:
super().__init__()
self.y: object = None

[file driver.py]
import gc
from native import B
from other import A

print(gc.is_tracked(A()), gc.is_tracked(B()))
[out]
True True
[out2]
False True
[out3]
False False

[rechecked native, other]
[rechecked2 native]

[case testExtraLibRtSourceFileDep]
table_list = list(range(256))
table_list[ord('A')] = ord('B')
Expand Down
Loading