def CalcBreakInfo(self): (source, encoding) = get_source(self.m_filename) _source = as_string(source + as_unicode('\n'), encoding) code = compile(_source, self.m_filename, "exec") self.m_scope_break_info = [] self.m_first_line = code.co_firstlineno self.m_last_line = 0 fqn = [] t = [code] while len(t) > 0: c = t.pop(0) if type(c) == tuple: self.m_scope_break_info.append(CScopeBreakInfo(*c)) fqn.pop() continue fqn = fqn + [c.co_name] valid_lines = CalcValidLines(c) self.m_last_line = max(self.m_last_line, valid_lines[-1]) _fqn = as_unicode('.'.join(fqn), encoding) si = (_fqn, valid_lines) subcodeslist = self.__CalcSubCodesList(c) t = subcodeslist + [si] + t
def get_source_line(filename, lineno): (lines, encoding, ffilesystem) = lines_cache(filename) if lineno > len(lines): return as_unicode('') return lines[lineno - 1] + as_unicode('\n')
def __init__(self, age, port, pid, filename, rid, state, fembedded): assert(is_unicode(rid)) self.m_age = age self.m_port = port self.m_pid = pid self.m_filename = as_unicode(filename, sys.getfilesystemencoding()) self.m_module_name = as_unicode(CalcModuleName(filename), sys.getfilesystemencoding()) self.m_rid = rid self.m_state = as_unicode(state) self.m_fembedded = fembedded
def lines_cache(filename): filename = g_found_unicode_files.get(filename, filename) if filename in g_lines_cache: return g_lines_cache[filename] (source, encoding, ffilesystem) = source_provider(filename) source = source.replace(as_unicode('\r\n'), as_unicode('\n')) lines = source.split(as_unicode('\n')) g_lines_cache[filename] = (lines, encoding, ffilesystem) return (lines, encoding, ffilesystem)
class CEventBreakpoint(CEvent): """ A breakpoint or breakpoints changed. """ DISABLE = as_unicode('disable') ENABLE = as_unicode('enable') REMOVE = as_unicode('remove') SET = as_unicode('set') def __init__(self, bp, action=SET, id_list=None, fAll=False): self.m_bp = breakpoint_copy(bp) self.m_action = action self.m_id_list = id_list or [] self.m_fAll = fAll
def repr_list(pattern, l, length, encoding, is_valid): length = max(0, length - len(pattern) + 2) s = '' index = 0 try: for i in l: # # Remove any trace of session password from data structures that # go over the network. # if type(i) == str and i in [ '_rpdb2_args', '_rpdb2_pwd', 'm_rpdb2_pwd' ]: continue s += repr_ltd(i, length - len(s), encoding, is_valid) index += 1 if index < len(l) and len(s) > length: is_valid[0] = False if not s.endswith('...'): s += '...' break if index < len(l) or (index == 1 and pattern[0] == '('): s += ', ' except AttributeError: is_valid[0] = False return as_unicode(pattern % s)
def do_crypto(self, args, fencrypt): """ Sign args and possibly encrypt. Return signed/encrypted string. """ if not fencrypt and not self.m_fAllowUnencrypted: raise EncryptionExpected if fencrypt and not is_encryption_supported(): raise EncryptionNotSupported (digest, s) = self.__sign(args) fcompress = False if len(s) > 50000: _s = zlib.compress(s) if len(_s) < len(s) * 0.4: s = _s fcompress = True if fencrypt: s = self.__encrypt(s) s = base64_encodestring(s) u = as_unicode(s) return (fcompress, digest, u)
def source_provider(filename): source = None ffilesystem = False try: if g_source_provider_aux != None: source = g_source_provider_aux(filename) except IOError: v = sys.exc_info()[1] if SOURCE_NOT_AVAILABLE in v.args: raise try: if source == None: source = source_provider_blender(filename) except IOError: v = sys.exc_info()[1] if BLENDER_SOURCE_NOT_AVAILABLE in v.args: raise if source == None: source = source_provider_filesystem(filename) ffilesystem = True encoding = ParseEncoding(source) if not is_unicode(source): source = as_unicode(source, encoding) return source, encoding, ffilesystem
def repr_base(v, length, is_valid): r = repr(v) if len(r) > length: is_valid[0] = False r = r[:length] + '...' return as_unicode(r)
def repr_str_raw(s, length, is_valid): if is_unicode(s): eli = ELLIPSIS_UNICODE else: eli = ELLIPSIS_BYTES if len(s) > length: is_valid[0] = False s = s[:length] + eli return as_unicode(repr(s))
def repr_str(s, length, encoding, is_valid): try: s = as_unicode(s, encoding, fstrict=True) r = repr_unicode(s, length, is_valid) return r[1:] except: # # If a string is not encoded as utf-8 its repr() will be done with # the regular repr() function. # return repr_str_raw(s, length, is_valid)
def breakpoint_copy(bp): if bp is None: return None _bp = copy.copy(bp) #filename = g_found_unicode_files.get(bp.m_filename, bp.m_filename) _bp.m_filename = as_unicode(bp.m_filename, sys.getfilesystemencoding()) _bp.m_code = None return _bp
def repr_unicode(s, length, is_valid): index = [2, 1][is_py3k()] rs = '' for c in s: if len(rs) > length: is_valid[0] = False rs += '...' break if ord(c) < 128: rs += repr(c)[index:-1] else: rs += c if not "'" in rs: return as_unicode("u'%s'" % rs) if not '"' in rs: return as_unicode('u"%s"' % rs) return as_unicode("u'%s'" % rs.replace("'", "\\'"))
def repr_dict(pattern, d, length, encoding, is_valid): length = max(0, length - len(pattern) + 2) s = '' index = 0 try: for k in d: # # Remove any trace of session password from data structures that # go over the network. # if type(k) == str and k in [ '_rpdb2_args', '_rpdb2_pwd', 'm_rpdb2_pwd' ]: continue v = d[k] s += repr_ltd(k, length - len(s), encoding, is_valid) if len(s) > length: is_valid[0] = False if not s.endswith('...'): s += '...' break s += ': ' + repr_ltd(v, length - len(s), encoding, is_valid) index += 1 if index < len(d) and len(s) > length: is_valid[0] = False if not s.endswith('...'): s += '...' break if index < len(d): s += ', ' except AttributeError: is_valid[0] = False return as_unicode(pattern % s)
def ParseEncoding(txt): """ Parse document encoding according to: http://docs.python.org/ref/encodings.html """ eol = '\n' if not is_unicode(txt): eol = as_bytes('\n') l = txt.split(eol, 20)[:-1] for line in l: line = as_unicode(line) encoding = ParseLineEncoding(line) if encoding is not None: try: codecs.lookup(encoding) return encoding except: return 'utf-8' return 'utf-8'
import sys, os from rpdb.utils import as_unicode, is_unicode, print_debug_exception, ENCODING_RAW_I, as_bytes from rpdb.compat import sets, unicode def is_py3k(): return sys.version_info[0] >= 3 DEFAULT_PATH_SUFFIX_LENGTH = 55 ELLIPSIS_UNICODE = as_unicode('...') ELLIPSIS_BYTES = as_bytes('...') def calc_suffix(_str, n): """ Return an n charaters suffix of the argument string of the form '...suffix'. """ if len(_str) <= n: return _str return '...' + _str[-(n - 3):] def class_name(c): s = safe_str(c)
def dispatcher_method(self, rpdb_version, fencrypt, fcompress, digest, msg): """ Process RPC call. """ #print_debug('dispatcher_method() called with: %s, %s, %s, %s' % (rpdb_version, fencrypt, digest, msg[:100])) if rpdb_version != as_unicode(get_interface_compatibility_version()): raise BadVersion(as_unicode(get_version())) try: try: # # Decrypt parameters. # ((name, __params, target_rid), client_id) = self.m_crypto.undo_crypto( fencrypt, fcompress, digest, msg) except AuthenticationBadIndex: e = sys.exc_info()[1] #print_debug_exception() # # Notify the caller on the expected index. # max_index = self.m_crypto.get_max_index() args = (max_index, None, e) (fcompress, digest, msg) = self.m_crypto.do_crypto(args, fencrypt) return (fencrypt, fcompress, digest, msg) r = None e = None try: # # We are forcing the 'export_' prefix on methods that are # callable through XML-RPC to prevent potential security # problems # func = getattr(self, 'export_' + name) except AttributeError: raise Exception('method "%s" is not supported' % ('export_' + name)) try: if (target_rid != 0) and (target_rid != self.m_rid): raise NotAttached # # Record that client id is still attached. # self.record_client_heartbeat(client_id, name, __params) r = func(*__params) except Exception: _e = sys.exc_info()[1] print_debug_exception() e = _e # # Send the encrypted result. # max_index = self.m_crypto.get_max_index() args = (max_index, r, e) (fcompress, digest, msg) = self.m_crypto.do_crypto(args, fencrypt) return (fencrypt, fcompress, digest, msg) except: print_debug_exception() raise
def get_source(filename): (lines, encoding, ffilesystem) = lines_cache(filename) source = as_unicode('\n').join(lines) return (source, encoding)
def __init__(self, state): self.m_state = as_unicode(state)
def is_match(self, arg): return self.m_state == as_unicode(arg)
import os.path import sys, codecs from rpdb.globals import g_found_unicode_files from rpdb.utils import as_unicode, is_unicode, winlower, as_bytes, mygetfile BLENDER_SOURCE_NOT_AVAILABLE = as_unicode('Blender script source code is not available.') SOURCE_NOT_AVAILABLE = as_unicode('Source code is not available.') ENCODING_UTF8_PREFIX_1 = '\xef\xbb\xbf' ENCODING_SOURCE = '# -*- coding: %s -*-\n' MODULE_SCOPE = '?' MODULE_SCOPE2 = '<module>' SCOPE_SEP = '.' g_source_provider_aux = None g_lines_cache = {} def ParseLineEncoding(l): if l.startswith('# -*- coding: '): e = l[len('# -*- coding: '):].split()[0] return e if l.startswith('# vim:fileencoding='): e = l[len('# vim:fileencoding='):].strip() return e return None
def repr_ltd(x, length, encoding, is_valid=[True]): try: length = max(0, length) try: if isinstance(x, frozenset): return repr_list('frozenset([%s])', x, length, encoding, is_valid) if isinstance(x, set): return repr_list('set([%s])', x, length, encoding, is_valid) except NameError: pass if isinstance(x, sets.Set): return repr_list('sets.Set([%s])', x, length, encoding, is_valid) if isinstance(x, sets.ImmutableSet): return repr_list('sets.ImmutableSet([%s])', x, length, encoding, is_valid) if isinstance(x, list): return repr_list('[%s]', x, length, encoding, is_valid) if isinstance(x, tuple): return repr_list('(%s)', x, length, encoding, is_valid) if isinstance(x, dict): return repr_dict('{%s}', x, length, encoding, is_valid) if encoding == ENCODING_RAW_I and [ True for t in [str, unicode, bytearray, bytes] if t is type(x) ]: return repr_str_raw(x, length, is_valid) if type(x) is unicode: return repr_unicode(x, length, is_valid) if type(x) is bytearray: return repr_bytearray(x, length, encoding, is_valid) if type(x) is bytes: return repr_bytes(x, length, encoding, is_valid) if type(x) is str: return repr_str(x, length, encoding, is_valid) if [True for t in [bool, int, float, type(None)] if t is type(x)]: return repr_base(x, length, is_valid) is_valid[0] = False y = safe_repr(x)[:length] if len(y) == length: y += '...' if encoding == ENCODING_RAW_I: encoding = 'utf-8' try: y = as_unicode(y, encoding, fstrict=True) return y except: pass encoding = sys.getfilesystemencoding() y = as_unicode(y, encoding) return y except: print_debug_exception() return as_unicode('N/A')
def __request(self, name, params): """ Call debuggee method 'name' with parameters 'params'. """ while True: try: # # Encrypt method and params. # fencrypt = self.get_encryption() args = (as_unicode(name), params, self.m_target_rid) (fcompress, digest, msg) = self.m_crypto.do_crypto(args, fencrypt) rpdb_version = as_unicode( get_interface_compatibility_version()) r = self.m_method(rpdb_version, fencrypt, fcompress, digest, msg) (fencrypt, fcompress, digest, msg) = r # # Decrypt response. # ((max_index, _r, _e), id) = self.m_crypto.undo_crypto(fencrypt, fcompress, digest, msg, fVerifyIndex=False) if _e is not None: raise _e except AuthenticationBadIndex: e = sys.exc_info()[1] print_debug("Caught AuthenticationBadIndex: %s" % str(e)) self.m_crypto.set_index(e.m_max_index, e.m_anchor) continue except xmlrpclib.Fault: fault = sys.exc_info()[1] print_debug("Caught xmlrpclib.Fault: %s" % fault.faultString) if class_name(BadVersion) in fault.faultString: s = fault.faultString.split("'") version = ['', s[1]][len(s) > 0] raise BadVersion(version) if class_name(EncryptionExpected) in fault.faultString: raise EncryptionExpected elif class_name(EncryptionNotSupported) in fault.faultString: if self.m_crypto.m_fAllowUnencrypted: self.__set_encryption(False) continue raise EncryptionNotSupported elif class_name(DecryptionFailure) in fault.faultString: raise DecryptionFailure elif class_name(AuthenticationBadData) in fault.faultString: raise AuthenticationBadData elif class_name(AuthenticationFailure) in fault.faultString: raise AuthenticationFailure else: print_debug_exception() assert False except xmlrpclib.ProtocolError: print_debug("Caught ProtocolError for %s" % name) #print_debug_exception() raise CConnectionException return _r
def __init__(self, tid, name): self.m_tid = tid self.m_name = as_unicode(name)