freelanceprogrammers.org Forum Index » Delphi

simple script, simple problem ?

View user's profile Post To page top
aap_beertje Posted: Mon Jan 01, 2007 4:38 pm


Joined: 30 Dec 2006

Posts: 15
simple script, simple problem ?
Now walking to the demos, I want to see what it does with my script,
so I created a very simple script (that runs perfectly in PyScripter),
and tested it in about 10 different demos,
and the result is always the same:
first run- floating point division by zero, (from Python24.dll)
next runs- cannot import name plot (from Pythonengine)

<Python>
IOI = range(100)
from pylab import plot, show
plot(IOI,`b`)
show()
print IOI
</Python>

Now I guess, that this may sound very obvious for someone with experience,
but I haven`t a clue where to search :-(
Can someone give me the right hint ?

thanks,
Stef Mientki
Reply with quote
Send private message
View user's profile Post To page top
morgan_martinet Posted: Tue Jan 02, 2007 12:39 am


Joined: 29 Apr 2005

Posts: 103
simple script, simple problem ?
Hi Stef,

You just came across a problem that we already encountered in
PyScripter and that we solved by providing an option that can disable
such FPU exceptions!
This exception is raised by Delphi because some Microsoft Dlls do not
handle the floating point with the same precision and affects flags
that are checked by Delphi RTL code.

So, here are 2 functions grabbed here:
http://www.koders.com/delphi/fid103385EB44F48D991C07ADDEB08526C98BC344DE.aspx

procedure DisableFPUExceptions;
var
FPUControlWord: WORD;
asm
FSTCW FPUControlWord;
OR FPUControlWord, $4 + $1; { Divide by zero + invalid operation }
FLDCW FPUControlWord;
end;


procedure EnableFPUExceptions;
var
FPUControlWord: WORD;
asm
FSTCW FPUControlWord;
AND FPUControlWord, $FFFF - $4 - $1; { Divide by zero + invalid
operation }
FLDCW FPUControlWord;
end;

call DisableFPUExceptions before executing your script and restore it
after with EnableFPUExceptions.
Note that if Python invokes Delphi callbacks then you should
enable/disable the block of code, but I guess that it doesn`t really
matter as long as your not doing floating point arithmetics!

Hope this helps,

Morgan


--- In pythonfordelphi@yahoogroups.com, Stef Mientki <s.mientki@...>
wrote:
>
> Now walking to the demos, I want to see what it does with my script,
> so I created a very simple script (that runs perfectly in PyScripter),
> and tested it in about 10 different demos,
> and the result is always the same:
> first run- floating point division by zero, (from Python24.dll)
> next runs- cannot import name plot (from Pythonengine)
>
> <Python>
> IOI = range(100)
> from pylab import plot, show
> plot(IOI,`b`)
> show()
> print IOI
> </Python>
>
> Now I guess, that this may sound very obvious for someone with
experience,
> but I haven`t a clue where to search :-(
> Can someone give me the right hint ?
>
> thanks,
> Stef Mientki
>
Reply with quote
Send private message
View user's profile Post To page top
aap_beertje Posted: Tue Jan 02, 2007 1:21 am


Joined: 30 Dec 2006

Posts: 15
simple script, simple problem ?
hi Morgan,

> Hi Stef,
>
> You just came across a problem that we already encountered in
> PyScripter and that we solved by providing an option that can disable
> such FPU exceptions!
> This exception is raised by Delphi because some Microsoft Dlls do not
> handle the floating point with the same precision and affects flags
> that are checked by Delphi RTL code.
>
> So, here are 2 functions grabbed here:
>
thanks very much !!
I`ld never heave found that.

cheers,
Stef
Reply with quote
Send private message
View user's profile Post To page top
aap_beertje Posted: Tue Jan 02, 2007 4:23 am


Joined: 30 Dec 2006

Posts: 15
simple script, simple problem ?
hi Morgan,

