Make poezio.poezio_shlex more Cython-friendly.

This commit is contained in:
Emmanuel Gil Peyrot 2015-11-26 00:23:01 +00:00
parent d0df65ba7b
commit 140065b580
2 changed files with 15 additions and 15 deletions

View file

@ -12,6 +12,7 @@ Various useful functions.
from sys import version_info from sys import version_info
from datetime import datetime, timedelta from datetime import datetime, timedelta
from slixmpp import JID, InvalidJID from slixmpp import JID, InvalidJID
from poezio_shlex import shlex
import base64 import base64
import os import os
@ -20,7 +21,6 @@ import hashlib
import subprocess import subprocess
import time import time
import string import string
import poezio_shlex as shlex
# Needed to avoid datetime.datetime.timestamp() # Needed to avoid datetime.datetime.timestamp()
@ -294,7 +294,7 @@ def shell_split(st):
>>> shell_split('"sdf 1" "toto 2"') >>> shell_split('"sdf 1" "toto 2"')
['sdf 1', 'toto 2'] ['sdf 1', 'toto 2']
""" """
sh = shlex.shlex(st) sh = shlex(st)
ret = [] ret = []
w = sh.get_token() w = sh.get_token()
while w and w[2] is not None: while w and w[2] is not None:
@ -329,7 +329,7 @@ def find_argument_quoted(pos, text):
Get the number of the argument at position pos in Get the number of the argument at position pos in
a string with possibly quoted text. a string with possibly quoted text.
""" """
sh = shlex.shlex(text) sh = shlex(text)
count = -1 count = -1
w = sh.get_token() w = sh.get_token()
while w and w[2] is not None: while w and w[2] is not None:

View file

@ -110,13 +110,13 @@ class shlex(object):
if self.debug >= 3: if self.debug >= 3:
print("shlex: in state", repr(self.state), \ print("shlex: in state", repr(self.state), \
"I see character:", repr(nextchar)) "I see character:", repr(nextchar))
if self.state is None: if self.state == '\0':
self.token = '' # past end of file self.token = '' # past end of file
token_end = self.instream.tell() token_end = self.instream.tell()
break break
elif self.state == ' ': elif self.state == ' ':
if not nextchar: if not nextchar:
self.state = None # end of file self.state = '\0' # end of file
token_end = self.instream.tell() token_end = self.instream.tell()
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace:
@ -131,7 +131,7 @@ class shlex(object):
token_start = self.instream.tell() - 1 token_start = self.instream.tell() - 1
self.token = nextchar self.token = nextchar
self.state = 'a' self.state = 'a'
elif nextchar in self.quotes: elif nextchar == self.quotes:
token_start = self.instream.tell() - 1 token_start = self.instream.tell() - 1
self.state = nextchar self.state = nextchar
elif self.whitespace_split: elif self.whitespace_split:
@ -146,7 +146,7 @@ class shlex(object):
break # emit current token break # emit current token
else: else:
continue continue
elif self.state in self.quotes: elif self.state == self.quotes:
quoted = True quoted = True
if not nextchar: # end of file if not nextchar: # end of file
if self.debug >= 2: if self.debug >= 2:
@ -162,13 +162,13 @@ class shlex(object):
break break
else: else:
self.state = 'a' self.state = 'a'
elif self.posix and nextchar in self.escape and \ elif self.posix and nextchar == self.escape and \
self.state in self.escapedquotes: self.state == self.escapedquotes:
escapedstate = self.state escapedstate = self.state
self.state = nextchar self.state = nextchar
else: else:
self.token = self.token + nextchar self.token = self.token + nextchar
elif self.state in self.escape: elif self.state == self.escape:
if not nextchar: # end of file if not nextchar: # end of file
if self.debug >= 2: if self.debug >= 2:
print("shlex: I see EOF in escape state") print("shlex: I see EOF in escape state")
@ -176,13 +176,13 @@ class shlex(object):
token_end = self.instream.tell() token_end = self.instream.tell()
break break
# only the quote may be escaped # only the quote may be escaped
if escapedstate in self.quotes and nextchar != escapedstate: if escapedstate == self.quotes and nextchar != escapedstate:
self.token = self.token + self.state self.token = self.token + self.state
self.token = self.token + nextchar self.token = self.token + nextchar
self.state = escapedstate self.state = escapedstate
elif self.state == 'a': elif self.state == 'a':
if not nextchar: if not nextchar:
self.state = None # end of file self.state = '\0' # end of file
token_end = self.instream.tell() token_end = self.instream.tell()
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace:
@ -194,7 +194,7 @@ class shlex(object):
break # emit current token break # emit current token
else: else:
continue continue
elif nextchar in self.wordchars or nextchar in self.quotes \ elif nextchar in self.wordchars or nextchar == self.quotes \
or self.whitespace_split: or self.whitespace_split:
self.token = self.token + nextchar self.token = self.token + nextchar
else: else:
@ -227,11 +227,11 @@ class shlex(object):
newfile = os.path.join(os.path.dirname(self.infile), newfile) newfile = os.path.join(os.path.dirname(self.infile), newfile)
return (newfile, open(newfile, "r")) return (newfile, open(newfile, "r"))
def error_leader(self, infile=None, lineno=None): def error_leader(self, infile=None, lineno=0):
"Emit a C-compiler-like, Emacs-friendly error-message leader." "Emit a C-compiler-like, Emacs-friendly error-message leader."
if infile is None: if infile is None:
infile = self.infile infile = self.infile
if lineno is None: if lineno == 0:
lineno = self.lineno lineno = self.lineno
return "\"%s\", line %d: " % (infile, lineno) return "\"%s\", line %d: " % (infile, lineno)