VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/__init__.py@ 59795

Last change on this file since 59795 was 59795, checked in by vboxsync, 9 years ago

temporarily back out the recent Python 3 changes (r105649, r105645, r105644, r105643, r105641)

  • Property svn:eol-style set to native
File size: 6.5 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>
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
39# The XPCOM (Cross Platform COM) package.
40import exceptions
41
42# A global "verbose" flag - currently used by the
43# server package to print trace messages
44verbose = 0
45# Map of nsresult -> constant_name.
46hr_map = {}
47
48# The standard XPCOM exception object.
49# Instances of this class are raised by the XPCOM extension module.
50class Exception(exceptions.Exception):
51 def __init__(self, errno, message = None):
52 assert int(errno) == errno, "The errno param must be an integer"
53 self.errno = errno
54 self.msg = message
55 exceptions.Exception.__init__(self, errno)
56 def __str__(self):
57 if not hr_map:
58 import nsError
59 for name, val in nsError.__dict__.items():
60 if type(val)==type(0):
61 hr_map[val] = name
62 message = self.msg
63 if message is None:
64 message = hr_map.get(self.errno)
65 if message is None:
66 message = ""
67 return "0x%x (%s)" % (self.errno & 0xFFFFFFFF, message)
68
69# An alias for Exception - allows code to say "from xpcom import COMException"
70# rather than "Exception", preventing clashes with the builtin Exception
71COMException = Exception
72
73# Exceptions thrown by servers. It can be good for diagnostics to
74# differentiate between a ServerException (which was presumably explicitly thrown)
75# and a normal exception which may simply be propagating down.
76# (When ServerException objects are thrown across the XPConnect
77# gateway they will be converted back to normal client exceptions if
78# subsequently re-caught by Python)
79class ServerException(Exception):
80 def __init__(self, errno=None, *args, **kw):
81 if errno is None:
82 import nsError
83 errno = nsError.NS_ERROR_FAILURE
84 Exception.__init__(self, errno, *args, **kw)
85
86# Logging support - setup the 'xpcom' logger to write to the Mozilla
87# console service, and also to sys.stderr, or optionally a file.
88# Environment variables supports:
89# PYXPCOM_LOG_FILE=filename - if set, used instead of sys.stderr.
90# PYXPCOM_LOG_LEVEL=level - level may be a number or a logging level
91# constant (eg, 'debug', 'error')
92# Later it may make sense to allow a different log level to be set for
93# the file than for the console service.
94import logging
95class ConsoleServiceStream:
96 # enough of a stream to keep logging happy
97 def flush(self):
98 pass
99 def write(self, msg):
100 import _xpcom
101 _xpcom.LogConsoleMessage(msg)
102 def close(self):
103 pass
104
105def setupLogging():
106 import sys, os, threading, thread
107 hdlr = logging.StreamHandler(ConsoleServiceStream())
108 fmt = logging.Formatter(logging.BASIC_FORMAT)
109 hdlr.setFormatter(fmt)
110 # There is a bug in 2.3 and 2.4.x logging module in that it doesn't
111 # use an RLock, leading to deadlocks in some cases (specifically,
112 # logger.warning("ob is %r", ob), and where repr(ob) itself tries to log)
113 # Later versions of logging use an RLock, so we detect an "old" style
114 # handler and update its lock
115 if type(hdlr.lock) == thread.LockType:
116 hdlr.lock = threading.RLock()
117
118 logger.addHandler(hdlr)
119 # The console handler in mozilla does not go to the console!?
120 # Add a handler to print to stderr, or optionally a file
121 # PYXPCOM_LOG_FILE can specify a filename
122 filename = os.environ.get("PYXPCOM_LOG_FILE")
123 stream = sys.stderr # this is what logging uses as default
124 if filename:
125 try:
126 # open without buffering so never pending output
127 stream = open(filename, "wU", 0)
128 except IOError, why:
129 print >> sys.stderr, "pyxpcom failed to open log file '%s': %s" \
130 % (filename, why)
131 # stream remains default
132
133 hdlr = logging.StreamHandler(stream)
134 # see above - fix a deadlock problem on this handler too.
135 if type(hdlr.lock) == thread.LockType:
136 hdlr.lock = threading.RLock()
137
138 fmt = logging.Formatter(logging.BASIC_FORMAT)
139 hdlr.setFormatter(fmt)
140 logger.addHandler(hdlr)
141 # Allow PYXPCOM_LOG_LEVEL to set the level
142 level = os.environ.get("PYXPCOM_LOG_LEVEL")
143 if level:
144 try:
145 level = int(level)
146 except ValueError:
147 try:
148 # might be a symbolic name - all are upper-case
149 level = int(getattr(logging, level.upper()))
150 except (AttributeError, ValueError):
151 logger.warning("The PYXPCOM_LOG_LEVEL variable specifies an "
152 "invalid level")
153 level = None
154 if level:
155 logger.setLevel(level)
156
157logger = logging.getLogger('xpcom')
158# If someone else has already setup this logger, leave things alone.
159if len(logger.handlers) == 0:
160 setupLogging()
161
162# Cleanup namespace - but leave 'logger' there for people to use, so they
163# don't need to know the exact name of the logger.
164del ConsoleServiceStream, logging, setupLogging
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette