Permalink
Browse files

Update dependencies to delete partially loaded constants.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5464 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent e1140f8 commit ad06514257e4472a986577ba7df66d9b8db8a2c2 @seckar seckar committed Nov 7, 2006
View
@@ -1,5 +1,7 @@
*SVN*
+* Update dependencies to delete partially loaded constants. [Nicholas Seckar]
+
* Fix unicode JSON regexp for Onigurama compatibility. #6494 [whitley]
* update XmlSimple to 1.0.10. Closes #6532. [nicksieger]
@@ -234,7 +234,7 @@ def load_missing_constant(from_mod, const_name)
raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
end
- raise ArgumentError, "Expected #{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name)
+ raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name)
qualified_name = qualified_name_for from_mod, const_name
path_suffix = qualified_name.underscore
@@ -301,6 +301,10 @@ def mark_for_unload(const_desc)
# its execution. Constants may only be regarded as 'new' once -- so if the
# block calls +new_constants_in+ again, then the constants defined within the
# inner call will not be reported in this one.
+ #
+ # If the provided block does not run to completion, and instead raises an
+ # exception, any new constants are regarded as being only partially defined
+ # and will be removed immediately.
def new_constants_in(*descs)
log_call(*descs)
@@ -328,28 +332,39 @@ def new_constants_in(*descs)
constant_watch_stack.concat watch_frames
- yield # Now yield to the code that is to define new constants.
-
- # Find the new constants.
- new_constants = watch_frames.collect do |mod_name, prior_constants|
- # Module still doesn't exist? Treat it as if it has no constants.
- next [] unless qualified_const_defined?(mod_name)
-
- mod = mod_name.constantize
- next [] unless mod.is_a? Module
- new_constants = mod.constants - prior_constants
+ aborting = true
+ begin
+ yield # Now yield to the code that is to define new constants.
+ aborting = false
+ ensure
+ # Find the new constants.
+ new_constants = watch_frames.collect do |mod_name, prior_constants|
+ # Module still doesn't exist? Treat it as if it has no constants.
+ next [] unless qualified_const_defined?(mod_name)
+
+ mod = mod_name.constantize
+ next [] unless mod.is_a? Module
+ new_constants = mod.constants - prior_constants
+
+ # Make sure no other frames takes credit for these constants.
+ constant_watch_stack.each do |frame_name, constants|
+ constants.concat new_constants if frame_name == mod_name
+ end
+
+ new_constants.collect do |suffix|
+ mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}"
+ end
+ end.flatten
- # Make sure no other frames takes credit for these constants.
- constant_watch_stack.each do |frame_name, constants|
- constants.concat new_constants if frame_name == mod_name
- end
+ log "New constants: #{new_constants * ', '}"
- new_constants.collect do |suffix|
- mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}"
+ if aborting
+ log "Error during loading, removing partially loaded constants "
+ new_constants.each { |name| remove_constant name }
+ new_constants.clear
end
- end.flatten
+ end
- log "New constants: #{new_constants * ', '}"
return new_constants
ensure
# Remove the stack frames that we added.
@@ -641,7 +641,8 @@ def test_autoload_doesnt_shadow_no_method_error_with_relative_constant
assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it should have failed!"
end
end
-
+
+ ensure
Object.send(:remove_const, :RaisesNoMethodError) if defined?(::RaisesNoMethodError)
end
@@ -653,7 +654,8 @@ def test_autoload_doesnt_shadow_no_method_error_with_absolute_constant
assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it should have failed!"
end
end
-
+
+ ensure
Object.send(:remove_const, :RaisesNoMethodError) if defined?(::RaisesNoMethodError)
end
@@ -677,6 +679,7 @@ def test_autoload_doesnt_shadow_name_error
end
end
+ ensure
Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError)
end
end

0 comments on commit ad06514

Please sign in to comment.