IronPython 1.0 - Bugs or Features?

C

Claudio Grondi

(just wanted to share my experience with IronPython 1.0)

The context:
C:\IronPython> ipy.exe
IronPython 1.0.60816 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
vs.
C:\Python24> python.exe
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32

IronPython raises "UnboundLocalError: local variable 'strData'
referenced before assignment" error in following case:
<code>
while(someCondition):
try:
strData = strSomeValue()
except:
pass
if( type(strData) == str ) : ### <<< HERE THE ERROR
doSomething()
</code>
CPython 2.4.2 doesn't raise an error with same code.

As strData is not set anywhere before in the code it seems, that
IronPython is somehow right, but I am not sure if it is a bug or a feature.

Another problem with IronPython where CPython 2.4.2 runs ok was while I
was trying to do:
f = file(r'\\.\PhysicalDrive0', 'rb')
getting "ValueError: FileStream will not open Win32 devices such as disk
partitions and tape drives. Avoid use of "\\.\" in the path."
Here the same - I am not sure if it is a bug or a feature.

Can someone knowledgeable elaborate on it a bit please?

Claudio Grondi
 
M

Marc 'BlackJack' Rintsch

The context:
C:\IronPython> ipy.exe
IronPython 1.0.60816 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
vs.
C:\Python24> python.exe
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32

IronPython raises "UnboundLocalError: local variable 'strData'
referenced before assignment" error in following case:
<code>
while(someCondition):
try:
strData = strSomeValue()
except:
pass
if( type(strData) == str ) : ### <<< HERE THE ERROR
doSomething()
</code>
CPython 2.4.2 doesn't raise an error with same code.

Well I get a `NameError` for `someCondition`. Please post a minimal
*working* example that produced the error.

Ciao,
Marc 'BlackJack' Rintsch
 
L

Larry Bates

Claudio said:
(just wanted to share my experience with IronPython 1.0)

The context:
C:\IronPython> ipy.exe
IronPython 1.0.60816 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
vs.
C:\Python24> python.exe
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32

IronPython raises "UnboundLocalError: local variable 'strData'
referenced before assignment" error in following case:
<code>
while(someCondition):
try:
strData = strSomeValue()
except:
pass
if( type(strData) == str ) : ### <<< HERE THE ERROR
doSomething()
</code>
CPython 2.4.2 doesn't raise an error with same code.

As strData is not set anywhere before in the code it seems, that
IronPython is somehow right, but I am not sure if it is a bug or a feature.

Another problem with IronPython where CPython 2.4.2 runs ok was while I
was trying to do:
f = file(r'\\.\PhysicalDrive0', 'rb')
getting "ValueError: FileStream will not open Win32 devices such as disk
partitions and tape drives. Avoid use of "\\.\" in the path."
Here the same - I am not sure if it is a bug or a feature.

Can someone knowledgeable elaborate on it a bit please?

Claudio Grondi

Your problem is a blanket exception handler that ignores
the exception. Blanket exceptions are almost always a
bad idea. Blanket exceptions with pass as the only
command in the except: block is ALWAYS a bad idea.

Basically the line strData = strSomeValue() caused an
exception. Since it was inside a try: block, it then
executed what was in the except: block. Since all that
was in the except: block was pass, it just fell through
to the if statement. At that point strData is not
defined because the try block failed and never create
strData object.

It is doing EXACTLY what you told it to do.

-Larry Bates
 
R

Robin Becker

Claudio said:
Another problem with IronPython where CPython 2.4.2 runs ok was while I
was trying to do:
f = file(r'\\.\PhysicalDrive0', 'rb')
getting "ValueError: FileStream will not open Win32 devices such as disk
partitions and tape drives. Avoid use of "\\.\" in the path."
Here the same - I am not sure if it is a bug or a feature.

Can someone knowledgeable elaborate on it a bit please?
I guess it's M$ being overprotective. Certainly it works in CPython as expected,
<open file '\\.\PhysicalDrive0', mode 'rb' at 0x01292650>

I have used similar to get boot sectors etc, but then did you really think Bill
would let us play with our own hardware? Just wait till DRM gets here.
 
C

Claudio Grondi

Larry said:
Claudio said:
(just wanted to share my experience with IronPython 1.0)

The context:
C:\IronPython> ipy.exe
IronPython 1.0.60816 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
vs.
C:\Python24> python.exe
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32

IronPython raises "UnboundLocalError: local variable 'strData'
referenced before assignment" error in following case:
<code>
while(someCondition):
try:
strData = strSomeValue()
except:
pass
if( type(strData) == str ) : ### <<< HERE THE ERROR
doSomething()
</code>
CPython 2.4.2 doesn't raise an error with same code.

As strData is not set anywhere before in the code it seems, that
IronPython is somehow right, but I am not sure if it is a bug or a feature.

Another problem with IronPython where CPython 2.4.2 runs ok was while I
was trying to do:
f = file(r'\\.\PhysicalDrive0', 'rb')
getting "ValueError: FileStream will not open Win32 devices such as disk
partitions and tape drives. Avoid use of "\\.\" in the path."
Here the same - I am not sure if it is a bug or a feature.

Can someone knowledgeable elaborate on it a bit please?

Claudio Grondi


Your problem is a blanket exception handler that ignores
the exception. Blanket exceptions are almost always a
bad idea. Blanket exceptions with pass as the only
command in the except: block is ALWAYS a bad idea.

