Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
resolves collision between popen and Net::HTTP by switching to popen3…
Browse files Browse the repository at this point in the history
… instead
  • Loading branch information
Matt E. Patterson authored and artob committed Jan 16, 2010
1 parent 85e7f8f commit 0b72091
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
24 changes: 17 additions & 7 deletions lib/crm114.rb
@@ -1,5 +1,5 @@
require 'crm114/version'

require 'open3'
module Classifier
class CRM114
CLASSIFICATION_TYPE = '<osb unique microgroom>'
Expand All @@ -14,7 +14,9 @@ class CRM114
#
# @return [String, nil]
def self.version
$1 if IO.popen(CMD_CRM + ' -v', 'r') { |pipe| pipe.readline } =~ /CRM114, version ([\d\w\-\.]+)/
# $1 if IO.popen(CMD_CRM + ' -v', 'r') { |pipe| pipe.readline } =~ /CRM114, version ([\d\w\-\.]+)/
Open3.popen3(CMD_CRM + ' -v') { |stdin,stdout,stderr| stdin.close; @out = stdout.read }
$1 if @out =~ /CRM114, version ([\d\w\-\.]+)/
end

##
Expand All @@ -38,7 +40,13 @@ def initialize(categories, options = {})
def learn!(category, text, &block)
cmd = CMD_CRM + " '" + (OPT_LEARN % [CLASSIFICATION_TYPE, css_file_path(category)]) + "'"
puts cmd if @debug
IO.popen(cmd, 'w') { |pipe| block_given? ? block.call(pipe) : pipe.write(text) }
Open3.popen3(cmd) do |stdin,stdout,stderr|
stdin.write(text)
stdin.close
@result, @err = stdout.read, stderr.read
logger.error "CRM114(learn!) ERROR: #{@err}" if @err.size > 0
end
text.size
end

alias_method :train!, :learn!
Expand All @@ -63,11 +71,13 @@ def classify(text = nil, &block)
files = @categories.collect { |category| css_file_path(category) }
cmd = CMD_CRM + " '" + (OPT_CLASSIFY % [CLASSIFICATION_TYPE, files.join(' '), @path.gsub(/\//, '\/'), FILE_EXTENSION]) + "'"
puts cmd if @debug
result = IO.popen(cmd, 'r+') do |pipe|
block_given? ? block.call(pipe) : pipe.write(text)
pipe.close_write
pipe.readline unless pipe.closed? || pipe.eof?
stdin, stdout, stderr = Open3.popen3(cmd) do |stdin, stdout, stderr|
stdin.write(text)
stdin.close
@result, @err = stdout.read, stderr.read
logger.error "CRM114(classify) ERROR: #{@err}" if @err.size > 0
end
result = @result
return [nil, 0.0] unless result && result.include?("\t")
result = result.split("\t")
[result.first.to_sym, result.last.to_f]
Expand Down
10 changes: 9 additions & 1 deletion test/test_crm114.rb
Expand Up @@ -38,12 +38,20 @@ def teardown
Dir["#{@path}/*.css"].each { |file| File.delete(file) }
end

def test_resolution_of_popen_conflict_with_net_http
# make arbitrary Net:HTTP call
require 'net/http'
txt = Net::HTTP.get_response(URI.parse('http://www.google.com'))
# something goes nuts and popen bombs; we switched to popen3 so this should work fine now
assert_equal(:interesting, @crm.classify('Thus, programs must be written for people to read,').first)
end

def test_version
assert_match(/^[\d]+-[\w\d]+$/, Classifier::CRM114.version)
end

def test_unlearning
assert_raise(RuntimeError) { @crm.unlearn!(:boring, 'Lorem ipsum') }
assert_raise(NotImplementedError) { @crm.unlearn!(:boring, 'Lorem ipsum') }
end

def test_interesting
Expand Down

0 comments on commit 0b72091

Please sign in to comment.