summaryrefslogtreecommitdiffstats
path: root/Python/codecs.c
blob: 4b0f4cb0d04f91771d4ee617f2f0a4e380321fe2 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/* ------------------------------------------------------------------------

   Python Codec Registry and support functions

Written by Marc-Andre Lemburg (mal@lemburg.com).

Copyright (c) Corporation for National Research Initiatives.

   ------------------------------------------------------------------------ */

#include "Python.h"
#include <ctype.h>

/* --- Codec Registry ----------------------------------------------------- */

/* Import the standard encodings package which will register the first
   codec search function. 

   This is done in a lazy way so that the Unicode implementation does
   not downgrade startup time of scripts not needing it.

   ImportErrors are silently ignored by this function. Only one try is
   made.

*/

static int _PyCodecRegistry_Init(void); /* Forward */

int PyCodec_Register(PyObject *search_function)
{
    PyInterpreterState *interp = PyThreadState_GET()->interp;
    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
	goto onError;
    if (search_function == NULL) {
	PyErr_BadArgument();
	goto onError;
    }
    if (!PyCallable_Check(search_function)) {
	PyErr_SetString(PyExc_TypeError, "argument must be callable");
	goto onError;
    }
    return PyList_Append(interp->codec_search_path, search_function);

 onError:
    return -1;
}

/* Convert a string to a normalized Python string: all characters are
   converted to lower case, spaces are replaced with underscores. */

static
PyObject *normalizestring(const char *string)
{
    register size_t i;
    size_t len = strlen(string);
    char *p;
    PyObject *v;
    
    if (len > PY_SSIZE_T_MAX) {
	PyErr_SetString(PyExc_OverflowError, "string is too large");
	return NULL;
    }
	
    v = PyString_FromStringAndSize(NULL, len);
    if (v == NULL)
	return NULL;
    p = PyString_AS_STRING(v);
    for (i = 0; i < len; i++) {
        register char ch = string[i];
        if (ch == ' ')
            ch = '-';
        else
            ch = tolower(Py_CHARMASK(ch));
	p[i] = ch;
    }
    return v;
}

/* Lookup the given encoding and return a tuple providing the codec
   facilities.

   The encoding string is looked up converted to all lower-case
   characters. This makes encodings looked up through this mechanism
   effectively case-insensitive.

   If no codec is found, a LookupError is set and NULL returned. 

   As side effect, this tries to load the encodings package, if not
   yet done. This is part of the lazy load strategy for the encodings
   package.

*/

PyObject *_PyCodec_Lookup(const char *encoding)
{
    PyInterpreterState *interp;
    PyObject *result, *args = NULL, *v;
    Py_ssize_t i, len;

    if (encoding == NULL) {
	PyErr_BadArgument();
	goto onError;
    }

    interp = PyThreadState_GET()->interp;
    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
	goto onError;

    /* Convert the encoding to a normalized Python string: all
       characters are converted to lower case, spaces and hyphens are
       replaced with underscores. */
    v = normalizestring(encoding);
    if (v == NULL)
	goto onError;
    PyString_InternInPlace(&v);

    /* First, try to lookup the name in the registry dictionary */
    result = PyDict_GetItem(interp->codec_search_cache, v);
    if (result != NULL) {
	Py_INCREF(result);
	Py_DECREF(v);
	return result;
    }
    
    /* Next, scan the search functions in order of registration */
    args = PyTuple_New(1);
    if (args == NULL)
	goto onError;
    PyTuple_SET_ITEM(args,0,v);

    len = PyList_Size(interp->codec_search_path);
    if (len < 0)
	goto onError;
    if (len == 0) {
	PyErr_SetString(PyExc_LookupError,
			"no codec search functions registered: "
			"can't find encoding");
	goto onError;
    }

    for (i = 0; i < len; i++) {
	PyObject *func;
	
	func = PyList_GetItem(interp->codec_search_path, i);
	if (func == NULL)
	    goto onError;
	result = PyEval_CallObject(func, args);
	if (result == NULL)
	    goto onError;
	if (result == Py_None) {
	    Py_DECREF(result);
	    continue;
	}
	if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
	    PyErr_SetString(PyExc_TypeError,
			    "codec search functions must return 4-tuples");
	    Py_DECREF(result);
	    goto onError;
	}
	break;
    }
    if (i == len) {
	/* XXX Perhaps we should cache misses too ? */
	PyErr_Format(PyExc_LookupError,
                     "unknown encoding: %s", encoding);
	goto onError;
    }

    /* Cache and return the result */
    PyDict_SetItem(interp->codec_search_cache, v, result);
    Py_DECREF(args);
    return result;

 onError:
    Py_XDECREF(args);
    return NULL;
}

static
PyObject *args_tuple(PyObject *object,
		     const char *errors)
{
    PyObject *args;
    
    args = PyTuple_New(1 + (errors != NULL));
    if (args == NULL)
	return NULL;
    Py_INCREF(object);
    PyTuple_SET_ITEM(args,0,object);
    if (errors) {
	PyObject *v;
	
	v = PyString_FromString(errors);
	if (v == NULL) {
	    Py_DECREF(args);
	    return NULL;
	}
	PyTuple_SET_ITEM(args, 1, v);
    }
    return args;
}

/* Helper function to get a codec item */

static
PyObject *codec_getitem(const char *encoding, int index)
{
    PyObject *codecs;
    PyObject *v;

    codecs = _PyCodec_Lookup(encoding);
    if (codecs == NULL)
	return NULL;
    v = PyTuple_GET_ITEM(codecs, index);
    Py_DECREF(codecs);
    Py_INCREF(v);
    return v;
}

/* Helper function to create an incremental codec. */

static
PyObject *codec_getincrementalcodec(const char *encoding,
				    const char *errors,
				    const char *attrname)
{
    PyObject *codecs, *ret, *inccodec;

    codecs = _PyCodec_Lookup(encoding);
    if (codecs == NULL)
	return NULL;
    inccodec = PyObject_GetAttrString(codecs, attrname);
    Py_DECREF(codecs);
    if (inccodec == NULL)
	return NULL;
    if (errors)
	ret = PyObject_CallFunction(inccodec, "s", errors);
    else
	ret = PyObject_CallFunction(inccodec, NULL);
    Py_DECREF(inccodec);
    return ret;
}

/* Helper function to create a stream codec. */

static
PyObject *codec_getstreamcodec(const char *encoding,
			       PyObject *stream,
			       const char *errors,
			       const int index)
{
    PyObject *codecs, *streamcodec, *codeccls;

    codecs = _PyCodec_Lookup(encoding);
    if (codecs == NULL)
	return NULL;

    codeccls = PyTuple_GET_ITEM(codecs, index);
    if (errors != NULL)
	streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
    else
	streamcodec = PyObject_CallFunction(codeccls, "O", stream);
    Py_DECREF(codecs);
    return streamcodec;
}

/* Convenience APIs to query the Codec registry. 
   
   All APIs return a codec object with incremented refcount.
   
 */

PyObject *PyCodec_Encoder(const char *encoding)
{
    return codec_getitem(encoding, 0);
}

PyObject *PyCodec_Decoder(const char *encoding)
{
    return codec_getitem(encoding, 1);
}

PyObject *PyCodec_IncrementalEncoder(const char *encoding,
				     const char *errors)
{
    return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
}

PyObject *PyCodec_IncrementalDecoder(const char *encoding,
				     const char *errors)
{
    return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
}

PyObject *PyCodec_StreamReader(const char *encoding,
			       PyObject *stream,
			       const char *errors)
{
    return codec_getstreamcodec(encoding, stream, errors, 2);
}

PyObject *PyCodec_StreamWriter(const char *encoding,
			       PyObject *stream,
			       const char *errors)
{
    return codec_getstreamcodec(encoding, stream, errors, 3);
}

/* Encode an object (e.g. an Unicode object) using the given encoding
   and return the resulting encoded object (usually a Python string).

   errors is passed to the encoder factory as argument if non-NULL. */

PyObject *PyCodec_Encode(PyObject *object,
			 const char *encoding,
			 const char *errors)
{
    PyObject *encoder = NULL;
    PyObject *args = NULL, *result = NULL;
    PyObject *v;

    encoder = PyCodec_Encoder(encoding);
    if (encoder == NULL)
	goto onError;

    args = args_tuple(object, errors);
    if (args == NULL)
	goto onError;
    
    result = PyEval_CallObject(encoder,args);
    if (result == NULL)
	goto onError;

    if (!PyTuple_Check(result) || 
	PyTuple_GET_SIZE(result) != 2) {
	PyErr_SetString(PyExc_TypeError,
			"encoder must return a tuple (object,integer)");
	goto onError;
    }
    v = PyTuple_GET_ITEM(result,0);
    Py_INCREF(v);
    /* We don't check or use the second (integer) entry. */

    Py_DECREF(args);
    Py_DECREF(encoder);
    Py_DECREF(result);
    return v;
	
 onError:
    Py_XDECREF(result);
    Py_XDECREF(args);
    Py_XDECREF(encoder);
    return NULL;
}

