summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscoder <none@none>2008-06-19 18:36:50 +0200
committerscoder <none@none>2008-06-19 18:36:50 +0200
commit7c987435b7743abfe586413407a9571488a49f86 (patch)
treedc757fcea544baaf247eb0c95ff8a48be2a17286
parentbb2ec5ab52a61bc0032ff76223c9f5fb6e0dfbac (diff)
downloadpython-lxml-7c987435b7743abfe586413407a9571488a49f86.tar.gz
[svn r3831] r4511@delle: sbehnel | 2008-06-19 10:46:05 +0200
fixes for Py3k stdlib restructuring --HG-- branch : trunk
-rw-r--r--src/lxml/ElementInclude.py10
-rw-r--r--src/lxml/html/__init__.py14
-rw-r--r--src/lxml/html/clean.py8
-rw-r--r--src/lxml/html/tests/test_feedparser_data.py8
-rw-r--r--src/lxml/html/tests/test_forms.txt5
-rw-r--r--src/lxml/html/tests/test_rewritelinks.txt3
6 files changed, 34 insertions, 14 deletions
diff --git a/src/lxml/ElementInclude.py b/src/lxml/ElementInclude.py
index 471893fa..a0224bb3 100644
--- a/src/lxml/ElementInclude.py
+++ b/src/lxml/ElementInclude.py
@@ -52,12 +52,18 @@ form of custom URL resolvers.
from lxml import etree
import copy
-from urlparse import urljoin
-from urllib2 import urlopen
+try:
+ from urlparse import urljoin
+ from urllib2 import urlopen
+except ImportError:
+ # Python 3
+ from urllib.parse import urljoin
+ from urllib.request import urlopen
try:
set
except NameError:
+ # Python 2.3
from sets import Set as set
XINCLUDE = "{http://www.w3.org/2001/XInclude}"
diff --git a/src/lxml/html/__init__.py b/src/lxml/html/__init__.py
index ed662858..ae047aaf 100644
--- a/src/lxml/html/__init__.py
+++ b/src/lxml/html/__init__.py
@@ -3,7 +3,11 @@
import threading
import re
-import urlparse
+try:
+ from urlparse import urljoin
+except ImportError:
+ # Python 3
+ from urllib.parse import urljoin
import copy
from lxml import etree
from lxml.html import defs
@@ -269,7 +273,7 @@ class HtmlMixin(object):
if resolve_base_href:
self.resolve_base_href()
def link_repl(href):
- return urlparse.urljoin(base_url, href)
+ return urljoin(base_url, href)
self.rewrite_links(link_repl)
def resolve_base_href(self):
@@ -316,13 +320,13 @@ class HtmlMixin(object):
if attrib in attribs:
value = el.get(attrib)
if codebase is not None:
- value = urlparse.urljoin(codebase, value)
+ value = urljoin(codebase, value)
yield (el, attrib, value, 0)
if 'archive' in attribs:
for match in _archive_re.finditer(el.get('archive')):
value = match.group(0)
if codebase is not None:
- value = urlparse.urljoin(codebase, value)
+ value = urljoin(codebase, value)
yield (el, 'archive', value, match.start())
if tag == 'param':
valuetype = el.get('valuetype') or ''
@@ -751,7 +755,7 @@ class FormElement(HtmlElement):
base_url = self.base_url
action = self.get('action')
if base_url and action is not None:
- return urlparse.urljoin(base_url, action)
+ return urljoin(base_url, action)
else:
return action
def _action__set(self, value):
diff --git a/src/lxml/html/clean.py b/src/lxml/html/clean.py
index c5837e6f..ecb9d998 100644
--- a/src/lxml/html/clean.py
+++ b/src/lxml/html/clean.py
@@ -6,7 +6,11 @@ details.
import re
import copy
-import urlparse
+try:
+ from urlparse import urlsplit
+except ImportError:
+ # Python 3
+ from urllib.parse import urlsplit
from lxml import etree
from lxml.html import defs
from lxml.html import fromstring, tostring, XHTML_NAMESPACE
@@ -418,7 +422,7 @@ class Cleaner(object):
if (self.whitelist_tags is not None
and el.tag not in self.whitelist_tags):
return False
- scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
+ scheme, netloc, path, query, fragment = urlsplit(url)
netloc = netloc.lower().split(':', 1)[0]
if scheme not in ('http', 'https'):
return False
diff --git a/src/lxml/html/tests/test_feedparser_data.py b/src/lxml/html/tests/test_feedparser_data.py
index 3efc9d88..b1d94213 100644
--- a/src/lxml/html/tests/test_feedparser_data.py
+++ b/src/lxml/html/tests/test_feedparser_data.py
@@ -1,7 +1,11 @@
import sys
import os
import re
-import rfc822
+try:
+ from rfc822 import Message
+except ImportError:
+ # Python 3
+ from email import message_from_file as Message
import unittest
from lxml.tests.common_imports import doctest
if sys.version_info >= (2,4):
@@ -28,7 +32,7 @@ class FeedTestCase(unittest.TestCase):
def parse(self):
f = open(self.filename, 'r')
- headers = rfc822.Message(f)
+ headers = Message(f)
c = f.read()
f.close()
if not headers.keys():
diff --git a/src/lxml/html/tests/test_forms.txt b/src/lxml/html/tests/test_forms.txt
index 3f4f3144..11d67ae9 100644
--- a/src/lxml/html/tests/test_forms.txt
+++ b/src/lxml/html/tests/test_forms.txt
@@ -120,8 +120,9 @@ Traceback (most recent call last):
ValueError: There is no option with the value 'asdf'
>>> select.value_options
['1', '2', '3']
->>> import urllib
->>> print(urllib.urlencode(f.form_values()))
+>>> try: from urllib import urlencode
+... except ImportError: from urllib.parse import urlencode
+>>> print(urlencode(f.form_values()))
hidden_field=new+value&text_field=text_value&single_checkbox=on&single_checkbox2=good&check_group=1&check_group=2&check_group=3&textarea_field=some+text&select1=&select2=1&select2=2&select2=3
>>> fields = f.fields
>>> fields # doctest:+NOPARSE_MARKUP
diff --git a/src/lxml/html/tests/test_rewritelinks.txt b/src/lxml/html/tests/test_rewritelinks.txt
index 674fcb2e..b4cdd779 100644
--- a/src/lxml/html/tests/test_rewritelinks.txt
+++ b/src/lxml/html/tests/test_rewritelinks.txt
@@ -1,7 +1,8 @@
We'll define a link translation function:
>>> base_href = 'http://old/base/path.html'
- >>> import urlparse
+ >>> try: import urlparse
+ ... except ImportError: import urllib.parse as urlparse
>>> def relocate_href(link):
... link = urlparse.urljoin(base_href, link)
... if link.startswith('http://old'):