[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

disable regexp warning

Gaspard Bucher

5/13/2008 1:09:00 PM

Hi there !

Is there a way to disable regexp warning ?

I want to do this:

begin
re = /#{str}/
rescue
# invalid regexp
return "<span class='parser_error'>invalid regexp #{str.inspect}</span>"
end

without having tons of messages printed on stderr. Any clue ?

Thanks,

Gaspard

7 Answers

Florian Gilcher

5/13/2008 1:44:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I don't see your problem.

===
def test
str = "a*("

begin
re = /#{str}/
rescue RegexpError => e
# invalid regexp
"<span class='parser_error'>invalid regexp #{str.inspect}:
#{e.message}</span>"
end
end

puts test.inspect
===

doesn't print any errors. I double checked that warnings are on and
the warning level was set to highest.

If it does not work with your ruby version, please post the version
string of you ruby installation (obtained by "ruby -v").

Regards,
Florian Gilcher

On May 13, 2008, at 3:08 PM, Gaspard Bucher wrote:

> Hi there !
>
> Is there a way to disable regexp warning ?
>
> I want to do this:
>
> begin
> re = /#{str}/
> rescue
> # invalid regexp
> return "<span class='parser_error'>invalid regexp #{str.inspect}</
> span>"
> end
>
> without having tons of messages printed on stderr. Any clue ?
>
> Thanks,
>
> Gaspard
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkgpmyoACgkQJA/zY0IIRZbOSwCeNQelH1ZsQkg7wD6LjFtQwEwy
HYEAoKXytOhB9IkwM4HC7vaGpwkWzLaa
=CkEQ
-----END PGP SIGNATURE-----

Gaspard Bucher

5/14/2008 7:28:00 AM

0

This is crazy. When I write a simple test case with your example it
works just fine but I still get this message when running all the
tests:
/lib/parser/test/../lib/rules/zena.rb:231: warning: regexp has invalid interval
/lib/parser/test/../lib/rules/zena.rb:231: warning: regexp has `}'
without escape
compiled-template:1: warning: regexp has invalid interval
compiled-template:1: warning: regexp has `}' without escape

This message does not come from the simple test I wrote but from this test:
ruby test/helpers/zena_parser_test.rb --name test_basic_show_mean_gsub

[edit] Ok, I found the problem:

def test_bad_regexp
assert_equal "<span class='parser_error'>invalid regexp \"# {puts
'I AM MEAN'}\"", validate_regexp("# {puts 'I AM MEAN'}")
end

private
def validate_regexp(str)
begin
re = /#{str}/
rescue => e
# invalid regexp
"<span class='parser_error'>invalid regexp #{str.inspect}</span>"
end
end

The regexp is seen as valid even though it is not. It's the actual run
of the regexp in a gsub that prints the error message.

Is there a way to detect this error ?

Thanks for your help.

Gaspard


Here is the code in place:
http://dev.zenadmin.org/browser/trunk/lib/parser/lib/rules/ze...

On Tue, May 13, 2008 at 3:44 PM, Florian Gilcher <flo@andersground.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I don't see your problem.
>
> ===
> def test
> str = "a*("
>
> begin
> re = /#{str}/
> rescue RegexpError => e
> # invalid regexp
> "<span class='parser_error'>invalid regexp #{str.inspect}:
> #{e.message}</span>"
> end
> end
>
> puts test.inspect
> ===
>
> doesn't print any errors. I double checked that warnings are on and the
> warning level was set to highest.
>
> If it does not work with your ruby version, please post the version string
> of you ruby installation (obtained by "ruby -v").
>
> Regards,
> Florian Gilcher
>
>
>
> On May 13, 2008, at 3:08 PM, Gaspard Bucher wrote:
>
>
> > Hi there !
> >
> > Is there a way to disable regexp warning ?
> >
> > I want to do this:
> >
> > begin
> > re = /#{str}/
> > rescue
> > # invalid regexp
> > return "<span class='parser_error'>invalid regexp #{str.inspect}</span>"
> > end
> >
> > without having tons of messages printed on stderr. Any clue ?
> >
> > Thanks,
> >
> > Gaspard
> >
> >
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.8 (Darwin)
>
> iEYEARECAAYFAkgpmyoACgkQJA/zY0IIRZbOSwCeNQelH1ZsQkg7wD6LjFtQwEwy
> HYEAoKXytOhB9IkwM4HC7vaGpwkWzLaa
> =CkEQ
> -----END PGP SIGNATURE-----
>
>

