Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52fc1f6

Browse files
committed
* calendar.py: minor cleanups
* ftplib.py: support __init__ with optional host, port args * aifc.py: ensure header is written on close even when no data is written
1 parent 234f942 commit 52fc1f6

3 files changed

Lines changed: 49 additions & 26 deletions

File tree

Lib/aifc.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ def initfp(self, file):
619619
self._nframes = 0
620620
self._nframeswritten = 0
621621
self._datawritten = 0
622+
self._datalength = 0
622623
self._markers = []
623624
self._marklength = 0
624625
self._aifc = 1 # AIFF-C is default
@@ -743,19 +744,7 @@ def getmarkers(self):
743744
return self._markers
744745

745746
def writeframesraw(self, data):
746-
if not self._nframeswritten:
747-
if self._comptype in ('ULAW', 'ALAW'):
748-
if not self._sampwidth:
749-
self._sampwidth = AL.SAMPLE_16
750-
if self._sampwidth != AL.SAMPLE_16:
751-
raise Error, 'sample width must be 2 when compressing with ULAW or ALAW'
752-
if not self._nchannels:
753-
raise Error, '# channels not specified'
754-
if not self._sampwidth:
755-
raise Error, 'sample width not specified'
756-
if not self._framerate:
757-
raise Error, 'sampling rate not specified'
758-
self._write_header(len(data))
747+
self._ensure_header_written(len(data))
759748
nframes = len(data) / (self._sampwidth * self._nchannels)
760749
if self._comp:
761750
dummy = self._comp.SetParam(CL.FRAME_BUFFER_SIZE, \
@@ -774,6 +763,7 @@ def writeframes(self, data):
774763
self._patchheader()
775764

776765
def close(self):
766+
self._ensure_header_written(0)
777767
if self._datawritten & 1:
778768
# quick pad to even size
779769
self._file.write(chr(0))
@@ -792,6 +782,21 @@ def close(self):
792782
#
793783
# Internal methods.
794784
#
785+
def _ensure_header_written(self, datasize):
786+
if not self._nframeswritten:
787+
if self._comptype in ('ULAW', 'ALAW'):
788+
if not self._sampwidth:
789+
self._sampwidth = AL.SAMPLE_16
790+
if self._sampwidth != AL.SAMPLE_16:
791+
raise Error, 'sample width must be 2 when compressing with ULAW or ALAW'
792+
if not self._nchannels:
793+
raise Error, '# channels not specified'
794+
if not self._sampwidth:
795+
raise Error, 'sample width not specified'
796+
if not self._framerate:
797+
raise Error, 'sampling rate not specified'
798+
self._write_header(datasize)
799+
795800
def _write_header(self, initlength):
796801
if self._aifc and self._comptype != 'NONE':
797802
try:

Lib/calendar.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
# - the order of the elements of a 'struct tm' differs, to ease sorting
1010
# - months numbers are 1-12, not 0-11; month arrays have a dummy element 0
1111
# - Monday is the first day of the week (numbered 0)
12+
# - years are expressed in full, e.g. 1970, not 70.
13+
# - timezone is currently hardcoded
14+
# - doesn't know about daylight saving time
1215

1316
# These are really parameters of the 'time' module:
1417
epoch = 1970 # Time began on January 1 of this year (00:00:00 UTC)
1518
day_0 = 3 # The epoch begins on a Thursday (Monday = 0)
1619

20+
# Localization: Minutes West from Greenwich
21+
timezone = -2*60 # Middle-European time with DST on
22+
# timezone = 5*60 # EST (sigh -- THINK time() doesn't return UTC)
23+
1724
# Return 1 for leap years, 0 for non-leap years
1825
def isleap(year):
1926
return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
@@ -97,10 +104,6 @@ def asctime(arg):
97104
s = s + ' ' + dd(`hours`) + ':' + dd(`mins`) + ':' + dd(`secs`)
98105
return s + ' ' + `year`
99106

100-
# Localization: Minutes West from Greenwich
101-
timezone = -2*60 # Middle-European time with DST on
102-
# timezone = 5*60 # EST (sigh -- THINK time() doesn't return UTC)
103-
104107
# Local time ignores DST issues for now -- adjust 'timezone' to fake it
105108
def localtime(secs):
106109
return gmtime(secs - timezone*60)

Lib/ftplib.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,31 @@
6868
# The class itself
6969
class FTP:
7070

71-
# Initialize an instance. Arguments:
72-
# - host: hostname to connect to
73-
# - port: port to connect to (default the standard FTP port)
74-
def init(self, host, *args):
75-
if len(args) > 1: raise TypeError, 'too many args'
76-
if args: port = args[0]
77-
else: port = FTP_PORT
78-
self.host = host
79-
self.port = port
71+
# New initialization method (called by class instantiation)
72+
# Initialize host to localhost, port to standard ftp port
73+
def __init__(self, *args):
74+
# Initialize the instance to something mostly harmless
8075
self.debugging = 0
76+
self.host = ''
77+
self.port = FTP_PORT
78+
self.sock = None
79+
self.file = None
80+
self.welcome = None
81+
if args:
82+
apply(self.connect, args)
83+
84+
# Old init method (explicitly called by caller)
85+
def init(self, *args):
86+
if args:
87+
apply(self.connect, args)
88+
89+
# Connect to host. Arguments:
90+
# - host: hostname to connect to (default previous host)
91+
# - port: port to connect to (default previous port)
92+
def init(self, *args):
93+
if args: self.host = args[0]
94+
if args[1:]: self.port = args[1]
95+
if args[2:]: raise TypeError, 'too many args'
8196
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8297
self.sock.connect(self.host, self.port)
8398
self.file = self.sock.makefile('r')

0 commit comments

Comments
 (0)