class RubyPython::Interpreter

An instance of this class represents information about a particular Python interpreter.

This class represents the current Python interpreter. A class that represents a Python executable.

End users may get the instance that represents the current running Python interpreter (from RubyPython.python), but should not directly instantiate this class.

Attributes

library[R]

The Python library.

python[R]

The name of the Python executable that is used. This is the value of ‘sys.executable’ for the Python interpreter provided in :python_exe or ‘python’ if it is not provided.

On Mac OS X Lion (10.7), this value is ‘/usr/bin/python’ for ‘python’.

sys_prefix[R]

The system prefix for the Python interpreter. This is the value of ‘sys.prefix’.

version[R]

The version of the Python interpreter. This is a decimalized version of ‘sys.version_info’ (such that Python 2.7.1 is reported as ‘2.7’).

version_name[R]

The basename of the Python interpreter with a version number. This is mostly an intermediate value used to find the shared Python library, but /usr/bin/python is often a link to /usr/bin/python2.7 so it may be of value. Note that this does not include the full path to the interpreter.

Public Class Methods

new(options = {}) click to toggle source

Create a new instance of an Interpreter instance describing a particular Python executable and shared library.

Expects a hash that matches the configuration options provided to RubyPython.start; currently only one value is recognized in that hash:

  • :python_exe: Specifies the name of the python executable to run.

# File lib/rubypython/interpreter.rb, line 38
def initialize(options = {})
  @python_exe = options[:python_exe]
  # Windows: 'C:\\Python27\python.exe'
  # Mac OS X: '/usr/bin/

  # The default interpreter might be python3 on some systems
  rc, majorversion = runpy "import sys; print(sys.version_info[0])"
  if majorversion == "3"
    warn "The python interpreter is python 3, switching to python2"
    @python_exe = "python2"
  end

  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('.', '')
    basename      = File.basename(@python, '.exe')

    if basename =~ /(?:#{@version}|#{flat_version})$/
      @version_name = basename
    else
      @version_name = "#{basename}#{flat_version}"
    end
  else
    basename = File.basename(@python)
    if basename =~ /#{@version}/
      @version_name = basename
    elsif basename.end_with?("2")
      @version_name = "#{basename[0..-2]}#{@version}"
    else
      @version_name = "#{basename}#{@version}"
    end
  end

  @library = find_python_lib
end

Public Instance Methods

==(other) click to toggle source

Compare the current Interpreter to the provided Interpreter or configuration hash. A configuration hash will be converted to an Interpreter object before being compared. :python_exe basename is used. If comparing against another Interpreter object, the Interpreter basename and version are used.

# File lib/rubypython/interpreter.rb, line 23
def ==(other)
  other = self.class.new(other) if other.kind_of? Hash
  return false unless other.kind_of? self.class
  (self.version == other.version) && (self.version_name == other.version_name)
end
debug_s(format = nil) click to toggle source
# File lib/rubypython/interpreter.rb, line 221
  def debug_s(format = nil)
    system = ""
    system << "windows " if ::FFI::Platform.windows?
    system << "mac " if ::FFI::Platform.mac?
    system << "unix " if ::FFI::Platform.unix?
    system << "unknown " if system.empty?

    case format
    when :report
      s = <<-EOS
python_exe:   #{@python_exe}
python:       #{@python}
version:      #{@version}
sys_prefix:   #{@sys_prefix}
version_name: #{@version_name}
platform:     #{system.chomp}
library:      #{@library.inspect}
  libbase:    #{@libbase}
  libext:     #{@libext}
  libname:    #{@libname}
  locations:  #{@locations.inspect}
      EOS
    else
      s = "#<#{self.class}: "
      s << "python_exe=#{@python_exe.inspect} "
      s << "python=#{@python.inspect} "
      s << "version=#{@version.inspect} "
      s << "sys_prefix=#{@sys_prefix.inspect} "
      s << "version_name=#{@version_name.inspect} "
      s << system
      s << "library=#{@library.inspect} "
      s << "libbase=#{@libbase.inspect} "
      s << "libext=#{@libext.inspect} "
      s << "libname=#{@libname.inspect} "
      s << "locations=#{@locations.inspect}"
    end

    s
  end
inspect(debug = false) click to toggle source
# File lib/rubypython/interpreter.rb, line 211
def inspect(debug = false)
  if debug
    debug_s
  elsif @python
    "#<#{self.class}: #{python} v#{version} #{sys_prefix} #{version_name}>"
  else
    "#<#{self.class}: invalid interpreter>"
  end
end
valid?() click to toggle source
# File lib/rubypython/interpreter.rb, line 162
def valid?
  if @python.nil? or @python.empty?
    false
  elsif @library.nil? or @library.empty?
    false
  else
    true
  end
end