[issue1261] PEP 3137: make bytesobject.c methods

2007-10-16 Thread Gregory P. Smith

Gregory P. Smith added the comment:

Committed revision 58493

--
resolution:  - accepted
status: open - closed

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1261
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1285] setp.py error The process cannot access the file ...

2007-10-16 Thread Ulrich Weber

New submission from Ulrich Weber:

When creating a windows installer using setup.py an error message is 
displayed: error: c:\docume~1\uweber\loca1s~1\temp\tmppnj1gz .zip: The 
process cannot access the file because it is being used by another 
process. The installer is created nevertheless and seems to work fine. 
I am using Python 2.5.1 on WinXP SP2.

--
components: Distutils
messages: 56496
nosy: uweber4711
severity: normal
status: open
title: setp.py error The process cannot access the file ...
type: behavior

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1285
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol

2007-10-16 Thread Yitz Gale

New submission from Yitz Gale:

The standard idiom for opening a file is now with open...
So I think it should be a goal that this should work with
any built-in file-like object that needs to be closed,
without having to explicitly wrap it in closing().
It certainly should work for fileinput and StringIO - since
these really are files, in some sense.

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1259] string find and rfind methods give a TypeError that is misleading

2007-10-16 Thread Wummel

Wummel added the comment:

I also hit this bug. The .index() methods have the same issue,
as well as the methods in the string and strop modules:

 123.index(2, None)
Traceback (most recent call last):
  File stdin, line 1, in ?
TypeError: slice indices must be integers or None
 import strop, string
 strop.rfind(123, 2, None)
Traceback (most recent call last):
  File stdin, line 1, in ?
TypeError: an integer is required
 string.rfind(123, 2, None)
Traceback (most recent call last):
  File stdin, line 1, in ?
  File /usr/lib/python2.4/string.py, line 374, in rfind
return s.rfind(*args)
TypeError: slice indices must be integers or None


--
nosy: +calvin

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1259
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1287] os.environ.pop doesn't work

2007-10-16 Thread Gustavo Niemeyer

New submission from Gustavo Niemeyer:

 import os
 os.system(echo $ASD)

0
 os.environ[ASD] = asd
 os.system(echo $ASD)
asd
0
 os.environ.pop(ASD)
'asd'
 os.system(echo $ASD)
asd
0

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1287
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1258] Removal of basestring type

2007-10-16 Thread Guido van Rossum

Guido van Rossum added the comment:

Committed revision 58495.

Thanks Christian!!!

--
resolution:  - accepted
status: open - closed

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1258
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1287] os.environ.pop doesn't work

2007-10-16 Thread Gustavo Niemeyer

Changes by Gustavo Niemeyer:


--
nosy: niemeyer
severity: normal
status: open
title: os.environ.pop doesn't work
versions: Python 2.1.1, Python 2.1.2, Python 2.2, Python 2.2.1, Python 2.2.2, 
Python 2.2.3, Python 2.3, Python 2.4, Python 2.5, Python 2.6, Python 3.0

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1287
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1267] Py3K cannot run as ``python -S``

2007-10-16 Thread Christian Heimes

Christian Heimes added the comment:

Christian Heimes wrote:
  * removed unused import of open in initstdio()
  * fixed infinite loop in PyTokenizer_FindEncoding() by checking
 tok-done == E_OK

I found another bug in Python/import.c:call_find_method. The function
mustn't set an encoding of ftp-mode contains 'b' for binary.

if (strchr(fdp-mode, 'b') == NULL) {
/* Python text file, get encoding from tokenizer */
encoding = PyTokenizer_FindEncoding(fp);
encoding = (encoding != NULL) ? encoding :
   PyUnicode_GetDefaultEncoding();
}

Christian

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1267
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1267] Py3K cannot run as ``python -S``

2007-10-16 Thread Guido van Rossum

Guido van Rossum added the comment:

Does this mean I should hold off reviewing the patch?

