rubypython 0.2.11 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data.tar.gz.sig +0 -0
  2. data/{History.txt → History.markdown} +34 -28
  3. data/Manifest.txt +26 -40
  4. data/PostInstall.txt +2 -1
  5. data/README.markdown +103 -0
  6. data/Rakefile +19 -3
  7. data/lib/rubypython.rb +118 -114
  8. data/lib/rubypython/blankobject.rb +21 -0
  9. data/lib/rubypython/conversion.rb +198 -0
  10. data/lib/rubypython/core_ext/string.rb +7 -0
  11. data/lib/rubypython/legacy.rb +15 -0
  12. data/lib/rubypython/macros.rb +47 -0
  13. data/lib/rubypython/operators.rb +111 -0
  14. data/lib/rubypython/pymainclass.rb +51 -0
  15. data/lib/rubypython/pyobject.rb +203 -0
  16. data/lib/rubypython/python.rb +111 -0
  17. data/lib/rubypython/pythonerror.rb +78 -0
  18. data/lib/rubypython/rubypyproxy.rb +214 -0
  19. data/lib/rubypython/version.rb +4 -3
  20. data/spec/conversion_spec.rb +66 -0
  21. data/spec/legacy_spec.rb +22 -0
  22. data/spec/pymainclass_spec.rb +26 -0
  23. data/spec/pyobject_spec.rb +264 -0
  24. data/spec/python_helpers/objects.py +41 -0
  25. data/spec/pythonerror_spec.rb +43 -0
  26. data/spec/refcnt_spec.rb +68 -0
  27. data/spec/rubypyclass_spec.rb +13 -0
  28. data/spec/rubypyproxy_spec.rb +249 -0
  29. data/spec/rubypython_spec.rb +62 -0
  30. data/spec/spec.opts +2 -0
  31. data/spec/spec_helper.rb +51 -0
  32. metadata +79 -73
  33. metadata.gz.sig +0 -0
  34. data/README.txt +0 -60
  35. data/ext/rubypython_bridge/cbridge.c +0 -150
  36. data/ext/rubypython_bridge/cbridge.h +0 -15
  37. data/ext/rubypython_bridge/config.h +0 -14
  38. data/ext/rubypython_bridge/extconf.rb +0 -43
  39. data/ext/rubypython_bridge/ptor.c +0 -242
  40. data/ext/rubypython_bridge/ptor.h +0 -15
  41. data/ext/rubypython_bridge/rp_blankobject.c +0 -42
  42. data/ext/rubypython_bridge/rp_blankobject.h +0 -11
  43. data/ext/rubypython_bridge/rp_class.c +0 -56
  44. data/ext/rubypython_bridge/rp_class.h +0 -7
  45. data/ext/rubypython_bridge/rp_error.c +0 -34
  46. data/ext/rubypython_bridge/rp_error.h +0 -11
  47. data/ext/rubypython_bridge/rp_function.c +0 -31
  48. data/ext/rubypython_bridge/rp_function.h +0 -7
  49. data/ext/rubypython_bridge/rp_instance.c +0 -164
  50. data/ext/rubypython_bridge/rp_instance.h +0 -7
  51. data/ext/rubypython_bridge/rp_module.c +0 -160
  52. data/ext/rubypython_bridge/rp_module.h +0 -8
  53. data/ext/rubypython_bridge/rp_object.c +0 -194
  54. data/ext/rubypython_bridge/rp_object.h +0 -23
  55. data/ext/rubypython_bridge/rp_util.c +0 -63
  56. data/ext/rubypython_bridge/rp_util.h +0 -11
  57. data/ext/rubypython_bridge/rtop.c +0 -212
  58. data/ext/rubypython_bridge/rtop.h +0 -17
  59. data/ext/rubypython_bridge/rubypython_bridge.c +0 -125
  60. data/ext/rubypython_bridge/rubypython_bridge.h +0 -10
  61. data/lib/rubypython/session.rb +0 -4
  62. data/lib/rubypython/wrapper_extensions.rb +0 -83
  63. data/setup.rb +0 -1585
  64. data/tasks/environment.rake +0 -7
  65. data/tasks/extconf.rake +0 -13
  66. data/tasks/extconf/rubypython_bridge.rake +0 -49
  67. data/test/python_helpers/objects.py +0 -12
  68. data/test/test.wav +0 -0
  69. data/test/test_helper.rb +0 -2
  70. data/test/test_rubypython.rb +0 -215
  71. data/test/test_rubypython_bridge_extn.rb +0 -133
  72. data/test/test_session.rb +0 -6
