Skip to content

Commit

Permalink
(3.3->default) Issue python#18377: Code cleanup in Python Launcher
Browse files Browse the repository at this point in the history
This changeset fixes a number of compiler warnings in the Python Launcher
binary for OSX. It also cleans up whitespace usage in those sources.
  • Loading branch information
ronaldoussoren committed Jul 7, 2013
2 parents 5f8e785 + 4e327c9 commit 071029f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 95 deletions.
5 changes: 0 additions & 5 deletions Mac/PythonLauncher/FileSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,13 @@
+ (id)getFactorySettingsForFileType: (NSString *)filetype;
+ (id)newSettingsForFileType: (NSString *)filetype;

//- (id)init;
- (id)initForFileType: (NSString *)filetype;
- (id)initForFSDefaultFileType: (NSString *)filetype;
- (id)initForDefaultFileType: (NSString *)filetype;
//- (id)initWithFileSettings: (FileSettings *)source;

- (void)updateFromSource: (id <FileSettingsSource>)source;
- (NSString *)commandLineForScript: (NSString *)script;

//- (void)applyFactorySettingsForFileType: (NSString *)filetype;
//- (void)saveDefaults;
//- (void)applyUserDefaults: (NSString *)filetype;
- (void)applyValuesFromDict: (NSDictionary *)dict;
- (void)reset;
- (NSArray *) interpreters;
Expand Down
46 changes: 20 additions & 26 deletions Mac/PythonLauncher/FileSettings.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ + (id)getFactorySettingsForFileType: (NSString *)filetype
{
static FileSettings *fsdefault_py, *fsdefault_pyw, *fsdefault_pyc;
FileSettings **curdefault;

if ([filetype isEqualToString: @"Python Script"]) {
curdefault = &fsdefault_py;
} else if ([filetype isEqualToString: @"Python GUI Script"]) {
Expand All @@ -36,7 +36,7 @@ + (id)getDefaultsForFileType: (NSString *)filetype
{
static FileSettings *default_py, *default_pyw, *default_pyc;
FileSettings **curdefault;

if ([filetype isEqualToString: @"Python Script"]) {
curdefault = &default_py;
} else if ([filetype isEqualToString: @"Python GUI Script"]) {
Expand All @@ -57,7 +57,7 @@ + (id)getDefaultsForFileType: (NSString *)filetype
+ (id)newSettingsForFileType: (NSString *)filetype
{
FileSettings *cur;

cur = [FileSettings new];
[cur initForFileType: filetype];
return [cur retain];
Expand All @@ -67,7 +67,7 @@ - (id)initWithFileSettings: (FileSettings *)source
{
self = [super init];
if (!self) return self;

interpreter = [source->interpreter retain];
honourhashbang = source->honourhashbang;
debug = source->debug;
Expand All @@ -81,36 +81,30 @@ - (id)initWithFileSettings: (FileSettings *)source
with_terminal = source->with_terminal;
prefskey = source->prefskey;
if (prefskey) [prefskey retain];

return self;
}

- (id)initForFileType: (NSString *)filetype
{
FileSettings *defaults;

defaults = [FileSettings getDefaultsForFileType: filetype];
self = [self initWithFileSettings: defaults];
origsource = [defaults retain];
return self;
}

//- (id)init
//{
// self = [self initForFileType: @"Python Script"];
// return self;
//}

- (id)initForFSDefaultFileType: (NSString *)filetype
{
int i;
NSString *filename;
NSDictionary *dict;
static NSDictionary *factorySettings;

self = [super init];
if (!self) return self;

if (factorySettings == NULL) {
NSBundle *bdl = [NSBundle mainBundle];
NSString *path = [ bdl pathForResource: @"factorySettings"
Expand Down Expand Up @@ -149,18 +143,18 @@ - (void)applyUserDefaults: (NSString *)filetype
{
NSUserDefaults *defaults;
NSDictionary *dict;

defaults = [NSUserDefaults standardUserDefaults];
dict = [defaults dictionaryForKey: filetype];
if (!dict)
return;
[self applyValuesFromDict: dict];
}

- (id)initForDefaultFileType: (NSString *)filetype
{
FileSettings *fsdefaults;

fsdefaults = [FileSettings getFactorySettingsForFileType: filetype];
self = [self initWithFileSettings: fsdefaults];
if (!self) return self;
Expand Down Expand Up @@ -220,7 +214,7 @@ - (void)updateFromSource: (id <FileSettingsSource>)source
- (void)applyValuesFromDict: (NSDictionary *)dict
{
id value;

value = [dict objectForKey: @"interpreter"];
if (value) interpreter = [value retain];
value = [dict objectForKey: @"honourhashbang"];
Expand All @@ -247,12 +241,12 @@ - (void)applyValuesFromDict: (NSDictionary *)dict

- (NSString*)_replaceSingleQuotes: (NSString*)string
{
/* Replace all single-quotes by '"'"', that way shellquoting will
* be correct when the result value is delimited using single quotes.
*/
NSArray* components = [string componentsSeparatedByString:@"'"];
/* Replace all single-quotes by '"'"', that way shellquoting will
* be correct when the result value is delimited using single quotes.
*/
NSArray* components = [string componentsSeparatedByString:@"'"];

return [components componentsJoinedByString:@"'\"'\"'"];
return [components componentsJoinedByString:@"'\"'\"'"];
}

- (NSString *)commandLineForScript: (NSString *)script
Expand All @@ -265,7 +259,7 @@ - (NSString *)commandLineForScript: (NSString *)script

script_dir = [script substringToIndex:
[script length]-[[script lastPathComponent] length]];

if (honourhashbang &&
(fp=fopen([script fileSystemRepresentation], "r")) &&
fgets(hashbangbuf, sizeof(hashbangbuf), fp) &&
Expand All @@ -278,7 +272,7 @@ - (NSString *)commandLineForScript: (NSString *)script
}
if (!cur_interp)
cur_interp = interpreter;

return [NSString stringWithFormat:
@"cd '%@' && '%@'%s%s%s%s%s%s %@ '%@' %@ %s",
[self _replaceSingleQuotes:script_dir],
Expand All @@ -297,7 +291,7 @@ - (NSString *)commandLineForScript: (NSString *)script

- (NSArray *) interpreters { return interpreters;};

// FileSettingsSource protocol
// FileSettingsSource protocol
- (NSString *) interpreter { return interpreter;};
- (BOOL) honourhashbang { return honourhashbang; };
- (BOOL) debug { return debug;};
Expand Down
6 changes: 3 additions & 3 deletions Mac/PythonLauncher/MyAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification

- (BOOL)shouldShowUI
{
// if this call comes before applicationDidFinishLaunching: we
// if this call comes before applicationDidFinishLaunching: we
// should terminate immedeately after starting the script.
if (!initial_action_done)
should_terminate = YES;
Expand Down Expand Up @@ -62,7 +62,7 @@ - (void)testFileTypeBinding
static NSString *extensions[] = { @"py", @"pyw", @"pyc", NULL};
NSString **ext_p;
int i;

if ([[NSUserDefaults standardUserDefaults] boolForKey: @"SkipFileBindingTest"])
return;
ourUrl = [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
Expand Down Expand Up @@ -92,5 +92,5 @@ - (void)testFileTypeBinding
}
}
}

@end
20 changes: 8 additions & 12 deletions Mac/PythonLauncher/MyDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ - (id)init
{
self = [super init];
if (self) {

// Add your subclass-specific initialization here.
// If an error occurs here, send a [self dealloc] message and return nil.
script = [@"<no script>.py" retain];
Expand All @@ -37,20 +37,17 @@ - (void)close
{
NSApplication *app = [NSApplication sharedApplication];
[super close];
if ([[app delegate] shouldTerminate])
if ([(MyAppDelegate*)[app delegate] shouldTerminate])
[app terminate: self];
}

- (void)load_defaults
{
// if (settings) [settings release];
settings = [FileSettings newSettingsForFileType: filetype];
}

- (void)update_display
{
// [[self window] setTitle: script];

[interpreter setStringValue: [settings interpreter]];
[honourhashbang setState: [settings honourhashbang]];
[debug setState: [settings debug]];
Expand All @@ -62,7 +59,7 @@ - (void)update_display
[others setStringValue: [settings others]];
[scriptargs setStringValue: [settings scriptargs]];
[with_terminal setState: [settings with_terminal]];

[commandline setStringValue: [settings commandLineForScript: script]];
}

Expand All @@ -75,7 +72,7 @@ - (BOOL)run
{
const char *cmdline;
int sts;

cmdline = [[settings commandLineForScript: script] UTF8String];
if ([settings with_terminal]) {
sts = doscript(cmdline);
Expand Down Expand Up @@ -107,14 +104,13 @@ - (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
{
// Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
BOOL show_ui;
// ask the app delegate whether we should show the UI or not.
show_ui = [[[NSApplication sharedApplication] delegate] shouldShowUI];

// ask the app delegate whether we should show the UI or not.
show_ui = [(MyAppDelegate*)[[NSApplication sharedApplication] delegate] shouldShowUI];
[script release];
script = [fileName retain];
[filetype release];
filetype = [type retain];
// if (settings) [settings release];
settings = [FileSettings newSettingsForFileType: filetype];
if (show_ui) {
[self update_display];
Expand Down Expand Up @@ -152,7 +148,7 @@ - (IBAction)do_apply:(id)sender
[self update_display];
}

// FileSettingsSource protocol
// FileSettingsSource protocol
- (NSString *) interpreter { return [interpreter stringValue];};
- (BOOL) honourhashbang { return [honourhashbang state];};
- (BOOL) debug { return [debug state];};
Expand Down
11 changes: 4 additions & 7 deletions Mac/PythonLauncher/PreferencesWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ @implementation PreferencesWindowController
+ getPreferencesWindow
{
static PreferencesWindowController *_singleton;

if (!_singleton)
_singleton = [[PreferencesWindowController alloc] init];
[_singleton showWindow: _singleton];
Expand All @@ -21,15 +21,13 @@ - (id) init
- (void)load_defaults
{
NSString *title = [filetype titleOfSelectedItem];

settings = [FileSettings getDefaultsForFileType: title];
}

- (void)update_display
{
// [[self window] setTitle: script];

[interpreter reloadData];
[interpreter reloadData];
[interpreter setStringValue: [settings interpreter]];
[honourhashbang setState: [settings honourhashbang]];
[debug setState: [settings debug]];
Expand All @@ -41,7 +39,6 @@ - (void)update_display
[others setStringValue: [settings others]];
[with_terminal setState: [settings with_terminal]];
// Not scriptargs, it isn't for preferences

[commandline setStringValue: [settings commandLineForScript: @"<your script here>"]];
}

Expand Down Expand Up @@ -75,7 +72,7 @@ - (IBAction)do_apply:(id)sender
[self update_display];
}

// FileSettingsSource protocol
// FileSettingsSource protocol
- (NSString *) interpreter { return [interpreter stringValue];};
- (BOOL) honourhashbang { return [honourhashbang state]; };
- (BOOL) debug { return [debug state];};
Expand Down
2 changes: 1 addition & 1 deletion Mac/PythonLauncher/doscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

#include <Carbon/Carbon.h>

extern int doscript(const char *command);
extern int doscript(const char *command);
Loading

0 comments on commit 071029f

Please sign in to comment.