Commits

Austin Ziegler committed 3ea8945

Made RubyPython.start work better on failure.

Fixed Ruby 1.8 compatibility.

Comments (0)

Files changed (2)

lib/rubypython.rb

     def start(options = {})
       Mutex.new.synchronize do
         # Has the Runtime interpreter been defined?
-        if self.const_defined?(:Runtime, false)
+        if self.const_defined?(:Runtime)
           # If this constant is defined, then yes it is. Since it is, let's
           # see if we should print a warning to the user.
           unless Runtime == options
             warn "The Python interpreter has already been loaded from #{Runtime.python} and cannot be changed in this process. Continuing with the current runtime."
           end
         else
-          self.const_set(:Runtime, RubyPython::Interpreter.new(options))
+          interp = RubyPython::Interpreter.new(options)
+          if interp.valid?
+            self.const_set(:Runtime, interp)
+          else
+            raise RubyPython::InvalidInterpreter, "An invalid interpreter was specified."
+          end
         end
 
         unless defined? RubyPython::Python.ffi_libraries

lib/rubypython/interpreter.rb

 # -*- ruby encoding: utf-8 -*-
 
+class RubyPython::InvalidInterpreter < Exception
+end
+
 ##
 # An instance of this class represents information about a particular
 # \Python interpreter.
 # interpreter (from +RubyPython.python+), but should not directly
 # instantiate this class.
 class RubyPython::Interpreter
+
   ##
   # Compare the current Interpreter to the provided Interpreter or
   # configuration hash. A configuration hash will be converted to an
     @python_exe = options[:python_exe]
     # Windows: 'C:\\Python27\python.exe'
     # Mac OS X: '/usr/bin/
-    @python     = runpy "import sys; print sys.executable"
-    @version    = runpy "import sys; print '%d.%d' % sys.version_info[:2]"
-    @sys_prefix = runpy "import sys; print sys.prefix"
+    rc, @python     = runpy "import sys; print sys.executable"
+    if rc.exitstatus.nonzero?
+      raise RubyPython::InvalidInterpreter, "An invalid interpreter was specified."
+    end
+    rc, @version    = runpy "import sys; print '%d.%d' % sys.version_info[:2]"
+    rc, @sys_prefix = runpy "import sys; print sys.prefix"
 
     if FFI::Platform.windows?
       flat_version  = @version.tr('.', '')
 
   # Run a Python command-line command.
   def runpy(command)
-    %x(#{@python || @python_exe || 'python'} -c "#{command}").chomp
+    i = @python || @python_exe || 'python'
+    if FFI::Platform.windows?
+      o = %x(#{i} -c "#{command}" 2> NUL:)
+    else
+      o = %x(#{i} -c "#{command}" 2> /dev/null)
+    end
+
+    [ $?, o.chomp ]
   end
   private :runpy