ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* lpdf.checkedkey has problem with false values
@ 2014-09-15 14:14 Michail Vidiassov
  2014-09-15 14:59 ` Peter Rolf
  0 siblings, 1 reply; 9+ messages in thread
From: Michail Vidiassov @ 2014-09-15 14:14 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Dear Developers and All,

lpdf.checkedkey routine from lpdf-ini.lua has problems with accessing
false boolean values, it returns them as nil. Is it by design or
neglect?
(I vaguely remember stumbling on this problems before, so  there is a
chance that I have already got the answer, but have forgotten it.)
The minimal demo:
\starttext
\startluacode
local function writebool(bv,text)
 if bv == true then
  io.write(text, " true \n")
 elseif bv == nil then
  io.write(text, " nil  \n")
 else
  io.write(text, " false\n")
 end
end
local a = {}
a["x"] = true
a["y"] = false
a["z"] = nil
writebool(lpdf.checkedkey(a,"x","boolean"),"lpdf.checkedkey(a,\"x\",\"boolean\")")
writebool(lpdf.checkedkey(a,"y","boolean"),"lpdf.checkedkey(a,\"y\",\"boolean\")")
writebool(lpdf.checkedkey(a,"z","boolean"),"lpdf.checkedkey(a,\"z\",\"boolean\")")
writebool(a.x,"a.x")
writebool(a.y,"a.y")
writebool(a.z,"a.z")
\stopluacode
TEST
\stoptext


It results in the following relevant output

lpdf.checkedkey(a,"x","boolean") true
lpdf.checkedkey(a,"y","boolean") nil
lpdf.checkedkey(a,"z","boolean") nil
a.x true
a.y false
a.z nil

Note that false value was read by checkedkey as nil.

Sincerely, MIchail
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lpdf.checkedkey has problem with false values
  2014-09-15 14:14 lpdf.checkedkey has problem with false values Michail Vidiassov
@ 2014-09-15 14:59 ` Peter Rolf
  2014-09-15 15:34   ` Michail Vidiassov
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Rolf @ 2014-09-15 14:59 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Am 15.09.2014 um 16:14 schrieb Michail Vidiassov:
> Dear Developers and All,
> 
> lpdf.checkedkey routine from lpdf-ini.lua has problems with accessing
> false boolean values, it returns them as nil. Is it by design or
> neglect?

More a lua design decision. 'nil' is equal to 'false' here, 'true' is
equal to all that is not 'nil'.

http://www.lua.org/pil/3.3.html

> (I vaguely remember stumbling on this problems before, so  there is a
> chance that I have already got the answer, but have forgotten it.)
> The minimal demo:
> \starttext
> \startluacode
> local function writebool(bv,text)
>  if bv == true then

if bv then

>   io.write(text, " true \n")
>  elseif bv == nil then

else
(if not bv then)


HTH,   Peter


>   io.write(text, " nil  \n")
>  else
>   io.write(text, " false\n")
>  end
> end
> local a = {}
> a["x"] = true
> a["y"] = false
> a["z"] = nil
> writebool(lpdf.checkedkey(a,"x","boolean"),"lpdf.checkedkey(a,\"x\",\"boolean\")")
> writebool(lpdf.checkedkey(a,"y","boolean"),"lpdf.checkedkey(a,\"y\",\"boolean\")")
> writebool(lpdf.checkedkey(a,"z","boolean"),"lpdf.checkedkey(a,\"z\",\"boolean\")")
> writebool(a.x,"a.x")
> writebool(a.y,"a.y")
> writebool(a.z,"a.z")
> \stopluacode
> TEST
> \stoptext
> 
> 
> It results in the following relevant output
> 
> lpdf.checkedkey(a,"x","boolean") true
> lpdf.checkedkey(a,"y","boolean") nil
> lpdf.checkedkey(a,"z","boolean") nil
> a.x true
> a.y false
> a.z nil
> 
> Note that false value was read by checkedkey as nil.
> 
> Sincerely, MIchail
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
> 

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lpdf.checkedkey has problem with false values
  2014-09-15 14:59 ` Peter Rolf
@ 2014-09-15 15:34   ` Michail Vidiassov
  2014-09-15 15:56     ` Aditya Mahajan
  0 siblings, 1 reply; 9+ messages in thread
From: Michail Vidiassov @ 2014-09-15 15:34 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Dear Peter,

> More a lua design decision. 'nil' is equal to 'false' here

I know no CS theory behind lua design, but try this:
\starttext
\startluacode
if nil == false then
  io.write("nil == false\n")
end
if nil ~= false then
  io.write("nil ~= false\n")
end
\stopluacode
TEST
\stoptext

and get

nil ~= false

As checkedkey is for reading parameter tables, based on user input,
there may be real difference between a property not set (i.e. nil) and
one set to be false. What if the default setting is true, for example?

