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.



188
189
190
# File 'lib/rubypython/interpreter.rb', line 188

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



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

def python
  @python
end

#sys_prefixObject (readonly)

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



178
179
180
# File 'lib/rubypython/interpreter.rb', line 178

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



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

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.



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

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



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
248
249
250
251
# File 'lib/rubypython/interpreter.rb', line 213

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



203
204
205
206
207
208
209
210
211
# File 'lib/rubypython/interpreter.rb', line 203

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)


154
155
156
157
158
159
160
161
162
# File 'lib/rubypython/interpreter.rb', line 154

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