/usr/lib/python3/dist-packages/tidylib/sink.py is in python3-tidylib 0.3.2~dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | # Copyright 2009-2014 Jason Stitt
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import ctypes
import sys
import threading
import platform
try:
from cStringIO import StringIO
except ImportError:
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
__all__ = ['Sink', 'create_sink', 'destroy_sink']
# -------------------------------------------------------------------------- #
# Globals
sinks = {} # of int: Sink
last_sink_id = 0
sink_id_lock = threading.Lock()
# -------------------------------------------------------------------------- #
# ctypes type definitions
# Fix for Windows b/c tidy uses stdcall on Windows
if "Windows" == platform.system():
functype = ctypes.WINFUNCTYPE
else:
functype = ctypes.CFUNCTYPE
PutByteType = functype(None, ctypes.c_int, ctypes.c_char)
class TidyOutputSink(ctypes.Structure):
""" Mirrors the _TidyOutputSink structure in tidy.h """
_fields_ = [
('sinkData', ctypes.c_void_p),
('putByte', PutByteType)
]
# -------------------------------------------------------------------------- #
# Python interface
class Sink(object):
""" Represent a buffer to which Tidy writes errors with a callback function """
def __init__(self, sink_id):
self.data = StringIO()
self.sink_id = sink_id
self.struct = TidyOutputSink()
self.struct.sinkData = ctypes.cast(
ctypes.pointer(ctypes.c_int(sink_id)), ctypes.c_void_p) # Windows fix
write_func = self.data.write # Avoid 2 attr accesses per byte
def put_byte(sink_id, byte):
# We don't need sink_id because we have a separate put_byte
# function for each sink
write_func(byte.decode('utf-8'))
self.struct.putByte = PutByteType(put_byte)
self._as_parameter_ = ctypes.byref(self.struct)
def __str__(self):
return self.data.getvalue()
def create_sink():
""" Return a new Sink with a numeric ID incremented in a threadsafe way """
global last_sink_id, sink_id_lock, sinks
sink_id_lock.acquire()
try:
this_sink_id = last_sink_id
last_sink_id = (last_sink_id + 1) % sys.maxsize
# If you have more than maxint sinks open at a time, you're screwed
finally:
sink_id_lock.release()
sink = Sink(this_sink_id)
sinks[this_sink_id] = sink
return sink
def destroy_sink(sink):
""" Free a Sink object by eliminating the reference from the global map """
global sinks
del sinks[sink.sink_id]
del sink
|