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