Skip to content

Dynamically disallow instantiating traits/interpreted subclasses #9248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2020
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
29 changes: 28 additions & 1 deletion mypyc/codegen/emitclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ def generate_class(cl: ClassIR, module: str, emitter: Emitter) -> None:
generate_full = not cl.is_trait and not cl.builtin_base
needs_getseters = not cl.is_generated

if generate_full:
if not cl.builtin_base:
fields['tp_new'] = new_name

if generate_full:
fields['tp_dealloc'] = '(destructor){}_dealloc'.format(name_prefix)
fields['tp_traverse'] = '(traverseproc){}_traverse'.format(name_prefix)
fields['tp_clear'] = '(inquiry){}_clear'.format(name_prefix)
Expand Down Expand Up @@ -229,6 +231,10 @@ def emit_line() -> None:
emit_line()
generate_getseters_table(cl, getseters_name, emitter)
emit_line()

if cl.is_trait:
generate_new_for_trait(cl, new_name, emitter)

generate_methods_table(cl, methods_name, emitter)
emit_line()

Expand Down Expand Up @@ -545,6 +551,27 @@ def generate_new_for_class(cl: ClassIR,
emitter.emit_line('}')


def generate_new_for_trait(cl: ClassIR,
func_name: str,
emitter: Emitter) -> None:
emitter.emit_line('static PyObject *')
emitter.emit_line(
'{}(PyTypeObject *type, PyObject *args, PyObject *kwds)'.format(func_name))
emitter.emit_line('{')
emitter.emit_line('if (type != {}) {{'.format(emitter.type_struct_name(cl)))
emitter.emit_line(
'PyErr_SetString(PyExc_TypeError, '
'"interpreted classes cannot inherit from compiled traits");'
)
emitter.emit_line('} else {')
emitter.emit_line(
'PyErr_SetString(PyExc_TypeError, "traits may not be directly created");'
)
emitter.emit_line('}')
emitter.emit_line('return NULL;')
emitter.emit_line('}')


def generate_traverse_for_class(cl: ClassIR,
func_name: str,
emitter: Emitter) -> None:
Expand Down
26 changes: 26 additions & 0 deletions mypyc/test-data/run-traits.test
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,29 @@ g(c1, c2)
assert c1.x == 1
assert c2.x == 2
[out]

[case testTraitErrorMessages]
from mypy_extensions import trait

@trait
class Trait:
pass

def create() -> Trait:
return Trait()

[file driver.py]
from native import Trait, create
from testutil import assertRaises

with assertRaises(TypeError, "traits may not be directly created"):
Trait()

with assertRaises(TypeError, "traits may not be directly created"):
create()

class Sub(Trait):
pass

with assertRaises(TypeError, "interpreted classes cannot inherit from compiled traits"):
Sub()
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy