Re: [Python-Dev] Floor division

2007-01-20 Thread Armin Rigo
Hi Tim,

On Fri, Jan 19, 2007 at 08:33:23PM -0500, Tim Peters wrote:
> >>> decimal.Decimal(-1) % decimal.Decimal("1e100")
> Decimal("-1")

BTW - isn't that case in contradiction with the general Python rule that
if b > 0, then a % b should return a number between 0 included and b
excluded?  We try hard to do that for ints, longs and floats.  The fact
that it works differently with Decimal could be unexpected.


A bientot,

Armin
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Problem between deallocation of modules and func_globals

2007-01-20 Thread M.-A. Lemburg
On 2007-01-20 00:01, Brett Cannon wrote:
> On 1/19/07, M.-A. Lemburg <[EMAIL PROTECTED]> wrote:
>> On 2007-01-19 22:33, Brett Cannon wrote:
 That's a typical error situation you get in __del__ methods at
 the time the interpreter is shut down.

>>> Yeah, but in this case this is at the end of Py_Initialize() for the
>>> stuff I am doing to the interpreter.  =)
>> Is that in some error branch of Py_Initialize() ? Otherwise
>> I don't see how the modules could get garbage-collected.
>>
> 
> Nope, it's code I am adding to clean out sys.modules of stuff the user
> didn't import themselves; it's for security reasons.

I'm not sure whether that's really going to increase
security: unloading of modules usually isn't safe and you
cannot be sure that it's possible to reinitialize a C
module once it has been loaded in the process. For Python
modules this is often possible, but there still may be
side-effects of the import that you cannot easily undo.

Perhaps you should just move those modules out to a different
dictionary and keep track of it in the import mechanism, so
that while you can't access the module directly via sys.modules,
the import mechanism still knows that it has been loaded and
reinserts it into sys.modules if it gets imported again.

I think that you get more security by explicitly
limiting which modules and packages you allow to be imported
in the first place and restricting what can be done with
sys.path and sys.modules.

 I'm not exactly sure which global state you are referring to. The
 aliase map, the cache used by the search function ?

>>> encodings._cache .
>>>
 Note that the search function registry is a global managed
 in the thread state (it's not stored in any module).

>>> Right, but that is not the issue.  If you have deleted the reference
>>> to the encodings module from sys.modules it then sets encodings._cache
>>> to None.  After the deletion, if you try to encode/decode a unicode
>>> string you can an AttributeError about how encodings._cache does not
>>> have a 'get' method since it is now None instead of a dict.  The
>>> function is fine and still runs, it's just that the global state it
>>> depends on is no longer the way it assume it should be.
>> While I could add some tricks to have the cache dictionary stay
>> alive even after the globals were set to None, I doubt that this
>> will really fix the problem.
>>
>> The encoding package relies on the import mechanism, the codecs
>> module and the _codecs builtin module. Any of these could fail
>> to work depending on the order in which the modules get
>> GCed.
>>
>> There's a reason why things in Py_Finalize() are as carefully
>> ordered :-) Perhaps we need to apply some reordering to the
>> steps in Py_Initialize() ?!
>
> 
> Nah, I just  need to not delete the modules.  =)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 20 2007)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! 
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] syntax misfeature (exception)

2007-01-20 Thread Neal Becker
I accidentally wrote:

try:
 ...
except a,b:

rather than:

try
 ...
except (a,b):

It appears that the 1st example syntax is silently accepted, but doesn't
seem to work.  Is this true?  If so, I'd say it's a wart.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] syntax misfeature (exception)

2007-01-20 Thread Georg Brandl
Neal Becker schrieb:
> I accidentally wrote:
> 
> try:
>  ...
> except a,b:
> 
> rather than:
> 
> try
>  ...
> except (a,b):
> 
> It appears that the 1st example syntax is silently accepted, but doesn't
> seem to work.  Is this true?  If so, I'd say it's a wart.

Both have a meaning: The first assigns the exception object to b, while the
second catches exception types a and b.

BTW, in Python 3.0, the first will be spelled "except a as b".

