|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import pathlib |
3 | 4 | import sys |
4 | 5 | from dataclasses import dataclass |
5 | 6 | from typing import ClassVar, Dict, Optional |
@@ -28,7 +29,7 @@ def test_workflow_sandbox_stdlib_module_names(): |
28 | 29 | if len(code_lines[-1]) > 80: |
29 | 30 | code_lines.append("") |
30 | 31 | code_lines[-1] += mod_name |
31 | | - code = f'_stdlib_module_names = (\n "' + '"\n "'.join(code_lines) + '"\n)' |
| 32 | + code = '_stdlib_module_names = (\n "' + '"\n "'.join(code_lines) + '"\n)' |
32 | 33 | # TODO(cretz): Point releases may add modules :-( |
33 | 34 | assert ( |
34 | 35 | actual_names == _stdlib_module_names |
@@ -56,6 +57,44 @@ class RestrictableObject: |
56 | 57 | RestrictableObject.qux = RestrictableObject(foo=RestrictableObject(bar=70), bar=80) |
57 | 58 |
|
58 | 59 |
|
| 60 | +class RestrictableClass: |
| 61 | + def __str__(self): |
| 62 | + return "__str__" |
| 63 | + |
| 64 | + def __repr__(self): |
| 65 | + return "__repr__" |
| 66 | + |
| 67 | + def __format__(self, __format_spec: str) -> str: |
| 68 | + return "__format__" |
| 69 | + |
| 70 | + |
| 71 | +def test_restricted_proxy_dunder_methods(): |
| 72 | + restricted_class = _RestrictedProxy( |
| 73 | + "RestrictableClass", |
| 74 | + RestrictableClass, |
| 75 | + RestrictionContext(), |
| 76 | + SandboxMatcher(), |
| 77 | + ) |
| 78 | + restricted_obj = restricted_class() |
| 79 | + assert type(restricted_obj) is _RestrictedProxy |
| 80 | + assert str(restricted_obj) == "__str__" |
| 81 | + assert repr(restricted_obj) == "__repr__" |
| 82 | + assert format(restricted_obj, "") == "__format__" |
| 83 | + assert f"{restricted_obj}" == "__format__" |
| 84 | + |
| 85 | + restricted_path = _RestrictedProxy( |
| 86 | + "Path", |
| 87 | + pathlib.Path, |
| 88 | + RestrictionContext(), |
| 89 | + SandboxMatcher(), |
| 90 | + ) |
| 91 | + assert isinstance(format(restricted_path, ""), str) |
| 92 | + restricted_path_obj = restricted_path("test/path") |
| 93 | + assert type(restricted_path_obj) is _RestrictedProxy |
| 94 | + assert format(restricted_path_obj, "") == "test/path" |
| 95 | + assert f"{restricted_path_obj}" == "test/path" |
| 96 | + |
| 97 | + |
59 | 98 | def test_workflow_sandbox_restricted_proxy(): |
60 | 99 | obj_class = _RestrictedProxy( |
61 | 100 | "RestrictableObject", |
|
0 commit comments