Skip to content

Commit

Permalink
py3k: Use relative imports
Browse files Browse the repository at this point in the history
In py3k, imports are absolute unless using the "from . import" syntax.

This commit also solves a recursive import between Image, ImageColor, and
ImagePalette by delay-importing ImagePalette in Image.

I'm not too keen on this commit because the syntax is ugly. I might go back
and prefer the prettier "from PIL import".
  • Loading branch information
Brian Crowell authored and Brian Crowell committed Jan 10, 2013
1 parent abd215e commit 83ff0b3
Show file tree
Hide file tree
Showing 68 changed files with 104 additions and 99 deletions.
4 changes: 2 additions & 2 deletions PIL/ArgImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

__version__ = "0.4"

import Image, ImageFile, ImagePalette
from . import Image, ImageFile, ImagePalette

from PngImagePlugin import i16, i32, ChunkStream, _MODES
from .PngImagePlugin import i16, i32, ChunkStream, _MODES

MAGIC = "\212ARG\r\n\032\n"

Expand Down
4 changes: 2 additions & 2 deletions PIL/BdfFontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# See the README file for information on usage and redistribution.
#

import Image
import FontFile
from . import Image
from . import FontFile


# --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
__version__ = "0.7"


import Image, ImageFile, ImagePalette
from . import Image, ImageFile, ImagePalette


#
Expand Down
2 changes: 1 addition & 1 deletion PIL/BufrStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageFile
from . import Image, ImageFile

_handler = None

Expand Down
2 changes: 1 addition & 1 deletion PIL/CurImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

__version__ = "0.1"

import Image, BmpImagePlugin
from . import Image, BmpImagePlugin


#
Expand Down
4 changes: 2 additions & 2 deletions PIL/DcxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

__version__ = "0.2"

import Image
from . import Image

from PcxImagePlugin import PcxImageFile
from .PcxImagePlugin import PcxImageFile

MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?

Expand Down
2 changes: 1 addition & 1 deletion PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
__version__ = "0.5"

import re
import Image, ImageFile
from . import Image, ImageFile

#
# --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion PIL/FitsStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageFile
from . import Image, ImageFile

_handler = None

Expand Down
2 changes: 1 addition & 1 deletion PIL/FliImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

__version__ = "0.2"

import Image, ImageFile, ImagePalette
from . import Image, ImageFile, ImagePalette


def i16(c):
Expand Down
2 changes: 1 addition & 1 deletion PIL/FontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

import os
import Image
from . import Image

import marshal

Expand Down
4 changes: 2 additions & 2 deletions PIL/FpxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
__version__ = "0.1"


import Image, ImageFile
from OleFileIO import *
from . import Image, ImageFile
from .OleFileIO import *


# we map from colour field tuples to (mode, rawmode) descriptors
Expand Down
2 changes: 1 addition & 1 deletion PIL/GbrImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageFile
from . import Image, ImageFile

def i32(c):
return ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16) + (ord(c[0])<<24L)
Expand Down
2 changes: 1 addition & 1 deletion PIL/GdImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

__version__ = "0.1"

import ImageFile, ImagePalette
from . import ImageFile, ImagePalette

def i16(c):
return ord(c[1]) + (ord(c[0])<<8)
Expand Down
2 changes: 1 addition & 1 deletion PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
__version__ = "0.9"


import Image, ImageFile, ImagePalette
from . import Image, ImageFile, ImagePalette


# --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion PIL/GribStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageFile
from . import Image, ImageFile

_handler = None

Expand Down
2 changes: 1 addition & 1 deletion PIL/Hdf5StubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageFile
from . import Image, ImageFile

_handler = None

Expand Down
2 changes: 1 addition & 1 deletion PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageFile
from . import Image, ImageFile
import struct

HEADERSIZE = 8
Expand Down
2 changes: 1 addition & 1 deletion PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

__version__ = "0.1"

import Image, BmpImagePlugin
from . import Image, BmpImagePlugin


#
Expand Down
2 changes: 1 addition & 1 deletion PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
__version__ = "0.7"

import re
import Image, ImageFile, ImagePalette
from . import Image, ImageFile, ImagePalette


# --------------------------------------------------------------------
Expand Down
23 changes: 12 additions & 11 deletions PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def __getattr__(self, id):
RuntimeWarning
)

import ImageMode
import ImagePalette
from . import ImageMode

import os, sys

Expand Down Expand Up @@ -295,23 +294,23 @@ def preinit():
return

