1{
2 buildPackages,
3 buildPythonPackage,
4 fetchpatch,
5 isPyPy,
6 lib,
7 stdenv,
8 numpy,
9 protobuf,
10 pytestCheckHook,
11 pythonAtLeast,
12 substituteAll,
13 tzdata,
14}:
15
16assert lib.versionOlder protobuf.version "21" -> throw "Protobuf 21 or newer required";
17
18let
19 protobufVersionMajor = lib.versions.major protobuf.version;
20 protobufVersionMinor = lib.versions.minor protobuf.version;
21in
22buildPythonPackage {
23 inherit (protobuf) pname src;
24
25 # protobuf 21 corresponds with its python library 4.21
26 version = "4.${protobufVersionMajor}.${protobufVersionMinor}";
27 format = "setuptools";
28
29 sourceRoot = "${protobuf.src.name}/python";
30
31 patches =
32 lib.optionals (lib.versionAtLeast protobuf.version "22") [
33 # Replace the vendored abseil-cpp with nixpkgs'
34 (substituteAll {
35 src = ./use-nixpkgs-abseil-cpp.patch;
36 abseil_cpp_include_path = "${lib.getDev protobuf.abseil-cpp}/include";
37 })
38 ]
39 ++ lib.optionals (pythonAtLeast "3.11" && lib.versionOlder protobuf.version "22") [
40 (fetchpatch {
41 name = "support-python311.patch";
42 url = "https://github.com/protocolbuffers/protobuf/commit/2206b63c4649cf2e8a06b66c9191c8ef862ca519.diff";
43 stripLen = 1; # because sourceRoot above
44 hash = "sha256-3GaoEyZIhS3QONq8LEvJCH5TdO9PKnOgcQF0GlEiwFo=";
45 })
46 ];
47
48 prePatch = ''
49 if [[ "$(<../version.json)" != *'"python": "'"$version"'"'* ]]; then
50 echo "Python library version mismatch. Derivation version: $version, actual: $(<../version.json)"
51 exit 1
52 fi
53 '';
54
55 # Remove the line in setup.py that forces compiling with C++14. Upstream's
56 # CMake build has been updated to support compiling with other versions of
57 # C++, but the Python build has not. Without this, we observe compile-time
58 # errors using GCC.
59 #
60 # Fedora appears to do the same, per this comment:
61 #
62 # https://github.com/protocolbuffers/protobuf/issues/12104#issuecomment-1542543967
63 #
64 postPatch = ''
65 sed -i "/extra_compile_args.append('-std=c++14')/d" setup.py
66
67 # The former function has been renamed into the latter in Python 3.12.
68 # Does not apply to all protobuf versions, hence --replace-warn.
69 substituteInPlace google/protobuf/internal/json_format_test.py \
70 --replace-warn assertRaisesRegexp assertRaisesRegex
71 '';
72
73 nativeBuildInputs = lib.optional isPyPy tzdata;
74
75 buildInputs = [ protobuf ];
76
77 propagatedNativeBuildInputs = [
78 # For protoc of the same version.
79 buildPackages."protobuf_${protobufVersionMajor}"
80 ];
81
82 setupPyGlobalFlags = [ "--cpp_implementation" ];
83
84 nativeCheckInputs = [
85 pytestCheckHook
86 ] ++ lib.optionals (lib.versionAtLeast protobuf.version "22") [ numpy ];
87
88 disabledTests =
89 lib.optionals isPyPy [
90 # error message differs
91 "testInvalidTimestamp"
92 # requires tracemalloc which pypy does not implement
93 # https://foss.heptapod.net/pypy/pypy/-/issues/3048
94 "testUnknownFieldsNoMemoryLeak"
95 # assertion is not raised for some reason
96 "testStrictUtf8Check"
97 ]
98 ++ lib.optionals stdenv.hostPlatform.is32bit [
99 # OverflowError: timestamp out of range for platform time_t
100 "testTimezoneAwareDatetimeConversionWhereTimestampLosesPrecision"
101 "testTimezoneNaiveDatetimeConversionWhereTimestampLosesPrecision"
102 ];
103
104 disabledTestPaths =
105 lib.optionals (lib.versionAtLeast protobuf.version "23") [
106 # The following commit (I think) added some internal test logic for Google
107 # that broke generator_test.py. There is a new proto file that setup.py is
108 # not generating into a .py file. However, adding this breaks a bunch of
109 # conflict detection in descriptor_test.py that I don't understand. So let's
110 # just disable generator_test.py for now.
111 #
112 # https://github.com/protocolbuffers/protobuf/commit/5abab0f47e81ac085f0b2d17ec3b3a3b252a11f1
113 #
114 "google/protobuf/internal/generator_test.py"
115 ]
116 ++ lib.optionals (lib.versionAtLeast protobuf.version "25") [
117 "minimal_test.py" # ModuleNotFoundError: No module named 'google3'
118 ];
119
120 pythonImportsCheck = [
121 "google.protobuf"
122 "google.protobuf.internal._api_implementation" # Verify that --cpp_implementation worked
123 ];
124
125 passthru = {
126 inherit protobuf;
127 };
128
129 meta = with lib; {
130 description = "Protocol Buffers are Google's data interchange format";
131 homepage = "https://developers.google.com/protocol-buffers/";
132 license = licenses.bsd3;
133 maintainers = with maintainers; [ knedlsepp ];
134 broken = lib.versionAtLeast protobuf.version "26";
135 };
136}