Skip to content

Commit bbef8e7

Browse files
chore(ci): fix mspack core tests (#15593)
## Description Following #15551 some core tests needed to be updated. This PR fixes this. Also revert the revert on 15551 APPSEC-60180
1 parent 1f52838 commit bbef8e7

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

ddtrace/internal/_encoding.pyx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,8 @@ cdef class Packer(object):
12571257
default_used = True
12581258
continue
12591259
else:
1260-
raise OverflowError("Integer value out of range")
1260+
o = "Integer value out of range"
1261+
continue
12611262
elif PyFloat_CheckExact(o):
12621263
dval = o
12631264
ret = msgpack_pack_double(&self.pk, dval)
@@ -1273,12 +1274,14 @@ cdef class Packer(object):
12731274
if self.encoding == NULL:
12741275
ret = msgpack_pack_unicode(&self.pk, o, ITEM_LIMIT)
12751276
if ret == -2:
1276-
raise ValueError("unicode string is too large")
1277+
o = f"Unicode string is too large {L}"
1278+
continue
12771279
else:
12781280
o = PyUnicode_AsEncodedString(o, self.encoding, self.unicode_errors)
12791281
L = len(o)
12801282
if L > ITEM_LIMIT:
1281-
raise ValueError("unicode string is too large")
1283+
o = f"Unicode string is too large {L}"
1284+
continue
12821285
ret = msgpack_pack_raw(&self.pk, L)
12831286
if ret == 0:
12841287
rawval = o
@@ -1287,7 +1290,8 @@ cdef class Packer(object):
12871290
d = <dict>o
12881291
L = len(d)
12891292
if L > ITEM_LIMIT:
1290-
raise ValueError("dict is too large")
1293+
o = f"Dictionnary is too large {L}"
1294+
continue
12911295
ret = msgpack_pack_map(&self.pk, L)
12921296
if ret == 0:
12931297
for k, v in d.items():
@@ -1300,7 +1304,8 @@ cdef class Packer(object):
13001304
elif PyList_CheckExact(o):
13011305
L = Py_SIZE(o)
13021306
if L > ITEM_LIMIT:
1303-
raise ValueError("list is too large")
1307+
o = f"List is too large {L}"
1308+
continue
13041309
ret = msgpack_pack_array(&self.pk, L)
13051310
if ret == 0:
13061311
for v in o:
@@ -1313,7 +1318,8 @@ cdef class Packer(object):
13131318
else:
13141319
ret = msgpack_pack_false(&self.pk)
13151320
else:
1316-
PyErr_Format(TypeError, b"can not serialize '%.200s' object", Py_TYPE(o).tp_name)
1321+
o = f"Can not serialize [{type(o).__name__}] object"
1322+
continue
13171323
return ret
13181324

13191325
cpdef pack(self, object obj):

tests/tracer/test_encoders.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,29 @@ def test_encode_traces_json_v2(self):
231231

232232
def test_encode_meta_struct():
233233
# test encoding for MsgPack format
234-
encoder = MSGPACK_ENCODERS["v0.4"](2 << 10, 2 << 10)
234+
# Add error case with recovery
235+
encoder = MSGPACK_ENCODERS["v0.4"](1 << 11, 1 << 11)
235236
super_span = Span(name="client.testing", trace_id=1)
236-
payload = {"tttt": {"iuopç": [{"abcd": 1, "bcde": True}, {}]}, "zzzz": b"\x93\x01\x02\x03", "ZZZZ": [1, 2, 3]}
237+
238+
class T:
239+
pass
240+
241+
payload = {
242+
"tttt": {"iuopç": [{"abcd": 1, "bcde": True}, {}]},
243+
"zzzz": b"\x93\x01\x02\x03",
244+
"ZZZZ": [1, 2, 3],
245+
"error_object": object(),
246+
"error_integer": 1 << 129,
247+
"list": [{}, {"x": "a"}, {"error_custom": T()}],
248+
}
249+
payload_expected = {
250+
"tttt": {"iuopç": [{"abcd": 1, "bcde": True}, {}]},
251+
"zzzz": b"\x93\x01\x02\x03",
252+
"ZZZZ": [1, 2, 3],
253+
"error_object": "Can not serialize [object] object",
254+
"error_integer": "Integer value out of range",
255+
"list": [{}, {"x": "a"}, {"error_custom": "Can not serialize [T] object"}],
256+
}
237257

238258
super_span._set_struct_tag("payload", payload)
239259
super_span.set_tag("payload", "meta_payload")
@@ -254,7 +274,7 @@ def test_encode_meta_struct():
254274
assert items[0][0][b"trace_id"] == items[0][1][b"trace_id"]
255275
for j in range(2):
256276
assert b"client.testing" == items[0][j][b"name"]
257-
assert msgpack.unpackb(items[0][0][b"meta_struct"][b"payload"]) == payload
277+
assert msgpack.unpackb(items[0][0][b"meta_struct"][b"payload"]) == payload_expected
258278

259279

260280
def decode(obj, reconstruct=True):

tests/vendor/msgpack/test_except.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class DummyException(Exception):
1818

1919

2020
def test_raise_on_find_unsupported_value():
21-
with raises(TypeError):
22-
packb(datetime.datetime.now())
21+
assert unpackb(packb(datetime.datetime.now())) == "Can not serialize [datetime] object"
2322

2423

2524
def test_raise_from_object_hook():

tests/vendor/msgpack/test_limits.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from __future__ import unicode_literals
77

88
from msgpack import Packer
9-
from msgpack import PackOverflowError
109
from msgpack import PackValueError
1110
from msgpack import Unpacker
1211
from msgpack import UnpackValueError
@@ -19,13 +18,11 @@
1918
def test_integer():
2019
x = -(2**63)
2120
assert unpackb(packb(x)) == x
22-
with pytest.raises(PackOverflowError):
23-
packb(x - 1)
21+
assert unpackb(packb(x - 1)) == "Integer value out of range"
2422

2523
x = 2**64 - 1
2624
assert unpackb(packb(x)) == x
27-
with pytest.raises(PackOverflowError):
28-
packb(x + 1)
25+
assert unpackb(packb(x + 1)) == "Integer value out of range"
2926

3027

3128
def test_array_header():

tests/vendor/msgpack/test_subtype.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from collections import namedtuple
55

6-
import pytest
6+
from msgpack import unpackb
77

88
from ddtrace.internal._encoding import packb
99

@@ -24,9 +24,6 @@ class MyTuple(tuple):
2424

2525

2626
def test_types():
27-
with pytest.raises(TypeError):
28-
assert packb(MyDict()) == packb(dict())
29-
with pytest.raises(TypeError):
30-
assert packb(MyList()) == packb(list())
31-
with pytest.raises(TypeError):
32-
assert packb(MyNamedTuple(1, 2)) == packb((1, 2))
27+
assert unpackb(packb(MyDict())) == "Can not serialize [MyDict] object"
28+
assert unpackb(packb(MyList())) == "Can not serialize [MyList] object"
29+
assert unpackb(packb(MyNamedTuple(1, 2))) == "Can not serialize [MyNamedTuple] object"

0 commit comments

Comments
 (0)