Skip to content

Commit

Permalink
Add clif/testing/python/absl_cord_test.py
Browse files Browse the repository at this point in the history
The new test passes with PyCLIF-pybind11 only with cl/620916112.

GitHub testing: #95

PiperOrigin-RevId: 620966877
  • Loading branch information
rwgk committed Apr 2, 2024
1 parent 7497c35 commit f2a7c07
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
32 changes: 32 additions & 0 deletions clif/python/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,24 @@ namespace clif {
//// To Python conversions.

// bytes

PyObject* Clif_PyObjFrom(std::string_view c, const py::PostConv& pc) {
return pc.Apply(PyBytes_FromStringAndSize(c.data(), c.size()));
}

#ifndef ABSL_USES_STD_STRING_VIEW
PyObject* Clif_PyObjFrom(absl::string_view c, const py::PostConv& pc) {
return pc.Apply(PyBytes_FromStringAndSize(c.data(), c.size()));
}
#endif

PyObject* Clif_PyObjFrom(const absl::Cord& c, const py::PostConv& pc) {
std::string s(c);
PyObject* py = PyBytes_FromStringAndSize(s.c_str(), s.size());
if (!py) return nullptr;
return pc.Apply(py);
}

PyObject* UnicodeFromBytes(PyObject* b) {
if (!b || PyUnicode_Check(b)) return b;
if (!PyBytes_Check(b)) {
Expand Down Expand Up @@ -409,4 +423,22 @@ bool Clif_PyObjAs(PyObject* p, std::string_view* c) {
return false;
}

#ifndef ABSL_USES_STD_STRING_VIEW
bool Clif_PyObjAs(PyObject* p, absl::string_view* c) {
std::string_view std_sv;
if (Clif_PyObjAs(p, &std_sv)) {
*c = absl::string_view(std_sv.data(), std_sv.size());
return true;
}
return false;
}
#endif

bool Clif_PyObjAs(PyObject* p, absl::Cord* c) {
CHECK(c != nullptr);
return py::ObjToStr(p, [c](const char* data, size_t length) {
*c = absl::string_view(data, length);
});
}

} // namespace clif
11 changes: 11 additions & 0 deletions clif/python/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ inline Clif_PyObjFrom(T c, const py::PostConv& pc) {
return pc.Apply(PyBool_FromLong(c));
}

// CLIF use `::std::string_view` as bytes
// CLIF use `::absl::string_view` as bytes
PyObject* Clif_PyObjFrom(std::string_view, const py::PostConv&);
// absl::string_view has an implicit conversion from std::string.

// CLIF use `::absl::Cord` as bytes
PyObject* Clif_PyObjFrom(const absl::Cord&, const py::PostConv&);

// CLIF use `::std::string` as bytes

typedef const char* char_ptr; // A distinct type for constexpr CONST string.
Expand Down Expand Up @@ -220,6 +227,10 @@ bool Clif_PyObjAs(PyObject*, bool*);
bool Clif_PyObjAs(PyObject*, std::string*);
bool Clif_PyObjAs(PyObject* py, std::shared_ptr<std::string>* c);
bool Clif_PyObjAs(PyObject*, std::string_view*);
#ifndef ABSL_USES_STD_STRING_VIEW
bool Clif_PyObjAs(PyObject* p, absl::string_view* c);
#endif
bool Clif_PyObjAs(PyObject*, absl::Cord*);

PyObject* UnicodeFromBytes(PyObject*);

Expand Down
15 changes: 15 additions & 0 deletions clif/testing/absl_cord.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef CLIF_TESTING_ABSL_CORD_H_
#define CLIF_TESTING_ABSL_CORD_H_

#include <string>

#include "absl/strings/cord.h"

namespace clif_testing_absl_cord {

inline std::string PassAbslCord(const absl::Cord& c) { return std::string(c); }
inline absl::Cord ReturnAbslCord(const std::string& s) { return absl::Cord(s); }

} // namespace clif_testing_absl_cord

#endif // CLIF_TESTING_ABSL_CORD_H_
2 changes: 2 additions & 0 deletions clif/testing/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ add_pyclif_library_for_test(t10 t10.clif

add_pyclif_library_for_test(t12 t12.clif)

add_pyclif_library_for_test(absl_cord absl_cord.clif)

add_pyclif_library_for_test(absl_int128 absl_int128.clif)

add_pyclif_library_for_test(absl_uint128 absl_uint128.clif)
Expand Down
6 changes: 6 additions & 0 deletions clif/testing/python/absl_cord.clif
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from "clif/testing/absl_cord.h":
namespace `clif_testing_absl_cord`:
def `PassAbslCord` as PassAbslCordStr(c: `absl::Cord` as str) -> bytes
def `PassAbslCord` as PassAbslCordBytes(c: `absl::Cord` as bytes) -> bytes
def `ReturnAbslCord` as ReturnAbslCordStr(s: bytes) -> `absl::Cord` as str
def `ReturnAbslCord` as ReturnAbslCordBytes(s: bytes) -> `absl::Cord` as bytes
18 changes: 18 additions & 0 deletions clif/testing/python/absl_cord_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from absl.testing import absltest

from clif.testing.python import absl_cord


class AbslCordTest(absltest.TestCase):

def testPassAbslCord(self):
self.assertEqual(absl_cord.PassAbslCordStr('P str'), b'P str')
self.assertEqual(absl_cord.PassAbslCordBytes(b'P bytes'), b'P bytes')

def testReturnAbslCord(self):
self.assertEqual(absl_cord.ReturnAbslCordStr(b'R str'), 'R str')
self.assertEqual(absl_cord.ReturnAbslCordBytes(b'R bytes'), b'R bytes')


if __name__ == '__main__':
absltest.main()

0 comments on commit f2a7c07

Please sign in to comment.