| 1 |
from .support import HPyTest, SUPPORTS_SYS_EXECUTABLE, trampoline |
| 2 |
|
| 3 |
|
| 4 |
class TestErr(HPyTest): |
| 5 |
|
| 6 |
def test_NoMemory(self): |
| 7 |
import pytest |
| 8 |
mod = self.make_module(""" |
| 9 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 10 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 11 |
{ |
| 12 |
return HPyErr_NoMemory(ctx); |
| 13 |
} |
| 14 |
@EXPORT(f) |
| 15 |
@INIT |
| 16 |
""") |
| 17 |
with pytest.raises(MemoryError): |
| 18 |
mod.f() |
| 19 |
|
| 20 |
def test_FatalError(self, python_subprocess, fatal_exit_code): |
| 21 |
mod = self.compile_module(""" |
| 22 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 23 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 24 |
{ |
| 25 |
HPy_FatalError(ctx, "boom!"); |
| 26 |
// note: no 'return' statement. This also tests that |
| 27 |
// the call above is known to never return---otherwise, |
| 28 |
// we get a warning from the missing 'return' and it is |
| 29 |
// turned into an error. |
| 30 |
} |
| 31 |
@EXPORT(f) |
| 32 |
@INIT |
| 33 |
""") |
| 34 |
if not SUPPORTS_SYS_EXECUTABLE: |
| 35 |
|
| 36 |
|
| 37 |
return |
| 38 |
|
| 39 |
result = python_subprocess.run(mod, "mod.f()") |
| 40 |
assert result.returncode == fatal_exit_code |
| 41 |
assert result.stdout == b"" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
stderr_msg = result.stderr.splitlines()[0] |
| 46 |
assert stderr_msg.startswith(b"Fatal Python error: ") |
| 47 |
assert stderr_msg.endswith(b": boom!") |
| 48 |
|
| 49 |
def test_HPyErr_Occurred(self): |
| 50 |
import pytest |
| 51 |
mod = self.make_module(""" |
| 52 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 53 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 54 |
{ |
| 55 |
HPyLong_AsLong(ctx, arg); |
| 56 |
if (HPyErr_Occurred(ctx)) { |
| 57 |
return HPyErr_SetString(ctx, ctx->h_ValueError, "hello world"); |
| 58 |
} |
| 59 |
return HPyLong_FromLong(ctx, -1002); |
| 60 |
} |
| 61 |
@EXPORT(f) |
| 62 |
@INIT |
| 63 |
""") |
| 64 |
assert mod.f(-10) == -1002 |
| 65 |
with pytest.raises(ValueError) as exc: |
| 66 |
mod.f("not an integer") |
| 67 |
assert str(exc.value) == 'hello world' |
| 68 |
|
| 69 |
def test_HPyErr_Cleared(self): |
| 70 |
import sys |
| 71 |
mod = self.make_module(""" |
| 72 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 73 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 74 |
{ |
| 75 |
HPyErr_SetString(ctx, ctx->h_ValueError, "hello world"); |
| 76 |
HPyErr_Clear(ctx); |
| 77 |
return HPy_Dup(ctx, ctx->h_None); |
| 78 |
} |
| 79 |
@EXPORT(f) |
| 80 |
@INIT |
| 81 |
""") |
| 82 |
assert mod.f() is None |
| 83 |
assert sys.exc_info() == (None, None, None) |
| 84 |
|
| 85 |
def test_HPyErr_SetString(self): |
| 86 |
import pytest |
| 87 |
mod = self.make_module(""" |
| 88 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 89 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 90 |
{ |
| 91 |
return HPyErr_SetString(ctx, ctx->h_ValueError, "error message"); |
| 92 |
} |
| 93 |
|
| 94 |
HPyDef_METH(g, "g", HPyFunc_NOARGS) |
| 95 |
static HPy g_impl(HPyContext *ctx, HPy self) |
| 96 |
{ |
| 97 |
HPyErr_SetString(ctx, ctx->h_ValueError, "error message"); |
| 98 |
return HPy_NULL; |
| 99 |
} |
| 100 |
|
| 101 |
@EXPORT(g) |
| 102 |
@EXPORT(f) |
| 103 |
@INIT |
| 104 |
""") |
| 105 |
with pytest.raises(ValueError) as err: |
| 106 |
mod.f() |
| 107 |
assert str(err.value) == "error message" |
| 108 |
|
| 109 |
with pytest.raises(ValueError) as err: |
| 110 |
mod.g() |
| 111 |
assert str(err.value) == "error message" |
| 112 |
|
| 113 |
def test_HPyErr_SetObject(self): |
| 114 |
import pytest |
| 115 |
mod = self.make_module(""" |
| 116 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 117 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 118 |
{ |
| 119 |
return HPyErr_SetObject(ctx, ctx->h_ValueError, arg); |
| 120 |
} |
| 121 |
@EXPORT(f) |
| 122 |
@INIT |
| 123 |
""") |
| 124 |
with pytest.raises(ValueError) as err: |
| 125 |
mod.f(ValueError("error message")) |
| 126 |
assert str(err.value) == "error message" |
| 127 |
|
| 128 |
def test_HPyErr_SetFromErrno(self): |
| 129 |
import pytest |
| 130 |
import errno |
| 131 |
mod = self.make_module(""" |
| 132 |
#include <errno.h> |
| 133 |
|
| 134 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 135 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy type) |
| 136 |
{{ |
| 137 |
errno = {errno}; |
| 138 |
return HPyErr_SetFromErrno(ctx, type); |
| 139 |
}} |
| 140 |
@EXPORT(f) |
| 141 |
@INIT |
| 142 |
""".format(errno = errno.EINVAL)) |
| 143 |
for type in [OSError, TimeoutError]: |
| 144 |
with pytest.raises(type) as err: |
| 145 |
mod.f(type) |
| 146 |
|
| 147 |
assert err.value.errno == errno.EINVAL |
| 148 |
|
| 149 |
def test_HPyErr_SetFromErrnoWithFilenameObjects(self): |
| 150 |
import pytest |
| 151 |
import errno |
| 152 |
mod = self.make_module(""" |
| 153 |
#include <errno.h> |
| 154 |
|
| 155 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 156 |
static HPy f_impl(HPyContext *ctx, HPy self, |
| 157 |
const HPy *args, size_t nargs) |
| 158 |
{{ |
| 159 |
HPy type, file1, file2; |
| 160 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OOO", &type, &file1, &file2)) |
| 161 |
return HPy_NULL; |
| 162 |
|
| 163 |
errno = {errno}; |
| 164 |
if (HPy_Is(ctx, file2, ctx->h_None)) {{ |
| 165 |
return HPyErr_SetFromErrnoWithFilenameObject(ctx, type, file1); |
| 166 |
}} else {{ |
| 167 |
return HPyErr_SetFromErrnoWithFilenameObjects(ctx, type, file1, file2); |
| 168 |
}} |
| 169 |
}} |
| 170 |
@EXPORT(f) |
| 171 |
@INIT |
| 172 |
""".format(errno = errno.EINVAL)) |
| 173 |
file1 = "some/file/name/to/be/asserted" |
| 174 |
with pytest.raises(OSError) as err: |
| 175 |
mod.f(OSError, file1, None) |
| 176 |
assert err.value.errno == errno.EINVAL |
| 177 |
assert err.value.filename == file1 |
| 178 |
|
| 179 |
file2 = "some/different/file/name/to/be/asserted" |
| 180 |
with pytest.raises(OSError) as err: |
| 181 |
mod.f(OSError, file1, file2) |
| 182 |
assert err.value.errno == errno.EINVAL |
| 183 |
assert err.value.filename == file1 |
| 184 |
assert err.value.filename2 == file2 |
| 185 |
|
| 186 |
def test_HPyErr_SetFromErrnoWithFilename(self): |
| 187 |
import pytest |
| 188 |
import errno |
| 189 |
mod = self.make_module(""" |
| 190 |
#include <errno.h> |
| 191 |
|
| 192 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 193 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy type) |
| 194 |
{{ |
| 195 |
errno = {errno}; |
| 196 |
return HPyErr_SetFromErrnoWithFilename(ctx, type, "Some message that will be asserted"); |
| 197 |
}} |
| 198 |
@EXPORT(f) |
| 199 |
@INIT |
| 200 |
""".format(errno = errno.EINVAL)) |
| 201 |
with pytest.raises(OSError) as err: |
| 202 |
mod.f(OSError) |
| 203 |
|
| 204 |
assert err.value.errno == errno.EINVAL |
| 205 |
assert "Some message that will be asserted" in str(err.value) |
| 206 |
|
| 207 |
def test_h_exceptions(self): |
| 208 |
import pytest |
| 209 |
mod = self.make_module(""" |
| 210 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 211 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 212 |
{ |
| 213 |
HPy h_dict, h_err; |
| 214 |
h_dict = HPyDict_New(ctx); |
| 215 |
HPy_SetItem_s(ctx, h_dict, "BaseException", ctx->h_BaseException); |
| 216 |
HPy_SetItem_s(ctx, h_dict, "Exception", ctx->h_Exception); |
| 217 |
HPy_SetItem_s(ctx, h_dict, "StopAsyncIteration", ctx->h_StopAsyncIteration); |
| 218 |
HPy_SetItem_s(ctx, h_dict, "StopIteration", ctx->h_StopIteration); |
| 219 |
HPy_SetItem_s(ctx, h_dict, "GeneratorExit", ctx->h_GeneratorExit); |
| 220 |
HPy_SetItem_s(ctx, h_dict, "ArithmeticError", ctx->h_ArithmeticError); |
| 221 |
HPy_SetItem_s(ctx, h_dict, "LookupError", ctx->h_LookupError); |
| 222 |
HPy_SetItem_s(ctx, h_dict, "AssertionError", ctx->h_AssertionError); |
| 223 |
HPy_SetItem_s(ctx, h_dict, "AttributeError", ctx->h_AttributeError); |
| 224 |
HPy_SetItem_s(ctx, h_dict, "BufferError", ctx->h_BufferError); |
| 225 |
HPy_SetItem_s(ctx, h_dict, "EOFError", ctx->h_EOFError); |
| 226 |
HPy_SetItem_s(ctx, h_dict, "FloatingPointError", ctx->h_FloatingPointError); |
| 227 |
HPy_SetItem_s(ctx, h_dict, "OSError", ctx->h_OSError); |
| 228 |
HPy_SetItem_s(ctx, h_dict, "ImportError", ctx->h_ImportError); |
| 229 |
HPy_SetItem_s(ctx, h_dict, "ModuleNotFoundError", ctx->h_ModuleNotFoundError); |
| 230 |
HPy_SetItem_s(ctx, h_dict, "IndexError", ctx->h_IndexError); |
| 231 |
HPy_SetItem_s(ctx, h_dict, "KeyError", ctx->h_KeyError); |
| 232 |
HPy_SetItem_s(ctx, h_dict, "KeyboardInterrupt", ctx->h_KeyboardInterrupt); |
| 233 |
HPy_SetItem_s(ctx, h_dict, "MemoryError", ctx->h_MemoryError); |
| 234 |
HPy_SetItem_s(ctx, h_dict, "NameError", ctx->h_NameError); |
| 235 |
HPy_SetItem_s(ctx, h_dict, "OverflowError", ctx->h_OverflowError); |
| 236 |
HPy_SetItem_s(ctx, h_dict, "RuntimeError", ctx->h_RuntimeError); |
| 237 |
HPy_SetItem_s(ctx, h_dict, "RecursionError", ctx->h_RecursionError); |
| 238 |
HPy_SetItem_s(ctx, h_dict, "NotImplementedError", ctx->h_NotImplementedError); |
| 239 |
HPy_SetItem_s(ctx, h_dict, "SyntaxError", ctx->h_SyntaxError); |
| 240 |
HPy_SetItem_s(ctx, h_dict, "IndentationError", ctx->h_IndentationError); |
| 241 |
HPy_SetItem_s(ctx, h_dict, "TabError", ctx->h_TabError); |
| 242 |
HPy_SetItem_s(ctx, h_dict, "ReferenceError", ctx->h_ReferenceError); |
| 243 |
HPy_SetItem_s(ctx, h_dict, "SystemError", ctx->h_SystemError); |
| 244 |
HPy_SetItem_s(ctx, h_dict, "SystemExit", ctx->h_SystemExit); |
| 245 |
HPy_SetItem_s(ctx, h_dict, "TypeError", ctx->h_TypeError); |
| 246 |
HPy_SetItem_s(ctx, h_dict, "UnboundLocalError", ctx->h_UnboundLocalError); |
| 247 |
HPy_SetItem_s(ctx, h_dict, "ValueError", ctx->h_ValueError); |
| 248 |
HPy_SetItem_s(ctx, h_dict, "ZeroDivisionError", ctx->h_ZeroDivisionError); |
| 249 |
HPy_SetItem_s(ctx, h_dict, "BlockingIOError", ctx->h_BlockingIOError); |
| 250 |
HPy_SetItem_s(ctx, h_dict, "BrokenPipeError", ctx->h_BrokenPipeError); |
| 251 |
HPy_SetItem_s(ctx, h_dict, "ChildProcessError", ctx->h_ChildProcessError); |
| 252 |
HPy_SetItem_s(ctx, h_dict, "ConnectionError", ctx->h_ConnectionError); |
| 253 |
HPy_SetItem_s(ctx, h_dict, "ConnectionAbortedError", ctx->h_ConnectionAbortedError); |
| 254 |
HPy_SetItem_s(ctx, h_dict, "ConnectionRefusedError", ctx->h_ConnectionRefusedError); |
| 255 |
HPy_SetItem_s(ctx, h_dict, "ConnectionResetError", ctx->h_ConnectionResetError); |
| 256 |
HPy_SetItem_s(ctx, h_dict, "FileExistsError", ctx->h_FileExistsError); |
| 257 |
HPy_SetItem_s(ctx, h_dict, "FileNotFoundError", ctx->h_FileNotFoundError); |
| 258 |
HPy_SetItem_s(ctx, h_dict, "InterruptedError", ctx->h_InterruptedError); |
| 259 |
HPy_SetItem_s(ctx, h_dict, "IsADirectoryError", ctx->h_IsADirectoryError); |
| 260 |
HPy_SetItem_s(ctx, h_dict, "NotADirectoryError", ctx->h_NotADirectoryError); |
| 261 |
HPy_SetItem_s(ctx, h_dict, "PermissionError", ctx->h_PermissionError); |
| 262 |
HPy_SetItem_s(ctx, h_dict, "ProcessLookupError", ctx->h_ProcessLookupError); |
| 263 |
HPy_SetItem_s(ctx, h_dict, "TimeoutError", ctx->h_TimeoutError); |
| 264 |
h_err = HPy_GetItem(ctx, h_dict, arg); |
| 265 |
if (HPy_IsNull(h_err)) { |
| 266 |
HPy_FatalError(ctx, "missing exception type"); |
| 267 |
} |
| 268 |
HPyErr_SetString(ctx, h_err, "error message"); |
| 269 |
HPy_Close(ctx, h_dict); |
| 270 |
HPy_Close(ctx, h_err); |
| 271 |
return HPy_NULL; |
| 272 |
} |
| 273 |
@EXPORT(f) |
| 274 |
@INIT |
| 275 |
""") |
| 276 |
|
| 277 |
def check_exception(cls): |
| 278 |
with pytest.raises(cls): |
| 279 |
mod.f(cls.__name__) |
| 280 |
|
| 281 |
check_exception(BaseException) |
| 282 |
check_exception(Exception) |
| 283 |
check_exception(StopAsyncIteration) |
| 284 |
check_exception(StopIteration) |
| 285 |
check_exception(GeneratorExit) |
| 286 |
check_exception(ArithmeticError) |
| 287 |
check_exception(LookupError) |
| 288 |
check_exception(AssertionError) |
| 289 |
check_exception(AttributeError) |
| 290 |
check_exception(BufferError) |
| 291 |
check_exception(EOFError) |
| 292 |
check_exception(FloatingPointError) |
| 293 |
check_exception(OSError) |
| 294 |
check_exception(ImportError) |
| 295 |
check_exception(ModuleNotFoundError) |
| 296 |
check_exception(IndexError) |
| 297 |
check_exception(KeyError) |
| 298 |
check_exception(KeyboardInterrupt) |
| 299 |
check_exception(MemoryError) |
| 300 |
check_exception(NameError) |
| 301 |
check_exception(OverflowError) |
| 302 |
check_exception(RuntimeError) |
| 303 |
check_exception(RecursionError) |
| 304 |
check_exception(NotImplementedError) |
| 305 |
check_exception(SyntaxError) |
| 306 |
check_exception(IndentationError) |
| 307 |
check_exception(TabError) |
| 308 |
check_exception(ReferenceError) |
| 309 |
check_exception(SystemError) |
| 310 |
check_exception(SystemExit) |
| 311 |
check_exception(TypeError) |
| 312 |
check_exception(UnboundLocalError) |
| 313 |
check_exception(ValueError) |
| 314 |
check_exception(ZeroDivisionError) |
| 315 |
check_exception(BlockingIOError) |
| 316 |
check_exception(BrokenPipeError) |
| 317 |
check_exception(ChildProcessError) |
| 318 |
check_exception(ConnectionError) |
| 319 |
check_exception(ConnectionAbortedError) |
| 320 |
check_exception(ConnectionRefusedError) |
| 321 |
check_exception(ConnectionResetError) |
| 322 |
check_exception(FileExistsError) |
| 323 |
check_exception(FileNotFoundError) |
| 324 |
check_exception(InterruptedError) |
| 325 |
check_exception(IsADirectoryError) |
| 326 |
check_exception(NotADirectoryError) |
| 327 |
check_exception(PermissionError) |
| 328 |
check_exception(ProcessLookupError) |
| 329 |
check_exception(TimeoutError) |
| 330 |
|
| 331 |
|
| 332 |
check_exception(EnvironmentError) |
| 333 |
check_exception(IOError) |
| 334 |
|
| 335 |
def test_h_unicode_exceptions(self): |
| 336 |
import pytest |
| 337 |
mod = self.make_module(""" |
| 338 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 339 |
static HPy f_impl(HPyContext *ctx, HPy self, |
| 340 |
const HPy *args, size_t nargs) |
| 341 |
{ |
| 342 |
HPy h_key, h_args, h_kw; |
| 343 |
HPy h_dict, h_err, h_err_value; |
| 344 |
|
| 345 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OOO", &h_key, &h_args, &h_kw)) |
| 346 |
return HPy_NULL; |
| 347 |
|
| 348 |
h_dict = HPyDict_New(ctx); |
| 349 |
if (HPy_IsNull(h_dict)) { |
| 350 |
return HPy_NULL; |
| 351 |
} |
| 352 |
HPy_SetItem_s(ctx, h_dict, "UnicodeError", ctx->h_UnicodeError); |
| 353 |
HPy_SetItem_s(ctx, h_dict, "UnicodeEncodeError", ctx->h_UnicodeEncodeError); |
| 354 |
HPy_SetItem_s(ctx, h_dict, "UnicodeDecodeError", ctx->h_UnicodeDecodeError); |
| 355 |
HPy_SetItem_s(ctx, h_dict, "UnicodeTranslateError", ctx->h_UnicodeTranslateError); |
| 356 |
|
| 357 |
h_err = HPy_GetItem(ctx, h_dict, h_key); |
| 358 |
if (HPy_IsNull(h_err)) { |
| 359 |
HPy_Close(ctx, h_dict); |
| 360 |
return HPy_NULL; |
| 361 |
} |
| 362 |
h_err_value = HPy_CallTupleDict(ctx, h_err, h_args, h_kw); |
| 363 |
if (HPy_IsNull(h_err_value)) { |
| 364 |
HPy_Close(ctx, h_dict); |
| 365 |
HPy_Close(ctx, h_err); |
| 366 |
return HPy_NULL; |
| 367 |
} |
| 368 |
|
| 369 |
HPyErr_SetObject(ctx, h_err, h_err_value); |
| 370 |
HPy_Close(ctx, h_dict); |
| 371 |
HPy_Close(ctx, h_err); |
| 372 |
HPy_Close(ctx, h_err_value); |
| 373 |
return HPy_NULL; |
| 374 |
} |
| 375 |
@EXPORT(f) |
| 376 |
@INIT |
| 377 |
""") |
| 378 |
|
| 379 |
def check_exception(cls, *args, **kw): |
| 380 |
with pytest.raises(cls): |
| 381 |
mod.f(cls.__name__, args, kw) |
| 382 |
|
| 383 |
check_exception(UnicodeError) |
| 384 |
check_exception( |
| 385 |
UnicodeEncodeError, "utf-8", "object", 0, 2, "reason" |
| 386 |
) |
| 387 |
check_exception( |
| 388 |
UnicodeDecodeError, "utf-8", b"object", 0, 2, "reason" |
| 389 |
) |
| 390 |
check_exception(UnicodeTranslateError, "object", 0, 2, "reason") |
| 391 |
|
| 392 |
def test_h_warnings(self): |
| 393 |
import pytest |
| 394 |
mod = self.make_module(""" |
| 395 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 396 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 397 |
{ |
| 398 |
HPy h_dict, h_err; |
| 399 |
h_dict = HPyDict_New(ctx); |
| 400 |
HPy_SetItem_s(ctx, h_dict, "Warning", ctx->h_Warning); |
| 401 |
HPy_SetItem_s(ctx, h_dict, "UserWarning", ctx->h_UserWarning); |
| 402 |
HPy_SetItem_s(ctx, h_dict, "DeprecationWarning", ctx->h_DeprecationWarning); |
| 403 |
HPy_SetItem_s(ctx, h_dict, "PendingDeprecationWarning", ctx->h_PendingDeprecationWarning); |
| 404 |
HPy_SetItem_s(ctx, h_dict, "SyntaxWarning", ctx->h_SyntaxWarning); |
| 405 |
HPy_SetItem_s(ctx, h_dict, "RuntimeWarning", ctx->h_RuntimeWarning); |
| 406 |
HPy_SetItem_s(ctx, h_dict, "FutureWarning", ctx->h_FutureWarning); |
| 407 |
HPy_SetItem_s(ctx, h_dict, "ImportWarning", ctx->h_ImportWarning); |
| 408 |
HPy_SetItem_s(ctx, h_dict, "UnicodeWarning", ctx->h_UnicodeWarning); |
| 409 |
HPy_SetItem_s(ctx, h_dict, "BytesWarning", ctx->h_BytesWarning); |
| 410 |
HPy_SetItem_s(ctx, h_dict, "ResourceWarning", ctx->h_ResourceWarning); |
| 411 |
h_err = HPy_GetItem(ctx, h_dict, arg); |
| 412 |
if (HPy_IsNull(h_err)) { |
| 413 |
HPy_FatalError(ctx, "missing exception type"); |
| 414 |
} |
| 415 |
HPyErr_SetString(ctx, h_err, "error message"); |
| 416 |
HPy_Close(ctx, h_dict); |
| 417 |
HPy_Close(ctx, h_err); |
| 418 |
return HPy_NULL; |
| 419 |
} |
| 420 |
@EXPORT(f) |
| 421 |
@INIT |
| 422 |
""") |
| 423 |
|
| 424 |
def check_warning(cls): |
| 425 |
with pytest.raises(cls): |
| 426 |
mod.f(cls.__name__) |
| 427 |
|
| 428 |
check_warning(Warning) |
| 429 |
check_warning(UserWarning) |
| 430 |
check_warning(DeprecationWarning) |
| 431 |
check_warning(PendingDeprecationWarning) |
| 432 |
check_warning(SyntaxWarning) |
| 433 |
check_warning(RuntimeWarning) |
| 434 |
check_warning(FutureWarning) |
| 435 |
check_warning(ImportWarning) |
| 436 |
check_warning(UnicodeWarning) |
| 437 |
check_warning(BytesWarning) |
| 438 |
check_warning(ResourceWarning) |
| 439 |
|
| 440 |
def test_HPyErr_WarnEx(self): |
| 441 |
import warnings |
| 442 |
mod = self.make_module(""" |
| 443 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 444 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 445 |
{ |
| 446 |
switch (HPyLong_AsLong(ctx, arg)) { |
| 447 |
case 0: |
| 448 |
HPyErr_WarnEx(ctx, ctx->h_RuntimeWarning, "warn qzp", 1); |
| 449 |
break; |
| 450 |
case 1: |
| 451 |
HPyErr_WarnEx(ctx, ctx->h_FutureWarning, "warn rtq", 2); |
| 452 |
break; |
| 453 |
case 2: |
| 454 |
HPyErr_WarnEx(ctx, HPy_NULL, "warn null", 1); |
| 455 |
break; |
| 456 |
} |
| 457 |
return HPy_Dup(ctx, ctx->h_None); |
| 458 |
} |
| 459 |
@EXPORT(f) |
| 460 |
@INIT |
| 461 |
""") |
| 462 |
|
| 463 |
|
| 464 |
def check_warning(arg, category, message, file): |
| 465 |
with warnings.catch_warnings(record=True) as warnings_list: |
| 466 |
trampoline(mod.f, arg) |
| 467 |
assert len(warnings_list) == 1, str(category) |
| 468 |
w = warnings_list[-1] |
| 469 |
assert issubclass(w.category, category), str(category) |
| 470 |
assert str(w.message) == message, str(category) |
| 471 |
assert w.filename.endswith(file), str(category) |
| 472 |
|
| 473 |
check_warning(0, RuntimeWarning, "warn qzp", "support.py") |
| 474 |
check_warning(1, FutureWarning, "warn rtq", "test_hpyerr.py") |
| 475 |
check_warning(2, Warning, "warn null", "support.py") |
| 476 |
|
| 477 |
|
| 478 |
def test_errorval_returned_by_api_functions_hpy(self): |
| 479 |
mod = self.make_module(""" |
| 480 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 481 |
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs) |
| 482 |
{ |
| 483 |
HPy a = HPy_NULL; |
| 484 |
HPy b = HPy_NULL; |
| 485 |
HPy res; |
| 486 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b)) |
| 487 |
return HPy_NULL; |
| 488 |
res = HPy_TrueDivide(ctx, a, b); |
| 489 |
|
| 490 |
// the point of the test is to check that in case of error |
| 491 |
// HPy_Div returns HPy_NULL |
| 492 |
if (HPy_IsNull(res)) { |
| 493 |
HPyErr_Clear(ctx); |
| 494 |
return HPyLong_FromLong(ctx, -42); |
| 495 |
} |
| 496 |
return res; |
| 497 |
} |
| 498 |
@EXPORT(f) |
| 499 |
@INIT |
| 500 |
""") |
| 501 |
assert mod.f(21, 3) == 7 |
| 502 |
assert mod.f(21, 0) == -42 |
| 503 |
|
| 504 |
def test_errorval_returned_by_api_functions_int(self): |
| 505 |
mod = self.make_module(""" |
| 506 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 507 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 508 |
{ |
| 509 |
HPy_ssize_t length = HPy_Length(ctx, arg); |
| 510 |
if (length == -1) { |
| 511 |
HPyErr_Clear(ctx); |
| 512 |
return HPyLong_FromLong(ctx, -42); |
| 513 |
} |
| 514 |
return HPyLong_FromLong(ctx, (long) length); |
| 515 |
} |
| 516 |
@EXPORT(f) |
| 517 |
@INIT |
| 518 |
""") |
| 519 |
assert mod.f([100, 200, 300]) == 3 |
| 520 |
assert mod.f(None) == -42 |
| 521 |
|
| 522 |
def test_HPyErr_NewException(self): |
| 523 |
import pytest |
| 524 |
mod = self.make_module(""" |
| 525 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 526 |
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs) |
| 527 |
{ |
| 528 |
// MSVC doesn't allow "static HPy h_FooErr = HPy_NULL" |
| 529 |
// so we do an initialization dance instead. |
| 530 |
static int foo_error_initialized = 0; |
| 531 |
static HPy h_FooError; |
| 532 |
HPy arg; |
| 533 |
HPy h_base = HPy_NULL; |
| 534 |
HPy h_dict = HPy_NULL; |
| 535 |
HPy h_doc = HPy_NULL; |
| 536 |
|
| 537 |
if (!foo_error_initialized) { |
| 538 |
foo_error_initialized = 1; |
| 539 |
h_FooError = HPy_NULL; |
| 540 |
} |
| 541 |
|
| 542 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "O|OOO", &arg, &h_base, &h_dict, &h_doc)) { |
| 543 |
return HPy_NULL; |
| 544 |
} |
| 545 |
|
| 546 |
if (!HPy_IsTrue(ctx, arg)) { |
| 547 |
// cleanup and close the FooError which we created earlier |
| 548 |
if (!HPy_IsNull(h_FooError)) |
| 549 |
HPy_Close(ctx, h_FooError); |
| 550 |
return HPy_Dup(ctx, ctx->h_None); |
| 551 |
} |
| 552 |
|
| 553 |
if(HPy_Is(ctx, h_base, ctx->h_None)) { |
| 554 |
h_base = HPy_NULL; |
| 555 |
} |
| 556 |
|
| 557 |
if(HPy_Is(ctx, h_dict, ctx->h_None)) { |
| 558 |
h_dict = HPy_NULL; |
| 559 |
} |
| 560 |
|
| 561 |
if(HPy_Is(ctx, h_doc, ctx->h_None)) { |
| 562 |
h_FooError = HPyErr_NewException(ctx, "mytest.FooError", h_base, h_dict); |
| 563 |
} else { |
| 564 |
// we use bytes because ATM we don't have HPyUnicode_AsUTF8 or similar |
| 565 |
h_FooError = HPyErr_NewExceptionWithDoc(ctx, "mytest.FooError", |
| 566 |
HPyBytes_AsString(ctx, h_doc), h_base, h_dict); |
| 567 |
} |
| 568 |
|
| 569 |
if (HPy_IsNull(h_FooError)) |
| 570 |
return HPy_NULL; |
| 571 |
HPyErr_SetString(ctx, h_FooError, "hello"); |
| 572 |
return HPy_NULL; |
| 573 |
} |
| 574 |
@EXPORT(f) |
| 575 |
@INIT |
| 576 |
""") |
| 577 |
|
| 578 |
def check(base, dict_, doc): |
| 579 |
with pytest.raises(Exception) as exc: |
| 580 |
mod.f(True, base, dict_, doc) |
| 581 |
assert issubclass(exc.type, RuntimeError if base else Exception) |
| 582 |
assert exc.type.__name__ == 'FooError', exc.value |
| 583 |
assert exc.type.__module__ == 'mytest' |
| 584 |
if doc is None: |
| 585 |
assert exc.type.__doc__ is None |
| 586 |
else: |
| 587 |
assert exc.type.__doc__ == doc.decode("utf-8") |
| 588 |
|
| 589 |
if dict_: |
| 590 |
assert exc.type.__dict__["test"] == "pass" |
| 591 |
mod.f(False, None, None, None) |
| 592 |
|
| 593 |
check(base=None, dict_=None, doc=None) |
| 594 |
check(base=None, dict_=None, doc=b'mytest') |
| 595 |
check(base=None, dict_={"test": "pass"}, doc=None) |
| 596 |
check(base=None, dict_={"test": "pass"}, doc=b'mytest') |
| 597 |
check(base=RuntimeError, dict_=None, doc=None) |
| 598 |
check(base=RuntimeError, dict_=None, doc=b'mytest') |
| 599 |
check(base=RuntimeError, dict_={"test": "pass"}, doc=None) |
| 600 |
check(base=RuntimeError, dict_={"test": "pass"}, doc=b'mytest') |
| 601 |
|
| 602 |
def test_exception_matches(self): |
| 603 |
import pytest |
| 604 |
mod = self.make_module(""" |
| 605 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 606 |
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs) |
| 607 |
{ |
| 608 |
HPyTracker ht; |
| 609 |
HPy fun, fun_args; |
| 610 |
HPy expected_exc_type; |
| 611 |
HPy tmp; |
| 612 |
HPy result; |
| 613 |
|
| 614 |
if (!HPyArg_Parse(ctx, &ht, args, nargs, "OOO", &fun, &fun_args, &expected_exc_type)) { |
| 615 |
return HPy_NULL; |
| 616 |
} |
| 617 |
|
| 618 |
tmp = HPy_CallTupleDict(ctx, fun, fun_args, HPy_NULL); |
| 619 |
if (HPy_IsNull(tmp)) { |
| 620 |
if (HPyErr_ExceptionMatches(ctx, expected_exc_type)) { |
| 621 |
HPyErr_Clear(ctx); |
| 622 |
result = HPy_Dup(ctx, ctx->h_True); |
| 623 |
} else { |
| 624 |
// propagate the unexpected exception to the caller |
| 625 |
result = HPy_NULL; |
| 626 |
} |
| 627 |
} else { |
| 628 |
HPy_Close(ctx, tmp); |
| 629 |
result = HPy_Dup(ctx, ctx->h_False); |
| 630 |
} |
| 631 |
|
| 632 |
HPyTracker_Close(ctx, ht); |
| 633 |
return result; |
| 634 |
} |
| 635 |
@EXPORT(f) |
| 636 |
@INIT |
| 637 |
""") |
| 638 |
|
| 639 |
class DummyException(Exception): |
| 640 |
pass |
| 641 |
|
| 642 |
def raise_exception(e): |
| 643 |
raise e |
| 644 |
|
| 645 |
def do_not_raise_exception(*args): |
| 646 |
return None |
| 647 |
|
| 648 |
exc_types = (StopAsyncIteration, StopIteration, GeneratorExit, ArithmeticError, LookupError, AssertionError, |
| 649 |
AttributeError, BufferError, EOFError, FloatingPointError, OSError, ImportError, ModuleNotFoundError, IndexError, |
| 650 |
KeyError, KeyboardInterrupt, MemoryError, NameError, OverflowError, RuntimeError, RecursionError, NotImplementedError, |
| 651 |
SyntaxError, IndentationError, TabError, ReferenceError, SystemError, SystemExit, TypeError, UnboundLocalError, |
| 652 |
ValueError, ZeroDivisionError, BlockingIOError, BrokenPipeError, ChildProcessError, ConnectionError, ConnectionAbortedError, |
| 653 |
ConnectionRefusedError, ConnectionResetError, FileExistsError, FileNotFoundError, InterruptedError, IsADirectoryError, |
| 654 |
NotADirectoryError, PermissionError, ProcessLookupError, TimeoutError) |
| 655 |
|
| 656 |
|
| 657 |
assert not mod.f(do_not_raise_exception, tuple(), DummyException) |
| 658 |
|
| 659 |
|
| 660 |
assert mod.f(raise_exception, (DummyException, ), DummyException) |
| 661 |
with pytest.raises(DummyException): |
| 662 |
mod.f(raise_exception, (DummyException, ), StopIteration) |
| 663 |
|
| 664 |
|
| 665 |
for exc_type in exc_types: |
| 666 |
assert mod.f(raise_exception, (exc_type, ), BaseException) |
| 667 |
assert mod.f(raise_exception, (DummyException, ), BaseException) |
| 668 |
|
| 669 |
|
| 670 |
for exc_type in exc_types: |
| 671 |
res = mod.f(raise_exception, (exc_type, ), exc_types) |
| 672 |
with pytest.raises(DummyException): |
| 673 |
mod.f(raise_exception, (DummyException, ), exc_types) |
| 674 |
|
| 675 |
def test_HPyErr_WriteUnraisable(self, python_subprocess): |
| 676 |
mod = self.compile_module(""" |
| 677 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 678 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 679 |
{ |
| 680 |
HPyErr_SetString(ctx, ctx->h_ValueError, "error message"); |
| 681 |
HPyErr_WriteUnraisable(ctx, HPy_NULL); |
| 682 |
return HPyBool_FromLong(ctx, HPyErr_Occurred(ctx)); |
| 683 |
} |
| 684 |
@EXPORT(f) |
| 685 |
@INIT |
| 686 |
""") |
| 687 |
if not SUPPORTS_SYS_EXECUTABLE: |
| 688 |
|
| 689 |
|
| 690 |
return |
| 691 |
|
| 692 |
result = python_subprocess.run(mod, "mod.f()") |
| 693 |
assert result.returncode == 0 |
| 694 |
|
| 695 |
|
| 696 |
def test_HPyErr_Format(self): |
| 697 |
import pytest |
| 698 |
mod = self.make_module(""" |
| 699 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 700 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 701 |
{ |
| 702 |
return HPyErr_Format(ctx, ctx->h_ValueError, "Formatted '%S' and %d", arg, 42); |
| 703 |
} |
| 704 |
@EXPORT(f) |
| 705 |
@INIT |
| 706 |
""") |
| 707 |
with pytest.raises(ValueError, match="Formatted 'error message' and 42"): |
| 708 |
mod.f("error message") |