pry 0.9.12.2 → 0.14.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (237) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1141 -0
  3. data/LICENSE +2 -2
  4. data/README.md +466 -0
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +97 -92
  10. data/lib/pry/code/code_file.rb +114 -0
  11. data/lib/pry/code/code_range.rb +7 -4
  12. data/lib/pry/code/loc.rb +27 -14
  13. data/lib/pry/code.rb +62 -90
  14. data/lib/pry/code_object.rb +83 -39
  15. data/lib/pry/color_printer.rb +66 -0
  16. data/lib/pry/command.rb +202 -371
  17. data/lib/pry/command_set.rb +151 -133
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +85 -73
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -63
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +64 -47
  27. data/lib/pry/commands/cd.rb +42 -26
  28. data/lib/pry/commands/change_inspector.rb +34 -0
  29. data/lib/pry/commands/change_prompt.rb +51 -0
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +33 -24
  36. data/lib/pry/commands/edit.rb +183 -167
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -17
  40. data/lib/pry/commands/find_method.rb +167 -167
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +153 -132
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +42 -0
  47. data/lib/pry/commands/ls/constants.rb +75 -0
  48. data/lib/pry/commands/ls/formatter.rb +55 -0
  49. data/lib/pry/commands/ls/globals.rb +50 -0
  50. data/lib/pry/commands/ls/grep.rb +23 -0
  51. data/lib/pry/commands/ls/instance_vars.rb +40 -0
  52. data/lib/pry/commands/ls/interrogatable.rb +24 -0
  53. data/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  54. data/lib/pry/commands/ls/local_names.rb +37 -0
  55. data/lib/pry/commands/ls/local_vars.rb +47 -0
  56. data/lib/pry/commands/ls/ls_entity.rb +65 -0
  57. data/lib/pry/commands/ls/methods.rb +55 -0
  58. data/lib/pry/commands/ls/methods_helper.rb +50 -0
  59. data/lib/pry/commands/ls/self_methods.rb +34 -0
  60. data/lib/pry/commands/ls.rb +100 -303
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +93 -49
  63. data/lib/pry/commands/pry_backtrace.rb +22 -18
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +57 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -38
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +66 -34
  71. data/lib/pry/commands/shell_mode.rb +22 -20
  72. data/lib/pry/commands/show_doc.rb +80 -65
  73. data/lib/pry/commands/show_info.rb +193 -159
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +113 -33
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +21 -13
  79. data/lib/pry/commands/watch_expression/expression.rb +43 -0
  80. data/lib/pry/commands/watch_expression.rb +110 -0
  81. data/lib/pry/commands/whereami.rb +157 -134
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +290 -220
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +50 -27
  90. data/lib/pry/editor.rb +130 -102
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +73 -0
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +22 -151
  96. data/lib/pry/helpers/command_helpers.rb +55 -63
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +55 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -86
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +101 -70
  104. data/lib/pry/hooks.rb +67 -137
  105. data/lib/pry/indent.rb +79 -73
  106. data/lib/pry/input_completer.rb +283 -0
  107. data/lib/pry/input_lock.rb +129 -0
  108. data/lib/pry/inspector.rb +39 -0
  109. data/lib/pry/last_exception.rb +61 -0
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +51 -42
  112. data/lib/pry/method/weird_method_locator.rb +80 -44
  113. data/lib/pry/method.rb +225 -176
  114. data/lib/pry/object_path.rb +91 -0
  115. data/lib/pry/output.rb +136 -0
  116. data/lib/pry/pager.rb +227 -68
  117. data/lib/pry/prompt.rb +214 -0
  118. data/lib/pry/pry_class.rb +216 -289
  119. data/lib/pry/pry_instance.rb +438 -500
  120. data/lib/pry/repl.rb +256 -0
  121. data/lib/pry/repl_file_loader.rb +34 -35
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/LICENSE +20 -0
  124. data/lib/pry/slop/commands.rb +190 -0
  125. data/lib/pry/slop/option.rb +210 -0
  126. data/lib/pry/slop.rb +672 -0
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable/evalable.rb +24 -0
  130. data/lib/pry/testable/mockable.rb +22 -0
  131. data/lib/pry/testable/pry_tester.rb +88 -0
  132. data/lib/pry/testable/utility.rb +34 -0
  133. data/lib/pry/testable/variables.rb +52 -0
  134. data/lib/pry/testable.rb +68 -0
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +20 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +36 -43
  138. data/lib/pry/wrapped_module.rb +102 -103
  139. data/lib/pry.rb +135 -261
  140. metadata +94 -283
  141. data/.document +0 -2
  142. data/.gitignore +0 -16
  143. data/.travis.yml +0 -21
  144. data/.yardopts +0 -1
  145. data/CHANGELOG +0 -534
  146. data/CONTRIBUTORS +0 -55
  147. data/Gemfile +0 -9
  148. data/Guardfile +0 -62
  149. data/README.markdown +0 -400
  150. data/Rakefile +0 -140
  151. data/TODO +0 -117
  152. data/lib/pry/commands/disabled_commands.rb +0 -2
  153. data/lib/pry/commands/gem_cd.rb +0 -26
  154. data/lib/pry/commands/gem_install.rb +0 -29
  155. data/lib/pry/commands/gem_list.rb +0 -33
  156. data/lib/pry/commands/gem_open.rb +0 -29
  157. data/lib/pry/commands/gist.rb +0 -102
  158. data/lib/pry/commands/install_command.rb +0 -51
  159. data/lib/pry/commands/simple_prompt.rb +0 -22
  160. data/lib/pry/commands.rb +0 -6
  161. data/lib/pry/completion.rb +0 -304
  162. data/lib/pry/custom_completions.rb +0 -6
  163. data/lib/pry/history_array.rb +0 -116
  164. data/lib/pry/plugins.rb +0 -103
  165. data/lib/pry/rbx_method.rb +0 -13
  166. data/lib/pry/rbx_path.rb +0 -22
  167. data/lib/pry/rubygem.rb +0 -74
  168. data/lib/pry/terminal.rb +0 -78
  169. data/lib/pry/test/helper.rb +0 -185
  170. data/man/pry.1 +0 -195
  171. data/man/pry.1.html +0 -204
  172. data/man/pry.1.ronn +0 -141
  173. data/pry.gemspec +0 -30
  174. data/spec/Procfile +0 -3
  175. data/spec/cli_spec.rb +0 -78
  176. data/spec/code_object_spec.rb +0 -277
  177. data/spec/code_spec.rb +0 -219
  178. data/spec/command_helpers_spec.rb +0 -29
  179. data/spec/command_integration_spec.rb +0 -644
  180. data/spec/command_set_spec.rb +0 -627
  181. data/spec/command_spec.rb +0 -821
  182. data/spec/commands/amend_line_spec.rb +0 -247
  183. data/spec/commands/bang_spec.rb +0 -19
  184. data/spec/commands/cat_spec.rb +0 -164
  185. data/spec/commands/cd_spec.rb +0 -250
  186. data/spec/commands/disable_pry_spec.rb +0 -25
  187. data/spec/commands/edit_spec.rb +0 -727
  188. data/spec/commands/exit_all_spec.rb +0 -34
  189. data/spec/commands/exit_program_spec.rb +0 -19
  190. data/spec/commands/exit_spec.rb +0 -34
  191. data/spec/commands/find_method_spec.rb +0 -70
  192. data/spec/commands/gem_list_spec.rb +0 -26
  193. data/spec/commands/gist_spec.rb +0 -79
  194. data/spec/commands/help_spec.rb +0 -56
  195. data/spec/commands/hist_spec.rb +0 -181
  196. data/spec/commands/jump_to_spec.rb +0 -15
  197. data/spec/commands/ls_spec.rb +0 -181
  198. data/spec/commands/play_spec.rb +0 -140
  199. data/spec/commands/raise_up_spec.rb +0 -56
  200. data/spec/commands/save_file_spec.rb +0 -177
  201. data/spec/commands/show_doc_spec.rb +0 -510
  202. data/spec/commands/show_input_spec.rb +0 -17
  203. data/spec/commands/show_source_spec.rb +0 -782
  204. data/spec/commands/whereami_spec.rb +0 -203
  205. data/spec/completion_spec.rb +0 -239
  206. data/spec/control_d_handler_spec.rb +0 -58
  207. data/spec/documentation_helper_spec.rb +0 -73
  208. data/spec/editor_spec.rb +0 -79
  209. data/spec/exception_whitelist_spec.rb +0 -21
  210. data/spec/fixtures/candidate_helper1.rb +0 -11
  211. data/spec/fixtures/candidate_helper2.rb +0 -8
  212. data/spec/fixtures/example.erb +0 -5
  213. data/spec/fixtures/example_nesting.rb +0 -33
  214. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  215. data/spec/fixtures/testrc +0 -2
  216. data/spec/fixtures/testrcbad +0 -2
  217. data/spec/fixtures/whereami_helper.rb +0 -6
  218. data/spec/helper.rb +0 -34
  219. data/spec/helpers/bacon.rb +0 -86
  220. data/spec/helpers/mock_pry.rb +0 -43
  221. data/spec/helpers/table_spec.rb +0 -105
  222. data/spec/history_array_spec.rb +0 -67
  223. data/spec/hooks_spec.rb +0 -522
  224. data/spec/indent_spec.rb +0 -301
  225. data/spec/input_stack_spec.rb +0 -90
  226. data/spec/method_spec.rb +0 -482
  227. data/spec/prompt_spec.rb +0 -60
  228. data/spec/pry_defaults_spec.rb +0 -419
  229. data/spec/pry_history_spec.rb +0 -99
  230. data/spec/pry_output_spec.rb +0 -95
  231. data/spec/pry_spec.rb +0 -504
  232. data/spec/run_command_spec.rb +0 -25
  233. data/spec/sticky_locals_spec.rb +0 -157
  234. data/spec/syntax_checking_spec.rb +0 -81
  235. data/spec/wrapped_module_spec.rb +0 -261
  236. data/wiki/Customizing-pry.md +0 -397
  237. data/wiki/Home.md +0 -4
