delphi – NaN giving an error depending on Python startup? – Education Career Blog

I am using Python4Delphi to embed Python in a Delphi program. Versions: Python 2.6.4, Delphi 2009, Windows XP.

The Delphi program crashes with EInvalidOp when importing json. I tracked it to the line

NaN, PosInf, NegInf = float('nan'), float('inf'), float('-inf')

in json.decoder.

Sure enough, the command float('nan') raises an EInvalidOp when run inside Python embedded in the Delphi program. When executed in the command line Python (same installation) it simply returns nan.

Any idea what is the difference between the Python standard startup and that of the embedded one that may result in such an error?

,

This is most likely that Python uses a different 8087 control word (CW) setting than Delphi.

Try this kind of code:

var
  OldControlWord: Word;
begin
  OldControlWord := Get8087CW();
  Set8087CW($133F);
  try
    // perform your Python code here
  finally
    Set8087CW(OldControlWord);      
  end;
end;

See my blog article on the 8087 CW in Delphi for a more detailed explanation of the $133F value.
It needs the JCL for the T8087Precision type (which is in the Jcl8087 unit).

–jeroen

,

I use the following:
$1332 is the delphi default.
$1232 is the value to deal with Python Issue 9980.

procedure MaskFPUExceptions(ExceptionsMasked : boolean);
begin
//  if ExceptionsMasked then
//    Set8087CW($1332 or $3F)
//  else
//    Set8087CW($1332);
  if ExceptionsMasked then
    Set8087CW($1232 or $3F)
  else
    Set8087CW($1232);
end;

Leave a Comment