~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Modules/python.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Minimal main program -- everything is loaded from the library */
 
2
 
 
3
#include "Python.h"
 
4
#include <locale.h>
 
5
 
 
6
#ifdef __FreeBSD__
 
7
#include <floatingpoint.h>
 
8
#endif
 
9
 
 
10
#ifdef MS_WINDOWS
 
11
int
 
12
wmain(int argc, wchar_t **argv)
 
13
{
 
14
        return Py_Main(argc, argv);
 
15
}
 
16
#else
 
17
int
 
18
main(int argc, char **argv)
 
19
{
 
20
        wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
 
21
        /* We need a second copies, as Python might modify the first one. */
 
22
        wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
 
23
        int i, res;
 
24
        char *oldloc;
 
25
        /* 754 requires that FP exceptions run in "no stop" mode by default,
 
26
         * and until C vendors implement C99's ways to control FP exceptions,
 
27
         * Python requires non-stop mode.  Alas, some platforms enable FP
 
28
         * exceptions by default.  Here we disable them.
 
29
         */
 
30
#ifdef __FreeBSD__
 
31
        fp_except_t m;
 
32
 
 
33
        m = fpgetmask();
 
34
        fpsetmask(m & ~FP_X_OFL);
 
35
#endif
 
36
        if (!argv_copy || !argv_copy2) {
 
37
                fprintf(stderr, "out of memory\n");
 
38
                return 1;
 
39
        }
 
40
        oldloc = strdup(setlocale(LC_ALL, NULL));
 
41
        setlocale(LC_ALL, "");
 
42
        for (i = 0; i < argc; i++) {
 
43
#ifdef HAVE_BROKEN_MBSTOWCS
 
44
                /* Some platforms have a broken implementation of
 
45
                 * mbstowcs which does not count the characters that
 
46
                 * would result from conversion.  Use an upper bound.
 
47
                 */
 
48
                size_t argsize = strlen(argv[i]);
 
49
#else
 
50
                size_t argsize = mbstowcs(NULL, argv[i], 0);
 
51
#endif
 
52
                size_t count;
 
53
                if (argsize == (size_t)-1) {
 
54
                        fprintf(stderr, "Could not convert argument %d to string\n", i);
 
55
                        return 1;
 
56
                }
 
57
                argv_copy[i] = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
 
58
                argv_copy2[i] = argv_copy[i];
 
59
                if (!argv_copy[i]) {
 
60
                        fprintf(stderr, "out of memory\n");
 
61
                        return 1;
 
62
                }
 
63
                count = mbstowcs(argv_copy[i], argv[i], argsize+1);
 
64
                if (count == (size_t)-1) {
 
65
                        fprintf(stderr, "Could not convert argument %d to string\n", i);
 
66
                        return 1;
 
67
                }
 
68
        }
 
69
        setlocale(LC_ALL, oldloc);
 
70
        free(oldloc);
 
71
        res = Py_Main(argc, argv_copy);
 
72
        for (i = 0; i < argc; i++) {
 
73
                PyMem_Free(argv_copy2[i]);
 
74
        }
 
75
        PyMem_Free(argv_copy);
 
76
        PyMem_Free(argv_copy2);
 
77
        return res;
 
78
}
 
79
#endif