data/lib/pry/pry_class.rb CHANGED
@@ -1,93 +1,100 @@
1
- require 'ostruct'
2
- require 'forwardable'
3
- require 'pry/config'
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+ require 'pathname'
4
5
 
5
6
  class Pry
7
+ LOCAL_RC_FILE = "./.pryrc".freeze
6
8
 
7
- # The RC Files to load.
8
- HOME_RC_FILE = "~/.pryrc"
9
- LOCAL_RC_FILE = "./.pryrc"
9
+ # @return [Boolean] true if this Ruby supports safe levels and tainting,
10
+ # to guard against using deprecated or unsupported features
11
+ HAS_SAFE_LEVEL = (
12
+ RUBY_ENGINE == 'ruby' &&
13
+ Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7')
14
+ )
10
15
 
11
- # @return [Hash] Pry's `Thread.current` hash
12
- def self.current
13
- Thread.current[:__pry__] ||= {}
14
- end
15
-
16
- # class accessors
17
16
  class << self
18
- extend Forwardable
19
-
20
- # convenience method
21
- def self.delegate_accessors(delagatee, *names)
22
- def_delegators delagatee, *names
23
- def_delegators delagatee, *names.map { |v| "#{v}=" }
24
- end
25
-
26
- # Get/Set the Proc that defines extra Readline completions (on top
27
- # of the ones defined for IRB).
28
- # @return [Proc] The Proc that defines extra Readline completions (on top
29
- # @example Add file names to completion list
30
- # Pry.custom_completions = proc { Dir.entries('.') }
17
+ extend Pry::Forwardable
31
18
  attr_accessor :custom_completions