data.tar.gz.sig ADDED
Binary file
@@ -1,49 +1,56 @@
1
- == 0.2.11 2010-07-12
2
- * Bug Fixes
3
- * Fixed some issues with building the extension under ruby 1.9. Sould now be truly 1.9 compatible.
1
+ ## 0.3.0 2010-09-xx
2
+ * Major Enhancements
3
+ * Version 0.3.0 represents an almost complete rewrite of the RubyPython codebase.
4
+ * A native C extension is no longer required. RubyPython uses the 'ffi' gem.
5
+ * RubyPython by default now returns proxy instances for all Ruby objects. See the documentation for more information.
6
+ * Compatibility Updates
7
+ * Version 0.3.0 was created with the goal of being compatible with as many Ruby versions as possible. It should at the very least run on MRI 1.8.6 - 1.9.1.
8
+ * A legacy mode option has been added to provide partial compatibility with version 0.2.x.
4
9
 
5
- == 0.2.10 2010-07-08
10
+ ## 0.2.10 2010-07-08
6
11
  * Bug Fixes
7
12
  * Made some changes to how the native extension is configured and build.
8
- == 0.2.9 2009-10-19
13
+
14
+ ## 0.2.9 2009-10-19
9
15
  * Minor Enhancements
10
16
  * Updated the C code to make it cleaner, more readable, and more maintainable.
11
- == 0.2.8 2009-10-05
17
+
18
+ ## 0.2.8 2009-10-05
12
19
  * Bug Fixes
13
20
  * Some test files were improperly formatted (minor bug fix).
14
- == 0.2.7 2009-3-30
21
+
22
+ ## 0.2.7 2009-3-30
15
23
  * Bug Fixes
16
24
  * Fixed some bugs which caused rubypython to be unable to determine python version correctly.
17
- == 0.2.6 2009-3-19
25
+
26
+ ## 0.2.6 2009-3-19
18
27
  * Bug Fixes
19
28
  * Further updates to increase compatibility with 1.9.
20
- == 0.2.5 2009-3-18
29
+
30
+ ## 0.2.5 2009-3-18
21
31
  * Bug Fixes
22
32
  * Updated to build and run under Ruby 1.9.
23
33
 
24
- == 0.2.4 2008-10-24
34
+ ## 0.2.4 2008-10-24
25
35
  * Major Enhancements
26
- * Provided setter methods for object attributes. Python object attributes can now be set
27
- from within ruby.
28
- * Made wrapper classes a subclass of custom made blank object class to try to avoid name
29
- collisions.
36
+ * Provided setter methods for object attributes. Python object attributes can now be set from within ruby.
37
+ * Made wrapper classes a subclass of custom made blank object class to try to avoid name collisions.
30
38
  * Bug Fix
31
39
  * Changed part of extconf.rb file that would not work under windows.
32
- == 0.2.3 2008-08-29
40
+
41
+ ## 0.2.3 2008-08-29
33
42
  * 2 Major Enhancements
34
- * Introduced PyMain object as a singleton wrapper for the Python __main__ and
35
- __builtin__ modules.
43
+ * Introduced PyMain object as a singleton wrapper for the Python __main__ and __builtin__ modules.
36
44
  * Introduced block functionality for PyMain object.
37
45
 
38
46
  * Compatibility Updates
39
- * Changed some declarations in the C code to make RubyPython more compatible with the
40
- style conventions of the Ruby C API.
47
+ * Changed some declarations in the C code to make RubyPython more compatible with the style conventions of the Ruby C API.
41
48
  * Update how RubyPython locates the Python headers and library.
42
49
  * 1 Bug Fix
43
- * Fixed an error in ptor.c that might have prevented RubyPython from building correctly
44
- on certain systems.
50
+ * Fixed an error in ptor.c that might have prevented RubyPython from building correctly on certain systems.
45
51
 
46
- == 0.2.2 2008-08-07
52
+
53
+ ## 0.2.2 2008-08-07
47
54
  * Major Enhancements
48
55
  * Wrapped classes and instances should now behave as expected.