Basically the line strData = strSomeValue() caused an
exception. Since it was inside a try: block, it then
executed what was in the except: block. Since all that
was in the except: block was pass, it just fell through
to the if statement. At that point strData is not
defined because the try block failed and never create
strData object.

It is doing EXACTLY what you told it to do.
Sorry for the confusion caused, but you are right.
The actual code was a bit more complex, so I tried to get it down to the
principle, but haven't expected, that
f = file(r'\\.\PhysicalDrive0', 'rb')
buried within strSomeValue() throws an exception as in CPython the code
was running ok.
So the second problem was the cause also for the first one ...
I also erroneously assumed, that the first problem was detected during
parsing ... so, by the way: how can I distinguish an error raised while
parsing the code and an error raised when actually running the code?

Claudio Grondi
 
C

Claudio Grondi

Marc said:
Claudio Grondi wrote: said:
The context:
C:\IronPython> ipy.exe
IronPython 1.0.60816 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
vs.
C:\Python24> python.exe
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32

IronPython raises "UnboundLocalError: local variable 'strData'
referenced before assignment" error in following case:
<code>
while(someCondition):
try:
strData = strSomeValue()
except:
pass
if( type(strData) == str ) : ### <<< HERE THE ERROR
doSomething()
</code>
CPython 2.4.2 doesn't raise an error with same code.


Well I get a `NameError` for `someCondition`. Please post a minimal
*working* example that produced the error.
I can't as after constructing one according to what I have thought was
the cause of it I detected that the error was caused by the exception in
the line
strData = strSomeValue()
(see also my other posting)
I have just misinterpreted the origin of the "UnboundLocalError:" as
raised while parsing and was wondering how it comes, that the parser is
able to detect such things ...
Sorry for the eventually caused confusion.

Claudio Grondi
 
T

tjreedy

Claudio Grondi said:
I also erroneously assumed, that the first problem was detected during
parsing ... so, by the way: how can I distinguish an error raised while
parsing the code and an error raised when actually running the code?

Parsing detects and reports syntax errors and maybe something else if you
use non-ascii chars without matching coding cookie. Other errors are
runtime.

tjr
 
C

Claudio Grondi

tjreedy said:
Parsing detects and reports syntax errors and maybe something else if you
use non-ascii chars without matching coding cookie. Other errors are
runtime.
Let's consider
print '"Data ê"'

In CPython 2.4.2 there is in case of non-ascii character:
sys:1: DeprecationWarning: Non-ASCII character '\xea' in file
C:\IronPython-1.0-BugsOrFeatures.py on line 3, but no encoding
declared; see http://www.python.org/peps/pep-0263.html for details
"Data♀♂ Û"

IronPython does not raise any warning and outputs:
"Data♀♂ ?"

So it seems, that IronPython is not that close to CPython as I have it
expected.
It takes much more time to run this above simple script in IronPython as
in CPython - it feels as IronPython were extremely busy with starting
itself.

Claudio Grondi
 
S

Super Spinner

Claudio said:
Let's consider
print '"Data ê"'

In CPython 2.4.2 there is in case of non-ascii character:
sys:1: DeprecationWarning: Non-ASCII character '\xea' in file
C:\IronPython-1.0-BugsOrFeatures.py on line 3, but no encoding
declared; see http://www.python.org/peps/pep-0263.html for details
"Data♀♂ Û"

IronPython does not raise any warning and outputs:
"Data♀♂ ?"

So it seems, that IronPython is not that close to CPython as I have it
expected.
It takes much more time to run this above simple script in IronPython as
in CPython - it feels as IronPython were extremely busy with starting
itself.

Claudio Grondi

IronPython is a .NET language, so does that mean that it invokes the
JIT before running actual code? If so, then "simple short scripts"
would take longer with IronPython "busy starting itself" loading .NET
and invoking the JIT. This effect would be less noticable, the longer
the program is. But I'm just guessing; I've not used IronPython.
 
C

Claudio Grondi

Super said:
IronPython is a .NET language, so does that mean that it invokes the
JIT before running actual code? If so, then "simple short scripts"
would take longer with IronPython "busy starting itself" loading .NET
and invoking the JIT. This effect would be less noticable, the longer
the program is. But I'm just guessing; I've not used IronPython.
The time of loading IronPython seem to pay out at the end if the script
takes a longer time to run, so you are most probably right. I am a bit
surprised, that the difference is not that big (e.g. at least half the
time) as I have expected from a JIT concept ... :

<code>
# import psyco
# psyco.full()

def arccot(x, unity):
sum = xpower = unity // x
n = 3
sign = -1
while 1:
xpower = xpower // (x*x)
term = xpower // n
if not term:
break
sum += sign * term
sign = -sign
n += 2
return sum

def pi(digits):
print ' start of setting unity value ...',
unity = 10**(digits + 10)
print ' set unity value, starting arccot() ... ',
pi = 4 * (4*arccot(5, unity) - arccot(239, unity))
return pi // 10**10


f = file("pi-decimal-100000digits.out","wb")
f.write(str(pi(100000)))

print """
PC: Pentium 4, 2.8 GHz, Windows XP SP2
writing 100.000 digits of Pi to a file takes using:
CPython 2.4.2 : 2 min 41 s (of CPU time)
CPython+Psyco : 2 min 45 s (of CPU time)
IronPython 1.0 : 1 min 48 s (of CPU time)
"""
</code>

Claudio Grondi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top