32
-
33
- # @return [Fixnum] The current input line.
34
19
  attr_accessor :current_line
35
-
36
- # @return [Array] The Array of evaluated expressions.
37
20
  attr_accessor :line_buffer
38
-
39
- # @return [String] The __FILE__ for the `eval()`. Should be "(pry)"
40
- # by default.
41
21
  attr_accessor :eval_path
42
-
43
- # @return [OpenStruct] Return Pry's config object.
44
- attr_accessor :config
45
-
46
- # @return [History] Return Pry's line history object.
47
- attr_accessor :history
48
-
49
- # @return [Boolean] Whether Pry was activated from the command line.
50
22
  attr_accessor :cli
51
-
52
- # @return [Boolean] Whether Pry sessions are quiet by default.
53
23
  attr_accessor :quiet
54
-
55
- # @return [Binding] A top level binding with no local variables
56
- attr_accessor :toplevel_binding
57
-
58
- # @return [Exception, nil] The last pry internal error.
59
- # (a CommandError in most cases)
60
24
  attr_accessor :last_internal_error
25
+ attr_accessor :config
61
26
 
62
- # plugin forwardables
63
- def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
27
+ def_delegators(
28
+ :@config, :input, :input=, :output, :output=, :commands,
29
+ :commands=, :print, :print=, :exception_handler, :exception_handler=,
30
+ :hooks, :hooks=, :color, :color=, :pager, :pager=, :editor, :editor=,
31
+ :memory_size, :memory_size=, :extra_sticky_locals, :extra_sticky_locals=,
32
+ :prompt, :prompt=, :history, :history=
33
+ )
34
+
35
+ #
36
+ # @example
37
+ # Pry.configure do |config|
38
+ # config.eager_load! # optional
39
+ # config.input = # ..
40
+ # config.foo = 2
41
+ # end
42
+ #
43
+ # @yield [config]
44
+ # Yields a block with {Pry.config} as its argument.
45
+ #
46
+ def configure
47
+ yield config
48
+ end
49
+ end
50
+
51
+ #
52
+ # @return [main]
53
+ # returns the special instance of Object, "main".
54
+ #
55
+ def self.main
56
+ @main ||= TOPLEVEL_BINDING.eval "self"
57
+ end
64
58
 
