Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-17 Thread Yair Chuchem
On 5/16/06, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:

Rotem should simply avoid to fork() in the toplevel code of a module.this is not what happened.we called subprocess which itself called fork.as a subprocess bug this could be easily fixed by moving the import in "os._execve".

we have made that fix locally and it fixes the problem.

cheers

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


Re: [Python-Dev] total ordering.

2006-05-17 Thread Vladimir 'Yu' Stepanov

Jason Orendorff wrote:

On 5/11/06, Vladimir 'Yu' Stepanov <[EMAIL PROTECTED]> wrote:

If for Python-3000 similar it will be shown concerning types
str(), int(), complex() and so on, and the type of exceptions
will strongly vary, it will make problematic redefinition of
behavior of function of sorting.


I don't see what you mean by "redefinition of behavior of function of
sorting".  Is this something a Python programmer might want to do?
Can you give an example?



It can be shown concerning external types. When everyone them
we shall compare to any internal type, but not with each other.
It concerns functions and the methods realized through CPython
API. For example types of the whole greater numbers of different
mathematical libraries. Or in case of use of several different
representation of character arrays.

One more example of work with a database which is more
realistic. For Python-3000 it will be possible to use
comparison only compatible types. There can be a problem
to sort types of data incompatible for comparison.

conn = MySQLdb.connect(...)
cu = conn.cursor()
cu.execute("""
CREATE TABLE bigtable (
   id INT NOT NULL AUTO_INCREMENT,
   group_id INT NULL,
   value VARCHAR(255) NOT NULL,
   PRIMARY KEY (id)
)""")
for query in (
"INSERT INTO bigtable VALUES (NULL, 50, 'i am lazy')",
"INSERT INTO bigtable VALUES (NULL, 20, 'i am very lazy')",
"INSERT INTO bigtable VALUES (NULL, 19, 'i am very very lazy')",
"INSERT INTO bigtable VALUES (NULL, 40, 'i am very very very lazy :)')",
"INSERT INTO bigtable VALUES (NULL, NULL, 'Something different')"
):
   cu.execute(query)
cu.execute("SELECT * FROM bigtable ORDER BY id")
lst = [ r for r in cu ]
... do something in given order ...
lst.sort(cmp=lambda a, b: cmp(a[1], b[1]))


If it is required to sort records on a column group_id it
hardly will be possible to us for python-3000 as numbers in
a column group_id will alternate with None values. Certainly
it is possible to filter the list preliminary. But it will not
be effective. Especially, if to assume, that sorted values can
have some various incompatible types (it already beyond the
framework of the given example).

The essence of my offer consists in at first to admit check of
types due to fast mechanisms CPython of methods, and only in
case of incompatibility of types of data to use spare model of
comparison, only if it is necessary. Creation of uniform
exception should facilitate creation of a design try-except.

Class of exceptions TypeError have wide using. For functions
and methods of comparison the specialized class is necessary.
In fact nobody uses class StopIteration for generation of
messages on a error :)

Other example when classes of different modules are used, and
it is necessary to sort them (see attached "example1.py").


On 5/16/06, Vladimir 'Yu' Stepanov <[EMAIL PROTECTED]> wrote:

It will be possible it conveniently to use as exception of
management by a stream, for indication of necessity to involve
`.__r(eq|ne|le|lt|ge|gt|cmp)__()' a method. This kind of a class
can carry out function, similarly to StopIteration for `.next()'.


There are no .__r(eq|ne|le|lt|ge|gt|cmp)__() methods, for a logical
reason which you might enjoy deducing yourself...


Oops :) I has simply hastened. About __rcmp__ has once read
through, and only here has emerged in memory when about it
already all to think have forgotten. As inopportunely.




At present time similar function is carried out with exception
NotImplemented. This exception is generated in a number of
mathematical operations. For this reason I ask to consider an
opportunity of creation of a new class.