On 10/16/07, Christian Heimes [EMAIL PROTECTED] wrote:

 Christian Heimes added the comment:

 Christian Heimes wrote:
   * removed unused import of open in initstdio()
   * fixed infinite loop in PyTokenizer_FindEncoding() by checking
  tok-done == E_OK

 I found another bug in Python/import.c:call_find_method. The function
 mustn't set an encoding of ftp-mode contains 'b' for binary.

 if (strchr(fdp-mode, 'b') == NULL) {
 /* Python text file, get encoding from tokenizer */
 encoding = PyTokenizer_FindEncoding(fp);
 encoding = (encoding != NULL) ? encoding :
PyUnicode_GetDefaultEncoding();
 }

 Christian

 __
 Tracker [EMAIL PROTECTED]
 http://bugs.python.org/issue1267
 __


__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1267
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1267] Py3K cannot run as ``python -S``

2007-10-16 Thread Christian Heimes

Christian Heimes added the comment:

Update since last patch

 * removed unnecessary const from const char*
PyTokenizer_FindEncoding(FILE *fp)
 * Fixed bug in find_module whith binary files

Please review the patch.

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1267
__Index: Python/pythonrun.c
===
--- Python/pythonrun.c	(revision 58495)
+++ Python/pythonrun.c	(working copy)
@@ -51,6 +51,7 @@
 /* Forward */
 static void initmain(void);
 static void initsite(void);
+static int initstdio(void);
 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
 			  PyCompilerFlags *, PyArena *);
 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
@@ -241,6 +242,9 @@
 		initsigs(); /* Signal handling stuff, including initintr() */
 
 	initmain(); /* Module __main__ */
+	if (initstdio()  0)
+		Py_FatalError(
+		Py_Initialize: can't initialize sys standard streams);
 	if (!Py_NoSiteFlag)
 		initsite(); /* Module site */
 
@@ -676,6 +680,81 @@
 	}
 }
 
+/* Initialize sys.stdin, stdout, stderr and __builtin__.open */
+static int
+initstdio(void)
+{
+	PyObject *iomod=NULL, *wrapper;
+	PyObject *bimod=NULL;
+	PyObject *m;
+	PyObject *std=NULL;
+	int status = 0;
+
+	/* Hack to avoid a nasty recursion issue when Python is invoked
+	   in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
+	if ((m = PyImport_ImportModule(encodings.utf_8)) == NULL) {
+		goto error;
+	}
+	Py_DECREF(m);
+	
+	if (!(m = PyImport_ImportModule(encodings.latin_1))) {
+		goto error;
+	}
+	Py_DECREF(m);
+
+	if (!(bimod = PyImport_ImportModule(__builtin__))) {
+		goto error;
+	}
+
+	if (!(iomod = PyImport_ImportModule(io))) {
+		goto error;
+	}
+	if (!(wrapper = PyObject_GetAttrString(iomod, OpenWrapper))) {
+		goto error;
+	}
+
+	/* Set __builtin__.open */
+	if (PyObject_SetAttrString(bimod, open, wrapper) == -1) {
+		goto error;
+	}
+	
+	/* Set sys.stdin */
+	if (!(std = PyFile_FromFileEx(stdin, stdin, r, fclose, -1,
+  NULL, \n))) {
+		goto error;
+	}
+	PySys_SetObject(__stdin__, std);
+	PySys_SetObject(stdin, std);
+	Py_DECREF(std);
+
+	/* Set sys.stdout */
+	if (!(std = PyFile_FromFileEx(stdout, stdout, w, fclose, -1,
+  NULL, \n))) {
+goto error;
+}
+	PySys_SetObject(__stdout__, std);
+	PySys_SetObject(stdout, std);
+	Py_DECREF(std);
+
+	/* Set sys.stderr */
+	if (!(std = PyFile_FromFileEx(stderr, stderr, w, fclose, -1,
+  NULL, \n))) {
+goto error;
+}
+PySys_SetObject(__stderr__, std);
+	PySys_SetObject(stderr, std);
+	Py_DECREF(std);
+
+if (0) {
+  error:
+status = -1;
+}
+
+	Py_XDECREF(bimod);
+	Py_XDECREF(iomod);
+	return status;
+}
+
 /* Parse input from a file and execute it */
 
 int
@@ -1146,10 +1225,10 @@
 	int err = 0;
 	PyObject *f = PySys_GetObject(stderr);
 	Py_INCREF(value);