65
- delegate_accessors :@config, :input, :output, :commands, :prompt, :print, :exception_handler,
66
- :hooks, :color, :pager, :editor, :memory_size, :input_stack, :extra_sticky_locals
59
+ #
60
+ # @return [Pry::Config]
61
+ # Returns a value store for an instance of Pry running on the current thread.
62
+ #
63
+ def self.current
64
+ Thread.current[:__pry__] ||= {}
67
65
  end
68
66
 
69
67
  # Load the given file in the context of `Pry.toplevel_binding`
70
- # @param [String] file_name The unexpanded file path.
71
- def self.load_file_at_toplevel(file_name)
72
- full_name = File.expand_path(file_name)
73
- begin
74
- toplevel_binding.eval(File.read(full_name), full_name) if File.exists?(full_name)
75
- rescue RescuableException => e
76
- puts "Error loading #{file_name}: #{e}\n#{e.backtrace.first}"
77
- end
68
+ # @param [String] file The unexpanded file path.
69
+ def self.load_file_at_toplevel(file)
70
+ toplevel_binding.eval(File.read(file), file)
71
+ rescue RescuableException => e
72
+ puts "Error loading #{file}: #{e}\n#{e.backtrace.first}"
78
73
  end
79
74
 
80
- # Load the rc files given in the `Pry::RC_FILES` array.
81
- # This method can also be used to reload the files if they have changed.
82
- def self.load_rc
83
- load_file_at_toplevel(HOME_RC_FILE)
75
+ # Load RC files if appropriate This method can also be used to reload the
76
+ # files if they have changed.
77
+ def self.load_rc_files
78
+ rc_files_to_load.each do |file|
79
+ critical_section do
80
+ load_file_at_toplevel(file)
81
+ end
82
+ end
84
83
  end
85
84
 
86
85
  # Load the local RC file (./.pryrc)
87
- def self.load_local_rc
88
- unless File.expand_path(HOME_RC_FILE) == File.expand_path(LOCAL_RC_FILE)
89
- load_file_at_toplevel(LOCAL_RC_FILE)
90
- end
86
+ def self.rc_files_to_load
87
+ files = []
88
+ files << Pry.config.rc_file if Pry.config.rc_file && Pry.config.should_load_rc
89
+ files << LOCAL_RC_FILE if Pry.config.should_load_local_rc
90
+ files.map { |file| real_path_to(file) }.compact.uniq
91
+ end
92
+
93
+ # Expand a file to its canonical name (following symlinks as appropriate)
94
+ def self.real_path_to(file)
95
+ Pathname.new(File.expand_path(file)).realpath.to_s
96
+ rescue Errno::ENOENT, Errno::EACCES
97
+ nil
91
98
  end
92
99
 
93
100
  # Load any Ruby files specified with the -r flag on the command line.
@@ -100,74 +107,91 @@ class Pry
100
107
  # Trap interrupts on jruby, and make them behave like MRI so we can
101
108
  # catch them.
102
109
  def self.load_traps
103
- trap('INT'){ raise Interrupt }
110
+ trap('INT') { raise Interrupt }
104
111
  end
105
112
 
106
- # Do basic setup for initial session.
107
- # Including: loading .pryrc, loading plugins, loading requires, and
108
- # loading history.
113
+ def self.load_win32console
114
+ require 'win32console'
115
+ # The mswin and mingw versions of pry require win32console, so this should
116
+ # only fail on jruby (where win32console doesn't work).
117
+ # Instead we'll recommend ansicon, which does.
118
+ rescue LoadError
119
+ warn <<-WARNING if Pry.config.windows_console_warning
120
+ For a better Pry experience on Windows, please use ansicon:
121
+ https://github.com/adoxa/ansicon
122
+ If you use an alternative to ansicon and don't want to see this warning again,
123
+ you can add "Pry.config.windows_console_warning = false" to your pryrc.
124
+ WARNING
125
+ end
126
+
127
+ # Do basic setup for initial session including: loading pryrc, plugins,
128
+ # requires, and history.
109
129
  def self.initial_session_setup
130
+ return unless initial_session?
110
131
 
111
- return if !initial_session?
132
+ @initial_session = false
112
133
 
113
- # note these have to be loaded here rather than in pry_instance as
134
+ # note these have to be loaded here rather than in _pry_ as
114
135
  # we only want them loaded once per entire Pry lifetime.
115
- load_rc if Pry.config.should_load_rc
116
- load_local_rc if Pry.config.should_load_local_rc
117
- load_plugins if Pry.config.should_load_plugins
136
+ load_rc_files
137
+ end
138
+
139
+ def self.final_session_setup
140
+ return if @session_finalized
141
+
142
+ @session_finalized = true
118
143
  load_requires if Pry.config.should_load_requires