Please post questions about using the language to the comp.lang.python group.

cheers,
Georg

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] syntax misfeature (exception)

2007-01-20 Thread Neal Becker
Georg Brandl wrote:

> Neal Becker schrieb:
>> I accidentally wrote:
>> 
>> try:
>>  ...
>> except a,b:
>> 
>> rather than:
>> 
>> try
>>  ...
>> except (a,b):
>> 
>> It appears that the 1st example syntax is silently accepted, but doesn't
>> seem to work.  Is this true?  If so, I'd say it's a wart.
> 
> Both have a meaning: The first assigns the exception object to b, while
> the second catches exception types a and b.
> 
> BTW, in Python 3.0, the first will be spelled "except a as b".
> 
> Please post questions about using the language to the comp.lang.python
> group.
> 
It's not a question, it's a critique.  I believe this is a misfeature since
it's so easy to make this mistake.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] syntax misfeature (exception)

2007-01-20 Thread Josiah Carlson

Neal Becker <[EMAIL PROTECTED]> wrote:
[snip]
> It's not a question, it's a critique.  I believe this is a misfeature since
> it's so easy to make this mistake.

And it is going away with Py3k.  Making it go away for Python 2.6 would
either allow for two syntaxes to do the same thing, or would require
everyone to change their except clauses.  Neither is very desireable
(especially if writing code for 2.6 makes it not work for 2.5).


 - Josiah

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Problem between deallocation of modules and func_globals

2007-01-20 Thread Brett Cannon
On 1/20/07, M.-A. Lemburg <[EMAIL PROTECTED]> wrote:
> On 2007-01-20 00:01, Brett Cannon wrote:
> > On 1/19/07, M.-A. Lemburg <[EMAIL PROTECTED]> wrote:
> >> On 2007-01-19 22:33, Brett Cannon wrote:
>  That's a typical error situation you get in __del__ methods at
>  the time the interpreter is shut down.
> 
> >>> Yeah, but in this case this is at the end of Py_Initialize() for the
> >>> stuff I am doing to the interpreter.  =)
> >> Is that in some error branch of Py_Initialize() ? Otherwise
> >> I don't see how the modules could get garbage-collected.
> >>
> >
> > Nope, it's code I am adding to clean out sys.modules of stuff the user
> > didn't import themselves; it's for security reasons.
>
> I'm not sure whether that's really going to increase
> security: unloading of modules usually isn't safe and you
> cannot be sure that it's possible to reinitialize a C
> module once it has been loaded in the process. For Python
> modules this is often possible, but there still may be
> side-effects of the import that you cannot easily undo.
>
> Perhaps you should just move those modules out to a different
> dictionary and keep track of it in the import mechanism, so
> that while you can't access the module directly via sys.modules,
> the import mechanism still knows that it has been loaded and
> reinserts it into sys.modules if it gets imported again.
>

That's an idea.

> I think that you get more security by explicitly
> limiting which modules and packages you allow to be imported
> in the first place and restricting what can be done with
> sys.path and sys.modules.
>

That's what I am doing.  I just wanted to simplify things by having
import not worry about what is already in sys.modules and just always
assume what is there is safe.

-Brett
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] syntax misfeature (exception)

2007-01-20 Thread Anthony Baxter
On Sunday 21 January 2007 05:17, Josiah Carlson wrote:
> Neal Becker <[EMAIL PROTECTED]> wrote:
> [snip]
>
> > It's not a question, it's a critique.  I believe this is a
> > misfeature since it's so easy to make this mistake.
>
> And it is going away with Py3k.  Making it go away for Python 2.6
> would either allow for two syntaxes to do the same thing, or
> would require everyone to change their except clauses.  Neither
> is very desireable (especially if writing code for 2.6 makes it
> not work for 2.5).

Note that we do plan to add "except a as b" to 2.6 - we're just not 
ripping out the old way of doing it.

Anthony
-- 
Anthony Baxter <[EMAIL PROTECTED]>
It's never too late to have a happy childhood.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com