Changeset 388 for python/vendor/current/Modules/signalmodule.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Modules/signalmodule.c
r2 r388 8 8 9 9 #ifdef MS_WINDOWS 10 #include <Windows.h> 11 #ifdef HAVE_PROCESS_H 10 12 #include <process.h> 11 13 #endif 12 14 #endif 15 16 #ifdef HAVE_SIGNAL_H 13 17 #include <signal.h> 14 18 #endif 19 #ifdef HAVE_SYS_STAT_H 15 20 #include <sys/stat.h> 21 #endif 16 22 #ifdef HAVE_SYS_TIME_H 17 23 #include <sys/time.h> … … 29 35 #ifndef NSIG 30 36 # if defined(_NSIG) 31 # define NSIG _NSIG 37 # define NSIG _NSIG /* For BSD/SysV */ 32 38 # elif defined(_SIGMAX) 33 # define NSIG (_SIGMAX + 1) 39 # define NSIG (_SIGMAX + 1) /* For QNX */ 34 40 # elif defined(SIGMAX) 35 # define NSIG (SIGMAX + 1) 41 # define NSIG (SIGMAX + 1) /* For djgpp */ 36 42 # else 37 # define NSIG 64 43 # define NSIG 64 /* Use a reasonable default value */ 38 44 # endif 39 45 #endif … … 77 83 78 84 static struct { 79 80 85 int tripped; 86 PyObject *func; 81 87 } Handlers[NSIG]; 82 88 … … 121 127 r = PyTuple_New(2); 122 128 if (r == NULL) 123 129 return NULL; 124 130 125 131 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) { 126 Py_DECREF(r);127 132 Py_DECREF(r); 133 return NULL; 128 134 } 129 135 … … 131 137 132 138 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) { 133 134 139 Py_DECREF(r); 140 return NULL; 135 141 } 136 142 … … 144 150 signal_default_int_handler(PyObject *self, PyObject *args) 145 151 { 146 147 152 PyErr_SetNone(PyExc_KeyboardInterrupt); 153 return NULL; 148 154 } 149 155 … … 158 164 checksignals_witharg(void * unused) 159 165 { 160 return PyErr_CheckSignals(); 166 return PyErr_CheckSignals(); 167 } 168 169 static void 170 trip_signal(int sig_num) 171 { 172 Handlers[sig_num].tripped = 1; 173 if (is_tripped) 174 return; 175 /* Set is_tripped after setting .tripped, as it gets 176 cleared in PyErr_CheckSignals() before .tripped. */ 177 is_tripped = 1; 178 Py_AddPendingCall(checksignals_witharg, NULL); 179 if (wakeup_fd != -1) 180 write(wakeup_fd, "\0", 1); 161 181 } 162 182 … … 164 184 signal_handler(int sig_num) 165 185 { 186 int save_errno = errno; 187 188 #if defined(WITH_THREAD) && defined(WITH_PTH) 189 if (PyThread_get_thread_ident() != main_thread) { 190 pth_raise(*(pth_t *) main_thread, sig_num); 191 } 192 else 193 #endif 194 { 166 195 #ifdef WITH_THREAD 167 #ifdef WITH_PTH 168 if (PyThread_get_thread_ident() != main_thread) { 169 pth_raise(*(pth_t *) main_thread, sig_num); 170 return; 171 } 172 #endif 173 /* See NOTES section above */ 174 if (getpid() == main_pid) { 175 #endif 176 Handlers[sig_num].tripped = 1; 177 /* Set is_tripped after setting .tripped, as it gets 178 cleared in PyErr_CheckSignals() before .tripped. */ 179 is_tripped = 1; 180 Py_AddPendingCall(checksignals_witharg, NULL); 181 if (wakeup_fd != -1) 182 write(wakeup_fd, "\0", 1); 183 #ifdef WITH_THREAD 184 } 185 #endif 196 /* See NOTES section above */ 197 if (getpid() == main_pid) 198 #endif 199 { 200 trip_signal(sig_num); 201 } 202 203 #ifndef HAVE_SIGACTION 186 204 #ifdef SIGCHLD 187 if (sig_num == SIGCHLD) { 188 /* To avoid infinite recursion, this signal remains 189 reset until explicit re-instated. 190 Don't clear the 'func' field as it is our pointer 191 to the Python handler... */ 192 return; 193 } 194 #endif 195 PyOS_setsig(sig_num, signal_handler); 205 /* To avoid infinite recursion, this signal remains 206 reset until explicit re-instated. 207 Don't clear the 'func' field as it is our pointer 208 to the Python handler... */ 209 if (sig_num != SIGCHLD) 210 #endif 211 /* If the handler was not set up with sigaction, reinstall it. See 212 * Python/pythonrun.c for the implementation of PyOS_setsig which 213 * makes this true. See also issue8354. */ 214 PyOS_setsig(sig_num, signal_handler); 215 #endif 216 } 217 218 /* Issue #10311: asynchronously executing signal handlers should not 219 mutate errno under the feet of unsuspecting C code. */ 220 errno = save_errno; 196 221 } 197 222 … … 201 226 signal_alarm(PyObject *self, PyObject *args) 202 227 { 203 204 205 206 207 228 int t; 229 if (!PyArg_ParseTuple(args, "i:alarm", &t)) 230 return NULL; 231 /* alarm() returns the number of seconds remaining */ 232 return PyInt_FromLong((long)alarm(t)); 208 233 } 209 234 … … 218 243 signal_pause(PyObject *self) 219 244 { 220 221 222 223 224 225 226 227 228 229 230 245 Py_BEGIN_ALLOW_THREADS 246 (void)pause(); 247 Py_END_ALLOW_THREADS 248 /* make sure that any exceptions that got raised are propagated 249 * back into Python 250 */ 251 if (PyErr_CheckSignals()) 252 return NULL; 253 254 Py_INCREF(Py_None); 255 return Py_None; 231 256 } 232 257 PyDoc_STRVAR(pause_doc, … … 241 266 signal_signal(PyObject *self, PyObject *args) 242 267 { 243 PyObject *obj; 244 int sig_num; 245 PyObject *old_handler; 246 void (*func)(int); 247 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj)) 248 return NULL; 268 PyObject *obj; 269 int sig_num; 270 PyObject *old_handler; 271 void (*func)(int); 272 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj)) 273 return NULL; 274 #ifdef MS_WINDOWS 275 /* Validate that sig_num is one of the allowable signals */ 276 switch (sig_num) { 277 case SIGABRT: break; 278 #ifdef SIGBREAK 279 /* Issue #10003: SIGBREAK is not documented as permitted, but works 280 and corresponds to CTRL_BREAK_EVENT. */ 281 case SIGBREAK: break; 282 #endif 283 case SIGFPE: break; 284 case SIGILL: break; 285 case SIGINT: break; 286 case SIGSEGV: break; 287 case SIGTERM: break; 288 default: 289 PyErr_SetString(PyExc_ValueError, "invalid signal value"); 290 return NULL; 291 } 292 #endif 249 293 #ifdef WITH_THREAD 250 251 252 253 254 255 #endif 256 257 258 259 260 261 262 263 264 265 266 294 if (PyThread_get_thread_ident() != main_thread) { 295 PyErr_SetString(PyExc_ValueError, 296 "signal only works in main thread"); 297 return NULL; 298 } 299 #endif 300 if (sig_num < 1 || sig_num >= NSIG) { 301 PyErr_SetString(PyExc_ValueError, 302 "signal number out of range"); 303 return NULL; 304 } 305 if (obj == IgnoreHandler) 306 func = SIG_IGN; 307 else if (obj == DefaultHandler) 308 func = SIG_DFL; 309 else if (!PyCallable_Check(obj)) { 310 PyErr_SetString(PyExc_TypeError, 267 311 "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); 268 return NULL; 269 } 270 else 271 func = signal_handler; 272 if (PyOS_setsig(sig_num, func) == SIG_ERR) { 273 PyErr_SetFromErrno(PyExc_RuntimeError); 274 return NULL; 275 } 276 old_handler = Handlers[sig_num].func; 277 Handlers[sig_num].tripped = 0; 278 Py_INCREF(obj); 279 Handlers[sig_num].func = obj; 280 return old_handler; 312 return NULL; 313 } 314 else 315 func = signal_handler; 316 if (PyOS_setsig(sig_num, func) == SIG_ERR) { 317 PyErr_SetFromErrno(PyExc_RuntimeError); 318 return NULL; 319 } 320 old_handler = Handlers[sig_num].func; 321 Handlers[sig_num].tripped = 0; 322 Py_INCREF(obj); 323 Handlers[sig_num].func = obj; 324 if (old_handler != NULL) 325 return old_handler; 326 else 327 Py_RETURN_NONE; 281 328 } 282 329 … … 296 343 signal_getsignal(PyObject *self, PyObject *args) 297 344 { 298 int sig_num; 299 PyObject *old_handler; 300 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num)) 301 return NULL; 302 if (sig_num < 1 || sig_num >= NSIG) { 303 PyErr_SetString(PyExc_ValueError, 304 "signal number out of range"); 305 return NULL; 306 } 307 old_handler = Handlers[sig_num].func; 308 Py_INCREF(old_handler); 309 return old_handler; 345 int sig_num; 346 PyObject *old_handler; 347 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num)) 348 return NULL; 349 if (sig_num < 1 || sig_num >= NSIG) { 350 PyErr_SetString(PyExc_ValueError, 351 "signal number out of range"); 352 return NULL; 353 } 354 old_handler = Handlers[sig_num].func; 355 if (old_handler != NULL) { 356 Py_INCREF(old_handler); 357 return old_handler; 358 } 359 else { 360 Py_RETURN_NONE; 361 } 310 362 } 311 363 … … 329 381 signal_siginterrupt(PyObject *self, PyObject *args) 330 382 { 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 383 int sig_num; 384 int flag; 385 386 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag)) 387 return NULL; 388 if (sig_num < 1 || sig_num >= NSIG) { 389 PyErr_SetString(PyExc_ValueError, 390 "signal number out of range"); 391 return NULL; 392 } 393 if (siginterrupt(sig_num, flag)<0) { 394 PyErr_SetFromErrno(PyExc_RuntimeError); 395 return NULL; 396 } 397 398 Py_INCREF(Py_None); 399 return Py_None; 348 400 } 349 401 … … 353 405 signal_set_wakeup_fd(PyObject *self, PyObject *args) 354 406 { 355 356 357 358 407 struct stat buf; 408 int fd, old_fd; 409 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd)) 410 return NULL; 359 411 #ifdef WITH_THREAD 360 361 362 363 364 365 #endif 366 if (fd != -1 && fstat(fd, &buf) != 0) {367 368 369 370 371 372 412 if (PyThread_get_thread_ident() != main_thread) { 413 PyErr_SetString(PyExc_ValueError, 414 "set_wakeup_fd only works in main thread"); 415 return NULL; 416 } 417 #endif 418 if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) { 419 PyErr_SetString(PyExc_ValueError, "invalid fd"); 420 return NULL; 421 } 422 old_fd = wakeup_fd; 423 wakeup_fd = fd; 424 return PyLong_FromLong(old_fd); 373 425 } 374 426 … … 386 438 PySignal_SetWakeupFd(int fd) 387 439 { 388 389 390 391 392 440 int old_fd = wakeup_fd; 441 if (fd < 0) 442 fd = -1; 443 wakeup_fd = fd; 444 return old_fd; 393 445 } 394 446 … … 404 456 405 457 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval)) 406 458 return NULL; 407 459 408 460 timeval_from_double(first, &new.it_value); … … 410 462 /* Let OS check "which" value */ 411 463 if (setitimer(which, &new, &old) != 0) { 412 413 464 PyErr_SetFromErrno(ItimerError); 465 return NULL; 414 466 } 415 467 … … 437 489 438 490 if (!PyArg_ParseTuple(args, "i:getitimer", &which)) 439 491 return NULL; 440 492 441 493 if (getitimer(which, &old) != 0) { 442 443 494 PyErr_SetFromErrno(ItimerError); 495 return NULL; 444 496 } 445 497 … … 457 509 static PyMethodDef signal_methods[] = { 458 510 #ifdef HAVE_ALARM 459 {"alarm",signal_alarm, METH_VARARGS, alarm_doc},511 {"alarm", signal_alarm, METH_VARARGS, alarm_doc}, 460 512 #endif 461 513 #ifdef HAVE_SETITIMER … … 463 515 #endif 464 516 #ifdef HAVE_GETITIMER 465 466 #endif 467 {"signal",signal_signal, METH_VARARGS, signal_doc},468 {"getsignal",signal_getsignal, METH_VARARGS, getsignal_doc},469 {"set_wakeup_fd",signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},517 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc}, 518 #endif 519 {"signal", signal_signal, METH_VARARGS, signal_doc}, 520 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc}, 521 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc}, 470 522 #ifdef HAVE_SIGINTERRUPT 471 {"siginterrupt",signal_siginterrupt, METH_VARARGS, siginterrupt_doc},523 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc}, 472 524 #endif 473 525 #ifdef HAVE_PAUSE 474 {"pause",(PyCFunction)signal_pause,475 476 #endif 477 478 479 {NULL, NULL}/* sentinel */526 {"pause", (PyCFunction)signal_pause, 527 METH_NOARGS,pause_doc}, 528 #endif 529 {"default_int_handler", signal_default_int_handler, 530 METH_VARARGS, default_int_handler_doc}, 531 {NULL, NULL} /* sentinel */ 480 532 }; 481 533 … … 520 572 initsignal(void) 521 573 { 522 523 574 PyObject *m, *d, *x; 575 int i; 524 576 525 577 #ifdef WITH_THREAD 526 527 528 #endif 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 578 main_thread = PyThread_get_thread_ident(); 579 main_pid = getpid(); 580 #endif 581 582 /* Create the module and add the functions */ 583 m = Py_InitModule3("signal", signal_methods, module_doc); 584 if (m == NULL) 585 return; 586 587 /* Add some symbolic constants to the module */ 588 d = PyModule_GetDict(m); 589 590 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); 591 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) 592 goto finally; 593 594 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); 595 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) 596 goto finally; 597 598 x = PyInt_FromLong((long)NSIG); 599 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0) 600 goto finally; 601 Py_DECREF(x); 602 603 x = IntHandler = PyDict_GetItemString(d, "default_int_handler"); 604 if (!x) 605 goto finally; 606 Py_INCREF(IntHandler); 607 608 Handlers[0].tripped = 0; 609 for (i = 1; i < NSIG; i++) { 610 void (*t)(int); 611 t = PyOS_getsig(i); 612 Handlers[i].tripped = 0; 613 if (t == SIG_DFL) 614 Handlers[i].func = DefaultHandler; 615 else if (t == SIG_IGN) 616 Handlers[i].func = IgnoreHandler; 617 else 618 Handlers[i].func = Py_None; /* None of our business */ 619 Py_INCREF(Handlers[i].func); 620 } 621 if (Handlers[SIGINT].func == DefaultHandler) { 622 /* Install default int handler */ 623 Py_INCREF(IntHandler); 624 Py_DECREF(Handlers[SIGINT].func); 625 Handlers[SIGINT].func = IntHandler; 626 old_siginthandler = PyOS_setsig(SIGINT, signal_handler); 627 } 576 628 577 629 #ifdef SIGHUP 578 579 580 630 x = PyInt_FromLong(SIGHUP); 631 PyDict_SetItemString(d, "SIGHUP", x); 632 Py_XDECREF(x); 581 633 #endif 582 634 #ifdef SIGINT 583 584 585 635 x = PyInt_FromLong(SIGINT); 636 PyDict_SetItemString(d, "SIGINT", x); 637 Py_XDECREF(x); 586 638 #endif 587 639 #ifdef SIGBREAK 588 589 590 640 x = PyInt_FromLong(SIGBREAK); 641 PyDict_SetItemString(d, "SIGBREAK", x); 642 Py_XDECREF(x); 591 643 #endif 592 644 #ifdef SIGQUIT 593 594 595 645 x = PyInt_FromLong(SIGQUIT); 646 PyDict_SetItemString(d, "SIGQUIT", x); 647 Py_XDECREF(x); 596 648 #endif 597 649 #ifdef SIGILL 598 599 600 650 x = PyInt_FromLong(SIGILL); 651 PyDict_SetItemString(d, "SIGILL", x); 652 Py_XDECREF(x); 601 653 #endif 602 654 #ifdef SIGTRAP 603 604 605 655 x = PyInt_FromLong(SIGTRAP); 656 PyDict_SetItemString(d, "SIGTRAP", x); 657 Py_XDECREF(x); 606 658 #endif 607 659 #ifdef SIGIOT 608 609 610 660 x = PyInt_FromLong(SIGIOT); 661 PyDict_SetItemString(d, "SIGIOT", x); 662 Py_XDECREF(x); 611 663 #endif 612 664 #ifdef SIGABRT 613 614 615 665 x = PyInt_FromLong(SIGABRT); 666 PyDict_SetItemString(d, "SIGABRT", x); 667 Py_XDECREF(x); 616 668 #endif 617 669 #ifdef SIGEMT 618 619 620 670 x = PyInt_FromLong(SIGEMT); 671 PyDict_SetItemString(d, "SIGEMT", x); 672 Py_XDECREF(x); 621 673 #endif 622 674 #ifdef SIGFPE 623 624 625 675 x = PyInt_FromLong(SIGFPE); 676 PyDict_SetItemString(d, "SIGFPE", x); 677 Py_XDECREF(x); 626 678 #endif 627 679 #ifdef SIGKILL 628 629 630 680 x = PyInt_FromLong(SIGKILL); 681 PyDict_SetItemString(d, "SIGKILL", x); 682 Py_XDECREF(x); 631 683 #endif 632 684 #ifdef SIGBUS 633 634 635 685 x = PyInt_FromLong(SIGBUS); 686 PyDict_SetItemString(d, "SIGBUS", x); 687 Py_XDECREF(x); 636 688 #endif 637 689 #ifdef SIGSEGV 638 639 640 690 x = PyInt_FromLong(SIGSEGV); 691 PyDict_SetItemString(d, "SIGSEGV", x); 692 Py_XDECREF(x); 641 693 #endif 642 694 #ifdef SIGSYS 643 644 645 695 x = PyInt_FromLong(SIGSYS); 696 PyDict_SetItemString(d, "SIGSYS", x); 697 Py_XDECREF(x); 646 698 #endif 647 699 #ifdef SIGPIPE 648 649 650 700 x = PyInt_FromLong(SIGPIPE); 701 PyDict_SetItemString(d, "SIGPIPE", x); 702 Py_XDECREF(x); 651 703 #endif 652 704 #ifdef SIGALRM 653 654 655 705 x = PyInt_FromLong(SIGALRM); 706 PyDict_SetItemString(d, "SIGALRM", x); 707 Py_XDECREF(x); 656 708 #endif 657 709 #ifdef SIGTERM 658 659 660 710 x = PyInt_FromLong(SIGTERM); 711 PyDict_SetItemString(d, "SIGTERM", x); 712 Py_XDECREF(x); 661 713 #endif 662 714 #ifdef SIGUSR1 663 664 665 715 x = PyInt_FromLong(SIGUSR1); 716 PyDict_SetItemString(d, "SIGUSR1", x); 717 Py_XDECREF(x); 666 718 #endif 667 719 #ifdef SIGUSR2 668 669 670 720 x = PyInt_FromLong(SIGUSR2); 721 PyDict_SetItemString(d, "SIGUSR2", x); 722 Py_XDECREF(x); 671 723 #endif 672 724 #ifdef SIGCLD 673 674 675 725 x = PyInt_FromLong(SIGCLD); 726 PyDict_SetItemString(d, "SIGCLD", x); 727 Py_XDECREF(x); 676 728 #endif 677 729 #ifdef SIGCHLD 678 679 680 730 x = PyInt_FromLong(SIGCHLD); 731 PyDict_SetItemString(d, "SIGCHLD", x); 732 Py_XDECREF(x); 681 733 #endif 682 734 #ifdef SIGPWR 683 684 685 735 x = PyInt_FromLong(SIGPWR); 736 PyDict_SetItemString(d, "SIGPWR", x); 737 Py_XDECREF(x); 686 738 #endif 687 739 #ifdef SIGIO 688 689 690 740 x = PyInt_FromLong(SIGIO); 741 PyDict_SetItemString(d, "SIGIO", x); 742 Py_XDECREF(x); 691 743 #endif 692 744 #ifdef SIGURG 693 694 695 745 x = PyInt_FromLong(SIGURG); 746 PyDict_SetItemString(d, "SIGURG", x); 747 Py_XDECREF(x); 696 748 #endif 697 749 #ifdef SIGWINCH 698 699 700 750 x = PyInt_FromLong(SIGWINCH); 751 PyDict_SetItemString(d, "SIGWINCH", x); 752 Py_XDECREF(x); 701 753 #endif 702 754 #ifdef SIGPOLL 703 704 705 755 x = PyInt_FromLong(SIGPOLL); 756 PyDict_SetItemString(d, "SIGPOLL", x); 757 Py_XDECREF(x); 706 758 #endif 707 759 #ifdef SIGSTOP 708 709 710 760 x = PyInt_FromLong(SIGSTOP); 761 PyDict_SetItemString(d, "SIGSTOP", x); 762 Py_XDECREF(x); 711 763 #endif 712 764 #ifdef SIGTSTP 713 714 715 765 x = PyInt_FromLong(SIGTSTP); 766 PyDict_SetItemString(d, "SIGTSTP", x); 767 Py_XDECREF(x); 716 768 #endif 717 769 #ifdef SIGCONT 718 719 720 770 x = PyInt_FromLong(SIGCONT); 771 PyDict_SetItemString(d, "SIGCONT", x); 772 Py_XDECREF(x); 721 773 #endif 722 774 #ifdef SIGTTIN 723 724 725 775 x = PyInt_FromLong(SIGTTIN); 776 PyDict_SetItemString(d, "SIGTTIN", x); 777 Py_XDECREF(x); 726 778 #endif 727 779 #ifdef SIGTTOU 728 729 730 780 x = PyInt_FromLong(SIGTTOU); 781 PyDict_SetItemString(d, "SIGTTOU", x); 782 Py_XDECREF(x); 731 783 #endif 732 784 #ifdef SIGVTALRM 733 734 735 785 x = PyInt_FromLong(SIGVTALRM); 786 PyDict_SetItemString(d, "SIGVTALRM", x); 787 Py_XDECREF(x); 736 788 #endif 737 789 #ifdef SIGPROF 738 739 740 790 x = PyInt_FromLong(SIGPROF); 791 PyDict_SetItemString(d, "SIGPROF", x); 792 Py_XDECREF(x); 741 793 #endif 742 794 #ifdef SIGXCPU 743 744 745 795 x = PyInt_FromLong(SIGXCPU); 796 PyDict_SetItemString(d, "SIGXCPU", x); 797 Py_XDECREF(x); 746 798 #endif 747 799 #ifdef SIGXFSZ 748 749 750 800 x = PyInt_FromLong(SIGXFSZ); 801 PyDict_SetItemString(d, "SIGXFSZ", x); 802 Py_XDECREF(x); 751 803 #endif 752 804 #ifdef SIGRTMIN 753 754 755 805 x = PyInt_FromLong(SIGRTMIN); 806 PyDict_SetItemString(d, "SIGRTMIN", x); 807 Py_XDECREF(x); 756 808 #endif 757 809 #ifdef SIGRTMAX 758 759 760 810 x = PyInt_FromLong(SIGRTMAX); 811 PyDict_SetItemString(d, "SIGRTMAX", x); 812 Py_XDECREF(x); 761 813 #endif 762 814 #ifdef SIGINFO 763 764 765 815 x = PyInt_FromLong(SIGINFO); 816 PyDict_SetItemString(d, "SIGINFO", x); 817 Py_XDECREF(x); 766 818 #endif 767 819 … … 783 835 784 836 #if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER) 785 ItimerError = PyErr_NewException("signal.ItimerError", 786 837 ItimerError = PyErr_NewException("signal.ItimerError", 838 PyExc_IOError, NULL); 787 839 if (ItimerError != NULL) 788 PyDict_SetItemString(d, "ItimerError", ItimerError); 789 #endif 790 791 if (!PyErr_Occurred()) 792 return; 793 794 /* Check for errors */ 840 PyDict_SetItemString(d, "ItimerError", ItimerError); 841 #endif 842 843 #ifdef CTRL_C_EVENT 844 x = PyInt_FromLong(CTRL_C_EVENT); 845 PyDict_SetItemString(d, "CTRL_C_EVENT", x); 846 Py_DECREF(x); 847 #endif 848 849 #ifdef CTRL_BREAK_EVENT 850 x = PyInt_FromLong(CTRL_BREAK_EVENT); 851 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x); 852 Py_DECREF(x); 853 #endif 854 855 if (!PyErr_Occurred()) 856 return; 857 858 /* Check for errors */ 795 859 finally: 796 860 return; 797 861 } 798 862 … … 800 864 finisignal(void) 801 865 { 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 866 int i; 867 PyObject *func; 868 869 PyOS_setsig(SIGINT, old_siginthandler); 870 old_siginthandler = SIG_DFL; 871 872 for (i = 1; i < NSIG; i++) { 873 func = Handlers[i].func; 874 Handlers[i].tripped = 0; 875 Handlers[i].func = NULL; 876 if (i != SIGINT && func != NULL && func != Py_None && 877 func != DefaultHandler && func != IgnoreHandler) 878 PyOS_setsig(i, SIG_DFL); 879 Py_XDECREF(func); 880 } 881 882 Py_XDECREF(IntHandler); 883 IntHandler = NULL; 884 Py_XDECREF(DefaultHandler); 885 DefaultHandler = NULL; 886 Py_XDECREF(IgnoreHandler); 887 IgnoreHandler = NULL; 824 888 } 825 889 … … 829 893 PyErr_CheckSignals(void) 830 894 { 831 832 833 834 835 895 int i; 896 PyObject *f; 897 898 if (!is_tripped) 899 return 0; 836 900 837 901 #ifdef WITH_THREAD 838 839 840 #endif 841 842 843 * The is_stripped variable is meant to speed up the calls to844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 902 if (PyThread_get_thread_ident() != main_thread) 903 return 0; 904 #endif 905 906 /* 907 * The is_tripped variable is meant to speed up the calls to 908 * PyErr_CheckSignals (both directly or via pending calls) when no 909 * signal has arrived. This variable is set to 1 when a signal arrives 910 * and it is set to 0 here, when we know some signals arrived. This way 911 * we can run the registered handlers with no signals blocked. 912 * 913 * NOTE: with this approach we can have a situation where is_tripped is 914 * 1 but we have no more signals to handle (Handlers[i].tripped 915 * is 0 for every signal i). This won't do us any harm (except 916 * we're gonna spent some cycles for nothing). This happens when 917 * we receive a signal i after we zero is_tripped and before we 918 * check Handlers[i].tripped. 919 */ 920 is_tripped = 0; 921 922 if (!(f = (PyObject *)PyEval_GetFrame())) 923 f = Py_None; 924 925 for (i = 1; i < NSIG; i++) { 926 if (Handlers[i].tripped) { 927 PyObject *result = NULL; 928 PyObject *arglist = Py_BuildValue("(iO)", i, f); 929 Handlers[i].tripped = 0; 930 931 if (arglist) { 932 result = PyEval_CallObject(Handlers[i].func, 933 arglist); 934 Py_DECREF(arglist); 935 } 936 if (!result) 937 return -1; 938 939 Py_DECREF(result); 940 } 941 } 942 943 return 0; 880 944 } 881 945 … … 887 951 PyErr_SetInterrupt(void) 888 952 { 889 is_tripped = 1; 890 Handlers[SIGINT].tripped = 1; 891 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL); 953 trip_signal(SIGINT); 892 954 } 893 955 … … 895 957 PyOS_InitInterrupts(void) 896 958 { 897 898 959 initsignal(); 960 _PyImport_FixupExtension("signal", "signal"); 899 961 } 900 962 … … 902 964 PyOS_FiniInterrupts(void) 903 965 { 904 966 finisignal(); 905 967 } 906 968 … … 908 970 PyOS_InterruptOccurred(void) 909 971 { 910 972 if (Handlers[SIGINT].tripped) { 911 973 #ifdef WITH_THREAD 912 if (PyThread_get_thread_ident() != main_thread) 913 return 0; 914 #endif 915 Handlers[SIGINT].tripped = 0; 916 return 1; 917 } 918 return 0; 974 if (PyThread_get_thread_ident() != main_thread) 975 return 0; 976 #endif 977 Handlers[SIGINT].tripped = 0; 978 return 1; 979 } 980 return 0; 981 } 982 983 static void 984 _clear_pending_signals(void) 985 { 986 int i; 987 if (!is_tripped) 988 return; 989 is_tripped = 0; 990 for (i = 1; i < NSIG; ++i) { 991 Handlers[i].tripped = 0; 992 } 919 993 } 920 994 … … 922 996 PyOS_AfterFork(void) 923 997 { 998 /* Clear the signal flags after forking so that they aren't handled 999 * in both processes if they came in just before the fork() but before 1000 * the interpreter had an opportunity to call the handlers. issue9535. */ 1001 _clear_pending_signals(); 924 1002 #ifdef WITH_THREAD 925 PyEval_ReInitThreads(); 926 main_thread = PyThread_get_thread_ident(); 927 main_pid = getpid(); 928 _PyImport_ReInitLock(); 929 PyThread_ReInitTLS(); 930 #endif 931 } 1003 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API 1004 * can be called safely. */ 1005 PyThread_ReInitTLS(); 1006 PyEval_ReInitThreads(); 1007 main_thread = PyThread_get_thread_ident(); 1008 main_pid = getpid(); 1009 _PyImport_ReInitLock(); 1010 #endif 1011 }
Note:
See TracChangeset
for help on using the changeset viewer.