support.py
Go to the documentation of this file.
1 # Copyright 2016 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from distutils import errors
16 import os
17 import os.path
18 import shutil
19 import sys
20 import tempfile
21 
22 import commands
23 
24 C_PYTHON_DEV = """
25 #include <Python.h>
26 int main(int argc, char **argv) { return 0; }
27 """
28 C_PYTHON_DEV_ERROR_MESSAGE = """
29 Could not find <Python.h>. This could mean the following:
30  * You're on Ubuntu and haven't run `apt-get install <PY_REPR>-dev`.
31  * You're on RHEL/Fedora and haven't run `yum install <PY_REPR>-devel` or
32  `dnf install <PY_REPR>-devel` (make sure you also have redhat-rpm-config
33  installed)
34  * You're on Mac OS X and the usual Python framework was somehow corrupted
35  (check your environment variables or try re-installing?)
36  * You're on Windows and your Python installation was somehow corrupted
37  (check your environment variables or try re-installing?)
38 """
39 if sys.version_info[0] == 2:
40  PYTHON_REPRESENTATION = 'python'
41 elif sys.version_info[0] == 3:
42  PYTHON_REPRESENTATION = 'python3'
43 else:
44  raise NotImplementedError('Unsupported Python version: %s' % sys.version)
45 
46 C_CHECKS = {
47  C_PYTHON_DEV:
48  C_PYTHON_DEV_ERROR_MESSAGE.replace('<PY_REPR>', PYTHON_REPRESENTATION),
49 }
50 
51 
52 def _compile(compiler, source_string):
53  tempdir = tempfile.mkdtemp()
54  cpath = os.path.join(tempdir, 'a.c')
55  with open(cpath, 'w') as cfile:
56  cfile.write(source_string)
57  try:
58  compiler.compile([cpath])
59  except errors.CompileError as error:
60  return error
61  finally:
62  shutil.rmtree(tempdir)
63 
64 
65 def _expect_compile(compiler, source_string, error_message):
66  if _compile(compiler, source_string) is not None:
67  sys.stderr.write(error_message)
69  "Diagnostics found a compilation environment issue:\n{}".format(
70  error_message))
71 
72 
73 def diagnose_compile_error(build_ext, error):
74  """Attempt to diagnose an error during compilation."""
75  for c_check, message in C_CHECKS.items():
76  _expect_compile(build_ext.compiler, c_check, message)
77  python_sources = [
78  source for source in build_ext.get_source_files()
79  if source.startswith('./src/python') and source.endswith('c')
80  ]
81  for source in python_sources:
82  if not os.path.isfile(source):
83  raise commands.CommandError((
84  "Diagnostics found a missing Python extension source file:\n{}\n\n"
85  "This is usually because the Cython sources haven't been transpiled "
86  "into C yet and you're building from source.\n"
87  "Try setting the environment variable "
88  "`GRPC_PYTHON_BUILD_WITH_CYTHON=1` when invoking `setup.py` or "
89  "when using `pip`, e.g.:\n\n"
90  "pip install -rrequirements.txt\n"
91  "GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .").format(source))
92 
93 
94 def diagnose_attribute_error(build_ext, error):
95  if any('_needs_stub' in arg for arg in error.args):
97  "We expect a missing `_needs_stub` attribute from older versions of "
98  "setuptools. Consider upgrading setuptools.")
99 
100 
101 _ERROR_DIAGNOSES = {
102  errors.CompileError: diagnose_compile_error,
103  AttributeError: diagnose_attribute_error,
104 }
105 
106 
107 def diagnose_build_ext_error(build_ext, error, formatted):
108  diagnostic = _ERROR_DIAGNOSES.get(type(error))
109  if diagnostic is None:
110  raise commands.CommandError(
111  "\n\nWe could not diagnose your build failure. If you are unable to "
112  "proceed, please file an issue at http://www.github.com/grpc/grpc "
113  "with `[Python install]` in the title; please attach the whole log "
114  "(including everything that may have appeared above the Python "
115  "backtrace).\n\n{}".format(formatted))
116  else:
117  diagnostic(build_ext, error)
http2_test_server.format
format
Definition: http2_test_server.py:118
support.diagnose_build_ext_error
def diagnose_build_ext_error(build_ext, error, formatted)
Definition: support.py:107
support._expect_compile
def _expect_compile(compiler, source_string, error_message)
Definition: support.py:65
commands.CommandError
Definition: commands.py:40
support.diagnose_compile_error
def diagnose_compile_error(build_ext, error)
Definition: support.py:73
support._compile
def _compile(compiler, source_string)
Definition: support.py:52
open
#define open
Definition: test-fs.c:46
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
support.diagnose_attribute_error
def diagnose_attribute_error(build_ext, error)
Definition: support.py:94


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:28