|
| 1 | +import os |
1 | 2 | import sys |
2 | 3 | from array import array |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 |
|
| 7 | +from dask.multiprocessing import get_context |
6 | 8 | from dask.sizeof import sizeof |
7 | 9 | from dask.utils import funcname |
8 | 10 |
|
@@ -147,3 +149,47 @@ def test_dict(): |
147 | 149 | d = {i: x for i in range(100)} |
148 | 150 | assert sizeof(d) > x.nbytes * 100 |
149 | 151 | assert isinstance(sizeof(d), int) |
| 152 | + |
| 153 | + |
| 154 | +def _get_sizeof_on_path(path, size): |
| 155 | + sys.path.append(os.fsdecode(path)) |
| 156 | + |
| 157 | + # Dask will have already called _register_entry_point_plugins |
| 158 | + # before we can modify sys.path, so we re-register here. |
| 159 | + import dask.sizeof |
| 160 | + |
| 161 | + dask.sizeof._register_entry_point_plugins() |
| 162 | + |
| 163 | + import class_impl |
| 164 | + |
| 165 | + cls = class_impl.Impl(size) |
| 166 | + return sizeof(cls) |
| 167 | + |
| 168 | + |
| 169 | +def test_register_backend_entrypoint(tmp_path): |
| 170 | + # Create special sizeof implementation for a dummy class |
| 171 | + (tmp_path / "impl_sizeof.py").write_bytes( |
| 172 | + b"def sizeof_plugin(sizeof):\n" |
| 173 | + b' print("REG")\n' |
| 174 | + b' @sizeof.register_lazy("class_impl")\n' |
| 175 | + b" def register_impl():\n" |
| 176 | + b" import class_impl\n" |
| 177 | + b" @sizeof.register(class_impl.Impl)\n" |
| 178 | + b" def sizeof_impl(obj):\n" |
| 179 | + b" return obj.size \n" |
| 180 | + ) |
| 181 | + # Define dummy class that possesses a size attribute |
| 182 | + (tmp_path / "class_impl.py").write_bytes( |
| 183 | + b"class Impl:\n def __init__(self, size):\n self.size = size" |
| 184 | + ) |
| 185 | + dist_info = tmp_path / "impl_sizeof-0.0.0.dist-info" |
| 186 | + dist_info.mkdir() |
| 187 | + (dist_info / "entry_points.txt").write_bytes( |
| 188 | + b"[dask.sizeof]\nimpl = impl_sizeof:sizeof_plugin\n" |
| 189 | + ) |
| 190 | + |
| 191 | + with get_context().Pool(1) as pool: |
| 192 | + assert ( |
| 193 | + pool.apply(_get_sizeof_on_path, args=(tmp_path, 3_14159265)) == 3_14159265 |
| 194 | + ) |
| 195 | + pool.join() |
0 commit comments