-	if (f == NULL)
+	if (f == NULL) {
 		_PyObject_Dump(value);
-	if (f == NULL)
 		fprintf(stderr, lost sys.stderr\n);
+	}
 	else {
 		fflush(stdout);
 		if (tb  tb != Py_None)
@@ -1589,6 +1668,9 @@
 Py_FatalError(const char *msg)
 {
 	fprintf(stderr, Fatal Python error: %s\n, msg);
+	if (PyErr_Occurred()) {
+		PyErr_Print();
+	}
 #ifdef MS_WINDOWS
 	OutputDebugString(Fatal Python error: );
 	OutputDebugString(msg);
Index: Python/import.c
===
--- Python/import.c	(revision 58495)
+++ Python/import.c	(working copy)
@@ -91,6 +91,9 @@
 /* This table is defined in config.c: */
 extern struct _inittab _PyImport_Inittab[];
 
+/* Method from Parser/tokenizer.c */
+extern char * PyTokenizer_FindEncoding(FILE *fp);
+
 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
 
 /* these tables define the module suffixes that Python recognizes */
@@ -2558,6 +2561,7 @@
 	struct filedescr *fdp;
 	char pathname[MAXPATHLEN+1];
 	FILE *fp = NULL;
+	char *encoding = NULL;
 
 	pathname[0] = '\0';
 	if (path == Py_None)
@@ -2566,7 +2570,14 @@
 	if (fdp == NULL)
 		return NULL;
 	if (fp != NULL) {
-		fob = PyFile_FromFile(fp, pathname, fdp-mode, fclose);
+		if (strchr(fdp-mode, 'b') == NULL) {
+			/* Python text file, get encoding from tokenizer */
+			encoding = PyTokenizer_FindEncoding(fp);
+			encoding = (encoding != NULL) ? encoding :
+   (char*)PyUnicode_GetDefaultEncoding();
+		}
+		fob = PyFile_FromFileEx(fp, pathname, fdp-mode, fclose, -1,
+	(char*)encoding, NULL);
 		if (fob == NULL) {
 			fclose(fp);
 			return NULL;
Index: Include/fileobject.h
===
--- Include/fileobject.h	(revision 58495)
+++ Include/fileobject.h	(working copy)
@@ -9,6 +9,9 @@
 #define PY_STDIOTEXTMODE b
 
 PyAPI_FUNC(PyObject *) PyFile_FromFile(FILE *, char *, char *, int (*)(FILE*));
+PyAPI_FUNC(PyObject *) PyFile_FromFileEx(FILE *, char *, char *,
+	 int (*)(FILE *), int, char *,

[issue1287] os.environ.pop doesn't work

2007-10-16 Thread Raghuram Devarakonda

Raghuram Devarakonda added the comment:

The following patch solves the problem (with the latest from trunk). I
only tested on Linux. I couldn't reproduce the problem with latest py3k
(again, on Linux).

===
--- Lib/os.py   (revision 58221)
+++ Lib/os.py   (working copy)
@@ -446,6 +446,9 @@
 def __delitem__(self, key):
 unsetenv(key)
 del self.data[key.upper()]
+def pop(self, key):
+unsetenv(key)
+return UserDict.IterableUserDict.pop(self, key)
 def clear(self):
 for key in self.data.keys():
 unsetenv(key)
@@ -513,6 +516,9 @@
 del self.data[key]
 def copy(self):
 return dict(self)
+def pop(self, key):
+unsetenv(key)
+return UserDict.IterableUserDict.pop(self, key)
 
 
 environ = _Environ(environ)

--
nosy: +draghuram

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1287
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1205] urllib fail to read URL contents, urllib2 crash Python

2007-10-16 Thread Michael Torrie

Michael Torrie added the comment:

I had a situation where I was talking to a Sharp MFD printer.  Their web
server apparently does not serve chunked data properly.  However the
patch posted here put it in an infinite loop.

Somewhere around line 525 in the python 2.4 version of httplib.py, I had
to make it look like this:

while True:
line = self.fp.readline()
if line == '\r\n' or not line:
break

I added or not line to the if statement.  The blank line in the
chunked http was confusing the _last_chunk thing, but even when it was
set to zero, since there was no more data, this loop to eat up crlfs was
never ending.

Is this really a proper fix?  

I'm in favor of changing urllib2 to be less strict because, despite the
RFCs, we're stuck talking to all kinds of web servers (embedded ones in
particular) that simply can't easily be changed.

--
nosy: +torriem

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1205
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1288] dict.fromkeys - Odd logic when passing second dict.fromkeys as value

2007-10-16 Thread Adam Doherty

New submission from Adam Doherty:

Hello:

I'm am trying to conduct some tests on a list of data that checks for
the position of values in list elements using the bisect module.  To
store the results of these tests for output to a template I have build a
dictionary with 47 keys the values of which are dictionaries themselves.
 These inner dictionaries contain 7 keys that initially are valued at
zero.  Looping through the data in my list I check for values from 1 to
47 and if I find the value I am looking for I lookup it's position in
the row using the bisect module.  Using the current value I am looking
for and the position returned from bisect I increase the value in the
matching dictionary key value position by 1. Now for speed I have built
the dictionary using d =
dict.fromkeys(xrange(1,48),dict.fromkeys(xrange(1,8),0)) as this gives
the desired result.  Unfortunately when I run my test for values each
value in the dictionary is listed as the total number of rows in the
data list.  This is not the desired result which is correctly achieved
using:
d = {}
for x in xrange(1,48):
d[x] = dict.fromkeys(xrange(1,8),0)

I have included output from IDLE to demonstrate the problem.

--
components: Interpreter Core
files: problem-report.txt
messages: 56507
nosy: dohertywa
severity: normal
status: open
title: dict.fromkeys - Odd logic when passing second dict.fromkeys as value
type: behavior
versions: Python 2.3, Python 2.4, Python 2.5

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1288
__ from bisect import bisect as bs
 from pysqlite2 import dbapi2 as sqlite
 conn = sqlite.connect(/home/dohertywa/testdata.db)
 data = [row for row in conn.execute(SELECT f1,f2,f3,f4,f5,f6,f7 FROM 
 testtable WHERE strftime('%m', testdate) = strftime('%m', '0001-10-01'))]
 conn.close()
 data
[(4, 6, 7, 10, 19, 39, 43), (2, 5, 30, 32, 36, 37, 42), (9, 14, 22, 29, 43, 44, 
46), (1, 13, 15, 18, 27, 32, 40), (2, 7, 19, 28, 31, 38, 45), (1, 2, 27, 33, 
34, 42, 45), (2, 11, 22, 23, 33, 45, 46), (10, 11, 19, 20, 31, 40, 44), (5, 10, 
26, 33, 42, 44, 45), (1, 11, 32, 33, 37, 45, 46), (8, 10, 12, 13, 19, 32, 42), 
(8, 13, 18, 21, 24, 25, 36), (8, 12, 17, 18, 28, 29, 35), (5, 7, 15, 24, 26, 
38, 43), (17, 21, 23, 25, 26, 33, 47), (16, 19, 27, 34, 44, 45, 46), (10, 22, 
31, 35, 40, 43, 46), (7, 15, 23, 25, 27, 37, 40), (1, 7, 19, 23, 35, 37, 45), 
(2, 5, 8, 11, 16, 20, 34), (7, 9, 16, 21, 27, 35, 45), (6, 14, 16, 20, 21, 23, 
37), (3, 10, 16, 18, 21, 37, 47), (8, 19, 20, 23, 27, 40, 44), (5, 9, 10, 14, 
32, 33, 39), (3, 4, 6, 13, 20, 34, 43), (25, 28, 29, 31, 32, 36, 45), (1, 2, 4, 
12, 20, 34, 36), (6, 10, 13, 25, 27, 40, 43), (6, 14, 25, 29, 30, 36, 40), (3, 
4, 5, 6, 41, 42, 45), (6, 19, 24, 25, 32, 34, 43), (5, 9, 10, 18, 20, 23, 46), 
(2, 3, 7, 10, 20, 24, 33), (8, 15, 16, 17, 22!
 , 32, 39), (7, 9, 12, 22, 24, 40, 41), (3, 11, 16, 21, 36, 43, 44), (2, 16, 
17, 31, 34, 36, 38), (10, 16, 18, 23, 35, 36, 46), (3, 14, 25, 31, 32, 44, 45), 
(3, 15, 23, 37, 38, 39, 43), (12, 22, 34, 37, 39, 43, 47), (3, 11, 23, 26, 27, 
28, 31), (1, 16, 18, 21, 31, 40, 42), (7, 24, 26, 29, 34, 42, 47), (3, 4, 9, 
18, 29, 34, 44), (3, 7, 14, 15, 20, 30, 46), (1, 8, 13, 23, 29, 31, 41), (14, 
15, 21, 34, 36, 42, 44), (21, 23, 25, 26, 39, 41, 45), (8, 10, 18, 19, 26, 35, 
42), (10, 15, 25, 30, 33, 40, 44), (2, 21, 23, 27, 29, 33, 41), (10, 16, 23, 
24, 27, 33, 46), (9, 15, 33, 34, 38, 41, 44), (3, 5, 13, 19, 26, 35, 41), (8, 
21, 29, 35, 38, 41, 46), (5, 20, 21, 23, 32, 40, 42), (3, 7, 27, 29, 38, 40, 
43)]
 dp = dict.fromkeys(xrange(1,48),dict.fromkeys(xrange(1,8),0)) #a dictionary 
 whose values are dictionaries whose values are 0.
 dp
{1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 
0, 6: 0, 7: 0}, 3: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 4: {1: 0, 2: 0, 
3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 5: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 
6: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 7: {1: 0, 2: 0, 3: 0, 4: 0, 5: 
0, 6: 0, 7: 0}, 8: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 9: {1: 0, 2: 0, 
3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 10: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 
11: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 12: {1: 0, 2: 0, 3: 0, 4: 0, 5: 
0, 6: 0, 7: 0}, 13: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 14: {1: 0, 2: 
0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 15: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 
0}, 16: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 17: {1: 0, 2: 0, 3: 0, 4: 
0, 5: 0, 6: 0, 7: 0}, 18: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 19: {1: 
0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 20: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 
0, 7: 0}, 21: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6:!
  0, 7: 0}, 22: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 23: {1: 0, 2: 0, 3: 
0, 4: 0, 5: 0, 6: 0, 7: 0}, 24: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 25: 
{1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}, 26: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 
6: 0, 7: 0}, 27: 

[issue1288] dict.fromkeys - Odd logic when passing second dict.fromkeys as value

2007-10-16 Thread Raymond Hettinger

Raymond Hettinger added the comment:

This isn't a bug.  Writing
dict.fromkeys(xrange(1,48),dict.fromkeys(xrange(1,8),0)) results in
the inner expression being evaluated just once and then passed to the
outer function call as a fully evaluated argument.  As a result, the
*same* dictionary is being used over and over again.

People commonly encounter similar issue when they try to create
initialized list-of-lists with something like s=[[0]*10] which repeats
ten of the *same* lists.  Instead they should write something like: 
s=[[0] for i in range(10)] which creates *distinct* inner lists.

For you application, consider using a defaultdict which can call a
function as needed to create new, distinct values:

  d = defaultdict(lambda: dict.fromkeys(xrange(1,8), 0))

--
nosy: +rhettinger
resolution:  - invalid
status: open - closed

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1288
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1631171] implement warnings module in C

2007-10-16 Thread Brett Cannon

Brett Cannon added the comment:

Attached is a new version of _warnings.c that checks to see if
'warnings' has been imported, and if so, uses the attributes from that
module for onceregistry and 'filters'.  I did it in such a way so that
'warnings' is in no way required nor imported through checking.

If Neal does a code review and OKs the approach then it can also be
implemented for showwarning() which should make testing test_warnings
possible.  =)  Then we can start testing that changes to the module
attributes actually affect things properly.

