Library of Assembled Shared Sources
streams.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "python_common.h"
44#include "streams.h"
45#include "gil.h"
46
47namespace lass
48{
49namespace python
50{
51namespace impl
52{
53
54// STL streams are all very thread unsafe, and we can't really fix that.
55
56
57SysStreamBuf::SysStreamBuf(FILE* file, const char* name):
58 file_(file),
59 name_(name)
60{
61 // make sure you have room for one more + \0
62 setp(buffer_, buffer_ + (bufferSize_ - 2));
63}
64
65
66
67SysStreamBuf::~SysStreamBuf()
68{
69 sync();
70}
71
72
73SysStreamBuf::int_type SysStreamBuf::overflow(int_type c)
74{
75 if (c != EOF)
76 {
77 // we have room for one more!
78 *pptr() = static_cast<char>(c);
79 pbump(1);
80 }
81 if (sync() != 0)
82 {
83 return EOF;
84 }
85 return c;
86}
87
88
89int SysStreamBuf::sync()
90{
91 const int n = static_cast<int>(pptr() - pbase());
92 assert(n >= 0 && (pbase() + n == pptr())); // no LASS_ASSERT, as that writes to ProxyOStream, which may attempt to write to this buffer again.
93 if (n == 0)
94 {
95 return 0;
96 }
97 buffer_[n] = 0;
98
99 bool ok = false;
100 if (Py_IsInitialized())
101 {
102 LockGIL LASS_UNUSED(lock);
103 PyObject* obj = PySys_GetObject(const_cast<char*>(name_));
104 if (obj)
105 {
106 ok = PyFile_WriteString(buffer_, obj) == 0;
107 if (!ok)
108 {
109 PyErr_Clear();
110 }
111 }
112 }
113
114 if (!ok)
115 {
116 // fall back on the C stream
117 ok = fputs(buffer_, file_) != EOF;
118 }
119
120 pbump(-n);
121 return ok ? 0 : -1;
122}
123
124
125SysOStream::SysOStream(FILE* file, const char* name):
126 std::ostream(&buffer_),
127 buffer_(file, name)
128{
129 setf(std::ios_base::unitbuf); // disable buffering so that it gets flushed after each insertion.
130}
131
132
133}
134
135
136impl::SysOStream sysStdout(stdout, "stdout");
137impl::SysOStream sysStderr(stderr, "stderr");
138
139}
140}
Comprehensive C++ to Python binding library.
Library for Assembled Shared Sources.
Definition config.h:53