Michail
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lpdf.checkedkey has problem with false values
  2014-09-15 15:34   ` Michail Vidiassov
@ 2014-09-15 15:56     ` Aditya Mahajan
  2014-09-15 16:12       ` Michail Vidiassov
  0 siblings, 1 reply; 9+ messages in thread
From: Aditya Mahajan @ 2014-09-15 15:56 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Mon, 15 Sep 2014, Michail Vidiassov wrote:

> Dear Peter,
>
>> More a lua design decision. 'nil' is equal to 'false' here
>
> I know no CS theory behind lua design, but try this:
> \starttext
> \startluacode
> if nil == false then
>  io.write("nil == false\n")
> end
> if nil ~= false then
>  io.write("nil ~= false\n")
> end
> \stopluacode
> TEST
> \stoptext
>
> and get
>
> nil ~= false

I think that what Peter meant was:

if nil then
  ....
end

if not nil then
   ...
end

Aditya
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lpdf.checkedkey has problem with false values
  2014-09-15 15:56     ` Aditya Mahajan
@ 2014-09-15 16:12       ` Michail Vidiassov
  2014-09-15 17:12         ` Hans Hagen
  0 siblings, 1 reply; 9+ messages in thread
From: Michail Vidiassov @ 2014-09-15 16:12 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Dear Aditya, Peter and All,

> I think that what Peter meant was:

the real issue is not teaching me lua and improving the style of my example,
but my complaint (correct or not) about inability of lpdf.checkedkey
to correctly fetch false boolean values: they are fetched as nil, just
as if they were absent or of incorrect type.

Please, someone with time and, preferably, authority - take a look at
lpdf.checkedkey code (5 minutes maximum).
Talking in general about lua, not knowing what lpdf.checkedkey is,
does and is supposed to do, does not look like efficient use of time.

Michail
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lpdf.checkedkey has problem with false values
  2014-09-15 16:12       ` Michail Vidiassov
@ 2014-09-15 17:12         ` Hans Hagen
  2014-09-15 18:25           ` Michail Vidiassov
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen @ 2014-09-15 17:12 UTC (permalink / raw)
  To: ntg-context

On 9/15/2014 6:12 PM, Michail Vidiassov wrote:
> Dear Aditya, Peter and All,
>
>> I think that what Peter meant was:
>
> the real issue is not teaching me lua and improving the style of my example,
> but my complaint (correct or not) about inability of lpdf.checkedkey

it looks like your question was not clear then

> to correctly fetch false boolean values: they are fetched as nil, just
> as if they were absent or of incorrect type.

well, they're just not treated special

> Please, someone with time and, preferably, authority - take a look at
> lpdf.checkedkey code (5 minutes maximum).
> Talking in general about lua, not knowing what lpdf.checkedkey is,
> does and is supposed to do, does not look like efficient use of time.

you can try this (untested)

function lpdf.checkedkey(t,key,variant)
     local pn = t and t[key]
     if pn then
         local tn = type(pn)
         if tn == variant then
             if variant == "string" then
                 return pn ~= "" and pn or nil
             elseif variant == "table" then
                 return next(pn) and pn or nil
             else
                 return pn
             end
         elseif tn == "string" then
             if variant == "number" then
                 return tonumber(pn)
             elseif variant == "boolean" then
                 return toboolean(pn)
             end
         end
     end
end

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lpdf.checkedkey has problem with false values
  2014-09-15 17:12         ` Hans Hagen
@ 2014-09-15 18:25           ` Michail Vidiassov
  2014-09-15 19:17             ` Hans Hagen
  0 siblings, 1 reply; 9+ messages in thread
From: Michail Vidiassov @ 2014-09-15 18:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Dear Hans,

> well, they're just not treated special

IMHO, they have to be treated special.

> you can try this (untested)

I have tested it. It adds nice enhancement, but does not fix the bug
In the following case
local a = {}
a["e"] = "blabla"
a["t"] = "true"
a["f"] = "false"
a["x"] = true
a["y"] = false
a["z"] = nil

your new lpdf.checkedkey correctly fetches "t" and "f", where the old
one failed.
And your new lpdf.checkedkey fails for "e" - if I attempt to fetch it
as boolean I get false instead of nil.
(That is caused by toboolean returning false for unconvertible data -
unlike tonumber).

But I did not complain about reading strings!

My problem was with "y".
Both your new and old lpdf.checkedkey fetch it as nil instead of false.
My (somewhat ugly) idea of the fix is the following

