* ftplib.py: added abort() command (sends oob data).

* 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
This commit is contained in:
Guido van Rossum 1993-05-24 14:16:22 +00:00
parent b3f7258f14
commit d316607732
15 changed files with 127 additions and 35 deletions

View File

@ -281,7 +281,7 @@ def _write_float(f, x):
_write_long(f, himant) _write_long(f, himant)
_write_long(f, lomant) _write_long(f, lomant)
class Chunk(): class Chunk:
def init(self, file): def init(self, file):
self.file = file self.file = file
self.chunkname = self.file.read(4) self.chunkname = self.file.read(4)
@ -322,7 +322,7 @@ class Chunk():
if self.chunksize & 1: if self.chunksize & 1:
dummy = self.read(1) dummy = self.read(1)
class Aifc_read(): class Aifc_read:
# Variables used in this class: # Variables used in this class:
# #
# These variables are available to the user though appropriate # These variables are available to the user though appropriate
@ -568,7 +568,7 @@ class Aifc_read():
name = _read_string(chunk) name = _read_string(chunk)
self._markers.append((id, pos, name)) self._markers.append((id, pos, name))
class Aifc_write(): class Aifc_write:
# Variables used in this class: # Variables used in this class:
# #
# These variables are user settable through appropriate methods # These variables are user settable through appropriate methods

View File

@ -32,6 +32,10 @@ import socket
import string import string
# Magic number from <socket.h>
MSG_OOB = 0x1 # Process data out of band
# The standard FTP server control port # The standard FTP server control port
FTP_PORT = 21 FTP_PORT = 21
@ -53,6 +57,10 @@ all_errors = (error_reply, error_temp, error_perm, error_proto, \
CRLF = '\r\n' 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 # Next port to be used by makeport(), with PORT_OFFSET added
# (This is now only used when the python interpreter doesn't support # (This is now only used when the python interpreter doesn't support
# the getsockname() method yet) # the getsockname() method yet)
@ -152,6 +160,18 @@ class FTP:
if resp[0] <> '2': if resp[0] <> '2':
raise error_reply, resp 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 # Send a command and return the response
def sendcmd(self, cmd): def sendcmd(self, cmd):
self.putcmd(cmd) self.putcmd(cmd)

View File

@ -25,7 +25,7 @@ def _dbid(v):
else: else:
return _dbid_map[v] return _dbid_map[v]
class Cddb(): class Cddb:
def init(self, tracklist): def init(self, tracklist):
self.artist = '' self.artist = ''
self.title = '' self.title = ''

View File

@ -16,7 +16,7 @@
cdplayerrc = '.cdplayerrc' cdplayerrc = '.cdplayerrc'
class Cdplayer(): class Cdplayer:
def init(self, tracklist): def init(self, tracklist):
import string import string
self.artist = '' self.artist = ''

View File

@ -59,7 +59,11 @@ def parse_forms(filename):
# Internal: see if a cached version of the file exists # Internal: see if a cached version of the file exists
# #
MAGIC = '.fdc' MAGIC = '.fdc'
_internal_cache = {} # Used by frozen scripts only
def checkcache(filename): def checkcache(filename):
if _internal_cache.has_key(filename):
altforms = _internal_cache[filename]
return _unpack_cache(altforms)
import marshal import marshal
fp, filename = _open_formfile2(filename) fp, filename = _open_formfile2(filename)
fp.close() fp.close()
@ -80,6 +84,11 @@ def checkcache(filename):
return None return None
#print 'flp: valid cache file', cachename #print 'flp: valid cache file', cachename
altforms = marshal.load(fp) altforms = marshal.load(fp)
return _unpack_cache(altforms)
finally:
fp.close()
def _unpack_cache(altforms):
forms = {} forms = {}
for name in altforms.keys(): for name in altforms.keys():
altobj, altlist = altforms[name] altobj, altlist = altforms[name]
@ -92,8 +101,6 @@ def checkcache(filename):
list.append(nobj) list.append(nobj)
forms[name] = obj, list forms[name] = obj, list
return forms return forms
finally:
fp.close()
def rdlong(fp): def rdlong(fp):
s = fp.read(4) s = fp.read(4)
@ -128,6 +135,32 @@ def writecache(filename, forms):
return # Never mind return # Never mind
fp.write('\0\0\0\0') # Seek back and write MAGIC when done fp.write('\0\0\0\0') # Seek back and write MAGIC when done
wrlong(fp, getmtime(filename)) 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 = {} altforms = {}
for name in forms.keys(): for name in forms.keys():
obj, list = forms[name] obj, list = forms[name]
@ -135,12 +168,8 @@ def writecache(filename, forms):
altlist = [] altlist = []
for obj in list: altlist.append(obj.__dict__) for obj in list: altlist.append(obj.__dict__)
altforms[name] = altobj, altlist altforms[name] = altobj, altlist
marshal.dump(altforms, fp) return altforms
fp.seek(0)
fp.write(MAGIC)
fp.close()
#print 'flp: wrote cache file', cachename
# #
# Internal: Locate form file (using PYTHONPATH) and open file # Internal: Locate form file (using PYTHONPATH) and open file
# #

View File

@ -21,7 +21,7 @@ def _dopnum(self, cb_type, data):
if func: if func:
func(arg, cb_type, data) func(arg, cb_type, data)
class Readcd(): class Readcd:
def init(self, *arg): def init(self, *arg):
if len(arg) == 0: if len(arg) == 0:
self.player = cd.open() self.player = cd.open()

View File

@ -170,14 +170,14 @@ def dumptype(x, typedict, types, stack):
print 'def some_function(): pass' print 'def some_function(): pass'
print FN, '[', `uid`, '] = type(some_function)' print FN, '[', `uid`, '] = type(some_function)'
elif x == type(some_class): elif x == type(some_class):
print 'class some_class(): pass' print 'class some_class: pass'
print FN, '[', `uid`, '] = type(some_class)' print FN, '[', `uid`, '] = type(some_class)'
elif x == type(some_instance): elif x == type(some_instance):
print 'class another_class(): pass' print 'class another_class: pass'
print 'some_instance = another_class()' print 'some_instance = another_class()'
print FN, '[', `uid`, '] = type(some_instance)' print FN, '[', `uid`, '] = type(some_instance)'
elif x == type(some_instance.method): elif x == type(some_instance.method):
print 'class yet_another_class():' print 'class yet_another_class:'
print ' def method(): pass' print ' def method(): pass'
print 'another_instance = yet_another_class()' print 'another_instance = yet_another_class()'
print FN, '[', `uid`, '] = type(another_instance.method)' print FN, '[', `uid`, '] = type(another_instance.method)'

View File

@ -25,7 +25,7 @@ def _dbid(v):
else: else:
return _dbid_map[v] return _dbid_map[v]
class Cddb(): class Cddb:
def init(self, tracklist): def init(self, tracklist):
self.artist = '' self.artist = ''
self.title = '' self.title = ''

View File

@ -16,7 +16,7 @@
cdplayerrc = '.cdplayerrc' cdplayerrc = '.cdplayerrc'
class Cdplayer(): class Cdplayer:
def init(self, tracklist): def init(self, tracklist):
import string import string
self.artist = '' self.artist = ''

View File

@ -59,7 +59,11 @@ def parse_forms(filename):
# Internal: see if a cached version of the file exists # Internal: see if a cached version of the file exists
# #
MAGIC = '.fdc' MAGIC = '.fdc'
_internal_cache = {} # Used by frozen scripts only
def checkcache(filename): def checkcache(filename):
if _internal_cache.has_key(filename):
altforms = _internal_cache[filename]
return _unpack_cache(altforms)
import marshal import marshal
fp, filename = _open_formfile2(filename) fp, filename = _open_formfile2(filename)
fp.close() fp.close()
@ -80,6 +84,11 @@ def checkcache(filename):
return None return None
#print 'flp: valid cache file', cachename #print 'flp: valid cache file', cachename
altforms = marshal.load(fp) altforms = marshal.load(fp)
return _unpack_cache(altforms)
finally:
fp.close()
def _unpack_cache(altforms):
forms = {} forms = {}
for name in altforms.keys(): for name in altforms.keys():
altobj, altlist = altforms[name] altobj, altlist = altforms[name]
@ -92,8 +101,6 @@ def checkcache(filename):
list.append(nobj) list.append(nobj)
forms[name] = obj, list forms[name] = obj, list
return forms return forms
finally:
fp.close()
def rdlong(fp): def rdlong(fp):
s = fp.read(4) s = fp.read(4)
@ -128,6 +135,32 @@ def writecache(filename, forms):
return # Never mind return # Never mind
fp.write('\0\0\0\0') # Seek back and write MAGIC when done fp.write('\0\0\0\0') # Seek back and write MAGIC when done
wrlong(fp, getmtime(filename)) 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 = {} altforms = {}
for name in forms.keys(): for name in forms.keys():
obj, list = forms[name] obj, list = forms[name]
@ -135,12 +168,8 @@ def writecache(filename, forms):
altlist = [] altlist = []
for obj in list: altlist.append(obj.__dict__) for obj in list: altlist.append(obj.__dict__)
altforms[name] = altobj, altlist altforms[name] = altobj, altlist
marshal.dump(altforms, fp) return altforms
fp.seek(0)
fp.write(MAGIC)
fp.close()
#print 'flp: wrote cache file', cachename
# #
# Internal: Locate form file (using PYTHONPATH) and open file # Internal: Locate form file (using PYTHONPATH) and open file
# #

View File

@ -21,7 +21,7 @@ def _dopnum(self, cb_type, data):
if func: if func:
func(arg, cb_type, data) func(arg, cb_type, data)
class Readcd(): class Readcd:
def init(self, *arg): def init(self, *arg):
if len(arg) == 0: if len(arg) == 0:
self.player = cd.open() self.player = cd.open()

View File

@ -12,7 +12,7 @@ import string
import fpformat import fpformat
import marshal import marshal
class Profile(): class Profile:
def init(self): def init(self):
self.timings = {} self.timings = {}
@ -212,7 +212,7 @@ def depth(frame):
frame = frame.f_back frame = frame.f_back
return d return d
class Stats(): class Stats:
def init(self, file): def init(self, file):
f = open(file, 'r') f = open(file, 'r')
self.stats = marshal.load(f) self.stats = marshal.load(f)

View File

@ -93,7 +93,7 @@ def joinfields(words, sep):
res = res + (sep + w) res = res + (sep + w)
return res[len(sep):] return res[len(sep):]
# Find substring # Find substring, raise exception if not found
index_error = 'substring not found in string.index' index_error = 'substring not found in string.index'
def index(s, sub, *args): def index(s, sub, *args):
if args: if args:
@ -107,7 +107,14 @@ def index(s, sub, *args):
while i < m: while i < m:
if sub == s[i:i+n]: return i if sub == s[i:i+n]: return i
i = i+1 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 # Convert string to integer
atoi_error = 'non-numeric argument to string.atoi' atoi_error = 'non-numeric argument to string.atoi'

View File

@ -93,7 +93,7 @@ def joinfields(words, sep):
res = res + (sep + w) res = res + (sep + w)
return res[len(sep):] return res[len(sep):]
# Find substring # Find substring, raise exception if not found
index_error = 'substring not found in string.index' index_error = 'substring not found in string.index'
def index(s, sub, *args): def index(s, sub, *args):
if args: if args:
@ -107,7 +107,14 @@ def index(s, sub, *args):
while i < m: while i < m:
if sub == s[i:i+n]: return i if sub == s[i:i+n]: return i
i = i+1 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 # Convert string to integer
atoi_error = 'non-numeric argument to string.atoi' atoi_error = 'non-numeric argument to string.atoi'

View File

@ -21,7 +21,7 @@ if not (1, 1): raise TestFailed, '(1, 1) is false instead of true'
if not [1]: raise TestFailed, '[1] is false instead of true' if not [1]: raise TestFailed, '[1] is false instead of true'
if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true' if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true'
def f(): pass def f(): pass
class C(): pass class C: pass
import sys import sys
x = C() x = C()
if not f: raise TestFailed, 'f is false instead of true' if not f: raise TestFailed, 'f is false instead of true'