* Several modules: change "class C(): ..." to "class C: ...".
* flp.py: support for frozen forms.
* Added string.find() which is like index but returns -1 if not found
_write_long(f, himant)
_write_long(f, lomant)
-class Chunk():
+class Chunk:
def init(self, file):
self.file = file
self.chunkname = self.file.read(4)
if self.chunksize & 1:
dummy = self.read(1)
-class Aifc_read():
+class Aifc_read:
# Variables used in this class:
#
# These variables are available to the user though appropriate
name = _read_string(chunk)
self._markers.append((id, pos, name))
-class Aifc_write():
+class Aifc_write:
# Variables used in this class:
#
# These variables are user settable through appropriate methods
import string
+# Magic number from <socket.h>
+MSG_OOB = 0x1 # Process data out of band
+
+
# The standard FTP server control port
FTP_PORT = 21
CRLF = '\r\n'
+# Telnet special characters
+DM = chr(242) # Data Mark
+IP = chr(244) # Interrupt Process
+
# Next port to be used by makeport(), with PORT_OFFSET added
# (This is now only used when the python interpreter doesn't support
# the getsockname() method yet)
if resp[0] <> '2':
raise error_reply, resp
+ # Abort a file transfer. Uses out-of-band data.
+ # This does not follow the procedure from the RFC to send Telnet
+ # IP and Synch; that doesn't seem to work with the servers I've
+ # tried. Instead, just send the ABOR command as OOB data.
+ def abort(self):
+ line = 'ABOR' + CRLF
+ if self.debugging > 1: print '*put urgent*', `line`
+ self.sock.send(line, MSG_OOB)
+ resp = self.getmultiline()
+ if resp[:3] not in ('426', '226'):
+ raise error_proto, resp
+
# Send a command and return the response
def sendcmd(self, cmd):
self.putcmd(cmd)
else:
return _dbid_map[v]
-class Cddb():
+class Cddb:
def init(self, tracklist):
self.artist = ''
self.title = ''
cdplayerrc = '.cdplayerrc'
-class Cdplayer():
+class Cdplayer:
def init(self, tracklist):
import string
self.artist = ''
# Internal: see if a cached version of the file exists
#
MAGIC = '.fdc'
+_internal_cache = {} # Used by frozen scripts only
def checkcache(filename):
+ if _internal_cache.has_key(filename):
+ altforms = _internal_cache[filename]
+ return _unpack_cache(altforms)
import marshal
fp, filename = _open_formfile2(filename)
fp.close()
return None
#print 'flp: valid cache file', cachename
altforms = marshal.load(fp)
+ return _unpack_cache(altforms)
+ finally:
+ fp.close()
+
+def _unpack_cache(altforms):
forms = {}
for name in altforms.keys():
altobj, altlist = altforms[name]
list.append(nobj)
forms[name] = obj, list
return forms
- finally:
- fp.close()
def rdlong(fp):
s = fp.read(4)
return # Never mind
fp.write('\0\0\0\0') # Seek back and write MAGIC when done
wrlong(fp, getmtime(filename))
+ altforms = _pack_cache(forms)
+ marshal.dump(altforms, fp)
+ fp.seek(0)
+ fp.write(MAGIC)
+ fp.close()
+ #print 'flp: wrote cache file', cachename
+
+#
+# External: print some statements that set up the internal cache.
+# This is for use with the "freeze" script. You should call
+# flp.freeze(filename) for all forms used by the script, and collect
+# the output on a file in a module file named "frozenforms.py". Then
+# in the main program of the script import frozenforms.
+# (Don't forget to take this out when using the unfrozen version of
+# the script!)
+#
+def freeze(filename):
+ forms = parse_forms(filename)
+ altforms = _pack_cache(forms)
+ print 'import flp'
+ print 'flp._internal_cache[', `filename`, '] =', altforms
+
+#
+# Internal: create the data structure to be placed in the cache
+#
+def _pack_cache(forms):
altforms = {}
for name in forms.keys():
obj, list = forms[name]
altlist = []
for obj in list: altlist.append(obj.__dict__)
altforms[name] = altobj, altlist
- marshal.dump(altforms, fp)
- fp.seek(0)
- fp.write(MAGIC)
- fp.close()
- #print 'flp: wrote cache file', cachename
-
+ return altforms
+
#
# Internal: Locate form file (using PYTHONPATH) and open file
#
if func:
func(arg, cb_type, data)
-class Readcd():
+class Readcd:
def init(self, *arg):
if len(arg) == 0:
self.player = cd.open()
print 'def some_function(): pass'
print FN, '[', `uid`, '] = type(some_function)'
elif x == type(some_class):
- print 'class some_class(): pass'
+ print 'class some_class: pass'
print FN, '[', `uid`, '] = type(some_class)'
elif x == type(some_instance):
- print 'class another_class(): pass'
+ print 'class another_class: pass'
print 'some_instance = another_class()'
print FN, '[', `uid`, '] = type(some_instance)'
elif x == type(some_instance.method):
- print 'class yet_another_class():'
+ print 'class yet_another_class:'
print ' def method(): pass'
print 'another_instance = yet_another_class()'
print FN, '[', `uid`, '] = type(another_instance.method)'
else:
return _dbid_map[v]
-class Cddb():
+class Cddb:
def init(self, tracklist):
self.artist = ''
self.title = ''
cdplayerrc = '.cdplayerrc'
-class Cdplayer():
+class Cdplayer:
def init(self, tracklist):
import string
self.artist = ''
# Internal: see if a cached version of the file exists
#
MAGIC = '.fdc'
+_internal_cache = {} # Used by frozen scripts only
def checkcache(filename):
+ if _internal_cache.has_key(filename):
+ altforms = _internal_cache[filename]
+ return _unpack_cache(altforms)
import marshal
fp, filename = _open_formfile2(filename)
fp.close()
return None
#print 'flp: valid cache file', cachename
altforms = marshal.load(fp)
+ return _unpack_cache(altforms)
+ finally:
+ fp.close()
+
+def _unpack_cache(altforms):
forms = {}
for name in altforms.keys():
altobj, altlist = altforms[name]
list.append(nobj)
forms[name] = obj, list
return forms
- finally:
- fp.close()
def rdlong(fp):
s = fp.read(4)
return # Never mind
fp.write('\0\0\0\0') # Seek back and write MAGIC when done
wrlong(fp, getmtime(filename))
+ altforms = _pack_cache(forms)
+ marshal.dump(altforms, fp)
+ fp.seek(0)
+ fp.write(MAGIC)
+ fp.close()
+ #print 'flp: wrote cache file', cachename
+
+#
+# External: print some statements that set up the internal cache.
+# This is for use with the "freeze" script. You should call
+# flp.freeze(filename) for all forms used by the script, and collect
+# the output on a file in a module file named "frozenforms.py". Then
+# in the main program of the script import frozenforms.
+# (Don't forget to take this out when using the unfrozen version of
+# the script!)
+#
+def freeze(filename):
+ forms = parse_forms(filename)
+ altforms = _pack_cache(forms)
+ print 'import flp'
+ print 'flp._internal_cache[', `filename`, '] =', altforms
+
+#
+# Internal: create the data structure to be placed in the cache
+#
+def _pack_cache(forms):
altforms = {}
for name in forms.keys():
obj, list = forms[name]
altlist = []
for obj in list: altlist.append(obj.__dict__)
altforms[name] = altobj, altlist
- marshal.dump(altforms, fp)
- fp.seek(0)
- fp.write(MAGIC)
- fp.close()
- #print 'flp: wrote cache file', cachename
-
+ return altforms
+
#
# Internal: Locate form file (using PYTHONPATH) and open file
#
if func:
func(arg, cb_type, data)
-class Readcd():
+class Readcd:
def init(self, *arg):
if len(arg) == 0:
self.player = cd.open()
import fpformat
import marshal
-class Profile():
+class Profile:
def init(self):
self.timings = {}
frame = frame.f_back
return d
-class Stats():
+class Stats:
def init(self, file):
f = open(file, 'r')
self.stats = marshal.load(f)
res = res + (sep + w)
return res[len(sep):]
-# Find substring
+# Find substring, raise exception if not found
index_error = 'substring not found in string.index'
def index(s, sub, *args):
if args:
while i < m:
if sub == s[i:i+n]: return i
i = i+1
- raise index_error, (s, sub)
+ raise index_error, (s, sub) + args
+
+# Find substring, return -1 if not found
+def find(*args):
+ try:
+ return apply(index, args)
+ except index_error:
+ return -1
# Convert string to integer
atoi_error = 'non-numeric argument to string.atoi'
res = res + (sep + w)
return res[len(sep):]
-# Find substring
+# Find substring, raise exception if not found
index_error = 'substring not found in string.index'
def index(s, sub, *args):
if args:
while i < m:
if sub == s[i:i+n]: return i
i = i+1
- raise index_error, (s, sub)
+ raise index_error, (s, sub) + args
+
+# Find substring, return -1 if not found
+def find(*args):
+ try:
+ return apply(index, args)
+ except index_error:
+ return -1
# Convert string to integer
atoi_error = 'non-numeric argument to string.atoi'
if not [1]: raise TestFailed, '[1] is false instead of true'
if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true'
def f(): pass
-class C(): pass
+class C: pass
import sys
x = C()
if not f: raise TestFailed, 'f is false instead of true'