try:
import BmpImagePlugin
from . import BmpImagePlugin
except ImportError:
pass
try:
import GifImagePlugin
from . import GifImagePlugin
except ImportError:
pass
try:
import JpegImagePlugin
from . import JpegImagePlugin
except ImportError:
pass
try:
import PpmImagePlugin
from . import PpmImagePlugin
except ImportError:
pass
try:
import PngImagePlugin
from . import PngImagePlugin
except ImportError:
pass
# try:
Expand Down Expand Up @@ -464,6 +463,7 @@ def _new(self, im):
new.size = im.size
new.palette = self.palette
if im.mode == "P":
from . import ImagePalette
new.palette = ImagePalette.ImagePalette()
try:
new.info = self.info.copy()
Expand Down Expand Up @@ -1014,7 +1014,7 @@ def offset(self, xoffset, yoffset=None):
"'offset' is deprecated; use 'ImageChops.offset' instead",
DeprecationWarning, stacklevel=2
)
import ImageChops
from . import ImageChops
return ImageChops.offset(self, xoffset, yoffset)

##
Expand Down Expand Up @@ -1081,7 +1081,7 @@ def paste(self, im, box=None, mask=None):
box = box + (box[0]+size[0], box[1]+size[1])

if isStringType(im):
import ImageColor
from . import ImageColor
im = ImageColor.getcolor(im, self.mode)

elif isImageType(im):
Expand Down Expand Up @@ -1227,6 +1227,7 @@ def putdata(self, data, scale=1.0, offset=0.0):

def putpalette(self, data, rawmode="RGB"):
"Put palette data into an image."
from . import ImagePalette

if self.mode not in ("L", "P"):
raise ValueError("illegal image mode")
Expand Down Expand Up @@ -1758,7 +1759,7 @@ def new(mode, size, color=0):
if isStringType(color):
# css3-style specifier

import ImageColor
from . import ImageColor
color = ImageColor.getcolor(color, mode)

return Image()._new(core.fill(mode, size, color))
Expand Down Expand Up @@ -2139,5 +2140,5 @@ def _show(image, **options):
apply(_showxv, (image,), options)

def _showxv(image, title=None, **options):
import ImageShow
from . import ImageShow
apply(ImageShow.show, (image, title), options)
2 changes: 1 addition & 1 deletion PIL/ImageChops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# See the README file for information on usage and redistribution.
#

import Image
from . import Image

##
# The <b>ImageChops</b> module contains a number of arithmetical image
Expand Down
6 changes: 3 additions & 3 deletions PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@

# --------------------------------------------------------------------.

import Image
from . import Image
import _imagingcms

core = _imagingcms
Expand Down Expand Up @@ -207,7 +207,7 @@ def apply_in_place(self, im):
def get_display_profile(handle=None):
import sys
if sys.platform == "win32":
import ImageWin
from . import ImageWin
if isinstance(handle, ImageWin.HDC):
profile = core.get_display_profile_win32(handle, 1)
else:
Expand Down Expand Up @@ -771,7 +771,7 @@ def versions():
if __name__ == "__main__":
# create a cheap manual from the __doc__ strings for the functions above

import ImageCms
from . import ImageCms
print(__doc__)

for f in dir(pyCMS):
Expand Down
2 changes: 1 addition & 1 deletion PIL/ImageColor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# See the README file for information on usage and redistribution.
#

import Image
from . import Image
import re


Expand Down
6 changes: 3 additions & 3 deletions PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageColor
from . import Image, ImageColor

try:
import warnings
Expand Down Expand Up @@ -127,7 +127,7 @@ def setfont(self, font):
def getfont(self):
if not self.font:
# FIXME: should add a font repository
import ImageFont
from . import ImageFont
self.font = ImageFont.load_default()
return self.font

Expand Down Expand Up @@ -318,7 +318,7 @@ def getdraw(im=None, hints=None):
except ImportError:
pass
if handler is None:
import ImageDraw2
from . import ImageDraw2
handler = ImageDraw2
if im:
im = handler.Draw(im)
Expand Down
2 changes: 1 addition & 1 deletion PIL/ImageDraw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageColor, ImageDraw, ImageFont, ImagePath
from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath

class Pen:
def __init__(self, color, width=1, opacity=255):
Expand Down
2 changes: 1 addition & 1 deletion PIL/ImageEnhance.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# See the README file for information on usage and redistribution.
#

import Image, ImageFilter, ImageStat
from . import Image, ImageFilter, ImageStat

class _Enhance:

Expand Down
2 changes: 1 addition & 1 deletion PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# See the README file for information on usage and redistribution.
#

import Image
from . import Image
import traceback, os
import io

Expand Down
2 changes: 1 addition & 1 deletion PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from __future__ import print_function

import Image
from . import Image
import os, sys

class _imagingft_not_installed:
Expand Down
5 changes: 3 additions & 2 deletions PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# See the README file for information on usage and redistribution.
#

import Image
from . import Image

##
# (New in 1.1.3) The <b>ImageGrab</b> module can be used to copy
Expand Down Expand Up @@ -66,6 +66,7 @@ def grabclipboard():
debug = 0 # temporary interface
data = Image.core.grabclipboard(debug)
if Image.isStringType(data):
import BmpImagePlugin, StringIO
from . import BmpImagePlugin
import StringIO
return BmpImagePlugin.DibImageFile(StringIO.StringIO(data))
return data
Loading

0 comments on commit 83ff0b3

Please sign in to comment.