> Note that if Python invokes Delphi callbacks then you should
> enable/disable the block of code, but I guess that it doesn`t really
> matter as long as your not doing floating point arithmetics!
>
> Hope this helps,
>
well it certainly helped for the first script.

But even after the adaptions, the next script generates eaxctly the
same error. Does this has something to do with your remark about
Delphi Callbacks + floating point ?

<Python>
import numpy
IOI = numpy.zeros(100)
</Python>

I get the following error from line 5566 of TPythonEngine,

executable : demo17.exe
exec. date/time : 2007-01-01 19:51
madExcept version : 3.0
callstack crc : $6bb59620, $6796d7f4, $6796d7f4
exception class : EOverflow
exception message : Floating point overflow.

1e02a66d +001d python24.dll PyEval_EvalCode
1e08ee68 +0068 python24.dll PyRun_String
004abad7 +0103 demo17.exe PythonEngine 5566 +25
TPythonEngine.Run_CommandAsObjectWithDict
004ab9c4 +0014 demo17.exe PythonEngine 5534 +1
TPythonEngine.Run_CommandAsObject
004abbd0 +003c demo17.exe PythonEngine 5581 +1
TPythonEngine.ExecStrings
004b132d +0261 demo17.exe Unit1 111 +16 TForm1.Button1Click

thanks,
Stef Mientki
Reply with quote
Send private message
View user's profile Post To page top
oliverbock Posted: Tue Jan 02, 2007 4:47 am


Joined: 15 Sep 2005

Posts: 12
simple script, simple problem ?
Pylab may expect to always be called from the Python interpreter, rather
than from within another program that is embedding the Python
interpreter. It could be that it does strange things with Python`s
global lock, or something else. Check whether anyone else is running
pylab within Python within another application.


Oliver

--
Stef Mientki wrote:
> Now walking to the demos, I want to see what it does with my script,
> so I created a very simple script (that runs perfectly in PyScripter),
> and tested it in about 10 different demos,
> and the result is always the same:
> first run- floating point division by zero, (from Python24.dll)
> next runs- cannot import name plot (from Pythonengine)
>
> <Python>
> IOI = range(100)
> from pylab import plot, show
> plot(IOI,`b`)
> show()
> print IOI
> </Python>
>
> Now I guess, that this may sound very obvious for someone with experience,
> but I haven`t a clue where to search :-(
> Can someone give me the right hint ?
>
> thanks,
> Stef Mientki
>
>
> To Post a message, send it to: pythonfordelphi@eGroups.com
>
> To Unsubscribe, send a blank message to:
pythonfordelphi-unsubscribe@eGroups.com
> Yahoo! Groups Links
>
>
>
>
>
Reply with quote
Send private message
View user's profile Post To page top
morgan_martinet Posted: Tue Jan 02, 2007 11:29 am


Joined: 29 Apr 2005

Posts: 103
simple script, simple problem ?
> But even after the adaptions, the next script generates eaxctly the
> same error. Does this has something to do with your remark about
> Delphi Callbacks + floating point ?

It is apparently the same problem.
Your script only relies on Python objects and makes no Delphi callback
. So, did you call DisableFpuException before
PythonEngine1.ExecStrings(...) ?
Do it each time you need to execute a script and not only once (in the
form create or unit initialization).

Morgan

> <Python>
> import numpy
> IOI = numpy.zeros(100)
> </Python>
>
> I get the following error from line 5566 of TPythonEngine,
>
> executable : demo17.exe
> exec. date/time : 2007-01-01 19:51
> madExcept version : 3.0
> callstack crc : $6bb59620, $6796d7f4, $6796d7f4
> exception class : EOverflow
> exception message : Floating point overflow.
>
> 1e02a66d +001d python24.dll PyEval_EvalCode
> 1e08ee68 +0068 python24.dll PyRun_String
> 004abad7 +0103 demo17.exe PythonEngine 5566 +25
> TPythonEngine.Run_CommandAsObjectWithDict
> 004ab9c4 +0014 demo17.exe PythonEngine 5534 +1
> TPythonEngine.Run_CommandAsObject
> 004abbd0 +003c demo17.exe PythonEngine 5581 +1
> TPythonEngine.ExecStrings
> 004b132d +0261 demo17.exe Unit1 111 +16 TForm1.Button1Click
>
> thanks,
> Stef Mientki
>
Reply with quote
Send private message
View user's profile Post To page top
aap_beertje Posted: Tue Jan 02, 2007 4:06 pm


Joined: 30 Dec 2006

Posts: 15
simple script, simple problem ?
hi Morgan,

> It is apparently the same problem.
> Your script only relies on Python objects and makes no Delphi callback
> . So, did you call DisableFpuException before
> PythonEngine1.ExecStrings(...) ?
> Do it each time you need to execute a script and not only once (in the
> form create or unit initialization).
>
As I described to Oliver, it runs perfect in PyScripter, both from the
normal edit window and from the commandline window. But the PyScripter
code is too difficult for me.

I implemented it exactly in this way
<Delphi>
DisableFPUExceptions;
PythonEngine1.ExecStrings(Memo1.Lines);
EnableFPUExceptions;
</Delphi>

When I try to exec twice or more, I get an diiferent error the second
time:
first time:
second time and all folowing times: an normal exception is raised:
madExcept version : 3.0
callstack crc : $3768d7ac, $14547c2e, $14547c2e
count : 2
exception class : EPyNameError
exception message : NameError: name `ones` is not defined.

main thread ($dac):
004ac5a1 +3f1 demo17.exe PythonEngine 5796 +49 TPythonEngine.RaiseError
004aedd2 +062 demo17.exe PythonEngine 6772 +14 TPythonEngine.CheckError
004abaee +11a demo17.exe PythonEngine 5568 +27
TPythonEngine.Run_CommandAsObjectWithDict
004ab9c4 +014 demo17.exe PythonEngine 5534 +1
TPythonEngine.Run_CommandAsObject
004abbd0 +03c demo17.exe PythonEngine 5581 +1 TPythonEngine.ExecStrings
004b132d +261 demo17.exe Unit1 111 +16 TForm1.Button1Click
00482204 +064 demo17.exe Controls TControl.Click

So I guess this looks that the import didn`t succeed,
the second time: the import has no effect,
and thus the numpy funtion is unknown.


