From 07c6e7168919c275e47fa35c741413270d3d80fd Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 24 Aug 2012 13:05:09 -0400 Subject: Issue #15778: Coerce ImportError.args to a string when it isn't already one. Patch by Dave Malcolm. --- Lib/test/test_exceptions.py | 5 +++++ Misc/NEWS | 3 +++ Objects/exceptions.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 0b1fd1b..55e9db3 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -937,6 +937,11 @@ class ImportErrorTests(unittest.TestCase): self.assertEqual(exc.name, 'somename') self.assertEqual(exc.path, 'somepath') + def test_non_str_argument(self): + # Issue #15778 + arg = b'abc' + exc = ImportError(arg) + self.assertEqual(str(arg), str(exc)) def test_main(): diff --git a/Misc/NEWS b/Misc/NEWS index 57f022c..e772a70 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.3.0 Release Candidate 1? Core and Builtins ----------------- +- Issue #15778: ensure that str(ImportError(msg)) returns a str + even when msg isn't a str. + - Issue #2051: Source file permission bits are once again correctly copied to the cached bytecode file. (The migration to importlib reintroduced this problem because these was no regression test. A test diff --git a/Objects/exceptions.c b/Objects/exceptions.c index b7e11f8..74bb262 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -679,7 +679,7 @@ ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg) static PyObject * ImportError_str(PyImportErrorObject *self) { - if (self->msg) { + if (self->msg && PyUnicode_CheckExact(self->msg)) { Py_INCREF(self->msg); return self->msg; } -- cgit v0.12 git/commit/Python/dup2.c?id=9925e70e4811841556747a77acd89c1a70bf344a'>commitdiffstats
path: root/Python/dup2.c
blob: 7c6bbfce11dbf80dae7fe4c79d4c52839a0c68ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * Public domain dup2() lookalike
 * by Curtis Jackson @ AT&T Technologies, Burlington, NC
 * electronic address:  burl!rcj
 *
 * dup2 performs the following functions:
 *
 * Check to make sure that fd1 is a valid open file descriptor.
 * Check to see if fd2 is already open; if so, close it.
 * Duplicate fd1 onto fd2; checking to make sure fd2 is a valid fd.
 * Return fd2 if all went well; return BADEXIT otherwise.
 */

#include <fcntl.h>
#include <unistd.h>

#define BADEXIT -1

int
dup2(int fd1, int fd2)
{
    if (fd1 != fd2) {
        if (fcntl(fd1, F_GETFL) < 0)
            return BADEXIT;
        if (fcntl(fd2, F_GETFL) >= 0)
            close(fd2);
        if (fcntl(fd1, F_DUPFD, fd2) < 0)
            return BADEXIT;
    }
    return fd2;
}