--
assignee: brett.cannon - nnorwitz

_
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1631171
_#include Python.h
#include frameobject.h

#define MODULE_NAME _warnings
#define DEFAULT_ACTION_NAME default_action

PyDoc_STRVAR(warnings__doc__,
MODULE_NAME  provides basic warning filtering support.\n
It is a helper module to speed up interpreter start-up.);

/* Both 'filters' and 'onceregistry' can be set in warnings.py;
   get_warnings_attr() will reset these variables accordingly. */
static PyObject *_filters;  /* List */
static PyObject *_once_registry;  /* Dict */


static int
check_matched(PyObject *obj, PyObject *arg)
{
PyObject *result;
int rc;

if (obj == Py_None)
return 1;
result = PyObject_CallMethod(obj, match, O, arg);
if (result == NULL)
return -1;

rc = PyObject_IsTrue(result);
Py_DECREF(result);
return rc;
}

/*
   Returns a new reference.
   A NULL return value can mean false or an error.
*/
static PyObject *
get_warnings_attr(const char *attr)
{
static PyObject *warnings_str = NULL;
PyObject *all_modules;
PyObject *warnings_module;
int result;

if (warnings_str == NULL) {
warnings_str = PyString_FromString(warnings);
if (warnings_str == NULL)
return NULL;
}

all_modules = PyImport_GetModuleDict();
result = PyDict_Contains(all_modules, warnings_str);
if (result == -1 || result == 0)
return NULL;

warnings_module = PyDict_GetItem(all_modules, warnings_str);
if (!PyObject_HasAttrString(warnings_module, attr))
return NULL;
return PyObject_GetAttrString(warnings_module, attr);
}