local function lpdf.checkedkey(t,key,variant)
    local pn = t and t[key]
    if pn then
        local tn = type(pn)
        if tn == variant then
            if variant == "string" then
                return pn ~= "" and pn or nil
            elseif variant == "table" then
                return next(pn) and pn or nil
            else
                return pn
            end
        elseif tn == "string" then
            if variant == "number" then
                return tonumber(pn)
            elseif variant == "boolean" then
                 if     pn == "true" then
                   return true
                elseif pn == "false" then
                   return false
                end
          end
        end
    elseif t and t[key] ~= nil and variant == "boolean" and
type(t[key]) == "boolean" then
        return t[key]
    end
end

Michail
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lpdf.checkedkey has problem with false values
  2014-09-15 18:25           ` Michail Vidiassov
@ 2014-09-15 19:17             ` Hans Hagen
  2014-09-16  3:33               ` Michail Vidiassov
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen @ 2014-09-15 19:17 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 9/15/2014 8:25 PM, Michail Vidiassov wrote:
> Dear Hans,
>
>> well, they're just not treated special
>
> IMHO, they have to be treated special.
>
>> you can try this (untested)
>
> I have tested it. It adds nice enhancement, but does not fix the bug
> In the following case
> local a = {}
> a["e"] = "blabla"
> a["t"] = "true"
> a["f"] = "false"
> a["x"] = true
> a["y"] = false
> a["z"] = nil
>
> your new lpdf.checkedkey correctly fetches "t" and "f", where the old
> one failed.
> And your new lpdf.checkedkey fails for "e" - if I attempt to fetch it
> as boolean I get false instead of nil.
> (That is caused by toboolean returning false for unconvertible data -
> unlike tonumber).
>
> But I did not complain about reading strings!
>
> My problem was with "y".
> Both your new and old lpdf.checkedkey fetch it as nil instead of false.
> My (somewhat ugly) idea of the fix is the following
>
> local function lpdf.checkedkey(t,key,variant)
>      local pn = t and t[key]
>      if pn then
>          local tn = type(pn)
>          if tn == variant then
>              if variant == "string" then
>                  return pn ~= "" and pn or nil
>              elseif variant == "table" then
>                  return next(pn) and pn or nil
>              else
>                  return pn
>              end
>          elseif tn == "string" then
>              if variant == "number" then
>                  return tonumber(pn)
>              elseif variant == "boolean" then
>                   if     pn == "true" then
>                     return true
>                  elseif pn == "false" then
>                     return false
>                  end
>            end
>          end
>      elseif t and t[key] ~= nil and variant == "boolean" and
> type(t[key]) == "boolean" then
>          return t[key]
>      end
> end

more like

function lpdf.checkedkey(t,key,variant)
     local pn = t and t[key]
     if pn ~= nil then
         local tn = type(pn)
         if tn == variant then
             if variant == "string" then
                 return pn ~= "" and pn or nil
             elseif variant == "table" then
                 return next(pn) and pn or nil
             else
                 return pn
             end
         elseif tn == "string" then
             if variant == "number" then
                 return tonumber(pn) -- or nil
             elseif variant == "boolean" then
                 return string.is_boolean(pn,nil,true)
             end
         end
     end
end



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lpdf.checkedkey has problem with false values
  2014-09-15 19:17             ` Hans Hagen
@ 2014-09-16  3:33               ` Michail Vidiassov
  0 siblings, 0 replies; 9+ messages in thread
From: Michail Vidiassov @ 2014-09-16  3:33 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Dear Hans,

> more like
>
> function lpdf.checkedkey(t,key,variant)

thank you. That works. Please, commit.

Michail

PS.
lpdf.checkedkey issue was raised while I reviewed 3D PDF support.
But there are greater problems:

It seems that u3d inclusion stopped to work in the current (2014.09.06
20:59) ConTeXt.
Texlive 2014 works.

As far as I can track, the problem is caused by the change in
function register(askedname,specification) from grph-inc.lua:
if "format %a supported by output file format" (and u3d falls under
this category)
specification.found is set to false in the current ConTeXt.
If I set it to true in this case (as in TeXLive 2014 version of
ConTeXt) things start to work again.
May be by doing so I am reversing some unfinished change, but "it works for me".

The relevant patch:
@@ -725,7 +725,7 @@
                     report_inclusion("format %a natively supported by
backend",format)
                 end
             else
-                specification.found = false
+                specification.found = true -- was false
                 if trace_figures then
                     report_inclusion("format %a supported by output
file format",format)
                 end
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-09-16  3:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-15 14:14 lpdf.checkedkey has problem with false values Michail Vidiassov
2014-09-15 14:59 ` Peter Rolf
2014-09-15 15:34   ` Michail Vidiassov
2014-09-15 15:56     ` Aditya Mahajan
2014-09-15 16:12       ` Michail Vidiassov
2014-09-15 17:12         ` Hans Hagen
2014-09-15 18:25           ` Michail Vidiassov
2014-09-15 19:17             ` Hans Hagen
2014-09-16  3:33               ` Michail Vidiassov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).