/* Decode an object (usually a Python string) using the given encoding
   and return an equivalent object (e.g. an Unicode object).

   errors is passed to the decoder factory as argument if non-NULL. */

PyObject *PyCodec_Decode(PyObject *object,
			 const char *encoding,
			 const char *errors)
{
    PyObject *decoder = NULL;
    PyObject *args = NULL, *result = NULL;
    PyObject *v;

    decoder = PyCodec_Decoder(encoding);
    if (decoder == NULL)
	goto onError;

    args = args_tuple(object, errors);
    if (args == NULL)
	goto onError;
    
    result = PyEval_CallObject(decoder,args);
    if (result == NULL)
	goto onError;
    if (!PyTuple_Check(result) || 
	PyTuple_GET_SIZE(result) != 2) {
	PyErr_SetString(PyExc_TypeError,
			"decoder must return a tuple (object,integer)");
	goto onError;
    }
    v = PyTuple_GET_ITEM(result,0);
    Py_INCREF(v);
    /* We don't check or use the second (integer) entry. */

    Py_DECREF(args);
    Py_DECREF(decoder);
    Py_DECREF(result);
    return v;
	
 onError:
    Py_XDECREF(args);
    Py_XDECREF(decoder);
    Py_XDECREF(result);
    return NULL;
}

/* Register the error handling callback function error under the name
   name. This function will be called by the codec when it encounters
   an unencodable characters/undecodable bytes and doesn't know the
   callback name, when name is specified as the error parameter
   in the call to the encode/decode function.
   Return 0 on success, -1 on error */
int PyCodec_RegisterError(const char *name, PyObject *error)
{
    PyInterpreterState *interp = PyThreadState_GET()->interp;
    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
	return -1;
    if (!PyCallable_Check(error)) {
	PyErr_SetString(PyExc_TypeError, "handler must be callable");
	return -1;
    }
    return PyDict_SetItemString(interp->codec_error_registry,
	    			(char *)name, error);
}

/* Lookup the error handling callback function registered under the
   name error. As a special case NULL can be passed, in which case
   the error handling callback for strict encoding will be returned. */
PyObject *PyCodec_LookupError(const char *name)
{
    PyObject *handler = NULL;

    PyInterpreterState *interp = PyThreadState_GET()->interp;
    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
	return NULL;

    if (name==NULL)
	name = "strict";
    handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
    if (!handler)
	PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
    else
	Py_INCREF(handler);
    return handler;
}

static void wrong_exception_type(PyObject *exc)
{
    PyObject *type = PyObject_GetAttrString(exc, "__class__");
    if (type != NULL) {
	PyObject *name = PyObject_GetAttrString(type, "__name__");
	Py_DECREF(type);
	if (name != NULL) {
	    PyObject *string = PyObject_Str(name);
	    Py_DECREF(name);
	    if (string != NULL) {
	        PyErr_Format(PyExc_TypeError,
		    "don't know how to handle %.400s in error callback",
		    PyString_AS_STRING(string));
	        Py_DECREF(string);
	    }
	}
    }
}

PyObject *PyCodec_StrictErrors(PyObject *exc)
{
    if (PyExceptionInstance_Check(exc))
        PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
    else
	PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
    return NULL;
}


#ifdef Py_USING_UNICODE
PyObject *PyCodec_IgnoreErrors(PyObject *exc)
{
    Py_ssize_t end;
    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
	if (PyUnicodeEncodeError_GetEnd(exc, &end))
	    return NULL;
    }
    else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
	if (PyUnicodeDecodeError_GetEnd(exc, &end))
	    return NULL;
    }
    else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
	if (PyUnicodeTranslateError_GetEnd(exc, &end))
	    return NULL;
    }
    else {
	wrong_exception_type(exc);
	return NULL;
    }
    /* ouch: passing NULL, 0, pos gives None instead of u'' */
    return Py_BuildValue("(u#n)", &end, 0, end);
}


PyObject *PyCodec_ReplaceErrors(PyObject *exc)
{
    PyObject *restuple;
    Py_ssize_t start;
    Py_ssize_t end;
    Py_ssize_t i;

    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
	PyObject *res;
	Py_UNICODE *p;
	if (PyUnicodeEncodeError_GetStart(exc, &start))
	    return NULL;
	if (PyUnicodeEncodeError_GetEnd(exc, &end))
	    return NULL;
	res = PyUnicode_FromUnicode(NULL, end-start);
	if (res == NULL)
	    return NULL;
	for (p = PyUnicode_AS_UNICODE(res), i = start;
	    i<end; ++p, ++i)
	    *p = '?';
	restuple = Py_BuildValue("(On)", res, end);
	Py_DECREF(res);
	return restuple;
    }
    else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
	Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
	if (PyUnicodeDecodeError_GetEnd(exc, &end))
	    return NULL;
	return Py_BuildValue("(u#n)", &res, 1, end);
    }
    else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
	PyObject *res;
	Py_UNICODE *p;
	if (PyUnicodeTranslateError_GetStart(exc, &start))
	    return NULL;
	if (PyUnicodeTranslateError_GetEnd(exc, &end))
	    return NULL;
	res = PyUnicode_FromUnicode(NULL, end-start);
	if (res == NULL)
	    return NULL;
	for (p = PyUnicode_AS_UNICODE(res), i = start;
	    i<end; ++p, ++i)
	    *p = Py_UNICODE_REPLACEMENT_CHARACTER;
	restuple = Py_BuildValue("(On)", res, end);
	Py_DECREF(res);
	return restuple;
    }
    else {
	wrong_exception_type(exc);
	return NULL;
    }
}

PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
{
    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
	PyObject *restuple;
	PyObject *object;
	Py_ssize_t start;
	Py_ssize_t end;
	PyObject *res;
	Py_UNICODE *p;
	Py_UNICODE *startp;
	Py_UNICODE *outp;
	int ressize;
	if (PyUnicodeEncodeError_GetStart(exc, &start))
	    return NULL;
	if (PyUnicodeEncodeError_GetEnd(exc, &end))
	    return NULL;
	if (!(object = PyUnicodeEncodeError_GetObject(exc)))
	    return NULL;
	startp = PyUnicode_AS_UNICODE(object);
	for (p = startp+start, ressize = 0; p < startp+end; ++p) {
	    if (*p<10)
		ressize += 2+1+1;
	    else if (*p<100)
		ressize += 2+2+1;
	    else if (*p<1000)
		ressize += 2+3+1;
	    else if (*p<10000)
		ressize += 2+4+1;
#ifndef Py_UNICODE_WIDE
	    else
		ressize += 2+5+1;
#else
	    else if (*p<100000)
		ressize += 2+5+1;
	    else if (*p<1000000)
		ressize += 2+6+1;
	    else
		ressize += 2+7+1;
#endif
	}
	/* allocate replacement */
	res = PyUnicode_FromUnicode(NULL, ressize);
	if (res == NULL) {
	    Py_DECREF(object);
	    return NULL;
	}
	/* generate replacement */
	for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
	    p < startp+end; ++p) {
	    Py_UNICODE c = *p;
	    int digits;
	    int base;
	    *outp++ = '&';
	    *outp++ = '#';
	    if (*p<10) {
		digits = 1;
		base = 1;
	    }
	    else if (*p<100) {
		digits = 2;
		base = 10;
	    }
	    else if (*p<1000) {
		digits = 3;
		base = 100;
	    }
	    else if (*p<10000) {
		digits = 4;
		base = 1000;
	    }
#ifndef Py_UNICODE_WIDE
	    else {
		digits = 5;
		base = 10000;
	    }
#else
	    else if (*p<100000) {
		digits = 5;
		base = 10000;
	    }
	    else if (*p<1000000) {
		digits = 6;
		base = 100000;
	    }
	    else {
		digits = 7;
		base = 1000000;
	    }
#endif
	    while (digits-->0) {
		*outp++ = '0' + c/base;
		c %= base;
		base /= 10;
	    }
	    *outp++ = ';';
	}
	restuple = Py_BuildValue("(On)", res, end);
	Py_DECREF(res);
	Py_DECREF(object);
	return restuple;
    }
    else {
	wrong_exception_type(exc);
	return NULL;
    }
}

