Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6d43f6f

Browse filesBrowse files
authored
bpo-35713: Split _Py_InitializeCore into subfunctions (GH-11650)
* Split _Py_InitializeCore_impl() into subfunctions: add multiple pycore_init_xxx() functions * Preliminary sys.stderr is now set earlier to get an usable sys.stderr ealier. * Move code into _Py_Initialize_ReconfigureCore() to be able to call it from _Py_InitializeCore(). * Split _PyExc_Init(): create a new _PyBuiltins_AddExceptions() function. * Call _PyExc_Init() earlier in _Py_InitializeCore_impl() and new_interpreter() to get working exceptions earlier. * _Py_ReadyTypes() now returns _PyInitError rather than calling Py_FatalError(). * Misc code cleanup
1 parent 28f6cb3 commit 6d43f6f
Copy full SHA for 6d43f6f

File tree

Expand file treeCollapse file tree

9 files changed

+325
-333
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+325
-333
lines changed

‎Include/internal/pycore_pylifecycle.h

Copy file name to clipboardExpand all lines: Include/internal/pycore_pylifecycle.h
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ extern PyObject * _PyBuiltin_Init(void);
3030
extern _PyInitError _PySys_BeginInit(PyObject **sysmod);
3131
extern int _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp);
3232
extern _PyInitError _PyImport_Init(PyInterpreterState *interp);
33-
extern _PyInitError _PyExc_Init(PyObject * bltinmod);
33+
extern _PyInitError _PyExc_Init(void);
34+
extern _PyInitError _PyBuiltins_AddExceptions(PyObject * bltinmod);
3435
extern _PyInitError _PyImportHooks_Init(void);
3536
extern int _PyFloat_Init(void);
3637
extern _PyInitError _Py_HashRandomization_Init(const _PyCoreConfig *);
3738

38-
extern void _Py_ReadyTypes(void);
39+
extern _PyInitError _Py_ReadyTypes(void);
3940

4041
/* Various internal finalizers */
4142

+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reorganize Python initialization to get working exceptions and sys.stderr
2+
earlier.

‎Objects/exceptions.c

