Frank Smit avatar Frank Smit committed 6cf8d86

If the file object was opened in detect_encoding, close it there too.

Comments (0)

Files changed (1)

 from io import StringIO, BytesIO, TextIOWrapper, BufferedReader
 # the default encoding to use when encoding cannot be detected
 default_encoding = 'utf-8'
 # }}}
 # function pofile() {{{
-def pofile(pofile, **kwargs):
+def pofile(po_object, wrapwidth=78, encoding=None,
+           check_for_duplicates=False, klass=None):
+    file_class = klass or POFile
+    if encoding is None:
+        encoding = detect_encoding(po_object)
+def _pofile(pofile, **kwargs):
     Convenience function that parses the po or pot file ``pofile`` and returns
     a :class:`~polib.POFile` instance.
     re_u = re.compile(u(PATTERN))
     re_b = re.compile(b(PATTERN))
+    close_file = False
     if isinstance(translation_file, str):  # Only unicode string
+        close_file = True
         if '\n' not in translation_file and os.path.exists(translation_file):
             # It's a file path, check if MO or PO and open
             if translation_file.endswith('.mo'):
     except LookupError:
         charset = default_encoding
-    translation_file.close()
+    # If the file object was opened here, close it
+    # If not, set the position back to the beginning of the file
+    if close_file:
+        translation_file.close()
+    else:
     return charset
 # }}}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.