119
- load_history if Pry.config.history.should_load
144
+ load_history if Pry.config.history_load
120
145
  load_traps if Pry.config.should_trap_interrupts
121
-
122
- @initial_session = false
146
+ load_win32console if Helpers::Platform.windows? && !Helpers::Platform.windows_ansi?
123
147
  end
124
148
 
125
149
  # Start a Pry REPL.
126
- # This method also loads the ~/.pryrc and ./.pryrc as necessary
127
- # first time it is invoked.
150
+ # This method also loads `pryrc` as necessary the first time it is invoked.
128
151
  # @param [Object, Binding] target The receiver of the Pry session
129
152
  # @param [Hash] options
130
153
  # @option options (see Pry#initialize)
131
154
  # @example
132
155
  # Pry.start(Object.new, :input => MyInput.new)
133
- def self.start(target=nil, options={})
134
- return if ENV['DISABLE_PRY']
156
+ def self.start(target = nil, options = {})
157
+ return if Pry::Env['DISABLE_PRY']
158
+ if Pry::Env['FAIL_PRY']
159
+ raise 'You have FAIL_PRY set to true, which results in Pry calls failing'
160
+ end
161
+
162
+ options = options.to_hash
135
163
 
136
164
  if in_critical_section?
137
165
  output.puts "ERROR: Pry started inside Pry."
138
- output.puts "This can happen if you have a binding.pry inside a #to_s or #inspect function."
166
+ output.puts "This can happen if you have a binding.pry inside a #to_s " \
167
+ "or #inspect function."
139
168
  return
140
169
  end
141
170
 
142
- target = Pry.binding_for(target || toplevel_binding)
143
- initial_session_setup
144
-
145
- # create the Pry instance to manage the session
146
- pry_instance = new(options)
147
-
148
- # save backtrace
149
- pry_instance.backtrace = caller
171
+ unless mutex_available?
172
+ output.puts "ERROR: Unable to obtain mutex lock."
173
+ output.puts "This can happen if binding.pry is called from a signal handler"
174
+ return
175
+ end
150
176
 
151
- # if Pry was started via binding.pry, elide that from the backtrace.
152
- pry_instance.backtrace.shift if pry_instance.backtrace.first =~ /pry.*core_extensions.*pry/
177
+ options[:target] = Pry.binding_for(target || toplevel_binding)
178
+ initial_session_setup
179
+ final_session_setup
153
180
 
154
- # yield the binding_stack to the hook for modification
155
- pry_instance.exec_hook(:when_started, target, options, pry_instance)
181
+ # Unless we were given a backtrace, save the current one
182
+ if options[:backtrace].nil?
183
+ options[:backtrace] = caller
156
184
 
157
- if !pry_instance.binding_stack.empty?
158
- head = pry_instance.binding_stack.pop
159
- else
160
- head = target
185
+ # If Pry was started via `binding.pry`, elide that from the backtrace
186
+ if options[:backtrace].first =~ /pry.*core_extensions.*pry/
187
+ options[:backtrace].shift
188
+ end
161
189
  end
162
190
 
163
- # Clear the line before starting Pry. This fixes the issue discussed here:
164
- # https://github.com/pry/pry/issues/566
165
- if Pry.config.auto_indent
166
- Kernel.print Pry::Helpers::BaseHelpers.windows_ansi? ? "\e[0F" : "\e[0G"
167
- end
191
+ driver = options[:driver] || Pry::REPL
168
192
 
169
193
  # Enter the matrix
170
- pry_instance.repl(head)
194
+ driver.start(options)
171
195
  rescue Pry::TooSafeException
172
196
  puts "ERROR: Pry cannot work with $SAFE > 0"
173
197
  raise
@@ -176,27 +200,49 @@ class Pry
176
200
  # Execute the file through the REPL loop, non-interactively.
177
201
  # @param [String] file_name File name to load through the REPL.
178
202
  def self.load_file_through_repl(file_name)
179
- require "pry/repl_file_loader"
180
203
  REPLFileLoader.new(file_name).load
181
204
  end
182
205
 
206
+ #
183
207
  # An inspector that clips the output to `max_length` chars.
184
208
  # In case of > `max_length` chars the `#<Object...> notation is used.
