serpent icon indicating copy to clipboard operation
serpent copied to clipboard

serpent does not handle locales that use comma decimal system

Open kyl3n opened this issue 6 years ago • 8 comments

Serpent does not handle comma decimals properly. In about half of the world, they use the comma decimal for numbers (1,1 instead of 1.1).

See reference: https://en.wikipedia.org/wiki/Decimal_separator#Countries_using_decimal_comma http://lua-users.org/wiki/LuaLocales

Changing Lua Locale: assert(os.setlocale('fr_FR'))

When you use serpent to load decimal numbers where the locale has been changed within lua it will not properly load the table. Serpent is interpreting the commas as separate indexes within the table.

See example:

local s = serpent.dump({TEST = 0.2,HI = 0.5})
local status,l = serpent.load(s)
print(serpent.block(l))

{
     5,
     20000000000000000, 
     HI = 0,
     TEST = 0
} --[[table: 0x12928ac0]]

Also serpent seems to be overly aggressive in trying to round floating point numbers for some reason (see above) . I will create a separate issue for that.

kyl3n avatar Mar 27 '20 16:03 kyl3n

When you use serpent to load decimal numbers where the locale has been changed within lua it will not properly load the table. Serpent is interpreting the commas as separate indexes within the table.

This is an interesting point. There is not much that Serpent does during load (as it's handled by Lua loadstring/load methods), but I'll check if it's safer to switch to using ; as the field separator instead of , in the output.

Also serpent seems to be overly aggressive in trying to round floating point numbers for some reason (see above) . I will create a separate issue for that.

Serpent already provides numformat option that controls the representation format for floating point numbers. Is that what you need?

pkulchenko avatar Mar 27 '20 16:03 pkulchenko

This is an interesting point. There is not much that Serpent does during load (as it's handled by Lua loadstring/load methods), but I'll check if it's safer to switch to using ; as the field separator instead of , in the output.

One of the issues here is that lua as a language still expects a period decimal when representing numbers, anything else will throw an error.

Quote from Lua Official:

Numbers in Lua code must be written in the C locale style: print(1.23) -- always valid print(1,23) -- always interpreted as two numbers: "1" and "23".

So I think what is happening is serpent is representing the decimal as a comma in a string, but lua cannot interpret a comma decimal string into a number via loadstring/load.

Maybe the easiest course of action is to ensure that serpent serializes comma decimals as point decimals in your serpent serialized form ("do local _={HI=0.5,TEST=0.2};return _;end").

When lua is in a comma decimal locale and point decimals are used within serpents serialized string, it behaves as expected. Below you will see the different behaviors of this in action.

These are both ran in a comma decimal locale:

-- USING POINT DECIMALS IN STRING
local f = "do local _={HI=0.5,TEST=0.2};return _;end"
local status,l = serpent.load(f)
print (serpent.block(l))

{
  HI = 0,5,
  TEST = 0,20000000000000001
} --[[table: 0x13761220]]

-- USING COMMA DECIMALS IN STRING
local f = "do local _={HI=0,5,TEST=0,2};return _;end"
local status,l = serpent.load(f)
print (serpent.block(l))
{
  5,
  2,
  HI = 0,
  TEST = 0
} --[[table: 0x11bcd660]]

Sepent already provides numformat option that controls the representation format for floating point numbers. Is that what you need?

The aggressive rounding i'm referring to can be seen in the first example above. I tried a good amount of combinations of numformat like you suggested. But could not achieve the exact floating point number. Is there a way that numformat can output the exact floating point number? Could I maybe even go so far as to propose an option to not try to "modify/round" floating point numbers.

kyl3n avatar Mar 27 '20 17:03 kyl3n

@kyl3n, according to this wiki page, it looks like the lexer is not locale-aware (which your tests confirm), so it will parse incorrectly any non-C-locale numbers. I think any number produced with the non-C-locale setting, needs to be wrapped into tonumber() call, such that HI=0,5 becomes HI=tonumber("0,5"), which will produce the expected result, assuming you (as the user) set the correct locale before executing load.

The main problem with this is that if you don't set the correct locale, all your floating point numbers turn into nil. Another (safer) option is to use HI=tonumber("0,5") or tonumber("0.5"), but this would require switching locale twice for each generated number, which is likely to slow down the serialization. Maybe HI=tonumber("0,5") or die"locale mismatch"?

Another option is to produce something like this `HI=(function(n) local locale = os.setlocale() if locale ~= "C" then os.setlocale("C") n = tonumber(n) os.setlocale(locale) end return n end)("0.5")

Yet another option is to call os.setlocale("C") in Serpent when generating the output, which will produce HI = 0.5, which will be correctly handled by the lexer. Something like this (not tested):

diff --git a/src/serpent.lua b/src/serpent.lua
index a043713..dd220cf 100644
--- a/src/serpent.lua
+++ b/src/serpent.lua
@@ -112,10 +112,13 @@ local function s(t, opts)
       return tag..(func or globerr(t, level))
     else return tag..safestr(t) end -- handle all other types
   end
+  local locale = os.setlocale and os.setlocale()
+  if locale and locale ~= "C" then os.setlocale("C") end
   local sepr = indent and "\n" or ";"..space
   local body = val2str(t, name, indent) -- this call also populates sref
   local tail = #sref>1 and table.concat(sref, sepr)..sepr or ''
   local warn = opts.comment and #sref>1 and space.."--[[incomplete output with shared/self-references skipped]]" or ''
+  if locale and locale ~= "C" then os.setlocale(locale) end
   return not name and body..warn or "do local "..body..sepr..tail.."return "..name..sepr.."end"
 end
 

pkulchenko avatar Mar 27 '20 17:03 pkulchenko

The aggressive rounding i'm referring to can be seen in the first example above. I tried a good amount of combinations of numformat like you suggested. But could not achieve the exact floating point number. Is there a way that numformat can output the exact floating point number? Could I maybe even go so far as to propose an option to not try to "modify/round" floating point numbers.

I'll need a specific example of this, as I don't see where the rounding happens. Using "%.17g" (which is the default) is supposed to preserve all the significant digits, but this again may be the case of different lexing of string literals.

pkulchenko avatar Mar 27 '20 17:03 pkulchenko

@kyl3n, according to this wiki page, it looks like the lexer is not locale-aware (which your tests confirm), so it will parse incorrectly any non-C-locale numbers. I think any number produced with the non-C-locale setting, needs to be wrapped into tonumber() call, such that HI=0,5 becomes HI=tonumber("0,5"), which will produce the expected result, assuming you (as the user) set the correct locale before executing load.

The main problem with this is that if you don't set the correct locale, all your floating point numbers turn into nil. Another (safer) option is to use HI=tonumber("0,5") or tonumber("0.5"), but this would require switching locale twice for each generated number, which is likely to slow down the serialization. Maybe HI=tonumber("0,5") or die"locale mismatch"?

Correct thinking here. However, I don't think we should worry about the user setting the correct locale, as that is a purely user facing problem. Where as the problem we are dealing with here is where the locale is set properly, but we are getting unexpected behavior because the results are not being translated properly ie(Code works in UK, does not work in France). I question why lua even has this behavior to begin with...

So my thinking was that we establish if we are a point vs comma decimal: locale_character = (string.match(tostring(1/2),'%p'))

And then use something like gsub to go through and modify the string represented integers before it's serialized: s = s:gsub('[%.%,]',locale_character)

What a tongue twister this one is.

Is there any way to accomplish this without using something along the lines of gsub and having to modify the string...

I need to think about this a bit more. Thanks.

kyl3n avatar Mar 27 '20 18:03 kyl3n

And then use something like gsub to go through and modify the string represented integers before it's serialized: Is there any way to accomplish this without using something along the lines of gsub and having to modify the string...

I think setting the locale to "C" before doing serialization will achieve the same result without gsub. That's why I suggested resetting it in Serpent (see the diff), as it would make it transparent to the user without using any string fixing hacks.

pkulchenko avatar Mar 27 '20 18:03 pkulchenko

I think setting the locale to "C" before doing serialization will achieve the same result without gsub. That's why I suggested resetting it in Serpent (see the diff), as it would make it transparent to the user without using any string fixing hacks

Understood and good point. However I actually don't think setting the locale this way is a viable option, In the case of systems which use lua along with their own API. Some may want to strip lua of its os.setlocale function for the user, to prevent tampering. Meaning the the locale could be changed on the backend, but be stripped before the user or the script in our case has the ability to switch between locales.

kyl3n avatar Mar 27 '20 19:03 kyl3n

Some may want to strip lua of its os.setlocale function for the user, to prevent tampering.

That's correct, but this would be handled by the patch. It may produce "incorrect" results in this case, but it would be the responsibility of the user code to "fix" it by setting "C" locale before serialization. The only other option would be to call emit tonumber("1,5") instead of 1.5, as we discussed earlier...

pkulchenko avatar Mar 27 '20 19:03 pkulchenko