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
71
72
73
74
75
76
77
78
79
# 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/

  # 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

Instance Attribute Details

#libraryObject (readonly)

The Python library.



197
198
199
# File 'lib/rubypython/interpreter.rb', line 197

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



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

def python
  @python
end

#sys_prefixObject (readonly)

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



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

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



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

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.



194
195
196
# File 'lib/rubypython/interpreter.rb', line 194

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



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
252
253
254
255
256
257
258
259
260
# File 'lib/rubypython/interpreter.rb', line 222

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



212
213
214
215
216
217
218
219
220
# File 'lib/rubypython/interpreter.rb', line 212

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)


163
164
165
166
167
168
169
170
171
# File 'lib/rubypython/interpreter.rb', line 163

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