Copy file name to clipboardExpand all lines: Objects/exceptions.c
+63-51Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,7 @@ SimpleExtendsException(PyExc_Warning, ResourceWarning,
24922492
#endif /* MS_WINDOWS */
24932493

24942494
_PyInitError
2495-
_PyExc_Init(PyObject *bltinmod)
2495+
_PyExc_Init(void)
24962496
{
24972497
#define PRE_INIT(TYPE) \
24982498
if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \
@@ -2502,21 +2502,6 @@ _PyExc_Init(PyObject *bltinmod)
25022502
Py_INCREF(PyExc_ ## TYPE); \
25032503
}
25042504

2505-
#define POST_INIT(TYPE) \
2506-
if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) { \
2507-
return _Py_INIT_ERR("Module dictionary insertion problem."); \
2508-
}
2509-
2510-
#define INIT_ALIAS(NAME, TYPE) \
2511-
do { \
2512-
Py_INCREF(PyExc_ ## TYPE); \
2513-
Py_XDECREF(PyExc_ ## NAME); \
2514-
PyExc_ ## NAME = PyExc_ ## TYPE; \
2515-
if (PyDict_SetItemString(bdict, # NAME, PyExc_ ## NAME)) { \
2516-
return _Py_INIT_ERR("Module dictionary insertion problem."); \
2517-
} \
2518-
} while (0)
2519-
25202505
#define ADD_ERRNO(TYPE, CODE) \
25212506
do { \
25222507
PyObject *_code = PyLong_FromLong(CODE); \
@@ -2526,8 +2511,6 @@ _PyExc_Init(PyObject *bltinmod)
25262511
Py_DECREF(_code); \
25272512
} while (0)
25282513

2529-
PyObject *bdict;
2530-
25312514
PRE_INIT(BaseException);
25322515
PRE_INIT(Exception);
25332516
PRE_INIT(TypeError);
@@ -2596,6 +2579,68 @@ _PyExc_Init(PyObject *bltinmod)
25962579
PRE_INIT(ProcessLookupError);
25972580
PRE_INIT(TimeoutError);
25982581

2582+
if (preallocate_memerrors() < 0) {
2583+
return _Py_INIT_ERR("Could not preallocate MemoryError object");
2584+
}
2585+
2586+
/* Add exceptions to errnomap */
2587+
if (!errnomap) {
2588+
errnomap = PyDict_New();
2589+
if (!errnomap) {
2590+
return _Py_INIT_ERR("Cannot allocate map from errnos to OSError subclasses");
2591+
}
2592+
}
2593+
2594+
ADD_ERRNO(BlockingIOError, EAGAIN);
2595+
ADD_ERRNO(BlockingIOError, EALREADY);
2596+
ADD_ERRNO(BlockingIOError, EINPROGRESS);
2597+
ADD_ERRNO(BlockingIOError, EWOULDBLOCK);
2598+
ADD_ERRNO(BrokenPipeError, EPIPE);
2599+
#ifdef ESHUTDOWN
2600+
ADD_ERRNO(BrokenPipeError, ESHUTDOWN);
2601+
#endif
2602+
ADD_ERRNO(ChildProcessError, ECHILD);
2603+
ADD_ERRNO(ConnectionAbortedError, ECONNABORTED);
2604+
ADD_ERRNO(ConnectionRefusedError, ECONNREFUSED);
2605+
ADD_ERRNO(ConnectionResetError, ECONNRESET);
2606+
ADD_ERRNO(FileExistsError, EEXIST);
2607+
ADD_ERRNO(FileNotFoundError, ENOENT);
2608+
ADD_ERRNO(IsADirectoryError, EISDIR);
2609+
ADD_ERRNO(NotADirectoryError, ENOTDIR);
2610+
ADD_ERRNO(InterruptedError, EINTR);
2611+
ADD_ERRNO(PermissionError, EACCES);
2612+
ADD_ERRNO(PermissionError, EPERM);
2613+
ADD_ERRNO(ProcessLookupError, ESRCH);
2614+
ADD_ERRNO(TimeoutError, ETIMEDOUT);
2615+
2616+
return _Py_INIT_OK();
2617+
2618+
#undef PRE_INIT
2619+
#undef ADD_ERRNO
2620+
}
2621+
2622+
2623+
/* Add exception types to the builtins module */
2624+
_PyInitError
2625+
_PyBuiltins_AddExceptions(PyObject *bltinmod)
2626+
{
2627+
#define POST_INIT(TYPE) \
2628+
if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) { \
2629+
return _Py_INIT_ERR("Module dictionary insertion problem."); \
2630+
}
2631+
2632+
#define INIT_ALIAS(NAME, TYPE) \
2633+
do { \
2634+
Py_INCREF(PyExc_ ## TYPE); \
2635+
Py_XDECREF(PyExc_ ## NAME); \
2636+
PyExc_ ## NAME = PyExc_ ## TYPE; \
2637+
if (PyDict_SetItemString(bdict, # NAME, PyExc_ ## NAME)) { \
2638+
return _Py_INIT_ERR("Module dictionary insertion problem."); \
2639+
} \
2640+
} while (0)
2641+
2642+
PyObject *bdict;
2643+
25992644
bdict = PyModule_GetDict(bltinmod);
26002645
if (bdict == NULL) {
26012646
return _Py_INIT_ERR("exceptions bootstrapping error.");
@@ -2656,61 +2701,28 @@ _PyExc_Init(PyObject *bltinmod)
26562701
POST_INIT(BytesWarning);
26572702
POST_INIT(ResourceWarning);
26582703

2659-
if (!errnomap) {
2660-
errnomap = PyDict_New();
2661-
if (!errnomap) {
2662-
return _Py_INIT_ERR("Cannot allocate map from errnos to OSError subclasses");
2663-
}
2664-
}
2665-
26662704
/* OSError subclasses */
26672705
POST_INIT(ConnectionError);
26682706

26692707
POST_INIT(BlockingIOError);
2670-
ADD_ERRNO(BlockingIOError, EAGAIN);
2671-
ADD_ERRNO(BlockingIOError, EALREADY);
2672-
ADD_ERRNO(BlockingIOError, EINPROGRESS);
2673-
ADD_ERRNO(BlockingIOError, EWOULDBLOCK);
26742708
POST_INIT(BrokenPipeError);
2675-
ADD_ERRNO(BrokenPipeError, EPIPE);
2676-
#ifdef ESHUTDOWN
2677-
ADD_ERRNO(BrokenPipeError, ESHUTDOWN);
2678-
#endif
26792709
POST_INIT(ChildProcessError);
2680-
ADD_ERRNO(ChildProcessError, ECHILD);
26812710
POST_INIT(ConnectionAbortedError);
2682-
ADD_ERRNO(ConnectionAbortedError, ECONNABORTED);
26832711
POST_INIT(ConnectionRefusedError);
2684-
ADD_ERRNO(ConnectionRefusedError, ECONNREFUSED);
26852712
POST_INIT(ConnectionResetError);
2686-
ADD_ERRNO(ConnectionResetError, ECONNRESET);
26872713
POST_INIT(FileExistsError);
2688-
ADD_ERRNO(FileExistsError, EEXIST);
26892714
POST_INIT(FileNotFoundError);
2690-
ADD_ERRNO(FileNotFoundError, ENOENT);
26912715
POST_INIT(IsADirectoryError);
2692-
ADD_ERRNO(IsADirectoryError, EISDIR);
26932716
POST_INIT(NotADirectoryError);
2694-
ADD_ERRNO(NotADirectoryError, ENOTDIR);
26952717
POST_INIT(InterruptedError);
2696-
ADD_ERRNO(InterruptedError, EINTR);
26972718
POST_INIT(PermissionError);
2698-
ADD_ERRNO(PermissionError, EACCES);
2699-
ADD_ERRNO(PermissionError, EPERM);
27002719
POST_INIT(ProcessLookupError);
2701-
ADD_ERRNO(ProcessLookupError, ESRCH);
27022720
POST_INIT(TimeoutError);
2703-
ADD_ERRNO(TimeoutError, ETIMEDOUT);
27042721

2705-
if (preallocate_memerrors() < 0) {
2706-
return _Py_INIT_ERR("Could not preallocate MemoryError object");
2707-
}
27082722
return _Py_INIT_OK();
27092723

2710-
#undef PRE_INIT
27112724
#undef POST_INIT
27122725
#undef INIT_ALIAS
2713-
#undef ADD_ERRNO
27142726
}
27152727

27162728
void

‎Objects/fileobject.c

Copy file name to clipboardExpand all lines: Objects/fileobject.c
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
359359
Py_ssize_t n;
360360
int err;
361361

362+
/* The function can clear the current exception */
363+
assert(!PyErr_Occurred());
364+
362365
if (self->fd < 0) {
363366
/* fd might be invalid on Windows
364367
* I can't raise an exception here. It may lead to an
@@ -367,10 +370,11 @@ stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
367370
Py_RETURN_NONE;
368371
}
369372

370-
if (!PyArg_ParseTuple(args, "U", &unicode))
373+
if (!PyArg_ParseTuple(args, "U", &unicode)) {
371374
return NULL;
375+
}
372376

373-
/* encode Unicode to UTF-8 */
377+
/* Encode Unicode to UTF-8/surrogateescape */
374378
str = PyUnicode_AsUTF8AndSize(unicode, &n);
375379
if (str == NULL) {
376380
PyErr_Clear();

‎Objects/floatobject.c

Copy file name to clipboardExpand all lines: Objects/floatobject.c
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1999,8 +1999,9 @@ _PyFloat_Init(void)
19991999

20002000
/* Init float info */
20012001
if (FloatInfoType.tp_name == NULL) {
2002-
if (PyStructSequence_InitType2(&FloatInfoType, &floatinfo_desc) < 0)
2002+
if (PyStructSequence_InitType2(&FloatInfoType, &floatinfo_desc) < 0) {
20032003
return 0;
2004+
}
20042005
}
20052006
return 1;
20062007
}

‎Objects/longobject.c

Copy file name to clipboardExpand all lines: Objects/longobject.c
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5635,8 +5635,9 @@ _PyLong_Init(void)
56355635

56365636
/* initialize int_info */
56375637
if (Int_InfoType.tp_name == NULL) {
5638-
if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0)
5638+
if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) {
56395639
return 0;
5640+
}
56405641
}
56415642

56425643
return 1;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.