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