Can you explain this?  NotImplemented isn't an exception.
(NotImplementedError is, but that's something quite different.)


I was mistaken. Sorry. It is valid not a class of exceptions.

NotImplemented has exactly one purpose in Python, as far as I can
tell.  What mathematical operations do you mean?


At multiplication/division/addition/subtraction/binary of operation/etc
- if types are non-comparable (binary_op1 in abstract.c). Very universal
element. CPython API will transform returned element NotImplemented to
generation of exception TypeError. Whether it is good?

In my opinion thus it is possible to hide a mistake of programming. As
an example the attached file example2.py.

Thanks.

#!/usr/local/bin/python
import types

class CompareError:
pass

# class in module1
class A:
uniform_global_sort_criterion = 1
def __init__(self, val):
self.val = val
def __cmp__(self, other):
if type(self) is type(other) and self.__class__ == 
other.__class__:
return cmp(self.val, other.val)
raise CompareError
def __repr__(self):
return "%s(%d)" % (self.__class__.__name__, self.val)

# class in mo

Re: [Python-Dev] MSVC 6.0 patch

2006-05-17 Thread Thomas Heller
Luke Dunstan wrote:
> Hi,
> 
> This patch submitted in March allows Python trunk to be built using MSVC 6:
> 
> http://sourceforge.net/tracker/index.php?func=detail&aid=1457736&group_id=5470&atid=305470
> 
> It is not my patch but I am quite interested because the code changes are 
> also required to build with MS eMbedded Visual C++ 4.0, the Windows CE 4.x 
> compiler. I'm guessing that the person assigned to the patch is too busy so 
> I hope somebody else can take over?

Since nobody else replied, let me answer this:
The reason that this patch has not been applied is probably that nobody
of the core developers is interested in it any longer.

IMO the best would be if you integrated this patch, or the subset that you need,
into your WinCE patch.

Thomas

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


[Python-Dev] New string method - splitquoted

2006-05-17 Thread Dave Cinege
Very oftenmake that very very very very very very very very very often,
I find myself processing text in python that  when .split()'ing a line, I'd 
like to exclude the split for a 'quoted' item...quoted because it contains 
whitespace or the sep char.

For example:

s = '  Chan: 11  SNR: 22  ESSID: "Spaced Out Wifi"  Enc: On'

If I want to yank the essid in the above example, it's a pain. But with my new 
dandy split quoted method, we have a 3rd argument to .split() that we can 
spec the quote delimiter where no splitting will occur, and the quote char 
will be dropped:

s.split(None,-1,'"')[5]
'Spaced Out Wifi'

Attached is a proof of concept patch against 
Python-2.4.1/Objects/stringobject.c  that implements this. It is limited to 
whitespace splitting only. (sep == None)

As implemented the quote delimiter also doubles as an additional separator for 
the spliting out a substr. 

For example:
'There is"no whitespace before these"quotes'.split(None,-1,'"')
['There', 'is', 'no whitespace before these', 'quotes']

This is useful, but possibly better put into practice as a separate method??

Comments please.

Dave
--- stringobject.c.orig	2006-05-17 16:12:13.0 -0400
+++ stringobject.c	2006-05-17 23:49:52.0 -0400
@@ -1336,6 +1336,85 @@
 	return NULL;
 }
 
