# Buggy code
def post_build(self, p, pay):
    p += pay
    dataofs = self.dataofs
    if dataofs is None:
        dataofs = 5 + (
            (len(self.get_field("options").i2m(self, self.options)) + 3) // 4
        )  # noqa: E501
        p = p[:12] + chb((dataofs << 4) | orb(p[12]) & 0x0F) + p[13:]
    if self.chksum is None:
        if isinstance(self.underlayer, IP):
            ck = in4_chksum(socket.IPPROTO_TCP, self.underlayer, p)
            p = p[:16] + struct.pack("!H", ck) + p[18:]
        elif (
            conf.ipv6_enabled
            and isinstance(self.underlayer, scapy.layers.inet6.IPv6)
            or isinstance(self.underlayer, scapy.layers.inet6._IPv6ExtHdr)
        ):  # noqa: E501
            ck = scapy.layers.inet6.in6_chksum(socket.IPPROTO_TCP, self.underlayer, p)  # noqa: E501
            p = p[:16] + struct.pack("!H", ck) + p[18:]
        else:
            warning("No IP underlayer to compute checksum. Leaving null.")
    return p


# Traceback (struct.error)
Traceback (most recent call last):
File "../../test.py", line 6, in <module>
bytes(test)
File "/usr/local/lib/python3.6/site-packages/scapy/packet.py", line 487, in __bytes__
return self.build()
File "/usr/local/lib/python3.6/site-packages/scapy/packet.py", line 607, in build
p = self.do_build()
File "/usr/local/lib/python3.6/site-packages/scapy/packet.py", line 592, in do_build
pay = self.do_build_payload()
File "/usr/local/lib/python3.6/site-packages/scapy/packet.py", line 579, in do_build_payload
return self.payload.do_build()
File "/usr/local/lib/python3.6/site-packages/scapy/packet.py", line 592, in do_build
pay = self.do_build_payload()
File "/usr/local/lib/python3.6/site-packages/scapy/packet.py", line 579, in do_build_payload
return self.payload.do_build()
File "/usr/local/lib/python3.6/site-packages/scapy/packet.py", line 594, in do_build
return self.post_build(pkt, pay)
File "/usr/local/lib/python3.6/site-packages/scapy/layers/inet.py", line 629, in post_build
p = p[:12] + chb((dataofs << 4) | orb(p[12]) &amp; 0x0f) + p[13:]
File "/usr/local/lib/python3.6/site-packages/scapy/compat.py", line 70, in chb
return struct.pack("!B", x)
struct.error: ubyte format requires 0 <= number <= 255

# Fixed code
def post_build(self, p, pay):
    p += pay
    dataofs = self.dataofs
    if dataofs is None:
        opt_len = len(self.get_field("options").i2m(self, self.options))
        dataofs = 5 + ((opt_len + 3) // 4)
        dataofs = (dataofs << 4) | orb(p[12]) & 0x0F
        p = p[:12] + chb(dataofs & 0xFF) + p[13:]
    if self.chksum is None:
        if isinstance(self.underlayer, IP):
            ck = in4_chksum(socket.IPPROTO_TCP, self.underlayer, p)
            p = p[:16] + struct.pack("!H", ck) + p[18:]
        elif (
            conf.ipv6_enabled
            and isinstance(self.underlayer, scapy.layers.inet6.IPv6)
            or isinstance(self.underlayer, scapy.layers.inet6._IPv6ExtHdr)
        ):  # noqa: E501
            ck = scapy.layers.inet6.in6_chksum(socket.IPPROTO_TCP, self.underlayer, p)  # noqa: E501
            p = p[:16] + struct.pack("!H", ck) + p[18:]
        else:
            warning("No IP underlayer to compute checksum. Leaving null.")
    return p