Skip to content

Commit

Permalink
[Frontend] No resource warning (#676)
Browse files Browse the repository at this point in the history
**Context:** PR #656 attempted to remove a warning, but experiments
shown that it didn't actually remove the warning.

**Description of the Change:** Revert PR #656. Create derived class from
`tempfile.Tempdir` and remove the warning generated in the `_cleanup`
method.

**Benefits:** No warning.

**Note**: Needs wheels to test different python versions. I will test
once with wheels and then after success I will remove the
ci:build-wheels tag. No-cover needs to be added due to different python
versions.

---------

Co-authored-by: David Ittah <dime10@users.noreply.github.com>
  • Loading branch information
erick-xanadu and dime10 authored Apr 26, 2024
1 parent 4027f42 commit 0a641cc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ignored-modules=pennylane.ops,jaxlib.mlir.ir,jaxlib.xla_extension
; ignored-classes=

# List of file/directory patters to ignore.
ignore=test,llvm-project,mlir-hlo,Enzyme,.git,__pycache__,build,doc
ignore=test,llvm-project,mlir-hlo,Enzyme,.git,__pycache__,build,doc,_revision.py

# List of decorators that change a function's signature. Will ignore certain errors
# like 'no-value-for-parameter' on functions with these decorators.
Expand Down
5 changes: 2 additions & 3 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@
a Jupyter Notebook.
[(#642)](https://github.com/PennyLaneAI/catalyst/pull/642)

* Manually cleanup the workspace, which prevents a warning from showing up during testing.
[(#656)](https://github.com/PennyLaneAI/catalyst/pull/656)

* Fix a stochastic autograph test failure due to broadly turning warnings into errors.
[(#652)](https://github.com/PennyLaneAI/catalyst/pull/652)

Expand Down Expand Up @@ -185,6 +182,8 @@
* Add optimization that removes redundant chains of self inverse operations. This is done within a new MLIR pass called `remove-chained-self-inverse`. Currently we only match redundant Hadamard operations but the list of supported operations can be expanded.
[(#630)](https://github.com/PennyLaneAI/catalyst/pull/630)

* Running tests should no longer see `ResourceWarning` from `tempfile.TemporaryDirectory`.
[(#676)](https://github.com/PennyLaneAI/catalyst/pull/676)

<h3>Breaking changes</h3>

Expand Down
1 change: 0 additions & 1 deletion frontend/catalyst/debug/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def _print_callback(*args, **kwargs):
builtins.print(*args, **kwargs)


# pylint: disable=redefined-builtin
def print_memref(x):
"""A :func:`qjit` compatible print function for printing numeric values at runtime with memref
information.
Expand Down
22 changes: 17 additions & 5 deletions frontend/catalyst/utils/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,22 @@ def cleanup(self):
return
shutil.rmtree(str(self))

def __del__(self):
"""Avoid warning by doing manual cleanup during deletion."""
if isinstance(self._impl, tempfile.TemporaryDirectory):
self._impl.cleanup()

class TemporaryDirectorySilent(tempfile.TemporaryDirectory):
"""Derived class from tempfile.TemporaryDirectory
This overrides the _cleanup method which would normally emit a warning
after removing the directory. This warning is unconditional and it is emitted
because it is not called explicitly.
"""

@classmethod
def _cleanup(cls, name, warn_message, **kwargs): # pylint: disable=arguments-differ
"""Ignore ResourceWarning during cleanup."""
del warn_message

# pylint: disable-next=protected-access
tempfile.TemporaryDirectory._rmtree(name, **kwargs)


class WorkspaceManager:
Expand Down Expand Up @@ -98,7 +110,7 @@ def _get_or_create_directory(path, name):
# TODO: Once Python 3.12 becomes the least supported version of python, consider
# setting the fields: delete and delete_on_close.
# This can likely avoid having all the code below.
return tempfile.TemporaryDirectory(dir=path.resolve(), prefix=name.name)
return TemporaryDirectorySilent(dir=path.resolve(), prefix=name.name)

count = 1
curr_preferred_abspath = path / name
Expand Down

0 comments on commit 0a641cc

Please sign in to comment.