FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

updated sys.path mangling · plotdevice/plotdevice@91e00bf · GitHub

Repository navigation

Commit 91e00bf

Browse files
committed
updated sys.path mangling
- the .run module is now responsible for adding PyObjC to the path (since we’re no longer using the system’s distribution of it) - both .lib and .run will recognize when they’re being imported from the source dist and adjust the sys.path accordingly
1 parent ba23488 commit 91e00bf

3 files changed

Lines changed: 44 additions & 37 deletions

File tree

‎plotdevice/__init__.py‎

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,35 @@
1818
All rights reserved.
1919
MIT Licensed (see README file for details)
2020
"""
21+
import sys, re, os, site
2122

2223
__version__ = '0.9.4'
2324
__author__ = 'Christian Swinehart'
24-
__email__ = "drafting@samizdat.cc"
25+
__email__ = 'drafting@samizdat.cc'
2526
__credits__ = 'Frederik De Bleser, Tom De Smedt, Just van Rossum, & Marcos Ojeda'
2627
__license__ = 'MIT'
2728

28-
29-
# add the shared directory (for Libraries) to the path
30-
import sys, re, os
31-
sys.path.append(os.path.join(os.getenv('HOME'), 'Library', 'Application Support', 'PlotDevice'))
32-
33-
# add the Extras directory to sys.path since every module depends on PyObjC and friends
34-
try:
35-
import objc
36-
except ImportError:
37-
extras = '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python'
38-
sys.path.extend([extras, '%s/PyObjC'%extras])
39-
import objc
40-
41-
# print python exceptions to the console rather than silently failing
42-
objc.setVerbose(True)
43-
44-
# the global non-conflicting token (fingers crossed)
45-
INTERNAL = '_p_l_o_t_d_e_v_i_c_e_'
46-
47-
# please excuse our technical difficulties
48-
class DeviceError(Exception):
49-
pass
50-
51-
# special exception to cleanly exit animations
52-
class Halted(Exception):
53-
pass
29+
INTERNAL = '_p_l_o_t_d_e_v_i_c_e_' # the global non-conflicting token (fingers crossed)
30+
class DeviceError(Exception): pass # please excuse our technical difficulties
31+
class Halted(Exception): pass # special exception to cleanly exit animations
5432

5533
# note whether the module is being used within the .app, via console.py, or from the repl
5634
called_from = getattr(sys.modules['__main__'], '__file__', '<interactive>')
5735
is_windowed = bool(re.search(r'plotdevice(-app|/run/console)\.py$', called_from))
5836
in_setup = bool(called_from.endswith('setup.py')) # (for builds)
5937

60-
# populate the namespace (or don't) accordingly
38+
# don't mess with sys.path during builds
39+
if not in_setup:
40+
# use the bundled copy of PyObjC rather than the system version
41+
from .run import objc
42+
43+
# add the shared directory (for Libraries) to the path
44+
sys.path.append(os.path.join(os.getenv('HOME'), 'Library', 'Application Support', 'PlotDevice'))
45+
46+
# print python exceptions to the console rather than silently failing
47+
objc.setVerbose(True)
48+
49+
# populate the namespace (or don't) depending on the context
6150
if is_windowed or in_setup:
6251
# if a script imports * from within the app/tool, nothing should be (re-)added to the
6352
# global namespace. we'll let the Sandbox handle populating the namespace instead.

‎plotdevice/lib/__init__.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# do some special-case handling when the module is imported from within the source dist
55
# (determined by checking the existence project files at a known path). if we're in the
66
# source dist, add the build dir to the path so we can pick up the .so files,
7-
module_root = abspath(join(abspath(dirname(__file__)), '../..'))
8-
if exists(join(module_root, 'app/PlotDevice-Info.plist')):
9-
so_dir = join(module_root, 'build/lib/plotdevice/lib')
7+
dist_root = abspath(join(abspath(dirname(__file__)), '../..'))
8+
if exists(join(dist_root, 'app/PlotDevice-Info.plist')):
9+
so_dir = join(dist_root, 'build/lib/plotdevice/lib')
1010
sys.path.append(so_dir)
1111
if not exists(so_dir):
12-
unbuilt = 'Build the plotdevice module with `python setup.pt build\' before attempting import it.'
12+
unbuilt = 'Build the plotdevice module with `python setup.py build\' before attempting import it.'
1313
raise RuntimeError(unbuilt)
1414

1515
# test the sys.path by attempting to load the c-extensions

‎plotdevice/run/__init__.py‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import linecache, re
2-
from sys import exc_info
3-
from os.path import abspath, dirname, relpath
1+
import re, sys, site, linecache
2+
from os.path import abspath, dirname, relpath, exists, join
43
from traceback import format_list, format_exception_only
5-
from AppKit import NSBundle
4+
5+
# add our embedded PyObjC site-dir to the sys.path
6+
dist_root = abspath(join(dirname(__file__), '../..'))
7+
if exists(join(dist_root, 'app/PlotDevice-Info.plist')):
8+
# if run from the sdist, look in the build dir...
9+
objc_dir = join(dist_root, 'build/lib/plotdevice/lib/PyObjC')
10+
else:
11+
# ...otherwise find the modules in .lib
12+
objc_dir = abspath(join(dirname(__file__), '../lib/PyObjC'))
13+
map(sys.path.remove, filter(lambda p:p.endswith('PyObjC'), sys.path))
14+
site.addsitedir(objc_dir)
15+
16+
# test the sys.path by attempting to load a PyObjC submodule
17+
try:
18+
import objc
19+
except:
20+
unbuilt = 'Build the plotdevice module with `python setup.py build\' before attempting import it.'
21+
raise RuntimeError(unbuilt)
22+
623

724
def encoding(src):
825
"""Searches the first two lines of a string looking for an `# encoding: ???` comment."""
@@ -51,7 +68,7 @@ def stacktrace(script=None, src=None):
5168
def coredump(script=None, src=None, syntax=True):
5269
"""Get a clean stacktrace with absolute paths and source pulled from the editor rather than disk"""
5370
# use the most recently caught exception
54-
etype, value, tb = exc_info()
71+
etype, value, tb = sys.exc_info()
5572
script = script or '<Untitled>' if src else None
5673
frames = extract_tb(tb, script, src)
5774

@@ -85,6 +102,7 @@ def extract_tb(tb, script=None, src=None):
85102
tb = tb.tb_next
86103

87104
# omit the internal plotdevice stack frames in `dist` builds
105+
from AppKit import NSBundle
88106
debug = 'flux' in NSBundle.mainBundle().infoDictionary().get('CFBundleVersion','')
89107
if not debug:
90108
moduledir = abspath(dirname(dirname(__file__)))

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL