Skip to content

Commit 6441dfa

Browse files
mprekajskicopybara-github
authored andcommitted
Simplify the C interface of DataBag.adopt_stub
PiperOrigin-RevId: 721717166 Change-Id: I7502cc4caa98755250a3bc7976c4d0e3c9f502d0
1 parent 4753c64 commit 6441dfa

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

py/koladata/types/data_bag.cc

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,18 +1207,10 @@ absl::Nullable<PyObject*> PyDataBag_merge_inplace(PyObject* self,
12071207
Py_RETURN_NONE;
12081208
}
12091209

1210-
absl::Nullable<PyObject*> PyDataBag_adopt(PyObject* self,
1211-
PyObject* const* py_args,
1212-
Py_ssize_t nargs) {
1210+
absl::Nullable<PyObject*> PyDataBag_adopt(PyObject* self, PyObject* ds) {
12131211
arolla::python::DCheckPyGIL();
1214-
DataBagPtr db = UnsafeDataBagPtr(self);
1215-
if (nargs != 1) {
1216-
PyErr_Format(PyExc_ValueError,
1217-
"DataBag.adopt accepts exactly 1 argument, got %d", nargs);
1218-
return nullptr;
1219-
}
1220-
1221-
const DataSlice* slice = UnwrapDataSlice(py_args[0], "slice");
1212+
const DataBagPtr& db = UnsafeDataBagPtr(self);
1213+
const DataSlice* slice = UnwrapDataSlice(ds, "slice");
12221214
if (!slice) {
12231215
return nullptr;
12241216
}
@@ -1231,18 +1223,10 @@ absl::Nullable<PyObject*> PyDataBag_adopt(PyObject* self,
12311223
return WrapPyDataSlice(slice->WithBag(std::move(db)));
12321224
}
12331225

1234-
absl::Nullable<PyObject*> PyDataBag_adopt_stub(PyObject* self,
1235-
PyObject* const* py_args,
1236-
Py_ssize_t nargs) {
1226+
absl::Nullable<PyObject*> PyDataBag_adopt_stub(PyObject* self, PyObject* ds) {
12371227
arolla::python::DCheckPyGIL();
1238-
DataBagPtr db = UnsafeDataBagPtr(self);
1239-
if (nargs != 1) {
1240-
PyErr_Format(PyExc_ValueError,
1241-
"DataBag.adopt_stub accepts exactly 1 argument, got %d",
1242-
nargs);
1243-
return nullptr;
1244-
}
1245-
const DataSlice* slice = UnwrapDataSlice(py_args[0], "slice");
1228+
const DataBagPtr& db = UnsafeDataBagPtr(self);
1229+
const DataSlice* slice = UnwrapDataSlice(ds, "slice");
12461230
if (!slice) {
12471231
return nullptr;
12481232
}
@@ -1663,7 +1647,7 @@ have different ids.
16631647
"*bags)\n"
16641648
"--\n\n"
16651649
"DataBag._merge_inplace"},
1666-
{"adopt", (PyCFunction)PyDataBag_adopt, METH_FASTCALL,
1650+
{"adopt", (PyCFunction)PyDataBag_adopt, METH_O,
16671651
"adopt(slice, /)\n"
16681652
"--\n\n"
16691653
R"""(Adopts all data reachable from the given slice into this DataBag.
@@ -1674,7 +1658,7 @@ have different ids.
16741658
Returns:
16751659
The DataSlice with this DataBag (including adopted data) attached.
16761660
)"""},
1677-
{"adopt_stub", (PyCFunction)PyDataBag_adopt_stub, METH_FASTCALL,
1661+
{"adopt_stub", (PyCFunction)PyDataBag_adopt_stub, METH_O,
16781662
"adopt_stub(slice, /)\n"
16791663
"--\n\n"
16801664
R"""(Copies the given DataSlice's schema stub into this DataBag.

py/koladata/types/data_bag_test.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,14 +1741,12 @@ def test_merge_inplace_not_databags(self):
17411741
db1.merge_inplace([x1])
17421742

17431743
def test_adopt_args_errors(self):
1744-
with self.assertRaisesWithLiteralMatch(
1745-
ValueError, 'DataBag.adopt accepts exactly 1 argument, got 0'
1746-
):
1744+
with self.assertRaises(TypeError): # Python runtime error.
17471745
bag().adopt()
1748-
with self.assertRaisesWithLiteralMatch(
1749-
ValueError, 'DataBag.adopt accepts exactly 1 argument, got 2'
1750-
):
1746+
with self.assertRaises(TypeError): # Python runtime error.
17511747
bag().adopt(ds(1), ds(2))
1748+
with self.assertRaisesRegex(TypeError, 'expecting slice to be a DataSlice'):
1749+
bag().adopt(bag())
17521750

17531751
def test_adopt_immutable(self):
17541752
db1 = bag()
@@ -1777,14 +1775,12 @@ def test_adopt(self):
17771775
testing.assert_equivalent(o3.y.no_bag(), ds(2))
17781776

17791777
def test_adopt_stub_args_errors(self):
1780-
with self.assertRaisesWithLiteralMatch(
1781-
ValueError, 'DataBag.adopt_stub accepts exactly 1 argument, got 0'
1782-
):
1778+
with self.assertRaises(TypeError): # Python runtime error.
17831779
bag().adopt_stub()
1784-
with self.assertRaisesWithLiteralMatch(
1785-
ValueError, 'DataBag.adopt_stub accepts exactly 1 argument, got 2'
1786-
):
1780+
with self.assertRaises(TypeError): # Python runtime error.
17871781
bag().adopt_stub(ds(1), ds(2))
1782+
with self.assertRaisesRegex(TypeError, 'expecting slice to be a DataSlice'):
1783+
bag().adopt_stub(bag())
17881784

17891785
def test_adopt_stub_immutable(self):
17901786
db1 = bag()

0 commit comments

Comments
 (0)