Class: RubyPython::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypython/interpreter.rb,
lib/rubypython/python.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Interpreter

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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubypython/interpreter.rb', line 39

def initialize(options = {})
  @python_exe = options[:python_exe]
  # Windows: 'C:\\Python27\python.exe'
  # Mac OS X: '/usr/bin/
  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
    else
      @version_name = "#{basename}#{@version}"
    end
  end

  @library = find_python_lib
end

Instance Attribute Details

#libraryObject (readonly)

The Python library.



184
185
186
# File 'lib/rubypython/interpreter.rb', line 184

def library
  @library
end

#pythonObject (readonly)

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’.



166
167
168
# File 'lib/rubypython/interpreter.rb', line 166

def python
  @python
end

#sys_prefixObject (readonly)

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



174
175
176
# File 'lib/rubypython/interpreter.rb', line 174

def sys_prefix
  @sys_prefix
end

#versionObject (readonly)

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’).



170
171
172
# File 'lib/rubypython/interpreter.rb', line 170

def version
  @version
end

#version_nameObject (readonly)

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.



181
182
183
# File 'lib/rubypython/interpreter.rb', line 181

def version_name
  @version_name
end

Instance Method Details

#==(other) ⇒ Object

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.



24
25
26
27
28
# File 'lib/rubypython/interpreter.rb', line 24

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) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rubypython/interpreter.rb', line 209

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) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/rubypython/interpreter.rb', line 199

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?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
# File 'lib/rubypython/interpreter.rb', line 150

def valid?
  if @python.nil? or @python.empty?
    false
  elsif @library.nil? or @library.empty?
    false
  else
    true
  end
end