Skip to content

Commit 3e33434

Browse files
committed
count *nix/windows newline chars properly for PyBasic file I/O
1 parent 5718dcb commit 3e33434

File tree

6 files changed

+50
-11
lines changed

6 files changed

+50
-11
lines changed

PyBasic/basicparser.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,23 @@ def __openstmt(self):
680680
raise RuntimeError('File '+filename+' could not be opened in line ' + str(self.__line_number))
681681

682682
if accessMode == "r+":
683+
if hasattr(self.__file_handles,'newlines'):
684+
try:
685+
self.__file_handles[filenum].readline()
686+
except:
687+
pass
688+
newlines = self.__file_handles.newlines
689+
else:
690+
newlines = None
683691
self.__file_handles[filenum].seek(0)
684692
filelen = 0
685693
for lines in self.__file_handles[filenum]:
686-
filelen += (len(lines)+(0 if uname()[0].upper() == 'LINUX' or \
687-
implementation.name.upper() in ['MICROPYTHON','CIRCUITPYTHON'] else 1))
694+
filelen += len(lines)
695+
if newlines != None:
696+
filelen += len(newlines)-1
697+
else:
698+
filelen += (0 if uname()[0].upper() == 'LINUX' or \
699+
implementation.name.upper() in ['MICROPYTHON','CIRCUITPYTHON'] else 1)
688700

689701
self.__file_handles[filenum].seek(filelen)
690702

PyBasic/program.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,26 @@ def load(self, file, tmpfile):
146146

147147
try:
148148
infile = open(file, 'r')
149+
if hasattr(infile,'newlines'):
150+
try:
151+
infile.readline()
152+
except:
153+
pass
154+
newlines = infile.newlines
155+
infile.seek(0)
156+
else:
157+
newlines = None
149158
fIndex = 0
150159
fOffset = 0
151160
pgmLoad = False
152161
if file.split(".")[-1].upper() == "PGM":
153162
pgmLoad = True
154163
for fileLine in infile:
155-
fOffset += (len(fileLine) + (0 if self.__imp == 'X' else 1))
164+
fOffset += len(fileLine)
165+
if newlines != None:
166+
fOffset += len(newlines)-1
167+
elif self.__imp != 'X':
168+
fOffset += 1
156169
if len(fileLine) >= 9 and fileLine[0:9] == "-999,-999":
157170
break
158171

@@ -173,8 +186,11 @@ def load(self, file, tmpfile):
173186
if fileLine.strip().upper()[fileLine.strip().find(' '):].strip()[:4] == "DATA":
174187
self.__data.addData(line_number,fIndex)
175188
#self.add_stmt(Lexer().tokenize((fileLine.replace("\n","")).replace("\r","")),fIndex+fOffset,tmpfile)
176-
fIndex += (len(fileLine) + (0 if self.__imp == 'X' else 1))
177-
189+
fIndex += len(fileLine)
190+
if newlines != None:
191+
fIndex += len(newlines)-1
192+
elif self.__imp != 'X':
193+
fIndex += 1
178194

179195
except OSError:
180196
print("Could not read file")
@@ -204,10 +220,22 @@ def add_stmt(self, tokenlist, fIndex, tmpfile):
204220
if tokenlist[1].lexeme == "DATA":
205221
self.__data.addData(line_number,fIndex)
206222
else:
223+
if hasattr(tmpfile,'newlines'):
224+
try:
225+
tmpfile.readline()
226+
except:
227+
pass
228+
newlines = tmpfile.newlines
229+
else:
230+
newlines = None
207231
tmpfile.seek(0)
208232
filelen = 0
209233
for lines in tmpfile:
210-
filelen += (len(lines)+(0 if self.__imp == 'X' else 1))
234+
filelen += len(lines)
235+
if newlines != None:
236+
filelen += len(newlines) - 1
237+
elif self.__imp != 'X':
238+
filelen += 1
211239

212240
self.__program[line_number] = -(filelen+1)
213241
if tokenlist[1].lexeme == "DATA":

PyDOS.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def PyDOS():
7373
global envVars
7474
if "envVars" not in globals().keys():
7575
envVars = {}
76-
_VER = "1.44"
76+
_VER = "1.45"
7777
prmpVals = ['>','(',')','&','|','\x1b','\b','<','=',' ',_VER,'\n','$','']
7878

7979
print("Starting Py-DOS...")

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ down - the number of tiles connected down the matrix display
200200

201201
If the parameters are omitted or not properly formatted, the program will prompt for each of the values.
202202

203-
**Playimage.py [filename[,filename2,filename3,etc,[seconds_to_display]]]** - (Circuitpython only, requires the adafruit_imageload library installed in the /lib folder) program to display .bmp, .jpg, .gif (incl animaged) or .png image files. If multiple comma
203+
**Playimage.py [filename[,filename2,filename3,etc[],seconds_to_display]]]** - (Circuitpython only, requires the adafruit_imageload library installed in the /lib folder) program to display .bmp, .jpg, .gif (incl animated) or .png image files. If multiple comma
204204
seperated files are entered a continous slide show is displayed with each image being
205205
displayed for `seconds_to_display` seconds. Wildcard's in the format of *.xxx can be used
206-
as an input filename. If the program is loaded from PyDOS it attempts to determine the appropriate display configuration from the PyDOS environment, otherwise several display options are supported and selected depending on the existence of BOARD.Display or locally installed display libraries.
206+
as an input filename. If the program is loaded from PyDOS it attempts to determine the appropriate display configuration from the PyDOS environment, otherwise several display options are supported and selected depending on the existence of BOARD.DISPLAY or locally installed display libraries.
207207

208208
**reboot.py** - performs a soft reboot (Micropython requires a Ctrl-D to complete)
209209

lib/pydos_bcfg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from machine import Pin
4848
try:
4949
led = "LED"
50-
test = machine.Pin(led,Pin.OUT)
50+
test = Pin(led,Pin.OUT)
5151
except:
5252
led = "D13"
5353

lib/pydos_ui.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def serial_bytes_available(self,timeout=1):
3232
retval = 0
3333
else:
3434
retval = 1
35-
retval = 1 if retval else 0
3635

3736
return retval
3837

0 commit comments

Comments
 (0)