49
56
  * Gave RubyPyClasses a "new" method for creating instances.
@@ -51,15 +58,14 @@
51
58
  * A wrapped object's respond_to? method now has some relation to its actual methods.
52
59
 
53
60
  * Bug fixes
54
- * Fixed bug with inspect method of RubyPyObject that caused a bus error when inspecting
55
- certain objects
61
+ * Fixed bug with inspect method of RubyPyObject that caused a bus error when inspecting certain objects
56
62
 
57
63
 
58
- == 0.2.1 2008-08-02
64
+ ## 0.2.1 2008-08-02
59
65
  * 1 Bug Fix
60
66
  * Incorrect require fixed
61
67
 
62
- == 0.2.0 2008-08-02
68
+ ## 0.2.0 2008-08-02
63
69
  * 3 major enhancements
64
70
  * RubyPython can now effectively convert or wrap most types from Python.
65
71
  * Errors in the Python interpreter are relayed to Ruby errors.
@@ -68,11 +74,11 @@
68
74
  * RubyPython.run did not work correctly. This is fixed now.
69
75
  * Cleanup in RubyPython.stop fixes some issues in RubyPythonBridge.stop
70
76
 
71
- == 0.1.0 2008-08-01
77
+ ## 0.1.0 2008-08-01
72
78
  * A lot of major enhancements
73
79
  * Too many to name. Hey I'm still developing
74
80
 
75
- == 0.0.1 2008-07-30
81
+ ## 0.0.1 2008-07-30
76
82
 
77
83
  * 1 major enhancement:
78
84
  * Initial release
data/Manifest.txt CHANGED
@@ -1,46 +1,32 @@
1
- History.txt
1
+ History.markdown
2
2
  License.txt
3
3
  Manifest.txt
4
4
  PostInstall.txt
5
- README.txt
5
+ README.markdown
6
6
  Rakefile
7
- ext/rubypython_bridge/cbridge.c
8
- ext/rubypython_bridge/cbridge.h
9
- ext/rubypython_bridge/config.h
10
- ext/rubypython_bridge/extconf.rb
11
- ext/rubypython_bridge/ptor.c
12
- ext/rubypython_bridge/ptor.h
13
- ext/rubypython_bridge/rp_blankobject.c
14
- ext/rubypython_bridge/rp_blankobject.h
15
- ext/rubypython_bridge/rp_class.c
16
- ext/rubypython_bridge/rp_class.h
17
- ext/rubypython_bridge/rp_error.c
18
- ext/rubypython_bridge/rp_error.h
19
- ext/rubypython_bridge/rp_function.c
20
- ext/rubypython_bridge/rp_function.h
21
- ext/rubypython_bridge/rp_instance.c
22
- ext/rubypython_bridge/rp_instance.h
23
- ext/rubypython_bridge/rp_module.c
24
- ext/rubypython_bridge/rp_module.h
25
- ext/rubypython_bridge/rp_object.c
26
- ext/rubypython_bridge/rp_object.h
27
- ext/rubypython_bridge/rp_util.c
28
- ext/rubypython_bridge/rp_util.h
29
- ext/rubypython_bridge/rtop.c
30
- ext/rubypython_bridge/rtop.h
31
- ext/rubypython_bridge/rubypython_bridge.c
32
- ext/rubypython_bridge/rubypython_bridge.h
33
7
  lib/rubypython.rb
34
- lib/rubypython/session.rb
8
+ lib/rubypython/blankobject.rb
9
+ lib/rubypython/core_ext/string.rb
10
+ lib/rubypython/pythonerror.rb
11
+ lib/rubypython/macros.rb
12
+ lib/rubypython/conversion.rb
13
+ lib/rubypython/operators.rb
14
+ lib/rubypython/pyobject.rb
15
+ lib/rubypython/pymainclass.rb
16
+ lib/rubypython/python.rb
17
+ lib/rubypython/rubypyproxy.rb
35
18
  lib/rubypython/version.rb