185
- # @param obj The object to view.
186
- # @param max_length The maximum number of chars before clipping occurs.
187
- # @return [String] The string representation of `obj`.
188
- def self.view_clip(obj, max_length = 60)
189
- if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length
209
+ #
210
+ # @param [Object] obj
211
+ # The object to view.
212
+ #
213
+ # @param [Hash] options
214
+ # @option options [Integer] :max_length (60)
215
+ # The maximum number of chars before clipping occurs.
216
+ #
217
+ # @option options [Boolean] :id (false)
218
+ # Boolean to indicate whether or not a hex reprsentation of the object ID
219
+ # is attached to the return value when the length of inspect is greater than
220
+ # value of `:max_length`.
221
+ #
222
+ # @return [String]
223
+ # The string representation of `obj`.
224
+ #
225
+ def self.view_clip(obj, options = {})
226
+ max = options.fetch :max_length, 60
227
+ id = options.fetch :id, false
228
+ if obj.is_a?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max
190
229
  obj.name.to_s
191
- elsif TOPLEVEL_BINDING.eval('self') == obj
192
- # special case for 'main' object :)
230
+ elsif Pry.main == obj
231
+ # Special-case to support jruby. Fixed as of:
232
+ # https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039
233
+ # we can drop in the future.
193
234
  obj.to_s
194
- elsif [String, Numeric, Symbol, nil, true, false].any? { |v| v === obj } && obj.inspect.length <= max_length
235
+ # rubocop:disable Style/CaseEquality
236
+ elsif Pry.config.prompt_safe_contexts.any? { |v| v === obj } &&
237
+ obj.inspect.length <= max
238
+ # rubocop:enable Style/CaseEquality
239
+
195
240
  obj.inspect
241
+ elsif id
242
+ format("#<#{obj.class}:0x%<id>x>", id: obj.object_id << 1)
196
243
  else
197
- "#<#{obj.class}>"#:%x>"# % (obj.object_id << 1)
244
+ "#<#{obj.class}>"
198
245
  end
199
-
200
246
  rescue RescuableException
201
247
  "unknown"
202
248
  end
@@ -206,11 +252,6 @@ class Pry
206
252
  Pry.history.load
207
253
  end
208
254
 
209
- # Save new lines of Readline history if required.
210
- def self.save_history
211
- Pry.history.save
212
- end
213
-
214
255
  # @return [Boolean] Whether this is the first time a Pry session has
215
256
  # been started since loading the Pry class.
216
257
  def self.initial_session?
@@ -218,206 +259,84 @@ class Pry
218
259
  end
219
260
 
220
261
  # Run a Pry command from outside a session. The commands available are
221
- # those referenced by `Pry.commands` (the default command set).
262
+ # those referenced by `Pry.config.commands` (the default command set).
222
263
  # @param [String] command_string The Pry command (including arguments,
223
264
  # if any).
224
265
  # @param [Hash] options Optional named parameters.
225
- # @return [Object] The return value of the Pry command.
226
- # @option options [Object, Binding] :context The object context to run the
266
+ # @return [nil]
267
+ # @option options [Object, Binding] :target The object to run the
227
268
  # command under. Defaults to `TOPLEVEL_BINDING` (main).
228
269
  # @option options [Boolean] :show_output Whether to show command
229
270
  # output. Defaults to true.
230
271
  # @example Run at top-level with no output.
231
272
  # Pry.run_command "ls"
232
273
  # @example Run under Pry class, returning only public methods.
233
- # Pry.run_command "ls -m", :context => Pry
274
+ # Pry.run_command "ls -m", :target => Pry
234
275
  # @example Display command output.
235
276
  # Pry.run_command "ls -av", :show_output => true
236
- def self.run_command(command_string, options={})
277
+ def self.run_command(command_string, options = {})
237
278
  options = {
238
- :context => TOPLEVEL_BINDING,
239
- :show_output => true,
240
- :output => Pry.output,
241
- :commands => Pry.commands
279
+ target: TOPLEVEL_BINDING,
280
+ show_output: true,
281
+ output: Pry.config.output,
282
+ commands: Pry.config.commands
242
283
  }.merge!(options)
243
284
 
285
+ # :context for compatibility with <= 0.9.11.4
286
+ target = options[:context] || options[:target]
244
287
  output = options[:show_output] ? options[:output] : StringIO.new
245
288
 
246
- Pry.new(:output => output, :input => StringIO.new("#{command_string}\nexit-all\n"),
247
- :commands => options[:commands],
248
- :prompt => proc {""}, :hooks => Pry::Hooks.new).repl(options[:context])
289
+ pry = Pry.new(output: output, target: target, commands: options[:commands])
290
+ pry.eval command_string
291
+ nil
249
292
  end
250
293
 
251
- def self.default_editor_for_platform
252
- return ENV['VISUAL'] if ENV['VISUAL'] and not ENV['VISUAL'].empty?
253
- return ENV['EDITOR'] if ENV['EDITOR'] and not ENV['EDITOR'].empty?
294
+ def self.auto_resize!
295
+ Pry.config.input # by default, load Readline
254
296
 