Any other suggestions ?

thanks,
Stef Mientki
Reply with quote
Send private message
View user's profile Post To page top
kiriakosvlahos Posted: Sun Jan 07, 2007 6:17 am


Joined: 25 Apr 2005

Posts: 8
simple script, simple problem ?
I tried Demo 17 with the following code:

import numpy
IOI = numpy.zeros(100)
print IOI

and in procedure TForm1.Button1Click(Sender: TObject);

I had:

MaskFPUExceptions(True);
PythonEngine1.ExecStrings(Memo1.Lines);

where MaskFPUExceptions is:

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


That worked fine multiple times. (Python 2.5 Numpy 1.01)

Hope this helps.
Reply with quote
Send private message
View user's profile Post To page top
aap_beertje Posted: Sun Jan 07, 2007 5:21 pm


Joined: 30 Dec 2006

Posts: 15
simple script, simple problem ?
hello Kiriakos,
that`s fantastic !!
it works like a charm,
the complete program can now be run from Delphi.
I use the Enthought edition which has slightly older versions,
but that doesn`t seem to be a problem.

(I still have a small problem, the program sometimes crashes,
after moving the graphical output window of Python,
but as I remember well, problems were to be expected with graphical
interface,
and I not really need it).

Thanks again,
cheers,
Stef
Reply with quote
Send private message
View user's profile Post To page top
aap_beertje Posted: Wed Jan 10, 2007 12:05 am


Joined: 30 Dec 2006

Posts: 15
simple script, simple problem ?
hello Kiriakos,
Removing the plotting routines,
makes everything completely stable.
Also no more memory leakage.

Brings me to a probably weird idea for PyScripter,
as plotting for some people (like me ;-) is very essential,
so judge the results of calculations,
and I`ve the idea that plotlib has serious bugs, that won`t be solved
soon,
can`t you add an option to run the first script, e.g "test.py" in normal
debug mode,
and finally run a second script e.g. "test-plot.py" in external mode.

cheers,
Stef Mientki

Stef Mientki wrote:
>
> hello Kiriakos,
> that`s fantastic !!
> it works like a charm,
> the complete program can now be run from Delphi.
> I use the Enthought edition which has slightly older versions,
> but that doesn`t seem to be a problem.
>
> (I still have a small problem, the program sometimes crashes,
> after moving the graphical output window of Python,
> but as I remember well, problems were to be expected with graphical
> interface,
> and I not really need it).
>
> Thanks again,
> cheers,
> Stef
>
>
Reply with quote
Send private message
View user's profile Post To page top
kiriakosvlahos Posted: Mon Jan 15, 2007 6:02 am


Joined: 25 Apr 2005

Posts: 8
simple script, simple problem ?
PyScripter will soon have a remote Python engine option that will
allow using GUI based modules such as plotlib.
Reply with quote
Send private message
View user's profile Post To page top
aap_beertje Posted: Tue Jan 16, 2007 12:15 am


Joined: 30 Dec 2006

Posts: 15
simple script, simple problem ?
--- In pythonfordelphi@yahoogroups.com, "kiriakosvlahos"
<pyscripter@...> wrote:
>
> PyScripter will soon have a remote Python engine option that will
> allow using GUI based modules such as plotlib.
>
hi Kiriakos,

that`s really great,
thanks very much !!

cheers,
Stef Mientki
Reply with quote
Send private message
View user's profile Post To page top
aap_beertje Posted: Wed Jan 17, 2007 2:41 am


Joined: 30 Dec 2006

Posts: 15
simple script, simple problem ?
--- In pythonfordelphi@yahoogroups.com, "aap_beertje" <s.mientki@...>

hi Kiriakos,

some other suggestions:
debugger window:
- remember the last position, positions of splitters, active tabpage
- interpretate the
in the interpreter (help texts)

cheers,
Stef Mientki
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help