From 140065b5804d945ec528dc45eb53848f14d60b5f Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 26 Nov 2015 00:23:01 +0000 Subject: [PATCH] Make poezio.poezio_shlex more Cython-friendly. --- poezio/common.py | 6 +++--- poezio/poezio_shlex.py | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/poezio/common.py b/poezio/common.py index a62c83f1..30a12daa 100644 --- a/poezio/common.py +++ b/poezio/common.py @@ -12,6 +12,7 @@ Various useful functions. from sys import version_info from datetime import datetime, timedelta from slixmpp import JID, InvalidJID +from poezio_shlex import shlex import base64 import os @@ -20,7 +21,6 @@ import hashlib import subprocess import time import string -import poezio_shlex as shlex # Needed to avoid datetime.datetime.timestamp() @@ -294,7 +294,7 @@ def shell_split(st): >>> shell_split('"sdf 1" "toto 2"') ['sdf 1', 'toto 2'] """ - sh = shlex.shlex(st) + sh = shlex(st) ret = [] w = sh.get_token() 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 a string with possibly quoted text. """ - sh = shlex.shlex(text) + sh = shlex(text) count = -1 w = sh.get_token() while w and w[2] is not None: diff --git a/poezio/poezio_shlex.py b/poezio/poezio_shlex.py index 032baeee..7072d10d 100644 --- a/poezio/poezio_shlex.py +++ b/poezio/poezio_shlex.py @@ -110,13 +110,13 @@ class shlex(object): if self.debug >= 3: print("shlex: in state", repr(self.state), \ "I see character:", repr(nextchar)) - if self.state is None: + if self.state == '\0': self.token = '' # past end of file token_end = self.instream.tell() break elif self.state == ' ': if not nextchar: - self.state = None # end of file + self.state = '\0' # end of file token_end = self.instream.tell() break elif nextchar in self.whitespace: @@ -131,7 +131,7 @@ class shlex(object): token_start = self.instream.tell() - 1 self.token = nextchar self.state = 'a' - elif nextchar in self.quotes: + elif nextchar == self.quotes: token_start = self.instream.tell() - 1 self.state = nextchar elif self.whitespace_split: @@ -146,7 +146,7 @@ class shlex(object): break # emit current token else: continue - elif self.state in self.quotes: + elif self.state == self.quotes: quoted = True if not nextchar: # end of file if self.debug >= 2: @@ -162,13 +162,13 @@ class shlex(object): break else: self.state = 'a' - elif self.posix and nextchar in self.escape and \ - self.state in self.escapedquotes: + elif self.posix and nextchar == self.escape and \ + self.state == self.escapedquotes: escapedstate = self.state self.state = nextchar else: self.token = self.token + nextchar - elif self.state in self.escape: + elif self.state == self.escape: if not nextchar: # end of file if self.debug >= 2: print("shlex: I see EOF in escape state") @@ -176,13 +176,13 @@ class shlex(object): token_end = self.instream.tell() break # 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 + nextchar self.state = escapedstate elif self.state == 'a': if not nextchar: - self.state = None # end of file + self.state = '\0' # end of file token_end = self.instream.tell() break elif nextchar in self.whitespace: @@ -194,7 +194,7 @@ class shlex(object): break # emit current token else: continue - elif nextchar in self.wordchars or nextchar in self.quotes \ + elif nextchar in self.wordchars or nextchar == self.quotes \ or self.whitespace_split: self.token = self.token + nextchar else: @@ -227,11 +227,11 @@ class shlex(object): newfile = os.path.join(os.path.dirname(self.infile), newfile) 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." if infile is None: infile = self.infile - if lineno is None: + if lineno == 0: lineno = self.lineno return "\"%s\", line %d: " % (infile, lineno)