@@ -10,8 +10,6 @@ import Conda
1010
1111immutable UseCondaPython <: Exception end
1212
13- try # make sure deps.jl file is removed on error
14-
1513# ########################################################################
1614
1715# Fix the environment for running `python`, and setts IO encoding to UTF-8.
@@ -36,7 +34,7 @@ function pythonenv(cmd::Cmd)
3634 setenv (cmd, env)
3735end
3836
39- pyvar (python:: AbstractString , mod:: AbstractString , var:: AbstractString ) = chomp (readstring (pythonenv (` $python -c "import $mod ; print($mod .$var )"` )))
37+ pyvar (python:: AbstractString , mod:: AbstractString , var:: AbstractString ) = chomp (read (pythonenv (` $python -c "import $mod ; print($mod .$var )"` ), String ))
4038
4139pyconfigvar (python:: AbstractString , var:: AbstractString ) = pyvar (python, " distutils.sysconfig" , " get_config_var('$var ')" )
4240pyconfigvar (python, var, default) = let v = pyconfigvar (python, var)
@@ -113,7 +111,7 @@ function find_libpython(python::AbstractString)
113111 print (s, " \n\n " )
114112 end
115113 println (STDERR, " ---------------------------------- get_config_vars ---------------------------------------" )
116- print (STDERR, readstring (` python -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_vars())"` ))
114+ print (STDERR, read (` python -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_vars())"` , String ))
117115 println (STDERR, " --------------------------------- directory contents -------------------------------------" )
118116 for libpath in libpaths
119117 if isdir (libpath)
@@ -142,76 +140,6 @@ include("depsutils.jl")
142140
143141# ########################################################################
144142
145- const python = try
146- let py = get (ENV , " PYTHON" , isfile (" PYTHON" ) ? readchomp (" PYTHON" ) :
147- Compat. Sys. islinux () || Sys. ARCH ∉ (:i686 , :x86_64 ) ? " python" : " " ),
148- vers = isempty (py) ? v " 0.0" : convert (VersionNumber, pyconfigvar (py," VERSION" ," 0.0" ))
149- if vers < v " 2.7"
150- if isempty (py)
151- throw (UseCondaPython ())
152- else
153- error (" Python version $vers < 2.7 is not supported" )
154- end
155- end
156-
157- # check word size of Python via sys.maxsize, since a common error
158- # on Windows is to link a 64-bit Julia to a 32-bit Python.
159- pywordsize = parse (UInt64, pysys (py, " maxsize" )) > (UInt64 (1 )<< 32 ) ? 64 : 32
160- if pywordsize != Sys. WORD_SIZE
161- error (" $py is $(pywordsize) -bit, but Julia is $(Sys. WORD_SIZE) -bit" )
162- end
163-
164- py
165- end
166- catch e1
167- if Sys. ARCH in (:i686 , :x86_64 )
168- if isa (e1, UseCondaPython)
169- info (" Using the Python distribution in the Conda package by default.\n " ,
170- " To use a different Python version, set ENV[\" PYTHON\" ]=\" pythoncommand\" and re-run Pkg.build(\" PyCall\" )." )
171- else
172- info ( " No system-wide Python was found; got the following error:\n " ,
173- " $e1 \n using the Python distribution in the Conda package" )
174- end
175- abspath (Conda. PYTHONDIR, " python" * (Compat. Sys. iswindows () ? " .exe" : " " ))
176- else
177- error (" No system-wide Python was found; got the following error:\n " ,
178- " $e1 " )
179- end
180- end
181-
182- use_conda = dirname (python) == abspath (Conda. PYTHONDIR)
183- if use_conda
184- Conda. add (" numpy" )
185- end
186-
187- const (libpython, libpy_name) = find_libpython (python)
188- const programname = pysys (python, " executable" )
189-
190- # Get PYTHONHOME, either from the environment or from Python
191- # itself (if it is not in the environment or if we are using Conda)
192- PYTHONHOME = if ! haskey (ENV , " PYTHONHOME" ) || use_conda
193- # PYTHONHOME tells python where to look for both pure python
194- # and binary modules. When it is set, it replaces both
195- # `prefix` and `exec_prefix` and we thus need to set it to
196- # both in case they differ. This is also what the
197- # documentation recommends. However, they are documented
198- # to always be the same on Windows, where it causes
199- # problems if we try to include both.
200- exec_prefix = pysys (python, " exec_prefix" )
201- Compat. Sys. iswindows () ? exec_prefix : pysys (python, " prefix" ) * " :" * exec_prefix
202- else
203- ENV [" PYTHONHOME" ]
204- end
205-
206- # cache the Python version as a Julia VersionNumber
207- const pyversion = VersionNumber (pyvar (python, " platform" , " python_version()" ))
208-
209- info (" PyCall is using $python (Python $pyversion ) at $programname , libpython = $libpy_name " )
210-
211- if pyversion < v " 2.7"
212- error (" Python 2.7 or later is required for PyCall" )
213- end
214-
215143# A couple of key strings need to be stored as constants so that
216144# they persist throughout the life of the program. In Python 3,
217145# they need to be wchar_t* data.
@@ -221,15 +149,86 @@ wstringconst(s) = string("Base.cconvert(Cwstring, \"", escape_string(s), "\")")
221149# to prevent unnecessary recompilation and to minimize
222150# problems in the unlikely event of read-only directories.
223151function writeifchanged (filename, str)
224- if ! isfile (filename) || readstring (filename) != str
152+ if ! isfile (filename) || read (filename, String ) != str
225153 info (abspath (filename), " has been updated" )
226154 write (filename, str)
227155 else
228156 info (abspath (filename), " has not changed" )
229157 end
230158end
231159
232- writeifchanged (" deps.jl" , """
160+ try # make sure deps.jl file is removed on error
161+ python = try
162+ let py = get (ENV , " PYTHON" , isfile (" PYTHON" ) ? readchomp (" PYTHON" ) :
163+ Compat. Sys. islinux () || Sys. ARCH ∉ (:i686 , :x86_64 ) ? " python" : " " ),
164+ vers = isempty (py) ? v " 0.0" : convert (VersionNumber, pyconfigvar (py," VERSION" ," 0.0" ))
165+ if vers < v " 2.7"
166+ if isempty (py)
167+ throw (UseCondaPython ())
168+ else
169+ error (" Python version $vers < 2.7 is not supported" )
170+ end
171+ end
172+
173+ # check word size of Python via sys.maxsize, since a common error
174+ # on Windows is to link a 64-bit Julia to a 32-bit Python.
175+ pywordsize = parse (UInt64, pysys (py, " maxsize" )) > (UInt64 (1 )<< 32 ) ? 64 : 32
176+ if pywordsize != Sys. WORD_SIZE
177+ error (" $py is $(pywordsize) -bit, but Julia is $(Sys. WORD_SIZE) -bit" )
178+ end
179+
180+ py
181+ end
182+ catch e1
183+ if Sys. ARCH in (:i686 , :x86_64 )
184+ if isa (e1, UseCondaPython)
185+ info (" Using the Python distribution in the Conda package by default.\n " ,
186+ " To use a different Python version, set ENV[\" PYTHON\" ]=\" pythoncommand\" and re-run Pkg.build(\" PyCall\" )." )
187+ else
188+ info ( " No system-wide Python was found; got the following error:\n " ,
189+ " $e1 \n using the Python distribution in the Conda package" )
190+ end
191+ abspath (Conda. PYTHONDIR, " python" * (Compat. Sys. iswindows () ? " .exe" : " " ))
192+ else
193+ error (" No system-wide Python was found; got the following error:\n " ,
194+ " $e1 " )
195+ end
196+ end
197+
198+ use_conda = dirname (python) == abspath (Conda. PYTHONDIR)
199+ if use_conda
200+ Conda. add (" numpy" )
201+ end
202+
203+ (libpython, libpy_name) = find_libpython (python)
204+ programname = pysys (python, " executable" )
205+
206+ # Get PYTHONHOME, either from the environment or from Python
207+ # itself (if it is not in the environment or if we are using Conda)
208+ PYTHONHOME = if ! haskey (ENV , " PYTHONHOME" ) || use_conda
209+ # PYTHONHOME tells python where to look for both pure python
210+ # and binary modules. When it is set, it replaces both
211+ # `prefix` and `exec_prefix` and we thus need to set it to
212+ # both in case they differ. This is also what the
213+ # documentation recommends. However, they are documented
214+ # to always be the same on Windows, where it causes
215+ # problems if we try to include both.
216+ exec_prefix = pysys (python, " exec_prefix" )
217+ Compat. Sys. iswindows () ? exec_prefix : pysys (python, " prefix" ) * " :" * exec_prefix
218+ else
219+ ENV [" PYTHONHOME" ]
220+ end
221+
222+ # cache the Python version as a Julia VersionNumber
223+ pyversion = VersionNumber (pyvar (python, " platform" , " python_version()" ))
224+
225+ info (" PyCall is using $python (Python $pyversion ) at $programname , libpython = $libpy_name " )
226+
227+ if pyversion < v " 2.7"
228+ error (" Python 2.7 or later is required for PyCall" )
229+ end
230+
231+ writeifchanged (" deps.jl" , """
233232 const python = "$(escape_string (python)) "
234233 const libpython = "$(escape_string (libpy_name)) "
235234 const pyprogramname = "$(escape_string (programname)) "
@@ -242,16 +241,16 @@ writeifchanged("deps.jl", """
242241 const conda = $use_conda
243242 """ )
244243
245- # Make subsequent builds (e.g. Pkg.update) use the same Python by default:
246- writeifchanged (" PYTHON" , isfile (programname) ? programname : python)
244+ # Make subsequent builds (e.g. Pkg.update) use the same Python by default:
245+ writeifchanged (" PYTHON" , isfile (programname) ? programname : python)
247246
248- # ########################################################################
247+ # ########################################################################
249248
250249catch
251250
252- # remove deps.jl (if it exists) on an error, so that PyCall will
253- # not load until it is properly configured.
254- isfile (" deps.jl" ) && rm (" deps.jl" )
255- rethrow ()
251+ # remove deps.jl (if it exists) on an error, so that PyCall will
252+ # not load until it is properly configured.
253+ isfile (" deps.jl" ) && rm (" deps.jl" )
254+ rethrow ()
256255
257256end
0 commit comments