Skip to content

Commit d2049cc

Browse files
authored
Merge pull request #380 from Zaczero/master
Better Python Runtime
2 parents f089248 + 66f66fe commit d2049cc

8 files changed

Lines changed: 167 additions & 143 deletions

File tree

Core/Generators/Python/PythonGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private string CompileEncodeField(TypeBase type, string target, int depth = 0, i
113113
var enumAppendix = isEnum ? ".value" : "";
114114
return type switch
115115
{
116-
ArrayType at when at.IsBytes() => $"writer.writeBytes({target})",
116+
ArrayType at when at.IsBytes() => $"writer.write_bytes({target})",
117117
ArrayType at =>
118118
$"length{depth} = len({target})" + nl +
119119
$"writer.write_uint32(length{depth})" + nl +
@@ -254,7 +254,7 @@ private string CompileDecodeField(TypeBase type, string target, int depth = 0)
254254
var i = GeneratorUtils.LoopVariable(depth);
255255
return type switch
256256
{
257-
ArrayType at when at.IsBytes() => $"{target} = reader.readBytes()",
257+
ArrayType at when at.IsBytes() => $"{target} = reader.read_bytes()",
258258
ArrayType at =>
259259
$"length{depth} = reader.read_uint32()" + nl +
260260
$"{target} = []" + nl +

Laboratory/Integration/Python/src/lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from schema import Library, Instrument, Album, StudioAlbum, LiveAlbum, Song, Musician
2-
from datetime import datetime
2+
from datetime import datetime, timezone
33
from uuid import UUID
44

55
def make_lib():
@@ -24,7 +24,7 @@ def make_lib():
2424

2525
adams_apple_album = LiveAlbum()
2626
adams_apple_album.venueName = "Tunisia"
27-
adams_apple_album.concertDate = datetime.fromtimestamp(528205479)
27+
adams_apple_album.concertDate = datetime.fromtimestamp(528205479, tz=timezone.utc)
2828

2929
unnamed_song = Song()
3030
unnamed_song.year = 1965

Laboratory/Python/test/test_basic_types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from schemas import BasicTypes
2-
from datetime import datetime
2+
from datetime import datetime, timezone
33
from uuid import UUID
44

55

@@ -17,7 +17,7 @@ def test_write_read():
1717
a_float64=2.0,
1818
a_string="a_string",
1919
a_guid=UUID("ff1ed055-1839-4d7f-84d3-5ca28fa298b9"),
20-
a_date=datetime(1999, 5, 17),
20+
a_date=datetime(1999, 5, 17, tzinfo=timezone.utc),
2121
)
2222

2323
encoded = BasicTypes.encode(bt)
@@ -36,6 +36,6 @@ def test_write_read():
3636
assert decoded.a_float64 == 2.0
3737
assert decoded.a_string == "a_string"
3838
assert decoded.a_guid == UUID("ff1ed055-1839-4d7f-84d3-5ca28fa298b9")
39-
assert decoded.a_date == datetime(1999, 5, 17)
39+
assert decoded.a_date == datetime(1999, 5, 17, tzinfo=timezone.utc)
4040

4141

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from datetime import datetime, timezone
2+
3+
from python_bebop.bebop import BebopReader, BebopWriter
4+
5+
6+
def test_date_precision_and_utc():
7+
# Arrange: Create a writer and a date with specific microseconds in UTC
8+
writer = BebopWriter()
9+
expected_dt = datetime(2023, 1, 1, 12, 0, 0, 123456, tzinfo=timezone.utc)
10+
11+
# Act: Write the date and read it back
12+
writer.write_date(expected_dt)
13+
buffer = writer.to_list()
14+
reader = BebopReader.from_buffer(bytearray(buffer))
15+
actual_dt = reader.read_date()
16+
17+
# Assert: Verify precision (microseconds) and Timezone (UTC) are preserved
18+
assert actual_dt.microsecond == 123456
19+
assert actual_dt.tzinfo == timezone.utc
20+
assert actual_dt == expected_dt
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from python_bebop.bebop import BebopReader, BebopWriter
2+
3+
4+
def test_fill_message_length_writes_4_bytes():
5+
# Arrange: Prepare writer with reserved length slot and dummy payload
6+
writer = BebopWriter()
7+
length_index = writer.reserve_message_length()
8+
payload = bytearray(b"\x01" * 10)
9+
# Manually write payload since write_bytes now enforces length prefix
10+
writer._buffer += payload
11+
12+
# Act: Backfill the correct length into the reserved slot
13+
writer.fill_message_length(length_index, len(payload))
14+
15+
# Assert: Verify the length was written correctly by reading it back
16+
reader = BebopReader.from_buffer(bytearray(writer.to_list()))
17+
18+
read_length = reader.read_message_length()
19+
assert read_length == 10
20+
21+
remaining = reader._buffer[reader.index :]
22+
assert remaining == payload

Runtime/Python/setup.cfg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ author_email = korbinian.habereder@gmail.com
66
description = bebop runtime bindings for Python
77
long_description = file: README.md
88
long_description_content_type = text/markdown
9-
url = https://github.com/liquidiert/bebop
9+
url = https://github.com/6over3/bebop
1010
project_urls =
11-
Bug Tracker = https://github.com/liquidiert/bebop
11+
Bug Tracker = https://github.com/6over3/bebop
1212
classifiers =
1313
Programming Language :: Python :: 3
14-
License :: OSI Approved :: Apache Software License
1514
Operating System :: OS Independent
15+
license = Apache-2.0
1616

1717
[options]
1818
package_dir =

0 commit comments

Comments
 (0)