Rick DeNatale

5/14/2008 1:06:00 PM

0

On Wed, May 14, 2008 at 3:27 AM, Gaspard Bucher <gaspard@teti.ch> wrote:
> This is crazy. When I write a simple test case with your example it
> works just fine but I still get this message when running all the
> tests:
> /lib/parser/test/../lib/rules/zena.rb:231: warning: regexp has invalid interval
> ./lib/parser/test/../lib/rules/zena.rb:231: warning: regexp has `}'
> without escape
> compiled-template:1: warning: regexp has invalid interval
> compiled-template:1: warning: regexp has `}' without escape
>
> This message does not come from the simple test I wrote but from this test:
> ruby test/helpers/zena_parser_test.rb --name test_basic_show_mean_gsub
>
> [edit] Ok, I found the problem:
>
> def test_bad_regexp
> assert_equal "<span class='parser_error'>invalid regexp \"# {puts
> 'I AM MEAN'}\"", validate_regexp("# {puts 'I AM MEAN'}")
> end
>
> private
> def validate_regexp(str)
> begin
> re = /#{str}/
> rescue => e
> # invalid regexp
> "<span class='parser_error'>invalid regexp #{str.inspect}</span>"
> end
> end
>
> The regexp is seen as valid even though it is not. I

Well the regexp you're testing IS valid, perhaps it's not the regexp
you think it is:

irb(main):009:0> a = "#{puts 'I am mean'}"
I am mean
=> ""
irb(main):010:0> a
=> ""
irb(main):011:0> /#{a}/
=> //

Remember that puts return nil, and nil.to_s is an empty string.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Gaspard Bucher

5/14/2008 10:02:00 PM

0

There is a space after '#'. The test actually makes sure no one can
inject ruby. So the role of the function to test is to replace '#{..}'
by '# {...}', but then the {..} is invalid but does not raise an
exception which is annoying.

On Wed, May 14, 2008 at 3:05 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On Wed, May 14, 2008 at 3:27 AM, Gaspard Bucher <gaspard@teti.ch> wrote:
>> This is crazy. When I write a simple test case with your example it
>> works just fine but I still get this message when running all the
>> tests:
>> /lib/parser/test/../lib/rules/zena.rb:231: warning: regexp has invalid interval
>> ./lib/parser/test/../lib/rules/zena.rb:231: warning: regexp has `}'
>> without escape
>> compiled-template:1: warning: regexp has invalid interval
>> compiled-template:1: warning: regexp has `}' without escape
>>
>> This message does not come from the simple test I wrote but from this test:
>> ruby test/helpers/zena_parser_test.rb --name test_basic_show_mean_gsub
>>
>> [edit] Ok, I found the problem:
>>
>> def test_bad_regexp
>> assert_equal "<span class='parser_error'>invalid regexp \"# {puts
>> 'I AM MEAN'}\"", validate_regexp("# {puts 'I AM MEAN'}")
>> end
>>
>> private
>> def validate_regexp(str)
>> begin
>> re = /#{str}/
>> rescue => e
>> # invalid regexp
>> "<span class='parser_error'>invalid regexp #{str.inspect}</span>"
>> end
>> end
>>
>> The regexp is seen as valid even though it is not. I
>
> Well the regexp you're testing IS valid, perhaps it's not the regexp
> you think it is:
>
> irb(main):009:0> a = "#{puts 'I am mean'}"
> I am mean
> => ""
> irb(main):010:0> a
> => ""
> irb(main):011:0> /#{a}/
> => //
>
> Remember that puts return nil, and nil.to_s is an empty string.
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...
>
>

Gaspard Bucher

5/18/2008 7:05:00 AM

0

I still cannot find a solution to disable regex warning in the
following situation:

def valid_regexp?(str)
begin
re = /#{str}/
true
rescue
false
end
end

# should return false and not print a warning
valid_regexp?("foo{bar}")
=> true, prints warning...

Many thanks for your help, advice.

