1{
2 buildPackages,
3 buildPythonPackage,
4 fetchpatch,
5 isPyPy,
6 lib,
7 protobuf,
8 pytestCheckHook,
9 pythonAtLeast,
10 tzdata,
11}:
12
13assert lib.versionAtLeast protobuf.version "3.21" -> throw "Protobuf 3.20 or older required";
14
15buildPythonPackage {
16 inherit (protobuf) pname src;
17
18 version = protobuf.version;
19
20 sourceRoot = "${protobuf.src.name}/python";
21
22 patches = lib.optionals (pythonAtLeast "3.11") [
23 (fetchpatch {
24 name = "support-python311.patch";
25 url = "https://github.com/protocolbuffers/protobuf/commit/2206b63c4649cf2e8a06b66c9191c8ef862ca519.diff";
26 stripLen = 1; # because sourceRoot above
27 hash = "sha256-3GaoEyZIhS3QONq8LEvJCH5TdO9PKnOgcQF0GlEiwFo=";
28 })
29 ];
30
31 prePatch = ''
32 if [[ "$(<../version.json)" != *'"python": "'"$version"'"'* ]]; then
33 echo "Python library version mismatch. Derivation version: $version, actual: $(<../version.json)"
34 exit 1
35 fi
36 '';
37
38 # Remove the line in setup.py that forces compiling with C++14. Upstream's
39 # CMake build has been updated to support compiling with other versions of
40 # C++, but the Python build has not. Without this, we observe compile-time
41 # errors using GCC.
42 #
43 # Fedora appears to do the same, per this comment:
44 #
45 # https://github.com/protocolbuffers/protobuf/issues/12104#issuecomment-1542543967
46 #
47 postPatch = ''
48 sed -i "/extra_compile_args.append('-std=c++14')/d" setup.py
49
50 substituteInPlace google/protobuf/internal/json_format_test.py \
51 --replace-fail assertRaisesRegexp assertRaisesRegex
52 '';
53
54 nativeBuildInputs = lib.optional isPyPy tzdata;
55
56 buildInputs = [ protobuf ];
57
58 propagatedNativeBuildInputs = [
59 # For protoc of the same version.
60 buildPackages."protobuf${lib.versions.major protobuf.version}_${lib.versions.minor protobuf.version}"
61 ];
62
63 setupPyGlobalFlags = [ "--cpp_implementation" ];
64
65 nativeCheckInputs = [ pytestCheckHook ];
66
67 disabledTests = lib.optionals isPyPy [
68 # error message differs
69 "testInvalidTimestamp"
70 # requires tracemalloc which pypy does not implement
71 # https://foss.heptapod.net/pypy/pypy/-/issues/3048
72 "testUnknownFieldsNoMemoryLeak"
73 # assertion is not raised for some reason
74 "testStrictUtf8Check"
75 ];
76
77 pythonImportsCheck = [
78 "google.protobuf"
79 "google.protobuf.internal._api_implementation" # Verify that --cpp_implementation worked
80 ];
81
82 passthru = {
83 inherit protobuf;
84 };
85
86 meta = with lib; {
87 description = "Protocol Buffers are Google's data interchange format";
88 homepage = "https://developers.google.com/protocol-buffers/";
89 license = licenses.bsd3;
90 maintainers = with maintainers; [ ];
91 };
92}