2021年12月17日 星期五

Python pytest set_exception_handler asyncio exception

 https://github.com/pytest-dev/pytest-asyncio/issues/205


New issue

set a default loop exception_handler that warns (similar to -p unraisablehook) #205

 Open
graingert opened this issue on Feb 23 · 1 comment

Comments

@graingert
Member

graingert commented on Feb 23

consider the following test:

import pytest
import asyncio

@pytest.mark.asyncio
async def test_foo():
    def raise_():
        raise Exception

    asyncio.get_running_loop().call_soon(raise_)
    await asyncio.sleep(0)

it passes cleanly.

However if you explicitly collect any errors:

import pytest
import asyncio

@pytest.mark.asyncio
async def test_foo():
    loop = asyncio.get_running_loop()
    old_exception_handler = loop.get_exception_handler()
    exceptions = []

    def exception_handler(*args, **kwargs):
        exceptions.append((args, kwargs))

    loop.set_exception_handler(exception_handler)
    try:
        def raise_():
            raise Exception

        asyncio.get_running_loop().call_soon(raise_)
        await asyncio.sleep(0)
        assert exceptions == []
    finally:
        loop.set_exception_handler(old_exception_handler)
=================================== test session starts ====================================
platform linux -- Python 3.9.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/graingert/projects/anyio, configfile: pyproject.toml
plugins: hypothesis-6.2.0, asyncio-0.14.0, anyio-2.1.0.post21+dirty
collected 1 item                                                                           

ham.py F                                                                             [100%]

========================================= FAILURES =========================================
_________________________________________ test_foo _________________________________________
ham.py:20: in test_foo
    assert exceptions == []
E   AssertionError: assert [((<_UnixSele...py:15'}), {})] == []
E     Left contains one more item: ((<_UnixSelectorEventLoop running=True closed=False debug=False>, {'exception': Exception(), 'handle': <Handle test_fo...y:15>, 'message': 'Exception in callback test_foo.<locals>.raise_() at /home/graingert/projects/anyio/ham.py:15'}), {})
E     Use -v to get the full diff
==================================== 1 failed in 0.04s ====================================
@HMaker

HMaker commented on Apr 5

I have a similar case, exceptions in background tasks pass silently. It would be good if background exceptions were checked at test end and re-raised there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
@graingert@HMaker

2021年12月15日 星期三

Little and Big Endianess, Network Byte Order, Host Byte Order

https://en.wikipedia.org/wiki/Endianness


Boiled Egg

In Gulliver's Travels, there is a discussion about how to break the shell of a boiled egg from the big of little end.


C programming

int x = 0x01234567;  // x has 4 bytes, low byte is 67
&x;  // address of &x is 0x100

Then x actually occupies 4 bytes at addresses of 0x100, 0x101, 0x102, 0x103.

if the high byte of 01 is stored at low address of 0x100, it is called big endianess.
If the low byte of 67 is stored at low address of 0x100, it is called little endianess.

x86 (i386) CPU uses little endianess. CPU order means host byte order.

Network byte order uses big endianess.


Berkeley Socket API

hotn means host byte order to network byte order
ntoh means network byte order to host byte order

#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);


C++ CPU endianess check

const int num = 1;
if (*(char *)&num == 1) {
    printf("Little-Endian\n");  # AMD Ryzen
} else {
    printf("Big-Endian\n");
}

Python CPU endianess check

import sys
print(sys.byteorder)  #  'little' on AMD Ryzen


Ubuntu endianess check

Terminal lscpu says AMD Ryzen uses Littel Endian

$ lscpu
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   43 bits physical, 48 bits virtual
CPU(s):                          16
On-line CPU(s) list:             0-15
Thread(s) per core:              2
Core(s) per socket:              8
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       AuthenticAMD
CPU family:                      23
Model:                           113
Model name:                      AMD Ryzen 7 3700X 8-Core Processor

Python to bytes(byteorder='big' or 'small')

16 0x10
32 0x20
64 0x40
128 0x80
256 0x100
512 0x200
1024 0x400 or 0x0400

(1024).to_bytes(2, byteorder='big')  # 0x0400
b'\x04\x00'
(1025).to_bytes(2, byteorder='big')  # 0x0401
b'\x04\x01'  # MSB at beginning of byte array

Python 0x01234567 to bytes

(0x01234567).to_bytes(4, byteorder='big')  # network byte order
b'\x01#Eg'  # 01 23 45 67  #MSB 01 at beginning of byte array (big endian is natural for human)

(0x01234567).to_bytes(4, byteorder='little')  # host byte order
b'gE#\x01  # 67 45 23 01 # MSB 01 at end of byte array,

C++ memcpy

int main() {
char buffer[4] = {};
unsigned int ival = 0x01234567;
memcpy(buffer, (char*)&ival, sizeof(unsigned int));
hexdump(buffer, 4);
return 0;
}

000000: 67 45 23 01                                      gE#.

// LSB 67 is at begin of byte array (low address), little endianess
// MSB 01 at end of byte array, i.e. little endian by memcpy()



2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...