Commit 88dd4b1d authored by Ashwin Bharambe's avatar Ashwin Bharambe Committed by Facebook Github Bot

Prepare for python3 compatibility [1]

Summary:
This is a first step towards python3 compatibility. Specifically, it
tackles:

 - `cPickle`: uses six.moves
 - `Queue`: uses six.moves
 - `urllib2`: uses six.moves and changes `urllib2.urlopen` to `urllib.request.urlopen`

Also, fundamentally it changes the types of all config "byte" types to
"string" types. Those configurations aren't un-encoded byte streams but very
specifically ascii (or unicode encoded) strings which are specified and
consumed by human eyes.

Reviewed By: rbgirshick

Differential Revision: D9662024

fbshipit-source-id: b8372f685b57ec4260ae881a2f8bb7967f337b10
parent fd09e37a
This diff is collapsed.
...@@ -26,11 +26,11 @@ from __future__ import print_function ...@@ -26,11 +26,11 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import copy import copy
import cPickle as pickle
import logging import logging
import numpy as np import numpy as np
import os import os
import scipy.sparse import scipy.sparse
from six.moves import cPickle as pickle
# Must happen before importing COCO API (which imports matplotlib) # Must happen before importing COCO API (which imports matplotlib)
import detectron.utils.env as envu import detectron.utils.env as envu
......
...@@ -22,11 +22,11 @@ ...@@ -22,11 +22,11 @@
"""Python implementation of the PASCAL VOC devkit's AP evaluation code.""" """Python implementation of the PASCAL VOC devkit's AP evaluation code."""
import cPickle
import logging import logging
import numpy as np import numpy as np
import os import os
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from six.moves import cPickle
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
......
...@@ -44,11 +44,11 @@ from collections import deque ...@@ -44,11 +44,11 @@ from collections import deque
from collections import OrderedDict from collections import OrderedDict
import logging import logging
import numpy as np import numpy as np
import Queue
import signal import signal
import threading import threading
import time import time
import uuid import uuid
from six.moves import queue as Queue
from caffe2.python import core, workspace from caffe2.python import core, workspace
......
...@@ -28,9 +28,9 @@ from __future__ import division ...@@ -28,9 +28,9 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import cPickle as pickle
import cv2 import cv2
import numpy as np import numpy as np
from six.moves import cPickle as pickle
from caffe2.proto import caffe2_pb2 from caffe2.proto import caffe2_pb2
......
...@@ -22,9 +22,9 @@ from __future__ import unicode_literals ...@@ -22,9 +22,9 @@ from __future__ import unicode_literals
import contextlib import contextlib
import logging import logging
import Queue
import threading import threading
import traceback import traceback
from six.moves import queue as Queue
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
......
...@@ -20,13 +20,13 @@ from __future__ import division ...@@ -20,13 +20,13 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import cPickle as pickle
import hashlib import hashlib
import logging import logging
import os import os
import re import re
import sys import sys
import urllib2 from six.moves import cPickle as pickle
from six.moves import urllib
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -45,7 +45,9 @@ def cache_url(url_or_file, cache_dir): ...@@ -45,7 +45,9 @@ def cache_url(url_or_file, cache_dir):
path to the cached file. If the argument is not a URL, simply return it as path to the cached file. If the argument is not a URL, simply return it as
is. is.
""" """
is_url = re.match(r'^(?:http)s?://', url_or_file, re.IGNORECASE) is not None is_url = re.match(
r'^(?:http)s?://', url_or_file, re.IGNORECASE
) is not None
if not is_url: if not is_url:
return url_or_file return url_or_file
...@@ -111,7 +113,7 @@ def download_url( ...@@ -111,7 +113,7 @@ def download_url(
Credit: Credit:
https://stackoverflow.com/questions/2028517/python-urllib2-progress-hook https://stackoverflow.com/questions/2028517/python-urllib2-progress-hook
""" """
response = urllib2.urlopen(url) response = urllib.request.urlopen(url)
total_size = response.info().getheader('Content-Length').strip() total_size = response.info().getheader('Content-Length').strip()
total_size = int(total_size) total_size = int(total_size)
bytes_so_far = 0 bytes_so_far = 0
...@@ -140,5 +142,5 @@ def _get_file_md5sum(file_name): ...@@ -140,5 +142,5 @@ def _get_file_md5sum(file_name):
def _get_reference_md5sum(url): def _get_reference_md5sum(url):
"""By convention the md5 hash for url is stored in url + '.md5sum'.""" """By convention the md5 hash for url is stored in url + '.md5sum'."""
url_md5sum = url + '.md5sum' url_md5sum = url + '.md5sum'
md5sum = urllib2.urlopen(url_md5sum).read().strip() md5sum = urllib.request.urlopen(url_md5sum).read().strip()
return md5sum return md5sum
...@@ -21,12 +21,12 @@ from __future__ import print_function ...@@ -21,12 +21,12 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
from collections import OrderedDict from collections import OrderedDict
import cPickle as pickle
import logging import logging
import numpy as np import numpy as np
import os import os
import pprint import pprint
import yaml import yaml
from six.moves import cPickle as pickle
from caffe2.python import core from caffe2.python import core
from caffe2.python import workspace from caffe2.python import workspace
......
...@@ -27,7 +27,7 @@ import os ...@@ -27,7 +27,7 @@ import os
import yaml import yaml
import numpy as np import numpy as np
import subprocess import subprocess
import cPickle as pickle from six.moves import cPickle as pickle
from six.moves import shlex_quote from six.moves import shlex_quote
from detectron.core.config import cfg from detectron.core.config import cfg
......
...@@ -6,3 +6,4 @@ setuptools ...@@ -6,3 +6,4 @@ setuptools
Cython Cython
mock mock
scipy scipy
six
...@@ -9,10 +9,10 @@ from __future__ import print_function ...@@ -9,10 +9,10 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import argparse import argparse
import cPickle as pickle
import numpy as np import numpy as np
import os import os
import sys import sys
from six.moves import cPickle as pickle
import detectron.datasets.coco_to_cityscapes_id as cs import detectron.datasets.coco_to_cityscapes_id as cs
......
...@@ -24,10 +24,10 @@ from __future__ import division ...@@ -24,10 +24,10 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import cPickle as pickle
import numpy as np import numpy as np
import scipy.io as sio import scipy.io as sio
import sys import sys
from six.moves import cPickle as pickle
from detectron.datasets.json_dataset import JsonDataset from detectron.datasets.json_dataset import JsonDataset
......
...@@ -26,10 +26,10 @@ from __future__ import print_function ...@@ -26,10 +26,10 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import argparse import argparse
import cPickle as pickle
import numpy as np import numpy as np
import os import os
import sys import sys
from six.moves import cPickle as pickle
from caffe.proto import caffe_pb2 from caffe.proto import caffe_pb2
from caffe2.proto import caffe2_pb2 from caffe2.proto import caffe2_pb2
......
...@@ -31,10 +31,10 @@ from __future__ import print_function ...@@ -31,10 +31,10 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import argparse import argparse
import cPickle as pickle
import os import os
import sys import sys
import yaml import yaml
from six.moves import cPickle as pickle
from detectron.core.config import cfg from detectron.core.config import cfg
from detectron.datasets import task_evaluation from detectron.datasets import task_evaluation
......
...@@ -23,10 +23,10 @@ from __future__ import print_function ...@@ -23,10 +23,10 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import argparse import argparse
import cPickle as pickle
import cv2 import cv2
import os import os
import sys import sys
from six.moves import cPickle as pickle
from detectron.datasets.json_dataset import JsonDataset from detectron.datasets.json_dataset import JsonDataset
import detectron.utils.vis as vis_utils import detectron.utils.vis as vis_utils
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment