-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
debug.py
94 lines (79 loc) · 2.59 KB
/
debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import subprocess
import sys
import tempfile
import textwrap
from contextlib import contextmanager
from typing import Any, Iterator
import cloudpickle
import prefect
def is_serializable(obj: Any, raise_on_error: bool = False) -> bool:
"""
Checks whether a given object can be registered with Prefect Cloud. This requires
that the object can be serialized in the current process and deserialized in a fresh process.
Args:
- obj (Any): the object to check
- raise_on_error(bool, optional): if `True`, raises the `CalledProcessError` for
inspection; the `output` attribute of this exception can contain useful information
about why the object is not registrable
Returns:
- bool: `True` if registrable, `False` otherwise
Raises:
- subprocess.CalledProcessError: if `raise_on_error=True` and the object is not registrable
"""
if sys.platform == "win32":
raise OSError("is_serializable is not supported on Windows")
template = textwrap.dedent(
"""
import cloudpickle
with open('{}', 'rb') as z76123:
res = cloudpickle.load(z76123)
"""
)
bd, binary_file = tempfile.mkstemp()
sd, script_file = tempfile.mkstemp()
os.close(bd)
os.close(sd)
try:
with open(binary_file, "wb") as bf:
cloudpickle.dump(obj, bf)
with open(script_file, "w") as sf:
sf.write(template.format(binary_file))
try:
subprocess.check_output(
"{} {}".format(sys.executable, script_file),
shell=True,
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as exc:
if raise_on_error:
raise exc
return False
except Exception as exc:
if raise_on_error:
raise exc
return False
finally:
os.unlink(binary_file)
os.unlink(script_file)
return True
@contextmanager
def raise_on_exception() -> Iterator:
"""
Context manager for raising exceptions when they occur instead of trapping them.
Intended to be used only for local debugging and testing.
Example:
```python
from prefect import Flow, task
from prefect.utilities.debug import raise_on_exception
@task
def div(x):
return 1 / x
with Flow("My Flow") as f:
res = div(0)
with raise_on_exception():
f.run() # raises ZeroDivisionError
```
"""
with prefect.context(raise_on_exception=True):
yield