Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

Side by Side Diff: Modules/signalmodule.c

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Modules/sha1module.c ('k') | Modules/_ssl.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Signal module -- many thanks to Lance Ellinghaus */ 2 /* Signal module -- many thanks to Lance Ellinghaus */
3 3
4 /* XXX Signals should be recorded per thread, now we have thread state. */ 4 /* XXX Signals should be recorded per thread, now we have thread state. */
5 5
6 #include "Python.h" 6 #include "Python.h"
7 7
8 #ifdef MS_WINDOWS 8 #ifdef MS_WINDOWS
9 #include <Windows.h> 9 #include <Windows.h>
10 #ifdef HAVE_PROCESS_H 10 #ifdef HAVE_PROCESS_H
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 500
501 return itimer_retval(&old); 501 return itimer_retval(&old);
502 } 502 }
503 503
504 PyDoc_STRVAR(getitimer_doc, 504 PyDoc_STRVAR(getitimer_doc,
505 "getitimer(which)\n\ 505 "getitimer(which)\n\
506 \n\ 506 \n\
507 Returns current value of given itimer."); 507 Returns current value of given itimer.");
508 #endif 508 #endif
509 509
510 #if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \ 510 #if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT)
511 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
512 /* Convert an iterable to a sigset. 511 /* Convert an iterable to a sigset.
513 Return 0 on success, return -1 and raise an exception on error. */ 512 Return 0 on success, return -1 and raise an exception on error. */
514 513
515 static int 514 static int
516 iterable_to_sigset(PyObject *iterable, sigset_t *mask) 515 iterable_to_sigset(PyObject *iterable, sigset_t *mask)
517 { 516 {
518 int result = -1; 517 int result = -1;
519 PyObject *iterator, *item; 518 PyObject *iterator, *item;
520 long signum; 519 long signum;
521 int err; 520 int err;
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 } 671 }
673 672
674 return PyLong_FromLong(signum); 673 return PyLong_FromLong(signum);
675 } 674 }
676 675
677 PyDoc_STRVAR(signal_sigwait_doc, 676 PyDoc_STRVAR(signal_sigwait_doc,
678 "sigwait(sigset) -> signum\n\ 677 "sigwait(sigset) -> signum\n\
679 \n\ 678 \n\
680 Wait a signal."); 679 Wait a signal.");
681 #endif /* #ifdef HAVE_SIGPENDING */ 680 #endif /* #ifdef HAVE_SIGPENDING */
682
683 #if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
684 static int initialized;
685 static PyStructSequence_Field struct_siginfo_fields[] = {
686 {"si_signo", "signal number"},
687 {"si_code", "signal code"},
688 {"si_errno", "errno associated with this signal"},
689 {"si_pid", "sending process ID"},
690 {"si_uid", "real user ID of sending process"},
691 {"si_status", "exit value or signal"},
692 {"si_band", "band event for SIGPOLL"},
693 {0}
694 };
695
696 PyDoc_STRVAR(struct_siginfo__doc__,
697 "struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
698 This object may be accessed either as a tuple of\n\
699 (si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
700 or via the attributes si_signo, si_code, and so on.");
701
702 static PyStructSequence_Desc struct_siginfo_desc = {
703 "signal.struct_siginfo", /* name */
704 struct_siginfo__doc__, /* doc */
705 struct_siginfo_fields, /* fields */
706 7 /* n_in_sequence */
707 };
708
709 static PyTypeObject SiginfoType;
710
711 static PyObject *
712 fill_siginfo(siginfo_t *si)
713 {
714 PyObject *result = PyStructSequence_New(&SiginfoType);
715 if (!result)
716 return NULL;
717
718 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
719 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
720 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
721 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
722 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si->si_uid)));
723 PyStructSequence_SET_ITEM(result, 5,
724 PyLong_FromLong((long)(si->si_status)));
725 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
726 if (PyErr_Occurred()) {
727 Py_DECREF(result);
728 return NULL;
729 }
730
731 return result;
732 }
733 #endif
734
735 #ifdef HAVE_SIGWAITINFO
736 static PyObject *
737 signal_sigwaitinfo(PyObject *self, PyObject *args)
738 {
739 PyObject *signals;
740 sigset_t set;
741 siginfo_t si;
742 int err;
743
744 if (!PyArg_ParseTuple(args, "O:sigwaitinfo", &signals))
745 return NULL;
746
747 if (iterable_to_sigset(signals, &set))
748 return NULL;
749
750 Py_BEGIN_ALLOW_THREADS
751 err = sigwaitinfo(&set, &si);
752 Py_END_ALLOW_THREADS
753 if (err == -1)
754 return PyErr_SetFromErrno(PyExc_OSError);
755
756 return fill_siginfo(&si);
757 }
758
759 PyDoc_STRVAR(signal_sigwaitinfo_doc,
760 "sigwaitinfo(sigset) -> struct_siginfo\n\
761 \n\
762 Wait synchronously for a signal until one of the signals in *sigset* is\n\
763 delivered.\n\
764 Returns a struct_siginfo containing information about the signal.");
765 #endif /* #ifdef HAVE_SIGWAITINFO */
766
767 #ifdef HAVE_SIGTIMEDWAIT
768 static PyObject *
769 signal_sigtimedwait(PyObject *self, PyObject *args)
770 {
771 PyObject *signals, *timeout;
772 struct timespec buf;
773 sigset_t set;
774 siginfo_t si;
775 int err;
776
777 if (!PyArg_ParseTuple(args, "OO:sigtimedwait", &signals, &timeout))
778 return NULL;
779
780 if (!PyTuple_Check(timeout) || PyTuple_Size(timeout) != 2) {
781 PyErr_SetString(PyExc_TypeError,
782 "sigtimedwait() arg 2 must be a tuple "
783 "(timeout_sec, timeout_nsec)");
784 return NULL;
785 } else if (!PyArg_ParseTuple(timeout, "ll:sigtimedwait",
786 &(buf.tv_sec), &(buf.tv_nsec)))
787 return NULL;
788
789 if (buf.tv_sec < 0 || buf.tv_nsec < 0) {
790 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
791 return NULL;
792 }
793
794 if (iterable_to_sigset(signals, &set))
795 return NULL;
796
797 Py_BEGIN_ALLOW_THREADS
798 err = sigtimedwait(&set, &si, &buf);
799 Py_END_ALLOW_THREADS
800 if (err == -1) {
801 if (errno == EAGAIN)
802 Py_RETURN_NONE;
803 else
804 return PyErr_SetFromErrno(PyExc_OSError);
805 }
806
807 return fill_siginfo(&si);
808 }
809
810 PyDoc_STRVAR(signal_sigtimedwait_doc,
811 "sigtimedwait(sigset, (timeout_sec, timeout_nsec)) -> struct_siginfo\n\
812 \n\
813 Like sigwaitinfo(), but with a timeout specified as a tuple of (seconds,\n\
814 nanoseconds).");
815 #endif /* #ifdef HAVE_SIGTIMEDWAIT */
816 681
817 682
818 #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) 683 #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
819 static PyObject * 684 static PyObject *
820 signal_pthread_kill(PyObject *self, PyObject *args) 685 signal_pthread_kill(PyObject *self, PyObject *args)
821 { 686 {
822 long tid; 687 long tid;
823 int signum; 688 int signum;
824 int err; 689 int err;
825 690
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
879 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask, 744 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
880 METH_VARARGS, signal_pthread_sigmask_doc}, 745 METH_VARARGS, signal_pthread_sigmask_doc},
881 #endif 746 #endif
882 #ifdef HAVE_SIGPENDING 747 #ifdef HAVE_SIGPENDING
883 {"sigpending", (PyCFunction)signal_sigpending, 748 {"sigpending", (PyCFunction)signal_sigpending,
884 METH_NOARGS, signal_sigpending_doc}, 749 METH_NOARGS, signal_sigpending_doc},
885 #endif 750 #endif
886 #ifdef HAVE_SIGWAIT 751 #ifdef HAVE_SIGWAIT
887 {"sigwait", (PyCFunction)signal_sigwait, 752 {"sigwait", (PyCFunction)signal_sigwait,
888 METH_VARARGS, signal_sigwait_doc}, 753 METH_VARARGS, signal_sigwait_doc},
889 #endif
890 #ifdef HAVE_SIGWAITINFO
891 {"sigwaitinfo", (PyCFunction)signal_sigwaitinfo,
892 METH_VARARGS, signal_sigwaitinfo_doc},
893 #endif
894 #ifdef HAVE_SIGTIMEDWAIT
895 {"sigtimedwait", (PyCFunction)signal_sigtimedwait,
896 METH_VARARGS, signal_sigtimedwait_doc},
897 #endif 754 #endif
898 {NULL, NULL} /* sentinel */ 755 {NULL, NULL} /* sentinel */
899 }; 756 };
900 757
901 758
902 PyDoc_STRVAR(module_doc, 759 PyDoc_STRVAR(module_doc,
903 "This module provides mechanisms to use signal handlers in Python.\n\ 760 "This module provides mechanisms to use signal handlers in Python.\n\
904 \n\ 761 \n\
905 Functions:\n\ 762 Functions:\n\
906 \n\ 763 \n\
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
955 812
956 #ifdef WITH_THREAD 813 #ifdef WITH_THREAD
957 main_thread = PyThread_get_thread_ident(); 814 main_thread = PyThread_get_thread_ident();
958 main_pid = getpid(); 815 main_pid = getpid();
959 #endif 816 #endif
960 817
961 /* Create the module and add the functions */ 818 /* Create the module and add the functions */
962 m = PyModule_Create(&signalmodule); 819 m = PyModule_Create(&signalmodule);
963 if (m == NULL) 820 if (m == NULL)
964 return NULL; 821 return NULL;
965
966 #if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
967 if (!initialized)
968 PyStructSequence_InitType(&SiginfoType, &struct_siginfo_desc);
969
970 Py_INCREF((PyObject*) &SiginfoType);
971 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
972 initialized = 1;
973 #endif
974 822
975 /* Add some symbolic constants to the module */ 823 /* Add some symbolic constants to the module */
976 d = PyModule_GetDict(m); 824 d = PyModule_GetDict(m);
977 825
978 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); 826 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
979 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) 827 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
980 goto finally; 828 goto finally;
981 829
982 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); 830 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
983 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) 831 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
1390 { 1238 {
1391 #ifdef WITH_THREAD 1239 #ifdef WITH_THREAD
1392 _PyGILState_Reinit(); 1240 _PyGILState_Reinit();
1393 PyEval_ReInitThreads(); 1241 PyEval_ReInitThreads();
1394 main_thread = PyThread_get_thread_ident(); 1242 main_thread = PyThread_get_thread_ident();
1395 main_pid = getpid(); 1243 main_pid = getpid();
1396 _PyImport_ReInitLock(); 1244 _PyImport_ReInitLock();
1397 PyThread_ReInitTLS(); 1245 PyThread_ReInitTLS();
1398 #endif 1246 #endif
1399 } 1247 }
OLDNEW
« no previous file with comments | « Modules/sha1module.c ('k') | Modules/_ssl.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+