Skip to content

Commit

Permalink
Fix segfault when calling Instance.to_bytes() for drivers not support…
Browse files Browse the repository at this point in the history
…ing memsave
  • Loading branch information
jesper-friis committed Aug 12, 2023
1 parent 6b0fc01 commit 9036d65
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion bindings/python/dlite-entity.i
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,11 @@ Call signatures:
void to_bytes(const char *driver, unsigned char **ARGOUT_BYTES, size_t *LEN) {
unsigned char *buf=NULL;
int m, n = dlite_instance_memsave(driver, buf, 0, $self);
if (n < 0) return;
if (n < 0) {
*ARGOUT_BYTES = NULL;
*LEN = 0;
return;
}
if (!(buf = malloc(n))) {
dlite_err(dliteMemoryError, "allocation failure");
return;
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/tests/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
try:
import pytest
HAVE_PYTEST = True
except ImportError:
except ModuleNotFoundError:
HAVE_PYTEST = False


Expand Down
16 changes: 15 additions & 1 deletion bindings/python/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

import dlite

try:
import pytest
HAVE_PYTEST = True
except ModuleNotFoundError:
HAVE_PYTEST = False


thisdir = os.path.abspath(os.path.dirname(__file__))

url = 'json://' + thisdir + '/MyEntity.json'
Expand Down Expand Up @@ -37,7 +44,6 @@
inst = dlite.Instance.from_url(f'json://{thisdir}/inst.json#my-data')



# Test yaml
try:
import yaml
Expand Down Expand Up @@ -95,3 +101,11 @@
#del inst
# FIXME: read from inst.ttl not db.xml
inst3 = dlite.Instance.from_url('rdf://db.xml#my-data')


# Tests for issue #587
bytearr = inst.to_bytes("yaml")
print(bytes(bytearr).decode())
if HAVE_PYTEST:
with pytest.raises(dlite.DLiteError):
inst.to_bytes("json")

0 comments on commit 9036d65

Please sign in to comment.