36
- lib/rubypython/wrapper_extensions.rb
37
- setup.rb
38
- tasks/environment.rake
39
- tasks/extconf.rake
40
- tasks/extconf/rubypython_bridge.rake
41
- test/python_helpers/objects.py
42
- test/test.wav
43
- test/test_helper.rb
44
- test/test_rubypython.rb
45
- test/test_rubypython_bridge_extn.rb
46
- test/test_session.rb
19
+ lib/rubypython/legacy.rb
20
+ spec/pymainclass_spec.rb
21
+ spec/pyobject_spec.rb
22
+ spec/python_helpers
23
+ spec/python_helpers/objects.py
24
+ spec/pythonerror_spec.rb
25
+ spec/rubypyclass_spec.rb
26
+ spec/rubypyproxy_spec.rb
27
+ spec/rubypython_spec.rb
28
+ spec/refcnt_spec.rb
29
+ spec/conversion_spec.rb
30
+ spec/legacy_spec.rb
31
+ spec/spec.opts
32
+ spec/spec_helper.rb
data/PostInstall.txt CHANGED
@@ -1,5 +1,6 @@
1
+ A number of things have changed with version 0.3.0. If you are upgrading from a previous version of RubyPython, you should check the docs for instructions on how to get your code working with the new version of RubyPython.
1
2
 
2
- For more information on RubyPython, see http://rubypython.rubyforge.org
3
+ For more information on RubyPython, see http://raineszm.bitbucket.org/rubypython/index.html
3
4
 
4
5
  If you find a bug, or have any suggestions, email me at: raineszm+rubypython@gmail.com
5
6
 
