Skip to content

Commit

Permalink
Fix memory leak issue (#258)
Browse files Browse the repository at this point in the history
* Test for memory leaks as described in #198

* Possible fix for #198: memory leak

* Optimization: avoid holding frame reference when locals == globals

* Get caller frame at decoration-time

Here we are more careful about which caller's locals we use to
resolve forward type references.  We want the callers locals
at decoration-time — not at decorator-construction time.

Consider:
```py
frozen_dataclass = marshmallow_dataclass.dataclass(frozen=True)

def f():
    @custom_dataclass
    class A:
        b: "B"

    @custom_dataclass
    class B:
        x: int
```

The locals we want in this case are the one from where the
custom_dataclass decorator is called, not from where
marshmallow_dataclass.dataclass is called.

* Add ability to pass explicit localns (and globalns) to class_schema

When class_schema is called, it doesn't need the caller's whole stack
frame.  What it really wants is a `localns` to pass to
`typing.get_type_hints` to be used to resolve type references.

Here we add the ability to pass an explicit `localns` parameter to
`class_schema`.  We also add the ability to pass an explicit
`globalns`, because ... might as well — it might come in useful.
(Since we need these only to pass to `get_type_hints`, we might
as well match `get_type_hints` API as closely as possible.)

* test: check for frame leakage when decorators throw exceptions

* Fix mypy by setting python to the minimum supported version, 3.8

---------

Co-authored-by: Jeff Dairiki <dairiki@dairiki.org>
  • Loading branch information
mvanderlee and dairiki authored Mar 20, 2024
1 parent d6396c1 commit 9668980
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 69 deletions.
Loading

0 comments on commit 9668980

Please sign in to comment.