 	;; if we landed inside a string, go to the beginning of that
 	;; string. this handles triple quoted, multi-line spanning
 	;; strings.
-	(let ((skip (nth 3 (parse-partial-sexp bod (point)))))
-	  (while skip
-	    (py-safe (search-backward (make-string 1 skip)))
-	    (setq skip (nth 3 (parse-partial-sexp bod (point))))))
+	(let* ((pps3 (nth 3 (parse-partial-sexp bod (point))))
+	       (delim (and pps3 (int-to-char pps3)))
+	       (skip (and delim (make-string 1 delim))))
+	  (when skip
+	    (save-excursion
+	      (py-safe (search-backward skip))
+	      (if (and (eq (char-before) delim)
+		       (eq (char-before (1- (point))) delim))
+		  (setq skip (make-string 3 delim))))
+	    ;; we're looking at a triple-quoted string
+	    (py-safe (search-backward skip))))
 	;; now skip backward over continued lines
 	(py-goto-initial-line)
 	(+ (current-indentation)

	(re-search-backward py-parse-state-re nil 'move)
	(setq ci (current-indentation))
	(beginning-of-line)
	(save-excursion
	  (setq pps (parse-partial-sexp (point) here)))
	;; make sure we don't land inside a triple-quoted string
	(setq done (or ;(zerop ci)
		       (not (nth 3 pps))
		       (bobp)))
	)
      pps)))

;; if point is at a non-zero nesting level, returns the number of the
;; character that opens the smallest enclosing unclosed list; else
;; returns nil.
(defun py-nesting-level ()
  (let ((status (py-parse-state)) )
    (if (zerop (car status))
	nil				; not in a nest
      (car (cdr status)))))		; char# of open bracket


;; Copyright (C) 1992,1993,1994  Tim Peters

;; Author: 1995-1998 Barry A. Warsaw
;;         1992-1994 Tim Peters
;; Maintainer: python-mode@python.org
;; Created:    Feb 1992
