diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-05-09 16:14:21 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-05-09 16:14:21 (GMT) |
commit | 7f14f0d8a0228c50d5b5de2acbfe9a64ebc6749a (patch) | |
tree | d25489e9531c01f1e9244012bbfaa929f382883e /Demo | |
parent | b7d943625cf4353f6cb72df16252759f2dbd8e06 (diff) | |
download | cpython-7f14f0d8a0228c50d5b5de2acbfe9a64ebc6749a.zip cpython-7f14f0d8a0228c50d5b5de2acbfe9a64ebc6749a.tar.gz cpython-7f14f0d8a0228c50d5b5de2acbfe9a64ebc6749a.tar.bz2 |
Recorded merge of revisions 81032 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r81032 | antoine.pitrou | 2010-05-09 17:52:27 +0200 (dim., 09 mai 2010) | 9 lines
Recorded merge of revisions 81029 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines
Untabify C files. Will watch buildbots.
........
................
Diffstat (limited to 'Demo')
-rw-r--r-- | Demo/embed/demo.c | 86 | ||||
-rw-r--r-- | Demo/embed/loop.c | 38 | ||||
-rw-r--r-- | Demo/pysvr/pysvr.c | 498 |
3 files changed, 311 insertions, 311 deletions
diff --git a/Demo/embed/demo.c b/Demo/embed/demo.c index b9ea6f5..22bfaff 100644 --- a/Demo/embed/demo.c +++ b/Demo/embed/demo.c @@ -6,44 +6,44 @@ PyObject* PyInit_xyzzy(void); /* Forward */ main(int argc, char **argv) { - /* Ignore passed-in argc/argv. If desired, conversion - should use mbstowcs to convert them. */ - wchar_t *args[] = {L"embed", L"hello", 0}; + /* Ignore passed-in argc/argv. If desired, conversion + should use mbstowcs to convert them. */ + wchar_t *args[] = {L"embed", L"hello", 0}; - /* Pass argv[0] to the Python interpreter */ - Py_SetProgramName(args[0]); + /* Pass argv[0] to the Python interpreter */ + Py_SetProgramName(args[0]); - /* Add a static module */ - PyImport_AppendInittab("xyzzy", PyInit_xyzzy); + /* Add a static module */ + PyImport_AppendInittab("xyzzy", PyInit_xyzzy); - /* Initialize the Python interpreter. Required. */ - Py_Initialize(); + /* Initialize the Python interpreter. Required. */ + Py_Initialize(); - /* Define sys.argv. It is up to the application if you - want this; you can also let it undefined (since the Python - code is generally not a main program it has no business - touching sys.argv...) */ - PySys_SetArgv(2, args); + /* Define sys.argv. It is up to the application if you + want this; you can also let it undefined (since the Python + code is generally not a main program it has no business + touching sys.argv...) */ + PySys_SetArgv(2, args); - /* Do some application specific code */ - printf("Hello, brave new world\n\n"); + /* Do some application specific code */ + printf("Hello, brave new world\n\n"); - /* Execute some Python statements (in module __main__) */ - PyRun_SimpleString("import sys\n"); - PyRun_SimpleString("print(sys.builtin_module_names)\n"); - PyRun_SimpleString("print(sys.modules.keys())\n"); - PyRun_SimpleString("print(sys.executable)\n"); - PyRun_SimpleString("print(sys.argv)\n"); + /* Execute some Python statements (in module __main__) */ + PyRun_SimpleString("import sys\n"); + PyRun_SimpleString("print(sys.builtin_module_names)\n"); + PyRun_SimpleString("print(sys.modules.keys())\n"); + PyRun_SimpleString("print(sys.executable)\n"); + PyRun_SimpleString("print(sys.argv)\n"); - /* Note that you can call any public function of the Python - interpreter here, e.g. call_object(). */ + /* Note that you can call any public function of the Python + interpreter here, e.g. call_object(). */ - /* Some more application specific code */ - printf("\nGoodbye, cruel world\n"); + /* Some more application specific code */ + printf("\nGoodbye, cruel world\n"); - /* Exit, cleaning up the interpreter */ - Py_Exit(0); - /*NOTREACHED*/ + /* Exit, cleaning up the interpreter */ + Py_Exit(0); + /*NOTREACHED*/ } /* A static module */ @@ -52,29 +52,29 @@ main(int argc, char **argv) static PyObject * xyzzy_foo(PyObject *self, PyObject* args) { - return PyLong_FromLong(42L); + return PyLong_FromLong(42L); } static PyMethodDef xyzzy_methods[] = { - {"foo", xyzzy_foo, METH_NOARGS, - "Return the meaning of everything."}, - {NULL, NULL} /* sentinel */ + {"foo", xyzzy_foo, METH_NOARGS, + "Return the meaning of everything."}, + {NULL, NULL} /* sentinel */ }; static struct PyModuleDef xyzzymodule = { - {}, /* m_base */ - "xyzzy", /* m_name */ - 0, /* m_doc */ - 0, /* m_size */ - xyzzy_methods, /* m_methods */ - 0, /* m_reload */ - 0, /* m_traverse */ - 0, /* m_clear */ - 0, /* m_free */ + {}, /* m_base */ + "xyzzy", /* m_name */ + 0, /* m_doc */ + 0, /* m_size */ + xyzzy_methods, /* m_methods */ + 0, /* m_reload */ + 0, /* m_traverse */ + 0, /* m_clear */ + 0, /* m_free */ }; PyObject* PyInit_xyzzy(void) { - return PyModule_Create(&xyzzymodule); + return PyModule_Create(&xyzzymodule); } diff --git a/Demo/embed/loop.c b/Demo/embed/loop.c index d5af829..2f7fe62 100644 --- a/Demo/embed/loop.c +++ b/Demo/embed/loop.c @@ -6,28 +6,28 @@ main(int argc, char **argv) { - int count = -1; - char *command; + int count = -1; + char *command; - if (argc < 2 || argc > 3) { - fprintf(stderr, "usage: loop <python-command> [count]\n"); - exit(2); - } - command = argv[1]; + if (argc < 2 || argc > 3) { + fprintf(stderr, "usage: loop <python-command> [count]\n"); + exit(2); + } + command = argv[1]; - if (argc == 3) { - count = atoi(argv[2]); - } + if (argc == 3) { + count = atoi(argv[2]); + } - Py_SetProgramName(argv[0]); + Py_SetProgramName(argv[0]); - /* uncomment this if you don't want to load site.py */ - /* Py_NoSiteFlag = 1; */ + /* uncomment this if you don't want to load site.py */ + /* Py_NoSiteFlag = 1; */ - while (count == -1 || --count >= 0 ) { - Py_Initialize(); - PyRun_SimpleString(command); - Py_Finalize(); - } - return 0; + while (count == -1 || --count >= 0 ) { + Py_Initialize(); + PyRun_SimpleString(command); + Py_Finalize(); + } + return 0; } diff --git a/Demo/pysvr/pysvr.c b/Demo/pysvr/pysvr.c index cced6da..706cd2a 100644 --- a/Demo/pysvr/pysvr.c +++ b/Demo/pysvr/pysvr.c @@ -34,8 +34,8 @@ extern int Py_VerboseFlag; #endif struct workorder { - int conn; - struct sockaddr_in addr; + int conn; + struct sockaddr_in addr; }; /* Forward */ @@ -55,40 +55,40 @@ static PyThreadState *gtstate; main(int argc, char **argv) { - int port = PORT; - int c; - - if (argc > 0 && argv[0] != NULL && argv[0][0] != '\0') - progname = argv[0]; - - while ((c = getopt(argc, argv, "v")) != EOF) { - switch (c) { - case 'v': - Py_VerboseFlag++; - break; - default: - usage(); - } - } - - if (optind < argc) { - if (optind+1 < argc) { - oprogname(); - fprintf(stderr, "too many arguments\n"); - usage(); - } - port = atoi(argv[optind]); - if (port <= 0) { - fprintf(stderr, "bad port (%s)\n", argv[optind]); - usage(); - } - } - - main_thread(port); - - fprintf(stderr, "Bye.\n"); - - exit(0); + int port = PORT; + int c; + + if (argc > 0 && argv[0] != NULL && argv[0][0] != '\0') + progname = argv[0]; + + while ((c = getopt(argc, argv, "v")) != EOF) { + switch (c) { + case 'v': + Py_VerboseFlag++; + break; + default: + usage(); + } + } + + if (optind < argc) { + if (optind+1 < argc) { + oprogname(); + fprintf(stderr, "too many arguments\n"); + usage(); + } + port = atoi(argv[optind]); + if (port <= 0) { + fprintf(stderr, "bad port (%s)\n", argv[optind]); + usage(); + } + } + + main_thread(port); + + fprintf(stderr, "Bye.\n"); + + exit(0); } static char usage_line[] = "usage: %s [port]\n"; @@ -96,120 +96,120 @@ static char usage_line[] = "usage: %s [port]\n"; static void usage(void) { - fprintf(stderr, usage_line, progname); - exit(2); + fprintf(stderr, usage_line, progname); + exit(2); } static void main_thread(int port) { - int sock, conn, size, i; - struct sockaddr_in addr, clientaddr; + int sock, conn, size, i; + struct sockaddr_in addr, clientaddr; - sock = socket(PF_INET, SOCK_STREAM, 0); - if (sock < 0) { - oprogname(); - perror("can't create socket"); - exit(1); - } + sock = socket(PF_INET, SOCK_STREAM, 0); + if (sock < 0) { + oprogname(); + perror("can't create socket"); + exit(1); + } #ifdef SO_REUSEADDR - i = 1; - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof i); + i = 1; + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof i); #endif - memset((char *)&addr, '\0', sizeof addr); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - addr.sin_addr.s_addr = 0L; - if (bind(sock, (struct sockaddr *)&addr, sizeof addr) < 0) { - oprogname(); - perror("can't bind socket to address"); - exit(1); - } - - if (listen(sock, 5) < 0) { - oprogname(); - perror("can't listen on socket"); - exit(1); - } - - fprintf(stderr, "Listening on port %d...\n", port); - - for (i = 0; ; i++) { - size = sizeof clientaddr; - memset((char *) &clientaddr, '\0', size); - conn = accept(sock, (struct sockaddr *) &clientaddr, &size); - if (conn < 0) { - oprogname(); - perror("can't accept connection from socket"); - exit(1); - } - - size = sizeof addr; - memset((char *) &addr, '\0', size); - if (getsockname(conn, (struct sockaddr *)&addr, &size) < 0) { - oprogname(); - perror("can't get socket name of connection"); - exit(1); - } - if (clientaddr.sin_addr.s_addr != addr.sin_addr.s_addr) { - oprogname(); - perror("connection from non-local host refused"); - fprintf(stderr, "(addr=%lx, clientaddr=%lx)\n", - ntohl(addr.sin_addr.s_addr), - ntohl(clientaddr.sin_addr.s_addr)); - close(conn); - continue; - } - if (i == 4) { - close(conn); - break; - } - create_thread(conn, &clientaddr); - } - - close(sock); - - if (gtstate) { - PyEval_AcquireThread(gtstate); - gtstate = NULL; - Py_Finalize(); - /* And a second time, just because we can. */ - Py_Finalize(); /* This should be harmless. */ - } - exit(0); + memset((char *)&addr, '\0', sizeof addr); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = 0L; + if (bind(sock, (struct sockaddr *)&addr, sizeof addr) < 0) { + oprogname(); + perror("can't bind socket to address"); + exit(1); + } + + if (listen(sock, 5) < 0) { + oprogname(); + perror("can't listen on socket"); + exit(1); + } + + fprintf(stderr, "Listening on port %d...\n", port); + + for (i = 0; ; i++) { + size = sizeof clientaddr; + memset((char *) &clientaddr, '\0', size); + conn = accept(sock, (struct sockaddr *) &clientaddr, &size); + if (conn < 0) { + oprogname(); + perror("can't accept connection from socket"); + exit(1); + } + + size = sizeof addr; + memset((char *) &addr, '\0', size); + if (getsockname(conn, (struct sockaddr *)&addr, &size) < 0) { + oprogname(); + perror("can't get socket name of connection"); + exit(1); + } + if (clientaddr.sin_addr.s_addr != addr.sin_addr.s_addr) { + oprogname(); + perror("connection from non-local host refused"); + fprintf(stderr, "(addr=%lx, clientaddr=%lx)\n", + ntohl(addr.sin_addr.s_addr), + ntohl(clientaddr.sin_addr.s_addr)); + close(conn); + continue; + } + if (i == 4) { + close(conn); + break; + } + create_thread(conn, &clientaddr); + } + + close(sock); + + if (gtstate) { + PyEval_AcquireThread(gtstate); + gtstate = NULL; + Py_Finalize(); + /* And a second time, just because we can. */ + Py_Finalize(); /* This should be harmless. */ + } + exit(0); } static void create_thread(int conn, struct sockaddr_in *addr) { - struct workorder *work; - pthread_t tdata; - - work = malloc(sizeof(struct workorder)); - if (work == NULL) { - oprogname(); - fprintf(stderr, "out of memory for thread.\n"); - close(conn); - return; - } - work->conn = conn; - work->addr = *addr; - - init_python(); - - if (pthread_create(&tdata, NULL, (void *)service_thread, work) < 0) { - oprogname(); - perror("can't create new thread"); - close(conn); - return; - } - - if (pthread_detach(tdata) < 0) { - oprogname(); - perror("can't detach from thread"); - } + struct workorder *work; + pthread_t tdata; + + work = malloc(sizeof(struct workorder)); + if (work == NULL) { + oprogname(); + fprintf(stderr, "out of memory for thread.\n"); + close(conn); + return; + } + work->conn = conn; + work->addr = *addr; + + init_python(); + + if (pthread_create(&tdata, NULL, (void *)service_thread, work) < 0) { + oprogname(); + perror("can't create new thread"); + close(conn); + return; + } + + if (pthread_detach(tdata) < 0) { + oprogname(); + perror("can't detach from thread"); + } } static PyThreadState *the_tstate; @@ -219,152 +219,152 @@ static PyObject *the_builtins; static void init_python(void) { - if (gtstate) - return; - Py_Initialize(); /* Initialize the interpreter */ - PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */ - gtstate = PyEval_SaveThread(); /* Release the thread state */ + if (gtstate) + return; + Py_Initialize(); /* Initialize the interpreter */ + PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */ + gtstate = PyEval_SaveThread(); /* Release the thread state */ } static void * service_thread(struct workorder *work) { - FILE *input, *output; + FILE *input, *output; - fprintf(stderr, "Start thread for connection %d.\n", work->conn); + fprintf(stderr, "Start thread for connection %d.\n", work->conn); - ps(); + ps(); - input = fdopen(work->conn, "r"); - if (input == NULL) { - oprogname(); - perror("can't create input stream"); - goto done; - } + input = fdopen(work->conn, "r"); + if (input == NULL) { + oprogname(); + perror("can't create input stream"); + goto done; + } - output = fdopen(work->conn, "w"); - if (output == NULL) { - oprogname(); - perror("can't create output stream"); - fclose(input); - goto done; - } + output = fdopen(work->conn, "w"); + if (output == NULL) { + oprogname(); + perror("can't create output stream"); + fclose(input); + goto done; + } - setvbuf(input, NULL, _IONBF, 0); - setvbuf(output, NULL, _IONBF, 0); + setvbuf(input, NULL, _IONBF, 0); + setvbuf(output, NULL, _IONBF, 0); - run_interpreter(input, output); + run_interpreter(input, output); - fclose(input); - fclose(output); + fclose(input); + fclose(output); done: - fprintf(stderr, "End thread for connection %d.\n", work->conn); - close(work->conn); - free(work); + fprintf(stderr, "End thread for connection %d.\n", work->conn); + close(work->conn); + free(work); } static void oprogname(void) { - int save = errno; - fprintf(stderr, "%s: ", progname); - errno = save; + int save = errno; + fprintf(stderr, "%s: ", progname); + errno = save; } static void run_interpreter(FILE *input, FILE *output) { - PyThreadState *tstate; - PyObject *new_stdin, *new_stdout; - PyObject *mainmod, *globals; - char buffer[1000]; - char *p, *q; - int n, end; - - PyEval_AcquireLock(); - tstate = Py_NewInterpreter(); - if (tstate == NULL) { - fprintf(output, "Sorry -- can't create an interpreter\n"); - return; - } - - mainmod = PyImport_AddModule("__main__"); - globals = PyModule_GetDict(mainmod); - Py_INCREF(globals); - - new_stdin = PyFile_FromFile(input, "<socket-in>", "r", NULL); - new_stdout = PyFile_FromFile(output, "<socket-out>", "w", NULL); - - PySys_SetObject("stdin", new_stdin); - PySys_SetObject("stdout", new_stdout); - PySys_SetObject("stderr", new_stdout); - - for (n = 1; !PyErr_Occurred(); n++) { - Py_BEGIN_ALLOW_THREADS - fprintf(output, "%d> ", n); - p = fgets(buffer, sizeof buffer, input); - Py_END_ALLOW_THREADS - - if (p == NULL) - break; - if (p[0] == '\377' && p[1] == '\354') - break; - - q = strrchr(p, '\r'); - if (q && q[1] == '\n' && q[2] == '\0') { - *q++ = '\n'; - *q++ = '\0'; - } - - while (*p && isspace(*p)) - p++; - if (p[0] == '#' || p[0] == '\0') - continue; - - end = run_command(buffer, globals); - if (end < 0) - PyErr_Print(); - - if (end) - break; - } - - Py_XDECREF(globals); - Py_XDECREF(new_stdin); - Py_XDECREF(new_stdout); - - Py_EndInterpreter(tstate); - PyEval_ReleaseLock(); - - fprintf(output, "Goodbye!\n"); + PyThreadState *tstate; + PyObject *new_stdin, *new_stdout; + PyObject *mainmod, *globals; + char buffer[1000]; + char *p, *q; + int n, end; + + PyEval_AcquireLock(); + tstate = Py_NewInterpreter(); + if (tstate == NULL) { + fprintf(output, "Sorry -- can't create an interpreter\n"); + return; + } + + mainmod = PyImport_AddModule("__main__"); + globals = PyModule_GetDict(mainmod); + Py_INCREF(globals); + + new_stdin = PyFile_FromFile(input, "<socket-in>", "r", NULL); + new_stdout = PyFile_FromFile(output, "<socket-out>", "w", NULL); + + PySys_SetObject("stdin", new_stdin); + PySys_SetObject("stdout", new_stdout); + PySys_SetObject("stderr", new_stdout); + + for (n = 1; !PyErr_Occurred(); n++) { + Py_BEGIN_ALLOW_THREADS + fprintf(output, "%d> ", n); + p = fgets(buffer, sizeof buffer, input); + Py_END_ALLOW_THREADS + + if (p == NULL) + break; + if (p[0] == '\377' && p[1] == '\354') + break; + + q = strrchr(p, '\r'); + if (q && q[1] == '\n' && q[2] == '\0') { + *q++ = '\n'; + *q++ = '\0'; + } + + while (*p && isspace(*p)) + p++; + if (p[0] == '#' || p[0] == '\0') + continue; + + end = run_command(buffer, globals); + if (end < 0) + PyErr_Print(); + + if (end) + break; + } + + Py_XDECREF(globals); + Py_XDECREF(new_stdin); + Py_XDECREF(new_stdout); + + Py_EndInterpreter(tstate); + PyEval_ReleaseLock(); + + fprintf(output, "Goodbye!\n"); } static int run_command(char *buffer, PyObject *globals) { - PyObject *m, *d, *v; - fprintf(stderr, "run_command: %s", buffer); - if (strchr(buffer, '\n') == NULL) - fprintf(stderr, "\n"); - v = PyRun_String(buffer, Py_single_input, globals, globals); - if (v == NULL) { - if (PyErr_Occurred() == PyExc_SystemExit) { - PyErr_Clear(); - return 1; - } - PyErr_Print(); - return 0; - } - Py_DECREF(v); - return 0; + PyObject *m, *d, *v; + fprintf(stderr, "run_command: %s", buffer); + if (strchr(buffer, '\n') == NULL) + fprintf(stderr, "\n"); + v = PyRun_String(buffer, Py_single_input, globals, globals); + if (v == NULL) { + if (PyErr_Occurred() == PyExc_SystemExit) { + PyErr_Clear(); + return 1; + } + PyErr_Print(); + return 0; + } + Py_DECREF(v); + return 0; } static void ps(void) { - char buffer[100]; - PyOS_snprintf(buffer, sizeof(buffer), - "ps -l -p %d </dev/null | sed 1d\n", getpid()); - system(buffer); + char buffer[100]; + PyOS_snprintf(buffer, sizeof(buffer), + "ps -l -p %d </dev/null | sed 1d\n", getpid()); + system(buffer); } |