static Py_UNICODE hexdigits[] = {
    '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
{
    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
	PyObject *restuple;
	PyObject *object;
	Py_ssize_t start;
	Py_ssize_t end;
	PyObject *res;
	Py_UNICODE *p;
	Py_UNICODE *startp;
	Py_UNICODE *outp;
	int ressize;
	if (PyUnicodeEncodeError_GetStart(exc, &start))
	    return NULL;
	if (PyUnicodeEncodeError_GetEnd(exc, &end))
	    return NULL;
	if (!(object = PyUnicodeEncodeError_GetObject(exc)))
	    return NULL;
	startp = PyUnicode_AS_UNICODE(object);
	for (p = startp+start, ressize = 0; p < startp+end; ++p) {
#ifdef Py_UNICODE_WIDE
	    if (*p >= 0x00010000)
		ressize += 1+1+8;
	    else
#endif
	    if (*p >= 0x100) {
		ressize += 1+1+4;
	    }
	    else
		ressize += 1+1+2;
	}
	res = PyUnicode_FromUnicode(NULL, ressize);
	if (res==NULL)
	    return NULL;
	for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
	    p < startp+end; ++p) {
	    Py_UNICODE c = *p;
	    *outp++ = '\\';
#ifdef Py_UNICODE_WIDE
	    if (c >= 0x00010000) {
		*outp++ = 'U';
		*outp++ = hexdigits[(c>>28)&0xf];
		*outp++ = hexdigits[(c>>24)&0xf];
		*outp++ = hexdigits[(c>>20)&0xf];
		*outp++ = hexdigits[(c>>16)&0xf];
		*outp++ = hexdigits[(c>>12)&0xf];
		*outp++ = hexdigits[(c>>8)&0xf];
	    }
	    else
#endif
	    if (c >= 0x100) {
		*outp++ = 'u';
		*outp++ = hexdigits[(c>>12)&0xf];
		*outp++ = hexdigits[(c>>8)&0xf];
	    }
	    else
		*outp++ = 'x';
	    *outp++ = hexdigits[(c>>4)&0xf];
	    *outp++ = hexdigits[c&0xf];
	}

	restuple = Py_BuildValue("(On)", res, end);
	Py_DECREF(res);
	Py_DECREF(object);
	return restuple;
    }
    else {
	wrong_exception_type(exc);
	return NULL;
    }
}
#endif

static PyObject *strict_errors(PyObject *self, PyObject *exc)
{
    return PyCodec_StrictErrors(exc);
}


#ifdef Py_USING_UNICODE
static PyObject *ignore_errors(PyObject *self, PyObject *exc)
{
    return PyCodec_IgnoreErrors(exc);
}


static PyObject *replace_errors(PyObject *self, PyObject *exc)
{
    return PyCodec_ReplaceErrors(exc);
}


static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)
{
    return PyCodec_XMLCharRefReplaceErrors(exc);
}


static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)
{
    return PyCodec_BackslashReplaceErrors(exc);
}
#endif

