VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/src/PyGInputStream.cpp

Last change on this file was 103176, checked in by vboxsync, 3 months ago

libs/xpcom/python: Some cleanup, bugref:3409

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is the Python XPCOM language bindings.
15 *
16 * The Initial Developer of the Original Code is
17 * ActiveState Tool Corp.
18 * Portions created by the Initial Developer are Copyright (C) 2000
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Mark Hammond <MarkH@ActiveState.com> (original author)
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38// PyGInputStream.cpp
39//
40// This code is part of the XPCOM extensions for Python.
41//
42// Written October 2000 by Mark Hammond.
43//
44// Based heavily on the Python COM support, which is
45// (c) Mark Hammond and Greg Stein.
46//
47// (c) 2000, ActiveState corp.
48
49#include "PyXPCOM_std.h"
50#include <nsIInputStream.h>
51
52class PyG_nsIInputStream : public PyG_Base, public nsIInputStream
53{
54public:
55 PyG_nsIInputStream(PyObject *instance) : PyG_Base(instance, NS_GET_IID(nsIInputStream)) {;}
56 PYGATEWAY_BASE_SUPPORT(nsIInputStream, PyG_Base);
57
58 NS_IMETHOD Close(void);
59 NS_IMETHOD Available(PRUint32 *_retval);
60 NS_IMETHOD Read(char * buf, PRUint32 count, PRUint32 *_retval);
61 NS_IMETHOD ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval);
62 NS_IMETHOD IsNonBlocking(PRBool *aNonBlocking);
63};
64
65
66DECLHIDDEN (PyG_Base *) MakePyG_nsIInputStream(PyObject *instance)
67{
68 return new PyG_nsIInputStream(instance);
69}
70
71NS_IMETHODIMP
72PyG_nsIInputStream::Close()
73{
74 CEnterLeavePython _celp;
75 const char *methodName = "close";
76 return InvokeNativeViaPolicy(methodName, NULL);
77}
78
79NS_IMETHODIMP
80PyG_nsIInputStream::Available(PRUint32 *_retval)
81{
82 NS_PRECONDITION(_retval, "null pointer");
83 CEnterLeavePython _celp;
84 PyObject *ret;
85 const char *methodName = "available";
86 nsresult nr = InvokeNativeViaPolicy(methodName, &ret);
87 if (NS_SUCCEEDED(nr)) {
88 *_retval = PyInt_AsLong(ret);
89 if (PyErr_Occurred())
90 nr = HandleNativeGatewayError(methodName);
91 Py_XDECREF(ret);
92 }
93 return nr;
94}
95
96NS_IMETHODIMP
97PyG_nsIInputStream::Read(char * buf, PRUint32 count, PRUint32 *_retval)
98{
99 NS_PRECONDITION(_retval, "null pointer");
100 NS_PRECONDITION(buf, "null pointer");
101 CEnterLeavePython _celp;
102 PyObject *ret;
103 const char *methodName = "read";
104 nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "i", count);
105 if (NS_SUCCEEDED(nr)) {
106#if 0 /* VBox: new buffer protocol (though I could use it for Py_LIMITED_API and ditch the warning, but cpython specific) */
107 Py_buffer py_view;
108 if (PyObject_GetBuffer(ret, &py_view, PyBUF_SIMPLE) == 0) {
109 if (py_view.len <= count) {
110 count = py_view.len;
111 } else {
112 PyXPCOM_LogWarning("nsIInputStream::read() was asked for %d bytes, but the string returned is %d bytes - truncating!\n", count, py_size);
113 }
114 memcpy(buf, py_view.py_buf, count);
115 PyBuffer_Release(&py_view);
116 *_retval = count;
117 } else {
118 PyErr_Format(PyExc_TypeError, "nsIInputStream::read() method must return a buffer object - not a '%s' object", PyXPCOM_ObTypeName(ret));
119 nr = HandleNativeGatewayError(methodName);
120 }
121#else /* Old protocol: */
122# ifndef VBOX /* unsafe cast on 64-bit hosts. */
123 PRUint32 py_size;
124 const void *py_buf;
125 if (PyObject_AsReadBuffer(ret, &py_buf, (Py_ssize_t*)&py_size)!=0) {
126# else /* VBOX */
127 const void *py_buf;
128# if PY_VERSION_HEX >= 0x02050000 || defined(PY_SSIZE_T_MIN)
129 Py_ssize_t py_size;
130# else
131 int py_size;
132# endif
133 if (PyObject_AsReadBuffer(ret, &py_buf, &py_size)!=0) {
134# endif /* VBOX */
135 PyErr_Format(PyExc_TypeError, "nsIInputStream::read() method must return a buffer object - not a '%s' object", PyXPCOM_ObTypeName(ret));
136 nr = HandleNativeGatewayError(methodName);
137 } else {
138 if (py_size > count) {
139 PyXPCOM_LogWarning("nsIInputStream::read() was asked for %d bytes, but the string returned is %d bytes - truncating!\n", count, py_size);
140 py_size = count;
141 }
142 memcpy(buf, py_buf, py_size);
143 *_retval = py_size;
144 }
145#endif
146 }
147 return nr;
148}
149
150
151NS_IMETHODIMP
152PyG_nsIInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval)
153{
154 NS_WARNING("ReadSegments() not implemented!!!");
155 return NS_ERROR_NOT_IMPLEMENTED;
156}
157
158NS_IMETHODIMP
159PyG_nsIInputStream::IsNonBlocking(PRBool *aNonBlocking)
160{
161 NS_PRECONDITION(aNonBlocking, "null pointer");
162 CEnterLeavePython _celp;
163 PyObject *ret;
164 const char *methodName = "isNonBlocking";
165 nsresult nr = InvokeNativeViaPolicy(methodName, &ret);
166 if (NS_SUCCEEDED(nr)) {
167 *aNonBlocking = PyInt_AsLong(ret);
168 if (PyErr_Occurred())
169 nr = HandleNativeGatewayError(methodName);
170 Py_XDECREF(ret);
171 }
172 return nr;
173}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use