PyObject *
get_once_registry(void)
{
PyObject *registry;

/* XXX(brett.cannon): Test detection of warnings.py change. */
registry = get_warnings_attr(onceregistry);
if (registry == NULL) {
if (PyErr_Occurred())
return NULL;
return _once_registry;
}
Py_DECREF(_once_registry);
_once_registry = registry;
return registry;
}


/* The item is a borrowed reference. */
static const char *
get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
   PyObject *module, PyObject **item)
{
PyObject *action, *m, *d;
Py_ssize_t i;
PyObject *warnings_filters;

/* XXX(brett.cannon): Test warning.py changes. */
warnings_filters = get_warnings_attr(filters);
if (warnings_filters == NULL) {
if (PyErr_Occurred())
return NULL;
}
else {
Py_DECREF(_filters);
_filters = warnings_filters;
}

if (!PyList_Check(_filters)) {
PyErr_SetString(PyExc_ValueError,
MODULE_NAME .filters must be a list);
return NULL;
}

/* _filters could change while we are iterating over it. */
for (i = 0; i  PyList_GET_SIZE(_filters); i++) {
PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
Py_ssize_t ln;
int is_subclass, good_msg, good_mod;

tmp_item = *item = PyList_GET_ITEM(_filters, i);
if (PyTuple_Size(tmp_item) != 5) {
PyErr_Format(PyExc_ValueError,
 MODULE_NAME .filters item %zd isn't a 5-tuple, i);
return NULL;
}

/* Python code: action, msg, cat, mod, ln = item */
action = PyTuple_GET_ITEM(tmp_item, 0);
msg = PyTuple_GET_ITEM(tmp_item, 1);
cat = PyTuple_GET_ITEM(tmp_item, 2);
mod = PyTuple_GET_ITEM(tmp_item, 3);
ln_obj = PyTuple_GET_ITEM(tmp_item, 4);

good_msg = check_matched(msg, text);
good_mod = check_matched(mod, module);
is_subclass = PyObject_IsSubclass(category, cat);
ln = PyInt_AsSsize_t(ln_obj);
if (good_msg == -1 || good_mod == -1 || is_subclass == -1 ||
(ln == -1  PyErr_Occurred()))
return NULL;

if (good_msg  is_subclass  good_mod  (ln == 0 || lineno == ln))
return PyString_AsString(action);
}

m = PyImport_ImportModule(MODULE_NAME);
if (m == NULL)
return NULL;
d = PyModule_GetDict(m);
Py_DECREF(m);
if (d == NULL)
return NULL;
action = PyDict_GetItemString(d, DEFAULT_ACTION_NAME);
if (action != NULL)
return PyString_AsString(action);

PyErr_SetString(PyExc_ValueError,