+// dc: split quoted example
+// 'This string has  "not only this" "and this" but"this mixed in string"as well as this "" empty one and two more at the end'.split(None,-1,'"')
+// CORRECT: ['This', 'string', 'has', 'not only this', 'and this', 'but', 'this mixed in string', 'as', 'well', 'as', 'this', '', 'empty', 'one', 'and', 'two', 'more', 'at', 'the', 'end', '', '']
+static PyObject *
+split_whitespace_quoted(const char *s, int len, int maxsplit, const char *qsub)
+{
+	int i, j, quoted = 0;
+	PyObject *str;
+	PyObject *list = PyList_New(0);
+
+	if (list == NULL)
+		return NULL;
+
+	for (i = j = 0; i < len; ) {
+			
+		if (!quoted) {
+			while (i < len && isspace(Py_CHARMASK(s[i])) )
+i++;
+		}
+		
+		if (Py_CHARMASK(s[i]) == Py_CHARMASK(qsub[0])) {
+			quoted = 1;
+			i++;
+		}
+		
+		j = i;
+			
+		while (i < len) {
+			if (Py_CHARMASK(s[i]) == Py_CHARMASK(qsub[0])) {	
+if (quoted)	
+	quoted = 2;	// End of quotes found 
+else {
+	quoted = 1;	// Else start of new quotes in the middle of a string
+}
+break;
+			} else if (!quoted && isspace(Py_CHARMASK(s[i])))
+	break;
+			i++;
+		}
+		
+		if (quoted == 2 && j == i) {	// Empty string in quotes
+			SPLIT_APPEND("", 0, 0);
+			quoted = 0;
+			i++;
+			j = i;
+
+		} else if (j < i) {
+			if (maxsplit-- <= 0)
+break;
+			SPLIT_APPEND(s, j, i);
+	
+			if (quoted == 2) {
+quoted = 0;
+i++;
+			} else if (quoted == 1) {
+i++;
+if (Py_CHARMASK(s[i]) == Py_CHARMASK(qsub[0])) { // Embedded empty string in quotes (at end of string?)
+	SPLIT_APPEND("", 0, 0);
+	quoted = 0;
+	i++;
+}
+			} else {
+while (i < len && isspace(Py_CHARMASK(s[i])))
+	i++;
+			}
+			
+			j = i;
+		}
+	}
+	if (j < len) {
+		SPLIT_APPEND(s, j, len);
+	}
+	return list;
+  onError:
+	Py_DECREF(list);
+	return NULL;
+}
+
+
 static PyObject *
 split_char(const char *s, int len, char ch, int maxcount)
 {
@@ -1376,15 +1455,27 @@
 static PyObject *
 string_split(PyStringObject *self, PyObject *args)
 {
-	int len = PyString_GET_SIZE(self), n, i, j, err;
+	int len = PyString_GET_SIZE(self), n, qn, i, j, err;
 	int maxsplit = -1;
-	const char *s = PyString_AS_STRING(self), *sub;
-	PyObject *list, *item, *subobj = Py_None;
+	const char *s = PyString_AS_STRING(self), *sub, *qsub;
+	PyObject *list, *item, *subobj = Py_None, *qsubobj = Py_None;
 
-	if (!PyArg_ParseTuple(args, "|Oi:split", &subobj, &maxsplit))
+	if (!PyArg_ParseTuple(args, "|OiO:split", &subobj, &maxsplit, &qsubobj))
 		return NULL;
 	if (maxsplit < 0)
 		maxsplit = INT_MAX;
+	if (qsubobj != Py_None) {
+		if (PyString_Check(qsubobj)) {
+			qsub = PyString_AS_STRING(qsubobj);
+			qn = PyString_GET_SIZE(qsubobj);
+		}
+		if (qn == 0) {
+			PyErr_SetString(PyExc_ValueError, "empty delimiter");
+			return NULL;
+		}
+		if (subobj == Py_None)
+			return split_whitespace_quoted(s, len, maxsplit, qsub);
+	}		
 	if (subobj == Py_None)
 		return split_whitespace(s, len, maxsplit);
 	if (PyString_Check(subobj)) {
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-17 Thread Neal Norwitz
On 5/16/06, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:
> Nick Coghlan wrote:
> > Broadening the ifdef to cover all posix systems rather than just AIX
> > might be the right thing to do.
>
> As I said: I doubt it is the right thing to do. Instead, the lock
> should get released in the child process if it is held at the
> point of the fork.
>
> I agree that we should find a repeatable test case first.

I'm not sure if this bug can be reproduced now, but the comment in
warnings.py points to:

http://python.org/sf/683658

I didn't investigate it further.  That might allow someone to create a
reproducible test case.

n
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] New string method - splitquoted

2006-05-17 Thread Heiko Wundram
Am Donnerstag 18 Mai 2006 06:06 schrieb Dave Cinege:
> This is useful, but possibly better put into practice as a separate
> method??

I personally don't think it's particularily useful, at least not in the 
special case that your patch tries to address.

1) Generally, you won't only have one character that does quoting, but 
several. Think of the Python syntax, where you have ", ', """ and ''', which 
all behave slightly differently. The logic for " and ' is simple enough to 
implement (basically that's what your patch does, and I'm sure it's easy 
enough to extend it to accept a range of characters as splitters), but if you 
have more complicated quoting operators (such as """), are you sure it's 
sensible to implement the logic in split()?

2) What should the result of "this is a \"test string".split(None,-1,'"') be? 
An exception (ParseError)? Silently ignoring the missing delimiter, and 
returning ['this','is','a','test string']? Ignoring the delimiter altogether, 
returning ['this','is','a','"test','string']? I don't think there's one case 
to satisfy all here...

3) What about escapes of the delimiter? Your current patch doesn't address 
them at all (AFAICT) at the moment, but what should the escaping character 
be? Should "escape processing" take place, i.E. what should the result 
of "this is a \\\"delimiter \\test".split(None,-1,'"') be?

Don't get me wrong, I personally find this functionality very, very 
interesting (I'm +0.5 on adding it in some way or another), especially as a 
part of the standard library (not necessarily as an extension to .split()).

But there's quite a lot of semantic stuff to get right before you can 
implement it properly; see the complexity of the csv module, where you have 
to define pretty much all of this in the dialect you use to parse the csv 
file...

Why not write up a PEP?

--- Heiko.
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com