blob: 0043d8530b501ce7de75f533f5b448ef456e2bd0 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +00004#include "osdefs.h"
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00005#include "import.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00006
Antoine Pitrou5651eaa2008-09-06 20:46:58 +00007#include <locale.h>
8
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00009#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +000010#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000011#endif
12
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000013#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Martin v. Löwis945362c2007-08-30 14:57:25 +000014#include <windows.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000015#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000016#include <fcntl.h>
Martin v. Löwis790465f2008-04-05 20:41:37 +000017#define PATH_MAX MAXPATHLEN
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000018#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000019#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000020
Martin v. Löwis945362c2007-08-30 14:57:25 +000021#ifdef _MSC_VER
22#include <crtdbg.h>
23#endif
24
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000025#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000026#define PYTHONHOMEHELP "<prefix>\\lib"
27#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000028#if defined(PYOS_OS2) && defined(PYCC_GCC)
29#define PYTHONHOMEHELP "<prefix>/Lib"
30#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000031#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000032#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000033#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000034
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000035#include "pygetopt.h"
36
Guido van Rossuma22865e2000-09-05 04:41:18 +000037#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000038 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
39 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000040
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041#ifdef __cplusplus
42extern "C" {
43#endif
44
Guido van Rossumac56b031996-07-21 02:33:38 +000045/* For Py_GetArgcArgv(); set by main() */
Martin v. Löwis790465f2008-04-05 20:41:37 +000046static wchar_t **orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +000047static int orig_argc;
48
Guido van Rossumbceccf52001-04-10 22:07:43 +000049/* command line options */
Christian Heimes8dc226f2008-05-06 23:45:46 +000050#define BASE_OPTS L"bBc:dEhiJm:OsStuvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000051
Guido van Rossumbceccf52001-04-10 22:07:43 +000052#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000053
Guido van Rossum667d7041995-08-04 04:20:48 +000054/* Short usage message (with %s for argv0) */
55static char *usage_line =
Martin v. Löwis790465f2008-04-05 20:41:37 +000056"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000057
58/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000059static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000060Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000061-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
62 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Christian Heimes790c8232008-01-07 21:14:23 +000063-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000064-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000065-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000066-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000068";
69static char *usage_2 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000070-i : inspect interactively after running script; forces a prompt even\n\
71 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000072-m mod : run library module as a script (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000073-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000074-OO : remove doc-strings in addition to the -O optimizations\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +000075-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000076-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000077";
78static char *usage_3 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000079-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000080 see man page for details on internal buffering relating to '-u'\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000081-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
82 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000084-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +000085 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000086-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000087";
Guido van Rossum393661d2001-08-31 17:40:15 +000088static char *usage_4 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000089file : program read from script file\n\
90- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000091arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000092Other environment variables:\n\
93PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000094PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000095 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +000096";
Victor Stinner9802b392010-08-19 11:36:43 +000097static char *usage_5 =
98"PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n"
99" The default module search path uses %s.\n"
100"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
101"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
102#if !(defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)) && !defined(__APPLE__)
103"PYTHONFSENCODING: Encoding used for the filesystem.\n"
104#endif
105;
Guido van Rossum667d7041995-08-04 04:20:48 +0000106
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000107FILE *
108_Py_wfopen(const wchar_t *path, const wchar_t *mode)
Martin v. Löwis790465f2008-04-05 20:41:37 +0000109{
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000110#ifndef MS_WINDOWS
Victor Stinnerf2e08b32010-08-13 23:29:08 +0000111 FILE *f;
112 char *cpath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 char cmode[10];
114 size_t r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 r = wcstombs(cmode, mode, 10);
116 if (r == (size_t)-1 || r >= 10) {
117 errno = EINVAL;
118 return NULL;
119 }
Victor Stinnerf2e08b32010-08-13 23:29:08 +0000120 cpath = _Py_wchar2char(path);
121 if (cpath == NULL)
122 return NULL;
123 f = fopen(cpath, cmode);
124 PyMem_Free(cpath);
125 return f;
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000126#else
127 return _wfopen(path, mode);
Martin v. Löwis790465f2008-04-05 20:41:37 +0000128#endif
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000129}
Martin v. Löwis790465f2008-04-05 20:41:37 +0000130
Guido van Rossum667d7041995-08-04 04:20:48 +0000131
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000132static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000133usage(int exitcode, wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 FILE *f = exitcode ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 fprintf(f, usage_line, program);
138 if (exitcode)
139 fprintf(f, "Try `python -h' for more information.\n");
140 else {
141 fputs(usage_1, f);
142 fputs(usage_2, f);
143 fputs(usage_3, f);
144 fprintf(f, usage_4, DELIM);
145 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
146 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000147#if defined(__VMS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (exitcode == 0) {
149 /* suppress 'error' message */
150 return 1;
151 }
152 else {
153 /* STS$M_INHIB_MSG + SS$_ABORT */
154 return 0x1000002c;
155 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000156#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000158#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 /*NOTREACHED*/
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000160}
161
Martin v. Löwis6caea372003-11-18 19:46:25 +0000162static void RunStartupFile(PyCompilerFlags *cf)
163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 char *startup = Py_GETENV("PYTHONSTARTUP");
165 if (startup != NULL && startup[0] != '\0') {
166 FILE *fp = fopen(startup, "r");
167 if (fp != NULL) {
168 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
169 PyErr_Clear();
170 fclose(fp);
171 } else {
172 int save_errno;
173
174 save_errno = errno;
175 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
176 errno = save_errno;
177 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
178 startup);
179 PyErr_Print();
180 PyErr_Clear();
181 }
182 }
Martin v. Löwis6caea372003-11-18 19:46:25 +0000183}
184
Thomas Woutersa9773292006-04-21 09:43:23 +0000185
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000186static int RunModule(wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyObject *module, *runpy, *runmodule, *runargs, *result;
189 runpy = PyImport_ImportModule("runpy");
190 if (runpy == NULL) {
191 fprintf(stderr, "Could not import runpy module\n");
192 return -1;
193 }
194 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
195 if (runmodule == NULL) {
196 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
197 Py_DECREF(runpy);
198 return -1;
199 }
200 module = PyUnicode_FromWideChar(modname, wcslen(modname));
201 if (module == NULL) {
202 fprintf(stderr, "Could not convert module name to unicode\n");
203 Py_DECREF(runpy);
204 Py_DECREF(runmodule);
205 return -1;
206 }
207 runargs = Py_BuildValue("(Oi)", module, set_argv0);
208 if (runargs == NULL) {
209 fprintf(stderr,
210 "Could not create arguments for runpy._run_module_as_main\n");
211 Py_DECREF(runpy);
212 Py_DECREF(runmodule);
213 Py_DECREF(module);
214 return -1;
215 }
216 result = PyObject_Call(runmodule, runargs, NULL);
217 if (result == NULL) {
218 PyErr_Print();
219 }
220 Py_DECREF(runpy);
221 Py_DECREF(runmodule);
222 Py_DECREF(module);
223 Py_DECREF(runargs);
224 if (result == NULL) {
225 return -1;
226 }
227 Py_DECREF(result);
228 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000229}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000230
Martin v. Löwis790465f2008-04-05 20:41:37 +0000231static int RunMainFromImporter(wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 PyObject *argv0 = NULL, *importer = NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 if ((argv0 = PyUnicode_FromWideChar(filename,wcslen(filename))) &&
236 (importer = PyImport_GetImporter(argv0)) &&
237 (importer->ob_type != &PyNullImporter_Type))
238 {
239 /* argv0 is usable as an import source, so
240 put it in sys.path[0] and import __main__ */
241 PyObject *sys_path = NULL;
242 if ((sys_path = PySys_GetObject("path")) &&
243 !PyList_SetItem(sys_path, 0, argv0))
244 {
245 Py_INCREF(argv0);
246 Py_DECREF(importer);
247 sys_path = NULL;
248 return RunModule(L"__main__", 0) != 0;
Guido van Rossum74c29c72007-11-19 18:36:41 +0000249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 }
251 Py_XDECREF(argv0);
252 Py_XDECREF(importer);
253 if (PyErr_Occurred()) {
254 PyErr_Print();
255 return 1;
256 }
257 else {
258 return -1;
259 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000260}
261
Victor Stinnera62207c2010-08-07 10:57:17 +0000262static int
263run_command(wchar_t *command, PyCompilerFlags *cf)
264{
265 PyObject *unicode, *bytes;
266 int ret;
267
268 unicode = PyUnicode_FromWideChar(command, -1);
269 if (unicode == NULL)
270 goto error;
271 bytes = PyUnicode_AsUTF8String(unicode);
272 Py_DECREF(unicode);
273 if (bytes == NULL)
274 goto error;
275 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
276 Py_DECREF(bytes);
277 return ret != 0;
278
279error:
Victor Stinner398356b2010-08-18 22:23:22 +0000280 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000281 PyErr_Print();
282 return 1;
283}
284
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000285static int
286run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf)
287{
288 PyObject *unicode, *bytes = NULL;
289 char *filename_str;
290 int run;
291
292 /* call pending calls like signal handlers (SIGINT) */
293 if (Py_MakePendingCalls() == -1) {
294 PyErr_Print();
295 return 1;
296 }
297
298 if (filename) {
299 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
300 if (unicode != NULL) {
301 bytes = PyUnicode_AsUTF8String(unicode);
302 Py_DECREF(unicode);
303 }
304 if (bytes != NULL)
305 filename_str = PyBytes_AsString(bytes);
306 else {
307 PyErr_Clear();
308 filename_str = "<decoding error>";
309 }
310 }
311 else
312 filename_str = "<stdin>";
313
314 run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf);
315 Py_XDECREF(bytes);
316 return run != 0;
317}
318
Christian Heimes9cd17752007-11-18 19:35:23 +0000319
Guido van Rossum667d7041995-08-04 04:20:48 +0000320/* Main program */
321
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000322int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000323Py_Main(int argc, wchar_t **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 int c;
326 int sts;
327 wchar_t *command = NULL;
328 wchar_t *filename = NULL;
329 wchar_t *module = NULL;
330 FILE *fp = stdin;
331 char *p;
Philip Jenveye53de3d2010-04-14 03:01:39 +0000332#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 wchar_t *wp;
Philip Jenveye53de3d2010-04-14 03:01:39 +0000334#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 int skipfirstline = 0;
336 int stdin_is_interactive = 0;
337 int help = 0;
338 int version = 0;
339 int saw_unbuffered_flag = 0;
340 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 cf.cf_flags = 0;
Guido van Rossum393661d2001-08-31 17:40:15 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 orig_argc = argc; /* For Py_GetArgcArgv() */
345 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 PySys_ResetWarnOptions();
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
350 if (c == 'c') {
351 size_t len;
352 /* -c is the last option; following arguments
353 that look like options are left for the
354 command to interpret. */
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 len = wcslen(_PyOS_optarg) + 1 + 1;
357 command = (wchar_t *)malloc(sizeof(wchar_t) * len);
358 if (command == NULL)
359 Py_FatalError(
360 "not enough memory to copy -c argument");
361 wcscpy(command, _PyOS_optarg);
362 command[len - 2] = '\n';
363 command[len - 1] = 0;
364 break;
365 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (c == 'm') {
368 /* -m is the last option; following arguments
369 that look like options are left for the
370 module to interpret. */
371 module = _PyOS_optarg;
372 break;
373 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 switch (c) {
376 case 'b':
377 Py_BytesWarningFlag++;
378 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 case 'd':
381 Py_DebugFlag++;
382 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 case 'i':
385 Py_InspectFlag++;
386 Py_InteractiveFlag++;
387 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 case 'O':
392 Py_OptimizeFlag++;
393 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 case 'B':
396 Py_DontWriteBytecodeFlag++;
397 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 case 's':
400 Py_NoUserSiteDirectory++;
401 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 case 'S':
404 Py_NoSiteFlag++;
405 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 case 'E':
408 Py_IgnoreEnvironmentFlag++;
409 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 case 't':
412 /* ignored for backwards compatibility */
413 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 case 'u':
416 Py_UnbufferedStdioFlag = 1;
417 saw_unbuffered_flag = 1;
418 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 case 'v':
421 Py_VerboseFlag++;
422 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 case 'x':
425 skipfirstline = 1;
426 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes33fe8092008-04-13 13:53:33 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 case 'h':
431 case '?':
432 help++;
433 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 case 'V':
436 version++;
437 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 case 'W':
440 PySys_AddWarnOption(_PyOS_optarg);
441 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 default:
446 return usage(2, argv[0]);
447 /*NOTREACHED*/
Guido van Rossum667d7041995-08-04 04:20:48 +0000448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 }
450 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 if (help)
453 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (version) {
456 fprintf(stderr, "Python %s\n", PY_VERSION);
457 return 0;
458 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (!Py_InspectFlag &&
461 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
462 Py_InspectFlag = 1;
463 if (!saw_unbuffered_flag &&
464 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
465 Py_UnbufferedStdioFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (!Py_NoUserSiteDirectory &&
468 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
469 Py_NoUserSiteDirectory = 1;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000470
Philip Jenveye53de3d2010-04-14 03:01:39 +0000471#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (!Py_IgnoreEnvironmentFlag && (wp = _wgetenv(L"PYTHONWARNINGS")) &&
473 *wp != L'\0') {
474 wchar_t *buf, *warning;
Philip Jenvey0805ca32010-04-07 04:04:10 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 buf = (wchar_t *)malloc((wcslen(wp) + 1) * sizeof(wchar_t));
477 if (buf == NULL)
478 Py_FatalError(
479 "not enough memory to copy PYTHONWARNINGS");
480 wcscpy(buf, wp);
481 for (warning = wcstok(buf, L",");
482 warning != NULL;
483 warning = wcstok(NULL, L",")) {
484 PySys_AddWarnOption(warning);
485 }
486 free(buf);
487 }
Philip Jenveye53de3d2010-04-14 03:01:39 +0000488#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
490 char *buf, *oldloc;
Victor Stinnerc2d76fd2010-09-10 23:13:52 +0000491 PyObject *unicode;
Philip Jenveye53de3d2010-04-14 03:01:39 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 /* settle for strtok here as there's no one standard
494 C89 wcstok */
495 buf = (char *)malloc(strlen(p) + 1);
496 if (buf == NULL)
497 Py_FatalError(
498 "not enough memory to copy PYTHONWARNINGS");
499 strcpy(buf, p);
500 oldloc = strdup(setlocale(LC_ALL, NULL));
501 setlocale(LC_ALL, "");
502 for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) {
Victor Stinner5c848a82010-09-12 08:00:41 +0000503#ifdef __APPLE__
504 /* Use utf-8 on Mac OS X */
505 unicode = PyUnicode_FromString(p);
506#else
507 wchar_t *wchar = _Py_char2wchar(p);
Victor Stinnerc2d76fd2010-09-10 23:13:52 +0000508 if (wchar == NULL)
509 continue;
510 unicode = PyUnicode_FromWideChar(wchar, wcslen(wchar));
511 PyMem_Free(wchar);
Victor Stinner5c848a82010-09-12 08:00:41 +0000512#endif
Victor Stinnerc2d76fd2010-09-10 23:13:52 +0000513 if (unicode == NULL)
514 continue;
515 PySys_AddWarnOptionUnicode(unicode);
516 Py_DECREF(unicode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
518 setlocale(LC_ALL, oldloc);
519 free(oldloc);
520 free(buf);
521 }
Philip Jenveye53de3d2010-04-14 03:01:39 +0000522#endif
Philip Jenvey0805ca32010-04-07 04:04:10 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (command == NULL && module == NULL && _PyOS_optind < argc &&
525 wcscmp(argv[_PyOS_optind], L"-") != 0)
526 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000527#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 filename = decc$translate_vms(argv[_PyOS_optind]);
529 if (filename == (char *)0 || filename == (char *)-1)
530 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000531
532#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000534#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 }
Guido van Rossum775af911997-02-14 19:50:32 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
Guido van Rossum775af911997-02-14 19:50:32 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (Py_UnbufferedStdioFlag) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000540#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 _setmode(fileno(stdin), O_BINARY);
542 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000543#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000544#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
546 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
547 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000548#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 setbuf(stdin, (char *)NULL);
550 setbuf(stdout, (char *)NULL);
551 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000552#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 }
554 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000555#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* Doesn't have to have line-buffered -- use unbuffered */
557 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
558 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000559#else /* !MS_WINDOWS */
560#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
562 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000563#endif /* HAVE_SETVBUF */
564#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* Leave stderr alone - it should be unbuffered anyway. */
566 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000567#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 else {
569 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
570 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000571#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000572
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000573#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 /* On MacOS X, when the Python interpreter is embedded in an
575 application bundle, it gets executed by a bootstrapping script
576 that does os.execve() with an argv[0] that's different from the
577 actual Python executable. This is needed to keep the Finder happy,
578 or rather, to work around Apple's overly strict requirements of
579 the process name. However, we still need a usable sys.executable,
580 so the actual executable path is passed in an environment variable.
581 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
582 script. */
583 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') {
584 wchar_t* buffer;
585 size_t len = strlen(p);
586 size_t r;
Ronald Oussoren3e264e12009-02-12 15:55:38 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 buffer = malloc(len * sizeof(wchar_t));
589 if (buffer == NULL) {
590 Py_FatalError(
591 "not enough memory to copy PYTHONEXECUTABLE");
592 }
Ronald Oussoren3e264e12009-02-12 15:55:38 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 r = mbstowcs(buffer, p, len);
595 Py_SetProgramName(buffer);
596 /* buffer is now handed off - do not free */
597 } else {
598 Py_SetProgramName(argv[0]);
599 }
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000600#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000602#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_Initialize();
Guido van Rossumed52aac1997-07-19 19:20:32 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 if (Py_VerboseFlag ||
606 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
607 fprintf(stderr, "Python %s on %s\n",
608 Py_GetVersion(), Py_GetPlatform());
609 if (!Py_NoSiteFlag)
610 fprintf(stderr, "%s\n", COPYRIGHT);
611 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 if (command != NULL) {
614 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
615 _PyOS_optind--;
616 argv[_PyOS_optind] = L"-c";
617 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (module != NULL) {
Nick Coghland26c18a2010-08-17 13:06:11 +0000620 /* Backup _PyOS_optind and force sys.argv[0] = '-m'*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 _PyOS_optind--;
Nick Coghland26c18a2010-08-17 13:06:11 +0000622 argv[_PyOS_optind] = L"-m";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
628 isatty(fileno(stdin))) {
629 PyObject *v;
630 v = PyImport_ImportModule("readline");
631 if (v == NULL)
632 PyErr_Clear();
633 else
634 Py_DECREF(v);
635 }
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (command) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000638 sts = run_command(command, &cf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 free(command);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 } else if (module) {
641 sts = RunModule(module, 1);
642 }
643 else {
Christian Heimes9cd17752007-11-18 19:35:23 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 if (filename == NULL && stdin_is_interactive) {
646 Py_InspectFlag = 0; /* do exit on SystemExit */
647 RunStartupFile(&cf);
648 }
649 /* XXX */
Christian Heimes9cd17752007-11-18 19:35:23 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 sts = -1; /* keep track of whether we've already run __main__ */
Christian Heimes9cd17752007-11-18 19:35:23 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (filename != NULL) {
654 sts = RunMainFromImporter(filename);
655 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 if (sts==-1 && filename!=NULL) {
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000658 if ((fp = _Py_wfopen(filename, L"r")) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 char cfilename[PATH_MAX];
660 size_t r = wcstombs(cfilename, filename, PATH_MAX);
661 if (r == PATH_MAX)
662 /* cfilename is not null-terminated;
663 * forcefully null-terminating it
664 * might break the shift state */
665 strcpy(cfilename, "<file name too long>");
666 if (r == ((size_t)-1))
667 strcpy(cfilename, "<unprintable file name>");
668 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
669 argv[0], cfilename, errno, strerror(errno));
Christian Heimesada8c3b2008-03-18 18:26:33 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 return 2;
672 }
673 else if (skipfirstline) {
674 int ch;
675 /* Push back first newline so line numbers
676 remain the same */
677 while ((ch = getc(fp)) != EOF) {
678 if (ch == '\n') {
679 (void)ungetc(ch, fp);
680 break;
681 }
682 }
683 }
684 {
685 /* XXX: does this work on Win/Win64? (see posix_fstat) */
686 struct stat sb;
687 if (fstat(fileno(fp), &sb) == 0 &&
688 S_ISDIR(sb.st_mode)) {
689 fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename);
690 fclose(fp);
691 return 1;
692 }
693 }
694 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000695
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000696 if (sts == -1)
697 sts = run_file(fp, filename, &cf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
Barry Warsawd86dcd32003-06-29 17:07:06 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 /* Check this environment variable at the end, to give programs the
701 * opportunity to set it from Python.
702 */
703 if (!Py_InspectFlag &&
704 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
705 {
706 Py_InspectFlag = 1;
707 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (Py_InspectFlag && stdin_is_interactive &&
710 (filename != NULL || command != NULL || module != NULL)) {
711 Py_InspectFlag = 0;
712 /* XXX */
713 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
714 }
715
716 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000717
718#ifdef __INSURE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* Insure++ is a memory analysis tool that aids in discovering
720 * memory leaks and other memory problems. On Python exit, the
721 * interned string dictionaries are flagged as being in use at exit
722 * (which it is). Under normal circumstances, this is fine because
723 * the memory will be automatically reclaimed by the system. Under
724 * memory debugging, it's a huge source of useless noise, so we
725 * trade off slower shutdown for less distraction in the memory
726 * reports. -baw
727 */
728 _Py_ReleaseInternedStrings();
729 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000730#endif /* __INSURE__ */
731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000733}
734
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000735/* this is gonna seem *real weird*, but if you put some other code between
736 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
737 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000738
Guido van Rossum667d7041995-08-04 04:20:48 +0000739/* Make the *original* argc/argv available to other modules.
740 This is rare, but it is needed by the secureware extension. */
741
742void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000743Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 *argc = orig_argc;
746 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000747}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000748
Ronald Oussorend61deca2010-04-18 14:46:12 +0000749
Victor Stinnerf2e08b32010-08-13 23:29:08 +0000750/* Encode a (wide) character string to the locale encoding with the
751 surrogateescape error handler (characters in range U+DC80..U+DCFF are
752 converted to bytes 0x80..0xFF).
753
754 This function is the reverse of _Py_char2wchar().
755
756 Return a pointer to a newly allocated byte string (use PyMem_Free() to free
757 the memory), or NULL on error (conversion error or memory error). */
758char*
759_Py_wchar2char(const wchar_t *text)
760{
761 const size_t len = wcslen(text);
762 char *result = NULL, *bytes = NULL;
763 size_t i, size, converted;
764 wchar_t c, buf[2];
765
766 /* The function works in two steps:
767 1. compute the length of the output buffer in bytes (size)
768 2. outputs the bytes */
769 size = 0;
770 buf[1] = 0;
771 while (1) {
772 for (i=0; i < len; i++) {
773 c = text[i];
774 if (c >= 0xdc80 && c <= 0xdcff) {
775 /* UTF-8b surrogate */
776 if (bytes != NULL) {
777 *bytes++ = c - 0xdc00;
778 size--;
779 }
780 else
781 size++;
782 continue;
783 }
784 else {
785 buf[0] = c;
786 if (bytes != NULL)
787 converted = wcstombs(bytes, buf, size);
788 else
789 converted = wcstombs(NULL, buf, 0);
790 if (converted == (size_t)-1) {
791 if (result != NULL)
792 PyMem_Free(result);
793 return NULL;
794 }
795 if (bytes != NULL) {
796 bytes += converted;
797 size -= converted;
798 }
799 else
800 size += converted;
801 }
802 }
803 if (result != NULL) {
804 *bytes = 0;
805 break;
806 }
807
808 size += 1; /* nul byte at the end */
809 result = PyMem_Malloc(size);
810 if (result == NULL)
811 return NULL;
812 bytes = result;
813 }
814 return result;
815}
816
817
818/* Decode a byte string from the locale encoding with the
819 surrogateescape error handler (undecodable bytes are decoded as characters
820 in range U+DC80..U+DCFF). If a byte sequence can be decoded as a surrogate
821 character, escape the bytes using the surrogateescape error handler instead
822 of decoding them.
823
824 Use _Py_wchar2char() to encode the character string back to a byte string.
825
826 Return a pointer to a newly allocated (wide) character string (use
827 PyMem_Free() to free the memory), or NULL on error (conversion error or
828 memory error). */
Ronald Oussorend61deca2010-04-18 14:46:12 +0000829wchar_t*
830_Py_char2wchar(char* arg)
831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 wchar_t *res;
Ronald Oussorend61deca2010-04-18 14:46:12 +0000833#ifdef HAVE_BROKEN_MBSTOWCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /* Some platforms have a broken implementation of
835 * mbstowcs which does not count the characters that
836 * would result from conversion. Use an upper bound.
837 */
838 size_t argsize = strlen(arg);
Ronald Oussorend61deca2010-04-18 14:46:12 +0000839#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 size_t argsize = mbstowcs(NULL, arg, 0);
Ronald Oussorend61deca2010-04-18 14:46:12 +0000841#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 size_t count;
843 unsigned char *in;
844 wchar_t *out;
Ronald Oussorend61deca2010-04-18 14:46:12 +0000845#ifdef HAVE_MBRTOWC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 mbstate_t mbs;
Ronald Oussorend61deca2010-04-18 14:46:12 +0000847#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 if (argsize != (size_t)-1) {
849 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
850 if (!res)
851 goto oom;
852 count = mbstowcs(res, arg, argsize+1);
853 if (count != (size_t)-1) {
854 wchar_t *tmp;
855 /* Only use the result if it contains no
856 surrogate characters. */
857 for (tmp = res; *tmp != 0 &&
858 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
859 ;
860 if (*tmp == 0)
861 return res;
862 }
863 PyMem_Free(res);
864 }
865 /* Conversion failed. Fall back to escaping with surrogateescape. */
Ronald Oussorend61deca2010-04-18 14:46:12 +0000866#ifdef HAVE_MBRTOWC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
868
869 /* Overallocate; as multi-byte characters are in the argument, the
870 actual output could use less memory. */
871 argsize = strlen(arg) + 1;
872 res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
873 if (!res) goto oom;
874 in = (unsigned char*)arg;
875 out = res;
876 memset(&mbs, 0, sizeof mbs);
877 while (argsize) {
878 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
879 if (converted == 0)
880 /* Reached end of string; null char stored. */
881 break;
882 if (converted == (size_t)-2) {
883 /* Incomplete character. This should never happen,
884 since we provide everything that we have -
885 unless there is a bug in the C library, or I
886 misunderstood how mbrtowc works. */
887 fprintf(stderr, "unexpected mbrtowc result -2\n");
888 return NULL;
889 }
890 if (converted == (size_t)-1) {
891 /* Conversion error. Escape as UTF-8b, and start over
892 in the initial shift state. */
893 *out++ = 0xdc00 + *in++;
894 argsize--;
895 memset(&mbs, 0, sizeof mbs);
896 continue;
897 }
898 if (*out >= 0xd800 && *out <= 0xdfff) {
899 /* Surrogate character. Escape the original
900 byte sequence with surrogateescape. */
901 argsize -= converted;
902 while (converted--)
903 *out++ = 0xdc00 + *in++;
904 continue;
905 }
906 /* successfully converted some bytes */
907 in += converted;
908 argsize -= converted;
909 out++;
910 }
Ronald Oussorend61deca2010-04-18 14:46:12 +0000911#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 /* Cannot use C locale for escaping; manually escape as if charset
913 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
914 correctly in the locale's charset, which must be an ASCII superset. */
915 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
916 if (!res) goto oom;
917 in = (unsigned char*)arg;
918 out = res;
919 while(*in)
920 if(*in < 128)
921 *out++ = *in++;
922 else
923 *out++ = 0xdc00 + *in++;
924 *out = 0;
Ronald Oussorend61deca2010-04-18 14:46:12 +0000925#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 return res;
Ronald Oussorend61deca2010-04-18 14:46:12 +0000927oom:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 fprintf(stderr, "out of memory\n");
929 return NULL;
Ronald Oussorend61deca2010-04-18 14:46:12 +0000930}
931
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932#ifdef __cplusplus
933}
934#endif