static int _PyCodecRegistry_Init(void)
{
    static struct {
	char *name;
	PyMethodDef def;
    } methods[] =
    {
	{
	    "strict",
	    {
		"strict_errors",
		strict_errors,
		METH_O
	    }
	},
#ifdef Py_USING_UNICODE
	{
	    "ignore",
	    {
		"ignore_errors",
		ignore_errors,
		METH_O
	    }
	},
	{
	    "replace",
	    {
		"replace_errors",
		replace_errors,
		METH_O
	    }
	},
	{
	    "xmlcharrefreplace",
	    {
		"xmlcharrefreplace_errors",
		xmlcharrefreplace_errors,
		METH_O
	    }
	},
	{
	    "backslashreplace",
	    {
		"backslashreplace_errors",
		backslashreplace_errors,
		METH_O
	    }
	}
#endif
    };

    PyInterpreterState *interp = PyThreadState_GET()->interp;
    PyObject *mod;
    unsigned i;

    if (interp->codec_search_path != NULL)
	return 0;

    interp->codec_search_path = PyList_New(0);
    interp->codec_search_cache = PyDict_New();
    interp->codec_error_registry = PyDict_New();

    if (interp->codec_error_registry) {
	for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {
	    PyObject *func = PyCFunction_New(&methods[i].def, NULL);
	    int res;
	    if (!func)
		Py_FatalError("can't initialize codec error registry");
	    res = PyCodec_RegisterError(methods[i].name, func);
	    Py_DECREF(func);
	    if (res)
		Py_FatalError("can't initialize codec error registry");
	}
    }

    if (interp->codec_search_path == NULL ||
	interp->codec_search_cache == NULL ||
	interp->codec_error_registry == NULL)
	Py_FatalError("can't initialize codec registry");

    mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0);
    if (mod == NULL) {
	if (PyErr_ExceptionMatches(PyExc_ImportError)) {
	    /* Ignore ImportErrors... this is done so that
	       distributions can disable the encodings package. Note
	       that other errors are not masked, e.g. SystemErrors
	       raised to inform the user of an error in the Python
	       configuration are still reported back to the user. */
	    PyErr_Clear();
	    return 0;
	}
	return -1;
    }
    Py_DECREF(mod);
    return 0;
}
CALES; target->locale_name; target++) { if (strcmp(ctype_loc, target->locale_name) == 0) { return 1; } } return 0; } #ifdef PY_COERCE_C_LOCALE static const char C_LOCALE_COERCION_WARNING[] = "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale " "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n"; static int _coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target) { const char *newloc = target->locale_name; /* Reset locale back to currently configured defaults */ _Py_SetLocaleFromEnv(LC_ALL); /* Set the relevant locale environment variable */ if (setenv("LC_CTYPE", newloc, 1)) { fprintf(stderr, "Error setting LC_CTYPE, skipping C locale coercion\n"); return 0; } if (warn) { fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc); } /* Reconfigure with the overridden environment variables */ _Py_SetLocaleFromEnv(LC_ALL); return 1; } #endif int _Py_CoerceLegacyLocale(int warn) { int coerced = 0; #ifdef PY_COERCE_C_LOCALE char *oldloc = NULL; oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL)); if (oldloc == NULL) { return coerced; } const char *locale_override = getenv("LC_ALL"); if (locale_override == NULL || *locale_override == '\0') { /* LC_ALL is also not set (or is set to an empty string) */ const _LocaleCoercionTarget *target = NULL; for (target = _TARGET_LOCALES; target->locale_name; target++) { const char *new_locale = setlocale(LC_CTYPE, target->locale_name); if (new_locale != NULL) { #if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET) /* Also ensure that nl_langinfo works in this locale */ char *codeset = nl_langinfo(CODESET); if (!codeset || *codeset == '\0') { /* CODESET is not set or empty, so skip coercion */ new_locale = NULL; _Py_SetLocaleFromEnv(LC_CTYPE); continue; } #endif /* Successfully configured locale, so make it the default */ coerced = _coerce_default_locale_settings(warn, target); goto done; } } } /* No C locale warning here, as Py_Initialize will emit one later */ setlocale(LC_CTYPE, oldloc); done: PyMem_RawFree(oldloc); #endif return coerced; } /* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to * isolate the idiosyncrasies of different libc implementations. It reads the * appropriate environment variable and uses its value to select the locale for * 'category'. */ char * _Py_SetLocaleFromEnv(int category) { char *res; #ifdef __ANDROID__ const char *locale; const char **pvar; #ifdef PY_COERCE_C_LOCALE const char *coerce_c_locale; #endif const char *utf8_locale = "C.UTF-8"; const char *env_var_set[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL, }; /* Android setlocale(category, "") doesn't check the environment variables * and incorrectly sets the "C" locale at API 24 and older APIs. We only * check the environment variables listed in env_var_set. */ for (pvar=env_var_set; *pvar; pvar++) { locale = getenv(*pvar); if (locale != NULL && *locale != '\0') { if (strcmp(locale, utf8_locale) == 0 || strcmp(locale, "en_US.UTF-8") == 0) { return setlocale(category, utf8_locale); } return setlocale(category, "C"); } } /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string. * Quote from POSIX section "8.2 Internationalization Variables": * "4. If the LANG environment variable is not set or is set to the empty * string, the implementation-defined default locale shall be used." */ #ifdef PY_COERCE_C_LOCALE coerce_c_locale = getenv("PYTHONCOERCECLOCALE"); if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) { /* Some other ported code may check the environment variables (e.g. in * extension modules), so we make sure that they match the locale * configuration */ if (setenv("LC_CTYPE", utf8_locale, 1)) { fprintf(stderr, "Warning: failed setting the LC_CTYPE " "environment variable to %s\n", utf8_locale); } } #endif res = setlocale(category, utf8_locale); #else /* !defined(__ANDROID__) */ res = setlocale(category, ""); #endif _Py_ResetForceASCII(); return res; } /* Global initializations. Can be undone by Py_Finalize(). Don't call this twice without an intervening Py_Finalize() call. Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx must have a corresponding call to Py_Finalize. Locking: you must hold the interpreter lock while calling these APIs. (If the lock has not yet been initialized, that's equivalent to having the lock, but you cannot use multiple threads.) */ static PyStatus pyinit_core_reconfigure(_PyRuntimeState *runtime, PyThreadState **tstate_p, const PyConfig *config) { PyStatus status; PyThreadState *tstate = _PyThreadState_GET(); if (!tstate) { return _PyStatus_ERR("failed to read thread state"); } *tstate_p = tstate; PyInterpreterState *interp = tstate->interp; if (interp == NULL) { return _PyStatus_ERR("can't make main interpreter"); } _PyConfig_Write(config, runtime); status = _PyConfig_Copy(&interp->config, config); if (_PyStatus_EXCEPTION(status)) { return status; } config = &interp->config; if (config->_install_importlib) { status = _PyConfig_WritePathConfig(config); if (_PyStatus_EXCEPTION(status)) { return status; } } return _PyStatus_OK(); } static PyStatus pycore_init_runtime(_PyRuntimeState *runtime, const PyConfig *config) { if (runtime->initialized) { return _PyStatus_ERR("main interpreter already initialized"); } _PyConfig_Write(config, runtime); /* Py_Finalize leaves _Py_Finalizing set in order to help daemon * threads behave a little more gracefully at interpreter shutdown. * We clobber it here so the new interpreter can start with a clean * slate. * * However, this may still lead to misbehaviour if there are daemon * threads still hanging around from a previous Py_Initialize/Finalize * pair :( */ runtime->finalizing = NULL; PyStatus status = _Py_HashRandomization_Init(config); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyInterpreterState_Enable(runtime); if (_PyStatus_EXCEPTION(status)) { return status; } return _PyStatus_OK(); } static PyStatus pycore_create_interpreter(_PyRuntimeState *runtime, const PyConfig *config, PyThreadState **tstate_p) { PyInterpreterState *interp = PyInterpreterState_New(); if (interp == NULL) { return _PyStatus_ERR("can't make main interpreter"); } PyStatus status = _PyConfig_Copy(&interp->config, config); if (_PyStatus_EXCEPTION(status)) { return status; } PyThreadState *tstate = PyThreadState_New(interp); if (tstate == NULL) { return _PyStatus_ERR("can't make first thread"); } (void) PyThreadState_Swap(tstate); /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because destroying the GIL might fail when it is being referenced from another running thread (see issue #9901). Instead we destroy the previously created GIL here, which ensures that we can call Py_Initialize / Py_FinalizeEx multiple times. */ _PyEval_FiniThreads(&runtime->ceval); /* Auto-thread-state API */ _PyGILState_Init(tstate); /* Create the GIL */ PyEval_InitThreads(); *tstate_p = tstate; return _PyStatus_OK(); } static PyStatus pycore_init_types(PyThreadState *tstate) { PyStatus status; int is_main_interp = _Py_IsMainInterpreter(tstate); status = _PyGC_Init(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } if (is_main_interp) { status = _PyTypes_Init(); if (_PyStatus_EXCEPTION(status)) { return status; } } if (!_PyLong_Init(tstate)) { return _PyStatus_ERR("can't init longs"); } if (is_main_interp) { status = _PyUnicode_Init(); if (_PyStatus_EXCEPTION(status)) { return status; } } status = _PyExc_Init(); if (_PyStatus_EXCEPTION(status)) { return status; } if (is_main_interp) { if (!_PyFloat_Init()) { return _PyStatus_ERR("can't init float"); } if (_PyStructSequence_Init() < 0) { return _PyStatus_ERR("can't initialize structseq"); } } status = _PyErr_Init(); if (_PyStatus_EXCEPTION(status)) { return status; } if (is_main_interp) { if (!_PyContext_Init()) { return _PyStatus_ERR("can't init context"); } } return _PyStatus_OK(); } static PyStatus pycore_init_builtins(PyThreadState *tstate) { assert(!_PyErr_Occurred(tstate)); PyObject *bimod = _PyBuiltin_Init(tstate); if (bimod == NULL) { goto error; } PyInterpreterState *interp = tstate->interp; if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) { goto error; } PyObject *builtins_dict = PyModule_GetDict(bimod); if (builtins_dict == NULL) { goto error; } Py_INCREF(builtins_dict); interp->builtins = builtins_dict; PyStatus status = _PyBuiltins_AddExceptions(bimod); if (_PyStatus_EXCEPTION(status)) { return status; } interp->builtins_copy = PyDict_Copy(interp->builtins); if (interp->builtins_copy == NULL) { goto error; } Py_DECREF(bimod); assert(!_PyErr_Occurred(tstate)); return _PyStatus_OK(); error: Py_XDECREF(bimod); return _PyStatus_ERR("can't initialize builtins module"); } static PyStatus pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod) { assert(!_PyErr_Occurred(tstate)); PyStatus status = _PyImportHooks_Init(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } const PyConfig *config = &tstate->interp->config; if (_Py_IsMainInterpreter(tstate)) { /* Initialize _warnings. */ if (_PyWarnings_Init() == NULL) { return _PyStatus_ERR("can't initialize warnings"); } if (config->_install_importlib) { status = _PyConfig_WritePathConfig(config); if (_PyStatus_EXCEPTION(status)) { return status; } } } /* This call sets up builtin and frozen import support */ if (config->_install_importlib) { status = init_importlib(tstate, sysmod); if (_PyStatus_EXCEPTION(status)) { return status; } } assert(!_PyErr_Occurred(tstate)); return _PyStatus_OK(); } static PyStatus pycore_interp_init(PyThreadState *tstate) { PyStatus status; PyObject *sysmod = NULL; status = pycore_init_types(tstate); if (_PyStatus_EXCEPTION(status)) { goto done; } status = _PySys_Create(tstate, &sysmod); if (_PyStatus_EXCEPTION(status)) { goto done; } status = pycore_init_builtins(tstate); if (_PyStatus_EXCEPTION(status)) { goto done; } status = pycore_init_import_warnings(tstate, sysmod); done: /* sys.modules['sys'] contains a strong reference to the module */ Py_XDECREF(sysmod); return status; } static PyStatus pyinit_config(_PyRuntimeState *runtime, PyThreadState **tstate_p, const PyConfig *config) { _PyConfig_Write(config, runtime); PyStatus status = pycore_init_runtime(runtime, config); if (_PyStatus_EXCEPTION(status)) { return status; } PyThreadState *tstate; status = pycore_create_interpreter(runtime, config, &tstate); if (_PyStatus_EXCEPTION(status)) { return status; } *tstate_p = tstate; status = pycore_interp_init(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } /* Only when we get here is the runtime core fully initialized */ runtime->core_initialized = 1; return _PyStatus_OK(); } PyStatus _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args) { PyStatus status; if (src_config == NULL) { return _PyStatus_ERR("preinitialization config is NULL"); } status = _PyRuntime_Initialize(); if (_PyStatus_EXCEPTION(status)) { return status; } _PyRuntimeState *runtime = &_PyRuntime; if (runtime->preinitialized) { /* If it's already configured: ignored the new configuration */ return _PyStatus_OK(); } /* Note: preinitialized remains 1 on error, it is only set to 0 at exit on success. */ runtime->preinitializing = 1; PyPreConfig config; status = _PyPreConfig_InitFromPreConfig(&config, src_config); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyPreConfig_Read(&config, args); if (_PyStatus_EXCEPTION(status)) { return status; } status = _PyPreConfig_Write(&config); if (_PyStatus_EXCEPTION(status)) { return status; } runtime->preinitializing = 0; runtime->preinitialized = 1; return _PyStatus_OK(); } PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv) { _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv}; return _Py_PreInitializeFromPyArgv(src_config, &args); } PyStatus Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv) { _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv}; return _Py_PreInitializeFromPyArgv(src_config, &args); } PyStatus Py_PreInitialize(const PyPreConfig *src_config) { return _Py_PreInitializeFromPyArgv(src_config, NULL); } PyStatus _Py_PreInitializeFromConfig(const PyConfig *config, const _PyArgv *args) { assert(config != NULL); PyStatus status = _PyRuntime_Initialize(); if (_PyStatus_EXCEPTION(status)) { return status; } _PyRuntimeState *runtime = &_PyRuntime; if (runtime->preinitialized) { /* Already initialized: do nothing */ return _PyStatus_OK(); } PyPreConfig preconfig; _PyPreConfig_InitFromConfig(&preconfig, config); if (!config->parse_argv) { return Py_PreInitialize(&preconfig); } else if (args == NULL) { _PyArgv config_args = { .use_bytes_argv = 0, .argc = config->argv.length, .wchar_argv = config->argv.items}; return _Py_PreInitializeFromPyArgv(&preconfig, &config_args); } else { return _Py_PreInitializeFromPyArgv(&preconfig, args); } } /* Begin interpreter initialization * * On return, the first thread and interpreter state have been created, * but the compiler, signal handling, multithreading and * multiple interpreter support, and codec infrastructure are not yet * available. * * The import system will support builtin and frozen modules only. * The only supported io is writing to sys.stderr * * If any operation invoked by this function fails, a fatal error is * issued and the function does not return. * * Any code invoked from this function should *not* assume it has access * to the Python C API (unless the API is explicitly listed as being * safe to call without calling Py_Initialize first) */ static PyStatus pyinit_core(_PyRuntimeState *runtime, const PyConfig *src_config, PyThreadState **tstate_p) { PyStatus status; status = _Py_PreInitializeFromConfig(src_config, NULL); if (_PyStatus_EXCEPTION(status)) { return status; } PyConfig config; _PyConfig_InitCompatConfig(&config); status = _PyConfig_Copy(&config, src_config); if (_PyStatus_EXCEPTION(status)) { goto done; } status = PyConfig_Read(&config); if (_PyStatus_EXCEPTION(status)) { goto done; } if (!runtime->core_initialized) { status = pyinit_config(runtime, tstate_p, &config); } else { status = pyinit_core_reconfigure(runtime, tstate_p, &config); } if (_PyStatus_EXCEPTION(status)) { goto done; } done: PyConfig_Clear(&config); return status; } /* Py_Initialize() has already been called: update the main interpreter configuration. Example of bpo-34008: Py_Main() called after Py_Initialize(). */ static PyStatus _Py_ReconfigureMainInterpreter(PyThreadState *tstate) { PyConfig *config = &tstate->interp->config; PyObject *argv = _PyWideStringList_AsList(&config->argv); if (argv == NULL) { return _PyStatus_NO_MEMORY(); \ } int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv); Py_DECREF(argv); if (res < 0) { return _PyStatus_ERR("fail to set sys.argv"); } return _PyStatus_OK(); } static PyStatus init_interp_main(PyThreadState *tstate) { assert(!_PyErr_Occurred(tstate)); PyStatus status; int is_main_interp = _Py_IsMainInterpreter(tstate); PyInterpreterState *interp = tstate->interp; PyConfig *config = &interp->config; if (!config->_install_importlib) { /* Special mode for freeze_importlib: run with no import system * * This means anything which needs support from extension modules * or pure Python code in the standard library won't work. */ if (is_main_interp) { interp->runtime->initialized = 1; } return _PyStatus_OK(); } if (is_main_interp) { if (_PyTime_Init() < 0) { return _PyStatus_ERR("can't initialize time"); } } if (_PySys_InitMain(tstate) < 0) { return _PyStatus_ERR("can't finish initializing sys"); } status = init_importlib_external(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } if (is_main_interp) { /* initialize the faulthandler module */ status = _PyFaulthandler_Init(config->faulthandler); if (_PyStatus_EXCEPTION(status)) { return status; } } status = _PyUnicode_InitEncodings(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } if (is_main_interp) { if (config->install_signal_handlers) { status = init_signals(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } } if (_PyTraceMalloc_Init(config->tracemalloc) < 0) { return _PyStatus_ERR("can't initialize tracemalloc"); } } status = init_sys_streams(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } status = init_set_builtins_open(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } status = add_main_module(interp); if (_PyStatus_EXCEPTION(status)) { return status; } if (is_main_interp) { /* Initialize warnings. */ PyObject *warnoptions = PySys_GetObject("warnoptions"); if (warnoptions != NULL && PyList_Size(warnoptions) > 0) { PyObject *warnings_module = PyImport_ImportModule("warnings"); if (warnings_module == NULL) { fprintf(stderr, "'import warnings' failed; traceback:\n"); _PyErr_Print(tstate); } Py_XDECREF(warnings_module); } interp->runtime->initialized = 1; } if (config->site_import) { status = init_import_site(); if (_PyStatus_EXCEPTION(status)) { return status; } } if (is_main_interp) { #ifndef MS_WINDOWS emit_stderr_warning_for_legacy_locale(interp->runtime); #endif } assert(!_PyErr_Occurred(tstate)); return _PyStatus_OK(); } /* Update interpreter state based on supplied configuration settings * * After calling this function, most of the restrictions on the interpreter * are lifted. The only remaining incomplete settings are those related * to the main module (sys.argv[0], __main__ metadata) * * Calling this when the interpreter is not initializing, is already * initialized or without a valid current thread state is a fatal error. * Other errors should be reported as normal Python exceptions with a * non-zero return code. */ static PyStatus pyinit_main(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; if (!interp->runtime->core_initialized) { return _PyStatus_ERR("runtime core not initialized"); } if (interp->runtime->initialized) { return _Py_ReconfigureMainInterpreter(tstate); } PyStatus status = init_interp_main(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } return _PyStatus_OK(); } PyStatus _Py_InitializeMain(void) { PyStatus status = _PyRuntime_Initialize(); if (_PyStatus_EXCEPTION(status)) { return status; } _PyRuntimeState *runtime = &_PyRuntime; PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); return pyinit_main(tstate); } PyStatus Py_InitializeFromConfig(const PyConfig *config) { if (config == NULL) { return _PyStatus_ERR("initialization config is NULL"); } PyStatus status; status = _PyRuntime_Initialize(); if (_PyStatus_EXCEPTION(status)) { return status; } _PyRuntimeState *runtime = &_PyRuntime; PyThreadState *tstate = NULL; status = pyinit_core(runtime, config, &tstate); if (_PyStatus_EXCEPTION(status)) { return status; } config = &tstate->interp->config; if (config->_init_main) { status = pyinit_main(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } } return _PyStatus_OK(); } void Py_InitializeEx(int install_sigs) { PyStatus status; status = _PyRuntime_Initialize(); if (_PyStatus_EXCEPTION(status)) { Py_ExitStatusException(status); } _PyRuntimeState *runtime = &_PyRuntime; if (runtime->initialized) { /* bpo-33932: Calling Py_Initialize() twice does nothing. */ return; } PyConfig config; _PyConfig_InitCompatConfig(&config); config.install_signal_handlers = install_sigs; status = Py_InitializeFromConfig(&config); if (_PyStatus_EXCEPTION(status)) { Py_ExitStatusException(status); } } void Py_Initialize(void) { Py_InitializeEx(1); } /* Flush stdout and stderr */ static int file_is_closed(PyObject *fobj) { int r; PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); if (tmp == NULL) { PyErr_Clear(); return 0; } r = PyObject_IsTrue(tmp); Py_DECREF(tmp); if (r < 0) PyErr_Clear(); return r > 0; } static int flush_std_files(void) { PyObject *fout = _PySys_GetObjectId(&PyId_stdout); PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); PyObject *tmp; int status = 0; if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush); if (tmp == NULL) { PyErr_WriteUnraisable(fout); status = -1; } else Py_DECREF(tmp); } if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush); if (tmp == NULL) { PyErr_Clear(); status = -1; } else Py_DECREF(tmp); } return status; } /* Undo the effect of Py_Initialize(). Beware: if multiple interpreter and/or thread states exist, these are not wiped out; only the current thread and interpreter state are deleted. But since everything else is deleted, those other interpreter and thread states should no longer be used. (XXX We should do better, e.g. wipe out all interpreters and threads.) Locking: as above. */ static void finalize_interp_types(PyThreadState *tstate, int is_main_interp) { if (is_main_interp) { /* Sundry finalizers */ _PyFrame_Fini(); _PyTuple_Fini(); _PyList_Fini(); _PySet_Fini(); _PyBytes_Fini(); } _PyLong_Fini(tstate); if (is_main_interp) { _PyFloat_Fini(); _PyDict_Fini(); _PySlice_Fini(); } _PyWarnings_Fini(tstate->interp); if (is_main_interp) { _Py_HashRandomization_Fini(); _PyArg_Fini(); _PyAsyncGen_Fini(); _PyContext_Fini(); } /* Cleanup Unicode implementation */ _PyUnicode_Fini(tstate); if (is_main_interp) { _Py_ClearFileSystemEncoding(); } } static void finalize_interp_clear(PyThreadState *tstate) { int is_main_interp = _Py_IsMainInterpreter(tstate); /* Clear interpreter state and all thread states */ PyInterpreterState_Clear(tstate->interp); /* Trigger a GC collection on subinterpreters*/ if (!is_main_interp) { _PyGC_CollectNoFail(); } finalize_interp_types(tstate, is_main_interp); if (is_main_interp) { /* XXX Still allocated: - various static ad-hoc pointers to interned strings - int and float free list blocks - whatever various modules and libraries allocate */ PyGrammar_RemoveAccelerators(&_PyParser_Grammar); _PyExc_Fini(); } _PyGC_Fini(tstate); } static void finalize_interp_delete(PyThreadState *tstate) { if (_Py_IsMainInterpreter(tstate)) { /* Cleanup auto-thread-state */ _PyGILState_Fini(tstate); } PyInterpreterState_Delete(tstate->interp); } int Py_FinalizeEx(void) { int status = 0; _PyRuntimeState *runtime = &_PyRuntime; if (!runtime->initialized) { return status; } /* Get current thread state and interpreter pointer */ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); PyInterpreterState *interp = tstate->interp; // Wrap up existing "threading"-module-created, non-daemon threads. wait_for_thread_shutdown(tstate); // Make any remaining pending calls. _Py_FinishPendingCalls(tstate); /* The interpreter is still entirely intact at this point, and the * exit funcs may be relying on that. In particular, if some thread * or exit func is still waiting to do an import, the import machinery * expects Py_IsInitialized() to return true. So don't say the * runtime is uninitialized until after the exit funcs have run. * Note that Threading.py uses an exit func to do a join on all the * threads created thru it, so this also protects pending imports in * the threads created via Threading. */ call_py_exitfuncs(tstate); /* Copy the core config, PyInterpreterState_Delete() free the core config memory */ #ifdef Py_REF_DEBUG int show_ref_count = interp->config.show_ref_count; #endif #ifdef Py_TRACE_REFS int dump_refs = interp->config.dump_refs; #endif #ifdef WITH_PYMALLOC int malloc_stats = interp->config.malloc_stats; #endif /* Remaining threads (e.g. daemon threads) will automatically exit after taking the GIL (in PyEval_RestoreThread()). */ runtime->finalizing = tstate; runtime->initialized = 0; runtime->core_initialized = 0; /* Flush sys.stdout and sys.stderr */ if (flush_std_files() < 0) { status = -1; } /* Disable signal handling */ PyOS_FiniInterrupts(); /* Collect garbage. This may call finalizers; it's nice to call these * before all modules are destroyed. * XXX If a __del__ or weakref callback is triggered here, and tries to * XXX import a module, bad things can happen, because Python no * XXX longer believes it's initialized. * XXX Fatal Python error: Interpreter not initialized (version mismatch?) * XXX is easy to provoke that way. I've also seen, e.g., * XXX Exception exceptions.ImportError: 'No module named sha' * XXX in <function callback at 0x008F5718> ignored * XXX but I'm unclear on exactly how that one happens. In any case, * XXX I haven't seen a real-life report of either of these. */ _PyGC_CollectIfEnabled(); /* Clear all loghooks */ /* We want minimal exposure of this function, so define the extern * here. The linker should discover the correct function without * exporting a symbol. */ extern void _PySys_ClearAuditHooks(void); _PySys_ClearAuditHooks(); /* Destroy all modules */ _PyImport_Cleanup(tstate); /* Print debug stats if any */ _PyEval_Fini(); /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ if (flush_std_files() < 0) { status = -1; } /* Collect final garbage. This disposes of cycles created by * class definitions, for example. * XXX This is disabled because it caused too many problems. If * XXX a __del__ or weakref callback triggers here, Python code has * XXX a hard time running, because even the sys module has been * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). * XXX One symptom is a sequence of information-free messages * XXX coming from threads (if a __del__ or callback is invoked, * XXX other threads can execute too, and any exception they encounter * XXX triggers a comedy of errors as subsystem after subsystem * XXX fails to find what it *expects* to find in sys to help report * XXX the exception and consequent unexpected failures). I've also * XXX seen segfaults then, after adding print statements to the * XXX Python code getting called. */ #if 0 _PyGC_CollectIfEnabled(); #endif /* Disable tracemalloc after all Python objects have been destroyed, so it is possible to use tracemalloc in objects destructor. */ _PyTraceMalloc_Fini(); /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ _PyImport_Fini(); /* Cleanup typeobject.c's internal caches. */ _PyType_Fini(); /* unload faulthandler module */ _PyFaulthandler_Fini(); /* dump hash stats */ _PyHash_Fini(); #ifdef Py_REF_DEBUG if (show_ref_count) { _PyDebug_PrintTotalRefs(); } #endif #ifdef Py_TRACE_REFS /* Display all objects still alive -- this can invoke arbitrary * __repr__ overrides, so requires a mostly-intact interpreter. * Alas, a lot of stuff may still be alive now that will be cleaned * up later. */ if (dump_refs) { _Py_PrintReferences(stderr); } #endif /* Py_TRACE_REFS */ finalize_interp_clear(tstate); finalize_interp_delete(tstate); #ifdef Py_TRACE_REFS /* Display addresses (& refcnts) of all objects still alive. * An address can be used to find the repr of the object, printed * above by _Py_PrintReferences. */ if (dump_refs) { _Py_PrintReferenceAddresses(stderr); } #endif /* Py_TRACE_REFS */ #ifdef WITH_PYMALLOC if (malloc_stats) { _PyObject_DebugMallocStats(stderr); } #endif call_ll_exitfuncs(runtime); _PyRuntime_Finalize(); return status; } void Py_Finalize(void) { Py_FinalizeEx(); } /* Create and initialize a new interpreter and thread, and return the new thread. This requires that Py_Initialize() has been called first. Unsuccessful initialization yields a NULL pointer. Note that *no* exception information is available even in this case -- the exception information is held in the thread, and there is no thread. Locking: as above. */ static PyStatus new_interpreter(PyThreadState **tstate_p) { PyStatus status; status = _PyRuntime_Initialize(); if (_PyStatus_EXCEPTION(status)) { return status; } _PyRuntimeState *runtime = &_PyRuntime; if (!runtime->initialized) { return _PyStatus_ERR("Py_Initialize must be called first"); } /* Issue #10915, #15751: The GIL API doesn't work with multiple interpreters: disable PyGILState_Check(). */ _PyGILState_check_enabled = 0; PyInterpreterState *interp = PyInterpreterState_New(); if (interp == NULL) { *tstate_p = NULL; return _PyStatus_OK(); } PyThreadState *tstate = PyThreadState_New(interp); if (tstate == NULL) { PyInterpreterState_Delete(interp); *tstate_p = NULL; return _PyStatus_OK(); } PyThreadState *save_tstate = PyThreadState_Swap(tstate); /* Copy the current interpreter config into the new interpreter */ PyConfig *config; if (save_tstate != NULL) { config = &save_tstate->interp->config; } else { /* No current thread state, copy from the main interpreter */ PyInterpreterState *main_interp = PyInterpreterState_Main(); config = &main_interp->config; } status = _PyConfig_Copy(&interp->config, config); if (_PyStatus_EXCEPTION(status)) { goto error; } status = pycore_interp_init(tstate); if (_PyStatus_EXCEPTION(status)) { goto error; } status = init_interp_main(tstate); if (_PyStatus_EXCEPTION(status)) { goto error; } *tstate_p = tstate; return _PyStatus_OK(); error: *tstate_p = NULL; /* Oops, it didn't work. Undo it all. */ PyErr_PrintEx(0); PyThreadState_Clear(tstate); PyThreadState_Delete(tstate); PyInterpreterState_Delete(interp); PyThreadState_Swap(save_tstate); return status; } PyThreadState * Py_NewInterpreter(void) { PyThreadState *tstate = NULL; PyStatus status = new_interpreter(&tstate); if (_PyStatus_EXCEPTION(status)) { Py_ExitStatusException(status); } return tstate; } /* Delete an interpreter and its last thread. This requires that the given thread state is current, that the thread has no remaining frames, and that it is its interpreter's only remaining thread. It is a fatal error to violate these constraints. (Py_FinalizeEx() doesn't have these constraints -- it zaps everything, regardless.) Locking: as above. */ void Py_EndInterpreter(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; if (tstate != _PyThreadState_GET()) { Py_FatalError("Py_EndInterpreter: thread is not current"); } if (tstate->frame != NULL) { Py_FatalError("Py_EndInterpreter: thread still has a frame"); } interp->finalizing = 1; // Wrap up existing "threading"-module-created, non-daemon threads. wait_for_thread_shutdown(tstate); call_py_exitfuncs(tstate); if (tstate != interp->tstate_head || tstate->next != NULL) { Py_FatalError("Py_EndInterpreter: not the last thread"); } _PyImport_Cleanup(tstate); finalize_interp_clear(tstate); finalize_interp_delete(tstate); } /* Add the __main__ module */ static PyStatus add_main_module(PyInterpreterState *interp) { PyObject *m, *d, *loader, *ann_dict; m = PyImport_AddModule("__main__"); if (m == NULL) return _PyStatus_ERR("can't create __main__ module"); d = PyModule_GetDict(m); ann_dict = PyDict_New(); if ((ann_dict == NULL) || (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) { return _PyStatus_ERR("Failed to initialize __main__.__annotations__"); } Py_DECREF(ann_dict); if (PyDict_GetItemString(d, "__builtins__") == NULL) { PyObject *bimod = PyImport_ImportModule("builtins"); if (bimod == NULL) { return _PyStatus_ERR("Failed to retrieve builtins module"); } if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { return _PyStatus_ERR("Failed to initialize __main__.__builtins__"); } Py_DECREF(bimod); } /* Main is a little special - imp.is_builtin("__main__") will return * False, but BuiltinImporter is still the most appropriate initial * setting for its __loader__ attribute. A more suitable value will * be set if __main__ gets further initialized later in the startup * process. */ loader = PyDict_GetItemString(d, "__loader__"); if (loader == NULL || loader == Py_None) { PyObject *loader = PyObject_GetAttrString(interp->importlib, "BuiltinImporter"); if (loader == NULL) { return _PyStatus_ERR("Failed to retrieve BuiltinImporter"); } if (PyDict_SetItemString(d, "__loader__", loader) < 0) { return _PyStatus_ERR("Failed to initialize __main__.__loader__"); } Py_DECREF(loader); } return _PyStatus_OK(); } /* Import the site module (not into __main__ though) */ static PyStatus init_import_site(void) { PyObject *m; m = PyImport_ImportModule("site"); if (m == NULL) { return _PyStatus_ERR("Failed to import the site module"); } Py_DECREF(m); return _PyStatus_OK(); } /* Check if a file descriptor is valid or not. Return 0 if the file descriptor is invalid, return non-zero otherwise. */ static int is_valid_fd(int fd) { /* dup() is faster than fstat(): fstat() can require input/output operations, whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python startup. Problem: dup() doesn't check if the file descriptor is valid on some platforms. bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with EBADF. FreeBSD has similar issue (bpo-32849). Only use dup() on platforms where dup() is enough to detect invalid FD in corner cases: on Linux and Windows (bpo-32849). */ #if defined(__linux__) || defined(MS_WINDOWS) if (fd < 0) { return 0; } int fd2; _Py_BEGIN_SUPPRESS_IPH fd2 = dup(fd); if (fd2 >= 0) { close(fd2); } _Py_END_SUPPRESS_IPH return (fd2 >= 0); #else struct stat st; return (fstat(fd, &st) == 0); #endif } /* returns Py_None if the fd is not valid */ static PyObject* create_stdio(const PyConfig *config, PyObject* io, int fd, int write_mode, const char* name, const wchar_t* encoding, const wchar_t* errors) { PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; const char* mode; const char* newline; PyObject *line_buffering, *write_through; int buffering, isatty; _Py_IDENTIFIER(open); _Py_IDENTIFIER(isatty); _Py_IDENTIFIER(TextIOWrapper); _Py_IDENTIFIER(mode); const int buffered_stdio = config->buffered_stdio; if (!is_valid_fd(fd)) Py_RETURN_NONE; /* stdin is always opened in buffered mode, first because it shouldn't make a difference in common use cases, second because TextIOWrapper depends on the presence of a read1() method which only exists on buffered streams. */ if (!buffered_stdio && write_mode) buffering = 0; else buffering = -1; if (write_mode) mode = "wb"; else mode = "rb"; buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO", fd, mode, buffering, Py_None, Py_None, /* encoding, errors */ Py_None, Py_False); /* newline, closefd */ if (buf == NULL) goto error; if (buffering) { _Py_IDENTIFIER(raw); raw = _PyObject_GetAttrId(buf, &PyId_raw); if (raw == NULL) goto error; } else { raw = buf; Py_INCREF(raw); } #ifdef MS_WINDOWS /* Windows console IO is always UTF-8 encoded */ if (PyWindowsConsoleIO_Check(raw)) encoding = L"utf-8"; #endif text = PyUnicode_FromString(name); if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) goto error; res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty); if (res == NULL) goto error; isatty = PyObject_IsTrue(res); Py_DECREF(res); if (isatty == -1) goto error; if (!buffered_stdio) write_through = Py_True; else write_through = Py_False; if (buffered_stdio && (isatty || fd == fileno(stderr))) line_buffering = Py_True; else line_buffering = Py_False; Py_CLEAR(raw); Py_CLEAR(text); #ifdef MS_WINDOWS /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" newlines to "\n". sys.stdout and sys.stderr: translate "\n" to "\r\n". */ newline = NULL; #else /* sys.stdin: split lines at "\n". sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ newline = "\n"; #endif PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1); if (encoding_str == NULL) { Py_CLEAR(buf); goto error; } PyObject *errors_str = PyUnicode_FromWideChar(errors, -1); if (errors_str == NULL) { Py_CLEAR(buf); Py_CLEAR(encoding_str); goto error; } stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO", buf, encoding_str, errors_str, newline, line_buffering, write_through); Py_CLEAR(buf); Py_CLEAR(encoding_str); Py_CLEAR(errors_str); if (stream == NULL) goto error; if (write_mode) mode = "w"; else mode = "r"; text = PyUnicode_FromString(mode); if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) goto error; Py_CLEAR(text); return stream; error: Py_XDECREF(buf); Py_XDECREF(stream); Py_XDECREF(text); Py_XDECREF(raw); if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) { /* Issue #24891: the file descriptor was closed after the first is_valid_fd() check was called. Ignore the OSError and set the stream to None. */ PyErr_Clear(); Py_RETURN_NONE; } return NULL; } /* Set builtins.open to io.OpenWrapper */ static PyStatus init_set_builtins_open(PyThreadState *tstate) { PyObject *iomod = NULL, *wrapper; PyObject *bimod = NULL; PyStatus res = _PyStatus_OK(); if (!(iomod = PyImport_ImportModule("io"))) { goto error; } if (!(bimod = PyImport_ImportModule("builtins"))) { goto error; } if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { goto error; } /* Set builtins.open */ if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { Py_DECREF(wrapper); goto error; } Py_DECREF(wrapper); goto done; error: res = _PyStatus_ERR("can't initialize io.open"); done: Py_XDECREF(bimod); Py_XDECREF(iomod); return res; } /* Initialize sys.stdin, stdout, stderr and builtins.open */ static PyStatus init_sys_streams(PyThreadState *tstate) { PyObject *iomod = NULL; PyObject *m; PyObject *std = NULL; int fd; PyObject * encoding_attr; PyStatus res = _PyStatus_OK(); const PyConfig *config = &tstate->interp->config; /* Check that stdin is not a directory Using shell redirection, you can redirect stdin to a directory, crashing the Python interpreter. Catch this common mistake here and output a useful error message. Note that under MS Windows, the shell already prevents that. */ #ifndef MS_WINDOWS struct _Py_stat_struct sb; if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 && S_ISDIR(sb.st_mode)) { return _PyStatus_ERR("<stdin> is a directory, cannot continue"); } #endif /* 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 (!(iomod = PyImport_ImportModule("io"))) { goto error; } /* Set sys.stdin */ fd = fileno(stdin); /* Under some conditions stdin, stdout and stderr may not be connected * and fileno() may point to an invalid file descriptor. For example * GUI apps don't have valid standard streams by default. */ std = create_stdio(config, iomod, fd, 0, "<stdin>", config->stdio_encoding, config->stdio_errors); if (std == NULL) goto error; PySys_SetObject("__stdin__", std); _PySys_SetObjectId(&PyId_stdin, std); Py_DECREF(std); /* Set sys.stdout */ fd = fileno(stdout); std = create_stdio(config, iomod, fd, 1, "<stdout>", config->stdio_encoding, config->stdio_errors); if (std == NULL) goto error; PySys_SetObject("__stdout__", std); _PySys_SetObjectId(&PyId_stdout, std); Py_DECREF(std); #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ /* Set sys.stderr, replaces the preliminary stderr */ fd = fileno(stderr); std = create_stdio(config, iomod, fd, 1, "<stderr>", config->stdio_encoding, L"backslashreplace"); if (std == NULL) goto error; /* Same as hack above, pre-import stderr's codec to avoid recursion when import.c tries to write to stderr in verbose mode. */ encoding_attr = PyObject_GetAttrString(std, "encoding"); if (encoding_attr != NULL) { const char *std_encoding = PyUnicode_AsUTF8(encoding_attr); if (std_encoding != NULL) { PyObject *codec_info = _PyCodec_Lookup(std_encoding); Py_XDECREF(codec_info); } Py_DECREF(encoding_attr); } _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */ if (PySys_SetObject("__stderr__", std) < 0) { Py_DECREF(std); goto error; } if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { Py_DECREF(std); goto error; } Py_DECREF(std); #endif goto done; error: res = _PyStatus_ERR("can't initialize sys standard streams"); done: _Py_ClearStandardStreamEncoding(); Py_XDECREF(iomod); return res; } static void _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp, PyThreadState *tstate) { fputc('\n', stderr); fflush(stderr); /* display the current Python stack */ _Py_DumpTracebackThreads(fd, interp, tstate); } /* Print the current exception (if an exception is set) with its traceback, or display the current Python stack. Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is called on catastrophic cases. Return 1 if the traceback was displayed, 0 otherwise. */ static int _Py_FatalError_PrintExc(int fd) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *ferr, *res; PyObject *exception, *v, *tb; int has_tb; _PyErr_Fetch(tstate, &exception, &v, &tb); if (exception == NULL) { /* No current exception */ return 0; } ferr = _PySys_GetObjectId(&PyId_stderr); if (ferr == NULL || ferr == Py_None) { /* sys.stderr is not set yet or set to None, no need to try to display the exception */ return 0; } _PyErr_NormalizeException(tstate, &exception, &v, &tb); if (tb == NULL) { tb = Py_None; Py_INCREF(tb); } PyException_SetTraceback(v, tb); if (exception == NULL) { /* PyErr_NormalizeException() failed */ return 0; } has_tb = (tb != Py_None); PyErr_Display(exception, v, tb); Py_XDECREF(exception); Py_XDECREF(v); Py_XDECREF(tb); /* sys.stderr may be buffered: call sys.stderr.flush() */ res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush); if (res == NULL) { _PyErr_Clear(tstate); } else { Py_DECREF(res); } return has_tb; } /* Print fatal error message and abort */ #ifdef MS_WINDOWS static void fatal_output_debug(const char *msg) { /* buffer of 256 bytes allocated on the stack */ WCHAR buffer[256 / sizeof(WCHAR)]; size_t buflen = Py_ARRAY_LENGTH(buffer) - 1; size_t msglen; OutputDebugStringW(L"Fatal Python error: "); msglen = strlen(msg); while (msglen) { size_t i; if (buflen > msglen) { buflen = msglen; } /* Convert the message to wchar_t. This uses a simple one-to-one conversion, assuming that the this error message actually uses ASCII only. If this ceases to be true, we will have to convert. */ for (i=0; i < buflen; ++i) { buffer[i] = msg[i]; } buffer[i] = L'\0'; OutputDebugStringW(buffer); msg += buflen; msglen -= buflen; } OutputDebugStringW(L"\n"); } #endif static void fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime) { fprintf(stream, "Python runtime state: "); if (runtime->finalizing) { fprintf(stream, "finalizing (tstate=%p)", runtime->finalizing); } else if (runtime->initialized) { fprintf(stream, "initialized"); } else if (runtime->core_initialized) { fprintf(stream, "core initialized"); } else if (runtime->preinitialized) { fprintf(stream, "preinitialized"); } else if (runtime->preinitializing) { fprintf(stream, "preinitializing"); } else { fprintf(stream, "unknown"); } fprintf(stream, "\n"); fflush(stream); } static void _Py_NO_RETURN fatal_error(const char *prefix, const char *msg, int status) { FILE *stream = stderr; const int fd = fileno(stream); static int reentrant = 0; if (reentrant) { /* Py_FatalError() caused a second fatal error. Example: flush_std_files() raises a recursion error. */ goto exit; } reentrant = 1; fprintf(stream, "Fatal Python error: "); if (prefix) { fputs(prefix, stream); fputs(": ", stream); } if (msg) { fputs(msg, stream); } else { fprintf(stream, "<message not set>"); } fputs("\n", stream); fflush(stream); /* it helps in Windows debug build */ _PyRuntimeState *runtime = &_PyRuntime; fatal_error_dump_runtime(stream, runtime); PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); PyInterpreterState *interp = NULL; if (tstate != NULL) { interp = tstate->interp; } /* Check if the current thread has a Python thread state and holds the GIL. tss_tstate is NULL if Py_FatalError() is called from a C thread which has no Python thread state. tss_tstate != tstate if the current Python thread does not hold the GIL. */ PyThreadState *tss_tstate = PyGILState_GetThisThreadState(); int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate); if (has_tstate_and_gil) { /* If an exception is set, print the exception with its traceback */ if (!_Py_FatalError_PrintExc(fd)) { /* No exception is set, or an exception is set without traceback */ _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate); } } else { _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate); } /* The main purpose of faulthandler is to display the traceback. This function already did its best to display a traceback. Disable faulthandler to prevent writing a second traceback on abort(). */ _PyFaulthandler_Fini(); /* Check if the current Python thread hold the GIL */ if (has_tstate_and_gil) { /* Flush sys.stdout and sys.stderr */ flush_std_files(); } #ifdef MS_WINDOWS fatal_output_debug(msg); #endif /* MS_WINDOWS */ exit: if (status < 0) { #if defined(MS_WINDOWS) && defined(_DEBUG) DebugBreak(); #endif abort(); } else { exit(status); } } void _Py_NO_RETURN Py_FatalError(const char *msg) { fatal_error(NULL, msg, -1); } void _Py_NO_RETURN Py_ExitStatusException(PyStatus status) { if (_PyStatus_IS_EXIT(status)) { exit(status.exitcode); } else if (_PyStatus_IS_ERROR(status)) { fatal_error(status.func, status.err_msg, 1); } else { Py_FatalError("Py_ExitStatusException() must not be called on success"); } } /* Clean up and exit */ # include "pythread.h" /* For the atexit module. */ void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module) { PyInterpreterState *is = _PyInterpreterState_GET_UNSAFE(); /* Guard against API misuse (see bpo-17852) */ assert(is->pyexitfunc == NULL || is->pyexitfunc == func); is->pyexitfunc = func; is->pyexitmodule = module; } static void call_py_exitfuncs(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; if (interp->pyexitfunc == NULL) return; (*interp->pyexitfunc)(interp->pyexitmodule); _PyErr_Clear(tstate); } /* Wait until threading._shutdown completes, provided the threading module was imported in the first place. The shutdown routine will wait until all non-daemon "threading" threads have completed. */ static void wait_for_thread_shutdown(PyThreadState *tstate) { _Py_IDENTIFIER(_shutdown); PyObject *result; PyObject *threading = _PyImport_GetModuleId(&PyId_threading); if (threading == NULL) { if (_PyErr_Occurred(tstate)) { PyErr_WriteUnraisable(NULL); } /* else: threading not imported */ return; } result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown); if (result == NULL) { PyErr_WriteUnraisable(threading); } else { Py_DECREF(result); } Py_DECREF(threading); } #define NEXITFUNCS 32 int Py_AtExit(void (*func)(void)) { if (_PyRuntime.nexitfuncs >= NEXITFUNCS) return -1; _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func; return 0; } static void call_ll_exitfuncs(_PyRuntimeState *runtime) { while (runtime->nexitfuncs > 0) { /* pop last function from the list */ runtime->nexitfuncs--; void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs]; runtime->exitfuncs[runtime->nexitfuncs] = NULL; exitfunc(); } fflush(stdout); fflush(stderr); } void _Py_NO_RETURN Py_Exit(int sts) { if (Py_FinalizeEx() < 0) { sts = 120; } exit(sts); } static PyStatus init_signals(PyThreadState *tstate) { #ifdef SIGPIPE PyOS_setsig(SIGPIPE, SIG_IGN); #endif #ifdef SIGXFZ PyOS_setsig(SIGXFZ, SIG_IGN); #endif #ifdef SIGXFSZ PyOS_setsig(SIGXFSZ, SIG_IGN); #endif PyOS_InitInterrupts(); /* May imply initsignal() */ if (_PyErr_Occurred(tstate)) { return _PyStatus_ERR("can't import signal"); } return _PyStatus_OK(); } /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. * * All of the code in this function must only use async-signal-safe functions, * listed at `man 7 signal` or * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. */ void _Py_RestoreSignals(void) { #ifdef SIGPIPE PyOS_setsig(SIGPIPE, SIG_DFL); #endif #ifdef SIGXFZ PyOS_setsig(SIGXFZ, SIG_DFL); #endif #ifdef SIGXFSZ PyOS_setsig(SIGXFSZ, SIG_DFL); #endif } /* * The file descriptor fd is considered ``interactive'' if either * a) isatty(fd) is TRUE, or * b) the -i flag was given, and the filename associated with * the descriptor is NULL or "<stdin>" or "???". */ int Py_FdIsInteractive(FILE *fp, const char *filename) { if (isatty((int)fileno(fp))) return 1; if (!Py_InteractiveFlag) return 0; return (filename == NULL) || (strcmp(filename, "<stdin>") == 0) || (strcmp(filename, "???") == 0); } /* Wrappers around sigaction() or signal(). */ PyOS_sighandler_t PyOS_getsig(int sig) { #ifdef HAVE_SIGACTION struct sigaction context; if (sigaction(sig, NULL, &context) == -1) return SIG_ERR; return context.sa_handler; #else PyOS_sighandler_t handler; /* Special signal handling for the secure CRT in Visual Studio 2005 */ #if defined(_MSC_VER) && _MSC_VER >= 1400 switch (sig) { /* Only these signals are valid */ case SIGINT: case SIGILL: case SIGFPE: case SIGSEGV: case SIGTERM: case SIGBREAK: case SIGABRT: break; /* Don't call signal() with other values or it will assert */ default: return SIG_ERR; } #endif /* _MSC_VER && _MSC_VER >= 1400 */ handler = signal(sig, SIG_IGN); if (handler != SIG_ERR) signal(sig, handler); return handler; #endif } /* * All of the code in this function must only use async-signal-safe functions, * listed at `man 7 signal` or * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. */ PyOS_sighandler_t PyOS_setsig(int sig, PyOS_sighandler_t handler) { #ifdef HAVE_SIGACTION /* Some code in Modules/signalmodule.c depends on sigaction() being * used here if HAVE_SIGACTION is defined. Fix that if this code * changes to invalidate that assumption. */ struct sigaction context, ocontext; context.sa_handler = handler; sigemptyset(&context.sa_mask); context.sa_flags = 0; if (sigaction(sig, &context, &ocontext) == -1) return SIG_ERR; return ocontext.sa_handler; #else PyOS_sighandler_t oldhandler; oldhandler = signal(sig, handler); #ifdef HAVE_SIGINTERRUPT siginterrupt(sig, 1); #endif return oldhandler; #endif } #ifdef __cplusplus } #endif