data/README.markdown ADDED
@@ -0,0 +1,103 @@
1
+ # RubyPython
2
+
3
+ * [RubyPython](http://raineszm.bitbucket.org/rubypython/)
4
+
5
+ ## DESCRIPTION:
6
+
7
+ RubyPython is a bridge between the Ruby and Python interpreters. It embeds a
8
+ running Python interpreter in the application's process using FFI and
9
+ provides a means for wrapping and converting Python objects.
10
+
11
+ ## FEATURES/PROBLEMS:
12
+
13
+ ### Features
14
+
15
+ * Can handle simple conversion of Python builtin types to Ruby builtin types and vice versa
16
+ * Can import Python modules
17
+ * Can execute arbitrary methods on imported modules and return the result
18
+ * Python objects can be treated as Ruby objects!
19
+ * Python's standard library available to you from within Ruby.
20
+
21
+ ### Known Problems
22
+
23
+ * Builtin Python methods which require a top level frame object (eval, dir, ...) do not work properly at present.
24
+ * There is no support for passing more complicated Ruby types to Python.
25
+
26
+ ## SYNOPSIS:
27
+ RubyPython lets you leverage the power of the Python standard library while
28
+ using the syntactical power of ruby. Using RubyPython you can write code such
29
+ as:
30
+
31
+ RubyPython.start
32
+ cPickle = RubyPython.import("cPickle")
33
+ p cPickle.dumps("RubyPython is awesome!").rubify
34
+ RubyPython.stop
35
+
36
+ The main point of the gem is to allow access to tools that are not readily availible in Ruby. However, it is clear that many people may wish to use Ruby tools with Python code bases using this library. The largest problem in this case is that there is no support for passing Ruby classes, procs, or methods to Python. That being said, with some creative coding it is still possible to do a lot.
37
+
38
+ One caveat is that it may be tempting to try to translate Python code to Ruby code directly using RubyPython. However, it often makes much more sense to use idiomatic Ruby code where possible. For example if we have the following Python code:
39
+
40
+ import library
41
+ for i in library.a_list:
42
+ print(library.function_call(i))
43
+
44
+ If we wanted for some reason to migrate this to RubyPython, we could do it as follows:
45
+
46
+ RubyPython.start
47
+ library = RubyPython.import 'library'
48
+ library.a_list.to_a.each { |i| puts library.function_call(i).rubify }
49
+ RubyPython.stop
50
+
51
+ There are several things to note about the code above:
52
+
53
+ 1. We made sure to call RubyPython.start before doing anything with the Python interpreter.
54
+ 1. We manually bound our imported library to a local variable. RubyPython will not do that for us.
55
+ 1. We used to\_a to convert a python iterable type to a Ruby array.
56
+ 1. We called rubify before we printed the objects so that they would be displayed as native Ruby objects.
57
+ 1. We stopped the interpreter after we were done with RubyPython.stop.
58
+
59
+ ## REQUIREMENTS:
60
+
61
+ * Python >= 2.4, < 3.0
62
+ * Ruby >= 1.8.6
63
+ * You must be able to build the ffi gem under your environment.
64
+
65
+ Note: RubyPython has been tested on Mac OS 10.5.x
66
+
67
+
68
+ ## INSTALL:
69
+
70
+ [sudo] gem install rubypython
71
+
72
+ ## DOCUMENTATION:
73
+
74
+ The documentation should provide a reasonable description of how to use RubyPython.
75
+ Starting with version 0.3.x there are two modes of operation: normal and
76
+ legacy. These are described in the docs.
77
+
78
+ The most useful to check out [docs](http://raineszm.bitbucket.org/rubypython/) will be those for RubyPython and RubyPython::RubyPyProxy.
79
+
80
+ ## LICENSE:
81
+
82
+ (The MIT License)
83
+
84
+ Copyright (c) 2008 Zach Raines
85
+
86
+ Permission is hereby granted, free of charge, to any person obtaining
87
+ a copy of this software and associated documentation files (the
88
+ 'Software'), to deal in the Software without restriction, including
89
+ without limitation the rights to use, copy, modify, merge, publish,
90
+ distribute, sublicense, and/or sell copies of the Software, and to
91
+ permit persons to whom the Software is furnished to do so, subject to
92
+ the following conditions:
93
+
94
+ The above copyright notice and this permission notice shall be
95
+ included in all copies or substantial portions of the Software.
96
+
97
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
98
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
99
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
100
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
101
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
102
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
103
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,4 +1,20 @@
1
- require 'config/requirements'
2
- require 'config/hoe' # setup Hoe + all gem configuration
1
+ require 'spec/rake/spectask'
2
+ require 'yard'
3
3
 
4
- Dir['tasks/**/*.rake'].each { |rake| load rake }
4
+ desc "Run all examples"
5
+ Spec::Rake::SpecTask.new('spec') do |t|
6
+ t.spec_files = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ desc "Run all examples with RCov"
10
+ Spec::Rake::SpecTask.new('spec:rcov') do |t|
11
+ t.spec_files = FileList['spec/**/*.rb']
12
+ t.rcov = true
13
+ t.rcov_opts = ['--exclude', 'spec']
14
+ end
15
+
16
+ YARD::Rake::YardocTask.new do |t|
17
+ t.options = [ '--markup','markdown', '--title', 'RubyPython Documentation' ]
18
+ end
19
+
20
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/lib/rubypython.rb CHANGED
@@ -1,126 +1,130 @@
1
- require 'rubypython_bridge'
2
- require 'rubypython/wrapper_extensions'
1
+ require 'rubypython/core_ext/string'
2
+ require 'rubypython/python'
3
+ require 'rubypython/pythonerror'
4
+ require 'rubypython/pyobject'
5
+ require 'rubypython/rubypyproxy'
6
+ require 'rubypython/pymainclass'
3
7
 
4
8
 
5
- =begin rdoc
6
- This module provides the direct user interface for the RubyPython extension.
9
+ #This module provides the direct user interface for the RubyPython extension.
10
+ #
11
+ #RubyPython interfaces to the Python C API via the {Python} module using the
12
+ #Ruby FFI gem. However, the end user should only worry about dealing with the
13
+ #methods made avaiable via the RubyPython module.
14
+ #
15
+ #Usage
16
+ #-----
17
+ #It is important to remember that the Python Interpreter must be
18
+ #started before the bridge is functional. This will start the embedded
19
+ #interpreter. If this approach is used, the user should remember to call
20
+ #RubyPython.stop when they are finished with Python.
21
+ #@example
22
+ # RubyPython.start
23
+ # cPickle = RubyPython.import "cPickle"
24
+ # puts cPickle.dumps("RubyPython is awesome!").rubify
25
+ # RubyPython.stop
26
+ #
27
+ #Legacy Mode vs Normal Mode
28
+ #---------------------------
29
+ #By default RubyPython always returns a proxy class which refers method calls to
30
+ #the wrapped Python object. If you instead would like RubyPython to aggressively
31
+ #attempt conversion of return values, as it did in RubyPython 0.2.x, then you
32
+ #should set {RubyPython.legacy_mode} to true. In this case RubyPython will
33
+ #attempt to convert any return value from Python to a native Ruby type, and only
34
+ #return a proxy if conversion is not possible. For further examples see
35
+ #{RubyPython.legacy_mode}.
36
+ module RubyPython
7
37
 
8
- The majority of the functionality lies in the _RubyPythonBridge_ module, which is provided
9
- by the C extension. However, the end user should only worry about dealing with the RubyPython
10
- module as that is designed for user interaction. Furthermore the RubyPythonBridge is somewhat
11
- bad with memory management and using it directly may result in some strange crashes.
38
+ class << self
12
39
 
13
- ==Usage
14
- It is important to remember that the Python Interpreter must be started before the bridge
15
- is functional. This may be done by two methods. One is to use the +start+ function.
16
- This will start the embedded interpreter. If this approach is used, the user should
17
- remember to call RubyPython.stop when they are finished with Python.
18
- Example:
19
- RubyPython.start
20
- cPickle=RubyPython.import "cPickle"
21
- puts cPickle.dumps "RubyPython is awesome!"
22
- RubyPython.stop
23
-
24
- The other method is preferable if one wants a simpler approach. This other method is to use
25
- <tt>RubyPython.run</tt>. run takes a block which is evaluated in the scope of the RubyPython module.
26
- In addition, the interpreter is started before the block is run and halted at its completion.
27
- This allows one to do something like the following:
28
- RubyPython.run do
29
- cPickle=import "cPickle"
30
- puts cPickle.dumps "RubyPython is still awesome!"
31
- end
40
+ #Determines whether RubyPython is operating in Normal Mode or Legacy Mode.
41
+ #If legacy_mode is true, RubyPython switches into a mode compatible with
42
+ #versions < 0.3.0. All Python objects returned by method invocations are
43
+ #automatically converted to natve Ruby Types if RubyPython knows how to do
44
+ #this. Only if no such conversion is known are the objects wrapped in proxy
45
+ #objects. Otherwise RubyPython automatically wraps all returned objects as
46
+ #an instance of {RubyPyProxy} or one of its subclasses.
47
+ #@return [Boolean]
48
+ #@example Normal Mode
49
+ # RubyPython.start
50
+ # string = RubyPython.import 'string'
51
+ # ascii_letters = string.ascii_letters # Here ascii_letters is a proxy object
52
+ # puts ascii_letters.rubify # we use the rubify method to convert it to a
53
+ # # native type
54
+ # RubyPython.stop
55
+ #
56
+ #@example Legacy Mode
57
+ # RubyPython.legacy_mode = true
58
+ # RubyPython.start
59
+ # string = RubyPython.import 'string'
60
+ # ascii_letters = string.ascii_letters # Here ascii_letters is a native ruby string
61
+ # puts ascii_letters # No explicit conversion is neccessary
62
+ # RubyPython.stop
63
+ attr_accessor :legacy_mode
32
64
 
33
- The downside to the above method is that the block has no access to the encompassing scope. An
34
- alternative is to use <tt>RubyPython.session</tt>. The downside to this approach is that the module
35
- methods are not available by their unqualified names: i.e.
36
- irb(main):001:0> RubyPython.session do
37
- irb(main):002:1* cPickle=import "cPickle"
38
- irb(main):003:1> end
39
- NoMethodError: undefined method `import' for main:Object
40
- from (irb):2
41
- from ./rubypython.rb:93:in `call'
42
- from ./rubypython.rb:93:in `session'
43
- from (irb):1
44
-
45
- However:
46
- irb(main):001:0> RubyPython.session do
47
- irb(main):002:1* cPickle=RubyPython.import "cPickle"
48
- irb(main):003:1> puts cPickle.dumps "RubyPython is still awesome!"
49
- irb(main):004:1> end
50
- S'RubyPython is still awesome!'
51
- .
52
- => nil
65
+ #Starts ups the Python interpreter. This method **must** be run
66
+ #before using any Python code. The only alternatives are use of the
67
+ #{session} and {run} methods.
68
+ #@return [Boolean] returns true if the interpreter was started here
69
+ # and false otherwise
70
+ def start
71
+ if Python.Py_IsInitialized != 0
72
+ return false
73
+ end
74
+ Python.Py_Initialize
75
+ true
76
+ end
53
77
 
54
- A compromise can be achieved by just including the RubyPython module into the scope you're
55
- working in.
78
+ #Stops the Python interpreter if it is running. Returns true if the
79
+ #intepreter is stopped by this invocation. All wrapped Python objects
80
+ #should be considered invalid after invocation of this method.
81
+ #@return [Boolean] returns true if the interpreter was stopped here
82
+ # and false otherwise
83
+ def stop
84
+ if Python.Py_IsInitialized !=0
85
+ PyMain.main = nil
86
+ PyMain.builtin = nil
87
+ RubyPython::Operators.send :class_variable_set, '@@operator', nil
88
+ Python.Py_Finalize
89
+ RubyPython::PyObject::AutoPyPointer.current_pointers.clear
90
+ return true
91
+ end
92
+ false
93
+ end
56
94
 
57
- If you really wish to be free of dealing with the interpreter, just import 'rubypython/session'.
58
- This will start the interpreter on import and will halt it when execution ends.
59
-
60
- ==Errors
61
- The RubyPythonModule defines a new error object, PythonError. Should any error occur within
62
- the Python interpreter, the class and value of the error will be passed back into ruby within
63
- the text of the raised PythonError.
64
- irb(main):001:0> RubyPython.start
65
- => true
66
- irb(main):002:0> RubyPython.import "does not exist"
67
- PythonError: ImportError:(No module named does not exist)
95
+ #Import a Python module into the interpreter and return a proxy object
96
+ #for it. This is the preferred way to gain access to Python object.
97
+ #@param [String] mod_name the name of the module to import
98
+ #@return [RubyPyModule] a proxy object wrapping the requested
99
+ #module
100
+ def import(mod_name)
101
+ pModule = Python.PyImport_ImportModule mod_name
102
+ if(PythonError.error?)
103
+ raise PythonError.handle_error
104
+ end
105
+ pymod = PyObject.new pModule
106
+ RubyPyModule.new(pymod)
107
+ end
68
108
 
69
- from ./rubypython.rb:66:in `initialize'
70
- from ./rubypython.rb:66:in `import'
71
- from ./rubypython.rb:66:in `import'
72
- from (irb):2
73
- =end
74
- module RubyPython
75
-
76
- # Used to started the python interpreter. Delegates to RubyPythonBridge
77
- #
78
- # RubyPython.start
79
- # --Some python code--
80
- # RubyPython.stop
81
- #
82
- # Also see, _stop_
83
- def self.start() #=> true||false
84
- RubyPythonBridge.start
85
- end
86
-
87
-
109
+ #Execute the given block, starting the Python interperter before its execution
110
+ #and stopping the interpreter after its execution. The last expression of the
111
+ #block is returned; be careful that this is not a Python object as it will
112
+ #become invalid when the interpreter is stopped.
113
+ #@param [Block] block the code to be executed while the interpreter is running
114
+ #@return the result of evaluating the given block
115
+ def session
116
+ start
117
+ result = yield
118
+ stop
119
+ result
120
+ end
88
121
 
89
- # Used to end the python session. Adds some cleanup on top of RubyPythonBridge.stop
90
- def self.stop() #=> true,false
91
- ObjectSpace.each_object(RubyPythonBridge::RubyPyObject) do |o|
92
- o.free_pobj
122
+ #The same as {session} except that the block is executed within the scope
123
+ #of the RubyPython module.
124
+ def run(&block)
125
+ start
126
+ result = module_eval(&block)
127
+ stop
93
128
  end
94
- PyMain.main=nil
95
- PyMain.builtin=nil
96
- RubyPythonBridge.stop
97
- end
98
-
99
- # Import the python module +mod+ and return it wrapped as a ruby object
100
- def self.import(mod)
101
- RubyPythonBridge.import(mod)
102
- end
103
-
104
- # Handles the setup and cleanup involved with using the interpreter for you.
105
- # Note that all Python object will be effectively scope to within the block
106
- # as the embedded interpreter will be halted at its end. The supplied block is
107
- # run within the scope of the RubyPython module.
108
- #
109
- # Alternatively the user may prefer RubyPython.session which simples handles
110
- # initialization and cleanup of the interpreter.
111
- def self.run(&block)
112
- start
113
- module_eval(&block)
114
- stop
115
- end
116
-
117
- # Simply starts the interpreter, runs the supplied block, and stops the interpreter.
118
- def self.session(&block)
119
- start
120
- retval = block.call
121
- stop
122
- return retval
123
129
  end
124
130
  end
125
-
126
-