255
- if Helpers::BaseHelpers.windows?
256
- 'notepad'
257
- else
258
- %w(editor nano vi).detect do |editor|
259
- system("which #{editor} > /dev/null 2>&1")
260
- end
297
+ if !defined?(Readline) || Pry.config.input != Readline
298
+ warn "Sorry, you must be using Readline for Pry.auto_resize! to work."
299
+ return
261
300
  end
262
- end
263
301
 
264
- def self.auto_resize!
265
- ver = Readline::VERSION
266
- if ver[/edit/i]
267
- warn <<-EOT
268
- Readline version #{ver} detected - will not auto_resize! correctly.
302
+ if Readline::VERSION =~ /edit/i
303
+ warn(<<-WARN)
304
+ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly.
269
305
  For the fix, use GNU Readline instead:
270
- https://github.com/guard/guard/wiki/Add-proper-Readline-support-to-Ruby-on-Mac-OS-X
271
- EOT
306
+ https://github.com/guard/guard/wiki/Add-Readline-support-to-Ruby-on-Mac-OS-X
307
+ WARN
272
308
  return
273
309
  end
310
+
274
311
  trap :WINCH do
275
312
  begin
276
- Readline.set_screen_size *Terminal.size!
277
- rescue => e
313
+ Readline.set_screen_size(*output.size)
314
+ rescue StandardError => e
278
315
  warn "\nPry.auto_resize!'s Readline.set_screen_size failed: #{e}"
279
316
  end
280
317
  begin
281
318
  Readline.refresh_line
282
- rescue => e
319
+ rescue StandardError => e
283
320
  warn "\nPry.auto_resize!'s Readline.refresh_line failed: #{e}"
284
321
  end
285
322
  end
286
323
  end
287
324
 
288
- def self.set_config_defaults
289
- config.input = Readline
290
- config.output = $stdout
291
- config.commands = Pry::Commands
292
- config.prompt_name = DEFAULT_PROMPT_NAME
293
- config.prompt = DEFAULT_PROMPT
294
- config.print = DEFAULT_PRINT
295
- config.exception_handler = DEFAULT_EXCEPTION_HANDLER
296
- config.exception_whitelist = DEFAULT_EXCEPTION_WHITELIST
297
- config.default_window_size = 5
298
- config.hooks = DEFAULT_HOOKS
299
- config.input_stack = []
300
- config.color = Helpers::BaseHelpers.use_ansi_codes?
301
- config.pager = true
302
- config.system = DEFAULT_SYSTEM
303
- config.editor = default_editor_for_platform
304
- config.should_load_rc = true
305
- config.should_load_local_rc = true
306
- config.should_trap_interrupts = Helpers::BaseHelpers.jruby?
307
- config.disable_auto_reload = false
308
- config.command_prefix = ""
309
- config.auto_indent = Helpers::BaseHelpers.use_ansi_codes?
310
- config.correct_indent = true
311
- config.collision_warning = false
312
- config.output_prefix = "=> "
313
-
314
- if defined?(Bond) && Readline::VERSION !~ /editline/i
315
- config.completer = Pry::BondCompleter
316
- else
317
- config.completer = Pry::InputCompleter
318
- end
319
-
320
- config.gist ||= OpenStruct.new
321
- config.gist.inspecter = proc(&:pretty_inspect)
322
-
323
- config.should_load_plugins = true
324
-
325
- config.requires ||= []
326
- config.should_load_requires = true
327
-
328
- config.history ||= OpenStruct.new
329
- config.history.should_save = true
330
- config.history.should_load = true
331
- config.history.file = File.expand_path("~/.pry_history") rescue nil
332
-
333
- if config.history.file.nil?
334
- config.should_load_rc = false
335
- config.history.should_save = false
336
- config.history.should_load = false
337
- end
338
-
339
- config.control_d_handler = DEFAULT_CONTROL_D_HANDLER
340
-
341
- config.memory_size = 100
342
-
343
- config.extra_sticky_locals = {}
344
-
345
- config.ls ||= OpenStruct.new({
346
- :heading_color => :bright_blue,
347
-
348
- :public_method_color => :default,
349
- :private_method_color => :blue,
350
- :protected_method_color => :blue,
351
- :method_missing_color => :bright_red,
352
-
353
- :local_var_color => :yellow,
354
- :pry_var_color => :default, # e.g. _, _pry_, _file_
355
-
356
- :instance_var_color => :blue, # e.g. @foo
357
- :class_var_color => :bright_blue, # e.g. @@foo
358
-
359
- :global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library
360
- :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID
361
- :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO
362
-
363
- :constant_color => :default, # e.g. VERSION, ARGF
364
- :class_constant_color => :blue, # e.g. Object, Kernel
365
- :exception_constant_color => :magenta, # e.g. Exception, RuntimeError
366
- :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state
367
-
368
- # What should separate items listed by ls? (TODO: we should allow a columnar layout)
369
- :separator => " ",
370
-
371
- # Any methods defined on these classes, or modules included into these classes, will not
372
- # be shown by ls unless the -v flag is used.
373
- # A user of Rails may wih to add ActiveRecord::Base to the list.
374
- # add the following to your .pryrc:
375
- # Pry.config.ls.ceiling << ActiveRecord::Base if defined? ActiveRecordBase
376
- :ceiling => [Object, Module, Class]
377
- })
378
- end
379
-
380
325
  # Set all the configurable options back to their default values