Gaspard

should print "bad regexp", but returns
On Thu, May 15, 2008 at 12:01 AM, Gaspard Bucher <gaspard@teti.ch> wrote:
> There is a space after '#'. The test actually makes sure no one can
> inject ruby. So the role of the function to test is to replace '#{..}'
> by '# {...}', but then the {..} is invalid but does not raise an
> exception which is annoying.
>
> On Wed, May 14, 2008 at 3:05 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:
>> On Wed, May 14, 2008 at 3:27 AM, Gaspard Bucher <gaspard@teti.ch> wrote:
>>> This is crazy. When I write a simple test case with your example it
>>> works just fine but I still get this message when running all the
>>> tests:
>>> /lib/parser/test/../lib/rules/zena.rb:231: warning: regexp has invalid interval
>>> ./lib/parser/test/../lib/rules/zena.rb:231: warning: regexp has `}'
>>> without escape
>>> compiled-template:1: warning: regexp has invalid interval
>>> compiled-template:1: warning: regexp has `}' without escape
>>>
>>> This message does not come from the simple test I wrote but from this test:
>>> ruby test/helpers/zena_parser_test.rb --name test_basic_show_mean_gsub
>>>
>>> [edit] Ok, I found the problem:
>>>
>>> def test_bad_regexp
>>> assert_equal "<span class='parser_error'>invalid regexp \"# {puts
>>> 'I AM MEAN'}\"", validate_regexp("# {puts 'I AM MEAN'}")
>>> end
>>>
>>> private
>>> def validate_regexp(str)
>>> begin
>>> re = /#{str}/
>>> rescue => e
>>> # invalid regexp
>>> "<span class='parser_error'>invalid regexp #{str.inspect}</span>"
>>> end
>>> end
>>>
>>> The regexp is seen as valid even though it is not. I
>>
>> Well the regexp you're testing IS valid, perhaps it's not the regexp
>> you think it is:
>>
>> irb(main):009:0> a = "#{puts 'I am mean'}"
>> I am mean
>> => ""
>> irb(main):010:0> a
>> => ""
>> irb(main):011:0> /#{a}/
>> => //
>>
>> Remember that puts return nil, and nil.to_s is an empty string.
>>
>> --
>> Rick DeNatale
>>
>> My blog on Ruby
>> http://talklikeaduck.denh...
>>
>>
>
>

Heesob Park

5/18/2008 7:59:00 AM

0

Hi,

Gaspard Bucher wrote:
> I still cannot find a solution to disable regex warning in the
> following situation:
>
> def valid_regexp?(str)
> begin
> re = /#{str}/
> true
> rescue
> false
> end
> end
>
> # should return false and not print a warning
> valid_regexp?("foo{bar}")
> => true, prints warning...
>
You can do something like this:

def valid_regexp?(str)
begin
output = StringIO.open('','w')
$stderr = output
re = /#{str}/
output.string !~ /warning:/
rescue
false
ensure
$stderr = STDERR
end
end

# should return false and not print a warning
valid_regexp?("foo{bar}")


Regards,

Park Heesob
--
Posted via http://www.ruby-....

Gaspard Bucher

5/18/2008 12:57:00 PM

0

Thanks Heesob Park !

That's exactly what I needed.

Gaspard

On Sun, May 18, 2008 at 9:59 AM, Heesob Park <phasis@gmail.com> wrote:
> Hi,
>
> Gaspard Bucher wrote:
>> I still cannot find a solution to disable regex warning in the
>> following situation:
>>
>> def valid_regexp?(str)
>> begin
>> re = /#{str}/
>> true
>> rescue
>> false
>> end
>> end
>>
>> # should return false and not print a warning
>> valid_regexp?("foo{bar}")
>> => true, prints warning...
>>
> You can do something like this:
>
> def valid_regexp?(str)
> begin
> output = StringIO.open('','w')
> $stderr = output
> re = /#{str}/
> output.string !~ /warning:/
> rescue
> false
> ensure
> $stderr = STDERR
> end
> end
>
> # should return false and not print a warning
> valid_regexp?("foo{bar}")
>
>
> Regards,
>
> Park Heesob
> --
> Posted via http://www.ruby-....
>
>