Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion mypyc/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,6 @@ def mypycify(
)

if install_librt:
os.makedirs("librt", exist_ok=True)
for name in RUNTIME_C_FILES:
rt_file = os.path.join(build_dir, name)
with open(os.path.join(include_dir(), name), encoding="utf-8") as f:
Expand Down
8 changes: 5 additions & 3 deletions mypyc/lib-rt/librt_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ Buffer_init_internal(BufferObject *self, PyObject *source) {
PyErr_SetString(PyExc_TypeError, "source must be a bytes object");
return -1;
}
self->size = PyBytes_GET_SIZE(source);
self->end = self->size;
self->end = PyBytes_GET_SIZE(source);
// Allocate at least one byte to simplify resizing logic.
// The original bytes buffer has last null byte, so this is safe.
self->size = self->end + 1;
// This returns a pointer to internal bytes data, so make our own copy.
char *buf = PyBytes_AsString(source);
self->buf = PyMem_Malloc(self->size);
memcpy(self->buf, buf, self->size);
memcpy(self->buf, buf, self->end);
} else {
self->buf = PyMem_Malloc(START_SIZE);
self->size = START_SIZE;
Expand Down
9 changes: 9 additions & 0 deletions mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2866,6 +2866,15 @@ test_buffer_roundtrip_interpreted()
test_buffer_int_size_interpreted()
test_buffer_str_size_interpreted()

[case testBufferEmpty_librt_internal]
from librt.internal import Buffer, write_int, read_int

def test_empty() -> None:
b = Buffer(b"")
write_int(b, 42)
b1 = Buffer(b.getvalue())
assert read_int(b1) == 42

[case testEnumMethodCalls]
from enum import Enum
from typing import overload, Optional, Union
Expand Down
6 changes: 6 additions & 0 deletions mypyc/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
)
)

if librt:
# This hack forces Python to prefer the local "installation".
os.makedirs("librt", exist_ok=True)
with open(os.path.join("librt", "__init__.py"), "a"):
pass

if not run_setup(setup_file, ["build_ext", "--inplace"]):
if testcase.config.getoption("--mypyc-showc"):
show_c(cfiles)
Expand Down