381
326
  def self.reset_defaults
382
- set_config_defaults
383
-
384
327
  @initial_session = true
328
+ @session_finalized = nil
385
329
 
386
- self.custom_completions = DEFAULT_CUSTOM_COMPLETIONS
330
+ self.config = Pry::Config.new
387
331
  self.cli = false
388
332
  self.current_line = 1
389
333
  self.line_buffer = [""]
390
334
  self.eval_path = "(pry)"
391
-
392
- fix_coderay_colors
393
- end
394
-
395
- # To avoid mass-confusion, we change the default colour of "white" to
396
- # "blue" enabling global legibility
397
- def self.fix_coderay_colors
398
- to_fix = if (CodeRay::Encoders::Terminal::TOKEN_COLORS rescue nil)
399
- # CodeRay 1.0.0
400
- CodeRay::Encoders::Terminal::TOKEN_COLORS
401
- else
402
- # CodeRay 0.9
403
- begin
404
- require 'coderay/encoders/term'
405
- CodeRay::Encoders::Term::TOKEN_COLORS
406
- rescue
407
- end
408
- end
409
-
410
- to_fix[:comment] = "0;34" if to_fix
411
335
  end
412
336
 
413
337
  # Basic initialization.
414
338
  def self.init
415
- @plugin_manager ||= PluginManager.new
416
- self.config ||= Config.new
417
- self.history ||= History.new
418
-
419
339
  reset_defaults
420
- locate_plugins
421
340
  end
422
341
 
423
342
  # Return a `Binding` object for `target` or return `target` if it is
@@ -426,19 +345,14 @@ Readline version #{ver} detected - will not auto_resize! correctly.
426
345
  # @param [Object] target The object to get a `Binding` object for.
427
346
  # @return [Binding] The `Binding` object.
428
347
  def self.binding_for(target)
429
- if Binding === target
430
- target
431
- else
432
- if TOPLEVEL_BINDING.eval('self') == target
433
- TOPLEVEL_BINDING
434
- else
435
- target.__binding__
436
- end
437
- end
348
+ return target if Binding === target # rubocop:disable Style/CaseEquality
349
+ return TOPLEVEL_BINDING if Pry.main == target
350
+
351
+ target.__binding__
438
352
  end
439
353
 
440
354
  def self.toplevel_binding
441
- unless @toplevel_binding
355
+ unless defined?(@toplevel_binding) && @toplevel_binding
442
356
  # Grab a copy of the TOPLEVEL_BINDING without any local variables.
443
357
  # This binding has a default definee of Object, and new methods are
444
358
  # private (just as in TOPLEVEL_BINDING).
@@ -454,16 +368,29 @@ Readline version #{ver} detected - will not auto_resize! correctly.
454
368
  @toplevel_binding
455
369
  end
456
370
 
371
+ class << self
372
+ attr_writer :toplevel_binding
373
+ end
374
+
457
375
  def self.in_critical_section?
458
- @critical_section.to_i > 0
376
+ Thread.current[:pry_critical_section] ||= 0
377
+ Thread.current[:pry_critical_section] > 0
459
378
  end
460
379
 
461
- def self.critical_section(&block)
462
- @critical_section = @critical_section.to_i + 1
380
+ def self.critical_section
381
+ Thread.current[:pry_critical_section] ||= 0
382
+ Thread.current[:pry_critical_section] += 1
463
383
  yield
464
384
  ensure
465
- @critical_section -= 1
385
+ Thread.current[:pry_critical_section] -= 1
386
+ end
387
+
388
+ def self.mutex_available?
389
+ Mutex.new.synchronize { true }
390
+ rescue ThreadError
391
+ false
466
392
  end
393
+ private_class_method :mutex_available?
467
394
  end
468
395
 
469
396
  Pry.init